riscv: Suppress the error report for QEMU testing with riscv_find_firmware()
[qemu/ar7.git] / hw / riscv / boot.c
blobda5817d43895ac5282b96091b373cf7ba473c528
1 /*
2 * QEMU RISC-V Boot Helper
4 * Copyright (c) 2017 SiFive, Inc.
5 * Copyright (c) 2019 Alistair Francis <alistair.francis@wdc.com>
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.
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.
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/>.
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"
31 #if defined(TARGET_RISCV32)
32 # define KERNEL_BOOT_ADDRESS 0x80400000
33 #else
34 # define KERNEL_BOOT_ADDRESS 0x80200000
35 #endif
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)
42 char *firmware_filename = NULL;
44 if (!machine->firmware) {
46 * The user didn't specify -bios.
47 * At the moment we default to loading nothing when this hapens.
48 * In the future this defaul will change to loading the prebuilt
49 * OpenSBI firmware. Let's warn the user and then continue.
51 if (!qtest_enabled()) {
52 warn_report("No -bios option specified. Not loading a firmware.");
53 warn_report("This default will change in a future QEMU release. " \
54 "Please use the -bios option to avoid breakages when "\
55 "this happens.");
56 warn_report("See QEMU's deprecation documentation for details.");
58 return;
61 if (!strcmp(machine->firmware, "default")) {
63 * The user has specified "-bios default". That means we are going to
64 * load the OpenSBI binary included in the QEMU source.
66 * We can't load the binary by default as it will break existing users
67 * as users are already loading their own firmware.
69 * Let's try to get everyone to specify the -bios option at all times,
70 * so then in the future we can make "-bios default" the default option
71 * if no -bios option is set without breaking anything.
73 firmware_filename = riscv_find_firmware(default_machine_firmware);
74 } else if (strcmp(machine->firmware, "none")) {
75 firmware_filename = riscv_find_firmware(machine->firmware);
78 if (firmware_filename) {
79 /* If not "none" load the firmware */
80 riscv_load_firmware(firmware_filename, firmware_load_addr, sym_cb);
81 g_free(firmware_filename);
85 char *riscv_find_firmware(const char *firmware_filename)
87 char *filename;
89 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, firmware_filename);
90 if (filename == NULL) {
91 if (!qtest_enabled()) {
93 * We only ship plain binary bios images in the QEMU source.
94 * With Spike machine that uses ELF images as the default bios,
95 * running QEMU test will complain hence let's suppress the error
96 * report for QEMU testing.
98 error_report("Unable to load the RISC-V firmware \"%s\"",
99 firmware_filename);
100 exit(1);
104 return filename;
107 target_ulong riscv_load_firmware(const char *firmware_filename,
108 hwaddr firmware_load_addr,
109 symbol_fn_t sym_cb)
111 uint64_t firmware_entry, firmware_start, firmware_end;
113 if (load_elf_ram_sym(firmware_filename, NULL, NULL, NULL,
114 &firmware_entry, &firmware_start, &firmware_end, NULL,
115 0, EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) {
116 return firmware_entry;
119 if (load_image_targphys_as(firmware_filename, firmware_load_addr,
120 ram_size, NULL) > 0) {
121 return firmware_load_addr;
124 error_report("could not load firmware '%s'", firmware_filename);
125 exit(1);
128 target_ulong riscv_load_kernel(const char *kernel_filename, symbol_fn_t sym_cb)
130 uint64_t kernel_entry, kernel_high;
132 if (load_elf_ram_sym(kernel_filename, NULL, NULL, NULL,
133 &kernel_entry, NULL, &kernel_high, NULL, 0,
134 EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) {
135 return kernel_entry;
138 if (load_uimage_as(kernel_filename, &kernel_entry, NULL, NULL,
139 NULL, NULL, NULL) > 0) {
140 return kernel_entry;
143 if (load_image_targphys_as(kernel_filename, KERNEL_BOOT_ADDRESS,
144 ram_size, NULL) > 0) {
145 return KERNEL_BOOT_ADDRESS;
148 error_report("could not load kernel '%s'", kernel_filename);
149 exit(1);
152 hwaddr riscv_load_initrd(const char *filename, uint64_t mem_size,
153 uint64_t kernel_entry, hwaddr *start)
155 int size;
158 * We want to put the initrd far enough into RAM that when the
159 * kernel is uncompressed it will not clobber the initrd. However
160 * on boards without much RAM we must ensure that we still leave
161 * enough room for a decent sized initrd, and on boards with large
162 * amounts of RAM we must avoid the initrd being so far up in RAM
163 * that it is outside lowmem and inaccessible to the kernel.
164 * So for boards with less than 256MB of RAM we put the initrd
165 * halfway into RAM, and for boards with 256MB of RAM or more we put
166 * the initrd at 128MB.
168 *start = kernel_entry + MIN(mem_size / 2, 128 * MiB);
170 size = load_ramdisk(filename, *start, mem_size - *start);
171 if (size == -1) {
172 size = load_image_targphys(filename, *start, mem_size - *start);
173 if (size == -1) {
174 error_report("could not load ramdisk '%s'", filename);
175 exit(1);
179 return *start + size;