153018216SPaolo Bonzini /* 253018216SPaolo Bonzini * ARM kernel loader. 353018216SPaolo Bonzini * 453018216SPaolo Bonzini * Copyright (c) 2006-2007 CodeSourcery. 553018216SPaolo Bonzini * Written by Paul Brook 653018216SPaolo Bonzini * 753018216SPaolo Bonzini * This code is licensed under the GPL. 853018216SPaolo Bonzini */ 953018216SPaolo Bonzini 1012b16722SPeter Maydell #include "qemu/osdep.h" 11da34e65cSMarkus Armbruster #include "qapi/error.h" 1253018216SPaolo Bonzini #include "hw/hw.h" 13bd2be150SPeter Maydell #include "hw/arm/arm.h" 14d8b1ae42SPeter Maydell #include "hw/arm/linux-boot-if.h" 15baf6b681SPeter Crosthwaite #include "sysemu/kvm.h" 1653018216SPaolo Bonzini #include "sysemu/sysemu.h" 17*9695200aSShannon Zhao #include "sysemu/numa.h" 1853018216SPaolo Bonzini #include "hw/boards.h" 1953018216SPaolo Bonzini #include "hw/loader.h" 2053018216SPaolo Bonzini #include "elf.h" 2153018216SPaolo Bonzini #include "sysemu/device_tree.h" 2253018216SPaolo Bonzini #include "qemu/config-file.h" 232198a121SEdgar E. Iglesias #include "exec/address-spaces.h" 2453018216SPaolo Bonzini 254d9ebf75SMian M. Hamayun /* Kernel boot protocol is specified in the kernel docs 264d9ebf75SMian M. Hamayun * Documentation/arm/Booting and Documentation/arm64/booting.txt 274d9ebf75SMian M. Hamayun * They have different preferred image load offsets from system RAM base. 284d9ebf75SMian M. Hamayun */ 2953018216SPaolo Bonzini #define KERNEL_ARGS_ADDR 0x100 3053018216SPaolo Bonzini #define KERNEL_LOAD_ADDR 0x00010000 314d9ebf75SMian M. Hamayun #define KERNEL64_LOAD_ADDR 0x00080000 3253018216SPaolo Bonzini 3347b1da81SPeter Maydell typedef enum { 3447b1da81SPeter Maydell FIXUP_NONE = 0, /* do nothing */ 3547b1da81SPeter Maydell FIXUP_TERMINATOR, /* end of insns */ 3647b1da81SPeter Maydell FIXUP_BOARDID, /* overwrite with board ID number */ 3710b8ec73SPeter Crosthwaite FIXUP_BOARD_SETUP, /* overwrite with board specific setup code address */ 3847b1da81SPeter Maydell FIXUP_ARGPTR, /* overwrite with pointer to kernel args */ 3947b1da81SPeter Maydell FIXUP_ENTRYPOINT, /* overwrite with kernel entry point */ 4047b1da81SPeter Maydell FIXUP_GIC_CPU_IF, /* overwrite with GIC CPU interface address */ 4147b1da81SPeter Maydell FIXUP_BOOTREG, /* overwrite with boot register address */ 4247b1da81SPeter Maydell FIXUP_DSB, /* overwrite with correct DSB insn for cpu */ 4347b1da81SPeter Maydell FIXUP_MAX, 4447b1da81SPeter Maydell } FixupType; 4547b1da81SPeter Maydell 4647b1da81SPeter Maydell typedef struct ARMInsnFixup { 4747b1da81SPeter Maydell uint32_t insn; 4847b1da81SPeter Maydell FixupType fixup; 4947b1da81SPeter Maydell } ARMInsnFixup; 5047b1da81SPeter Maydell 514d9ebf75SMian M. Hamayun static const ARMInsnFixup bootloader_aarch64[] = { 524d9ebf75SMian M. Hamayun { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */ 534d9ebf75SMian M. Hamayun { 0xaa1f03e1 }, /* mov x1, xzr */ 544d9ebf75SMian M. Hamayun { 0xaa1f03e2 }, /* mov x2, xzr */ 554d9ebf75SMian M. Hamayun { 0xaa1f03e3 }, /* mov x3, xzr */ 564d9ebf75SMian M. Hamayun { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */ 574d9ebf75SMian M. Hamayun { 0xd61f0080 }, /* br x4 ; Jump to the kernel entry point */ 584d9ebf75SMian M. Hamayun { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */ 594d9ebf75SMian M. Hamayun { 0 }, /* .word @DTB Higher 32-bits */ 604d9ebf75SMian M. Hamayun { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */ 614d9ebf75SMian M. Hamayun { 0 }, /* .word @Kernel Entry Higher 32-bits */ 624d9ebf75SMian M. Hamayun { 0, FIXUP_TERMINATOR } 634d9ebf75SMian M. Hamayun }; 644d9ebf75SMian M. Hamayun 6510b8ec73SPeter Crosthwaite /* A very small bootloader: call the board-setup code (if needed), 6610b8ec73SPeter Crosthwaite * set r0-r2, then jump to the kernel. 6710b8ec73SPeter Crosthwaite * If we're not calling boot setup code then we don't copy across 6810b8ec73SPeter Crosthwaite * the first BOOTLOADER_NO_BOARD_SETUP_OFFSET insns in this array. 6910b8ec73SPeter Crosthwaite */ 7010b8ec73SPeter Crosthwaite 7147b1da81SPeter Maydell static const ARMInsnFixup bootloader[] = { 72b4850e5aSSylvain Garrigues { 0xe28fe004 }, /* add lr, pc, #4 */ 7310b8ec73SPeter Crosthwaite { 0xe51ff004 }, /* ldr pc, [pc, #-4] */ 7410b8ec73SPeter Crosthwaite { 0, FIXUP_BOARD_SETUP }, 7510b8ec73SPeter Crosthwaite #define BOOTLOADER_NO_BOARD_SETUP_OFFSET 3 7647b1da81SPeter Maydell { 0xe3a00000 }, /* mov r0, #0 */ 7747b1da81SPeter Maydell { 0xe59f1004 }, /* ldr r1, [pc, #4] */ 7847b1da81SPeter Maydell { 0xe59f2004 }, /* ldr r2, [pc, #4] */ 7947b1da81SPeter Maydell { 0xe59ff004 }, /* ldr pc, [pc, #4] */ 8047b1da81SPeter Maydell { 0, FIXUP_BOARDID }, 8147b1da81SPeter Maydell { 0, FIXUP_ARGPTR }, 8247b1da81SPeter Maydell { 0, FIXUP_ENTRYPOINT }, 8347b1da81SPeter Maydell { 0, FIXUP_TERMINATOR } 8453018216SPaolo Bonzini }; 8553018216SPaolo Bonzini 8653018216SPaolo Bonzini /* Handling for secondary CPU boot in a multicore system. 8753018216SPaolo Bonzini * Unlike the uniprocessor/primary CPU boot, this is platform 8853018216SPaolo Bonzini * dependent. The default code here is based on the secondary 8953018216SPaolo Bonzini * CPU boot protocol used on realview/vexpress boards, with 9053018216SPaolo Bonzini * some parameterisation to increase its flexibility. 9153018216SPaolo Bonzini * QEMU platform models for which this code is not appropriate 9253018216SPaolo Bonzini * should override write_secondary_boot and secondary_cpu_reset_hook 9353018216SPaolo Bonzini * instead. 9453018216SPaolo Bonzini * 9553018216SPaolo Bonzini * This code enables the interrupt controllers for the secondary 9653018216SPaolo Bonzini * CPUs and then puts all the secondary CPUs into a loop waiting 9753018216SPaolo Bonzini * for an interprocessor interrupt and polling a configurable 9853018216SPaolo Bonzini * location for the kernel secondary CPU entry point. 9953018216SPaolo Bonzini */ 10053018216SPaolo Bonzini #define DSB_INSN 0xf57ff04f 10153018216SPaolo Bonzini #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */ 10253018216SPaolo Bonzini 10347b1da81SPeter Maydell static const ARMInsnFixup smpboot[] = { 10447b1da81SPeter Maydell { 0xe59f2028 }, /* ldr r2, gic_cpu_if */ 10547b1da81SPeter Maydell { 0xe59f0028 }, /* ldr r0, bootreg_addr */ 10647b1da81SPeter Maydell { 0xe3a01001 }, /* mov r1, #1 */ 10747b1da81SPeter Maydell { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */ 10847b1da81SPeter Maydell { 0xe3a010ff }, /* mov r1, #0xff */ 10947b1da81SPeter Maydell { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */ 11047b1da81SPeter Maydell { 0, FIXUP_DSB }, /* dsb */ 11147b1da81SPeter Maydell { 0xe320f003 }, /* wfi */ 11247b1da81SPeter Maydell { 0xe5901000 }, /* ldr r1, [r0] */ 11347b1da81SPeter Maydell { 0xe1110001 }, /* tst r1, r1 */ 11447b1da81SPeter Maydell { 0x0afffffb }, /* beq <wfi> */ 11547b1da81SPeter Maydell { 0xe12fff11 }, /* bx r1 */ 11647b1da81SPeter Maydell { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */ 11747b1da81SPeter Maydell { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */ 11847b1da81SPeter Maydell { 0, FIXUP_TERMINATOR } 11953018216SPaolo Bonzini }; 12053018216SPaolo Bonzini 12147b1da81SPeter Maydell static void write_bootloader(const char *name, hwaddr addr, 12247b1da81SPeter Maydell const ARMInsnFixup *insns, uint32_t *fixupcontext) 12347b1da81SPeter Maydell { 12447b1da81SPeter Maydell /* Fix up the specified bootloader fragment and write it into 12547b1da81SPeter Maydell * guest memory using rom_add_blob_fixed(). fixupcontext is 12647b1da81SPeter Maydell * an array giving the values to write in for the fixup types 12747b1da81SPeter Maydell * which write a value into the code array. 12847b1da81SPeter Maydell */ 12947b1da81SPeter Maydell int i, len; 13047b1da81SPeter Maydell uint32_t *code; 13147b1da81SPeter Maydell 13247b1da81SPeter Maydell len = 0; 13347b1da81SPeter Maydell while (insns[len].fixup != FIXUP_TERMINATOR) { 13447b1da81SPeter Maydell len++; 13547b1da81SPeter Maydell } 13647b1da81SPeter Maydell 13747b1da81SPeter Maydell code = g_new0(uint32_t, len); 13847b1da81SPeter Maydell 13947b1da81SPeter Maydell for (i = 0; i < len; i++) { 14047b1da81SPeter Maydell uint32_t insn = insns[i].insn; 14147b1da81SPeter Maydell FixupType fixup = insns[i].fixup; 14247b1da81SPeter Maydell 14347b1da81SPeter Maydell switch (fixup) { 14447b1da81SPeter Maydell case FIXUP_NONE: 14547b1da81SPeter Maydell break; 14647b1da81SPeter Maydell case FIXUP_BOARDID: 14710b8ec73SPeter Crosthwaite case FIXUP_BOARD_SETUP: 14847b1da81SPeter Maydell case FIXUP_ARGPTR: 14947b1da81SPeter Maydell case FIXUP_ENTRYPOINT: 15047b1da81SPeter Maydell case FIXUP_GIC_CPU_IF: 15147b1da81SPeter Maydell case FIXUP_BOOTREG: 15247b1da81SPeter Maydell case FIXUP_DSB: 15347b1da81SPeter Maydell insn = fixupcontext[fixup]; 15447b1da81SPeter Maydell break; 15547b1da81SPeter Maydell default: 15647b1da81SPeter Maydell abort(); 15747b1da81SPeter Maydell } 15847b1da81SPeter Maydell code[i] = tswap32(insn); 15947b1da81SPeter Maydell } 16047b1da81SPeter Maydell 16147b1da81SPeter Maydell rom_add_blob_fixed(name, code, len * sizeof(uint32_t), addr); 16247b1da81SPeter Maydell 16347b1da81SPeter Maydell g_free(code); 16447b1da81SPeter Maydell } 16547b1da81SPeter Maydell 16653018216SPaolo Bonzini static void default_write_secondary(ARMCPU *cpu, 16753018216SPaolo Bonzini const struct arm_boot_info *info) 16853018216SPaolo Bonzini { 16947b1da81SPeter Maydell uint32_t fixupcontext[FIXUP_MAX]; 17047b1da81SPeter Maydell 17147b1da81SPeter Maydell fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr; 17247b1da81SPeter Maydell fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr; 17347b1da81SPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_V7)) { 17447b1da81SPeter Maydell fixupcontext[FIXUP_DSB] = DSB_INSN; 17547b1da81SPeter Maydell } else { 17647b1da81SPeter Maydell fixupcontext[FIXUP_DSB] = CP15_DSB_INSN; 17753018216SPaolo Bonzini } 17847b1da81SPeter Maydell 17947b1da81SPeter Maydell write_bootloader("smpboot", info->smp_loader_start, 18047b1da81SPeter Maydell smpboot, fixupcontext); 18153018216SPaolo Bonzini } 18253018216SPaolo Bonzini 183716536a9SAndrew Baumann void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu, 184716536a9SAndrew Baumann const struct arm_boot_info *info, 185716536a9SAndrew Baumann hwaddr mvbar_addr) 186716536a9SAndrew Baumann { 187716536a9SAndrew Baumann int n; 188716536a9SAndrew Baumann uint32_t mvbar_blob[] = { 189716536a9SAndrew Baumann /* mvbar_addr: secure monitor vectors 190716536a9SAndrew Baumann * Default unimplemented and unused vectors to spin. Makes it 191716536a9SAndrew Baumann * easier to debug (as opposed to the CPU running away). 192716536a9SAndrew Baumann */ 193716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 194716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 195716536a9SAndrew Baumann 0xe1b0f00e, /* movs pc, lr ;SMC exception return */ 196716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 197716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 198716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 199716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 200716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 201716536a9SAndrew Baumann }; 202716536a9SAndrew Baumann uint32_t board_setup_blob[] = { 203716536a9SAndrew Baumann /* board setup addr */ 204716536a9SAndrew Baumann 0xe3a00e00 + (mvbar_addr >> 4), /* mov r0, #mvbar_addr */ 205716536a9SAndrew Baumann 0xee0c0f30, /* mcr p15, 0, r0, c12, c0, 1 ;set MVBAR */ 206716536a9SAndrew Baumann 0xee110f11, /* mrc p15, 0, r0, c1 , c1, 0 ;read SCR */ 207716536a9SAndrew Baumann 0xe3800031, /* orr r0, #0x31 ;enable AW, FW, NS */ 208716536a9SAndrew Baumann 0xee010f11, /* mcr p15, 0, r0, c1, c1, 0 ;write SCR */ 209716536a9SAndrew Baumann 0xe1a0100e, /* mov r1, lr ;save LR across SMC */ 210716536a9SAndrew Baumann 0xe1600070, /* smc #0 ;call monitor to flush SCR */ 211716536a9SAndrew Baumann 0xe1a0f001, /* mov pc, r1 ;return */ 212716536a9SAndrew Baumann }; 213716536a9SAndrew Baumann 214716536a9SAndrew Baumann /* check that mvbar_addr is correctly aligned and relocatable (using MOV) */ 215716536a9SAndrew Baumann assert((mvbar_addr & 0x1f) == 0 && (mvbar_addr >> 4) < 0x100); 216716536a9SAndrew Baumann 217716536a9SAndrew Baumann /* check that these blobs don't overlap */ 218716536a9SAndrew Baumann assert((mvbar_addr + sizeof(mvbar_blob) <= info->board_setup_addr) 219716536a9SAndrew Baumann || (info->board_setup_addr + sizeof(board_setup_blob) <= mvbar_addr)); 220716536a9SAndrew Baumann 221716536a9SAndrew Baumann for (n = 0; n < ARRAY_SIZE(mvbar_blob); n++) { 222716536a9SAndrew Baumann mvbar_blob[n] = tswap32(mvbar_blob[n]); 223716536a9SAndrew Baumann } 224716536a9SAndrew Baumann rom_add_blob_fixed("board-setup-mvbar", mvbar_blob, sizeof(mvbar_blob), 225716536a9SAndrew Baumann mvbar_addr); 226716536a9SAndrew Baumann 227716536a9SAndrew Baumann for (n = 0; n < ARRAY_SIZE(board_setup_blob); n++) { 228716536a9SAndrew Baumann board_setup_blob[n] = tswap32(board_setup_blob[n]); 229716536a9SAndrew Baumann } 230716536a9SAndrew Baumann rom_add_blob_fixed("board-setup", board_setup_blob, 231716536a9SAndrew Baumann sizeof(board_setup_blob), info->board_setup_addr); 232716536a9SAndrew Baumann } 233716536a9SAndrew Baumann 23453018216SPaolo Bonzini static void default_reset_secondary(ARMCPU *cpu, 23553018216SPaolo Bonzini const struct arm_boot_info *info) 23653018216SPaolo Bonzini { 2374df81c6eSPeter Crosthwaite CPUState *cs = CPU(cpu); 23853018216SPaolo Bonzini 23942874d3aSPeter Maydell address_space_stl_notdirty(&address_space_memory, info->smp_bootreg_addr, 24042874d3aSPeter Maydell 0, MEMTXATTRS_UNSPECIFIED, NULL); 2414df81c6eSPeter Crosthwaite cpu_set_pc(cs, info->smp_loader_start); 24253018216SPaolo Bonzini } 24353018216SPaolo Bonzini 24483bfffecSPeter Maydell static inline bool have_dtb(const struct arm_boot_info *info) 24583bfffecSPeter Maydell { 24683bfffecSPeter Maydell return info->dtb_filename || info->get_dtb; 24783bfffecSPeter Maydell } 24883bfffecSPeter Maydell 24953018216SPaolo Bonzini #define WRITE_WORD(p, value) do { \ 25042874d3aSPeter Maydell address_space_stl_notdirty(&address_space_memory, p, value, \ 25142874d3aSPeter Maydell MEMTXATTRS_UNSPECIFIED, NULL); \ 25253018216SPaolo Bonzini p += 4; \ 25353018216SPaolo Bonzini } while (0) 25453018216SPaolo Bonzini 25553018216SPaolo Bonzini static void set_kernel_args(const struct arm_boot_info *info) 25653018216SPaolo Bonzini { 25753018216SPaolo Bonzini int initrd_size = info->initrd_size; 25853018216SPaolo Bonzini hwaddr base = info->loader_start; 25953018216SPaolo Bonzini hwaddr p; 26053018216SPaolo Bonzini 26153018216SPaolo Bonzini p = base + KERNEL_ARGS_ADDR; 26253018216SPaolo Bonzini /* ATAG_CORE */ 26353018216SPaolo Bonzini WRITE_WORD(p, 5); 26453018216SPaolo Bonzini WRITE_WORD(p, 0x54410001); 26553018216SPaolo Bonzini WRITE_WORD(p, 1); 26653018216SPaolo Bonzini WRITE_WORD(p, 0x1000); 26753018216SPaolo Bonzini WRITE_WORD(p, 0); 26853018216SPaolo Bonzini /* ATAG_MEM */ 26953018216SPaolo Bonzini /* TODO: handle multiple chips on one ATAG list */ 27053018216SPaolo Bonzini WRITE_WORD(p, 4); 27153018216SPaolo Bonzini WRITE_WORD(p, 0x54410002); 27253018216SPaolo Bonzini WRITE_WORD(p, info->ram_size); 27353018216SPaolo Bonzini WRITE_WORD(p, info->loader_start); 27453018216SPaolo Bonzini if (initrd_size) { 27553018216SPaolo Bonzini /* ATAG_INITRD2 */ 27653018216SPaolo Bonzini WRITE_WORD(p, 4); 27753018216SPaolo Bonzini WRITE_WORD(p, 0x54420005); 27853018216SPaolo Bonzini WRITE_WORD(p, info->initrd_start); 27953018216SPaolo Bonzini WRITE_WORD(p, initrd_size); 28053018216SPaolo Bonzini } 28153018216SPaolo Bonzini if (info->kernel_cmdline && *info->kernel_cmdline) { 28253018216SPaolo Bonzini /* ATAG_CMDLINE */ 28353018216SPaolo Bonzini int cmdline_size; 28453018216SPaolo Bonzini 28553018216SPaolo Bonzini cmdline_size = strlen(info->kernel_cmdline); 286e1fe50dcSStefan Weil cpu_physical_memory_write(p + 8, info->kernel_cmdline, 28753018216SPaolo Bonzini cmdline_size + 1); 28853018216SPaolo Bonzini cmdline_size = (cmdline_size >> 2) + 1; 28953018216SPaolo Bonzini WRITE_WORD(p, cmdline_size + 2); 29053018216SPaolo Bonzini WRITE_WORD(p, 0x54410009); 29153018216SPaolo Bonzini p += cmdline_size * 4; 29253018216SPaolo Bonzini } 29353018216SPaolo Bonzini if (info->atag_board) { 29453018216SPaolo Bonzini /* ATAG_BOARD */ 29553018216SPaolo Bonzini int atag_board_len; 29653018216SPaolo Bonzini uint8_t atag_board_buf[0x1000]; 29753018216SPaolo Bonzini 29853018216SPaolo Bonzini atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3; 29953018216SPaolo Bonzini WRITE_WORD(p, (atag_board_len + 8) >> 2); 30053018216SPaolo Bonzini WRITE_WORD(p, 0x414f4d50); 30153018216SPaolo Bonzini cpu_physical_memory_write(p, atag_board_buf, atag_board_len); 30253018216SPaolo Bonzini p += atag_board_len; 30353018216SPaolo Bonzini } 30453018216SPaolo Bonzini /* ATAG_END */ 30553018216SPaolo Bonzini WRITE_WORD(p, 0); 30653018216SPaolo Bonzini WRITE_WORD(p, 0); 30753018216SPaolo Bonzini } 30853018216SPaolo Bonzini 30953018216SPaolo Bonzini static void set_kernel_args_old(const struct arm_boot_info *info) 31053018216SPaolo Bonzini { 31153018216SPaolo Bonzini hwaddr p; 31253018216SPaolo Bonzini const char *s; 31353018216SPaolo Bonzini int initrd_size = info->initrd_size; 31453018216SPaolo Bonzini hwaddr base = info->loader_start; 31553018216SPaolo Bonzini 31653018216SPaolo Bonzini /* see linux/include/asm-arm/setup.h */ 31753018216SPaolo Bonzini p = base + KERNEL_ARGS_ADDR; 31853018216SPaolo Bonzini /* page_size */ 31953018216SPaolo Bonzini WRITE_WORD(p, 4096); 32053018216SPaolo Bonzini /* nr_pages */ 32153018216SPaolo Bonzini WRITE_WORD(p, info->ram_size / 4096); 32253018216SPaolo Bonzini /* ramdisk_size */ 32353018216SPaolo Bonzini WRITE_WORD(p, 0); 32453018216SPaolo Bonzini #define FLAG_READONLY 1 32553018216SPaolo Bonzini #define FLAG_RDLOAD 4 32653018216SPaolo Bonzini #define FLAG_RDPROMPT 8 32753018216SPaolo Bonzini /* flags */ 32853018216SPaolo Bonzini WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT); 32953018216SPaolo Bonzini /* rootdev */ 33053018216SPaolo Bonzini WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */ 33153018216SPaolo Bonzini /* video_num_cols */ 33253018216SPaolo Bonzini WRITE_WORD(p, 0); 33353018216SPaolo Bonzini /* video_num_rows */ 33453018216SPaolo Bonzini WRITE_WORD(p, 0); 33553018216SPaolo Bonzini /* video_x */ 33653018216SPaolo Bonzini WRITE_WORD(p, 0); 33753018216SPaolo Bonzini /* video_y */ 33853018216SPaolo Bonzini WRITE_WORD(p, 0); 33953018216SPaolo Bonzini /* memc_control_reg */ 34053018216SPaolo Bonzini WRITE_WORD(p, 0); 34153018216SPaolo Bonzini /* unsigned char sounddefault */ 34253018216SPaolo Bonzini /* unsigned char adfsdrives */ 34353018216SPaolo Bonzini /* unsigned char bytes_per_char_h */ 34453018216SPaolo Bonzini /* unsigned char bytes_per_char_v */ 34553018216SPaolo Bonzini WRITE_WORD(p, 0); 34653018216SPaolo Bonzini /* pages_in_bank[4] */ 34753018216SPaolo Bonzini WRITE_WORD(p, 0); 34853018216SPaolo Bonzini WRITE_WORD(p, 0); 34953018216SPaolo Bonzini WRITE_WORD(p, 0); 35053018216SPaolo Bonzini WRITE_WORD(p, 0); 35153018216SPaolo Bonzini /* pages_in_vram */ 35253018216SPaolo Bonzini WRITE_WORD(p, 0); 35353018216SPaolo Bonzini /* initrd_start */ 35453018216SPaolo Bonzini if (initrd_size) { 35553018216SPaolo Bonzini WRITE_WORD(p, info->initrd_start); 35653018216SPaolo Bonzini } else { 35753018216SPaolo Bonzini WRITE_WORD(p, 0); 35853018216SPaolo Bonzini } 35953018216SPaolo Bonzini /* initrd_size */ 36053018216SPaolo Bonzini WRITE_WORD(p, initrd_size); 36153018216SPaolo Bonzini /* rd_start */ 36253018216SPaolo Bonzini WRITE_WORD(p, 0); 36353018216SPaolo Bonzini /* system_rev */ 36453018216SPaolo Bonzini WRITE_WORD(p, 0); 36553018216SPaolo Bonzini /* system_serial_low */ 36653018216SPaolo Bonzini WRITE_WORD(p, 0); 36753018216SPaolo Bonzini /* system_serial_high */ 36853018216SPaolo Bonzini WRITE_WORD(p, 0); 36953018216SPaolo Bonzini /* mem_fclk_21285 */ 37053018216SPaolo Bonzini WRITE_WORD(p, 0); 37153018216SPaolo Bonzini /* zero unused fields */ 37253018216SPaolo Bonzini while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) { 37353018216SPaolo Bonzini WRITE_WORD(p, 0); 37453018216SPaolo Bonzini } 37553018216SPaolo Bonzini s = info->kernel_cmdline; 37653018216SPaolo Bonzini if (s) { 377e1fe50dcSStefan Weil cpu_physical_memory_write(p, s, strlen(s) + 1); 37853018216SPaolo Bonzini } else { 37953018216SPaolo Bonzini WRITE_WORD(p, 0); 38053018216SPaolo Bonzini } 38153018216SPaolo Bonzini } 38253018216SPaolo Bonzini 383fee8ea12SArd Biesheuvel /** 384fee8ea12SArd Biesheuvel * load_dtb() - load a device tree binary image into memory 385fee8ea12SArd Biesheuvel * @addr: the address to load the image at 386fee8ea12SArd Biesheuvel * @binfo: struct describing the boot environment 387fee8ea12SArd Biesheuvel * @addr_limit: upper limit of the available memory area at @addr 388fee8ea12SArd Biesheuvel * 389fee8ea12SArd Biesheuvel * Load a device tree supplied by the machine or by the user with the 390fee8ea12SArd Biesheuvel * '-dtb' command line option, and put it at offset @addr in target 391fee8ea12SArd Biesheuvel * memory. 392fee8ea12SArd Biesheuvel * 393fee8ea12SArd Biesheuvel * If @addr_limit contains a meaningful value (i.e., it is strictly greater 394fee8ea12SArd Biesheuvel * than @addr), the device tree is only loaded if its size does not exceed 395fee8ea12SArd Biesheuvel * the limit. 396fee8ea12SArd Biesheuvel * 397fee8ea12SArd Biesheuvel * Returns: the size of the device tree image on success, 398fee8ea12SArd Biesheuvel * 0 if the image size exceeds the limit, 399fee8ea12SArd Biesheuvel * -1 on errors. 400a554ecb4Szhanghailiang * 401a554ecb4Szhanghailiang * Note: Must not be called unless have_dtb(binfo) is true. 402fee8ea12SArd Biesheuvel */ 403fee8ea12SArd Biesheuvel static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo, 404fee8ea12SArd Biesheuvel hwaddr addr_limit) 40553018216SPaolo Bonzini { 40653018216SPaolo Bonzini void *fdt = NULL; 40753018216SPaolo Bonzini int size, rc; 40870976c41SPeter Maydell uint32_t acells, scells; 409*9695200aSShannon Zhao char *nodename; 410*9695200aSShannon Zhao unsigned int i; 411*9695200aSShannon Zhao hwaddr mem_base, mem_len; 41253018216SPaolo Bonzini 4130fb79851SJohn Rigby if (binfo->dtb_filename) { 4140fb79851SJohn Rigby char *filename; 41553018216SPaolo Bonzini filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename); 41653018216SPaolo Bonzini if (!filename) { 41753018216SPaolo Bonzini fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename); 418c23045deSPeter Maydell goto fail; 41953018216SPaolo Bonzini } 42053018216SPaolo Bonzini 42153018216SPaolo Bonzini fdt = load_device_tree(filename, &size); 42253018216SPaolo Bonzini if (!fdt) { 42353018216SPaolo Bonzini fprintf(stderr, "Couldn't open dtb file %s\n", filename); 42453018216SPaolo Bonzini g_free(filename); 425c23045deSPeter Maydell goto fail; 42653018216SPaolo Bonzini } 42753018216SPaolo Bonzini g_free(filename); 428a554ecb4Szhanghailiang } else { 4290fb79851SJohn Rigby fdt = binfo->get_dtb(binfo, &size); 4300fb79851SJohn Rigby if (!fdt) { 4310fb79851SJohn Rigby fprintf(stderr, "Board was unable to create a dtb blob\n"); 4320fb79851SJohn Rigby goto fail; 4330fb79851SJohn Rigby } 4340fb79851SJohn Rigby } 43553018216SPaolo Bonzini 436fee8ea12SArd Biesheuvel if (addr_limit > addr && size > (addr_limit - addr)) { 437fee8ea12SArd Biesheuvel /* Installing the device tree blob at addr would exceed addr_limit. 438fee8ea12SArd Biesheuvel * Whether this constitutes failure is up to the caller to decide, 439fee8ea12SArd Biesheuvel * so just return 0 as size, i.e., no error. 440fee8ea12SArd Biesheuvel */ 441fee8ea12SArd Biesheuvel g_free(fdt); 442fee8ea12SArd Biesheuvel return 0; 443fee8ea12SArd Biesheuvel } 444fee8ea12SArd Biesheuvel 44558e71097SEric Auger acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells", 44658e71097SEric Auger NULL, &error_fatal); 44758e71097SEric Auger scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells", 44858e71097SEric Auger NULL, &error_fatal); 44953018216SPaolo Bonzini if (acells == 0 || scells == 0) { 45053018216SPaolo Bonzini fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n"); 451c23045deSPeter Maydell goto fail; 45253018216SPaolo Bonzini } 45353018216SPaolo Bonzini 45470976c41SPeter Maydell if (scells < 2 && binfo->ram_size >= (1ULL << 32)) { 45570976c41SPeter Maydell /* This is user error so deserves a friendlier error message 45670976c41SPeter Maydell * than the failure of setprop_sized_cells would provide 45770976c41SPeter Maydell */ 45853018216SPaolo Bonzini fprintf(stderr, "qemu: dtb file not compatible with " 45953018216SPaolo Bonzini "RAM size > 4GB\n"); 460c23045deSPeter Maydell goto fail; 46153018216SPaolo Bonzini } 46253018216SPaolo Bonzini 463*9695200aSShannon Zhao if (nb_numa_nodes > 0) { 464*9695200aSShannon Zhao /* 465*9695200aSShannon Zhao * Turn the /memory node created before into a NOP node, then create 466*9695200aSShannon Zhao * /memory@addr nodes for all numa nodes respectively. 467*9695200aSShannon Zhao */ 468*9695200aSShannon Zhao qemu_fdt_nop_node(fdt, "/memory"); 469*9695200aSShannon Zhao mem_base = binfo->loader_start; 470*9695200aSShannon Zhao for (i = 0; i < nb_numa_nodes; i++) { 471*9695200aSShannon Zhao mem_len = numa_info[i].node_mem; 472*9695200aSShannon Zhao nodename = g_strdup_printf("/memory@%" PRIx64, mem_base); 473*9695200aSShannon Zhao qemu_fdt_add_subnode(fdt, nodename); 474*9695200aSShannon Zhao qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); 475*9695200aSShannon Zhao rc = qemu_fdt_setprop_sized_cells(fdt, nodename, "reg", 476*9695200aSShannon Zhao acells, mem_base, 477*9695200aSShannon Zhao scells, mem_len); 478*9695200aSShannon Zhao if (rc < 0) { 479*9695200aSShannon Zhao fprintf(stderr, "couldn't set %s/reg for node %d\n", nodename, 480*9695200aSShannon Zhao i); 481*9695200aSShannon Zhao goto fail; 482*9695200aSShannon Zhao } 483*9695200aSShannon Zhao 484*9695200aSShannon Zhao qemu_fdt_setprop_cell(fdt, nodename, "numa-node-id", i); 485*9695200aSShannon Zhao mem_base += mem_len; 486*9695200aSShannon Zhao g_free(nodename); 487*9695200aSShannon Zhao } 488*9695200aSShannon Zhao } else { 4895a4348d1SPeter Crosthwaite rc = qemu_fdt_setprop_sized_cells(fdt, "/memory", "reg", 49070976c41SPeter Maydell acells, binfo->loader_start, 49170976c41SPeter Maydell scells, binfo->ram_size); 49253018216SPaolo Bonzini if (rc < 0) { 49353018216SPaolo Bonzini fprintf(stderr, "couldn't set /memory/reg\n"); 494c23045deSPeter Maydell goto fail; 49553018216SPaolo Bonzini } 496*9695200aSShannon Zhao } 49753018216SPaolo Bonzini 49853018216SPaolo Bonzini if (binfo->kernel_cmdline && *binfo->kernel_cmdline) { 4995a4348d1SPeter Crosthwaite rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", 50053018216SPaolo Bonzini binfo->kernel_cmdline); 50153018216SPaolo Bonzini if (rc < 0) { 50253018216SPaolo Bonzini fprintf(stderr, "couldn't set /chosen/bootargs\n"); 503c23045deSPeter Maydell goto fail; 50453018216SPaolo Bonzini } 50553018216SPaolo Bonzini } 50653018216SPaolo Bonzini 50753018216SPaolo Bonzini if (binfo->initrd_size) { 5085a4348d1SPeter Crosthwaite rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start", 50953018216SPaolo Bonzini binfo->initrd_start); 51053018216SPaolo Bonzini if (rc < 0) { 51153018216SPaolo Bonzini fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n"); 512c23045deSPeter Maydell goto fail; 51353018216SPaolo Bonzini } 51453018216SPaolo Bonzini 5155a4348d1SPeter Crosthwaite rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end", 51653018216SPaolo Bonzini binfo->initrd_start + binfo->initrd_size); 51753018216SPaolo Bonzini if (rc < 0) { 51853018216SPaolo Bonzini fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n"); 519c23045deSPeter Maydell goto fail; 52053018216SPaolo Bonzini } 52153018216SPaolo Bonzini } 5223b1cceb8SPeter Maydell 5233b1cceb8SPeter Maydell if (binfo->modify_dtb) { 5243b1cceb8SPeter Maydell binfo->modify_dtb(binfo, fdt); 5253b1cceb8SPeter Maydell } 5263b1cceb8SPeter Maydell 5275a4348d1SPeter Crosthwaite qemu_fdt_dumpdtb(fdt, size); 52853018216SPaolo Bonzini 5294c4bf654SArd Biesheuvel /* Put the DTB into the memory map as a ROM image: this will ensure 5304c4bf654SArd Biesheuvel * the DTB is copied again upon reset, even if addr points into RAM. 5314c4bf654SArd Biesheuvel */ 5324c4bf654SArd Biesheuvel rom_add_blob_fixed("dtb", fdt, size, addr); 53353018216SPaolo Bonzini 534c23045deSPeter Maydell g_free(fdt); 535c23045deSPeter Maydell 536fee8ea12SArd Biesheuvel return size; 537c23045deSPeter Maydell 538c23045deSPeter Maydell fail: 539c23045deSPeter Maydell g_free(fdt); 540c23045deSPeter Maydell return -1; 54153018216SPaolo Bonzini } 54253018216SPaolo Bonzini 54353018216SPaolo Bonzini static void do_cpu_reset(void *opaque) 54453018216SPaolo Bonzini { 54553018216SPaolo Bonzini ARMCPU *cpu = opaque; 5464df81c6eSPeter Crosthwaite CPUState *cs = CPU(cpu); 54753018216SPaolo Bonzini CPUARMState *env = &cpu->env; 54853018216SPaolo Bonzini const struct arm_boot_info *info = env->boot_info; 54953018216SPaolo Bonzini 5504df81c6eSPeter Crosthwaite cpu_reset(cs); 55153018216SPaolo Bonzini if (info) { 55253018216SPaolo Bonzini if (!info->is_linux) { 5539776f636SPeter Crosthwaite int i; 55453018216SPaolo Bonzini /* Jump to the entry point. */ 5554df81c6eSPeter Crosthwaite uint64_t entry = info->entry; 5564df81c6eSPeter Crosthwaite 5579776f636SPeter Crosthwaite switch (info->endianness) { 5589776f636SPeter Crosthwaite case ARM_ENDIANNESS_LE: 5599776f636SPeter Crosthwaite env->cp15.sctlr_el[1] &= ~SCTLR_E0E; 5609776f636SPeter Crosthwaite for (i = 1; i < 4; ++i) { 5619776f636SPeter Crosthwaite env->cp15.sctlr_el[i] &= ~SCTLR_EE; 5629776f636SPeter Crosthwaite } 5639776f636SPeter Crosthwaite env->uncached_cpsr &= ~CPSR_E; 5649776f636SPeter Crosthwaite break; 5659776f636SPeter Crosthwaite case ARM_ENDIANNESS_BE8: 5669776f636SPeter Crosthwaite env->cp15.sctlr_el[1] |= SCTLR_E0E; 5679776f636SPeter Crosthwaite for (i = 1; i < 4; ++i) { 5689776f636SPeter Crosthwaite env->cp15.sctlr_el[i] |= SCTLR_EE; 5699776f636SPeter Crosthwaite } 5709776f636SPeter Crosthwaite env->uncached_cpsr |= CPSR_E; 5719776f636SPeter Crosthwaite break; 5729776f636SPeter Crosthwaite case ARM_ENDIANNESS_BE32: 5739776f636SPeter Crosthwaite env->cp15.sctlr_el[1] |= SCTLR_B; 5749776f636SPeter Crosthwaite break; 5759776f636SPeter Crosthwaite case ARM_ENDIANNESS_UNKNOWN: 5769776f636SPeter Crosthwaite break; /* Board's decision */ 5779776f636SPeter Crosthwaite default: 5789776f636SPeter Crosthwaite g_assert_not_reached(); 5799776f636SPeter Crosthwaite } 5809776f636SPeter Crosthwaite 5814df81c6eSPeter Crosthwaite if (!env->aarch64) { 58253018216SPaolo Bonzini env->thumb = info->entry & 1; 5834df81c6eSPeter Crosthwaite entry &= 0xfffffffe; 584a9047ec3SPeter Maydell } 5854df81c6eSPeter Crosthwaite cpu_set_pc(cs, entry); 58653018216SPaolo Bonzini } else { 587c8e829b7SGreg Bellows /* If we are booting Linux then we need to check whether we are 588c8e829b7SGreg Bellows * booting into secure or non-secure state and adjust the state 589c8e829b7SGreg Bellows * accordingly. Out of reset, ARM is defined to be in secure state 590c8e829b7SGreg Bellows * (SCR.NS = 0), we change that here if non-secure boot has been 591c8e829b7SGreg Bellows * requested. 592c8e829b7SGreg Bellows */ 5935097227cSGreg Bellows if (arm_feature(env, ARM_FEATURE_EL3)) { 5945097227cSGreg Bellows /* AArch64 is defined to come out of reset into EL3 if enabled. 5955097227cSGreg Bellows * If we are booting Linux then we need to adjust our EL as 5965097227cSGreg Bellows * Linux expects us to be in EL2 or EL1. AArch32 resets into 5975097227cSGreg Bellows * SVC, which Linux expects, so no privilege/exception level to 5985097227cSGreg Bellows * adjust. 5995097227cSGreg Bellows */ 6005097227cSGreg Bellows if (env->aarch64) { 60148d21a57SEdgar E. Iglesias env->cp15.scr_el3 |= SCR_RW; 6025097227cSGreg Bellows if (arm_feature(env, ARM_FEATURE_EL2)) { 60348d21a57SEdgar E. Iglesias env->cp15.hcr_el2 |= HCR_RW; 6045097227cSGreg Bellows env->pstate = PSTATE_MODE_EL2h; 6055097227cSGreg Bellows } else { 6065097227cSGreg Bellows env->pstate = PSTATE_MODE_EL1h; 6075097227cSGreg Bellows } 6085097227cSGreg Bellows } 6095097227cSGreg Bellows 6105097227cSGreg Bellows /* Set to non-secure if not a secure boot */ 611baf6b681SPeter Crosthwaite if (!info->secure_boot && 612baf6b681SPeter Crosthwaite (cs != first_cpu || !info->secure_board_setup)) { 6135097227cSGreg Bellows /* Linux expects non-secure state */ 614c8e829b7SGreg Bellows env->cp15.scr_el3 |= SCR_NS; 615c8e829b7SGreg Bellows } 6165097227cSGreg Bellows } 617c8e829b7SGreg Bellows 6184df81c6eSPeter Crosthwaite if (cs == first_cpu) { 6194df81c6eSPeter Crosthwaite cpu_set_pc(cs, info->loader_start); 6204d9ebf75SMian M. Hamayun 62183bfffecSPeter Maydell if (!have_dtb(info)) { 62253018216SPaolo Bonzini if (old_param) { 62353018216SPaolo Bonzini set_kernel_args_old(info); 62453018216SPaolo Bonzini } else { 62553018216SPaolo Bonzini set_kernel_args(info); 62653018216SPaolo Bonzini } 62753018216SPaolo Bonzini } 62853018216SPaolo Bonzini } else { 62953018216SPaolo Bonzini info->secondary_cpu_reset_hook(cpu, info); 63053018216SPaolo Bonzini } 63153018216SPaolo Bonzini } 63253018216SPaolo Bonzini } 63353018216SPaolo Bonzini } 63453018216SPaolo Bonzini 63507abe45cSLaszlo Ersek /** 63607abe45cSLaszlo Ersek * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified 63707abe45cSLaszlo Ersek * by key. 63807abe45cSLaszlo Ersek * @fw_cfg: The firmware config instance to store the data in. 63907abe45cSLaszlo Ersek * @size_key: The firmware config key to store the size of the loaded 64007abe45cSLaszlo Ersek * data under, with fw_cfg_add_i32(). 64107abe45cSLaszlo Ersek * @data_key: The firmware config key to store the loaded data under, 64207abe45cSLaszlo Ersek * with fw_cfg_add_bytes(). 64307abe45cSLaszlo Ersek * @image_name: The name of the image file to load. If it is NULL, the 64407abe45cSLaszlo Ersek * function returns without doing anything. 64507abe45cSLaszlo Ersek * @try_decompress: Whether the image should be decompressed (gunzipped) before 64607abe45cSLaszlo Ersek * adding it to fw_cfg. If decompression fails, the image is 64707abe45cSLaszlo Ersek * loaded as-is. 64807abe45cSLaszlo Ersek * 64907abe45cSLaszlo Ersek * In case of failure, the function prints an error message to stderr and the 65007abe45cSLaszlo Ersek * process exits with status 1. 65107abe45cSLaszlo Ersek */ 65207abe45cSLaszlo Ersek static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key, 65307abe45cSLaszlo Ersek uint16_t data_key, const char *image_name, 65407abe45cSLaszlo Ersek bool try_decompress) 65507abe45cSLaszlo Ersek { 65607abe45cSLaszlo Ersek size_t size = -1; 65707abe45cSLaszlo Ersek uint8_t *data; 65807abe45cSLaszlo Ersek 65907abe45cSLaszlo Ersek if (image_name == NULL) { 66007abe45cSLaszlo Ersek return; 66107abe45cSLaszlo Ersek } 66207abe45cSLaszlo Ersek 66307abe45cSLaszlo Ersek if (try_decompress) { 66407abe45cSLaszlo Ersek size = load_image_gzipped_buffer(image_name, 66507abe45cSLaszlo Ersek LOAD_IMAGE_MAX_GUNZIP_BYTES, &data); 66607abe45cSLaszlo Ersek } 66707abe45cSLaszlo Ersek 66807abe45cSLaszlo Ersek if (size == (size_t)-1) { 66907abe45cSLaszlo Ersek gchar *contents; 67007abe45cSLaszlo Ersek gsize length; 67107abe45cSLaszlo Ersek 67207abe45cSLaszlo Ersek if (!g_file_get_contents(image_name, &contents, &length, NULL)) { 67307abe45cSLaszlo Ersek fprintf(stderr, "failed to load \"%s\"\n", image_name); 67407abe45cSLaszlo Ersek exit(1); 67507abe45cSLaszlo Ersek } 67607abe45cSLaszlo Ersek size = length; 67707abe45cSLaszlo Ersek data = (uint8_t *)contents; 67807abe45cSLaszlo Ersek } 67907abe45cSLaszlo Ersek 68007abe45cSLaszlo Ersek fw_cfg_add_i32(fw_cfg, size_key, size); 68107abe45cSLaszlo Ersek fw_cfg_add_bytes(fw_cfg, data_key, data, size); 68207abe45cSLaszlo Ersek } 68307abe45cSLaszlo Ersek 684d8b1ae42SPeter Maydell static int do_arm_linux_init(Object *obj, void *opaque) 685d8b1ae42SPeter Maydell { 686d8b1ae42SPeter Maydell if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) { 687d8b1ae42SPeter Maydell ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj); 688d8b1ae42SPeter Maydell ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj); 689d8b1ae42SPeter Maydell struct arm_boot_info *info = opaque; 690d8b1ae42SPeter Maydell 691d8b1ae42SPeter Maydell if (albifc->arm_linux_init) { 692d8b1ae42SPeter Maydell albifc->arm_linux_init(albif, info->secure_boot); 693d8b1ae42SPeter Maydell } 694d8b1ae42SPeter Maydell } 695d8b1ae42SPeter Maydell return 0; 696d8b1ae42SPeter Maydell } 697d8b1ae42SPeter Maydell 6989776f636SPeter Crosthwaite static uint64_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry, 6999776f636SPeter Crosthwaite uint64_t *lowaddr, uint64_t *highaddr, 7009776f636SPeter Crosthwaite int elf_machine) 7019776f636SPeter Crosthwaite { 7029776f636SPeter Crosthwaite bool elf_is64; 7039776f636SPeter Crosthwaite union { 7049776f636SPeter Crosthwaite Elf32_Ehdr h32; 7059776f636SPeter Crosthwaite Elf64_Ehdr h64; 7069776f636SPeter Crosthwaite } elf_header; 7079776f636SPeter Crosthwaite int data_swab = 0; 7089776f636SPeter Crosthwaite bool big_endian; 7099776f636SPeter Crosthwaite uint64_t ret = -1; 7109776f636SPeter Crosthwaite Error *err = NULL; 7119776f636SPeter Crosthwaite 7129776f636SPeter Crosthwaite 7139776f636SPeter Crosthwaite load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err); 7149776f636SPeter Crosthwaite if (err) { 7159776f636SPeter Crosthwaite return ret; 7169776f636SPeter Crosthwaite } 7179776f636SPeter Crosthwaite 7189776f636SPeter Crosthwaite if (elf_is64) { 7199776f636SPeter Crosthwaite big_endian = elf_header.h64.e_ident[EI_DATA] == ELFDATA2MSB; 7209776f636SPeter Crosthwaite info->endianness = big_endian ? ARM_ENDIANNESS_BE8 7219776f636SPeter Crosthwaite : ARM_ENDIANNESS_LE; 7229776f636SPeter Crosthwaite } else { 7239776f636SPeter Crosthwaite big_endian = elf_header.h32.e_ident[EI_DATA] == ELFDATA2MSB; 7249776f636SPeter Crosthwaite if (big_endian) { 7259776f636SPeter Crosthwaite if (bswap32(elf_header.h32.e_flags) & EF_ARM_BE8) { 7269776f636SPeter Crosthwaite info->endianness = ARM_ENDIANNESS_BE8; 7279776f636SPeter Crosthwaite } else { 7289776f636SPeter Crosthwaite info->endianness = ARM_ENDIANNESS_BE32; 7299776f636SPeter Crosthwaite /* In BE32, the CPU has a different view of the per-byte 7309776f636SPeter Crosthwaite * address map than the rest of the system. BE32 ELF files 7319776f636SPeter Crosthwaite * are organised such that they can be programmed through 7329776f636SPeter Crosthwaite * the CPU's per-word byte-reversed view of the world. QEMU 7339776f636SPeter Crosthwaite * however loads ELF files independently of the CPU. So 7349776f636SPeter Crosthwaite * tell the ELF loader to byte reverse the data for us. 7359776f636SPeter Crosthwaite */ 7369776f636SPeter Crosthwaite data_swab = 2; 7379776f636SPeter Crosthwaite } 7389776f636SPeter Crosthwaite } else { 7399776f636SPeter Crosthwaite info->endianness = ARM_ENDIANNESS_LE; 7409776f636SPeter Crosthwaite } 7419776f636SPeter Crosthwaite } 7429776f636SPeter Crosthwaite 7439776f636SPeter Crosthwaite ret = load_elf(info->kernel_filename, NULL, NULL, 7449776f636SPeter Crosthwaite pentry, lowaddr, highaddr, big_endian, elf_machine, 7459776f636SPeter Crosthwaite 1, data_swab); 7469776f636SPeter Crosthwaite if (ret <= 0) { 7479776f636SPeter Crosthwaite /* The header loaded but the image didn't */ 7489776f636SPeter Crosthwaite exit(1); 7499776f636SPeter Crosthwaite } 7509776f636SPeter Crosthwaite 7519776f636SPeter Crosthwaite return ret; 7529776f636SPeter Crosthwaite } 7539776f636SPeter Crosthwaite 754ac9d32e3SEric Auger static void arm_load_kernel_notify(Notifier *notifier, void *data) 75553018216SPaolo Bonzini { 756c6faa758SArd Biesheuvel CPUState *cs; 75753018216SPaolo Bonzini int kernel_size; 75853018216SPaolo Bonzini int initrd_size; 75953018216SPaolo Bonzini int is_linux = 0; 76092df8450SArd Biesheuvel uint64_t elf_entry, elf_low_addr, elf_high_addr; 761da0af40dSPeter Maydell int elf_machine; 7624d9ebf75SMian M. Hamayun hwaddr entry, kernel_load_offset; 7634d9ebf75SMian M. Hamayun static const ARMInsnFixup *primary_loader; 764ac9d32e3SEric Auger ArmLoadKernelNotifier *n = DO_UPCAST(ArmLoadKernelNotifier, 765ac9d32e3SEric Auger notifier, notifier); 766ac9d32e3SEric Auger ARMCPU *cpu = n->cpu; 767ac9d32e3SEric Auger struct arm_boot_info *info = 768ac9d32e3SEric Auger container_of(n, struct arm_boot_info, load_kernel_notifier); 76953018216SPaolo Bonzini 770baf6b681SPeter Crosthwaite /* The board code is not supposed to set secure_board_setup unless 771baf6b681SPeter Crosthwaite * running its code in secure mode is actually possible, and KVM 772baf6b681SPeter Crosthwaite * doesn't support secure. 773baf6b681SPeter Crosthwaite */ 774baf6b681SPeter Crosthwaite assert(!(info->secure_board_setup && kvm_enabled())); 775baf6b681SPeter Crosthwaite 77653018216SPaolo Bonzini /* Load the kernel. */ 77707abe45cSLaszlo Ersek if (!info->kernel_filename || info->firmware_loaded) { 77869e7f76fSArd Biesheuvel 77969e7f76fSArd Biesheuvel if (have_dtb(info)) { 78007abe45cSLaszlo Ersek /* If we have a device tree blob, but no kernel to supply it to (or 78107abe45cSLaszlo Ersek * the kernel is supposed to be loaded by the bootloader), copy the 78207abe45cSLaszlo Ersek * DTB to the base of RAM for the bootloader to pick up. 78369e7f76fSArd Biesheuvel */ 78469e7f76fSArd Biesheuvel if (load_dtb(info->loader_start, info, 0) < 0) { 78569e7f76fSArd Biesheuvel exit(1); 78669e7f76fSArd Biesheuvel } 78769e7f76fSArd Biesheuvel } 78869e7f76fSArd Biesheuvel 78907abe45cSLaszlo Ersek if (info->kernel_filename) { 79007abe45cSLaszlo Ersek FWCfgState *fw_cfg; 79107abe45cSLaszlo Ersek bool try_decompressing_kernel; 79207abe45cSLaszlo Ersek 79307abe45cSLaszlo Ersek fw_cfg = fw_cfg_find(); 79407abe45cSLaszlo Ersek try_decompressing_kernel = arm_feature(&cpu->env, 79507abe45cSLaszlo Ersek ARM_FEATURE_AARCH64); 79607abe45cSLaszlo Ersek 79707abe45cSLaszlo Ersek /* Expose the kernel, the command line, and the initrd in fw_cfg. 79807abe45cSLaszlo Ersek * We don't process them here at all, it's all left to the 79907abe45cSLaszlo Ersek * firmware. 80007abe45cSLaszlo Ersek */ 80107abe45cSLaszlo Ersek load_image_to_fw_cfg(fw_cfg, 80207abe45cSLaszlo Ersek FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA, 80307abe45cSLaszlo Ersek info->kernel_filename, 80407abe45cSLaszlo Ersek try_decompressing_kernel); 80507abe45cSLaszlo Ersek load_image_to_fw_cfg(fw_cfg, 80607abe45cSLaszlo Ersek FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA, 80707abe45cSLaszlo Ersek info->initrd_filename, false); 80807abe45cSLaszlo Ersek 80907abe45cSLaszlo Ersek if (info->kernel_cmdline) { 81007abe45cSLaszlo Ersek fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 81107abe45cSLaszlo Ersek strlen(info->kernel_cmdline) + 1); 81207abe45cSLaszlo Ersek fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, 81307abe45cSLaszlo Ersek info->kernel_cmdline); 81407abe45cSLaszlo Ersek } 81507abe45cSLaszlo Ersek } 81607abe45cSLaszlo Ersek 81707abe45cSLaszlo Ersek /* We will start from address 0 (typically a boot ROM image) in the 81807abe45cSLaszlo Ersek * same way as hardware. 8199546dbabSPeter Maydell */ 8209546dbabSPeter Maydell return; 82153018216SPaolo Bonzini } 82253018216SPaolo Bonzini 8234d9ebf75SMian M. Hamayun if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 8244d9ebf75SMian M. Hamayun primary_loader = bootloader_aarch64; 8254d9ebf75SMian M. Hamayun kernel_load_offset = KERNEL64_LOAD_ADDR; 826da0af40dSPeter Maydell elf_machine = EM_AARCH64; 8274d9ebf75SMian M. Hamayun } else { 8284d9ebf75SMian M. Hamayun primary_loader = bootloader; 82910b8ec73SPeter Crosthwaite if (!info->write_board_setup) { 83010b8ec73SPeter Crosthwaite primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET; 83110b8ec73SPeter Crosthwaite } 8324d9ebf75SMian M. Hamayun kernel_load_offset = KERNEL_LOAD_ADDR; 833da0af40dSPeter Maydell elf_machine = EM_ARM; 8344d9ebf75SMian M. Hamayun } 8354d9ebf75SMian M. Hamayun 8362ff3de68SMarkus Armbruster info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb"); 83753018216SPaolo Bonzini 83853018216SPaolo Bonzini if (!info->secondary_cpu_reset_hook) { 83953018216SPaolo Bonzini info->secondary_cpu_reset_hook = default_reset_secondary; 84053018216SPaolo Bonzini } 84153018216SPaolo Bonzini if (!info->write_secondary_boot) { 84253018216SPaolo Bonzini info->write_secondary_boot = default_write_secondary; 84353018216SPaolo Bonzini } 84453018216SPaolo Bonzini 84553018216SPaolo Bonzini if (info->nb_cpus == 0) 84653018216SPaolo Bonzini info->nb_cpus = 1; 84753018216SPaolo Bonzini 84853018216SPaolo Bonzini /* We want to put the initrd far enough into RAM that when the 84953018216SPaolo Bonzini * kernel is uncompressed it will not clobber the initrd. However 85053018216SPaolo Bonzini * on boards without much RAM we must ensure that we still leave 85153018216SPaolo Bonzini * enough room for a decent sized initrd, and on boards with large 85253018216SPaolo Bonzini * amounts of RAM we must avoid the initrd being so far up in RAM 85353018216SPaolo Bonzini * that it is outside lowmem and inaccessible to the kernel. 85453018216SPaolo Bonzini * So for boards with less than 256MB of RAM we put the initrd 85553018216SPaolo Bonzini * halfway into RAM, and for boards with 256MB of RAM or more we put 85653018216SPaolo Bonzini * the initrd at 128MB. 85753018216SPaolo Bonzini */ 85853018216SPaolo Bonzini info->initrd_start = info->loader_start + 85953018216SPaolo Bonzini MIN(info->ram_size / 2, 128 * 1024 * 1024); 86053018216SPaolo Bonzini 86153018216SPaolo Bonzini /* Assume that raw images are linux kernels, and ELF images are not. */ 8629776f636SPeter Crosthwaite kernel_size = arm_load_elf(info, &elf_entry, &elf_low_addr, 8639776f636SPeter Crosthwaite &elf_high_addr, elf_machine); 86492df8450SArd Biesheuvel if (kernel_size > 0 && have_dtb(info)) { 86592df8450SArd Biesheuvel /* If there is still some room left at the base of RAM, try and put 86692df8450SArd Biesheuvel * the DTB there like we do for images loaded with -bios or -pflash. 86792df8450SArd Biesheuvel */ 86892df8450SArd Biesheuvel if (elf_low_addr > info->loader_start 86992df8450SArd Biesheuvel || elf_high_addr < info->loader_start) { 87092df8450SArd Biesheuvel /* Pass elf_low_addr as address limit to load_dtb if it may be 87192df8450SArd Biesheuvel * pointing into RAM, otherwise pass '0' (no limit) 87292df8450SArd Biesheuvel */ 87392df8450SArd Biesheuvel if (elf_low_addr < info->loader_start) { 87492df8450SArd Biesheuvel elf_low_addr = 0; 87592df8450SArd Biesheuvel } 87692df8450SArd Biesheuvel if (load_dtb(info->loader_start, info, elf_low_addr) < 0) { 87792df8450SArd Biesheuvel exit(1); 87892df8450SArd Biesheuvel } 87992df8450SArd Biesheuvel } 88092df8450SArd Biesheuvel } 88153018216SPaolo Bonzini entry = elf_entry; 88253018216SPaolo Bonzini if (kernel_size < 0) { 88353018216SPaolo Bonzini kernel_size = load_uimage(info->kernel_filename, &entry, NULL, 88425bda50aSMax Filippov &is_linux, NULL, NULL); 88553018216SPaolo Bonzini } 8866f5d3cbeSRichard W.M. Jones /* On aarch64, it's the bootloader's job to uncompress the kernel. */ 8876f5d3cbeSRichard W.M. Jones if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) { 8886f5d3cbeSRichard W.M. Jones entry = info->loader_start + kernel_load_offset; 8896f5d3cbeSRichard W.M. Jones kernel_size = load_image_gzipped(info->kernel_filename, entry, 8906f5d3cbeSRichard W.M. Jones info->ram_size - kernel_load_offset); 8916f5d3cbeSRichard W.M. Jones is_linux = 1; 8926f5d3cbeSRichard W.M. Jones } 89353018216SPaolo Bonzini if (kernel_size < 0) { 8944d9ebf75SMian M. Hamayun entry = info->loader_start + kernel_load_offset; 89553018216SPaolo Bonzini kernel_size = load_image_targphys(info->kernel_filename, entry, 8964d9ebf75SMian M. Hamayun info->ram_size - kernel_load_offset); 89753018216SPaolo Bonzini is_linux = 1; 89853018216SPaolo Bonzini } 89953018216SPaolo Bonzini if (kernel_size < 0) { 90053018216SPaolo Bonzini fprintf(stderr, "qemu: could not load kernel '%s'\n", 90153018216SPaolo Bonzini info->kernel_filename); 90253018216SPaolo Bonzini exit(1); 90353018216SPaolo Bonzini } 90453018216SPaolo Bonzini info->entry = entry; 90553018216SPaolo Bonzini if (is_linux) { 90647b1da81SPeter Maydell uint32_t fixupcontext[FIXUP_MAX]; 90747b1da81SPeter Maydell 90853018216SPaolo Bonzini if (info->initrd_filename) { 909fd76663eSSoren Brinkmann initrd_size = load_ramdisk(info->initrd_filename, 910fd76663eSSoren Brinkmann info->initrd_start, 911fd76663eSSoren Brinkmann info->ram_size - 912fd76663eSSoren Brinkmann info->initrd_start); 913fd76663eSSoren Brinkmann if (initrd_size < 0) { 91453018216SPaolo Bonzini initrd_size = load_image_targphys(info->initrd_filename, 91553018216SPaolo Bonzini info->initrd_start, 91653018216SPaolo Bonzini info->ram_size - 91753018216SPaolo Bonzini info->initrd_start); 918fd76663eSSoren Brinkmann } 91953018216SPaolo Bonzini if (initrd_size < 0) { 92053018216SPaolo Bonzini fprintf(stderr, "qemu: could not load initrd '%s'\n", 92153018216SPaolo Bonzini info->initrd_filename); 92253018216SPaolo Bonzini exit(1); 92353018216SPaolo Bonzini } 92453018216SPaolo Bonzini } else { 92553018216SPaolo Bonzini initrd_size = 0; 92653018216SPaolo Bonzini } 92753018216SPaolo Bonzini info->initrd_size = initrd_size; 92853018216SPaolo Bonzini 92947b1da81SPeter Maydell fixupcontext[FIXUP_BOARDID] = info->board_id; 93010b8ec73SPeter Crosthwaite fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr; 93153018216SPaolo Bonzini 93253018216SPaolo Bonzini /* for device tree boot, we pass the DTB directly in r2. Otherwise 93353018216SPaolo Bonzini * we point to the kernel args. 93453018216SPaolo Bonzini */ 93583bfffecSPeter Maydell if (have_dtb(info)) { 93676e2aef3SAlexander Graf hwaddr align; 93776e2aef3SAlexander Graf hwaddr dtb_start; 93876e2aef3SAlexander Graf 93976e2aef3SAlexander Graf if (elf_machine == EM_AARCH64) { 94076e2aef3SAlexander Graf /* 94176e2aef3SAlexander Graf * Some AArch64 kernels on early bootup map the fdt region as 94276e2aef3SAlexander Graf * 94376e2aef3SAlexander Graf * [ ALIGN_DOWN(fdt, 2MB) ... ALIGN_DOWN(fdt, 2MB) + 2MB ] 94476e2aef3SAlexander Graf * 94576e2aef3SAlexander Graf * Let's play safe and prealign it to 2MB to give us some space. 94653018216SPaolo Bonzini */ 94776e2aef3SAlexander Graf align = 2 * 1024 * 1024; 94876e2aef3SAlexander Graf } else { 94976e2aef3SAlexander Graf /* 95076e2aef3SAlexander Graf * Some 32bit kernels will trash anything in the 4K page the 95176e2aef3SAlexander Graf * initrd ends in, so make sure the DTB isn't caught up in that. 95276e2aef3SAlexander Graf */ 95376e2aef3SAlexander Graf align = 4096; 95476e2aef3SAlexander Graf } 95576e2aef3SAlexander Graf 95676e2aef3SAlexander Graf /* Place the DTB after the initrd in memory with alignment. */ 95776e2aef3SAlexander Graf dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size, align); 958fee8ea12SArd Biesheuvel if (load_dtb(dtb_start, info, 0) < 0) { 95953018216SPaolo Bonzini exit(1); 96053018216SPaolo Bonzini } 96147b1da81SPeter Maydell fixupcontext[FIXUP_ARGPTR] = dtb_start; 96253018216SPaolo Bonzini } else { 96347b1da81SPeter Maydell fixupcontext[FIXUP_ARGPTR] = info->loader_start + KERNEL_ARGS_ADDR; 96453018216SPaolo Bonzini if (info->ram_size >= (1ULL << 32)) { 96553018216SPaolo Bonzini fprintf(stderr, "qemu: RAM size must be less than 4GB to boot" 96653018216SPaolo Bonzini " Linux kernel using ATAGS (try passing a device tree" 96753018216SPaolo Bonzini " using -dtb)\n"); 96853018216SPaolo Bonzini exit(1); 96953018216SPaolo Bonzini } 97053018216SPaolo Bonzini } 97147b1da81SPeter Maydell fixupcontext[FIXUP_ENTRYPOINT] = entry; 97247b1da81SPeter Maydell 97347b1da81SPeter Maydell write_bootloader("bootloader", info->loader_start, 9744d9ebf75SMian M. Hamayun primary_loader, fixupcontext); 97547b1da81SPeter Maydell 97653018216SPaolo Bonzini if (info->nb_cpus > 1) { 97753018216SPaolo Bonzini info->write_secondary_boot(cpu, info); 97853018216SPaolo Bonzini } 97910b8ec73SPeter Crosthwaite if (info->write_board_setup) { 98010b8ec73SPeter Crosthwaite info->write_board_setup(cpu, info); 98110b8ec73SPeter Crosthwaite } 982d8b1ae42SPeter Maydell 983d8b1ae42SPeter Maydell /* Notify devices which need to fake up firmware initialization 984d8b1ae42SPeter Maydell * that we're doing a direct kernel boot. 985d8b1ae42SPeter Maydell */ 986d8b1ae42SPeter Maydell object_child_foreach_recursive(object_get_root(), 987d8b1ae42SPeter Maydell do_arm_linux_init, info); 98853018216SPaolo Bonzini } 98953018216SPaolo Bonzini info->is_linux = is_linux; 99053018216SPaolo Bonzini 991c6faa758SArd Biesheuvel for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) { 992c6faa758SArd Biesheuvel ARM_CPU(cs)->env.boot_info = info; 99353018216SPaolo Bonzini } 99453018216SPaolo Bonzini } 995ac9d32e3SEric Auger 996ac9d32e3SEric Auger void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info) 997ac9d32e3SEric Auger { 99863a183edSEric Auger CPUState *cs; 99963a183edSEric Auger 1000ac9d32e3SEric Auger info->load_kernel_notifier.cpu = cpu; 1001ac9d32e3SEric Auger info->load_kernel_notifier.notifier.notify = arm_load_kernel_notify; 1002ac9d32e3SEric Auger qemu_add_machine_init_done_notifier(&info->load_kernel_notifier.notifier); 100363a183edSEric Auger 100463a183edSEric Auger /* CPU objects (unlike devices) are not automatically reset on system 100563a183edSEric Auger * reset, so we must always register a handler to do so. If we're 100663a183edSEric Auger * actually loading a kernel, the handler is also responsible for 100763a183edSEric Auger * arranging that we start it correctly. 100863a183edSEric Auger */ 100963a183edSEric Auger for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) { 101063a183edSEric Auger qemu_register_reset(do_cpu_reset, ARM_CPU(cs)); 101163a183edSEric Auger } 1012ac9d32e3SEric Auger } 1013d8b1ae42SPeter Maydell 1014d8b1ae42SPeter Maydell static const TypeInfo arm_linux_boot_if_info = { 1015d8b1ae42SPeter Maydell .name = TYPE_ARM_LINUX_BOOT_IF, 1016d8b1ae42SPeter Maydell .parent = TYPE_INTERFACE, 1017d8b1ae42SPeter Maydell .class_size = sizeof(ARMLinuxBootIfClass), 1018d8b1ae42SPeter Maydell }; 1019d8b1ae42SPeter Maydell 1020d8b1ae42SPeter Maydell static void arm_linux_boot_register_types(void) 1021d8b1ae42SPeter Maydell { 1022d8b1ae42SPeter Maydell type_register_static(&arm_linux_boot_if_info); 1023d8b1ae42SPeter Maydell } 1024d8b1ae42SPeter Maydell 1025d8b1ae42SPeter Maydell type_init(arm_linux_boot_register_types) 1026