]>
Commit | Line | Data |
---|---|---|
5fafdf24 | 1 | /* |
16406950 PB |
2 | * ARM kernel loader. |
3 | * | |
9ee6e8bb | 4 | * Copyright (c) 2006-2007 CodeSourcery. |
16406950 PB |
5 | * Written by Paul Brook |
6 | * | |
8e31bf38 | 7 | * This code is licensed under the GPL. |
16406950 PB |
8 | */ |
9 | ||
412beee6 | 10 | #include "config.h" |
83c9f4ca | 11 | #include "hw/hw.h" |
bd2be150 | 12 | #include "hw/arm/arm.h" |
9c17d615 | 13 | #include "sysemu/sysemu.h" |
83c9f4ca PB |
14 | #include "hw/boards.h" |
15 | #include "hw/loader.h" | |
ca20cf32 | 16 | #include "elf.h" |
9c17d615 | 17 | #include "sysemu/device_tree.h" |
1de7afc9 | 18 | #include "qemu/config-file.h" |
2198a121 | 19 | #include "exec/address-spaces.h" |
16406950 | 20 | |
4d9ebf75 MH |
21 | /* Kernel boot protocol is specified in the kernel docs |
22 | * Documentation/arm/Booting and Documentation/arm64/booting.txt | |
23 | * They have different preferred image load offsets from system RAM base. | |
24 | */ | |
16406950 PB |
25 | #define KERNEL_ARGS_ADDR 0x100 |
26 | #define KERNEL_LOAD_ADDR 0x00010000 | |
4d9ebf75 | 27 | #define KERNEL64_LOAD_ADDR 0x00080000 |
16406950 | 28 | |
47b1da81 PM |
29 | typedef enum { |
30 | FIXUP_NONE = 0, /* do nothing */ | |
31 | FIXUP_TERMINATOR, /* end of insns */ | |
32 | FIXUP_BOARDID, /* overwrite with board ID number */ | |
33 | FIXUP_ARGPTR, /* overwrite with pointer to kernel args */ | |
34 | FIXUP_ENTRYPOINT, /* overwrite with kernel entry point */ | |
35 | FIXUP_GIC_CPU_IF, /* overwrite with GIC CPU interface address */ | |
36 | FIXUP_BOOTREG, /* overwrite with boot register address */ | |
37 | FIXUP_DSB, /* overwrite with correct DSB insn for cpu */ | |
38 | FIXUP_MAX, | |
39 | } FixupType; | |
40 | ||
41 | typedef struct ARMInsnFixup { | |
42 | uint32_t insn; | |
43 | FixupType fixup; | |
44 | } ARMInsnFixup; | |
45 | ||
4d9ebf75 MH |
46 | static const ARMInsnFixup bootloader_aarch64[] = { |
47 | { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */ | |
48 | { 0xaa1f03e1 }, /* mov x1, xzr */ | |
49 | { 0xaa1f03e2 }, /* mov x2, xzr */ | |
50 | { 0xaa1f03e3 }, /* mov x3, xzr */ | |
51 | { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */ | |
52 | { 0xd61f0080 }, /* br x4 ; Jump to the kernel entry point */ | |
53 | { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */ | |
54 | { 0 }, /* .word @DTB Higher 32-bits */ | |
55 | { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */ | |
56 | { 0 }, /* .word @Kernel Entry Higher 32-bits */ | |
57 | { 0, FIXUP_TERMINATOR } | |
58 | }; | |
59 | ||
16406950 | 60 | /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */ |
47b1da81 PM |
61 | static const ARMInsnFixup bootloader[] = { |
62 | { 0xe3a00000 }, /* mov r0, #0 */ | |
63 | { 0xe59f1004 }, /* ldr r1, [pc, #4] */ | |
64 | { 0xe59f2004 }, /* ldr r2, [pc, #4] */ | |
65 | { 0xe59ff004 }, /* ldr pc, [pc, #4] */ | |
66 | { 0, FIXUP_BOARDID }, | |
67 | { 0, FIXUP_ARGPTR }, | |
68 | { 0, FIXUP_ENTRYPOINT }, | |
69 | { 0, FIXUP_TERMINATOR } | |
16406950 PB |
70 | }; |
71 | ||
9d5ba9bf ML |
72 | /* Handling for secondary CPU boot in a multicore system. |
73 | * Unlike the uniprocessor/primary CPU boot, this is platform | |
74 | * dependent. The default code here is based on the secondary | |
75 | * CPU boot protocol used on realview/vexpress boards, with | |
76 | * some parameterisation to increase its flexibility. | |
77 | * QEMU platform models for which this code is not appropriate | |
78 | * should override write_secondary_boot and secondary_cpu_reset_hook | |
79 | * instead. | |
80 | * | |
81 | * This code enables the interrupt controllers for the secondary | |
82 | * CPUs and then puts all the secondary CPUs into a loop waiting | |
83 | * for an interprocessor interrupt and polling a configurable | |
84 | * location for the kernel secondary CPU entry point. | |
85 | */ | |
bf471f79 PM |
86 | #define DSB_INSN 0xf57ff04f |
87 | #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */ | |
88 | ||
47b1da81 PM |
89 | static const ARMInsnFixup smpboot[] = { |
90 | { 0xe59f2028 }, /* ldr r2, gic_cpu_if */ | |
91 | { 0xe59f0028 }, /* ldr r0, bootreg_addr */ | |
92 | { 0xe3a01001 }, /* mov r1, #1 */ | |
93 | { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */ | |
94 | { 0xe3a010ff }, /* mov r1, #0xff */ | |
95 | { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */ | |
96 | { 0, FIXUP_DSB }, /* dsb */ | |
97 | { 0xe320f003 }, /* wfi */ | |
98 | { 0xe5901000 }, /* ldr r1, [r0] */ | |
99 | { 0xe1110001 }, /* tst r1, r1 */ | |
100 | { 0x0afffffb }, /* beq <wfi> */ | |
101 | { 0xe12fff11 }, /* bx r1 */ | |
102 | { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */ | |
103 | { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */ | |
104 | { 0, FIXUP_TERMINATOR } | |
9ee6e8bb PB |
105 | }; |
106 | ||
47b1da81 PM |
107 | static void write_bootloader(const char *name, hwaddr addr, |
108 | const ARMInsnFixup *insns, uint32_t *fixupcontext) | |
109 | { | |
110 | /* Fix up the specified bootloader fragment and write it into | |
111 | * guest memory using rom_add_blob_fixed(). fixupcontext is | |
112 | * an array giving the values to write in for the fixup types | |
113 | * which write a value into the code array. | |
114 | */ | |
115 | int i, len; | |
116 | uint32_t *code; | |
117 | ||
118 | len = 0; | |
119 | while (insns[len].fixup != FIXUP_TERMINATOR) { | |
120 | len++; | |
121 | } | |
122 | ||
123 | code = g_new0(uint32_t, len); | |
124 | ||
125 | for (i = 0; i < len; i++) { | |
126 | uint32_t insn = insns[i].insn; | |
127 | FixupType fixup = insns[i].fixup; | |
128 | ||
129 | switch (fixup) { | |
130 | case FIXUP_NONE: | |
131 | break; | |
132 | case FIXUP_BOARDID: | |
133 | case FIXUP_ARGPTR: | |
134 | case FIXUP_ENTRYPOINT: | |
135 | case FIXUP_GIC_CPU_IF: | |
136 | case FIXUP_BOOTREG: | |
137 | case FIXUP_DSB: | |
138 | insn = fixupcontext[fixup]; | |
139 | break; | |
140 | default: | |
141 | abort(); | |
142 | } | |
143 | code[i] = tswap32(insn); | |
144 | } | |
145 | ||
146 | rom_add_blob_fixed(name, code, len * sizeof(uint32_t), addr); | |
147 | ||
148 | g_free(code); | |
149 | } | |
150 | ||
9543b0cd | 151 | static void default_write_secondary(ARMCPU *cpu, |
9d5ba9bf ML |
152 | const struct arm_boot_info *info) |
153 | { | |
47b1da81 PM |
154 | uint32_t fixupcontext[FIXUP_MAX]; |
155 | ||
156 | fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr; | |
157 | fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr; | |
158 | if (arm_feature(&cpu->env, ARM_FEATURE_V7)) { | |
159 | fixupcontext[FIXUP_DSB] = DSB_INSN; | |
160 | } else { | |
161 | fixupcontext[FIXUP_DSB] = CP15_DSB_INSN; | |
9d5ba9bf | 162 | } |
47b1da81 PM |
163 | |
164 | write_bootloader("smpboot", info->smp_loader_start, | |
165 | smpboot, fixupcontext); | |
9d5ba9bf ML |
166 | } |
167 | ||
5d309320 | 168 | static void default_reset_secondary(ARMCPU *cpu, |
9d5ba9bf ML |
169 | const struct arm_boot_info *info) |
170 | { | |
5d309320 AF |
171 | CPUARMState *env = &cpu->env; |
172 | ||
2198a121 | 173 | stl_phys_notdirty(&address_space_memory, info->smp_bootreg_addr, 0); |
9d5ba9bf ML |
174 | env->regs[15] = info->smp_loader_start; |
175 | } | |
176 | ||
83bfffec PM |
177 | static inline bool have_dtb(const struct arm_boot_info *info) |
178 | { | |
179 | return info->dtb_filename || info->get_dtb; | |
180 | } | |
181 | ||
52b43737 | 182 | #define WRITE_WORD(p, value) do { \ |
2198a121 | 183 | stl_phys_notdirty(&address_space_memory, p, value); \ |
52b43737 PB |
184 | p += 4; \ |
185 | } while (0) | |
186 | ||
761c9eb0 | 187 | static void set_kernel_args(const struct arm_boot_info *info) |
16406950 | 188 | { |
761c9eb0 | 189 | int initrd_size = info->initrd_size; |
a8170e5e AK |
190 | hwaddr base = info->loader_start; |
191 | hwaddr p; | |
16406950 | 192 | |
52b43737 | 193 | p = base + KERNEL_ARGS_ADDR; |
16406950 | 194 | /* ATAG_CORE */ |
52b43737 PB |
195 | WRITE_WORD(p, 5); |
196 | WRITE_WORD(p, 0x54410001); | |
197 | WRITE_WORD(p, 1); | |
198 | WRITE_WORD(p, 0x1000); | |
199 | WRITE_WORD(p, 0); | |
16406950 | 200 | /* ATAG_MEM */ |
f93eb9ff | 201 | /* TODO: handle multiple chips on one ATAG list */ |
52b43737 PB |
202 | WRITE_WORD(p, 4); |
203 | WRITE_WORD(p, 0x54410002); | |
204 | WRITE_WORD(p, info->ram_size); | |
205 | WRITE_WORD(p, info->loader_start); | |
16406950 PB |
206 | if (initrd_size) { |
207 | /* ATAG_INITRD2 */ | |
52b43737 PB |
208 | WRITE_WORD(p, 4); |
209 | WRITE_WORD(p, 0x54420005); | |
fc53b7d4 | 210 | WRITE_WORD(p, info->initrd_start); |
52b43737 | 211 | WRITE_WORD(p, initrd_size); |
16406950 | 212 | } |
f93eb9ff | 213 | if (info->kernel_cmdline && *info->kernel_cmdline) { |
16406950 PB |
214 | /* ATAG_CMDLINE */ |
215 | int cmdline_size; | |
216 | ||
f93eb9ff | 217 | cmdline_size = strlen(info->kernel_cmdline); |
e1fe50dc | 218 | cpu_physical_memory_write(p + 8, info->kernel_cmdline, |
52b43737 | 219 | cmdline_size + 1); |
16406950 | 220 | cmdline_size = (cmdline_size >> 2) + 1; |
52b43737 PB |
221 | WRITE_WORD(p, cmdline_size + 2); |
222 | WRITE_WORD(p, 0x54410009); | |
223 | p += cmdline_size * 4; | |
16406950 | 224 | } |
f93eb9ff AZ |
225 | if (info->atag_board) { |
226 | /* ATAG_BOARD */ | |
227 | int atag_board_len; | |
52b43737 | 228 | uint8_t atag_board_buf[0x1000]; |
f93eb9ff | 229 | |
52b43737 PB |
230 | atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3; |
231 | WRITE_WORD(p, (atag_board_len + 8) >> 2); | |
232 | WRITE_WORD(p, 0x414f4d50); | |
233 | cpu_physical_memory_write(p, atag_board_buf, atag_board_len); | |
f93eb9ff AZ |
234 | p += atag_board_len; |
235 | } | |
16406950 | 236 | /* ATAG_END */ |
52b43737 PB |
237 | WRITE_WORD(p, 0); |
238 | WRITE_WORD(p, 0); | |
16406950 PB |
239 | } |
240 | ||
761c9eb0 | 241 | static void set_kernel_args_old(const struct arm_boot_info *info) |
2b8f2d41 | 242 | { |
a8170e5e | 243 | hwaddr p; |
52b43737 | 244 | const char *s; |
761c9eb0 | 245 | int initrd_size = info->initrd_size; |
a8170e5e | 246 | hwaddr base = info->loader_start; |
2b8f2d41 AZ |
247 | |
248 | /* see linux/include/asm-arm/setup.h */ | |
52b43737 | 249 | p = base + KERNEL_ARGS_ADDR; |
2b8f2d41 | 250 | /* page_size */ |
52b43737 | 251 | WRITE_WORD(p, 4096); |
2b8f2d41 | 252 | /* nr_pages */ |
52b43737 | 253 | WRITE_WORD(p, info->ram_size / 4096); |
2b8f2d41 | 254 | /* ramdisk_size */ |
52b43737 | 255 | WRITE_WORD(p, 0); |
2b8f2d41 AZ |
256 | #define FLAG_READONLY 1 |
257 | #define FLAG_RDLOAD 4 | |
258 | #define FLAG_RDPROMPT 8 | |
259 | /* flags */ | |
52b43737 | 260 | WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT); |
2b8f2d41 | 261 | /* rootdev */ |
52b43737 | 262 | WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */ |
2b8f2d41 | 263 | /* video_num_cols */ |
52b43737 | 264 | WRITE_WORD(p, 0); |
2b8f2d41 | 265 | /* video_num_rows */ |
52b43737 | 266 | WRITE_WORD(p, 0); |
2b8f2d41 | 267 | /* video_x */ |
52b43737 | 268 | WRITE_WORD(p, 0); |
2b8f2d41 | 269 | /* video_y */ |
52b43737 | 270 | WRITE_WORD(p, 0); |
2b8f2d41 | 271 | /* memc_control_reg */ |
52b43737 | 272 | WRITE_WORD(p, 0); |
2b8f2d41 AZ |
273 | /* unsigned char sounddefault */ |
274 | /* unsigned char adfsdrives */ | |
275 | /* unsigned char bytes_per_char_h */ | |
276 | /* unsigned char bytes_per_char_v */ | |
52b43737 | 277 | WRITE_WORD(p, 0); |
2b8f2d41 | 278 | /* pages_in_bank[4] */ |
52b43737 PB |
279 | WRITE_WORD(p, 0); |
280 | WRITE_WORD(p, 0); | |
281 | WRITE_WORD(p, 0); | |
282 | WRITE_WORD(p, 0); | |
2b8f2d41 | 283 | /* pages_in_vram */ |
52b43737 | 284 | WRITE_WORD(p, 0); |
2b8f2d41 | 285 | /* initrd_start */ |
fc53b7d4 PM |
286 | if (initrd_size) { |
287 | WRITE_WORD(p, info->initrd_start); | |
288 | } else { | |
52b43737 | 289 | WRITE_WORD(p, 0); |
fc53b7d4 | 290 | } |
2b8f2d41 | 291 | /* initrd_size */ |
52b43737 | 292 | WRITE_WORD(p, initrd_size); |
2b8f2d41 | 293 | /* rd_start */ |
52b43737 | 294 | WRITE_WORD(p, 0); |
2b8f2d41 | 295 | /* system_rev */ |
52b43737 | 296 | WRITE_WORD(p, 0); |
2b8f2d41 | 297 | /* system_serial_low */ |
52b43737 | 298 | WRITE_WORD(p, 0); |
2b8f2d41 | 299 | /* system_serial_high */ |
52b43737 | 300 | WRITE_WORD(p, 0); |
2b8f2d41 | 301 | /* mem_fclk_21285 */ |
52b43737 | 302 | WRITE_WORD(p, 0); |
2b8f2d41 | 303 | /* zero unused fields */ |
52b43737 PB |
304 | while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) { |
305 | WRITE_WORD(p, 0); | |
306 | } | |
307 | s = info->kernel_cmdline; | |
308 | if (s) { | |
e1fe50dc | 309 | cpu_physical_memory_write(p, s, strlen(s) + 1); |
52b43737 PB |
310 | } else { |
311 | WRITE_WORD(p, 0); | |
312 | } | |
2b8f2d41 AZ |
313 | } |
314 | ||
a8170e5e | 315 | static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo) |
412beee6 | 316 | { |
412beee6 | 317 | void *fdt = NULL; |
412beee6 | 318 | int size, rc; |
70976c41 | 319 | uint32_t acells, scells; |
412beee6 | 320 | |
0fb79851 JR |
321 | if (binfo->dtb_filename) { |
322 | char *filename; | |
323 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename); | |
324 | if (!filename) { | |
325 | fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename); | |
326 | goto fail; | |
327 | } | |
412beee6 | 328 | |
0fb79851 JR |
329 | fdt = load_device_tree(filename, &size); |
330 | if (!fdt) { | |
331 | fprintf(stderr, "Couldn't open dtb file %s\n", filename); | |
332 | g_free(filename); | |
333 | goto fail; | |
334 | } | |
412beee6 | 335 | g_free(filename); |
0fb79851 JR |
336 | } else if (binfo->get_dtb) { |
337 | fdt = binfo->get_dtb(binfo, &size); | |
338 | if (!fdt) { | |
339 | fprintf(stderr, "Board was unable to create a dtb blob\n"); | |
340 | goto fail; | |
341 | } | |
412beee6 | 342 | } |
412beee6 | 343 | |
5a4348d1 PC |
344 | acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells"); |
345 | scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells"); | |
9bfa659e PM |
346 | if (acells == 0 || scells == 0) { |
347 | fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n"); | |
c23045de | 348 | goto fail; |
9bfa659e PM |
349 | } |
350 | ||
70976c41 PM |
351 | if (scells < 2 && binfo->ram_size >= (1ULL << 32)) { |
352 | /* This is user error so deserves a friendlier error message | |
353 | * than the failure of setprop_sized_cells would provide | |
354 | */ | |
9bfa659e PM |
355 | fprintf(stderr, "qemu: dtb file not compatible with " |
356 | "RAM size > 4GB\n"); | |
c23045de | 357 | goto fail; |
9bfa659e PM |
358 | } |
359 | ||
5a4348d1 PC |
360 | rc = qemu_fdt_setprop_sized_cells(fdt, "/memory", "reg", |
361 | acells, binfo->loader_start, | |
362 | scells, binfo->ram_size); | |
412beee6 GL |
363 | if (rc < 0) { |
364 | fprintf(stderr, "couldn't set /memory/reg\n"); | |
c23045de | 365 | goto fail; |
412beee6 GL |
366 | } |
367 | ||
5e87975c | 368 | if (binfo->kernel_cmdline && *binfo->kernel_cmdline) { |
5a4348d1 PC |
369 | rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", |
370 | binfo->kernel_cmdline); | |
5e87975c PC |
371 | if (rc < 0) { |
372 | fprintf(stderr, "couldn't set /chosen/bootargs\n"); | |
c23045de | 373 | goto fail; |
5e87975c | 374 | } |
412beee6 GL |
375 | } |
376 | ||
377 | if (binfo->initrd_size) { | |
5a4348d1 PC |
378 | rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start", |
379 | binfo->initrd_start); | |
412beee6 GL |
380 | if (rc < 0) { |
381 | fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n"); | |
c23045de | 382 | goto fail; |
412beee6 GL |
383 | } |
384 | ||
5a4348d1 PC |
385 | rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end", |
386 | binfo->initrd_start + binfo->initrd_size); | |
412beee6 GL |
387 | if (rc < 0) { |
388 | fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n"); | |
c23045de | 389 | goto fail; |
412beee6 GL |
390 | } |
391 | } | |
3b1cceb8 PM |
392 | |
393 | if (binfo->modify_dtb) { | |
394 | binfo->modify_dtb(binfo, fdt); | |
395 | } | |
396 | ||
5a4348d1 | 397 | qemu_fdt_dumpdtb(fdt, size); |
412beee6 GL |
398 | |
399 | cpu_physical_memory_write(addr, fdt, size); | |
400 | ||
c23045de PM |
401 | g_free(fdt); |
402 | ||
412beee6 | 403 | return 0; |
c23045de PM |
404 | |
405 | fail: | |
406 | g_free(fdt); | |
407 | return -1; | |
412beee6 GL |
408 | } |
409 | ||
6ed221b6 | 410 | static void do_cpu_reset(void *opaque) |
f2d74978 | 411 | { |
351d5666 AF |
412 | ARMCPU *cpu = opaque; |
413 | CPUARMState *env = &cpu->env; | |
462a8bc6 | 414 | const struct arm_boot_info *info = env->boot_info; |
f2d74978 | 415 | |
351d5666 | 416 | cpu_reset(CPU(cpu)); |
f2d74978 PB |
417 | if (info) { |
418 | if (!info->is_linux) { | |
419 | /* Jump to the entry point. */ | |
420 | env->regs[15] = info->entry & 0xfffffffe; | |
421 | env->thumb = info->entry & 1; | |
422 | } else { | |
182735ef | 423 | if (CPU(cpu) == first_cpu) { |
4d9ebf75 MH |
424 | if (env->aarch64) { |
425 | env->pc = info->loader_start; | |
426 | } else { | |
427 | env->regs[15] = info->loader_start; | |
428 | } | |
429 | ||
83bfffec | 430 | if (!have_dtb(info)) { |
412beee6 GL |
431 | if (old_param) { |
432 | set_kernel_args_old(info); | |
433 | } else { | |
434 | set_kernel_args(info); | |
435 | } | |
6ed221b6 | 436 | } |
f2d74978 | 437 | } else { |
5d309320 | 438 | info->secondary_cpu_reset_hook(cpu, info); |
f2d74978 PB |
439 | } |
440 | } | |
441 | } | |
f2d74978 PB |
442 | } |
443 | ||
3aaa8dfa | 444 | void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info) |
16406950 | 445 | { |
182735ef | 446 | CPUState *cs = CPU(cpu); |
16406950 PB |
447 | int kernel_size; |
448 | int initrd_size; | |
1c7b3754 PB |
449 | int is_linux = 0; |
450 | uint64_t elf_entry; | |
da0af40d | 451 | int elf_machine; |
4d9ebf75 | 452 | hwaddr entry, kernel_load_offset; |
ca20cf32 | 453 | int big_endian; |
4d9ebf75 | 454 | static const ARMInsnFixup *primary_loader; |
16406950 PB |
455 | |
456 | /* Load the kernel. */ | |
f93eb9ff | 457 | if (!info->kernel_filename) { |
9546dbab PM |
458 | /* If no kernel specified, do nothing; we will start from address 0 |
459 | * (typically a boot ROM image) in the same way as hardware. | |
460 | */ | |
461 | return; | |
16406950 | 462 | } |
daf90626 | 463 | |
4d9ebf75 MH |
464 | if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { |
465 | primary_loader = bootloader_aarch64; | |
466 | kernel_load_offset = KERNEL64_LOAD_ADDR; | |
da0af40d | 467 | elf_machine = EM_AARCH64; |
4d9ebf75 MH |
468 | } else { |
469 | primary_loader = bootloader; | |
470 | kernel_load_offset = KERNEL_LOAD_ADDR; | |
da0af40d | 471 | elf_machine = EM_ARM; |
4d9ebf75 MH |
472 | } |
473 | ||
2ff3de68 | 474 | info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb"); |
412beee6 | 475 | |
9d5ba9bf ML |
476 | if (!info->secondary_cpu_reset_hook) { |
477 | info->secondary_cpu_reset_hook = default_reset_secondary; | |
478 | } | |
479 | if (!info->write_secondary_boot) { | |
480 | info->write_secondary_boot = default_write_secondary; | |
481 | } | |
482 | ||
f2d74978 PB |
483 | if (info->nb_cpus == 0) |
484 | info->nb_cpus = 1; | |
f93eb9ff | 485 | |
ca20cf32 BS |
486 | #ifdef TARGET_WORDS_BIGENDIAN |
487 | big_endian = 1; | |
488 | #else | |
489 | big_endian = 0; | |
490 | #endif | |
491 | ||
fc53b7d4 PM |
492 | /* We want to put the initrd far enough into RAM that when the |
493 | * kernel is uncompressed it will not clobber the initrd. However | |
494 | * on boards without much RAM we must ensure that we still leave | |
495 | * enough room for a decent sized initrd, and on boards with large | |
496 | * amounts of RAM we must avoid the initrd being so far up in RAM | |
497 | * that it is outside lowmem and inaccessible to the kernel. | |
498 | * So for boards with less than 256MB of RAM we put the initrd | |
499 | * halfway into RAM, and for boards with 256MB of RAM or more we put | |
500 | * the initrd at 128MB. | |
501 | */ | |
502 | info->initrd_start = info->loader_start + | |
503 | MIN(info->ram_size / 2, 128 * 1024 * 1024); | |
504 | ||
1c7b3754 | 505 | /* Assume that raw images are linux kernels, and ELF images are not. */ |
409dbce5 | 506 | kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry, |
da0af40d | 507 | NULL, NULL, big_endian, elf_machine, 1); |
1c7b3754 PB |
508 | entry = elf_entry; |
509 | if (kernel_size < 0) { | |
5a9154e0 AL |
510 | kernel_size = load_uimage(info->kernel_filename, &entry, NULL, |
511 | &is_linux); | |
1c7b3754 PB |
512 | } |
513 | if (kernel_size < 0) { | |
4d9ebf75 | 514 | entry = info->loader_start + kernel_load_offset; |
3b760e04 | 515 | kernel_size = load_image_targphys(info->kernel_filename, entry, |
4d9ebf75 | 516 | info->ram_size - kernel_load_offset); |
1c7b3754 PB |
517 | is_linux = 1; |
518 | } | |
519 | if (kernel_size < 0) { | |
f93eb9ff AZ |
520 | fprintf(stderr, "qemu: could not load kernel '%s'\n", |
521 | info->kernel_filename); | |
1c7b3754 PB |
522 | exit(1); |
523 | } | |
f2d74978 PB |
524 | info->entry = entry; |
525 | if (is_linux) { | |
47b1da81 PM |
526 | uint32_t fixupcontext[FIXUP_MAX]; |
527 | ||
f93eb9ff | 528 | if (info->initrd_filename) { |
fd76663e SB |
529 | initrd_size = load_ramdisk(info->initrd_filename, |
530 | info->initrd_start, | |
531 | info->ram_size - | |
532 | info->initrd_start); | |
533 | if (initrd_size < 0) { | |
534 | initrd_size = load_image_targphys(info->initrd_filename, | |
535 | info->initrd_start, | |
536 | info->ram_size - | |
537 | info->initrd_start); | |
538 | } | |
daf90626 PB |
539 | if (initrd_size < 0) { |
540 | fprintf(stderr, "qemu: could not load initrd '%s'\n", | |
f93eb9ff | 541 | info->initrd_filename); |
daf90626 PB |
542 | exit(1); |
543 | } | |
544 | } else { | |
545 | initrd_size = 0; | |
546 | } | |
412beee6 GL |
547 | info->initrd_size = initrd_size; |
548 | ||
47b1da81 | 549 | fixupcontext[FIXUP_BOARDID] = info->board_id; |
412beee6 GL |
550 | |
551 | /* for device tree boot, we pass the DTB directly in r2. Otherwise | |
552 | * we point to the kernel args. | |
553 | */ | |
83bfffec | 554 | if (have_dtb(info)) { |
98ed805c PM |
555 | /* Place the DTB after the initrd in memory. Note that some |
556 | * kernels will trash anything in the 4K page the initrd | |
557 | * ends in, so make sure the DTB isn't caught up in that. | |
558 | */ | |
559 | hwaddr dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size, | |
560 | 4096); | |
412beee6 GL |
561 | if (load_dtb(dtb_start, info)) { |
562 | exit(1); | |
563 | } | |
47b1da81 | 564 | fixupcontext[FIXUP_ARGPTR] = dtb_start; |
412beee6 | 565 | } else { |
47b1da81 | 566 | fixupcontext[FIXUP_ARGPTR] = info->loader_start + KERNEL_ARGS_ADDR; |
3871481c PM |
567 | if (info->ram_size >= (1ULL << 32)) { |
568 | fprintf(stderr, "qemu: RAM size must be less than 4GB to boot" | |
569 | " Linux kernel using ATAGS (try passing a device tree" | |
570 | " using -dtb)\n"); | |
571 | exit(1); | |
572 | } | |
412beee6 | 573 | } |
47b1da81 PM |
574 | fixupcontext[FIXUP_ENTRYPOINT] = entry; |
575 | ||
576 | write_bootloader("bootloader", info->loader_start, | |
4d9ebf75 | 577 | primary_loader, fixupcontext); |
47b1da81 | 578 | |
52b43737 | 579 | if (info->nb_cpus > 1) { |
9543b0cd | 580 | info->write_secondary_boot(cpu, info); |
52b43737 | 581 | } |
16406950 | 582 | } |
f2d74978 | 583 | info->is_linux = is_linux; |
6ed221b6 | 584 | |
bdc44640 | 585 | for (; cs; cs = CPU_NEXT(cs)) { |
182735ef AF |
586 | cpu = ARM_CPU(cs); |
587 | cpu->env.boot_info = info; | |
351d5666 | 588 | qemu_register_reset(do_cpu_reset, cpu); |
6ed221b6 | 589 | } |
16406950 | 590 | } |