xref: /openbmc/qemu/hw/arm/boot.c (revision 922a01a013d2270682a188258cbccacfecf8129c)
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"
11c0dbca36SAlistair Francis #include "qemu/error-report.h"
12da34e65cSMarkus Armbruster #include "qapi/error.h"
13b77257d7SGuenter Roeck #include <libfdt.h>
1453018216SPaolo Bonzini #include "hw/hw.h"
15bd2be150SPeter Maydell #include "hw/arm/arm.h"
16d8b1ae42SPeter Maydell #include "hw/arm/linux-boot-if.h"
17baf6b681SPeter Crosthwaite #include "sysemu/kvm.h"
1853018216SPaolo Bonzini #include "sysemu/sysemu.h"
199695200aSShannon Zhao #include "sysemu/numa.h"
2053018216SPaolo Bonzini #include "hw/boards.h"
2153018216SPaolo Bonzini #include "hw/loader.h"
2253018216SPaolo Bonzini #include "elf.h"
2353018216SPaolo Bonzini #include "sysemu/device_tree.h"
2453018216SPaolo Bonzini #include "qemu/config-file.h"
25*922a01a0SMarkus Armbruster #include "qemu/option.h"
262198a121SEdgar E. Iglesias #include "exec/address-spaces.h"
2753018216SPaolo Bonzini 
284d9ebf75SMian M. Hamayun /* Kernel boot protocol is specified in the kernel docs
294d9ebf75SMian M. Hamayun  * Documentation/arm/Booting and Documentation/arm64/booting.txt
304d9ebf75SMian M. Hamayun  * They have different preferred image load offsets from system RAM base.
314d9ebf75SMian M. Hamayun  */
3253018216SPaolo Bonzini #define KERNEL_ARGS_ADDR 0x100
3353018216SPaolo Bonzini #define KERNEL_LOAD_ADDR 0x00010000
344d9ebf75SMian M. Hamayun #define KERNEL64_LOAD_ADDR 0x00080000
3553018216SPaolo Bonzini 
3668115ed5SArd Biesheuvel #define ARM64_TEXT_OFFSET_OFFSET    8
3768115ed5SArd Biesheuvel #define ARM64_MAGIC_OFFSET          56
3868115ed5SArd Biesheuvel 
3947b1da81SPeter Maydell typedef enum {
4047b1da81SPeter Maydell     FIXUP_NONE = 0,     /* do nothing */
4147b1da81SPeter Maydell     FIXUP_TERMINATOR,   /* end of insns */
4247b1da81SPeter Maydell     FIXUP_BOARDID,      /* overwrite with board ID number */
4310b8ec73SPeter Crosthwaite     FIXUP_BOARD_SETUP,  /* overwrite with board specific setup code address */
4447b1da81SPeter Maydell     FIXUP_ARGPTR,       /* overwrite with pointer to kernel args */
4547b1da81SPeter Maydell     FIXUP_ENTRYPOINT,   /* overwrite with kernel entry point */
4647b1da81SPeter Maydell     FIXUP_GIC_CPU_IF,   /* overwrite with GIC CPU interface address */
4747b1da81SPeter Maydell     FIXUP_BOOTREG,      /* overwrite with boot register address */
4847b1da81SPeter Maydell     FIXUP_DSB,          /* overwrite with correct DSB insn for cpu */
4947b1da81SPeter Maydell     FIXUP_MAX,
5047b1da81SPeter Maydell } FixupType;
5147b1da81SPeter Maydell 
5247b1da81SPeter Maydell typedef struct ARMInsnFixup {
5347b1da81SPeter Maydell     uint32_t insn;
5447b1da81SPeter Maydell     FixupType fixup;
5547b1da81SPeter Maydell } ARMInsnFixup;
5647b1da81SPeter Maydell 
574d9ebf75SMian M. Hamayun static const ARMInsnFixup bootloader_aarch64[] = {
584d9ebf75SMian M. Hamayun     { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
594d9ebf75SMian M. Hamayun     { 0xaa1f03e1 }, /* mov x1, xzr */
604d9ebf75SMian M. Hamayun     { 0xaa1f03e2 }, /* mov x2, xzr */
614d9ebf75SMian M. Hamayun     { 0xaa1f03e3 }, /* mov x3, xzr */
624d9ebf75SMian M. Hamayun     { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
634d9ebf75SMian M. Hamayun     { 0xd61f0080 }, /* br x4      ; Jump to the kernel entry point */
644d9ebf75SMian M. Hamayun     { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */
654d9ebf75SMian M. Hamayun     { 0 }, /* .word @DTB Higher 32-bits */
664d9ebf75SMian M. Hamayun     { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */
674d9ebf75SMian M. Hamayun     { 0 }, /* .word @Kernel Entry Higher 32-bits */
684d9ebf75SMian M. Hamayun     { 0, FIXUP_TERMINATOR }
694d9ebf75SMian M. Hamayun };
704d9ebf75SMian M. Hamayun 
7110b8ec73SPeter Crosthwaite /* A very small bootloader: call the board-setup code (if needed),
7210b8ec73SPeter Crosthwaite  * set r0-r2, then jump to the kernel.
7310b8ec73SPeter Crosthwaite  * If we're not calling boot setup code then we don't copy across
7410b8ec73SPeter Crosthwaite  * the first BOOTLOADER_NO_BOARD_SETUP_OFFSET insns in this array.
7510b8ec73SPeter Crosthwaite  */
7610b8ec73SPeter Crosthwaite 
7747b1da81SPeter Maydell static const ARMInsnFixup bootloader[] = {
78b4850e5aSSylvain Garrigues     { 0xe28fe004 }, /* add     lr, pc, #4 */
7910b8ec73SPeter Crosthwaite     { 0xe51ff004 }, /* ldr     pc, [pc, #-4] */
8010b8ec73SPeter Crosthwaite     { 0, FIXUP_BOARD_SETUP },
8110b8ec73SPeter Crosthwaite #define BOOTLOADER_NO_BOARD_SETUP_OFFSET 3
8247b1da81SPeter Maydell     { 0xe3a00000 }, /* mov     r0, #0 */
8347b1da81SPeter Maydell     { 0xe59f1004 }, /* ldr     r1, [pc, #4] */
8447b1da81SPeter Maydell     { 0xe59f2004 }, /* ldr     r2, [pc, #4] */
8547b1da81SPeter Maydell     { 0xe59ff004 }, /* ldr     pc, [pc, #4] */
8647b1da81SPeter Maydell     { 0, FIXUP_BOARDID },
8747b1da81SPeter Maydell     { 0, FIXUP_ARGPTR },
8847b1da81SPeter Maydell     { 0, FIXUP_ENTRYPOINT },
8947b1da81SPeter Maydell     { 0, FIXUP_TERMINATOR }
9053018216SPaolo Bonzini };
9153018216SPaolo Bonzini 
9253018216SPaolo Bonzini /* Handling for secondary CPU boot in a multicore system.
9353018216SPaolo Bonzini  * Unlike the uniprocessor/primary CPU boot, this is platform
9453018216SPaolo Bonzini  * dependent. The default code here is based on the secondary
9553018216SPaolo Bonzini  * CPU boot protocol used on realview/vexpress boards, with
9653018216SPaolo Bonzini  * some parameterisation to increase its flexibility.
9753018216SPaolo Bonzini  * QEMU platform models for which this code is not appropriate
9853018216SPaolo Bonzini  * should override write_secondary_boot and secondary_cpu_reset_hook
9953018216SPaolo Bonzini  * instead.
10053018216SPaolo Bonzini  *
10153018216SPaolo Bonzini  * This code enables the interrupt controllers for the secondary
10253018216SPaolo Bonzini  * CPUs and then puts all the secondary CPUs into a loop waiting
10353018216SPaolo Bonzini  * for an interprocessor interrupt and polling a configurable
10453018216SPaolo Bonzini  * location for the kernel secondary CPU entry point.
10553018216SPaolo Bonzini  */
10653018216SPaolo Bonzini #define DSB_INSN 0xf57ff04f
10753018216SPaolo Bonzini #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
10853018216SPaolo Bonzini 
10947b1da81SPeter Maydell static const ARMInsnFixup smpboot[] = {
11047b1da81SPeter Maydell     { 0xe59f2028 }, /* ldr r2, gic_cpu_if */
11147b1da81SPeter Maydell     { 0xe59f0028 }, /* ldr r0, bootreg_addr */
11247b1da81SPeter Maydell     { 0xe3a01001 }, /* mov r1, #1 */
11347b1da81SPeter Maydell     { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */
11447b1da81SPeter Maydell     { 0xe3a010ff }, /* mov r1, #0xff */
11547b1da81SPeter Maydell     { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
11647b1da81SPeter Maydell     { 0, FIXUP_DSB },   /* dsb */
11747b1da81SPeter Maydell     { 0xe320f003 }, /* wfi */
11847b1da81SPeter Maydell     { 0xe5901000 }, /* ldr     r1, [r0] */
11947b1da81SPeter Maydell     { 0xe1110001 }, /* tst     r1, r1 */
12047b1da81SPeter Maydell     { 0x0afffffb }, /* beq     <wfi> */
12147b1da81SPeter Maydell     { 0xe12fff11 }, /* bx      r1 */
12247b1da81SPeter Maydell     { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */
12347b1da81SPeter Maydell     { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */
12447b1da81SPeter Maydell     { 0, FIXUP_TERMINATOR }
12553018216SPaolo Bonzini };
12653018216SPaolo Bonzini 
12747b1da81SPeter Maydell static void write_bootloader(const char *name, hwaddr addr,
12847b1da81SPeter Maydell                              const ARMInsnFixup *insns, uint32_t *fixupcontext)
12947b1da81SPeter Maydell {
13047b1da81SPeter Maydell     /* Fix up the specified bootloader fragment and write it into
13147b1da81SPeter Maydell      * guest memory using rom_add_blob_fixed(). fixupcontext is
13247b1da81SPeter Maydell      * an array giving the values to write in for the fixup types
13347b1da81SPeter Maydell      * which write a value into the code array.
13447b1da81SPeter Maydell      */
13547b1da81SPeter Maydell     int i, len;
13647b1da81SPeter Maydell     uint32_t *code;
13747b1da81SPeter Maydell 
13847b1da81SPeter Maydell     len = 0;
13947b1da81SPeter Maydell     while (insns[len].fixup != FIXUP_TERMINATOR) {
14047b1da81SPeter Maydell         len++;
14147b1da81SPeter Maydell     }
14247b1da81SPeter Maydell 
14347b1da81SPeter Maydell     code = g_new0(uint32_t, len);
14447b1da81SPeter Maydell 
14547b1da81SPeter Maydell     for (i = 0; i < len; i++) {
14647b1da81SPeter Maydell         uint32_t insn = insns[i].insn;
14747b1da81SPeter Maydell         FixupType fixup = insns[i].fixup;
14847b1da81SPeter Maydell 
14947b1da81SPeter Maydell         switch (fixup) {
15047b1da81SPeter Maydell         case FIXUP_NONE:
15147b1da81SPeter Maydell             break;
15247b1da81SPeter Maydell         case FIXUP_BOARDID:
15310b8ec73SPeter Crosthwaite         case FIXUP_BOARD_SETUP:
15447b1da81SPeter Maydell         case FIXUP_ARGPTR:
15547b1da81SPeter Maydell         case FIXUP_ENTRYPOINT:
15647b1da81SPeter Maydell         case FIXUP_GIC_CPU_IF:
15747b1da81SPeter Maydell         case FIXUP_BOOTREG:
15847b1da81SPeter Maydell         case FIXUP_DSB:
15947b1da81SPeter Maydell             insn = fixupcontext[fixup];
16047b1da81SPeter Maydell             break;
16147b1da81SPeter Maydell         default:
16247b1da81SPeter Maydell             abort();
16347b1da81SPeter Maydell         }
16447b1da81SPeter Maydell         code[i] = tswap32(insn);
16547b1da81SPeter Maydell     }
16647b1da81SPeter Maydell 
16747b1da81SPeter Maydell     rom_add_blob_fixed(name, code, len * sizeof(uint32_t), addr);
16847b1da81SPeter Maydell 
16947b1da81SPeter Maydell     g_free(code);
17047b1da81SPeter Maydell }
17147b1da81SPeter Maydell 
17253018216SPaolo Bonzini static void default_write_secondary(ARMCPU *cpu,
17353018216SPaolo Bonzini                                     const struct arm_boot_info *info)
17453018216SPaolo Bonzini {
17547b1da81SPeter Maydell     uint32_t fixupcontext[FIXUP_MAX];
17647b1da81SPeter Maydell 
17747b1da81SPeter Maydell     fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr;
17847b1da81SPeter Maydell     fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr;
17947b1da81SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
18047b1da81SPeter Maydell         fixupcontext[FIXUP_DSB] = DSB_INSN;
18147b1da81SPeter Maydell     } else {
18247b1da81SPeter Maydell         fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
18353018216SPaolo Bonzini     }
18447b1da81SPeter Maydell 
18547b1da81SPeter Maydell     write_bootloader("smpboot", info->smp_loader_start,
18647b1da81SPeter Maydell                      smpboot, fixupcontext);
18753018216SPaolo Bonzini }
18853018216SPaolo Bonzini 
189716536a9SAndrew Baumann void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,
190716536a9SAndrew Baumann                                             const struct arm_boot_info *info,
191716536a9SAndrew Baumann                                             hwaddr mvbar_addr)
192716536a9SAndrew Baumann {
193716536a9SAndrew Baumann     int n;
194716536a9SAndrew Baumann     uint32_t mvbar_blob[] = {
195716536a9SAndrew Baumann         /* mvbar_addr: secure monitor vectors
196716536a9SAndrew Baumann          * Default unimplemented and unused vectors to spin. Makes it
197716536a9SAndrew Baumann          * easier to debug (as opposed to the CPU running away).
198716536a9SAndrew Baumann          */
199716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
200716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
201716536a9SAndrew Baumann         0xe1b0f00e, /* movs pc, lr ;SMC exception return */
202716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
203716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
204716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
205716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
206716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
207716536a9SAndrew Baumann     };
208716536a9SAndrew Baumann     uint32_t board_setup_blob[] = {
209716536a9SAndrew Baumann         /* board setup addr */
210716536a9SAndrew Baumann         0xe3a00e00 + (mvbar_addr >> 4), /* mov r0, #mvbar_addr */
211716536a9SAndrew Baumann         0xee0c0f30, /* mcr     p15, 0, r0, c12, c0, 1 ;set MVBAR */
212716536a9SAndrew Baumann         0xee110f11, /* mrc     p15, 0, r0, c1 , c1, 0 ;read SCR */
213716536a9SAndrew Baumann         0xe3800031, /* orr     r0, #0x31              ;enable AW, FW, NS */
214716536a9SAndrew Baumann         0xee010f11, /* mcr     p15, 0, r0, c1, c1, 0  ;write SCR */
215716536a9SAndrew Baumann         0xe1a0100e, /* mov     r1, lr                 ;save LR across SMC */
216716536a9SAndrew Baumann         0xe1600070, /* smc     #0                     ;call monitor to flush SCR */
217716536a9SAndrew Baumann         0xe1a0f001, /* mov     pc, r1                 ;return */
218716536a9SAndrew Baumann     };
219716536a9SAndrew Baumann 
220716536a9SAndrew Baumann     /* check that mvbar_addr is correctly aligned and relocatable (using MOV) */
221716536a9SAndrew Baumann     assert((mvbar_addr & 0x1f) == 0 && (mvbar_addr >> 4) < 0x100);
222716536a9SAndrew Baumann 
223716536a9SAndrew Baumann     /* check that these blobs don't overlap */
224716536a9SAndrew Baumann     assert((mvbar_addr + sizeof(mvbar_blob) <= info->board_setup_addr)
225716536a9SAndrew Baumann           || (info->board_setup_addr + sizeof(board_setup_blob) <= mvbar_addr));
226716536a9SAndrew Baumann 
227716536a9SAndrew Baumann     for (n = 0; n < ARRAY_SIZE(mvbar_blob); n++) {
228716536a9SAndrew Baumann         mvbar_blob[n] = tswap32(mvbar_blob[n]);
229716536a9SAndrew Baumann     }
230716536a9SAndrew Baumann     rom_add_blob_fixed("board-setup-mvbar", mvbar_blob, sizeof(mvbar_blob),
231716536a9SAndrew Baumann                        mvbar_addr);
232716536a9SAndrew Baumann 
233716536a9SAndrew Baumann     for (n = 0; n < ARRAY_SIZE(board_setup_blob); n++) {
234716536a9SAndrew Baumann         board_setup_blob[n] = tswap32(board_setup_blob[n]);
235716536a9SAndrew Baumann     }
236716536a9SAndrew Baumann     rom_add_blob_fixed("board-setup", board_setup_blob,
237716536a9SAndrew Baumann                        sizeof(board_setup_blob), info->board_setup_addr);
238716536a9SAndrew Baumann }
239716536a9SAndrew Baumann 
24053018216SPaolo Bonzini static void default_reset_secondary(ARMCPU *cpu,
24153018216SPaolo Bonzini                                     const struct arm_boot_info *info)
24253018216SPaolo Bonzini {
2434df81c6eSPeter Crosthwaite     CPUState *cs = CPU(cpu);
24453018216SPaolo Bonzini 
24542874d3aSPeter Maydell     address_space_stl_notdirty(&address_space_memory, info->smp_bootreg_addr,
24642874d3aSPeter Maydell                                0, MEMTXATTRS_UNSPECIFIED, NULL);
2474df81c6eSPeter Crosthwaite     cpu_set_pc(cs, info->smp_loader_start);
24853018216SPaolo Bonzini }
24953018216SPaolo Bonzini 
25083bfffecSPeter Maydell static inline bool have_dtb(const struct arm_boot_info *info)
25183bfffecSPeter Maydell {
25283bfffecSPeter Maydell     return info->dtb_filename || info->get_dtb;
25383bfffecSPeter Maydell }
25483bfffecSPeter Maydell 
25553018216SPaolo Bonzini #define WRITE_WORD(p, value) do { \
25642874d3aSPeter Maydell     address_space_stl_notdirty(&address_space_memory, p, value, \
25742874d3aSPeter Maydell                                MEMTXATTRS_UNSPECIFIED, NULL);  \
25853018216SPaolo Bonzini     p += 4;                       \
25953018216SPaolo Bonzini } while (0)
26053018216SPaolo Bonzini 
26153018216SPaolo Bonzini static void set_kernel_args(const struct arm_boot_info *info)
26253018216SPaolo Bonzini {
26353018216SPaolo Bonzini     int initrd_size = info->initrd_size;
26453018216SPaolo Bonzini     hwaddr base = info->loader_start;
26553018216SPaolo Bonzini     hwaddr p;
26653018216SPaolo Bonzini 
26753018216SPaolo Bonzini     p = base + KERNEL_ARGS_ADDR;
26853018216SPaolo Bonzini     /* ATAG_CORE */
26953018216SPaolo Bonzini     WRITE_WORD(p, 5);
27053018216SPaolo Bonzini     WRITE_WORD(p, 0x54410001);
27153018216SPaolo Bonzini     WRITE_WORD(p, 1);
27253018216SPaolo Bonzini     WRITE_WORD(p, 0x1000);
27353018216SPaolo Bonzini     WRITE_WORD(p, 0);
27453018216SPaolo Bonzini     /* ATAG_MEM */
27553018216SPaolo Bonzini     /* TODO: handle multiple chips on one ATAG list */
27653018216SPaolo Bonzini     WRITE_WORD(p, 4);
27753018216SPaolo Bonzini     WRITE_WORD(p, 0x54410002);
27853018216SPaolo Bonzini     WRITE_WORD(p, info->ram_size);
27953018216SPaolo Bonzini     WRITE_WORD(p, info->loader_start);
28053018216SPaolo Bonzini     if (initrd_size) {
28153018216SPaolo Bonzini         /* ATAG_INITRD2 */
28253018216SPaolo Bonzini         WRITE_WORD(p, 4);
28353018216SPaolo Bonzini         WRITE_WORD(p, 0x54420005);
28453018216SPaolo Bonzini         WRITE_WORD(p, info->initrd_start);
28553018216SPaolo Bonzini         WRITE_WORD(p, initrd_size);
28653018216SPaolo Bonzini     }
28753018216SPaolo Bonzini     if (info->kernel_cmdline && *info->kernel_cmdline) {
28853018216SPaolo Bonzini         /* ATAG_CMDLINE */
28953018216SPaolo Bonzini         int cmdline_size;
29053018216SPaolo Bonzini 
29153018216SPaolo Bonzini         cmdline_size = strlen(info->kernel_cmdline);
292e1fe50dcSStefan Weil         cpu_physical_memory_write(p + 8, info->kernel_cmdline,
29353018216SPaolo Bonzini                                   cmdline_size + 1);
29453018216SPaolo Bonzini         cmdline_size = (cmdline_size >> 2) + 1;
29553018216SPaolo Bonzini         WRITE_WORD(p, cmdline_size + 2);
29653018216SPaolo Bonzini         WRITE_WORD(p, 0x54410009);
29753018216SPaolo Bonzini         p += cmdline_size * 4;
29853018216SPaolo Bonzini     }
29953018216SPaolo Bonzini     if (info->atag_board) {
30053018216SPaolo Bonzini         /* ATAG_BOARD */
30153018216SPaolo Bonzini         int atag_board_len;
30253018216SPaolo Bonzini         uint8_t atag_board_buf[0x1000];
30353018216SPaolo Bonzini 
30453018216SPaolo Bonzini         atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
30553018216SPaolo Bonzini         WRITE_WORD(p, (atag_board_len + 8) >> 2);
30653018216SPaolo Bonzini         WRITE_WORD(p, 0x414f4d50);
30753018216SPaolo Bonzini         cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
30853018216SPaolo Bonzini         p += atag_board_len;
30953018216SPaolo Bonzini     }
31053018216SPaolo Bonzini     /* ATAG_END */
31153018216SPaolo Bonzini     WRITE_WORD(p, 0);
31253018216SPaolo Bonzini     WRITE_WORD(p, 0);
31353018216SPaolo Bonzini }
31453018216SPaolo Bonzini 
31553018216SPaolo Bonzini static void set_kernel_args_old(const struct arm_boot_info *info)
31653018216SPaolo Bonzini {
31753018216SPaolo Bonzini     hwaddr p;
31853018216SPaolo Bonzini     const char *s;
31953018216SPaolo Bonzini     int initrd_size = info->initrd_size;
32053018216SPaolo Bonzini     hwaddr base = info->loader_start;
32153018216SPaolo Bonzini 
32253018216SPaolo Bonzini     /* see linux/include/asm-arm/setup.h */
32353018216SPaolo Bonzini     p = base + KERNEL_ARGS_ADDR;
32453018216SPaolo Bonzini     /* page_size */
32553018216SPaolo Bonzini     WRITE_WORD(p, 4096);
32653018216SPaolo Bonzini     /* nr_pages */
32753018216SPaolo Bonzini     WRITE_WORD(p, info->ram_size / 4096);
32853018216SPaolo Bonzini     /* ramdisk_size */
32953018216SPaolo Bonzini     WRITE_WORD(p, 0);
33053018216SPaolo Bonzini #define FLAG_READONLY	1
33153018216SPaolo Bonzini #define FLAG_RDLOAD	4
33253018216SPaolo Bonzini #define FLAG_RDPROMPT	8
33353018216SPaolo Bonzini     /* flags */
33453018216SPaolo Bonzini     WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
33553018216SPaolo Bonzini     /* rootdev */
33653018216SPaolo Bonzini     WRITE_WORD(p, (31 << 8) | 0);	/* /dev/mtdblock0 */
33753018216SPaolo Bonzini     /* video_num_cols */
33853018216SPaolo Bonzini     WRITE_WORD(p, 0);
33953018216SPaolo Bonzini     /* video_num_rows */
34053018216SPaolo Bonzini     WRITE_WORD(p, 0);
34153018216SPaolo Bonzini     /* video_x */
34253018216SPaolo Bonzini     WRITE_WORD(p, 0);
34353018216SPaolo Bonzini     /* video_y */
34453018216SPaolo Bonzini     WRITE_WORD(p, 0);
34553018216SPaolo Bonzini     /* memc_control_reg */
34653018216SPaolo Bonzini     WRITE_WORD(p, 0);
34753018216SPaolo Bonzini     /* unsigned char sounddefault */
34853018216SPaolo Bonzini     /* unsigned char adfsdrives */
34953018216SPaolo Bonzini     /* unsigned char bytes_per_char_h */
35053018216SPaolo Bonzini     /* unsigned char bytes_per_char_v */
35153018216SPaolo Bonzini     WRITE_WORD(p, 0);
35253018216SPaolo Bonzini     /* pages_in_bank[4] */
35353018216SPaolo Bonzini     WRITE_WORD(p, 0);
35453018216SPaolo Bonzini     WRITE_WORD(p, 0);
35553018216SPaolo Bonzini     WRITE_WORD(p, 0);
35653018216SPaolo Bonzini     WRITE_WORD(p, 0);
35753018216SPaolo Bonzini     /* pages_in_vram */
35853018216SPaolo Bonzini     WRITE_WORD(p, 0);
35953018216SPaolo Bonzini     /* initrd_start */
36053018216SPaolo Bonzini     if (initrd_size) {
36153018216SPaolo Bonzini         WRITE_WORD(p, info->initrd_start);
36253018216SPaolo Bonzini     } else {
36353018216SPaolo Bonzini         WRITE_WORD(p, 0);
36453018216SPaolo Bonzini     }
36553018216SPaolo Bonzini     /* initrd_size */
36653018216SPaolo Bonzini     WRITE_WORD(p, initrd_size);
36753018216SPaolo Bonzini     /* rd_start */
36853018216SPaolo Bonzini     WRITE_WORD(p, 0);
36953018216SPaolo Bonzini     /* system_rev */
37053018216SPaolo Bonzini     WRITE_WORD(p, 0);
37153018216SPaolo Bonzini     /* system_serial_low */
37253018216SPaolo Bonzini     WRITE_WORD(p, 0);
37353018216SPaolo Bonzini     /* system_serial_high */
37453018216SPaolo Bonzini     WRITE_WORD(p, 0);
37553018216SPaolo Bonzini     /* mem_fclk_21285 */
37653018216SPaolo Bonzini     WRITE_WORD(p, 0);
37753018216SPaolo Bonzini     /* zero unused fields */
37853018216SPaolo Bonzini     while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
37953018216SPaolo Bonzini         WRITE_WORD(p, 0);
38053018216SPaolo Bonzini     }
38153018216SPaolo Bonzini     s = info->kernel_cmdline;
38253018216SPaolo Bonzini     if (s) {
383e1fe50dcSStefan Weil         cpu_physical_memory_write(p, s, strlen(s) + 1);
38453018216SPaolo Bonzini     } else {
38553018216SPaolo Bonzini         WRITE_WORD(p, 0);
38653018216SPaolo Bonzini     }
38753018216SPaolo Bonzini }
38853018216SPaolo Bonzini 
389fee8ea12SArd Biesheuvel /**
390fee8ea12SArd Biesheuvel  * load_dtb() - load a device tree binary image into memory
391fee8ea12SArd Biesheuvel  * @addr:       the address to load the image at
392fee8ea12SArd Biesheuvel  * @binfo:      struct describing the boot environment
393fee8ea12SArd Biesheuvel  * @addr_limit: upper limit of the available memory area at @addr
394fee8ea12SArd Biesheuvel  *
395fee8ea12SArd Biesheuvel  * Load a device tree supplied by the machine or by the user  with the
396fee8ea12SArd Biesheuvel  * '-dtb' command line option, and put it at offset @addr in target
397fee8ea12SArd Biesheuvel  * memory.
398fee8ea12SArd Biesheuvel  *
399fee8ea12SArd Biesheuvel  * If @addr_limit contains a meaningful value (i.e., it is strictly greater
400fee8ea12SArd Biesheuvel  * than @addr), the device tree is only loaded if its size does not exceed
401fee8ea12SArd Biesheuvel  * the limit.
402fee8ea12SArd Biesheuvel  *
403fee8ea12SArd Biesheuvel  * Returns: the size of the device tree image on success,
404fee8ea12SArd Biesheuvel  *          0 if the image size exceeds the limit,
405fee8ea12SArd Biesheuvel  *          -1 on errors.
406a554ecb4Szhanghailiang  *
407a554ecb4Szhanghailiang  * Note: Must not be called unless have_dtb(binfo) is true.
408fee8ea12SArd Biesheuvel  */
409fee8ea12SArd Biesheuvel static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
410fee8ea12SArd Biesheuvel                     hwaddr addr_limit)
41153018216SPaolo Bonzini {
41253018216SPaolo Bonzini     void *fdt = NULL;
41353018216SPaolo Bonzini     int size, rc;
41470976c41SPeter Maydell     uint32_t acells, scells;
4159695200aSShannon Zhao     char *nodename;
4169695200aSShannon Zhao     unsigned int i;
4179695200aSShannon Zhao     hwaddr mem_base, mem_len;
41853018216SPaolo Bonzini 
4190fb79851SJohn Rigby     if (binfo->dtb_filename) {
4200fb79851SJohn Rigby         char *filename;
42153018216SPaolo Bonzini         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
42253018216SPaolo Bonzini         if (!filename) {
42353018216SPaolo Bonzini             fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
424c23045deSPeter Maydell             goto fail;
42553018216SPaolo Bonzini         }
42653018216SPaolo Bonzini 
42753018216SPaolo Bonzini         fdt = load_device_tree(filename, &size);
42853018216SPaolo Bonzini         if (!fdt) {
42953018216SPaolo Bonzini             fprintf(stderr, "Couldn't open dtb file %s\n", filename);
43053018216SPaolo Bonzini             g_free(filename);
431c23045deSPeter Maydell             goto fail;
43253018216SPaolo Bonzini         }
43353018216SPaolo Bonzini         g_free(filename);
434a554ecb4Szhanghailiang     } else {
4350fb79851SJohn Rigby         fdt = binfo->get_dtb(binfo, &size);
4360fb79851SJohn Rigby         if (!fdt) {
4370fb79851SJohn Rigby             fprintf(stderr, "Board was unable to create a dtb blob\n");
4380fb79851SJohn Rigby             goto fail;
4390fb79851SJohn Rigby         }
4400fb79851SJohn Rigby     }
44153018216SPaolo Bonzini 
442fee8ea12SArd Biesheuvel     if (addr_limit > addr && size > (addr_limit - addr)) {
443fee8ea12SArd Biesheuvel         /* Installing the device tree blob at addr would exceed addr_limit.
444fee8ea12SArd Biesheuvel          * Whether this constitutes failure is up to the caller to decide,
445fee8ea12SArd Biesheuvel          * so just return 0 as size, i.e., no error.
446fee8ea12SArd Biesheuvel          */
447fee8ea12SArd Biesheuvel         g_free(fdt);
448fee8ea12SArd Biesheuvel         return 0;
449fee8ea12SArd Biesheuvel     }
450fee8ea12SArd Biesheuvel 
45158e71097SEric Auger     acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells",
45258e71097SEric Auger                                    NULL, &error_fatal);
45358e71097SEric Auger     scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells",
45458e71097SEric Auger                                    NULL, &error_fatal);
45553018216SPaolo Bonzini     if (acells == 0 || scells == 0) {
45653018216SPaolo Bonzini         fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
457c23045deSPeter Maydell         goto fail;
45853018216SPaolo Bonzini     }
45953018216SPaolo Bonzini 
46070976c41SPeter Maydell     if (scells < 2 && binfo->ram_size >= (1ULL << 32)) {
46170976c41SPeter Maydell         /* This is user error so deserves a friendlier error message
46270976c41SPeter Maydell          * than the failure of setprop_sized_cells would provide
46370976c41SPeter Maydell          */
46453018216SPaolo Bonzini         fprintf(stderr, "qemu: dtb file not compatible with "
46553018216SPaolo Bonzini                 "RAM size > 4GB\n");
466c23045deSPeter Maydell         goto fail;
46753018216SPaolo Bonzini     }
46853018216SPaolo Bonzini 
4699695200aSShannon Zhao     if (nb_numa_nodes > 0) {
4709695200aSShannon Zhao         /*
4719695200aSShannon Zhao          * Turn the /memory node created before into a NOP node, then create
4729695200aSShannon Zhao          * /memory@addr nodes for all numa nodes respectively.
4739695200aSShannon Zhao          */
4749695200aSShannon Zhao         qemu_fdt_nop_node(fdt, "/memory");
4759695200aSShannon Zhao         mem_base = binfo->loader_start;
4769695200aSShannon Zhao         for (i = 0; i < nb_numa_nodes; i++) {
4779695200aSShannon Zhao             mem_len = numa_info[i].node_mem;
4789695200aSShannon Zhao             nodename = g_strdup_printf("/memory@%" PRIx64, mem_base);
4799695200aSShannon Zhao             qemu_fdt_add_subnode(fdt, nodename);
4809695200aSShannon Zhao             qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
4819695200aSShannon Zhao             rc = qemu_fdt_setprop_sized_cells(fdt, nodename, "reg",
4829695200aSShannon Zhao                                               acells, mem_base,
4839695200aSShannon Zhao                                               scells, mem_len);
4849695200aSShannon Zhao             if (rc < 0) {
4859695200aSShannon Zhao                 fprintf(stderr, "couldn't set %s/reg for node %d\n", nodename,
4869695200aSShannon Zhao                         i);
4879695200aSShannon Zhao                 goto fail;
4889695200aSShannon Zhao             }
4899695200aSShannon Zhao 
4909695200aSShannon Zhao             qemu_fdt_setprop_cell(fdt, nodename, "numa-node-id", i);
4919695200aSShannon Zhao             mem_base += mem_len;
4929695200aSShannon Zhao             g_free(nodename);
4939695200aSShannon Zhao         }
4949695200aSShannon Zhao     } else {
495b77257d7SGuenter Roeck         Error *err = NULL;
496b77257d7SGuenter Roeck 
497b77257d7SGuenter Roeck         rc = fdt_path_offset(fdt, "/memory");
498b77257d7SGuenter Roeck         if (rc < 0) {
499b77257d7SGuenter Roeck             qemu_fdt_add_subnode(fdt, "/memory");
500b77257d7SGuenter Roeck         }
501b77257d7SGuenter Roeck 
502b77257d7SGuenter Roeck         if (!qemu_fdt_getprop(fdt, "/memory", "device_type", NULL, &err)) {
503b77257d7SGuenter Roeck             qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory");
504b77257d7SGuenter Roeck         }
505b77257d7SGuenter Roeck 
5065a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_sized_cells(fdt, "/memory", "reg",
50770976c41SPeter Maydell                                           acells, binfo->loader_start,
50870976c41SPeter Maydell                                           scells, binfo->ram_size);
50953018216SPaolo Bonzini         if (rc < 0) {
51053018216SPaolo Bonzini             fprintf(stderr, "couldn't set /memory/reg\n");
511c23045deSPeter Maydell             goto fail;
51253018216SPaolo Bonzini         }
5139695200aSShannon Zhao     }
51453018216SPaolo Bonzini 
515b77257d7SGuenter Roeck     rc = fdt_path_offset(fdt, "/chosen");
516b77257d7SGuenter Roeck     if (rc < 0) {
517b77257d7SGuenter Roeck         qemu_fdt_add_subnode(fdt, "/chosen");
518b77257d7SGuenter Roeck     }
519b77257d7SGuenter Roeck 
52053018216SPaolo Bonzini     if (binfo->kernel_cmdline && *binfo->kernel_cmdline) {
5215a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
52253018216SPaolo Bonzini                                      binfo->kernel_cmdline);
52353018216SPaolo Bonzini         if (rc < 0) {
52453018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/bootargs\n");
525c23045deSPeter Maydell             goto fail;
52653018216SPaolo Bonzini         }
52753018216SPaolo Bonzini     }
52853018216SPaolo Bonzini 
52953018216SPaolo Bonzini     if (binfo->initrd_size) {
5305a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
53153018216SPaolo Bonzini                                    binfo->initrd_start);
53253018216SPaolo Bonzini         if (rc < 0) {
53353018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
534c23045deSPeter Maydell             goto fail;
53553018216SPaolo Bonzini         }
53653018216SPaolo Bonzini 
5375a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
53853018216SPaolo Bonzini                                    binfo->initrd_start + binfo->initrd_size);
53953018216SPaolo Bonzini         if (rc < 0) {
54053018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
541c23045deSPeter Maydell             goto fail;
54253018216SPaolo Bonzini         }
54353018216SPaolo Bonzini     }
5443b1cceb8SPeter Maydell 
5453b1cceb8SPeter Maydell     if (binfo->modify_dtb) {
5463b1cceb8SPeter Maydell         binfo->modify_dtb(binfo, fdt);
5473b1cceb8SPeter Maydell     }
5483b1cceb8SPeter Maydell 
5495a4348d1SPeter Crosthwaite     qemu_fdt_dumpdtb(fdt, size);
55053018216SPaolo Bonzini 
5514c4bf654SArd Biesheuvel     /* Put the DTB into the memory map as a ROM image: this will ensure
5524c4bf654SArd Biesheuvel      * the DTB is copied again upon reset, even if addr points into RAM.
5534c4bf654SArd Biesheuvel      */
5544c4bf654SArd Biesheuvel     rom_add_blob_fixed("dtb", fdt, size, addr);
55553018216SPaolo Bonzini 
556c23045deSPeter Maydell     g_free(fdt);
557c23045deSPeter Maydell 
558fee8ea12SArd Biesheuvel     return size;
559c23045deSPeter Maydell 
560c23045deSPeter Maydell fail:
561c23045deSPeter Maydell     g_free(fdt);
562c23045deSPeter Maydell     return -1;
56353018216SPaolo Bonzini }
56453018216SPaolo Bonzini 
56553018216SPaolo Bonzini static void do_cpu_reset(void *opaque)
56653018216SPaolo Bonzini {
56753018216SPaolo Bonzini     ARMCPU *cpu = opaque;
5684df81c6eSPeter Crosthwaite     CPUState *cs = CPU(cpu);
56953018216SPaolo Bonzini     CPUARMState *env = &cpu->env;
57053018216SPaolo Bonzini     const struct arm_boot_info *info = env->boot_info;
57153018216SPaolo Bonzini 
5724df81c6eSPeter Crosthwaite     cpu_reset(cs);
57353018216SPaolo Bonzini     if (info) {
57453018216SPaolo Bonzini         if (!info->is_linux) {
5759776f636SPeter Crosthwaite             int i;
57653018216SPaolo Bonzini             /* Jump to the entry point.  */
5774df81c6eSPeter Crosthwaite             uint64_t entry = info->entry;
5784df81c6eSPeter Crosthwaite 
5799776f636SPeter Crosthwaite             switch (info->endianness) {
5809776f636SPeter Crosthwaite             case ARM_ENDIANNESS_LE:
5819776f636SPeter Crosthwaite                 env->cp15.sctlr_el[1] &= ~SCTLR_E0E;
5829776f636SPeter Crosthwaite                 for (i = 1; i < 4; ++i) {
5839776f636SPeter Crosthwaite                     env->cp15.sctlr_el[i] &= ~SCTLR_EE;
5849776f636SPeter Crosthwaite                 }
5859776f636SPeter Crosthwaite                 env->uncached_cpsr &= ~CPSR_E;
5869776f636SPeter Crosthwaite                 break;
5879776f636SPeter Crosthwaite             case ARM_ENDIANNESS_BE8:
5889776f636SPeter Crosthwaite                 env->cp15.sctlr_el[1] |= SCTLR_E0E;
5899776f636SPeter Crosthwaite                 for (i = 1; i < 4; ++i) {
5909776f636SPeter Crosthwaite                     env->cp15.sctlr_el[i] |= SCTLR_EE;
5919776f636SPeter Crosthwaite                 }
5929776f636SPeter Crosthwaite                 env->uncached_cpsr |= CPSR_E;
5939776f636SPeter Crosthwaite                 break;
5949776f636SPeter Crosthwaite             case ARM_ENDIANNESS_BE32:
5959776f636SPeter Crosthwaite                 env->cp15.sctlr_el[1] |= SCTLR_B;
5969776f636SPeter Crosthwaite                 break;
5979776f636SPeter Crosthwaite             case ARM_ENDIANNESS_UNKNOWN:
5989776f636SPeter Crosthwaite                 break; /* Board's decision */
5999776f636SPeter Crosthwaite             default:
6009776f636SPeter Crosthwaite                 g_assert_not_reached();
6019776f636SPeter Crosthwaite             }
6029776f636SPeter Crosthwaite 
6034df81c6eSPeter Crosthwaite             if (!env->aarch64) {
60453018216SPaolo Bonzini                 env->thumb = info->entry & 1;
6054df81c6eSPeter Crosthwaite                 entry &= 0xfffffffe;
606a9047ec3SPeter Maydell             }
6074df81c6eSPeter Crosthwaite             cpu_set_pc(cs, entry);
60853018216SPaolo Bonzini         } else {
609c8e829b7SGreg Bellows             /* If we are booting Linux then we need to check whether we are
610c8e829b7SGreg Bellows              * booting into secure or non-secure state and adjust the state
611c8e829b7SGreg Bellows              * accordingly.  Out of reset, ARM is defined to be in secure state
612c8e829b7SGreg Bellows              * (SCR.NS = 0), we change that here if non-secure boot has been
613c8e829b7SGreg Bellows              * requested.
614c8e829b7SGreg Bellows              */
6155097227cSGreg Bellows             if (arm_feature(env, ARM_FEATURE_EL3)) {
6165097227cSGreg Bellows                 /* AArch64 is defined to come out of reset into EL3 if enabled.
6175097227cSGreg Bellows                  * If we are booting Linux then we need to adjust our EL as
6185097227cSGreg Bellows                  * Linux expects us to be in EL2 or EL1.  AArch32 resets into
6195097227cSGreg Bellows                  * SVC, which Linux expects, so no privilege/exception level to
6205097227cSGreg Bellows                  * adjust.
6215097227cSGreg Bellows                  */
6225097227cSGreg Bellows                 if (env->aarch64) {
62348d21a57SEdgar E. Iglesias                     env->cp15.scr_el3 |= SCR_RW;
6245097227cSGreg Bellows                     if (arm_feature(env, ARM_FEATURE_EL2)) {
62548d21a57SEdgar E. Iglesias                         env->cp15.hcr_el2 |= HCR_RW;
6265097227cSGreg Bellows                         env->pstate = PSTATE_MODE_EL2h;
6275097227cSGreg Bellows                     } else {
6285097227cSGreg Bellows                         env->pstate = PSTATE_MODE_EL1h;
6295097227cSGreg Bellows                     }
6305097227cSGreg Bellows                 }
6315097227cSGreg Bellows 
6325097227cSGreg Bellows                 /* Set to non-secure if not a secure boot */
633baf6b681SPeter Crosthwaite                 if (!info->secure_boot &&
634baf6b681SPeter Crosthwaite                     (cs != first_cpu || !info->secure_board_setup)) {
6355097227cSGreg Bellows                     /* Linux expects non-secure state */
636c8e829b7SGreg Bellows                     env->cp15.scr_el3 |= SCR_NS;
637c8e829b7SGreg Bellows                 }
6385097227cSGreg Bellows             }
639c8e829b7SGreg Bellows 
6404df81c6eSPeter Crosthwaite             if (cs == first_cpu) {
6414df81c6eSPeter Crosthwaite                 cpu_set_pc(cs, info->loader_start);
6424d9ebf75SMian M. Hamayun 
64383bfffecSPeter Maydell                 if (!have_dtb(info)) {
64453018216SPaolo Bonzini                     if (old_param) {
64553018216SPaolo Bonzini                         set_kernel_args_old(info);
64653018216SPaolo Bonzini                     } else {
64753018216SPaolo Bonzini                         set_kernel_args(info);
64853018216SPaolo Bonzini                     }
64953018216SPaolo Bonzini                 }
65053018216SPaolo Bonzini             } else {
65153018216SPaolo Bonzini                 info->secondary_cpu_reset_hook(cpu, info);
65253018216SPaolo Bonzini             }
65353018216SPaolo Bonzini         }
65453018216SPaolo Bonzini     }
65553018216SPaolo Bonzini }
65653018216SPaolo Bonzini 
65707abe45cSLaszlo Ersek /**
65807abe45cSLaszlo Ersek  * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
65907abe45cSLaszlo Ersek  *                          by key.
66007abe45cSLaszlo Ersek  * @fw_cfg:         The firmware config instance to store the data in.
66107abe45cSLaszlo Ersek  * @size_key:       The firmware config key to store the size of the loaded
66207abe45cSLaszlo Ersek  *                  data under, with fw_cfg_add_i32().
66307abe45cSLaszlo Ersek  * @data_key:       The firmware config key to store the loaded data under,
66407abe45cSLaszlo Ersek  *                  with fw_cfg_add_bytes().
66507abe45cSLaszlo Ersek  * @image_name:     The name of the image file to load. If it is NULL, the
66607abe45cSLaszlo Ersek  *                  function returns without doing anything.
66707abe45cSLaszlo Ersek  * @try_decompress: Whether the image should be decompressed (gunzipped) before
66807abe45cSLaszlo Ersek  *                  adding it to fw_cfg. If decompression fails, the image is
66907abe45cSLaszlo Ersek  *                  loaded as-is.
67007abe45cSLaszlo Ersek  *
67107abe45cSLaszlo Ersek  * In case of failure, the function prints an error message to stderr and the
67207abe45cSLaszlo Ersek  * process exits with status 1.
67307abe45cSLaszlo Ersek  */
67407abe45cSLaszlo Ersek static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
67507abe45cSLaszlo Ersek                                  uint16_t data_key, const char *image_name,
67607abe45cSLaszlo Ersek                                  bool try_decompress)
67707abe45cSLaszlo Ersek {
67807abe45cSLaszlo Ersek     size_t size = -1;
67907abe45cSLaszlo Ersek     uint8_t *data;
68007abe45cSLaszlo Ersek 
68107abe45cSLaszlo Ersek     if (image_name == NULL) {
68207abe45cSLaszlo Ersek         return;
68307abe45cSLaszlo Ersek     }
68407abe45cSLaszlo Ersek 
68507abe45cSLaszlo Ersek     if (try_decompress) {
68607abe45cSLaszlo Ersek         size = load_image_gzipped_buffer(image_name,
68707abe45cSLaszlo Ersek                                          LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
68807abe45cSLaszlo Ersek     }
68907abe45cSLaszlo Ersek 
69007abe45cSLaszlo Ersek     if (size == (size_t)-1) {
69107abe45cSLaszlo Ersek         gchar *contents;
69207abe45cSLaszlo Ersek         gsize length;
69307abe45cSLaszlo Ersek 
69407abe45cSLaszlo Ersek         if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
695c0dbca36SAlistair Francis             error_report("failed to load \"%s\"", image_name);
69607abe45cSLaszlo Ersek             exit(1);
69707abe45cSLaszlo Ersek         }
69807abe45cSLaszlo Ersek         size = length;
69907abe45cSLaszlo Ersek         data = (uint8_t *)contents;
70007abe45cSLaszlo Ersek     }
70107abe45cSLaszlo Ersek 
70207abe45cSLaszlo Ersek     fw_cfg_add_i32(fw_cfg, size_key, size);
70307abe45cSLaszlo Ersek     fw_cfg_add_bytes(fw_cfg, data_key, data, size);
70407abe45cSLaszlo Ersek }
70507abe45cSLaszlo Ersek 
706d8b1ae42SPeter Maydell static int do_arm_linux_init(Object *obj, void *opaque)
707d8b1ae42SPeter Maydell {
708d8b1ae42SPeter Maydell     if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
709d8b1ae42SPeter Maydell         ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj);
710d8b1ae42SPeter Maydell         ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj);
711d8b1ae42SPeter Maydell         struct arm_boot_info *info = opaque;
712d8b1ae42SPeter Maydell 
713d8b1ae42SPeter Maydell         if (albifc->arm_linux_init) {
714d8b1ae42SPeter Maydell             albifc->arm_linux_init(albif, info->secure_boot);
715d8b1ae42SPeter Maydell         }
716d8b1ae42SPeter Maydell     }
717d8b1ae42SPeter Maydell     return 0;
718d8b1ae42SPeter Maydell }
719d8b1ae42SPeter Maydell 
7209776f636SPeter Crosthwaite static uint64_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,
7219776f636SPeter Crosthwaite                              uint64_t *lowaddr, uint64_t *highaddr,
7229776f636SPeter Crosthwaite                              int elf_machine)
7239776f636SPeter Crosthwaite {
7249776f636SPeter Crosthwaite     bool elf_is64;
7259776f636SPeter Crosthwaite     union {
7269776f636SPeter Crosthwaite         Elf32_Ehdr h32;
7279776f636SPeter Crosthwaite         Elf64_Ehdr h64;
7289776f636SPeter Crosthwaite     } elf_header;
7299776f636SPeter Crosthwaite     int data_swab = 0;
7309776f636SPeter Crosthwaite     bool big_endian;
7319776f636SPeter Crosthwaite     uint64_t ret = -1;
7329776f636SPeter Crosthwaite     Error *err = NULL;
7339776f636SPeter Crosthwaite 
7349776f636SPeter Crosthwaite 
7359776f636SPeter Crosthwaite     load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err);
7369776f636SPeter Crosthwaite     if (err) {
7379776f636SPeter Crosthwaite         return ret;
7389776f636SPeter Crosthwaite     }
7399776f636SPeter Crosthwaite 
7409776f636SPeter Crosthwaite     if (elf_is64) {
7419776f636SPeter Crosthwaite         big_endian = elf_header.h64.e_ident[EI_DATA] == ELFDATA2MSB;
7429776f636SPeter Crosthwaite         info->endianness = big_endian ? ARM_ENDIANNESS_BE8
7439776f636SPeter Crosthwaite                                       : ARM_ENDIANNESS_LE;
7449776f636SPeter Crosthwaite     } else {
7459776f636SPeter Crosthwaite         big_endian = elf_header.h32.e_ident[EI_DATA] == ELFDATA2MSB;
7469776f636SPeter Crosthwaite         if (big_endian) {
7479776f636SPeter Crosthwaite             if (bswap32(elf_header.h32.e_flags) & EF_ARM_BE8) {
7489776f636SPeter Crosthwaite                 info->endianness = ARM_ENDIANNESS_BE8;
7499776f636SPeter Crosthwaite             } else {
7509776f636SPeter Crosthwaite                 info->endianness = ARM_ENDIANNESS_BE32;
7519776f636SPeter Crosthwaite                 /* In BE32, the CPU has a different view of the per-byte
7529776f636SPeter Crosthwaite                  * address map than the rest of the system. BE32 ELF files
7539776f636SPeter Crosthwaite                  * are organised such that they can be programmed through
7549776f636SPeter Crosthwaite                  * the CPU's per-word byte-reversed view of the world. QEMU
7559776f636SPeter Crosthwaite                  * however loads ELF files independently of the CPU. So
7569776f636SPeter Crosthwaite                  * tell the ELF loader to byte reverse the data for us.
7579776f636SPeter Crosthwaite                  */
7589776f636SPeter Crosthwaite                 data_swab = 2;
7599776f636SPeter Crosthwaite             }
7609776f636SPeter Crosthwaite         } else {
7619776f636SPeter Crosthwaite             info->endianness = ARM_ENDIANNESS_LE;
7629776f636SPeter Crosthwaite         }
7639776f636SPeter Crosthwaite     }
7649776f636SPeter Crosthwaite 
7659776f636SPeter Crosthwaite     ret = load_elf(info->kernel_filename, NULL, NULL,
7669776f636SPeter Crosthwaite                    pentry, lowaddr, highaddr, big_endian, elf_machine,
7679776f636SPeter Crosthwaite                    1, data_swab);
7689776f636SPeter Crosthwaite     if (ret <= 0) {
7699776f636SPeter Crosthwaite         /* The header loaded but the image didn't */
7709776f636SPeter Crosthwaite         exit(1);
7719776f636SPeter Crosthwaite     }
7729776f636SPeter Crosthwaite 
7739776f636SPeter Crosthwaite     return ret;
7749776f636SPeter Crosthwaite }
7759776f636SPeter Crosthwaite 
77668115ed5SArd Biesheuvel static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base,
77768115ed5SArd Biesheuvel                                    hwaddr *entry)
77868115ed5SArd Biesheuvel {
77968115ed5SArd Biesheuvel     hwaddr kernel_load_offset = KERNEL64_LOAD_ADDR;
78068115ed5SArd Biesheuvel     uint8_t *buffer;
78168115ed5SArd Biesheuvel     int size;
78268115ed5SArd Biesheuvel 
78368115ed5SArd Biesheuvel     /* On aarch64, it's the bootloader's job to uncompress the kernel. */
78468115ed5SArd Biesheuvel     size = load_image_gzipped_buffer(filename, LOAD_IMAGE_MAX_GUNZIP_BYTES,
78568115ed5SArd Biesheuvel                                      &buffer);
78668115ed5SArd Biesheuvel 
78768115ed5SArd Biesheuvel     if (size < 0) {
78868115ed5SArd Biesheuvel         gsize len;
78968115ed5SArd Biesheuvel 
79068115ed5SArd Biesheuvel         /* Load as raw file otherwise */
79168115ed5SArd Biesheuvel         if (!g_file_get_contents(filename, (char **)&buffer, &len, NULL)) {
79268115ed5SArd Biesheuvel             return -1;
79368115ed5SArd Biesheuvel         }
79468115ed5SArd Biesheuvel         size = len;
79568115ed5SArd Biesheuvel     }
79668115ed5SArd Biesheuvel 
79768115ed5SArd Biesheuvel     /* check the arm64 magic header value -- very old kernels may not have it */
79868115ed5SArd Biesheuvel     if (memcmp(buffer + ARM64_MAGIC_OFFSET, "ARM\x64", 4) == 0) {
79968115ed5SArd Biesheuvel         uint64_t hdrvals[2];
80068115ed5SArd Biesheuvel 
80168115ed5SArd Biesheuvel         /* The arm64 Image header has text_offset and image_size fields at 8 and
80268115ed5SArd Biesheuvel          * 16 bytes into the Image header, respectively. The text_offset field
80368115ed5SArd Biesheuvel          * is only valid if the image_size is non-zero.
80468115ed5SArd Biesheuvel          */
80568115ed5SArd Biesheuvel         memcpy(&hdrvals, buffer + ARM64_TEXT_OFFSET_OFFSET, sizeof(hdrvals));
80668115ed5SArd Biesheuvel         if (hdrvals[1] != 0) {
80768115ed5SArd Biesheuvel             kernel_load_offset = le64_to_cpu(hdrvals[0]);
80868115ed5SArd Biesheuvel         }
80968115ed5SArd Biesheuvel     }
81068115ed5SArd Biesheuvel 
81168115ed5SArd Biesheuvel     *entry = mem_base + kernel_load_offset;
81268115ed5SArd Biesheuvel     rom_add_blob_fixed(filename, buffer, size, *entry);
81368115ed5SArd Biesheuvel 
81468115ed5SArd Biesheuvel     g_free(buffer);
81568115ed5SArd Biesheuvel 
81668115ed5SArd Biesheuvel     return size;
81768115ed5SArd Biesheuvel }
81868115ed5SArd Biesheuvel 
819ac9d32e3SEric Auger static void arm_load_kernel_notify(Notifier *notifier, void *data)
82053018216SPaolo Bonzini {
821c6faa758SArd Biesheuvel     CPUState *cs;
82253018216SPaolo Bonzini     int kernel_size;
82353018216SPaolo Bonzini     int initrd_size;
82453018216SPaolo Bonzini     int is_linux = 0;
82592df8450SArd Biesheuvel     uint64_t elf_entry, elf_low_addr, elf_high_addr;
826da0af40dSPeter Maydell     int elf_machine;
82768115ed5SArd Biesheuvel     hwaddr entry;
8284d9ebf75SMian M. Hamayun     static const ARMInsnFixup *primary_loader;
829ac9d32e3SEric Auger     ArmLoadKernelNotifier *n = DO_UPCAST(ArmLoadKernelNotifier,
830ac9d32e3SEric Auger                                          notifier, notifier);
831ac9d32e3SEric Auger     ARMCPU *cpu = n->cpu;
832ac9d32e3SEric Auger     struct arm_boot_info *info =
833ac9d32e3SEric Auger         container_of(n, struct arm_boot_info, load_kernel_notifier);
83453018216SPaolo Bonzini 
835baf6b681SPeter Crosthwaite     /* The board code is not supposed to set secure_board_setup unless
836baf6b681SPeter Crosthwaite      * running its code in secure mode is actually possible, and KVM
837baf6b681SPeter Crosthwaite      * doesn't support secure.
838baf6b681SPeter Crosthwaite      */
839baf6b681SPeter Crosthwaite     assert(!(info->secure_board_setup && kvm_enabled()));
840baf6b681SPeter Crosthwaite 
8414c8afda7SMichael Olbrich     info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb");
8424c8afda7SMichael Olbrich 
84353018216SPaolo Bonzini     /* Load the kernel.  */
84407abe45cSLaszlo Ersek     if (!info->kernel_filename || info->firmware_loaded) {
84569e7f76fSArd Biesheuvel 
84669e7f76fSArd Biesheuvel         if (have_dtb(info)) {
84707abe45cSLaszlo Ersek             /* If we have a device tree blob, but no kernel to supply it to (or
84807abe45cSLaszlo Ersek              * the kernel is supposed to be loaded by the bootloader), copy the
84907abe45cSLaszlo Ersek              * DTB to the base of RAM for the bootloader to pick up.
85069e7f76fSArd Biesheuvel              */
85169e7f76fSArd Biesheuvel             if (load_dtb(info->loader_start, info, 0) < 0) {
85269e7f76fSArd Biesheuvel                 exit(1);
85369e7f76fSArd Biesheuvel             }
85469e7f76fSArd Biesheuvel         }
85569e7f76fSArd Biesheuvel 
85607abe45cSLaszlo Ersek         if (info->kernel_filename) {
85707abe45cSLaszlo Ersek             FWCfgState *fw_cfg;
85807abe45cSLaszlo Ersek             bool try_decompressing_kernel;
85907abe45cSLaszlo Ersek 
86007abe45cSLaszlo Ersek             fw_cfg = fw_cfg_find();
86107abe45cSLaszlo Ersek             try_decompressing_kernel = arm_feature(&cpu->env,
86207abe45cSLaszlo Ersek                                                    ARM_FEATURE_AARCH64);
86307abe45cSLaszlo Ersek 
86407abe45cSLaszlo Ersek             /* Expose the kernel, the command line, and the initrd in fw_cfg.
86507abe45cSLaszlo Ersek              * We don't process them here at all, it's all left to the
86607abe45cSLaszlo Ersek              * firmware.
86707abe45cSLaszlo Ersek              */
86807abe45cSLaszlo Ersek             load_image_to_fw_cfg(fw_cfg,
86907abe45cSLaszlo Ersek                                  FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
87007abe45cSLaszlo Ersek                                  info->kernel_filename,
87107abe45cSLaszlo Ersek                                  try_decompressing_kernel);
87207abe45cSLaszlo Ersek             load_image_to_fw_cfg(fw_cfg,
87307abe45cSLaszlo Ersek                                  FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
87407abe45cSLaszlo Ersek                                  info->initrd_filename, false);
87507abe45cSLaszlo Ersek 
87607abe45cSLaszlo Ersek             if (info->kernel_cmdline) {
87707abe45cSLaszlo Ersek                 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
87807abe45cSLaszlo Ersek                                strlen(info->kernel_cmdline) + 1);
87907abe45cSLaszlo Ersek                 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
88007abe45cSLaszlo Ersek                                   info->kernel_cmdline);
88107abe45cSLaszlo Ersek             }
88207abe45cSLaszlo Ersek         }
88307abe45cSLaszlo Ersek 
88407abe45cSLaszlo Ersek         /* We will start from address 0 (typically a boot ROM image) in the
88507abe45cSLaszlo Ersek          * same way as hardware.
8869546dbabSPeter Maydell          */
8879546dbabSPeter Maydell         return;
88853018216SPaolo Bonzini     }
88953018216SPaolo Bonzini 
8904d9ebf75SMian M. Hamayun     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
8914d9ebf75SMian M. Hamayun         primary_loader = bootloader_aarch64;
892da0af40dSPeter Maydell         elf_machine = EM_AARCH64;
8934d9ebf75SMian M. Hamayun     } else {
8944d9ebf75SMian M. Hamayun         primary_loader = bootloader;
89510b8ec73SPeter Crosthwaite         if (!info->write_board_setup) {
89610b8ec73SPeter Crosthwaite             primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET;
89710b8ec73SPeter Crosthwaite         }
898da0af40dSPeter Maydell         elf_machine = EM_ARM;
8994d9ebf75SMian M. Hamayun     }
9004d9ebf75SMian M. Hamayun 
90153018216SPaolo Bonzini     if (!info->secondary_cpu_reset_hook) {
90253018216SPaolo Bonzini         info->secondary_cpu_reset_hook = default_reset_secondary;
90353018216SPaolo Bonzini     }
90453018216SPaolo Bonzini     if (!info->write_secondary_boot) {
90553018216SPaolo Bonzini         info->write_secondary_boot = default_write_secondary;
90653018216SPaolo Bonzini     }
90753018216SPaolo Bonzini 
90853018216SPaolo Bonzini     if (info->nb_cpus == 0)
90953018216SPaolo Bonzini         info->nb_cpus = 1;
91053018216SPaolo Bonzini 
91153018216SPaolo Bonzini     /* We want to put the initrd far enough into RAM that when the
91253018216SPaolo Bonzini      * kernel is uncompressed it will not clobber the initrd. However
91353018216SPaolo Bonzini      * on boards without much RAM we must ensure that we still leave
91453018216SPaolo Bonzini      * enough room for a decent sized initrd, and on boards with large
91553018216SPaolo Bonzini      * amounts of RAM we must avoid the initrd being so far up in RAM
91653018216SPaolo Bonzini      * that it is outside lowmem and inaccessible to the kernel.
91753018216SPaolo Bonzini      * So for boards with less  than 256MB of RAM we put the initrd
91853018216SPaolo Bonzini      * halfway into RAM, and for boards with 256MB of RAM or more we put
91953018216SPaolo Bonzini      * the initrd at 128MB.
92053018216SPaolo Bonzini      */
92153018216SPaolo Bonzini     info->initrd_start = info->loader_start +
92253018216SPaolo Bonzini         MIN(info->ram_size / 2, 128 * 1024 * 1024);
92353018216SPaolo Bonzini 
92453018216SPaolo Bonzini     /* Assume that raw images are linux kernels, and ELF images are not.  */
9259776f636SPeter Crosthwaite     kernel_size = arm_load_elf(info, &elf_entry, &elf_low_addr,
9269776f636SPeter Crosthwaite                                &elf_high_addr, elf_machine);
92792df8450SArd Biesheuvel     if (kernel_size > 0 && have_dtb(info)) {
92892df8450SArd Biesheuvel         /* If there is still some room left at the base of RAM, try and put
92992df8450SArd Biesheuvel          * the DTB there like we do for images loaded with -bios or -pflash.
93092df8450SArd Biesheuvel          */
93192df8450SArd Biesheuvel         if (elf_low_addr > info->loader_start
93292df8450SArd Biesheuvel             || elf_high_addr < info->loader_start) {
93392df8450SArd Biesheuvel             /* Pass elf_low_addr as address limit to load_dtb if it may be
93492df8450SArd Biesheuvel              * pointing into RAM, otherwise pass '0' (no limit)
93592df8450SArd Biesheuvel              */
93692df8450SArd Biesheuvel             if (elf_low_addr < info->loader_start) {
93792df8450SArd Biesheuvel                 elf_low_addr = 0;
93892df8450SArd Biesheuvel             }
93992df8450SArd Biesheuvel             if (load_dtb(info->loader_start, info, elf_low_addr) < 0) {
94092df8450SArd Biesheuvel                 exit(1);
94192df8450SArd Biesheuvel             }
94292df8450SArd Biesheuvel         }
94392df8450SArd Biesheuvel     }
94453018216SPaolo Bonzini     entry = elf_entry;
94553018216SPaolo Bonzini     if (kernel_size < 0) {
94653018216SPaolo Bonzini         kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
94725bda50aSMax Filippov                                   &is_linux, NULL, NULL);
94853018216SPaolo Bonzini     }
9496f5d3cbeSRichard W.M. Jones     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
95068115ed5SArd Biesheuvel         kernel_size = load_aarch64_image(info->kernel_filename,
95168115ed5SArd Biesheuvel                                          info->loader_start, &entry);
9526f5d3cbeSRichard W.M. Jones         is_linux = 1;
95368115ed5SArd Biesheuvel     } else if (kernel_size < 0) {
95468115ed5SArd Biesheuvel         /* 32-bit ARM */
95568115ed5SArd Biesheuvel         entry = info->loader_start + KERNEL_LOAD_ADDR;
95653018216SPaolo Bonzini         kernel_size = load_image_targphys(info->kernel_filename, entry,
95768115ed5SArd Biesheuvel                                           info->ram_size - KERNEL_LOAD_ADDR);
95853018216SPaolo Bonzini         is_linux = 1;
95953018216SPaolo Bonzini     }
96053018216SPaolo Bonzini     if (kernel_size < 0) {
961c0dbca36SAlistair Francis         error_report("could not load kernel '%s'", info->kernel_filename);
96253018216SPaolo Bonzini         exit(1);
96353018216SPaolo Bonzini     }
96453018216SPaolo Bonzini     info->entry = entry;
96553018216SPaolo Bonzini     if (is_linux) {
96647b1da81SPeter Maydell         uint32_t fixupcontext[FIXUP_MAX];
96747b1da81SPeter Maydell 
96853018216SPaolo Bonzini         if (info->initrd_filename) {
969fd76663eSSoren Brinkmann             initrd_size = load_ramdisk(info->initrd_filename,
970fd76663eSSoren Brinkmann                                        info->initrd_start,
971fd76663eSSoren Brinkmann                                        info->ram_size -
972fd76663eSSoren Brinkmann                                        info->initrd_start);
973fd76663eSSoren Brinkmann             if (initrd_size < 0) {
97453018216SPaolo Bonzini                 initrd_size = load_image_targphys(info->initrd_filename,
97553018216SPaolo Bonzini                                                   info->initrd_start,
97653018216SPaolo Bonzini                                                   info->ram_size -
97753018216SPaolo Bonzini                                                   info->initrd_start);
978fd76663eSSoren Brinkmann             }
97953018216SPaolo Bonzini             if (initrd_size < 0) {
980c0dbca36SAlistair Francis                 error_report("could not load initrd '%s'",
98153018216SPaolo Bonzini                              info->initrd_filename);
98253018216SPaolo Bonzini                 exit(1);
98353018216SPaolo Bonzini             }
98453018216SPaolo Bonzini         } else {
98553018216SPaolo Bonzini             initrd_size = 0;
98653018216SPaolo Bonzini         }
98753018216SPaolo Bonzini         info->initrd_size = initrd_size;
98853018216SPaolo Bonzini 
98947b1da81SPeter Maydell         fixupcontext[FIXUP_BOARDID] = info->board_id;
99010b8ec73SPeter Crosthwaite         fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr;
99153018216SPaolo Bonzini 
99253018216SPaolo Bonzini         /* for device tree boot, we pass the DTB directly in r2. Otherwise
99353018216SPaolo Bonzini          * we point to the kernel args.
99453018216SPaolo Bonzini          */
99583bfffecSPeter Maydell         if (have_dtb(info)) {
99676e2aef3SAlexander Graf             hwaddr align;
99776e2aef3SAlexander Graf             hwaddr dtb_start;
99876e2aef3SAlexander Graf 
99976e2aef3SAlexander Graf             if (elf_machine == EM_AARCH64) {
100076e2aef3SAlexander Graf                 /*
100176e2aef3SAlexander Graf                  * Some AArch64 kernels on early bootup map the fdt region as
100276e2aef3SAlexander Graf                  *
100376e2aef3SAlexander Graf                  *   [ ALIGN_DOWN(fdt, 2MB) ... ALIGN_DOWN(fdt, 2MB) + 2MB ]
100476e2aef3SAlexander Graf                  *
100576e2aef3SAlexander Graf                  * Let's play safe and prealign it to 2MB to give us some space.
100653018216SPaolo Bonzini                  */
100776e2aef3SAlexander Graf                 align = 2 * 1024 * 1024;
100876e2aef3SAlexander Graf             } else {
100976e2aef3SAlexander Graf                 /*
101076e2aef3SAlexander Graf                  * Some 32bit kernels will trash anything in the 4K page the
101176e2aef3SAlexander Graf                  * initrd ends in, so make sure the DTB isn't caught up in that.
101276e2aef3SAlexander Graf                  */
101376e2aef3SAlexander Graf                 align = 4096;
101476e2aef3SAlexander Graf             }
101576e2aef3SAlexander Graf 
101676e2aef3SAlexander Graf             /* Place the DTB after the initrd in memory with alignment. */
101776e2aef3SAlexander Graf             dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size, align);
1018fee8ea12SArd Biesheuvel             if (load_dtb(dtb_start, info, 0) < 0) {
101953018216SPaolo Bonzini                 exit(1);
102053018216SPaolo Bonzini             }
102147b1da81SPeter Maydell             fixupcontext[FIXUP_ARGPTR] = dtb_start;
102253018216SPaolo Bonzini         } else {
102347b1da81SPeter Maydell             fixupcontext[FIXUP_ARGPTR] = info->loader_start + KERNEL_ARGS_ADDR;
102453018216SPaolo Bonzini             if (info->ram_size >= (1ULL << 32)) {
1025c0dbca36SAlistair Francis                 error_report("RAM size must be less than 4GB to boot"
102653018216SPaolo Bonzini                              " Linux kernel using ATAGS (try passing a device tree"
1027c0dbca36SAlistair Francis                              " using -dtb)");
102853018216SPaolo Bonzini                 exit(1);
102953018216SPaolo Bonzini             }
103053018216SPaolo Bonzini         }
103147b1da81SPeter Maydell         fixupcontext[FIXUP_ENTRYPOINT] = entry;
103247b1da81SPeter Maydell 
103347b1da81SPeter Maydell         write_bootloader("bootloader", info->loader_start,
10344d9ebf75SMian M. Hamayun                          primary_loader, fixupcontext);
103547b1da81SPeter Maydell 
103653018216SPaolo Bonzini         if (info->nb_cpus > 1) {
103753018216SPaolo Bonzini             info->write_secondary_boot(cpu, info);
103853018216SPaolo Bonzini         }
103910b8ec73SPeter Crosthwaite         if (info->write_board_setup) {
104010b8ec73SPeter Crosthwaite             info->write_board_setup(cpu, info);
104110b8ec73SPeter Crosthwaite         }
1042d8b1ae42SPeter Maydell 
1043d8b1ae42SPeter Maydell         /* Notify devices which need to fake up firmware initialization
1044d8b1ae42SPeter Maydell          * that we're doing a direct kernel boot.
1045d8b1ae42SPeter Maydell          */
1046d8b1ae42SPeter Maydell         object_child_foreach_recursive(object_get_root(),
1047d8b1ae42SPeter Maydell                                        do_arm_linux_init, info);
104853018216SPaolo Bonzini     }
104953018216SPaolo Bonzini     info->is_linux = is_linux;
105053018216SPaolo Bonzini 
1051c6faa758SArd Biesheuvel     for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
1052c6faa758SArd Biesheuvel         ARM_CPU(cs)->env.boot_info = info;
105353018216SPaolo Bonzini     }
105453018216SPaolo Bonzini }
1055ac9d32e3SEric Auger 
1056ac9d32e3SEric Auger void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
1057ac9d32e3SEric Auger {
105863a183edSEric Auger     CPUState *cs;
105963a183edSEric Auger 
1060ac9d32e3SEric Auger     info->load_kernel_notifier.cpu = cpu;
1061ac9d32e3SEric Auger     info->load_kernel_notifier.notifier.notify = arm_load_kernel_notify;
1062ac9d32e3SEric Auger     qemu_add_machine_init_done_notifier(&info->load_kernel_notifier.notifier);
106363a183edSEric Auger 
106463a183edSEric Auger     /* CPU objects (unlike devices) are not automatically reset on system
106563a183edSEric Auger      * reset, so we must always register a handler to do so. If we're
106663a183edSEric Auger      * actually loading a kernel, the handler is also responsible for
106763a183edSEric Auger      * arranging that we start it correctly.
106863a183edSEric Auger      */
106963a183edSEric Auger     for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
107063a183edSEric Auger         qemu_register_reset(do_cpu_reset, ARM_CPU(cs));
107163a183edSEric Auger     }
1072ac9d32e3SEric Auger }
1073d8b1ae42SPeter Maydell 
1074d8b1ae42SPeter Maydell static const TypeInfo arm_linux_boot_if_info = {
1075d8b1ae42SPeter Maydell     .name = TYPE_ARM_LINUX_BOOT_IF,
1076d8b1ae42SPeter Maydell     .parent = TYPE_INTERFACE,
1077d8b1ae42SPeter Maydell     .class_size = sizeof(ARMLinuxBootIfClass),
1078d8b1ae42SPeter Maydell };
1079d8b1ae42SPeter Maydell 
1080d8b1ae42SPeter Maydell static void arm_linux_boot_register_types(void)
1081d8b1ae42SPeter Maydell {
1082d8b1ae42SPeter Maydell     type_register_static(&arm_linux_boot_if_info);
1083d8b1ae42SPeter Maydell }
1084d8b1ae42SPeter Maydell 
1085d8b1ae42SPeter Maydell type_init(arm_linux_boot_register_types)
1086