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 18 #include <libfdt.h> 19 20 #define KERNEL_LOAD_ADDR 0x100 21 22 hwaddr openrisc_load_kernel(ram_addr_t ram_size, 23 const char *kernel_filename, 24 uint32_t *bootstrap_pc) 25 { 26 long kernel_size; 27 uint64_t elf_entry; 28 uint64_t high_addr; 29 hwaddr entry; 30 31 if (kernel_filename && !qtest_enabled()) { 32 kernel_size = load_elf(kernel_filename, NULL, NULL, NULL, 33 &elf_entry, NULL, &high_addr, NULL, 1, 34 EM_OPENRISC, 1, 0); 35 entry = elf_entry; 36 if (kernel_size < 0) { 37 kernel_size = load_uimage(kernel_filename, 38 &entry, NULL, NULL, NULL, NULL); 39 high_addr = entry + kernel_size; 40 } 41 if (kernel_size < 0) { 42 kernel_size = load_image_targphys(kernel_filename, 43 KERNEL_LOAD_ADDR, 44 ram_size - KERNEL_LOAD_ADDR); 45 high_addr = KERNEL_LOAD_ADDR + kernel_size; 46 } 47 48 if (entry <= 0) { 49 entry = KERNEL_LOAD_ADDR; 50 } 51 52 if (kernel_size < 0) { 53 error_report("couldn't load the kernel '%s'", kernel_filename); 54 exit(1); 55 } 56 *bootstrap_pc = entry; 57 58 return high_addr; 59 } 60 return 0; 61 } 62 63 hwaddr openrisc_load_initrd(void *fdt, const char *filename, 64 hwaddr load_start, uint64_t mem_size) 65 { 66 int size; 67 hwaddr start; 68 69 /* We put the initrd right after the kernel; page aligned. */ 70 start = TARGET_PAGE_ALIGN(load_start); 71 72 size = load_ramdisk(filename, start, mem_size - start); 73 if (size < 0) { 74 size = load_image_targphys(filename, start, mem_size - start); 75 if (size < 0) { 76 error_report("could not load ramdisk '%s'", filename); 77 exit(1); 78 } 79 } 80 81 if (fdt) { 82 qemu_fdt_setprop_cell(fdt, "/chosen", 83 "linux,initrd-start", start); 84 qemu_fdt_setprop_cell(fdt, "/chosen", 85 "linux,initrd-end", start + size); 86 } 87 88 return start + size; 89 } 90 91 uint32_t openrisc_load_fdt(void *fdt, hwaddr load_start, 92 uint64_t mem_size) 93 { 94 uint32_t fdt_addr; 95 int ret; 96 int fdtsize = fdt_totalsize(fdt); 97 98 if (fdtsize <= 0) { 99 error_report("invalid device-tree"); 100 exit(1); 101 } 102 103 /* We put fdt right after the kernel and/or initrd. */ 104 fdt_addr = TARGET_PAGE_ALIGN(load_start); 105 106 ret = fdt_pack(fdt); 107 /* Should only fail if we've built a corrupted tree */ 108 g_assert(ret == 0); 109 /* copy in the device tree */ 110 qemu_fdt_dumpdtb(fdt, fdtsize); 111 112 rom_add_blob_fixed_as("fdt", fdt, fdtsize, fdt_addr, 113 &address_space_memory); 114 115 return fdt_addr; 116 } 117