util/bufferiszero: Improve scalar variant
[qemu/armbru.git] / hw / openrisc / boot.c
blob55475aa6d608be6c3b5c4eda82ab9e24f186235d
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
4 * QEMU OpenRISC boot helpers.
6 * (c) 2022 Stafford Horne <shorne@gmail.com>
7 */
9 #include "qemu/osdep.h"
10 #include "cpu.h"
11 #include "exec/cpu-defs.h"
12 #include "elf.h"
13 #include "hw/loader.h"
14 #include "hw/openrisc/boot.h"
15 #include "sysemu/device_tree.h"
16 #include "sysemu/qtest.h"
17 #include "sysemu/reset.h"
18 #include "qemu/error-report.h"
20 #include <libfdt.h>
22 #define KERNEL_LOAD_ADDR 0x100
24 hwaddr openrisc_load_kernel(ram_addr_t ram_size,
25 const char *kernel_filename,
26 uint32_t *bootstrap_pc)
28 long kernel_size;
29 uint64_t elf_entry;
30 uint64_t high_addr;
31 hwaddr entry;
33 if (kernel_filename && !qtest_enabled()) {
34 kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
35 &elf_entry, NULL, &high_addr, NULL, 1,
36 EM_OPENRISC, 1, 0);
37 entry = elf_entry;
38 if (kernel_size < 0) {
39 kernel_size = load_uimage(kernel_filename,
40 &entry, NULL, NULL, NULL, NULL);
41 high_addr = entry + kernel_size;
43 if (kernel_size < 0) {
44 kernel_size = load_image_targphys(kernel_filename,
45 KERNEL_LOAD_ADDR,
46 ram_size - KERNEL_LOAD_ADDR);
47 high_addr = KERNEL_LOAD_ADDR + kernel_size;
50 if (entry <= 0) {
51 entry = KERNEL_LOAD_ADDR;
54 if (kernel_size < 0) {
55 error_report("couldn't load the kernel '%s'", kernel_filename);
56 exit(1);
58 *bootstrap_pc = entry;
60 return high_addr;
62 return 0;
65 hwaddr openrisc_load_initrd(void *fdt, const char *filename,
66 hwaddr load_start, uint64_t mem_size)
68 int size;
69 hwaddr start;
71 /* We put the initrd right after the kernel; page aligned. */
72 start = TARGET_PAGE_ALIGN(load_start);
74 size = load_ramdisk(filename, start, mem_size - start);
75 if (size < 0) {
76 size = load_image_targphys(filename, start, mem_size - start);
77 if (size < 0) {
78 error_report("could not load ramdisk '%s'", filename);
79 exit(1);
83 if (fdt) {
84 qemu_fdt_setprop_cell(fdt, "/chosen",
85 "linux,initrd-start", start);
86 qemu_fdt_setprop_cell(fdt, "/chosen",
87 "linux,initrd-end", start + size);
90 return start + size;
93 uint32_t openrisc_load_fdt(void *fdt, hwaddr load_start,
94 uint64_t mem_size)
96 uint32_t fdt_addr;
97 int ret;
98 int fdtsize = fdt_totalsize(fdt);
100 if (fdtsize <= 0) {
101 error_report("invalid device-tree");
102 exit(1);
105 /* We put fdt right after the kernel and/or initrd. */
106 fdt_addr = TARGET_PAGE_ALIGN(load_start);
108 ret = fdt_pack(fdt);
109 /* Should only fail if we've built a corrupted tree */
110 g_assert(ret == 0);
111 /* copy in the device tree */
112 qemu_fdt_dumpdtb(fdt, fdtsize);
114 rom_add_blob_fixed_as("fdt", fdt, fdtsize, fdt_addr,
115 &address_space_memory);
116 qemu_register_reset_nosnapshotload(qemu_fdt_randomize_seeds,
117 rom_ptr_for_as(&address_space_memory, fdt_addr, fdtsize));
119 return fdt_addr;