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
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"
29 #include "sysemu/device_tree.h"
30 #include "sysemu/qtest.h"
34 #if defined(TARGET_RISCV32)
35 # define KERNEL_BOOT_ADDRESS 0x80400000
37 # define KERNEL_BOOT_ADDRESS 0x80200000
40 void riscv_find_and_load_firmware(MachineState
*machine
,
41 const char *default_machine_firmware
,
42 hwaddr firmware_load_addr
,
45 char *firmware_filename
= NULL
;
47 if ((!machine
->firmware
) || (!strcmp(machine
->firmware
, "default"))) {
49 * The user didn't specify -bios, or has specified "-bios default".
50 * That means we are going to load the OpenSBI binary included in
53 firmware_filename
= riscv_find_firmware(default_machine_firmware
);
54 } else if (strcmp(machine
->firmware
, "none")) {
55 firmware_filename
= riscv_find_firmware(machine
->firmware
);
58 if (firmware_filename
) {
59 /* If not "none" load the firmware */
60 riscv_load_firmware(firmware_filename
, firmware_load_addr
, sym_cb
);
61 g_free(firmware_filename
);
65 char *riscv_find_firmware(const char *firmware_filename
)
69 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, firmware_filename
);
70 if (filename
== NULL
) {
71 if (!qtest_enabled()) {
73 * We only ship plain binary bios images in the QEMU source.
74 * With Spike machine that uses ELF images as the default bios,
75 * running QEMU test will complain hence let's suppress the error
76 * report for QEMU testing.
78 error_report("Unable to load the RISC-V firmware \"%s\"",
87 target_ulong
riscv_load_firmware(const char *firmware_filename
,
88 hwaddr firmware_load_addr
,
91 uint64_t firmware_entry
, firmware_start
, firmware_end
;
93 if (load_elf_ram_sym(firmware_filename
, NULL
, NULL
, NULL
,
94 &firmware_entry
, &firmware_start
, &firmware_end
, NULL
,
95 0, EM_RISCV
, 1, 0, NULL
, true, sym_cb
) > 0) {
96 return firmware_entry
;
99 if (load_image_targphys_as(firmware_filename
, firmware_load_addr
,
100 ram_size
, NULL
) > 0) {
101 return firmware_load_addr
;
104 error_report("could not load firmware '%s'", firmware_filename
);
108 target_ulong
riscv_load_kernel(const char *kernel_filename
, symbol_fn_t sym_cb
)
110 uint64_t kernel_entry
, kernel_high
;
112 if (load_elf_ram_sym(kernel_filename
, NULL
, NULL
, NULL
,
113 &kernel_entry
, NULL
, &kernel_high
, NULL
, 0,
114 EM_RISCV
, 1, 0, NULL
, true, sym_cb
) > 0) {
118 if (load_uimage_as(kernel_filename
, &kernel_entry
, NULL
, NULL
,
119 NULL
, NULL
, NULL
) > 0) {
123 if (load_image_targphys_as(kernel_filename
, KERNEL_BOOT_ADDRESS
,
124 ram_size
, NULL
) > 0) {
125 return KERNEL_BOOT_ADDRESS
;
128 error_report("could not load kernel '%s'", kernel_filename
);
132 hwaddr
riscv_load_initrd(const char *filename
, uint64_t mem_size
,
133 uint64_t kernel_entry
, hwaddr
*start
)
138 * We want to put the initrd far enough into RAM that when the
139 * kernel is uncompressed it will not clobber the initrd. However
140 * on boards without much RAM we must ensure that we still leave
141 * enough room for a decent sized initrd, and on boards with large
142 * amounts of RAM we must avoid the initrd being so far up in RAM
143 * that it is outside lowmem and inaccessible to the kernel.
144 * So for boards with less than 256MB of RAM we put the initrd
145 * halfway into RAM, and for boards with 256MB of RAM or more we put
146 * the initrd at 128MB.
148 *start
= kernel_entry
+ MIN(mem_size
/ 2, 128 * MiB
);
150 size
= load_ramdisk(filename
, *start
, mem_size
- *start
);
152 size
= load_image_targphys(filename
, *start
, mem_size
- *start
);
154 error_report("could not load ramdisk '%s'", filename
);
159 return *start
+ size
;
162 void riscv_setup_rom_reset_vec(hwaddr start_addr
, hwaddr rom_base
,
163 hwaddr rom_size
, void *fdt
)
168 uint32_t reset_vec
[8] = {
169 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */
170 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */
171 0xf1402573, /* csrr a0, mhartid */
172 #if defined(TARGET_RISCV32)
173 0x0182a283, /* lw t0, 24(t0) */
174 #elif defined(TARGET_RISCV64)
175 0x0182b283, /* ld t0, 24(t0) */
177 0x00028067, /* jr t0 */
179 start_addr
, /* start: .dword */
184 /* copy in the reset vector in little_endian byte order */
185 for (i
= 0; i
< sizeof(reset_vec
) >> 2; i
++) {
186 reset_vec
[i
] = cpu_to_le32(reset_vec
[i
]);
188 rom_add_blob_fixed_as("mrom.reset", reset_vec
, sizeof(reset_vec
),
189 rom_base
, &address_space_memory
);
191 /* copy in the device tree */
192 if (fdt_pack(fdt
) || fdt_totalsize(fdt
) >
193 rom_size
- sizeof(reset_vec
)) {
194 error_report("not enough space to store device-tree");
197 qemu_fdt_dumpdtb(fdt
, fdt_totalsize(fdt
));
198 rom_add_blob_fixed_as("mrom.fdt", fdt
, fdt_totalsize(fdt
),
199 rom_base
+ sizeof(reset_vec
),
200 &address_space_memory
);