xref: /openbmc/qemu/hw/arm/boot.c (revision 63a183ed0eac2956574745c84faffa042d99afb8)
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 
1053018216SPaolo Bonzini #include "config.h"
1153018216SPaolo Bonzini #include "hw/hw.h"
12bd2be150SPeter Maydell #include "hw/arm/arm.h"
1353018216SPaolo Bonzini #include "sysemu/sysemu.h"
1453018216SPaolo Bonzini #include "hw/boards.h"
1553018216SPaolo Bonzini #include "hw/loader.h"
1653018216SPaolo Bonzini #include "elf.h"
1753018216SPaolo Bonzini #include "sysemu/device_tree.h"
1853018216SPaolo Bonzini #include "qemu/config-file.h"
192198a121SEdgar E. Iglesias #include "exec/address-spaces.h"
2053018216SPaolo Bonzini 
214d9ebf75SMian M. Hamayun /* Kernel boot protocol is specified in the kernel docs
224d9ebf75SMian M. Hamayun  * Documentation/arm/Booting and Documentation/arm64/booting.txt
234d9ebf75SMian M. Hamayun  * They have different preferred image load offsets from system RAM base.
244d9ebf75SMian M. Hamayun  */
2553018216SPaolo Bonzini #define KERNEL_ARGS_ADDR 0x100
2653018216SPaolo Bonzini #define KERNEL_LOAD_ADDR 0x00010000
274d9ebf75SMian M. Hamayun #define KERNEL64_LOAD_ADDR 0x00080000
2853018216SPaolo Bonzini 
2947b1da81SPeter Maydell typedef enum {
3047b1da81SPeter Maydell     FIXUP_NONE = 0,   /* do nothing */
3147b1da81SPeter Maydell     FIXUP_TERMINATOR, /* end of insns */
3247b1da81SPeter Maydell     FIXUP_BOARDID,    /* overwrite with board ID number */
3347b1da81SPeter Maydell     FIXUP_ARGPTR,     /* overwrite with pointer to kernel args */
3447b1da81SPeter Maydell     FIXUP_ENTRYPOINT, /* overwrite with kernel entry point */
3547b1da81SPeter Maydell     FIXUP_GIC_CPU_IF, /* overwrite with GIC CPU interface address */
3647b1da81SPeter Maydell     FIXUP_BOOTREG,    /* overwrite with boot register address */
3747b1da81SPeter Maydell     FIXUP_DSB,        /* overwrite with correct DSB insn for cpu */
3847b1da81SPeter Maydell     FIXUP_MAX,
3947b1da81SPeter Maydell } FixupType;
4047b1da81SPeter Maydell 
4147b1da81SPeter Maydell typedef struct ARMInsnFixup {
4247b1da81SPeter Maydell     uint32_t insn;
4347b1da81SPeter Maydell     FixupType fixup;
4447b1da81SPeter Maydell } ARMInsnFixup;
4547b1da81SPeter Maydell 
464d9ebf75SMian M. Hamayun static const ARMInsnFixup bootloader_aarch64[] = {
474d9ebf75SMian M. Hamayun     { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
484d9ebf75SMian M. Hamayun     { 0xaa1f03e1 }, /* mov x1, xzr */
494d9ebf75SMian M. Hamayun     { 0xaa1f03e2 }, /* mov x2, xzr */
504d9ebf75SMian M. Hamayun     { 0xaa1f03e3 }, /* mov x3, xzr */
514d9ebf75SMian M. Hamayun     { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
524d9ebf75SMian M. Hamayun     { 0xd61f0080 }, /* br x4      ; Jump to the kernel entry point */
534d9ebf75SMian M. Hamayun     { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */
544d9ebf75SMian M. Hamayun     { 0 }, /* .word @DTB Higher 32-bits */
554d9ebf75SMian M. Hamayun     { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */
564d9ebf75SMian M. Hamayun     { 0 }, /* .word @Kernel Entry Higher 32-bits */
574d9ebf75SMian M. Hamayun     { 0, FIXUP_TERMINATOR }
584d9ebf75SMian M. Hamayun };
594d9ebf75SMian M. Hamayun 
6053018216SPaolo Bonzini /* The worlds second smallest bootloader.  Set r0-r2, then jump to kernel.  */
6147b1da81SPeter Maydell static const ARMInsnFixup bootloader[] = {
6247b1da81SPeter Maydell     { 0xe3a00000 }, /* mov     r0, #0 */
6347b1da81SPeter Maydell     { 0xe59f1004 }, /* ldr     r1, [pc, #4] */
6447b1da81SPeter Maydell     { 0xe59f2004 }, /* ldr     r2, [pc, #4] */
6547b1da81SPeter Maydell     { 0xe59ff004 }, /* ldr     pc, [pc, #4] */
6647b1da81SPeter Maydell     { 0, FIXUP_BOARDID },
6747b1da81SPeter Maydell     { 0, FIXUP_ARGPTR },
6847b1da81SPeter Maydell     { 0, FIXUP_ENTRYPOINT },
6947b1da81SPeter Maydell     { 0, FIXUP_TERMINATOR }
7053018216SPaolo Bonzini };
7153018216SPaolo Bonzini 
7253018216SPaolo Bonzini /* Handling for secondary CPU boot in a multicore system.
7353018216SPaolo Bonzini  * Unlike the uniprocessor/primary CPU boot, this is platform
7453018216SPaolo Bonzini  * dependent. The default code here is based on the secondary
7553018216SPaolo Bonzini  * CPU boot protocol used on realview/vexpress boards, with
7653018216SPaolo Bonzini  * some parameterisation to increase its flexibility.
7753018216SPaolo Bonzini  * QEMU platform models for which this code is not appropriate
7853018216SPaolo Bonzini  * should override write_secondary_boot and secondary_cpu_reset_hook
7953018216SPaolo Bonzini  * instead.
8053018216SPaolo Bonzini  *
8153018216SPaolo Bonzini  * This code enables the interrupt controllers for the secondary
8253018216SPaolo Bonzini  * CPUs and then puts all the secondary CPUs into a loop waiting
8353018216SPaolo Bonzini  * for an interprocessor interrupt and polling a configurable
8453018216SPaolo Bonzini  * location for the kernel secondary CPU entry point.
8553018216SPaolo Bonzini  */
8653018216SPaolo Bonzini #define DSB_INSN 0xf57ff04f
8753018216SPaolo Bonzini #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
8853018216SPaolo Bonzini 
8947b1da81SPeter Maydell static const ARMInsnFixup smpboot[] = {
9047b1da81SPeter Maydell     { 0xe59f2028 }, /* ldr r2, gic_cpu_if */
9147b1da81SPeter Maydell     { 0xe59f0028 }, /* ldr r0, bootreg_addr */
9247b1da81SPeter Maydell     { 0xe3a01001 }, /* mov r1, #1 */
9347b1da81SPeter Maydell     { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */
9447b1da81SPeter Maydell     { 0xe3a010ff }, /* mov r1, #0xff */
9547b1da81SPeter Maydell     { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
9647b1da81SPeter Maydell     { 0, FIXUP_DSB },   /* dsb */
9747b1da81SPeter Maydell     { 0xe320f003 }, /* wfi */
9847b1da81SPeter Maydell     { 0xe5901000 }, /* ldr     r1, [r0] */
9947b1da81SPeter Maydell     { 0xe1110001 }, /* tst     r1, r1 */
10047b1da81SPeter Maydell     { 0x0afffffb }, /* beq     <wfi> */
10147b1da81SPeter Maydell     { 0xe12fff11 }, /* bx      r1 */
10247b1da81SPeter Maydell     { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */
10347b1da81SPeter Maydell     { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */
10447b1da81SPeter Maydell     { 0, FIXUP_TERMINATOR }
10553018216SPaolo Bonzini };
10653018216SPaolo Bonzini 
10747b1da81SPeter Maydell static void write_bootloader(const char *name, hwaddr addr,
10847b1da81SPeter Maydell                              const ARMInsnFixup *insns, uint32_t *fixupcontext)
10947b1da81SPeter Maydell {
11047b1da81SPeter Maydell     /* Fix up the specified bootloader fragment and write it into
11147b1da81SPeter Maydell      * guest memory using rom_add_blob_fixed(). fixupcontext is
11247b1da81SPeter Maydell      * an array giving the values to write in for the fixup types
11347b1da81SPeter Maydell      * which write a value into the code array.
11447b1da81SPeter Maydell      */
11547b1da81SPeter Maydell     int i, len;
11647b1da81SPeter Maydell     uint32_t *code;
11747b1da81SPeter Maydell 
11847b1da81SPeter Maydell     len = 0;
11947b1da81SPeter Maydell     while (insns[len].fixup != FIXUP_TERMINATOR) {
12047b1da81SPeter Maydell         len++;
12147b1da81SPeter Maydell     }
12247b1da81SPeter Maydell 
12347b1da81SPeter Maydell     code = g_new0(uint32_t, len);
12447b1da81SPeter Maydell 
12547b1da81SPeter Maydell     for (i = 0; i < len; i++) {
12647b1da81SPeter Maydell         uint32_t insn = insns[i].insn;
12747b1da81SPeter Maydell         FixupType fixup = insns[i].fixup;
12847b1da81SPeter Maydell 
12947b1da81SPeter Maydell         switch (fixup) {
13047b1da81SPeter Maydell         case FIXUP_NONE:
13147b1da81SPeter Maydell             break;
13247b1da81SPeter Maydell         case FIXUP_BOARDID:
13347b1da81SPeter Maydell         case FIXUP_ARGPTR:
13447b1da81SPeter Maydell         case FIXUP_ENTRYPOINT:
13547b1da81SPeter Maydell         case FIXUP_GIC_CPU_IF:
13647b1da81SPeter Maydell         case FIXUP_BOOTREG:
13747b1da81SPeter Maydell         case FIXUP_DSB:
13847b1da81SPeter Maydell             insn = fixupcontext[fixup];
13947b1da81SPeter Maydell             break;
14047b1da81SPeter Maydell         default:
14147b1da81SPeter Maydell             abort();
14247b1da81SPeter Maydell         }
14347b1da81SPeter Maydell         code[i] = tswap32(insn);
14447b1da81SPeter Maydell     }
14547b1da81SPeter Maydell 
14647b1da81SPeter Maydell     rom_add_blob_fixed(name, code, len * sizeof(uint32_t), addr);
14747b1da81SPeter Maydell 
14847b1da81SPeter Maydell     g_free(code);
14947b1da81SPeter Maydell }
15047b1da81SPeter Maydell 
15153018216SPaolo Bonzini static void default_write_secondary(ARMCPU *cpu,
15253018216SPaolo Bonzini                                     const struct arm_boot_info *info)
15353018216SPaolo Bonzini {
15447b1da81SPeter Maydell     uint32_t fixupcontext[FIXUP_MAX];
15547b1da81SPeter Maydell 
15647b1da81SPeter Maydell     fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr;
15747b1da81SPeter Maydell     fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr;
15847b1da81SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
15947b1da81SPeter Maydell         fixupcontext[FIXUP_DSB] = DSB_INSN;
16047b1da81SPeter Maydell     } else {
16147b1da81SPeter Maydell         fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
16253018216SPaolo Bonzini     }
16347b1da81SPeter Maydell 
16447b1da81SPeter Maydell     write_bootloader("smpboot", info->smp_loader_start,
16547b1da81SPeter Maydell                      smpboot, fixupcontext);
16653018216SPaolo Bonzini }
16753018216SPaolo Bonzini 
16853018216SPaolo Bonzini static void default_reset_secondary(ARMCPU *cpu,
16953018216SPaolo Bonzini                                     const struct arm_boot_info *info)
17053018216SPaolo Bonzini {
17153018216SPaolo Bonzini     CPUARMState *env = &cpu->env;
17253018216SPaolo Bonzini 
17342874d3aSPeter Maydell     address_space_stl_notdirty(&address_space_memory, info->smp_bootreg_addr,
17442874d3aSPeter Maydell                                0, MEMTXATTRS_UNSPECIFIED, NULL);
17553018216SPaolo Bonzini     env->regs[15] = info->smp_loader_start;
17653018216SPaolo Bonzini }
17753018216SPaolo Bonzini 
17883bfffecSPeter Maydell static inline bool have_dtb(const struct arm_boot_info *info)
17983bfffecSPeter Maydell {
18083bfffecSPeter Maydell     return info->dtb_filename || info->get_dtb;
18183bfffecSPeter Maydell }
18283bfffecSPeter Maydell 
18353018216SPaolo Bonzini #define WRITE_WORD(p, value) do { \
18442874d3aSPeter Maydell     address_space_stl_notdirty(&address_space_memory, p, value, \
18542874d3aSPeter Maydell                                MEMTXATTRS_UNSPECIFIED, NULL);  \
18653018216SPaolo Bonzini     p += 4;                       \
18753018216SPaolo Bonzini } while (0)
18853018216SPaolo Bonzini 
18953018216SPaolo Bonzini static void set_kernel_args(const struct arm_boot_info *info)
19053018216SPaolo Bonzini {
19153018216SPaolo Bonzini     int initrd_size = info->initrd_size;
19253018216SPaolo Bonzini     hwaddr base = info->loader_start;
19353018216SPaolo Bonzini     hwaddr p;
19453018216SPaolo Bonzini 
19553018216SPaolo Bonzini     p = base + KERNEL_ARGS_ADDR;
19653018216SPaolo Bonzini     /* ATAG_CORE */
19753018216SPaolo Bonzini     WRITE_WORD(p, 5);
19853018216SPaolo Bonzini     WRITE_WORD(p, 0x54410001);
19953018216SPaolo Bonzini     WRITE_WORD(p, 1);
20053018216SPaolo Bonzini     WRITE_WORD(p, 0x1000);
20153018216SPaolo Bonzini     WRITE_WORD(p, 0);
20253018216SPaolo Bonzini     /* ATAG_MEM */
20353018216SPaolo Bonzini     /* TODO: handle multiple chips on one ATAG list */
20453018216SPaolo Bonzini     WRITE_WORD(p, 4);
20553018216SPaolo Bonzini     WRITE_WORD(p, 0x54410002);
20653018216SPaolo Bonzini     WRITE_WORD(p, info->ram_size);
20753018216SPaolo Bonzini     WRITE_WORD(p, info->loader_start);
20853018216SPaolo Bonzini     if (initrd_size) {
20953018216SPaolo Bonzini         /* ATAG_INITRD2 */
21053018216SPaolo Bonzini         WRITE_WORD(p, 4);
21153018216SPaolo Bonzini         WRITE_WORD(p, 0x54420005);
21253018216SPaolo Bonzini         WRITE_WORD(p, info->initrd_start);
21353018216SPaolo Bonzini         WRITE_WORD(p, initrd_size);
21453018216SPaolo Bonzini     }
21553018216SPaolo Bonzini     if (info->kernel_cmdline && *info->kernel_cmdline) {
21653018216SPaolo Bonzini         /* ATAG_CMDLINE */
21753018216SPaolo Bonzini         int cmdline_size;
21853018216SPaolo Bonzini 
21953018216SPaolo Bonzini         cmdline_size = strlen(info->kernel_cmdline);
220e1fe50dcSStefan Weil         cpu_physical_memory_write(p + 8, info->kernel_cmdline,
22153018216SPaolo Bonzini                                   cmdline_size + 1);
22253018216SPaolo Bonzini         cmdline_size = (cmdline_size >> 2) + 1;
22353018216SPaolo Bonzini         WRITE_WORD(p, cmdline_size + 2);
22453018216SPaolo Bonzini         WRITE_WORD(p, 0x54410009);
22553018216SPaolo Bonzini         p += cmdline_size * 4;
22653018216SPaolo Bonzini     }
22753018216SPaolo Bonzini     if (info->atag_board) {
22853018216SPaolo Bonzini         /* ATAG_BOARD */
22953018216SPaolo Bonzini         int atag_board_len;
23053018216SPaolo Bonzini         uint8_t atag_board_buf[0x1000];
23153018216SPaolo Bonzini 
23253018216SPaolo Bonzini         atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
23353018216SPaolo Bonzini         WRITE_WORD(p, (atag_board_len + 8) >> 2);
23453018216SPaolo Bonzini         WRITE_WORD(p, 0x414f4d50);
23553018216SPaolo Bonzini         cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
23653018216SPaolo Bonzini         p += atag_board_len;
23753018216SPaolo Bonzini     }
23853018216SPaolo Bonzini     /* ATAG_END */
23953018216SPaolo Bonzini     WRITE_WORD(p, 0);
24053018216SPaolo Bonzini     WRITE_WORD(p, 0);
24153018216SPaolo Bonzini }
24253018216SPaolo Bonzini 
24353018216SPaolo Bonzini static void set_kernel_args_old(const struct arm_boot_info *info)
24453018216SPaolo Bonzini {
24553018216SPaolo Bonzini     hwaddr p;
24653018216SPaolo Bonzini     const char *s;
24753018216SPaolo Bonzini     int initrd_size = info->initrd_size;
24853018216SPaolo Bonzini     hwaddr base = info->loader_start;
24953018216SPaolo Bonzini 
25053018216SPaolo Bonzini     /* see linux/include/asm-arm/setup.h */
25153018216SPaolo Bonzini     p = base + KERNEL_ARGS_ADDR;
25253018216SPaolo Bonzini     /* page_size */
25353018216SPaolo Bonzini     WRITE_WORD(p, 4096);
25453018216SPaolo Bonzini     /* nr_pages */
25553018216SPaolo Bonzini     WRITE_WORD(p, info->ram_size / 4096);
25653018216SPaolo Bonzini     /* ramdisk_size */
25753018216SPaolo Bonzini     WRITE_WORD(p, 0);
25853018216SPaolo Bonzini #define FLAG_READONLY	1
25953018216SPaolo Bonzini #define FLAG_RDLOAD	4
26053018216SPaolo Bonzini #define FLAG_RDPROMPT	8
26153018216SPaolo Bonzini     /* flags */
26253018216SPaolo Bonzini     WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
26353018216SPaolo Bonzini     /* rootdev */
26453018216SPaolo Bonzini     WRITE_WORD(p, (31 << 8) | 0);	/* /dev/mtdblock0 */
26553018216SPaolo Bonzini     /* video_num_cols */
26653018216SPaolo Bonzini     WRITE_WORD(p, 0);
26753018216SPaolo Bonzini     /* video_num_rows */
26853018216SPaolo Bonzini     WRITE_WORD(p, 0);
26953018216SPaolo Bonzini     /* video_x */
27053018216SPaolo Bonzini     WRITE_WORD(p, 0);
27153018216SPaolo Bonzini     /* video_y */
27253018216SPaolo Bonzini     WRITE_WORD(p, 0);
27353018216SPaolo Bonzini     /* memc_control_reg */
27453018216SPaolo Bonzini     WRITE_WORD(p, 0);
27553018216SPaolo Bonzini     /* unsigned char sounddefault */
27653018216SPaolo Bonzini     /* unsigned char adfsdrives */
27753018216SPaolo Bonzini     /* unsigned char bytes_per_char_h */
27853018216SPaolo Bonzini     /* unsigned char bytes_per_char_v */
27953018216SPaolo Bonzini     WRITE_WORD(p, 0);
28053018216SPaolo Bonzini     /* pages_in_bank[4] */
28153018216SPaolo Bonzini     WRITE_WORD(p, 0);
28253018216SPaolo Bonzini     WRITE_WORD(p, 0);
28353018216SPaolo Bonzini     WRITE_WORD(p, 0);
28453018216SPaolo Bonzini     WRITE_WORD(p, 0);
28553018216SPaolo Bonzini     /* pages_in_vram */
28653018216SPaolo Bonzini     WRITE_WORD(p, 0);
28753018216SPaolo Bonzini     /* initrd_start */
28853018216SPaolo Bonzini     if (initrd_size) {
28953018216SPaolo Bonzini         WRITE_WORD(p, info->initrd_start);
29053018216SPaolo Bonzini     } else {
29153018216SPaolo Bonzini         WRITE_WORD(p, 0);
29253018216SPaolo Bonzini     }
29353018216SPaolo Bonzini     /* initrd_size */
29453018216SPaolo Bonzini     WRITE_WORD(p, initrd_size);
29553018216SPaolo Bonzini     /* rd_start */
29653018216SPaolo Bonzini     WRITE_WORD(p, 0);
29753018216SPaolo Bonzini     /* system_rev */
29853018216SPaolo Bonzini     WRITE_WORD(p, 0);
29953018216SPaolo Bonzini     /* system_serial_low */
30053018216SPaolo Bonzini     WRITE_WORD(p, 0);
30153018216SPaolo Bonzini     /* system_serial_high */
30253018216SPaolo Bonzini     WRITE_WORD(p, 0);
30353018216SPaolo Bonzini     /* mem_fclk_21285 */
30453018216SPaolo Bonzini     WRITE_WORD(p, 0);
30553018216SPaolo Bonzini     /* zero unused fields */
30653018216SPaolo Bonzini     while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
30753018216SPaolo Bonzini         WRITE_WORD(p, 0);
30853018216SPaolo Bonzini     }
30953018216SPaolo Bonzini     s = info->kernel_cmdline;
31053018216SPaolo Bonzini     if (s) {
311e1fe50dcSStefan Weil         cpu_physical_memory_write(p, s, strlen(s) + 1);
31253018216SPaolo Bonzini     } else {
31353018216SPaolo Bonzini         WRITE_WORD(p, 0);
31453018216SPaolo Bonzini     }
31553018216SPaolo Bonzini }
31653018216SPaolo Bonzini 
317fee8ea12SArd Biesheuvel /**
318fee8ea12SArd Biesheuvel  * load_dtb() - load a device tree binary image into memory
319fee8ea12SArd Biesheuvel  * @addr:       the address to load the image at
320fee8ea12SArd Biesheuvel  * @binfo:      struct describing the boot environment
321fee8ea12SArd Biesheuvel  * @addr_limit: upper limit of the available memory area at @addr
322fee8ea12SArd Biesheuvel  *
323fee8ea12SArd Biesheuvel  * Load a device tree supplied by the machine or by the user  with the
324fee8ea12SArd Biesheuvel  * '-dtb' command line option, and put it at offset @addr in target
325fee8ea12SArd Biesheuvel  * memory.
326fee8ea12SArd Biesheuvel  *
327fee8ea12SArd Biesheuvel  * If @addr_limit contains a meaningful value (i.e., it is strictly greater
328fee8ea12SArd Biesheuvel  * than @addr), the device tree is only loaded if its size does not exceed
329fee8ea12SArd Biesheuvel  * the limit.
330fee8ea12SArd Biesheuvel  *
331fee8ea12SArd Biesheuvel  * Returns: the size of the device tree image on success,
332fee8ea12SArd Biesheuvel  *          0 if the image size exceeds the limit,
333fee8ea12SArd Biesheuvel  *          -1 on errors.
334a554ecb4Szhanghailiang  *
335a554ecb4Szhanghailiang  * Note: Must not be called unless have_dtb(binfo) is true.
336fee8ea12SArd Biesheuvel  */
337fee8ea12SArd Biesheuvel static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
338fee8ea12SArd Biesheuvel                     hwaddr addr_limit)
33953018216SPaolo Bonzini {
34053018216SPaolo Bonzini     void *fdt = NULL;
34153018216SPaolo Bonzini     int size, rc;
34270976c41SPeter Maydell     uint32_t acells, scells;
34353018216SPaolo Bonzini 
3440fb79851SJohn Rigby     if (binfo->dtb_filename) {
3450fb79851SJohn Rigby         char *filename;
34653018216SPaolo Bonzini         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
34753018216SPaolo Bonzini         if (!filename) {
34853018216SPaolo Bonzini             fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
349c23045deSPeter Maydell             goto fail;
35053018216SPaolo Bonzini         }
35153018216SPaolo Bonzini 
35253018216SPaolo Bonzini         fdt = load_device_tree(filename, &size);
35353018216SPaolo Bonzini         if (!fdt) {
35453018216SPaolo Bonzini             fprintf(stderr, "Couldn't open dtb file %s\n", filename);
35553018216SPaolo Bonzini             g_free(filename);
356c23045deSPeter Maydell             goto fail;
35753018216SPaolo Bonzini         }
35853018216SPaolo Bonzini         g_free(filename);
359a554ecb4Szhanghailiang     } else {
3600fb79851SJohn Rigby         fdt = binfo->get_dtb(binfo, &size);
3610fb79851SJohn Rigby         if (!fdt) {
3620fb79851SJohn Rigby             fprintf(stderr, "Board was unable to create a dtb blob\n");
3630fb79851SJohn Rigby             goto fail;
3640fb79851SJohn Rigby         }
3650fb79851SJohn Rigby     }
36653018216SPaolo Bonzini 
367fee8ea12SArd Biesheuvel     if (addr_limit > addr && size > (addr_limit - addr)) {
368fee8ea12SArd Biesheuvel         /* Installing the device tree blob at addr would exceed addr_limit.
369fee8ea12SArd Biesheuvel          * Whether this constitutes failure is up to the caller to decide,
370fee8ea12SArd Biesheuvel          * so just return 0 as size, i.e., no error.
371fee8ea12SArd Biesheuvel          */
372fee8ea12SArd Biesheuvel         g_free(fdt);
373fee8ea12SArd Biesheuvel         return 0;
374fee8ea12SArd Biesheuvel     }
375fee8ea12SArd Biesheuvel 
3765a4348d1SPeter Crosthwaite     acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells");
3775a4348d1SPeter Crosthwaite     scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells");
37853018216SPaolo Bonzini     if (acells == 0 || scells == 0) {
37953018216SPaolo Bonzini         fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
380c23045deSPeter Maydell         goto fail;
38153018216SPaolo Bonzini     }
38253018216SPaolo Bonzini 
38370976c41SPeter Maydell     if (scells < 2 && binfo->ram_size >= (1ULL << 32)) {
38470976c41SPeter Maydell         /* This is user error so deserves a friendlier error message
38570976c41SPeter Maydell          * than the failure of setprop_sized_cells would provide
38670976c41SPeter Maydell          */
38753018216SPaolo Bonzini         fprintf(stderr, "qemu: dtb file not compatible with "
38853018216SPaolo Bonzini                 "RAM size > 4GB\n");
389c23045deSPeter Maydell         goto fail;
39053018216SPaolo Bonzini     }
39153018216SPaolo Bonzini 
3925a4348d1SPeter Crosthwaite     rc = qemu_fdt_setprop_sized_cells(fdt, "/memory", "reg",
39370976c41SPeter Maydell                                       acells, binfo->loader_start,
39470976c41SPeter Maydell                                       scells, binfo->ram_size);
39553018216SPaolo Bonzini     if (rc < 0) {
39653018216SPaolo Bonzini         fprintf(stderr, "couldn't set /memory/reg\n");
397c23045deSPeter Maydell         goto fail;
39853018216SPaolo Bonzini     }
39953018216SPaolo Bonzini 
40053018216SPaolo Bonzini     if (binfo->kernel_cmdline && *binfo->kernel_cmdline) {
4015a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
40253018216SPaolo Bonzini                                      binfo->kernel_cmdline);
40353018216SPaolo Bonzini         if (rc < 0) {
40453018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/bootargs\n");
405c23045deSPeter Maydell             goto fail;
40653018216SPaolo Bonzini         }
40753018216SPaolo Bonzini     }
40853018216SPaolo Bonzini 
40953018216SPaolo Bonzini     if (binfo->initrd_size) {
4105a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
41153018216SPaolo Bonzini                                    binfo->initrd_start);
41253018216SPaolo Bonzini         if (rc < 0) {
41353018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
414c23045deSPeter Maydell             goto fail;
41553018216SPaolo Bonzini         }
41653018216SPaolo Bonzini 
4175a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
41853018216SPaolo Bonzini                                    binfo->initrd_start + binfo->initrd_size);
41953018216SPaolo Bonzini         if (rc < 0) {
42053018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
421c23045deSPeter Maydell             goto fail;
42253018216SPaolo Bonzini         }
42353018216SPaolo Bonzini     }
4243b1cceb8SPeter Maydell 
4253b1cceb8SPeter Maydell     if (binfo->modify_dtb) {
4263b1cceb8SPeter Maydell         binfo->modify_dtb(binfo, fdt);
4273b1cceb8SPeter Maydell     }
4283b1cceb8SPeter Maydell 
4295a4348d1SPeter Crosthwaite     qemu_fdt_dumpdtb(fdt, size);
43053018216SPaolo Bonzini 
4314c4bf654SArd Biesheuvel     /* Put the DTB into the memory map as a ROM image: this will ensure
4324c4bf654SArd Biesheuvel      * the DTB is copied again upon reset, even if addr points into RAM.
4334c4bf654SArd Biesheuvel      */
4344c4bf654SArd Biesheuvel     rom_add_blob_fixed("dtb", fdt, size, addr);
43553018216SPaolo Bonzini 
436c23045deSPeter Maydell     g_free(fdt);
437c23045deSPeter Maydell 
438fee8ea12SArd Biesheuvel     return size;
439c23045deSPeter Maydell 
440c23045deSPeter Maydell fail:
441c23045deSPeter Maydell     g_free(fdt);
442c23045deSPeter Maydell     return -1;
44353018216SPaolo Bonzini }
44453018216SPaolo Bonzini 
44553018216SPaolo Bonzini static void do_cpu_reset(void *opaque)
44653018216SPaolo Bonzini {
44753018216SPaolo Bonzini     ARMCPU *cpu = opaque;
44853018216SPaolo Bonzini     CPUARMState *env = &cpu->env;
44953018216SPaolo Bonzini     const struct arm_boot_info *info = env->boot_info;
45053018216SPaolo Bonzini 
45153018216SPaolo Bonzini     cpu_reset(CPU(cpu));
45253018216SPaolo Bonzini     if (info) {
45353018216SPaolo Bonzini         if (!info->is_linux) {
45453018216SPaolo Bonzini             /* Jump to the entry point.  */
455a9047ec3SPeter Maydell             if (env->aarch64) {
456a9047ec3SPeter Maydell                 env->pc = info->entry;
457a9047ec3SPeter Maydell             } else {
45853018216SPaolo Bonzini                 env->regs[15] = info->entry & 0xfffffffe;
45953018216SPaolo Bonzini                 env->thumb = info->entry & 1;
460a9047ec3SPeter Maydell             }
46153018216SPaolo Bonzini         } else {
462c8e829b7SGreg Bellows             /* If we are booting Linux then we need to check whether we are
463c8e829b7SGreg Bellows              * booting into secure or non-secure state and adjust the state
464c8e829b7SGreg Bellows              * accordingly.  Out of reset, ARM is defined to be in secure state
465c8e829b7SGreg Bellows              * (SCR.NS = 0), we change that here if non-secure boot has been
466c8e829b7SGreg Bellows              * requested.
467c8e829b7SGreg Bellows              */
4685097227cSGreg Bellows             if (arm_feature(env, ARM_FEATURE_EL3)) {
4695097227cSGreg Bellows                 /* AArch64 is defined to come out of reset into EL3 if enabled.
4705097227cSGreg Bellows                  * If we are booting Linux then we need to adjust our EL as
4715097227cSGreg Bellows                  * Linux expects us to be in EL2 or EL1.  AArch32 resets into
4725097227cSGreg Bellows                  * SVC, which Linux expects, so no privilege/exception level to
4735097227cSGreg Bellows                  * adjust.
4745097227cSGreg Bellows                  */
4755097227cSGreg Bellows                 if (env->aarch64) {
4765097227cSGreg Bellows                     if (arm_feature(env, ARM_FEATURE_EL2)) {
4775097227cSGreg Bellows                         env->pstate = PSTATE_MODE_EL2h;
4785097227cSGreg Bellows                     } else {
4795097227cSGreg Bellows                         env->pstate = PSTATE_MODE_EL1h;
4805097227cSGreg Bellows                     }
4815097227cSGreg Bellows                 }
4825097227cSGreg Bellows 
4835097227cSGreg Bellows                 /* Set to non-secure if not a secure boot */
4845097227cSGreg Bellows                 if (!info->secure_boot) {
4855097227cSGreg Bellows                     /* Linux expects non-secure state */
486c8e829b7SGreg Bellows                     env->cp15.scr_el3 |= SCR_NS;
487c8e829b7SGreg Bellows                 }
4885097227cSGreg Bellows             }
489c8e829b7SGreg Bellows 
490182735efSAndreas Färber             if (CPU(cpu) == first_cpu) {
4914d9ebf75SMian M. Hamayun                 if (env->aarch64) {
4924d9ebf75SMian M. Hamayun                     env->pc = info->loader_start;
4934d9ebf75SMian M. Hamayun                 } else {
49453018216SPaolo Bonzini                     env->regs[15] = info->loader_start;
4954d9ebf75SMian M. Hamayun                 }
4964d9ebf75SMian M. Hamayun 
49783bfffecSPeter Maydell                 if (!have_dtb(info)) {
49853018216SPaolo Bonzini                     if (old_param) {
49953018216SPaolo Bonzini                         set_kernel_args_old(info);
50053018216SPaolo Bonzini                     } else {
50153018216SPaolo Bonzini                         set_kernel_args(info);
50253018216SPaolo Bonzini                     }
50353018216SPaolo Bonzini                 }
50453018216SPaolo Bonzini             } else {
50553018216SPaolo Bonzini                 info->secondary_cpu_reset_hook(cpu, info);
50653018216SPaolo Bonzini             }
50753018216SPaolo Bonzini         }
50853018216SPaolo Bonzini     }
50953018216SPaolo Bonzini }
51053018216SPaolo Bonzini 
51107abe45cSLaszlo Ersek /**
51207abe45cSLaszlo Ersek  * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
51307abe45cSLaszlo Ersek  *                          by key.
51407abe45cSLaszlo Ersek  * @fw_cfg:         The firmware config instance to store the data in.
51507abe45cSLaszlo Ersek  * @size_key:       The firmware config key to store the size of the loaded
51607abe45cSLaszlo Ersek  *                  data under, with fw_cfg_add_i32().
51707abe45cSLaszlo Ersek  * @data_key:       The firmware config key to store the loaded data under,
51807abe45cSLaszlo Ersek  *                  with fw_cfg_add_bytes().
51907abe45cSLaszlo Ersek  * @image_name:     The name of the image file to load. If it is NULL, the
52007abe45cSLaszlo Ersek  *                  function returns without doing anything.
52107abe45cSLaszlo Ersek  * @try_decompress: Whether the image should be decompressed (gunzipped) before
52207abe45cSLaszlo Ersek  *                  adding it to fw_cfg. If decompression fails, the image is
52307abe45cSLaszlo Ersek  *                  loaded as-is.
52407abe45cSLaszlo Ersek  *
52507abe45cSLaszlo Ersek  * In case of failure, the function prints an error message to stderr and the
52607abe45cSLaszlo Ersek  * process exits with status 1.
52707abe45cSLaszlo Ersek  */
52807abe45cSLaszlo Ersek static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
52907abe45cSLaszlo Ersek                                  uint16_t data_key, const char *image_name,
53007abe45cSLaszlo Ersek                                  bool try_decompress)
53107abe45cSLaszlo Ersek {
53207abe45cSLaszlo Ersek     size_t size = -1;
53307abe45cSLaszlo Ersek     uint8_t *data;
53407abe45cSLaszlo Ersek 
53507abe45cSLaszlo Ersek     if (image_name == NULL) {
53607abe45cSLaszlo Ersek         return;
53707abe45cSLaszlo Ersek     }
53807abe45cSLaszlo Ersek 
53907abe45cSLaszlo Ersek     if (try_decompress) {
54007abe45cSLaszlo Ersek         size = load_image_gzipped_buffer(image_name,
54107abe45cSLaszlo Ersek                                          LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
54207abe45cSLaszlo Ersek     }
54307abe45cSLaszlo Ersek 
54407abe45cSLaszlo Ersek     if (size == (size_t)-1) {
54507abe45cSLaszlo Ersek         gchar *contents;
54607abe45cSLaszlo Ersek         gsize length;
54707abe45cSLaszlo Ersek 
54807abe45cSLaszlo Ersek         if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
54907abe45cSLaszlo Ersek             fprintf(stderr, "failed to load \"%s\"\n", image_name);
55007abe45cSLaszlo Ersek             exit(1);
55107abe45cSLaszlo Ersek         }
55207abe45cSLaszlo Ersek         size = length;
55307abe45cSLaszlo Ersek         data = (uint8_t *)contents;
55407abe45cSLaszlo Ersek     }
55507abe45cSLaszlo Ersek 
55607abe45cSLaszlo Ersek     fw_cfg_add_i32(fw_cfg, size_key, size);
55707abe45cSLaszlo Ersek     fw_cfg_add_bytes(fw_cfg, data_key, data, size);
55807abe45cSLaszlo Ersek }
55907abe45cSLaszlo Ersek 
560ac9d32e3SEric Auger static void arm_load_kernel_notify(Notifier *notifier, void *data)
56153018216SPaolo Bonzini {
562c6faa758SArd Biesheuvel     CPUState *cs;
56353018216SPaolo Bonzini     int kernel_size;
56453018216SPaolo Bonzini     int initrd_size;
56553018216SPaolo Bonzini     int is_linux = 0;
56692df8450SArd Biesheuvel     uint64_t elf_entry, elf_low_addr, elf_high_addr;
567da0af40dSPeter Maydell     int elf_machine;
5684d9ebf75SMian M. Hamayun     hwaddr entry, kernel_load_offset;
56953018216SPaolo Bonzini     int big_endian;
5704d9ebf75SMian M. Hamayun     static const ARMInsnFixup *primary_loader;
571ac9d32e3SEric Auger     ArmLoadKernelNotifier *n = DO_UPCAST(ArmLoadKernelNotifier,
572ac9d32e3SEric Auger                                          notifier, notifier);
573ac9d32e3SEric Auger     ARMCPU *cpu = n->cpu;
574ac9d32e3SEric Auger     struct arm_boot_info *info =
575ac9d32e3SEric Auger         container_of(n, struct arm_boot_info, load_kernel_notifier);
57653018216SPaolo Bonzini 
57753018216SPaolo Bonzini     /* Load the kernel.  */
57807abe45cSLaszlo Ersek     if (!info->kernel_filename || info->firmware_loaded) {
57969e7f76fSArd Biesheuvel 
58069e7f76fSArd Biesheuvel         if (have_dtb(info)) {
58107abe45cSLaszlo Ersek             /* If we have a device tree blob, but no kernel to supply it to (or
58207abe45cSLaszlo Ersek              * the kernel is supposed to be loaded by the bootloader), copy the
58307abe45cSLaszlo Ersek              * DTB to the base of RAM for the bootloader to pick up.
58469e7f76fSArd Biesheuvel              */
58569e7f76fSArd Biesheuvel             if (load_dtb(info->loader_start, info, 0) < 0) {
58669e7f76fSArd Biesheuvel                 exit(1);
58769e7f76fSArd Biesheuvel             }
58869e7f76fSArd Biesheuvel         }
58969e7f76fSArd Biesheuvel 
59007abe45cSLaszlo Ersek         if (info->kernel_filename) {
59107abe45cSLaszlo Ersek             FWCfgState *fw_cfg;
59207abe45cSLaszlo Ersek             bool try_decompressing_kernel;
59307abe45cSLaszlo Ersek 
59407abe45cSLaszlo Ersek             fw_cfg = fw_cfg_find();
59507abe45cSLaszlo Ersek             try_decompressing_kernel = arm_feature(&cpu->env,
59607abe45cSLaszlo Ersek                                                    ARM_FEATURE_AARCH64);
59707abe45cSLaszlo Ersek 
59807abe45cSLaszlo Ersek             /* Expose the kernel, the command line, and the initrd in fw_cfg.
59907abe45cSLaszlo Ersek              * We don't process them here at all, it's all left to the
60007abe45cSLaszlo Ersek              * firmware.
60107abe45cSLaszlo Ersek              */
60207abe45cSLaszlo Ersek             load_image_to_fw_cfg(fw_cfg,
60307abe45cSLaszlo Ersek                                  FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
60407abe45cSLaszlo Ersek                                  info->kernel_filename,
60507abe45cSLaszlo Ersek                                  try_decompressing_kernel);
60607abe45cSLaszlo Ersek             load_image_to_fw_cfg(fw_cfg,
60707abe45cSLaszlo Ersek                                  FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
60807abe45cSLaszlo Ersek                                  info->initrd_filename, false);
60907abe45cSLaszlo Ersek 
61007abe45cSLaszlo Ersek             if (info->kernel_cmdline) {
61107abe45cSLaszlo Ersek                 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
61207abe45cSLaszlo Ersek                                strlen(info->kernel_cmdline) + 1);
61307abe45cSLaszlo Ersek                 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
61407abe45cSLaszlo Ersek                                   info->kernel_cmdline);
61507abe45cSLaszlo Ersek             }
61607abe45cSLaszlo Ersek         }
61707abe45cSLaszlo Ersek 
61807abe45cSLaszlo Ersek         /* We will start from address 0 (typically a boot ROM image) in the
61907abe45cSLaszlo Ersek          * same way as hardware.
6209546dbabSPeter Maydell          */
6219546dbabSPeter Maydell         return;
62253018216SPaolo Bonzini     }
62353018216SPaolo Bonzini 
6244d9ebf75SMian M. Hamayun     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
6254d9ebf75SMian M. Hamayun         primary_loader = bootloader_aarch64;
6264d9ebf75SMian M. Hamayun         kernel_load_offset = KERNEL64_LOAD_ADDR;
627da0af40dSPeter Maydell         elf_machine = EM_AARCH64;
6284d9ebf75SMian M. Hamayun     } else {
6294d9ebf75SMian M. Hamayun         primary_loader = bootloader;
6304d9ebf75SMian M. Hamayun         kernel_load_offset = KERNEL_LOAD_ADDR;
631da0af40dSPeter Maydell         elf_machine = EM_ARM;
6324d9ebf75SMian M. Hamayun     }
6334d9ebf75SMian M. Hamayun 
6342ff3de68SMarkus Armbruster     info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb");
63553018216SPaolo Bonzini 
63653018216SPaolo Bonzini     if (!info->secondary_cpu_reset_hook) {
63753018216SPaolo Bonzini         info->secondary_cpu_reset_hook = default_reset_secondary;
63853018216SPaolo Bonzini     }
63953018216SPaolo Bonzini     if (!info->write_secondary_boot) {
64053018216SPaolo Bonzini         info->write_secondary_boot = default_write_secondary;
64153018216SPaolo Bonzini     }
64253018216SPaolo Bonzini 
64353018216SPaolo Bonzini     if (info->nb_cpus == 0)
64453018216SPaolo Bonzini         info->nb_cpus = 1;
64553018216SPaolo Bonzini 
64653018216SPaolo Bonzini #ifdef TARGET_WORDS_BIGENDIAN
64753018216SPaolo Bonzini     big_endian = 1;
64853018216SPaolo Bonzini #else
64953018216SPaolo Bonzini     big_endian = 0;
65053018216SPaolo Bonzini #endif
65153018216SPaolo Bonzini 
65253018216SPaolo Bonzini     /* We want to put the initrd far enough into RAM that when the
65353018216SPaolo Bonzini      * kernel is uncompressed it will not clobber the initrd. However
65453018216SPaolo Bonzini      * on boards without much RAM we must ensure that we still leave
65553018216SPaolo Bonzini      * enough room for a decent sized initrd, and on boards with large
65653018216SPaolo Bonzini      * amounts of RAM we must avoid the initrd being so far up in RAM
65753018216SPaolo Bonzini      * that it is outside lowmem and inaccessible to the kernel.
65853018216SPaolo Bonzini      * So for boards with less  than 256MB of RAM we put the initrd
65953018216SPaolo Bonzini      * halfway into RAM, and for boards with 256MB of RAM or more we put
66053018216SPaolo Bonzini      * the initrd at 128MB.
66153018216SPaolo Bonzini      */
66253018216SPaolo Bonzini     info->initrd_start = info->loader_start +
66353018216SPaolo Bonzini         MIN(info->ram_size / 2, 128 * 1024 * 1024);
66453018216SPaolo Bonzini 
66553018216SPaolo Bonzini     /* Assume that raw images are linux kernels, and ELF images are not.  */
66653018216SPaolo Bonzini     kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry,
66792df8450SArd Biesheuvel                            &elf_low_addr, &elf_high_addr, big_endian,
66892df8450SArd Biesheuvel                            elf_machine, 1);
66992df8450SArd Biesheuvel     if (kernel_size > 0 && have_dtb(info)) {
67092df8450SArd Biesheuvel         /* If there is still some room left at the base of RAM, try and put
67192df8450SArd Biesheuvel          * the DTB there like we do for images loaded with -bios or -pflash.
67292df8450SArd Biesheuvel          */
67392df8450SArd Biesheuvel         if (elf_low_addr > info->loader_start
67492df8450SArd Biesheuvel             || elf_high_addr < info->loader_start) {
67592df8450SArd Biesheuvel             /* Pass elf_low_addr as address limit to load_dtb if it may be
67692df8450SArd Biesheuvel              * pointing into RAM, otherwise pass '0' (no limit)
67792df8450SArd Biesheuvel              */
67892df8450SArd Biesheuvel             if (elf_low_addr < info->loader_start) {
67992df8450SArd Biesheuvel                 elf_low_addr = 0;
68092df8450SArd Biesheuvel             }
68192df8450SArd Biesheuvel             if (load_dtb(info->loader_start, info, elf_low_addr) < 0) {
68292df8450SArd Biesheuvel                 exit(1);
68392df8450SArd Biesheuvel             }
68492df8450SArd Biesheuvel         }
68592df8450SArd Biesheuvel     }
68653018216SPaolo Bonzini     entry = elf_entry;
68753018216SPaolo Bonzini     if (kernel_size < 0) {
68853018216SPaolo Bonzini         kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
68925bda50aSMax Filippov                                   &is_linux, NULL, NULL);
69053018216SPaolo Bonzini     }
6916f5d3cbeSRichard W.M. Jones     /* On aarch64, it's the bootloader's job to uncompress the kernel. */
6926f5d3cbeSRichard W.M. Jones     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
6936f5d3cbeSRichard W.M. Jones         entry = info->loader_start + kernel_load_offset;
6946f5d3cbeSRichard W.M. Jones         kernel_size = load_image_gzipped(info->kernel_filename, entry,
6956f5d3cbeSRichard W.M. Jones                                          info->ram_size - kernel_load_offset);
6966f5d3cbeSRichard W.M. Jones         is_linux = 1;
6976f5d3cbeSRichard W.M. Jones     }
69853018216SPaolo Bonzini     if (kernel_size < 0) {
6994d9ebf75SMian M. Hamayun         entry = info->loader_start + kernel_load_offset;
70053018216SPaolo Bonzini         kernel_size = load_image_targphys(info->kernel_filename, entry,
7014d9ebf75SMian M. Hamayun                                           info->ram_size - kernel_load_offset);
70253018216SPaolo Bonzini         is_linux = 1;
70353018216SPaolo Bonzini     }
70453018216SPaolo Bonzini     if (kernel_size < 0) {
70553018216SPaolo Bonzini         fprintf(stderr, "qemu: could not load kernel '%s'\n",
70653018216SPaolo Bonzini                 info->kernel_filename);
70753018216SPaolo Bonzini         exit(1);
70853018216SPaolo Bonzini     }
70953018216SPaolo Bonzini     info->entry = entry;
71053018216SPaolo Bonzini     if (is_linux) {
71147b1da81SPeter Maydell         uint32_t fixupcontext[FIXUP_MAX];
71247b1da81SPeter Maydell 
71353018216SPaolo Bonzini         if (info->initrd_filename) {
714fd76663eSSoren Brinkmann             initrd_size = load_ramdisk(info->initrd_filename,
715fd76663eSSoren Brinkmann                                        info->initrd_start,
716fd76663eSSoren Brinkmann                                        info->ram_size -
717fd76663eSSoren Brinkmann                                        info->initrd_start);
718fd76663eSSoren Brinkmann             if (initrd_size < 0) {
71953018216SPaolo Bonzini                 initrd_size = load_image_targphys(info->initrd_filename,
72053018216SPaolo Bonzini                                                   info->initrd_start,
72153018216SPaolo Bonzini                                                   info->ram_size -
72253018216SPaolo Bonzini                                                   info->initrd_start);
723fd76663eSSoren Brinkmann             }
72453018216SPaolo Bonzini             if (initrd_size < 0) {
72553018216SPaolo Bonzini                 fprintf(stderr, "qemu: could not load initrd '%s'\n",
72653018216SPaolo Bonzini                         info->initrd_filename);
72753018216SPaolo Bonzini                 exit(1);
72853018216SPaolo Bonzini             }
72953018216SPaolo Bonzini         } else {
73053018216SPaolo Bonzini             initrd_size = 0;
73153018216SPaolo Bonzini         }
73253018216SPaolo Bonzini         info->initrd_size = initrd_size;
73353018216SPaolo Bonzini 
73447b1da81SPeter Maydell         fixupcontext[FIXUP_BOARDID] = info->board_id;
73553018216SPaolo Bonzini 
73653018216SPaolo Bonzini         /* for device tree boot, we pass the DTB directly in r2. Otherwise
73753018216SPaolo Bonzini          * we point to the kernel args.
73853018216SPaolo Bonzini          */
73983bfffecSPeter Maydell         if (have_dtb(info)) {
74053018216SPaolo Bonzini             /* Place the DTB after the initrd in memory. Note that some
74153018216SPaolo Bonzini              * kernels will trash anything in the 4K page the initrd
74253018216SPaolo Bonzini              * ends in, so make sure the DTB isn't caught up in that.
74353018216SPaolo Bonzini              */
74453018216SPaolo Bonzini             hwaddr dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size,
74553018216SPaolo Bonzini                                              4096);
746fee8ea12SArd Biesheuvel             if (load_dtb(dtb_start, info, 0) < 0) {
74753018216SPaolo Bonzini                 exit(1);
74853018216SPaolo Bonzini             }
74947b1da81SPeter Maydell             fixupcontext[FIXUP_ARGPTR] = dtb_start;
75053018216SPaolo Bonzini         } else {
75147b1da81SPeter Maydell             fixupcontext[FIXUP_ARGPTR] = info->loader_start + KERNEL_ARGS_ADDR;
75253018216SPaolo Bonzini             if (info->ram_size >= (1ULL << 32)) {
75353018216SPaolo Bonzini                 fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
75453018216SPaolo Bonzini                         " Linux kernel using ATAGS (try passing a device tree"
75553018216SPaolo Bonzini                         " using -dtb)\n");
75653018216SPaolo Bonzini                 exit(1);
75753018216SPaolo Bonzini             }
75853018216SPaolo Bonzini         }
75947b1da81SPeter Maydell         fixupcontext[FIXUP_ENTRYPOINT] = entry;
76047b1da81SPeter Maydell 
76147b1da81SPeter Maydell         write_bootloader("bootloader", info->loader_start,
7624d9ebf75SMian M. Hamayun                          primary_loader, fixupcontext);
76347b1da81SPeter Maydell 
76453018216SPaolo Bonzini         if (info->nb_cpus > 1) {
76553018216SPaolo Bonzini             info->write_secondary_boot(cpu, info);
76653018216SPaolo Bonzini         }
76753018216SPaolo Bonzini     }
76853018216SPaolo Bonzini     info->is_linux = is_linux;
76953018216SPaolo Bonzini 
770c6faa758SArd Biesheuvel     for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
771c6faa758SArd Biesheuvel         ARM_CPU(cs)->env.boot_info = info;
77253018216SPaolo Bonzini     }
77353018216SPaolo Bonzini }
774ac9d32e3SEric Auger 
775ac9d32e3SEric Auger void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
776ac9d32e3SEric Auger {
777*63a183edSEric Auger     CPUState *cs;
778*63a183edSEric Auger 
779ac9d32e3SEric Auger     info->load_kernel_notifier.cpu = cpu;
780ac9d32e3SEric Auger     info->load_kernel_notifier.notifier.notify = arm_load_kernel_notify;
781ac9d32e3SEric Auger     qemu_add_machine_init_done_notifier(&info->load_kernel_notifier.notifier);
782*63a183edSEric Auger 
783*63a183edSEric Auger     /* CPU objects (unlike devices) are not automatically reset on system
784*63a183edSEric Auger      * reset, so we must always register a handler to do so. If we're
785*63a183edSEric Auger      * actually loading a kernel, the handler is also responsible for
786*63a183edSEric Auger      * arranging that we start it correctly.
787*63a183edSEric Auger      */
788*63a183edSEric Auger     for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
789*63a183edSEric Auger         qemu_register_reset(do_cpu_reset, ARM_CPU(cs));
790*63a183edSEric Auger     }
791ac9d32e3SEric Auger }
792