1 /* 2 * QEMU RISC-V Boot Helper 3 * 4 * Copyright (c) 2017 SiFive, Inc. 5 * Copyright (c) 2019 Alistair Francis <alistair.francis@wdc.com> 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms and conditions of the GNU General Public License, 9 * version 2 or later, as published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 * more details. 15 * 16 * You should have received a copy of the GNU General Public License along with 17 * this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "qemu/osdep.h" 21 #include "qemu-common.h" 22 #include "qemu/units.h" 23 #include "qemu/error-report.h" 24 #include "exec/cpu-defs.h" 25 #include "hw/loader.h" 26 #include "hw/riscv/boot.h" 27 #include "hw/boards.h" 28 #include "elf.h" 29 30 #if defined(TARGET_RISCV32) 31 # define KERNEL_BOOT_ADDRESS 0x80400000 32 #else 33 # define KERNEL_BOOT_ADDRESS 0x80200000 34 #endif 35 36 void riscv_find_and_load_firmware(MachineState *machine, 37 const char *default_machine_firmware, 38 hwaddr firmware_load_addr) 39 { 40 char *firmware_filename; 41 42 if (!machine->firmware) { 43 /* 44 * The user didn't specify -bios. 45 * At the moment we default to loading nothing when this hapens. 46 * In the future this defaul will change to loading the prebuilt 47 * OpenSBI firmware. Let's warn the user and then continue. 48 */ 49 warn_report("No -bios option specified. Not loading a firmware."); 50 warn_report("This default will change in QEMU 4.3. Please use the " \ 51 "-bios option to aviod breakages when this happens."); 52 warn_report("See QEMU's deprecation documentation for details"); 53 return; 54 } 55 56 if (!strcmp(machine->firmware, "default")) { 57 /* 58 * The user has specified "-bios default". That means we are going to 59 * load the OpenSBI binary included in the QEMU source. 60 * 61 * We can't load the binary by default as it will break existing users 62 * as users are already loading their own firmware. 63 * 64 * Let's try to get everyone to specify the -bios option at all times, 65 * so then in the future we can make "-bios default" the default option 66 * if no -bios option is set without breaking anything. 67 */ 68 firmware_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, 69 default_machine_firmware); 70 if (firmware_filename == NULL) { 71 error_report("Unable to load the default RISC-V firmware \"%s\"", 72 default_machine_firmware); 73 exit(1); 74 } 75 } else { 76 firmware_filename = machine->firmware; 77 } 78 79 if (strcmp(firmware_filename, "none")) { 80 /* If not "none" load the firmware */ 81 riscv_load_firmware(firmware_filename, firmware_load_addr); 82 } 83 84 if (!strcmp(machine->firmware, "default")) { 85 g_free(firmware_filename); 86 } 87 } 88 89 target_ulong riscv_load_firmware(const char *firmware_filename, 90 hwaddr firmware_load_addr) 91 { 92 uint64_t firmware_entry, firmware_start, firmware_end; 93 94 if (load_elf(firmware_filename, NULL, NULL, NULL, &firmware_entry, 95 &firmware_start, &firmware_end, 0, EM_RISCV, 1, 0) > 0) { 96 return firmware_entry; 97 } 98 99 if (load_image_targphys_as(firmware_filename, firmware_load_addr, 100 ram_size, NULL) > 0) { 101 return firmware_load_addr; 102 } 103 104 error_report("could not load firmware '%s'", firmware_filename); 105 exit(1); 106 } 107 108 target_ulong riscv_load_kernel(const char *kernel_filename) 109 { 110 uint64_t kernel_entry, kernel_high; 111 112 if (load_elf(kernel_filename, NULL, NULL, NULL, 113 &kernel_entry, NULL, &kernel_high, 0, EM_RISCV, 1, 0) > 0) { 114 return kernel_entry; 115 } 116 117 if (load_uimage_as(kernel_filename, &kernel_entry, NULL, NULL, 118 NULL, NULL, NULL) > 0) { 119 return kernel_entry; 120 } 121 122 if (load_image_targphys_as(kernel_filename, KERNEL_BOOT_ADDRESS, 123 ram_size, NULL) > 0) { 124 return KERNEL_BOOT_ADDRESS; 125 } 126 127 error_report("could not load kernel '%s'", kernel_filename); 128 exit(1); 129 } 130 131 hwaddr riscv_load_initrd(const char *filename, uint64_t mem_size, 132 uint64_t kernel_entry, hwaddr *start) 133 { 134 int size; 135 136 /* 137 * We want to put the initrd far enough into RAM that when the 138 * kernel is uncompressed it will not clobber the initrd. However 139 * on boards without much RAM we must ensure that we still leave 140 * enough room for a decent sized initrd, and on boards with large 141 * amounts of RAM we must avoid the initrd being so far up in RAM 142 * that it is outside lowmem and inaccessible to the kernel. 143 * So for boards with less than 256MB of RAM we put the initrd 144 * halfway into RAM, and for boards with 256MB of RAM or more we put 145 * the initrd at 128MB. 146 */ 147 *start = kernel_entry + MIN(mem_size / 2, 128 * MiB); 148 149 size = load_ramdisk(filename, *start, mem_size - *start); 150 if (size == -1) { 151 size = load_image_targphys(filename, *start, mem_size - *start); 152 if (size == -1) { 153 error_report("could not load ramdisk '%s'", filename); 154 exit(1); 155 } 156 } 157 158 return *start + size; 159 } 160