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