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