xref: /openbmc/qemu/hw/openrisc/boot.c (revision 7d87775f)
1 /*
2  * SPDX-License-Identifier: GPL-2.0-or-later
3  *
4  * QEMU OpenRISC boot helpers.
5  *
6  * (c) 2022 Stafford Horne <shorne@gmail.com>
7  */
8 
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"
19 
20 #include <libfdt.h>
21 
22 #define KERNEL_LOAD_ADDR 0x100
23 
24 hwaddr openrisc_load_kernel(ram_addr_t ram_size,
25                             const char *kernel_filename,
26                             uint32_t *bootstrap_pc)
27 {
28     long kernel_size;
29     uint64_t elf_entry;
30     uint64_t high_addr;
31     hwaddr entry;
32 
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;
42         }
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;
48         }
49 
50         if (entry <= 0) {
51             entry = KERNEL_LOAD_ADDR;
52         }
53 
54         if (kernel_size < 0) {
55             error_report("couldn't load the kernel '%s'", kernel_filename);
56             exit(1);
57         }
58         *bootstrap_pc = entry;
59 
60         return high_addr;
61     }
62     return 0;
63 }
64 
65 hwaddr openrisc_load_initrd(void *fdt, const char *filename,
66                             hwaddr load_start, uint64_t mem_size)
67 {
68     int size;
69     hwaddr start;
70 
71     /* We put the initrd right after the kernel; page aligned. */
72     start = TARGET_PAGE_ALIGN(load_start);
73 
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);
80         }
81     }
82 
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);
88     }
89 
90     return start + size;
91 }
92 
93 uint32_t openrisc_load_fdt(void *fdt, hwaddr load_start,
94                            uint64_t mem_size)
95 {
96     uint32_t fdt_addr;
97     int ret;
98     int fdtsize = fdt_totalsize(fdt);
99 
100     if (fdtsize <= 0) {
101         error_report("invalid device-tree");
102         exit(1);
103     }
104 
105     /* We put fdt right after the kernel and/or initrd. */
106     fdt_addr = TARGET_PAGE_ALIGN(load_start);
107 
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);
113 
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));
118 
119     return fdt_addr;
120 }
121