]> git.proxmox.com Git - mirror_qemu.git/blob - hw/riscv/boot.c
3df802380a36b87b12fcb4613e613f78c85d53e9
[mirror_qemu.git] / hw / riscv / boot.c
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/device_tree.h"
30 #include "sysemu/qtest.h"
31
32 #include <libfdt.h>
33
34 #if defined(TARGET_RISCV32)
35 # define KERNEL_BOOT_ADDRESS 0x80400000
36 #else
37 # define KERNEL_BOOT_ADDRESS 0x80200000
38 #endif
39
40 void riscv_find_and_load_firmware(MachineState *machine,
41 const char *default_machine_firmware,
42 hwaddr firmware_load_addr,
43 symbol_fn_t sym_cb)
44 {
45 char *firmware_filename = NULL;
46
47 if ((!machine->firmware) || (!strcmp(machine->firmware, "default"))) {
48 /*
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
51 * the QEMU source.
52 */
53 firmware_filename = riscv_find_firmware(default_machine_firmware);
54 } else if (strcmp(machine->firmware, "none")) {
55 firmware_filename = riscv_find_firmware(machine->firmware);
56 }
57
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);
62 }
63 }
64
65 char *riscv_find_firmware(const char *firmware_filename)
66 {
67 char *filename;
68
69 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, firmware_filename);
70 if (filename == NULL) {
71 if (!qtest_enabled()) {
72 /*
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.
77 */
78 error_report("Unable to load the RISC-V firmware \"%s\"",
79 firmware_filename);
80 exit(1);
81 }
82 }
83
84 return filename;
85 }
86
87 target_ulong riscv_load_firmware(const char *firmware_filename,
88 hwaddr firmware_load_addr,
89 symbol_fn_t sym_cb)
90 {
91 uint64_t firmware_entry, firmware_start, firmware_end;
92
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;
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, symbol_fn_t sym_cb)
109 {
110 uint64_t kernel_entry, kernel_high;
111
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) {
115 return kernel_entry;
116 }
117
118 if (load_uimage_as(kernel_filename, &kernel_entry, NULL, NULL,
119 NULL, NULL, NULL) > 0) {
120 return kernel_entry;
121 }
122
123 if (load_image_targphys_as(kernel_filename, KERNEL_BOOT_ADDRESS,
124 ram_size, NULL) > 0) {
125 return KERNEL_BOOT_ADDRESS;
126 }
127
128 error_report("could not load kernel '%s'", kernel_filename);
129 exit(1);
130 }
131
132 hwaddr riscv_load_initrd(const char *filename, uint64_t mem_size,
133 uint64_t kernel_entry, hwaddr *start)
134 {
135 int size;
136
137 /*
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.
147 */
148 *start = kernel_entry + MIN(mem_size / 2, 128 * MiB);
149
150 size = load_ramdisk(filename, *start, mem_size - *start);
151 if (size == -1) {
152 size = load_image_targphys(filename, *start, mem_size - *start);
153 if (size == -1) {
154 error_report("could not load ramdisk '%s'", filename);
155 exit(1);
156 }
157 }
158
159 return *start + size;
160 }
161
162 void riscv_setup_rom_reset_vec(hwaddr start_addr, hwaddr rom_base,
163 hwaddr rom_size, void *fdt)
164 {
165 int i;
166
167 /* reset vector */
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) */
176 #endif
177 0x00028067, /* jr t0 */
178 0x00000000,
179 start_addr, /* start: .dword */
180 0x00000000,
181 /* dtb: */
182 };
183
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]);
187 }
188 rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
189 rom_base, &address_space_memory);
190
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");
195 exit(1);
196 }
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);
201
202 return;
203 }