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