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/option.h" 29 #include "qemu/config-file.h" 30 #include "qemu/error-report.h" 31 #include "qemu-common.h" 32 #include "sysemu/device_tree.h" 33 #include "sysemu/sysemu.h" 34 #include "hw/loader.h" 35 #include "elf.h" 36 37 #include "boot.h" 38 39 static struct 40 { 41 void (*machine_cpu_reset)(MicroBlazeCPU *); 42 uint32_t bootstrap_pc; 43 uint32_t cmdline; 44 uint32_t initrd_start; 45 uint32_t initrd_end; 46 uint32_t fdt; 47 } boot_info; 48 49 static void main_cpu_reset(void *opaque) 50 { 51 MicroBlazeCPU *cpu = opaque; 52 CPUState *cs = CPU(cpu); 53 CPUMBState *env = &cpu->env; 54 55 cpu_reset(cs); 56 env->regs[5] = boot_info.cmdline; 57 env->regs[6] = boot_info.initrd_start; 58 env->regs[7] = boot_info.fdt; 59 cpu_set_pc(cs, boot_info.bootstrap_pc); 60 if (boot_info.machine_cpu_reset) { 61 boot_info.machine_cpu_reset(cpu); 62 } 63 } 64 65 static int microblaze_load_dtb(hwaddr addr, 66 uint32_t ramsize, 67 uint32_t initrd_start, 68 uint32_t initrd_end, 69 const char *kernel_cmdline, 70 const char *dtb_filename) 71 { 72 int fdt_size; 73 void *fdt = NULL; 74 int r; 75 76 if (dtb_filename) { 77 fdt = load_device_tree(dtb_filename, &fdt_size); 78 } 79 if (!fdt) { 80 return 0; 81 } 82 83 if (kernel_cmdline) { 84 r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", 85 kernel_cmdline); 86 if (r < 0) { 87 fprintf(stderr, "couldn't set /chosen/bootargs\n"); 88 } 89 } 90 91 if (initrd_start) { 92 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start", 93 initrd_start); 94 95 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end", 96 initrd_end); 97 } 98 99 cpu_physical_memory_write(addr, fdt, fdt_size); 100 return fdt_size; 101 } 102 103 static uint64_t translate_kernel_address(void *opaque, uint64_t addr) 104 { 105 return addr - 0x30000000LL; 106 } 107 108 void microblaze_load_kernel(MicroBlazeCPU *cpu, hwaddr ddr_base, 109 uint32_t ramsize, 110 const char *initrd_filename, 111 const char *dtb_filename, 112 void (*machine_cpu_reset)(MicroBlazeCPU *)) 113 { 114 QemuOpts *machine_opts; 115 const char *kernel_filename; 116 const char *kernel_cmdline; 117 const char *dtb_arg; 118 char *filename = NULL; 119 120 machine_opts = qemu_get_machine_opts(); 121 kernel_filename = qemu_opt_get(machine_opts, "kernel"); 122 kernel_cmdline = qemu_opt_get(machine_opts, "append"); 123 dtb_arg = qemu_opt_get(machine_opts, "dtb"); 124 /* default to pcbios dtb as passed by machine_init */ 125 if (!dtb_arg) { 126 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename); 127 } 128 129 boot_info.machine_cpu_reset = machine_cpu_reset; 130 qemu_register_reset(main_cpu_reset, cpu); 131 132 if (kernel_filename) { 133 int kernel_size; 134 uint64_t entry, low, high; 135 uint32_t base32; 136 int big_endian = 0; 137 138 #ifdef TARGET_WORDS_BIGENDIAN 139 big_endian = 1; 140 #endif 141 142 /* Boots a kernel elf binary. */ 143 kernel_size = load_elf(kernel_filename, NULL, NULL, 144 &entry, &low, &high, 145 big_endian, EM_MICROBLAZE, 0); 146 base32 = entry; 147 if (base32 == 0xc0000000) { 148 kernel_size = load_elf(kernel_filename, translate_kernel_address, 149 NULL, &entry, NULL, NULL, 150 big_endian, EM_MICROBLAZE, 0); 151 } 152 /* Always boot into physical ram. */ 153 boot_info.bootstrap_pc = (uint32_t)entry; 154 155 /* If it wasn't an ELF image, try an u-boot image. */ 156 if (kernel_size < 0) { 157 hwaddr uentry, loadaddr; 158 159 kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0, 160 NULL, NULL); 161 boot_info.bootstrap_pc = uentry; 162 high = (loadaddr + kernel_size + 3) & ~3; 163 } 164 165 /* Not an ELF image nor an u-boot image, try a RAW image. */ 166 if (kernel_size < 0) { 167 kernel_size = load_image_targphys(kernel_filename, ddr_base, 168 ram_size); 169 boot_info.bootstrap_pc = ddr_base; 170 high = (ddr_base + kernel_size + 3) & ~3; 171 } 172 173 if (initrd_filename) { 174 int initrd_size; 175 uint32_t initrd_offset; 176 177 high = ROUND_UP(high + kernel_size, 4); 178 boot_info.initrd_start = high; 179 initrd_offset = boot_info.initrd_start - ddr_base; 180 181 initrd_size = load_ramdisk(initrd_filename, 182 boot_info.initrd_start, 183 ram_size - initrd_offset); 184 if (initrd_size < 0) { 185 initrd_size = load_image_targphys(initrd_filename, 186 boot_info.initrd_start, 187 ram_size - initrd_offset); 188 } 189 if (initrd_size < 0) { 190 error_report("qemu: could not load initrd '%s'", 191 initrd_filename); 192 exit(EXIT_FAILURE); 193 } 194 boot_info.initrd_end = boot_info.initrd_start + initrd_size; 195 high = ROUND_UP(high + initrd_size, 4); 196 } 197 198 boot_info.cmdline = high + 4096; 199 if (kernel_cmdline && strlen(kernel_cmdline)) { 200 pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline); 201 } 202 /* Provide a device-tree. */ 203 boot_info.fdt = boot_info.cmdline + 4096; 204 microblaze_load_dtb(boot_info.fdt, ram_size, 205 boot_info.initrd_start, 206 boot_info.initrd_end, 207 kernel_cmdline, 208 /* Preference a -dtb argument */ 209 dtb_arg ? dtb_arg : filename); 210 } 211 g_free(filename); 212 } 213