]>
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" |
d8b1ae42 | 13 | #include "hw/arm/linux-boot-if.h" |
baf6b681 | 14 | #include "sysemu/kvm.h" |
9c17d615 | 15 | #include "sysemu/sysemu.h" |
83c9f4ca PB |
16 | #include "hw/boards.h" |
17 | #include "hw/loader.h" | |
ca20cf32 | 18 | #include "elf.h" |
9c17d615 | 19 | #include "sysemu/device_tree.h" |
1de7afc9 | 20 | #include "qemu/config-file.h" |
2198a121 | 21 | #include "exec/address-spaces.h" |
16406950 | 22 | |
4d9ebf75 MH |
23 | /* Kernel boot protocol is specified in the kernel docs |
24 | * Documentation/arm/Booting and Documentation/arm64/booting.txt | |
25 | * They have different preferred image load offsets from system RAM base. | |
26 | */ | |
16406950 PB |
27 | #define KERNEL_ARGS_ADDR 0x100 |
28 | #define KERNEL_LOAD_ADDR 0x00010000 | |
4d9ebf75 | 29 | #define KERNEL64_LOAD_ADDR 0x00080000 |
16406950 | 30 | |
47b1da81 | 31 | typedef enum { |
84e59397 PC |
32 | FIXUP_NONE = 0, /* do nothing */ |
33 | FIXUP_TERMINATOR, /* end of insns */ | |
34 | FIXUP_BOARDID, /* overwrite with board ID number */ | |
10b8ec73 | 35 | FIXUP_BOARD_SETUP, /* overwrite with board specific setup code address */ |
84e59397 PC |
36 | FIXUP_ARGPTR, /* overwrite with pointer to kernel args */ |
37 | FIXUP_ENTRYPOINT, /* overwrite with kernel entry point */ | |
38 | FIXUP_GIC_CPU_IF, /* overwrite with GIC CPU interface address */ | |
39 | FIXUP_BOOTREG, /* overwrite with boot register address */ | |
40 | FIXUP_DSB, /* overwrite with correct DSB insn for cpu */ | |
47b1da81 PM |
41 | FIXUP_MAX, |
42 | } FixupType; | |
43 | ||
44 | typedef struct ARMInsnFixup { | |
45 | uint32_t insn; | |
46 | FixupType fixup; | |
47 | } ARMInsnFixup; | |
48 | ||
4d9ebf75 MH |
49 | static const ARMInsnFixup bootloader_aarch64[] = { |
50 | { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */ | |
51 | { 0xaa1f03e1 }, /* mov x1, xzr */ | |
52 | { 0xaa1f03e2 }, /* mov x2, xzr */ | |
53 | { 0xaa1f03e3 }, /* mov x3, xzr */ | |
54 | { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */ | |
55 | { 0xd61f0080 }, /* br x4 ; Jump to the kernel entry point */ | |
56 | { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */ | |
57 | { 0 }, /* .word @DTB Higher 32-bits */ | |
58 | { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */ | |
59 | { 0 }, /* .word @Kernel Entry Higher 32-bits */ | |
60 | { 0, FIXUP_TERMINATOR } | |
61 | }; | |
62 | ||
10b8ec73 PC |
63 | /* A very small bootloader: call the board-setup code (if needed), |
64 | * set r0-r2, then jump to the kernel. | |
65 | * If we're not calling boot setup code then we don't copy across | |
66 | * the first BOOTLOADER_NO_BOARD_SETUP_OFFSET insns in this array. | |
67 | */ | |
68 | ||
47b1da81 | 69 | static const ARMInsnFixup bootloader[] = { |
10b8ec73 PC |
70 | { 0xe28fe008 }, /* add lr, pc, #8 */ |
71 | { 0xe51ff004 }, /* ldr pc, [pc, #-4] */ | |
72 | { 0, FIXUP_BOARD_SETUP }, | |
73 | #define BOOTLOADER_NO_BOARD_SETUP_OFFSET 3 | |
47b1da81 PM |
74 | { 0xe3a00000 }, /* mov r0, #0 */ |
75 | { 0xe59f1004 }, /* ldr r1, [pc, #4] */ | |
76 | { 0xe59f2004 }, /* ldr r2, [pc, #4] */ | |
77 | { 0xe59ff004 }, /* ldr pc, [pc, #4] */ | |
78 | { 0, FIXUP_BOARDID }, | |
79 | { 0, FIXUP_ARGPTR }, | |
80 | { 0, FIXUP_ENTRYPOINT }, | |
81 | { 0, FIXUP_TERMINATOR } | |
16406950 PB |
82 | }; |
83 | ||
9d5ba9bf ML |
84 | /* Handling for secondary CPU boot in a multicore system. |
85 | * Unlike the uniprocessor/primary CPU boot, this is platform | |
86 | * dependent. The default code here is based on the secondary | |
87 | * CPU boot protocol used on realview/vexpress boards, with | |
88 | * some parameterisation to increase its flexibility. | |
89 | * QEMU platform models for which this code is not appropriate | |
90 | * should override write_secondary_boot and secondary_cpu_reset_hook | |
91 | * instead. | |
92 | * | |
93 | * This code enables the interrupt controllers for the secondary | |
94 | * CPUs and then puts all the secondary CPUs into a loop waiting | |
95 | * for an interprocessor interrupt and polling a configurable | |
96 | * location for the kernel secondary CPU entry point. | |
97 | */ | |
bf471f79 PM |
98 | #define DSB_INSN 0xf57ff04f |
99 | #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */ | |
100 | ||
47b1da81 PM |
101 | static const ARMInsnFixup smpboot[] = { |
102 | { 0xe59f2028 }, /* ldr r2, gic_cpu_if */ | |
103 | { 0xe59f0028 }, /* ldr r0, bootreg_addr */ | |
104 | { 0xe3a01001 }, /* mov r1, #1 */ | |
105 | { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */ | |
106 | { 0xe3a010ff }, /* mov r1, #0xff */ | |
107 | { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */ | |
108 | { 0, FIXUP_DSB }, /* dsb */ | |
109 | { 0xe320f003 }, /* wfi */ | |
110 | { 0xe5901000 }, /* ldr r1, [r0] */ | |
111 | { 0xe1110001 }, /* tst r1, r1 */ | |
112 | { 0x0afffffb }, /* beq <wfi> */ | |
113 | { 0xe12fff11 }, /* bx r1 */ | |
114 | { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */ | |
115 | { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */ | |
116 | { 0, FIXUP_TERMINATOR } | |
9ee6e8bb PB |
117 | }; |
118 | ||
47b1da81 PM |
119 | static void write_bootloader(const char *name, hwaddr addr, |
120 | const ARMInsnFixup *insns, uint32_t *fixupcontext) | |
121 | { | |
122 | /* Fix up the specified bootloader fragment and write it into | |
123 | * guest memory using rom_add_blob_fixed(). fixupcontext is | |
124 | * an array giving the values to write in for the fixup types | |
125 | * which write a value into the code array. | |
126 | */ | |
127 | int i, len; | |
128 | uint32_t *code; | |
129 | ||
130 | len = 0; | |
131 | while (insns[len].fixup != FIXUP_TERMINATOR) { | |
132 | len++; | |
133 | } | |
134 | ||
135 | code = g_new0(uint32_t, len); | |
136 | ||
137 | for (i = 0; i < len; i++) { | |
138 | uint32_t insn = insns[i].insn; | |
139 | FixupType fixup = insns[i].fixup; | |
140 | ||
141 | switch (fixup) { | |
142 | case FIXUP_NONE: | |
143 | break; | |
144 | case FIXUP_BOARDID: | |
10b8ec73 | 145 | case FIXUP_BOARD_SETUP: |
47b1da81 PM |
146 | case FIXUP_ARGPTR: |
147 | case FIXUP_ENTRYPOINT: | |
148 | case FIXUP_GIC_CPU_IF: | |
149 | case FIXUP_BOOTREG: | |
150 | case FIXUP_DSB: | |
151 | insn = fixupcontext[fixup]; | |
152 | break; | |
153 | default: | |
154 | abort(); | |
155 | } | |
156 | code[i] = tswap32(insn); | |
157 | } | |
158 | ||
159 | rom_add_blob_fixed(name, code, len * sizeof(uint32_t), addr); | |
160 | ||
161 | g_free(code); | |
162 | } | |
163 | ||
9543b0cd | 164 | static void default_write_secondary(ARMCPU *cpu, |
9d5ba9bf ML |
165 | const struct arm_boot_info *info) |
166 | { | |
47b1da81 PM |
167 | uint32_t fixupcontext[FIXUP_MAX]; |
168 | ||
169 | fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr; | |
170 | fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr; | |
171 | if (arm_feature(&cpu->env, ARM_FEATURE_V7)) { | |
172 | fixupcontext[FIXUP_DSB] = DSB_INSN; | |
173 | } else { | |
174 | fixupcontext[FIXUP_DSB] = CP15_DSB_INSN; | |
9d5ba9bf | 175 | } |
47b1da81 PM |
176 | |
177 | write_bootloader("smpboot", info->smp_loader_start, | |
178 | smpboot, fixupcontext); | |
9d5ba9bf ML |
179 | } |
180 | ||
5d309320 | 181 | static void default_reset_secondary(ARMCPU *cpu, |
9d5ba9bf ML |
182 | const struct arm_boot_info *info) |
183 | { | |
4df81c6e | 184 | CPUState *cs = CPU(cpu); |
5d309320 | 185 | |
42874d3a PM |
186 | address_space_stl_notdirty(&address_space_memory, info->smp_bootreg_addr, |
187 | 0, MEMTXATTRS_UNSPECIFIED, NULL); | |
4df81c6e | 188 | cpu_set_pc(cs, info->smp_loader_start); |
9d5ba9bf ML |
189 | } |
190 | ||
83bfffec PM |
191 | static inline bool have_dtb(const struct arm_boot_info *info) |
192 | { | |
193 | return info->dtb_filename || info->get_dtb; | |
194 | } | |
195 | ||
52b43737 | 196 | #define WRITE_WORD(p, value) do { \ |
42874d3a PM |
197 | address_space_stl_notdirty(&address_space_memory, p, value, \ |
198 | MEMTXATTRS_UNSPECIFIED, NULL); \ | |
52b43737 PB |
199 | p += 4; \ |
200 | } while (0) | |
201 | ||
761c9eb0 | 202 | static void set_kernel_args(const struct arm_boot_info *info) |
16406950 | 203 | { |
761c9eb0 | 204 | int initrd_size = info->initrd_size; |
a8170e5e AK |
205 | hwaddr base = info->loader_start; |
206 | hwaddr p; | |
16406950 | 207 | |
52b43737 | 208 | p = base + KERNEL_ARGS_ADDR; |
16406950 | 209 | /* ATAG_CORE */ |
52b43737 PB |
210 | WRITE_WORD(p, 5); |
211 | WRITE_WORD(p, 0x54410001); | |
212 | WRITE_WORD(p, 1); | |
213 | WRITE_WORD(p, 0x1000); | |
214 | WRITE_WORD(p, 0); | |
16406950 | 215 | /* ATAG_MEM */ |
f93eb9ff | 216 | /* TODO: handle multiple chips on one ATAG list */ |
52b43737 PB |
217 | WRITE_WORD(p, 4); |
218 | WRITE_WORD(p, 0x54410002); | |
219 | WRITE_WORD(p, info->ram_size); | |
220 | WRITE_WORD(p, info->loader_start); | |
16406950 PB |
221 | if (initrd_size) { |
222 | /* ATAG_INITRD2 */ | |
52b43737 PB |
223 | WRITE_WORD(p, 4); |
224 | WRITE_WORD(p, 0x54420005); | |
fc53b7d4 | 225 | WRITE_WORD(p, info->initrd_start); |
52b43737 | 226 | WRITE_WORD(p, initrd_size); |
16406950 | 227 | } |
f93eb9ff | 228 | if (info->kernel_cmdline && *info->kernel_cmdline) { |
16406950 PB |
229 | /* ATAG_CMDLINE */ |
230 | int cmdline_size; | |
231 | ||
f93eb9ff | 232 | cmdline_size = strlen(info->kernel_cmdline); |
e1fe50dc | 233 | cpu_physical_memory_write(p + 8, info->kernel_cmdline, |
52b43737 | 234 | cmdline_size + 1); |
16406950 | 235 | cmdline_size = (cmdline_size >> 2) + 1; |
52b43737 PB |
236 | WRITE_WORD(p, cmdline_size + 2); |
237 | WRITE_WORD(p, 0x54410009); | |
238 | p += cmdline_size * 4; | |
16406950 | 239 | } |
f93eb9ff AZ |
240 | if (info->atag_board) { |
241 | /* ATAG_BOARD */ | |
242 | int atag_board_len; | |
52b43737 | 243 | uint8_t atag_board_buf[0x1000]; |
f93eb9ff | 244 | |
52b43737 PB |
245 | atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3; |
246 | WRITE_WORD(p, (atag_board_len + 8) >> 2); | |
247 | WRITE_WORD(p, 0x414f4d50); | |
248 | cpu_physical_memory_write(p, atag_board_buf, atag_board_len); | |
f93eb9ff AZ |
249 | p += atag_board_len; |
250 | } | |
16406950 | 251 | /* ATAG_END */ |
52b43737 PB |
252 | WRITE_WORD(p, 0); |
253 | WRITE_WORD(p, 0); | |
16406950 PB |
254 | } |
255 | ||
761c9eb0 | 256 | static void set_kernel_args_old(const struct arm_boot_info *info) |
2b8f2d41 | 257 | { |
a8170e5e | 258 | hwaddr p; |
52b43737 | 259 | const char *s; |
761c9eb0 | 260 | int initrd_size = info->initrd_size; |
a8170e5e | 261 | hwaddr base = info->loader_start; |
2b8f2d41 AZ |
262 | |
263 | /* see linux/include/asm-arm/setup.h */ | |
52b43737 | 264 | p = base + KERNEL_ARGS_ADDR; |
2b8f2d41 | 265 | /* page_size */ |
52b43737 | 266 | WRITE_WORD(p, 4096); |
2b8f2d41 | 267 | /* nr_pages */ |
52b43737 | 268 | WRITE_WORD(p, info->ram_size / 4096); |
2b8f2d41 | 269 | /* ramdisk_size */ |
52b43737 | 270 | WRITE_WORD(p, 0); |
2b8f2d41 AZ |
271 | #define FLAG_READONLY 1 |
272 | #define FLAG_RDLOAD 4 | |
273 | #define FLAG_RDPROMPT 8 | |
274 | /* flags */ | |
52b43737 | 275 | WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT); |
2b8f2d41 | 276 | /* rootdev */ |
52b43737 | 277 | WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */ |
2b8f2d41 | 278 | /* video_num_cols */ |
52b43737 | 279 | WRITE_WORD(p, 0); |
2b8f2d41 | 280 | /* video_num_rows */ |
52b43737 | 281 | WRITE_WORD(p, 0); |
2b8f2d41 | 282 | /* video_x */ |
52b43737 | 283 | WRITE_WORD(p, 0); |
2b8f2d41 | 284 | /* video_y */ |
52b43737 | 285 | WRITE_WORD(p, 0); |
2b8f2d41 | 286 | /* memc_control_reg */ |
52b43737 | 287 | WRITE_WORD(p, 0); |
2b8f2d41 AZ |
288 | /* unsigned char sounddefault */ |
289 | /* unsigned char adfsdrives */ | |
290 | /* unsigned char bytes_per_char_h */ | |
291 | /* unsigned char bytes_per_char_v */ | |
52b43737 | 292 | WRITE_WORD(p, 0); |
2b8f2d41 | 293 | /* pages_in_bank[4] */ |
52b43737 PB |
294 | WRITE_WORD(p, 0); |
295 | WRITE_WORD(p, 0); | |
296 | WRITE_WORD(p, 0); | |
297 | WRITE_WORD(p, 0); | |
2b8f2d41 | 298 | /* pages_in_vram */ |
52b43737 | 299 | WRITE_WORD(p, 0); |
2b8f2d41 | 300 | /* initrd_start */ |
fc53b7d4 PM |
301 | if (initrd_size) { |
302 | WRITE_WORD(p, info->initrd_start); | |
303 | } else { | |
52b43737 | 304 | WRITE_WORD(p, 0); |
fc53b7d4 | 305 | } |
2b8f2d41 | 306 | /* initrd_size */ |
52b43737 | 307 | WRITE_WORD(p, initrd_size); |
2b8f2d41 | 308 | /* rd_start */ |
52b43737 | 309 | WRITE_WORD(p, 0); |
2b8f2d41 | 310 | /* system_rev */ |
52b43737 | 311 | WRITE_WORD(p, 0); |
2b8f2d41 | 312 | /* system_serial_low */ |
52b43737 | 313 | WRITE_WORD(p, 0); |
2b8f2d41 | 314 | /* system_serial_high */ |
52b43737 | 315 | WRITE_WORD(p, 0); |
2b8f2d41 | 316 | /* mem_fclk_21285 */ |
52b43737 | 317 | WRITE_WORD(p, 0); |
2b8f2d41 | 318 | /* zero unused fields */ |
52b43737 PB |
319 | while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) { |
320 | WRITE_WORD(p, 0); | |
321 | } | |
322 | s = info->kernel_cmdline; | |
323 | if (s) { | |
e1fe50dc | 324 | cpu_physical_memory_write(p, s, strlen(s) + 1); |
52b43737 PB |
325 | } else { |
326 | WRITE_WORD(p, 0); | |
327 | } | |
2b8f2d41 AZ |
328 | } |
329 | ||
fee8ea12 AB |
330 | /** |
331 | * load_dtb() - load a device tree binary image into memory | |
332 | * @addr: the address to load the image at | |
333 | * @binfo: struct describing the boot environment | |
334 | * @addr_limit: upper limit of the available memory area at @addr | |
335 | * | |
336 | * Load a device tree supplied by the machine or by the user with the | |
337 | * '-dtb' command line option, and put it at offset @addr in target | |
338 | * memory. | |
339 | * | |
340 | * If @addr_limit contains a meaningful value (i.e., it is strictly greater | |
341 | * than @addr), the device tree is only loaded if its size does not exceed | |
342 | * the limit. | |
343 | * | |
344 | * Returns: the size of the device tree image on success, | |
345 | * 0 if the image size exceeds the limit, | |
346 | * -1 on errors. | |
a554ecb4 HZ |
347 | * |
348 | * Note: Must not be called unless have_dtb(binfo) is true. | |
fee8ea12 AB |
349 | */ |
350 | static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo, | |
351 | hwaddr addr_limit) | |
412beee6 | 352 | { |
412beee6 | 353 | void *fdt = NULL; |
412beee6 | 354 | int size, rc; |
70976c41 | 355 | uint32_t acells, scells; |
412beee6 | 356 | |
0fb79851 JR |
357 | if (binfo->dtb_filename) { |
358 | char *filename; | |
359 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename); | |
360 | if (!filename) { | |
361 | fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename); | |
362 | goto fail; | |
363 | } | |
412beee6 | 364 | |
0fb79851 JR |
365 | fdt = load_device_tree(filename, &size); |
366 | if (!fdt) { | |
367 | fprintf(stderr, "Couldn't open dtb file %s\n", filename); | |
368 | g_free(filename); | |
369 | goto fail; | |
370 | } | |
412beee6 | 371 | g_free(filename); |
a554ecb4 | 372 | } else { |
0fb79851 JR |
373 | fdt = binfo->get_dtb(binfo, &size); |
374 | if (!fdt) { | |
375 | fprintf(stderr, "Board was unable to create a dtb blob\n"); | |
376 | goto fail; | |
377 | } | |
412beee6 | 378 | } |
412beee6 | 379 | |
fee8ea12 AB |
380 | if (addr_limit > addr && size > (addr_limit - addr)) { |
381 | /* Installing the device tree blob at addr would exceed addr_limit. | |
382 | * Whether this constitutes failure is up to the caller to decide, | |
383 | * so just return 0 as size, i.e., no error. | |
384 | */ | |
385 | g_free(fdt); | |
386 | return 0; | |
387 | } | |
388 | ||
5a4348d1 PC |
389 | acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells"); |
390 | scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells"); | |
9bfa659e PM |
391 | if (acells == 0 || scells == 0) { |
392 | fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n"); | |
c23045de | 393 | goto fail; |
9bfa659e PM |
394 | } |
395 | ||
70976c41 PM |
396 | if (scells < 2 && binfo->ram_size >= (1ULL << 32)) { |
397 | /* This is user error so deserves a friendlier error message | |
398 | * than the failure of setprop_sized_cells would provide | |
399 | */ | |
9bfa659e PM |
400 | fprintf(stderr, "qemu: dtb file not compatible with " |
401 | "RAM size > 4GB\n"); | |
c23045de | 402 | goto fail; |
9bfa659e PM |
403 | } |
404 | ||
5a4348d1 PC |
405 | rc = qemu_fdt_setprop_sized_cells(fdt, "/memory", "reg", |
406 | acells, binfo->loader_start, | |
407 | scells, binfo->ram_size); | |
412beee6 GL |
408 | if (rc < 0) { |
409 | fprintf(stderr, "couldn't set /memory/reg\n"); | |
c23045de | 410 | goto fail; |
412beee6 GL |
411 | } |
412 | ||
5e87975c | 413 | if (binfo->kernel_cmdline && *binfo->kernel_cmdline) { |
5a4348d1 PC |
414 | rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", |
415 | binfo->kernel_cmdline); | |
5e87975c PC |
416 | if (rc < 0) { |
417 | fprintf(stderr, "couldn't set /chosen/bootargs\n"); | |
c23045de | 418 | goto fail; |
5e87975c | 419 | } |
412beee6 GL |
420 | } |
421 | ||
422 | if (binfo->initrd_size) { | |
5a4348d1 PC |
423 | rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start", |
424 | binfo->initrd_start); | |
412beee6 GL |
425 | if (rc < 0) { |
426 | fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n"); | |
c23045de | 427 | goto fail; |
412beee6 GL |
428 | } |
429 | ||
5a4348d1 PC |
430 | rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end", |
431 | binfo->initrd_start + binfo->initrd_size); | |
412beee6 GL |
432 | if (rc < 0) { |
433 | fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n"); | |
c23045de | 434 | goto fail; |
412beee6 GL |
435 | } |
436 | } | |
3b1cceb8 PM |
437 | |
438 | if (binfo->modify_dtb) { | |
439 | binfo->modify_dtb(binfo, fdt); | |
440 | } | |
441 | ||
5a4348d1 | 442 | qemu_fdt_dumpdtb(fdt, size); |
412beee6 | 443 | |
4c4bf654 AB |
444 | /* Put the DTB into the memory map as a ROM image: this will ensure |
445 | * the DTB is copied again upon reset, even if addr points into RAM. | |
446 | */ | |
447 | rom_add_blob_fixed("dtb", fdt, size, addr); | |
412beee6 | 448 | |
c23045de PM |
449 | g_free(fdt); |
450 | ||
fee8ea12 | 451 | return size; |
c23045de PM |
452 | |
453 | fail: | |
454 | g_free(fdt); | |
455 | return -1; | |
412beee6 GL |
456 | } |
457 | ||
6ed221b6 | 458 | static void do_cpu_reset(void *opaque) |
f2d74978 | 459 | { |
351d5666 | 460 | ARMCPU *cpu = opaque; |
4df81c6e | 461 | CPUState *cs = CPU(cpu); |
351d5666 | 462 | CPUARMState *env = &cpu->env; |
462a8bc6 | 463 | const struct arm_boot_info *info = env->boot_info; |
f2d74978 | 464 | |
4df81c6e | 465 | cpu_reset(cs); |
f2d74978 PB |
466 | if (info) { |
467 | if (!info->is_linux) { | |
468 | /* Jump to the entry point. */ | |
4df81c6e PC |
469 | uint64_t entry = info->entry; |
470 | ||
471 | if (!env->aarch64) { | |
a9047ec3 | 472 | env->thumb = info->entry & 1; |
4df81c6e | 473 | entry &= 0xfffffffe; |
a9047ec3 | 474 | } |
4df81c6e | 475 | cpu_set_pc(cs, entry); |
f2d74978 | 476 | } else { |
c8e829b7 GB |
477 | /* If we are booting Linux then we need to check whether we are |
478 | * booting into secure or non-secure state and adjust the state | |
479 | * accordingly. Out of reset, ARM is defined to be in secure state | |
480 | * (SCR.NS = 0), we change that here if non-secure boot has been | |
481 | * requested. | |
482 | */ | |
5097227c GB |
483 | if (arm_feature(env, ARM_FEATURE_EL3)) { |
484 | /* AArch64 is defined to come out of reset into EL3 if enabled. | |
485 | * If we are booting Linux then we need to adjust our EL as | |
486 | * Linux expects us to be in EL2 or EL1. AArch32 resets into | |
487 | * SVC, which Linux expects, so no privilege/exception level to | |
488 | * adjust. | |
489 | */ | |
490 | if (env->aarch64) { | |
491 | if (arm_feature(env, ARM_FEATURE_EL2)) { | |
492 | env->pstate = PSTATE_MODE_EL2h; | |
493 | } else { | |
494 | env->pstate = PSTATE_MODE_EL1h; | |
495 | } | |
496 | } | |
497 | ||
498 | /* Set to non-secure if not a secure boot */ | |
baf6b681 PC |
499 | if (!info->secure_boot && |
500 | (cs != first_cpu || !info->secure_board_setup)) { | |
5097227c GB |
501 | /* Linux expects non-secure state */ |
502 | env->cp15.scr_el3 |= SCR_NS; | |
503 | } | |
c8e829b7 GB |
504 | } |
505 | ||
4df81c6e PC |
506 | if (cs == first_cpu) { |
507 | cpu_set_pc(cs, info->loader_start); | |
4d9ebf75 | 508 | |
83bfffec | 509 | if (!have_dtb(info)) { |
412beee6 GL |
510 | if (old_param) { |
511 | set_kernel_args_old(info); | |
512 | } else { | |
513 | set_kernel_args(info); | |
514 | } | |
6ed221b6 | 515 | } |
f2d74978 | 516 | } else { |
5d309320 | 517 | info->secondary_cpu_reset_hook(cpu, info); |
f2d74978 PB |
518 | } |
519 | } | |
520 | } | |
f2d74978 PB |
521 | } |
522 | ||
07abe45c LE |
523 | /** |
524 | * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified | |
525 | * by key. | |
526 | * @fw_cfg: The firmware config instance to store the data in. | |
527 | * @size_key: The firmware config key to store the size of the loaded | |
528 | * data under, with fw_cfg_add_i32(). | |
529 | * @data_key: The firmware config key to store the loaded data under, | |
530 | * with fw_cfg_add_bytes(). | |
531 | * @image_name: The name of the image file to load. If it is NULL, the | |
532 | * function returns without doing anything. | |
533 | * @try_decompress: Whether the image should be decompressed (gunzipped) before | |
534 | * adding it to fw_cfg. If decompression fails, the image is | |
535 | * loaded as-is. | |
536 | * | |
537 | * In case of failure, the function prints an error message to stderr and the | |
538 | * process exits with status 1. | |
539 | */ | |
540 | static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key, | |
541 | uint16_t data_key, const char *image_name, | |
542 | bool try_decompress) | |
543 | { | |
544 | size_t size = -1; | |
545 | uint8_t *data; | |
546 | ||
547 | if (image_name == NULL) { | |
548 | return; | |
549 | } | |
550 | ||
551 | if (try_decompress) { | |
552 | size = load_image_gzipped_buffer(image_name, | |
553 | LOAD_IMAGE_MAX_GUNZIP_BYTES, &data); | |
554 | } | |
555 | ||
556 | if (size == (size_t)-1) { | |
557 | gchar *contents; | |
558 | gsize length; | |
559 | ||
560 | if (!g_file_get_contents(image_name, &contents, &length, NULL)) { | |
561 | fprintf(stderr, "failed to load \"%s\"\n", image_name); | |
562 | exit(1); | |
563 | } | |
564 | size = length; | |
565 | data = (uint8_t *)contents; | |
566 | } | |
567 | ||
568 | fw_cfg_add_i32(fw_cfg, size_key, size); | |
569 | fw_cfg_add_bytes(fw_cfg, data_key, data, size); | |
570 | } | |
571 | ||
d8b1ae42 PM |
572 | static int do_arm_linux_init(Object *obj, void *opaque) |
573 | { | |
574 | if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) { | |
575 | ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj); | |
576 | ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj); | |
577 | struct arm_boot_info *info = opaque; | |
578 | ||
579 | if (albifc->arm_linux_init) { | |
580 | albifc->arm_linux_init(albif, info->secure_boot); | |
581 | } | |
582 | } | |
583 | return 0; | |
584 | } | |
585 | ||
ac9d32e3 | 586 | static void arm_load_kernel_notify(Notifier *notifier, void *data) |
16406950 | 587 | { |
c6faa758 | 588 | CPUState *cs; |
16406950 PB |
589 | int kernel_size; |
590 | int initrd_size; | |
1c7b3754 | 591 | int is_linux = 0; |
92df8450 | 592 | uint64_t elf_entry, elf_low_addr, elf_high_addr; |
da0af40d | 593 | int elf_machine; |
4d9ebf75 | 594 | hwaddr entry, kernel_load_offset; |
ca20cf32 | 595 | int big_endian; |
4d9ebf75 | 596 | static const ARMInsnFixup *primary_loader; |
ac9d32e3 EA |
597 | ArmLoadKernelNotifier *n = DO_UPCAST(ArmLoadKernelNotifier, |
598 | notifier, notifier); | |
599 | ARMCPU *cpu = n->cpu; | |
600 | struct arm_boot_info *info = | |
601 | container_of(n, struct arm_boot_info, load_kernel_notifier); | |
16406950 | 602 | |
baf6b681 PC |
603 | /* The board code is not supposed to set secure_board_setup unless |
604 | * running its code in secure mode is actually possible, and KVM | |
605 | * doesn't support secure. | |
606 | */ | |
607 | assert(!(info->secure_board_setup && kvm_enabled())); | |
608 | ||
16406950 | 609 | /* Load the kernel. */ |
07abe45c | 610 | if (!info->kernel_filename || info->firmware_loaded) { |
69e7f76f AB |
611 | |
612 | if (have_dtb(info)) { | |
07abe45c LE |
613 | /* If we have a device tree blob, but no kernel to supply it to (or |
614 | * the kernel is supposed to be loaded by the bootloader), copy the | |
615 | * DTB to the base of RAM for the bootloader to pick up. | |
69e7f76f AB |
616 | */ |
617 | if (load_dtb(info->loader_start, info, 0) < 0) { | |
618 | exit(1); | |
619 | } | |
620 | } | |
621 | ||
07abe45c LE |
622 | if (info->kernel_filename) { |
623 | FWCfgState *fw_cfg; | |
624 | bool try_decompressing_kernel; | |
625 | ||
626 | fw_cfg = fw_cfg_find(); | |
627 | try_decompressing_kernel = arm_feature(&cpu->env, | |
628 | ARM_FEATURE_AARCH64); | |
629 | ||
630 | /* Expose the kernel, the command line, and the initrd in fw_cfg. | |
631 | * We don't process them here at all, it's all left to the | |
632 | * firmware. | |
633 | */ | |
634 | load_image_to_fw_cfg(fw_cfg, | |
635 | FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA, | |
636 | info->kernel_filename, | |
637 | try_decompressing_kernel); | |
638 | load_image_to_fw_cfg(fw_cfg, | |
639 | FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA, | |
640 | info->initrd_filename, false); | |
641 | ||
642 | if (info->kernel_cmdline) { | |
643 | fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, | |
644 | strlen(info->kernel_cmdline) + 1); | |
645 | fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, | |
646 | info->kernel_cmdline); | |
647 | } | |
648 | } | |
649 | ||
650 | /* We will start from address 0 (typically a boot ROM image) in the | |
651 | * same way as hardware. | |
9546dbab PM |
652 | */ |
653 | return; | |
16406950 | 654 | } |
daf90626 | 655 | |
4d9ebf75 MH |
656 | if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { |
657 | primary_loader = bootloader_aarch64; | |
658 | kernel_load_offset = KERNEL64_LOAD_ADDR; | |
da0af40d | 659 | elf_machine = EM_AARCH64; |
4d9ebf75 MH |
660 | } else { |
661 | primary_loader = bootloader; | |
10b8ec73 PC |
662 | if (!info->write_board_setup) { |
663 | primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET; | |
664 | } | |
4d9ebf75 | 665 | kernel_load_offset = KERNEL_LOAD_ADDR; |
da0af40d | 666 | elf_machine = EM_ARM; |
4d9ebf75 MH |
667 | } |
668 | ||
2ff3de68 | 669 | info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb"); |
412beee6 | 670 | |
9d5ba9bf ML |
671 | if (!info->secondary_cpu_reset_hook) { |
672 | info->secondary_cpu_reset_hook = default_reset_secondary; | |
673 | } | |
674 | if (!info->write_secondary_boot) { | |
675 | info->write_secondary_boot = default_write_secondary; | |
676 | } | |
677 | ||
f2d74978 PB |
678 | if (info->nb_cpus == 0) |
679 | info->nb_cpus = 1; | |
f93eb9ff | 680 | |
ca20cf32 BS |
681 | #ifdef TARGET_WORDS_BIGENDIAN |
682 | big_endian = 1; | |
683 | #else | |
684 | big_endian = 0; | |
685 | #endif | |
686 | ||
fc53b7d4 PM |
687 | /* We want to put the initrd far enough into RAM that when the |
688 | * kernel is uncompressed it will not clobber the initrd. However | |
689 | * on boards without much RAM we must ensure that we still leave | |
690 | * enough room for a decent sized initrd, and on boards with large | |
691 | * amounts of RAM we must avoid the initrd being so far up in RAM | |
692 | * that it is outside lowmem and inaccessible to the kernel. | |
693 | * So for boards with less than 256MB of RAM we put the initrd | |
694 | * halfway into RAM, and for boards with 256MB of RAM or more we put | |
695 | * the initrd at 128MB. | |
696 | */ | |
697 | info->initrd_start = info->loader_start + | |
698 | MIN(info->ram_size / 2, 128 * 1024 * 1024); | |
699 | ||
1c7b3754 | 700 | /* Assume that raw images are linux kernels, and ELF images are not. */ |
409dbce5 | 701 | kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry, |
92df8450 AB |
702 | &elf_low_addr, &elf_high_addr, big_endian, |
703 | elf_machine, 1); | |
704 | if (kernel_size > 0 && have_dtb(info)) { | |
705 | /* If there is still some room left at the base of RAM, try and put | |
706 | * the DTB there like we do for images loaded with -bios or -pflash. | |
707 | */ | |
708 | if (elf_low_addr > info->loader_start | |
709 | || elf_high_addr < info->loader_start) { | |
710 | /* Pass elf_low_addr as address limit to load_dtb if it may be | |
711 | * pointing into RAM, otherwise pass '0' (no limit) | |
712 | */ | |
713 | if (elf_low_addr < info->loader_start) { | |
714 | elf_low_addr = 0; | |
715 | } | |
716 | if (load_dtb(info->loader_start, info, elf_low_addr) < 0) { | |
717 | exit(1); | |
718 | } | |
719 | } | |
720 | } | |
1c7b3754 PB |
721 | entry = elf_entry; |
722 | if (kernel_size < 0) { | |
5a9154e0 | 723 | kernel_size = load_uimage(info->kernel_filename, &entry, NULL, |
25bda50a | 724 | &is_linux, NULL, NULL); |
1c7b3754 | 725 | } |
6f5d3cbe RJ |
726 | /* On aarch64, it's the bootloader's job to uncompress the kernel. */ |
727 | if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) { | |
728 | entry = info->loader_start + kernel_load_offset; | |
729 | kernel_size = load_image_gzipped(info->kernel_filename, entry, | |
730 | info->ram_size - kernel_load_offset); | |
731 | is_linux = 1; | |
732 | } | |
1c7b3754 | 733 | if (kernel_size < 0) { |
4d9ebf75 | 734 | entry = info->loader_start + kernel_load_offset; |
3b760e04 | 735 | kernel_size = load_image_targphys(info->kernel_filename, entry, |
4d9ebf75 | 736 | info->ram_size - kernel_load_offset); |
1c7b3754 PB |
737 | is_linux = 1; |
738 | } | |
739 | if (kernel_size < 0) { | |
f93eb9ff AZ |
740 | fprintf(stderr, "qemu: could not load kernel '%s'\n", |
741 | info->kernel_filename); | |
1c7b3754 PB |
742 | exit(1); |
743 | } | |
f2d74978 PB |
744 | info->entry = entry; |
745 | if (is_linux) { | |
47b1da81 PM |
746 | uint32_t fixupcontext[FIXUP_MAX]; |
747 | ||
f93eb9ff | 748 | if (info->initrd_filename) { |
fd76663e SB |
749 | initrd_size = load_ramdisk(info->initrd_filename, |
750 | info->initrd_start, | |
751 | info->ram_size - | |
752 | info->initrd_start); | |
753 | if (initrd_size < 0) { | |
754 | initrd_size = load_image_targphys(info->initrd_filename, | |
755 | info->initrd_start, | |
756 | info->ram_size - | |
757 | info->initrd_start); | |
758 | } | |
daf90626 PB |
759 | if (initrd_size < 0) { |
760 | fprintf(stderr, "qemu: could not load initrd '%s'\n", | |
f93eb9ff | 761 | info->initrd_filename); |
daf90626 PB |
762 | exit(1); |
763 | } | |
764 | } else { | |
765 | initrd_size = 0; | |
766 | } | |
412beee6 GL |
767 | info->initrd_size = initrd_size; |
768 | ||
47b1da81 | 769 | fixupcontext[FIXUP_BOARDID] = info->board_id; |
10b8ec73 | 770 | fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr; |
412beee6 GL |
771 | |
772 | /* for device tree boot, we pass the DTB directly in r2. Otherwise | |
773 | * we point to the kernel args. | |
774 | */ | |
83bfffec | 775 | if (have_dtb(info)) { |
76e2aef3 AG |
776 | hwaddr align; |
777 | hwaddr dtb_start; | |
778 | ||
779 | if (elf_machine == EM_AARCH64) { | |
780 | /* | |
781 | * Some AArch64 kernels on early bootup map the fdt region as | |
782 | * | |
783 | * [ ALIGN_DOWN(fdt, 2MB) ... ALIGN_DOWN(fdt, 2MB) + 2MB ] | |
784 | * | |
785 | * Let's play safe and prealign it to 2MB to give us some space. | |
786 | */ | |
787 | align = 2 * 1024 * 1024; | |
788 | } else { | |
789 | /* | |
790 | * Some 32bit kernels will trash anything in the 4K page the | |
791 | * initrd ends in, so make sure the DTB isn't caught up in that. | |
792 | */ | |
793 | align = 4096; | |
794 | } | |
795 | ||
796 | /* Place the DTB after the initrd in memory with alignment. */ | |
797 | dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size, align); | |
fee8ea12 | 798 | if (load_dtb(dtb_start, info, 0) < 0) { |
412beee6 GL |
799 | exit(1); |
800 | } | |
47b1da81 | 801 | fixupcontext[FIXUP_ARGPTR] = dtb_start; |
412beee6 | 802 | } else { |
47b1da81 | 803 | fixupcontext[FIXUP_ARGPTR] = info->loader_start + KERNEL_ARGS_ADDR; |
3871481c PM |
804 | if (info->ram_size >= (1ULL << 32)) { |
805 | fprintf(stderr, "qemu: RAM size must be less than 4GB to boot" | |
806 | " Linux kernel using ATAGS (try passing a device tree" | |
807 | " using -dtb)\n"); | |
808 | exit(1); | |
809 | } | |
412beee6 | 810 | } |
47b1da81 PM |
811 | fixupcontext[FIXUP_ENTRYPOINT] = entry; |
812 | ||
813 | write_bootloader("bootloader", info->loader_start, | |
4d9ebf75 | 814 | primary_loader, fixupcontext); |
47b1da81 | 815 | |
52b43737 | 816 | if (info->nb_cpus > 1) { |
9543b0cd | 817 | info->write_secondary_boot(cpu, info); |
52b43737 | 818 | } |
10b8ec73 PC |
819 | if (info->write_board_setup) { |
820 | info->write_board_setup(cpu, info); | |
821 | } | |
d8b1ae42 PM |
822 | |
823 | /* Notify devices which need to fake up firmware initialization | |
824 | * that we're doing a direct kernel boot. | |
825 | */ | |
826 | object_child_foreach_recursive(object_get_root(), | |
827 | do_arm_linux_init, info); | |
16406950 | 828 | } |
f2d74978 | 829 | info->is_linux = is_linux; |
6ed221b6 | 830 | |
c6faa758 AB |
831 | for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) { |
832 | ARM_CPU(cs)->env.boot_info = info; | |
6ed221b6 | 833 | } |
16406950 | 834 | } |
ac9d32e3 EA |
835 | |
836 | void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info) | |
837 | { | |
63a183ed EA |
838 | CPUState *cs; |
839 | ||
ac9d32e3 EA |
840 | info->load_kernel_notifier.cpu = cpu; |
841 | info->load_kernel_notifier.notifier.notify = arm_load_kernel_notify; | |
842 | qemu_add_machine_init_done_notifier(&info->load_kernel_notifier.notifier); | |
63a183ed EA |
843 | |
844 | /* CPU objects (unlike devices) are not automatically reset on system | |
845 | * reset, so we must always register a handler to do so. If we're | |
846 | * actually loading a kernel, the handler is also responsible for | |
847 | * arranging that we start it correctly. | |
848 | */ | |
849 | for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) { | |
850 | qemu_register_reset(do_cpu_reset, ARM_CPU(cs)); | |
851 | } | |
ac9d32e3 | 852 | } |
d8b1ae42 PM |
853 | |
854 | static const TypeInfo arm_linux_boot_if_info = { | |
855 | .name = TYPE_ARM_LINUX_BOOT_IF, | |
856 | .parent = TYPE_INTERFACE, | |
857 | .class_size = sizeof(ARMLinuxBootIfClass), | |
858 | }; | |
859 | ||
860 | static void arm_linux_boot_register_types(void) | |
861 | { | |
862 | type_register_static(&arm_linux_boot_if_info); | |
863 | } | |
864 | ||
865 | type_init(arm_linux_boot_register_types) |