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