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