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/boards.h" 26 #include "hw/loader.h" 27 #include "hw/riscv/boot.h" 28 #include "elf.h" 29 #include "sysemu/qtest.h" 30 31 #if defined(TARGET_RISCV32) 32 # define KERNEL_BOOT_ADDRESS 0x80400000 33 #else 34 # define KERNEL_BOOT_ADDRESS 0x80200000 35 #endif 36 37 void riscv_find_and_load_firmware(MachineState *machine, 38 const char *default_machine_firmware, 39 hwaddr firmware_load_addr, 40 symbol_fn_t sym_cb) 41 { 42 char *firmware_filename = NULL; 43 44 if ((!machine->firmware) || (!strcmp(machine->firmware, "default"))) { 45 /* 46 * The user didn't specify -bios, or has specified "-bios default". 47 * That means we are going to load the OpenSBI binary included in 48 * the QEMU source. 49 */ 50 firmware_filename = riscv_find_firmware(default_machine_firmware); 51 } else if (strcmp(machine->firmware, "none")) { 52 firmware_filename = riscv_find_firmware(machine->firmware); 53 } 54 55 if (firmware_filename) { 56 /* If not "none" load the firmware */ 57 riscv_load_firmware(firmware_filename, firmware_load_addr, sym_cb); 58 g_free(firmware_filename); 59 } 60 } 61 62 char *riscv_find_firmware(const char *firmware_filename) 63 { 64 char *filename; 65 66 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, firmware_filename); 67 if (filename == NULL) { 68 if (!qtest_enabled()) { 69 /* 70 * We only ship plain binary bios images in the QEMU source. 71 * With Spike machine that uses ELF images as the default bios, 72 * running QEMU test will complain hence let's suppress the error 73 * report for QEMU testing. 74 */ 75 error_report("Unable to load the RISC-V firmware \"%s\"", 76 firmware_filename); 77 exit(1); 78 } 79 } 80 81 return filename; 82 } 83 84 target_ulong riscv_load_firmware(const char *firmware_filename, 85 hwaddr firmware_load_addr, 86 symbol_fn_t sym_cb) 87 { 88 uint64_t firmware_entry, firmware_start, firmware_end; 89 90 if (load_elf_ram_sym(firmware_filename, NULL, NULL, NULL, 91 &firmware_entry, &firmware_start, &firmware_end, NULL, 92 0, EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) { 93 return firmware_entry; 94 } 95 96 if (load_image_targphys_as(firmware_filename, firmware_load_addr, 97 ram_size, NULL) > 0) { 98 return firmware_load_addr; 99 } 100 101 error_report("could not load firmware '%s'", firmware_filename); 102 exit(1); 103 } 104 105 target_ulong riscv_load_kernel(const char *kernel_filename, symbol_fn_t sym_cb) 106 { 107 uint64_t kernel_entry, kernel_high; 108 109 if (load_elf_ram_sym(kernel_filename, NULL, NULL, NULL, 110 &kernel_entry, NULL, &kernel_high, NULL, 0, 111 EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) { 112 return kernel_entry; 113 } 114 115 if (load_uimage_as(kernel_filename, &kernel_entry, NULL, NULL, 116 NULL, NULL, NULL) > 0) { 117 return kernel_entry; 118 } 119 120 if (load_image_targphys_as(kernel_filename, KERNEL_BOOT_ADDRESS, 121 ram_size, NULL) > 0) { 122 return KERNEL_BOOT_ADDRESS; 123 } 124 125 error_report("could not load kernel '%s'", kernel_filename); 126 exit(1); 127 } 128 129 hwaddr riscv_load_initrd(const char *filename, uint64_t mem_size, 130 uint64_t kernel_entry, hwaddr *start) 131 { 132 int size; 133 134 /* 135 * We want to put the initrd far enough into RAM that when the 136 * kernel is uncompressed it will not clobber the initrd. However 137 * on boards without much RAM we must ensure that we still leave 138 * enough room for a decent sized initrd, and on boards with large 139 * amounts of RAM we must avoid the initrd being so far up in RAM 140 * that it is outside lowmem and inaccessible to the kernel. 141 * So for boards with less than 256MB of RAM we put the initrd 142 * halfway into RAM, and for boards with 256MB of RAM or more we put 143 * the initrd at 128MB. 144 */ 145 *start = kernel_entry + MIN(mem_size / 2, 128 * MiB); 146 147 size = load_ramdisk(filename, *start, mem_size - *start); 148 if (size == -1) { 149 size = load_image_targphys(filename, *start, mem_size - *start); 150 if (size == -1) { 151 error_report("could not load ramdisk '%s'", filename); 152 exit(1); 153 } 154 } 155 156 return *start + size; 157 } 158