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/units.h"
22 #include "qemu/error-report.h"
23 #include "exec/cpu-defs.h"
24 #include "hw/loader.h"
25 #include "hw/riscv/boot.h"
26 #include "hw/boards.h"
29 #if defined(TARGET_RISCV32)
30 # define KERNEL_BOOT_ADDRESS 0x80400000
32 # define KERNEL_BOOT_ADDRESS 0x80200000
35 target_ulong
riscv_load_firmware(const char *firmware_filename
,
36 hwaddr firmware_load_addr
)
38 uint64_t firmware_entry
, firmware_start
, firmware_end
;
40 if (load_elf(firmware_filename
, NULL
, NULL
, NULL
, &firmware_entry
,
41 &firmware_start
, &firmware_end
, 0, EM_RISCV
, 1, 0) > 0) {
42 return firmware_entry
;
45 if (load_image_targphys_as(firmware_filename
, firmware_load_addr
,
46 ram_size
, NULL
) > 0) {
47 return firmware_load_addr
;
50 error_report("could not load firmware '%s'", firmware_filename
);
54 target_ulong
riscv_load_kernel(const char *kernel_filename
)
56 uint64_t kernel_entry
, kernel_high
;
58 if (load_elf(kernel_filename
, NULL
, NULL
, NULL
,
59 &kernel_entry
, NULL
, &kernel_high
, 0, EM_RISCV
, 1, 0) > 0) {
63 if (load_uimage_as(kernel_filename
, &kernel_entry
, NULL
, NULL
,
64 NULL
, NULL
, NULL
) > 0) {
68 if (load_image_targphys_as(kernel_filename
, KERNEL_BOOT_ADDRESS
,
69 ram_size
, NULL
) > 0) {
70 return KERNEL_BOOT_ADDRESS
;
73 error_report("could not load kernel '%s'", kernel_filename
);
77 hwaddr
riscv_load_initrd(const char *filename
, uint64_t mem_size
,
78 uint64_t kernel_entry
, hwaddr
*start
)
83 * We want to put the initrd far enough into RAM that when the
84 * kernel is uncompressed it will not clobber the initrd. However
85 * on boards without much RAM we must ensure that we still leave
86 * enough room for a decent sized initrd, and on boards with large
87 * amounts of RAM we must avoid the initrd being so far up in RAM
88 * that it is outside lowmem and inaccessible to the kernel.
89 * So for boards with less than 256MB of RAM we put the initrd
90 * halfway into RAM, and for boards with 256MB of RAM or more we put
91 * the initrd at 128MB.
93 *start
= kernel_entry
+ MIN(mem_size
/ 2, 128 * MiB
);
95 size
= load_ramdisk(filename
, *start
, mem_size
- *start
);
97 size
= load_image_targphys(filename
, *start
, mem_size
- *start
);
99 error_report("could not load ramdisk '%s'", filename
);
104 return *start
+ size
;