1 /* 2 * Microblaze kernel loader 3 * 4 * Copyright (c) 2012 Peter Crosthwaite <peter.crosthwaite@petalogix.com> 5 * Copyright (c) 2012 PetaLogix 6 * Copyright (c) 2009 Edgar E. Iglesias. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a copy 9 * of this software and associated documentation files (the "Software"), to deal 10 * in the Software without restriction, including without limitation the rights 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 * copies of the Software, and to permit persons to whom the Software is 13 * furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 * THE SOFTWARE. 25 */ 26 27 #include "qemu/osdep.h" 28 #include "qemu-common.h" 29 #include "cpu.h" 30 #include "qemu/option.h" 31 #include "qemu/config-file.h" 32 #include "qemu/error-report.h" 33 #include "sysemu/device_tree.h" 34 #include "sysemu/sysemu.h" 35 #include "hw/loader.h" 36 #include "elf.h" 37 #include "qemu/cutils.h" 38 39 #include "boot.h" 40 41 static struct 42 { 43 void (*machine_cpu_reset)(MicroBlazeCPU *); 44 uint32_t bootstrap_pc; 45 uint32_t cmdline; 46 uint32_t initrd_start; 47 uint32_t initrd_end; 48 uint32_t fdt; 49 } boot_info; 50 51 static void main_cpu_reset(void *opaque) 52 { 53 MicroBlazeCPU *cpu = opaque; 54 CPUState *cs = CPU(cpu); 55 CPUMBState *env = &cpu->env; 56 57 cpu_reset(cs); 58 env->regs[5] = boot_info.cmdline; 59 env->regs[6] = boot_info.initrd_start; 60 env->regs[7] = boot_info.fdt; 61 cpu_set_pc(cs, boot_info.bootstrap_pc); 62 if (boot_info.machine_cpu_reset) { 63 boot_info.machine_cpu_reset(cpu); 64 } 65 } 66 67 static int microblaze_load_dtb(hwaddr addr, 68 uint32_t ramsize, 69 uint32_t initrd_start, 70 uint32_t initrd_end, 71 const char *kernel_cmdline, 72 const char *dtb_filename) 73 { 74 int fdt_size; 75 void *fdt = NULL; 76 int r; 77 78 if (dtb_filename) { 79 fdt = load_device_tree(dtb_filename, &fdt_size); 80 } 81 if (!fdt) { 82 return 0; 83 } 84 85 if (kernel_cmdline) { 86 r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", 87 kernel_cmdline); 88 if (r < 0) { 89 fprintf(stderr, "couldn't set /chosen/bootargs\n"); 90 } 91 } 92 93 if (initrd_start) { 94 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start", 95 initrd_start); 96 97 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end", 98 initrd_end); 99 } 100 101 cpu_physical_memory_write(addr, fdt, fdt_size); 102 return fdt_size; 103 } 104 105 static uint64_t translate_kernel_address(void *opaque, uint64_t addr) 106 { 107 return addr - 0x30000000LL; 108 } 109 110 void microblaze_load_kernel(MicroBlazeCPU *cpu, hwaddr ddr_base, 111 uint32_t ramsize, 112 const char *initrd_filename, 113 const char *dtb_filename, 114 void (*machine_cpu_reset)(MicroBlazeCPU *)) 115 { 116 QemuOpts *machine_opts; 117 const char *kernel_filename; 118 const char *kernel_cmdline; 119 const char *dtb_arg; 120 char *filename = NULL; 121 122 machine_opts = qemu_get_machine_opts(); 123 kernel_filename = qemu_opt_get(machine_opts, "kernel"); 124 kernel_cmdline = qemu_opt_get(machine_opts, "append"); 125 dtb_arg = qemu_opt_get(machine_opts, "dtb"); 126 /* default to pcbios dtb as passed by machine_init */ 127 if (!dtb_arg) { 128 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename); 129 } 130 131 boot_info.machine_cpu_reset = machine_cpu_reset; 132 qemu_register_reset(main_cpu_reset, cpu); 133 134 if (kernel_filename) { 135 int kernel_size; 136 uint64_t entry, low, high; 137 uint32_t base32; 138 int big_endian = 0; 139 140 #ifdef TARGET_WORDS_BIGENDIAN 141 big_endian = 1; 142 #endif 143 144 /* Boots a kernel elf binary. */ 145 kernel_size = load_elf(kernel_filename, NULL, NULL, 146 &entry, &low, &high, 147 big_endian, EM_MICROBLAZE, 0, 0); 148 base32 = entry; 149 if (base32 == 0xc0000000) { 150 kernel_size = load_elf(kernel_filename, translate_kernel_address, 151 NULL, &entry, NULL, NULL, 152 big_endian, EM_MICROBLAZE, 0, 0); 153 } 154 /* Always boot into physical ram. */ 155 boot_info.bootstrap_pc = (uint32_t)entry; 156 157 /* If it wasn't an ELF image, try an u-boot image. */ 158 if (kernel_size < 0) { 159 hwaddr uentry, loadaddr; 160 161 kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0, 162 NULL, NULL); 163 boot_info.bootstrap_pc = uentry; 164 high = (loadaddr + kernel_size + 3) & ~3; 165 } 166 167 /* Not an ELF image nor an u-boot image, try a RAW image. */ 168 if (kernel_size < 0) { 169 kernel_size = load_image_targphys(kernel_filename, ddr_base, 170 ram_size); 171 boot_info.bootstrap_pc = ddr_base; 172 high = (ddr_base + kernel_size + 3) & ~3; 173 } 174 175 if (initrd_filename) { 176 int initrd_size; 177 uint32_t initrd_offset; 178 179 high = ROUND_UP(high + kernel_size, 4); 180 boot_info.initrd_start = high; 181 initrd_offset = boot_info.initrd_start - ddr_base; 182 183 initrd_size = load_ramdisk(initrd_filename, 184 boot_info.initrd_start, 185 ram_size - initrd_offset); 186 if (initrd_size < 0) { 187 initrd_size = load_image_targphys(initrd_filename, 188 boot_info.initrd_start, 189 ram_size - initrd_offset); 190 } 191 if (initrd_size < 0) { 192 error_report("could not load initrd '%s'", 193 initrd_filename); 194 exit(EXIT_FAILURE); 195 } 196 boot_info.initrd_end = boot_info.initrd_start + initrd_size; 197 high = ROUND_UP(high + initrd_size, 4); 198 } 199 200 boot_info.cmdline = high + 4096; 201 if (kernel_cmdline && strlen(kernel_cmdline)) { 202 pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline); 203 } 204 /* Provide a device-tree. */ 205 boot_info.fdt = boot_info.cmdline + 4096; 206 microblaze_load_dtb(boot_info.fdt, ram_size, 207 boot_info.initrd_start, 208 boot_info.initrd_end, 209 kernel_cmdline, 210 /* Preference a -dtb argument */ 211 dtb_arg ? dtb_arg : filename); 212 } 213 g_free(filename); 214 } 215