xref: /openbmc/qemu/hw/arm/boot.c (revision b77257d7bae26a0fca6a90af88d54ee2c45f5b61)
153018216SPaolo Bonzini /*
253018216SPaolo Bonzini  * ARM kernel loader.
353018216SPaolo Bonzini  *
453018216SPaolo Bonzini  * Copyright (c) 2006-2007 CodeSourcery.
553018216SPaolo Bonzini  * Written by Paul Brook
653018216SPaolo Bonzini  *
753018216SPaolo Bonzini  * This code is licensed under the GPL.
853018216SPaolo Bonzini  */
953018216SPaolo Bonzini 
1012b16722SPeter Maydell #include "qemu/osdep.h"
11da34e65cSMarkus Armbruster #include "qapi/error.h"
12*b77257d7SGuenter Roeck #include <libfdt.h>
1353018216SPaolo Bonzini #include "hw/hw.h"
14bd2be150SPeter Maydell #include "hw/arm/arm.h"
15d8b1ae42SPeter Maydell #include "hw/arm/linux-boot-if.h"
16baf6b681SPeter Crosthwaite #include "sysemu/kvm.h"
1753018216SPaolo Bonzini #include "sysemu/sysemu.h"
189695200aSShannon Zhao #include "sysemu/numa.h"
1953018216SPaolo Bonzini #include "hw/boards.h"
2053018216SPaolo Bonzini #include "hw/loader.h"
2153018216SPaolo Bonzini #include "elf.h"
2253018216SPaolo Bonzini #include "sysemu/device_tree.h"
2353018216SPaolo Bonzini #include "qemu/config-file.h"
242198a121SEdgar E. Iglesias #include "exec/address-spaces.h"
2553018216SPaolo Bonzini 
264d9ebf75SMian M. Hamayun /* Kernel boot protocol is specified in the kernel docs
274d9ebf75SMian M. Hamayun  * Documentation/arm/Booting and Documentation/arm64/booting.txt
284d9ebf75SMian M. Hamayun  * They have different preferred image load offsets from system RAM base.
294d9ebf75SMian M. Hamayun  */
3053018216SPaolo Bonzini #define KERNEL_ARGS_ADDR 0x100
3153018216SPaolo Bonzini #define KERNEL_LOAD_ADDR 0x00010000
324d9ebf75SMian M. Hamayun #define KERNEL64_LOAD_ADDR 0x00080000
3353018216SPaolo Bonzini 
3447b1da81SPeter Maydell typedef enum {
3547b1da81SPeter Maydell     FIXUP_NONE = 0,     /* do nothing */
3647b1da81SPeter Maydell     FIXUP_TERMINATOR,   /* end of insns */
3747b1da81SPeter Maydell     FIXUP_BOARDID,      /* overwrite with board ID number */
3810b8ec73SPeter Crosthwaite     FIXUP_BOARD_SETUP,  /* overwrite with board specific setup code address */
3947b1da81SPeter Maydell     FIXUP_ARGPTR,       /* overwrite with pointer to kernel args */
4047b1da81SPeter Maydell     FIXUP_ENTRYPOINT,   /* overwrite with kernel entry point */
4147b1da81SPeter Maydell     FIXUP_GIC_CPU_IF,   /* overwrite with GIC CPU interface address */
4247b1da81SPeter Maydell     FIXUP_BOOTREG,      /* overwrite with boot register address */
4347b1da81SPeter Maydell     FIXUP_DSB,          /* overwrite with correct DSB insn for cpu */
4447b1da81SPeter Maydell     FIXUP_MAX,
4547b1da81SPeter Maydell } FixupType;
4647b1da81SPeter Maydell 
4747b1da81SPeter Maydell typedef struct ARMInsnFixup {
4847b1da81SPeter Maydell     uint32_t insn;
4947b1da81SPeter Maydell     FixupType fixup;
5047b1da81SPeter Maydell } ARMInsnFixup;
5147b1da81SPeter Maydell 
524d9ebf75SMian M. Hamayun static const ARMInsnFixup bootloader_aarch64[] = {
534d9ebf75SMian M. Hamayun     { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
544d9ebf75SMian M. Hamayun     { 0xaa1f03e1 }, /* mov x1, xzr */
554d9ebf75SMian M. Hamayun     { 0xaa1f03e2 }, /* mov x2, xzr */
564d9ebf75SMian M. Hamayun     { 0xaa1f03e3 }, /* mov x3, xzr */
574d9ebf75SMian M. Hamayun     { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
584d9ebf75SMian M. Hamayun     { 0xd61f0080 }, /* br x4      ; Jump to the kernel entry point */
594d9ebf75SMian M. Hamayun     { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */
604d9ebf75SMian M. Hamayun     { 0 }, /* .word @DTB Higher 32-bits */
614d9ebf75SMian M. Hamayun     { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */
624d9ebf75SMian M. Hamayun     { 0 }, /* .word @Kernel Entry Higher 32-bits */
634d9ebf75SMian M. Hamayun     { 0, FIXUP_TERMINATOR }
644d9ebf75SMian M. Hamayun };
654d9ebf75SMian M. Hamayun 
6610b8ec73SPeter Crosthwaite /* A very small bootloader: call the board-setup code (if needed),
6710b8ec73SPeter Crosthwaite  * set r0-r2, then jump to the kernel.
6810b8ec73SPeter Crosthwaite  * If we're not calling boot setup code then we don't copy across
6910b8ec73SPeter Crosthwaite  * the first BOOTLOADER_NO_BOARD_SETUP_OFFSET insns in this array.
7010b8ec73SPeter Crosthwaite  */
7110b8ec73SPeter Crosthwaite 
7247b1da81SPeter Maydell static const ARMInsnFixup bootloader[] = {
73b4850e5aSSylvain Garrigues     { 0xe28fe004 }, /* add     lr, pc, #4 */
7410b8ec73SPeter Crosthwaite     { 0xe51ff004 }, /* ldr     pc, [pc, #-4] */
7510b8ec73SPeter Crosthwaite     { 0, FIXUP_BOARD_SETUP },
7610b8ec73SPeter Crosthwaite #define BOOTLOADER_NO_BOARD_SETUP_OFFSET 3
7747b1da81SPeter Maydell     { 0xe3a00000 }, /* mov     r0, #0 */
7847b1da81SPeter Maydell     { 0xe59f1004 }, /* ldr     r1, [pc, #4] */
7947b1da81SPeter Maydell     { 0xe59f2004 }, /* ldr     r2, [pc, #4] */
8047b1da81SPeter Maydell     { 0xe59ff004 }, /* ldr     pc, [pc, #4] */
8147b1da81SPeter Maydell     { 0, FIXUP_BOARDID },
8247b1da81SPeter Maydell     { 0, FIXUP_ARGPTR },
8347b1da81SPeter Maydell     { 0, FIXUP_ENTRYPOINT },
8447b1da81SPeter Maydell     { 0, FIXUP_TERMINATOR }
8553018216SPaolo Bonzini };
8653018216SPaolo Bonzini 
8753018216SPaolo Bonzini /* Handling for secondary CPU boot in a multicore system.
8853018216SPaolo Bonzini  * Unlike the uniprocessor/primary CPU boot, this is platform
8953018216SPaolo Bonzini  * dependent. The default code here is based on the secondary
9053018216SPaolo Bonzini  * CPU boot protocol used on realview/vexpress boards, with
9153018216SPaolo Bonzini  * some parameterisation to increase its flexibility.
9253018216SPaolo Bonzini  * QEMU platform models for which this code is not appropriate
9353018216SPaolo Bonzini  * should override write_secondary_boot and secondary_cpu_reset_hook
9453018216SPaolo Bonzini  * instead.
9553018216SPaolo Bonzini  *
9653018216SPaolo Bonzini  * This code enables the interrupt controllers for the secondary
9753018216SPaolo Bonzini  * CPUs and then puts all the secondary CPUs into a loop waiting
9853018216SPaolo Bonzini  * for an interprocessor interrupt and polling a configurable
9953018216SPaolo Bonzini  * location for the kernel secondary CPU entry point.
10053018216SPaolo Bonzini  */
10153018216SPaolo Bonzini #define DSB_INSN 0xf57ff04f
10253018216SPaolo Bonzini #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
10353018216SPaolo Bonzini 
10447b1da81SPeter Maydell static const ARMInsnFixup smpboot[] = {
10547b1da81SPeter Maydell     { 0xe59f2028 }, /* ldr r2, gic_cpu_if */
10647b1da81SPeter Maydell     { 0xe59f0028 }, /* ldr r0, bootreg_addr */
10747b1da81SPeter Maydell     { 0xe3a01001 }, /* mov r1, #1 */
10847b1da81SPeter Maydell     { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */
10947b1da81SPeter Maydell     { 0xe3a010ff }, /* mov r1, #0xff */
11047b1da81SPeter Maydell     { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
11147b1da81SPeter Maydell     { 0, FIXUP_DSB },   /* dsb */
11247b1da81SPeter Maydell     { 0xe320f003 }, /* wfi */
11347b1da81SPeter Maydell     { 0xe5901000 }, /* ldr     r1, [r0] */
11447b1da81SPeter Maydell     { 0xe1110001 }, /* tst     r1, r1 */
11547b1da81SPeter Maydell     { 0x0afffffb }, /* beq     <wfi> */
11647b1da81SPeter Maydell     { 0xe12fff11 }, /* bx      r1 */
11747b1da81SPeter Maydell     { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */
11847b1da81SPeter Maydell     { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */
11947b1da81SPeter Maydell     { 0, FIXUP_TERMINATOR }
12053018216SPaolo Bonzini };
12153018216SPaolo Bonzini 
12247b1da81SPeter Maydell static void write_bootloader(const char *name, hwaddr addr,
12347b1da81SPeter Maydell                              const ARMInsnFixup *insns, uint32_t *fixupcontext)
12447b1da81SPeter Maydell {
12547b1da81SPeter Maydell     /* Fix up the specified bootloader fragment and write it into
12647b1da81SPeter Maydell      * guest memory using rom_add_blob_fixed(). fixupcontext is
12747b1da81SPeter Maydell      * an array giving the values to write in for the fixup types
12847b1da81SPeter Maydell      * which write a value into the code array.
12947b1da81SPeter Maydell      */
13047b1da81SPeter Maydell     int i, len;
13147b1da81SPeter Maydell     uint32_t *code;
13247b1da81SPeter Maydell 
13347b1da81SPeter Maydell     len = 0;
13447b1da81SPeter Maydell     while (insns[len].fixup != FIXUP_TERMINATOR) {
13547b1da81SPeter Maydell         len++;
13647b1da81SPeter Maydell     }
13747b1da81SPeter Maydell 
13847b1da81SPeter Maydell     code = g_new0(uint32_t, len);
13947b1da81SPeter Maydell 
14047b1da81SPeter Maydell     for (i = 0; i < len; i++) {
14147b1da81SPeter Maydell         uint32_t insn = insns[i].insn;
14247b1da81SPeter Maydell         FixupType fixup = insns[i].fixup;
14347b1da81SPeter Maydell 
14447b1da81SPeter Maydell         switch (fixup) {
14547b1da81SPeter Maydell         case FIXUP_NONE:
14647b1da81SPeter Maydell             break;
14747b1da81SPeter Maydell         case FIXUP_BOARDID:
14810b8ec73SPeter Crosthwaite         case FIXUP_BOARD_SETUP:
14947b1da81SPeter Maydell         case FIXUP_ARGPTR:
15047b1da81SPeter Maydell         case FIXUP_ENTRYPOINT:
15147b1da81SPeter Maydell         case FIXUP_GIC_CPU_IF:
15247b1da81SPeter Maydell         case FIXUP_BOOTREG:
15347b1da81SPeter Maydell         case FIXUP_DSB:
15447b1da81SPeter Maydell             insn = fixupcontext[fixup];
15547b1da81SPeter Maydell             break;
15647b1da81SPeter Maydell         default:
15747b1da81SPeter Maydell             abort();
15847b1da81SPeter Maydell         }
15947b1da81SPeter Maydell         code[i] = tswap32(insn);
16047b1da81SPeter Maydell     }
16147b1da81SPeter Maydell 
16247b1da81SPeter Maydell     rom_add_blob_fixed(name, code, len * sizeof(uint32_t), addr);
16347b1da81SPeter Maydell 
16447b1da81SPeter Maydell     g_free(code);
16547b1da81SPeter Maydell }
16647b1da81SPeter Maydell 
16753018216SPaolo Bonzini static void default_write_secondary(ARMCPU *cpu,
16853018216SPaolo Bonzini                                     const struct arm_boot_info *info)
16953018216SPaolo Bonzini {
17047b1da81SPeter Maydell     uint32_t fixupcontext[FIXUP_MAX];
17147b1da81SPeter Maydell 
17247b1da81SPeter Maydell     fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr;
17347b1da81SPeter Maydell     fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr;
17447b1da81SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
17547b1da81SPeter Maydell         fixupcontext[FIXUP_DSB] = DSB_INSN;
17647b1da81SPeter Maydell     } else {
17747b1da81SPeter Maydell         fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
17853018216SPaolo Bonzini     }
17947b1da81SPeter Maydell 
18047b1da81SPeter Maydell     write_bootloader("smpboot", info->smp_loader_start,
18147b1da81SPeter Maydell                      smpboot, fixupcontext);
18253018216SPaolo Bonzini }
18353018216SPaolo Bonzini 
184716536a9SAndrew Baumann void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,
185716536a9SAndrew Baumann                                             const struct arm_boot_info *info,
186716536a9SAndrew Baumann                                             hwaddr mvbar_addr)
187716536a9SAndrew Baumann {
188716536a9SAndrew Baumann     int n;
189716536a9SAndrew Baumann     uint32_t mvbar_blob[] = {
190716536a9SAndrew Baumann         /* mvbar_addr: secure monitor vectors
191716536a9SAndrew Baumann          * Default unimplemented and unused vectors to spin. Makes it
192716536a9SAndrew Baumann          * easier to debug (as opposed to the CPU running away).
193716536a9SAndrew Baumann          */
194716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
195716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
196716536a9SAndrew Baumann         0xe1b0f00e, /* movs pc, lr ;SMC exception return */
197716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
198716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
199716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
200716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
201716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
202716536a9SAndrew Baumann     };
203716536a9SAndrew Baumann     uint32_t board_setup_blob[] = {
204716536a9SAndrew Baumann         /* board setup addr */
205716536a9SAndrew Baumann         0xe3a00e00 + (mvbar_addr >> 4), /* mov r0, #mvbar_addr */
206716536a9SAndrew Baumann         0xee0c0f30, /* mcr     p15, 0, r0, c12, c0, 1 ;set MVBAR */
207716536a9SAndrew Baumann         0xee110f11, /* mrc     p15, 0, r0, c1 , c1, 0 ;read SCR */
208716536a9SAndrew Baumann         0xe3800031, /* orr     r0, #0x31              ;enable AW, FW, NS */
209716536a9SAndrew Baumann         0xee010f11, /* mcr     p15, 0, r0, c1, c1, 0  ;write SCR */
210716536a9SAndrew Baumann         0xe1a0100e, /* mov     r1, lr                 ;save LR across SMC */
211716536a9SAndrew Baumann         0xe1600070, /* smc     #0                     ;call monitor to flush SCR */
212716536a9SAndrew Baumann         0xe1a0f001, /* mov     pc, r1                 ;return */
213716536a9SAndrew Baumann     };
214716536a9SAndrew Baumann 
215716536a9SAndrew Baumann     /* check that mvbar_addr is correctly aligned and relocatable (using MOV) */
216716536a9SAndrew Baumann     assert((mvbar_addr & 0x1f) == 0 && (mvbar_addr >> 4) < 0x100);
217716536a9SAndrew Baumann 
218716536a9SAndrew Baumann     /* check that these blobs don't overlap */
219716536a9SAndrew Baumann     assert((mvbar_addr + sizeof(mvbar_blob) <= info->board_setup_addr)
220716536a9SAndrew Baumann           || (info->board_setup_addr + sizeof(board_setup_blob) <= mvbar_addr));
221716536a9SAndrew Baumann 
222716536a9SAndrew Baumann     for (n = 0; n < ARRAY_SIZE(mvbar_blob); n++) {
223716536a9SAndrew Baumann         mvbar_blob[n] = tswap32(mvbar_blob[n]);
224716536a9SAndrew Baumann     }
225716536a9SAndrew Baumann     rom_add_blob_fixed("board-setup-mvbar", mvbar_blob, sizeof(mvbar_blob),
226716536a9SAndrew Baumann                        mvbar_addr);
227716536a9SAndrew Baumann 
228716536a9SAndrew Baumann     for (n = 0; n < ARRAY_SIZE(board_setup_blob); n++) {
229716536a9SAndrew Baumann         board_setup_blob[n] = tswap32(board_setup_blob[n]);
230716536a9SAndrew Baumann     }
231716536a9SAndrew Baumann     rom_add_blob_fixed("board-setup", board_setup_blob,
232716536a9SAndrew Baumann                        sizeof(board_setup_blob), info->board_setup_addr);
233716536a9SAndrew Baumann }
234716536a9SAndrew Baumann 
23553018216SPaolo Bonzini static void default_reset_secondary(ARMCPU *cpu,
23653018216SPaolo Bonzini                                     const struct arm_boot_info *info)
23753018216SPaolo Bonzini {
2384df81c6eSPeter Crosthwaite     CPUState *cs = CPU(cpu);
23953018216SPaolo Bonzini 
24042874d3aSPeter Maydell     address_space_stl_notdirty(&address_space_memory, info->smp_bootreg_addr,
24142874d3aSPeter Maydell                                0, MEMTXATTRS_UNSPECIFIED, NULL);
2424df81c6eSPeter Crosthwaite     cpu_set_pc(cs, info->smp_loader_start);
24353018216SPaolo Bonzini }
24453018216SPaolo Bonzini 
24583bfffecSPeter Maydell static inline bool have_dtb(const struct arm_boot_info *info)
24683bfffecSPeter Maydell {
24783bfffecSPeter Maydell     return info->dtb_filename || info->get_dtb;
24883bfffecSPeter Maydell }
24983bfffecSPeter Maydell 
25053018216SPaolo Bonzini #define WRITE_WORD(p, value) do { \
25142874d3aSPeter Maydell     address_space_stl_notdirty(&address_space_memory, p, value, \
25242874d3aSPeter Maydell                                MEMTXATTRS_UNSPECIFIED, NULL);  \
25353018216SPaolo Bonzini     p += 4;                       \
25453018216SPaolo Bonzini } while (0)
25553018216SPaolo Bonzini 
25653018216SPaolo Bonzini static void set_kernel_args(const struct arm_boot_info *info)
25753018216SPaolo Bonzini {
25853018216SPaolo Bonzini     int initrd_size = info->initrd_size;
25953018216SPaolo Bonzini     hwaddr base = info->loader_start;
26053018216SPaolo Bonzini     hwaddr p;
26153018216SPaolo Bonzini 
26253018216SPaolo Bonzini     p = base + KERNEL_ARGS_ADDR;
26353018216SPaolo Bonzini     /* ATAG_CORE */
26453018216SPaolo Bonzini     WRITE_WORD(p, 5);
26553018216SPaolo Bonzini     WRITE_WORD(p, 0x54410001);
26653018216SPaolo Bonzini     WRITE_WORD(p, 1);
26753018216SPaolo Bonzini     WRITE_WORD(p, 0x1000);
26853018216SPaolo Bonzini     WRITE_WORD(p, 0);
26953018216SPaolo Bonzini     /* ATAG_MEM */
27053018216SPaolo Bonzini     /* TODO: handle multiple chips on one ATAG list */
27153018216SPaolo Bonzini     WRITE_WORD(p, 4);
27253018216SPaolo Bonzini     WRITE_WORD(p, 0x54410002);
27353018216SPaolo Bonzini     WRITE_WORD(p, info->ram_size);
27453018216SPaolo Bonzini     WRITE_WORD(p, info->loader_start);
27553018216SPaolo Bonzini     if (initrd_size) {
27653018216SPaolo Bonzini         /* ATAG_INITRD2 */
27753018216SPaolo Bonzini         WRITE_WORD(p, 4);
27853018216SPaolo Bonzini         WRITE_WORD(p, 0x54420005);
27953018216SPaolo Bonzini         WRITE_WORD(p, info->initrd_start);
28053018216SPaolo Bonzini         WRITE_WORD(p, initrd_size);
28153018216SPaolo Bonzini     }
28253018216SPaolo Bonzini     if (info->kernel_cmdline && *info->kernel_cmdline) {
28353018216SPaolo Bonzini         /* ATAG_CMDLINE */
28453018216SPaolo Bonzini         int cmdline_size;
28553018216SPaolo Bonzini 
28653018216SPaolo Bonzini         cmdline_size = strlen(info->kernel_cmdline);
287e1fe50dcSStefan Weil         cpu_physical_memory_write(p + 8, info->kernel_cmdline,
28853018216SPaolo Bonzini                                   cmdline_size + 1);
28953018216SPaolo Bonzini         cmdline_size = (cmdline_size >> 2) + 1;
29053018216SPaolo Bonzini         WRITE_WORD(p, cmdline_size + 2);
29153018216SPaolo Bonzini         WRITE_WORD(p, 0x54410009);
29253018216SPaolo Bonzini         p += cmdline_size * 4;
29353018216SPaolo Bonzini     }
29453018216SPaolo Bonzini     if (info->atag_board) {
29553018216SPaolo Bonzini         /* ATAG_BOARD */
29653018216SPaolo Bonzini         int atag_board_len;
29753018216SPaolo Bonzini         uint8_t atag_board_buf[0x1000];
29853018216SPaolo Bonzini 
29953018216SPaolo Bonzini         atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
30053018216SPaolo Bonzini         WRITE_WORD(p, (atag_board_len + 8) >> 2);
30153018216SPaolo Bonzini         WRITE_WORD(p, 0x414f4d50);
30253018216SPaolo Bonzini         cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
30353018216SPaolo Bonzini         p += atag_board_len;
30453018216SPaolo Bonzini     }
30553018216SPaolo Bonzini     /* ATAG_END */
30653018216SPaolo Bonzini     WRITE_WORD(p, 0);
30753018216SPaolo Bonzini     WRITE_WORD(p, 0);
30853018216SPaolo Bonzini }
30953018216SPaolo Bonzini 
31053018216SPaolo Bonzini static void set_kernel_args_old(const struct arm_boot_info *info)
31153018216SPaolo Bonzini {
31253018216SPaolo Bonzini     hwaddr p;
31353018216SPaolo Bonzini     const char *s;
31453018216SPaolo Bonzini     int initrd_size = info->initrd_size;
31553018216SPaolo Bonzini     hwaddr base = info->loader_start;
31653018216SPaolo Bonzini 
31753018216SPaolo Bonzini     /* see linux/include/asm-arm/setup.h */
31853018216SPaolo Bonzini     p = base + KERNEL_ARGS_ADDR;
31953018216SPaolo Bonzini     /* page_size */
32053018216SPaolo Bonzini     WRITE_WORD(p, 4096);
32153018216SPaolo Bonzini     /* nr_pages */
32253018216SPaolo Bonzini     WRITE_WORD(p, info->ram_size / 4096);
32353018216SPaolo Bonzini     /* ramdisk_size */
32453018216SPaolo Bonzini     WRITE_WORD(p, 0);
32553018216SPaolo Bonzini #define FLAG_READONLY	1
32653018216SPaolo Bonzini #define FLAG_RDLOAD	4
32753018216SPaolo Bonzini #define FLAG_RDPROMPT	8
32853018216SPaolo Bonzini     /* flags */
32953018216SPaolo Bonzini     WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
33053018216SPaolo Bonzini     /* rootdev */
33153018216SPaolo Bonzini     WRITE_WORD(p, (31 << 8) | 0);	/* /dev/mtdblock0 */
33253018216SPaolo Bonzini     /* video_num_cols */
33353018216SPaolo Bonzini     WRITE_WORD(p, 0);
33453018216SPaolo Bonzini     /* video_num_rows */
33553018216SPaolo Bonzini     WRITE_WORD(p, 0);
33653018216SPaolo Bonzini     /* video_x */
33753018216SPaolo Bonzini     WRITE_WORD(p, 0);
33853018216SPaolo Bonzini     /* video_y */
33953018216SPaolo Bonzini     WRITE_WORD(p, 0);
34053018216SPaolo Bonzini     /* memc_control_reg */
34153018216SPaolo Bonzini     WRITE_WORD(p, 0);
34253018216SPaolo Bonzini     /* unsigned char sounddefault */
34353018216SPaolo Bonzini     /* unsigned char adfsdrives */
34453018216SPaolo Bonzini     /* unsigned char bytes_per_char_h */
34553018216SPaolo Bonzini     /* unsigned char bytes_per_char_v */
34653018216SPaolo Bonzini     WRITE_WORD(p, 0);
34753018216SPaolo Bonzini     /* pages_in_bank[4] */
34853018216SPaolo Bonzini     WRITE_WORD(p, 0);
34953018216SPaolo Bonzini     WRITE_WORD(p, 0);
35053018216SPaolo Bonzini     WRITE_WORD(p, 0);
35153018216SPaolo Bonzini     WRITE_WORD(p, 0);
35253018216SPaolo Bonzini     /* pages_in_vram */
35353018216SPaolo Bonzini     WRITE_WORD(p, 0);
35453018216SPaolo Bonzini     /* initrd_start */
35553018216SPaolo Bonzini     if (initrd_size) {
35653018216SPaolo Bonzini         WRITE_WORD(p, info->initrd_start);
35753018216SPaolo Bonzini     } else {
35853018216SPaolo Bonzini         WRITE_WORD(p, 0);
35953018216SPaolo Bonzini     }
36053018216SPaolo Bonzini     /* initrd_size */
36153018216SPaolo Bonzini     WRITE_WORD(p, initrd_size);
36253018216SPaolo Bonzini     /* rd_start */
36353018216SPaolo Bonzini     WRITE_WORD(p, 0);
36453018216SPaolo Bonzini     /* system_rev */
36553018216SPaolo Bonzini     WRITE_WORD(p, 0);
36653018216SPaolo Bonzini     /* system_serial_low */
36753018216SPaolo Bonzini     WRITE_WORD(p, 0);
36853018216SPaolo Bonzini     /* system_serial_high */
36953018216SPaolo Bonzini     WRITE_WORD(p, 0);
37053018216SPaolo Bonzini     /* mem_fclk_21285 */
37153018216SPaolo Bonzini     WRITE_WORD(p, 0);
37253018216SPaolo Bonzini     /* zero unused fields */
37353018216SPaolo Bonzini     while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
37453018216SPaolo Bonzini         WRITE_WORD(p, 0);
37553018216SPaolo Bonzini     }
37653018216SPaolo Bonzini     s = info->kernel_cmdline;
37753018216SPaolo Bonzini     if (s) {
378e1fe50dcSStefan Weil         cpu_physical_memory_write(p, s, strlen(s) + 1);
37953018216SPaolo Bonzini     } else {
38053018216SPaolo Bonzini         WRITE_WORD(p, 0);
38153018216SPaolo Bonzini     }
38253018216SPaolo Bonzini }
38353018216SPaolo Bonzini 
384fee8ea12SArd Biesheuvel /**
385fee8ea12SArd Biesheuvel  * load_dtb() - load a device tree binary image into memory
386fee8ea12SArd Biesheuvel  * @addr:       the address to load the image at
387fee8ea12SArd Biesheuvel  * @binfo:      struct describing the boot environment
388fee8ea12SArd Biesheuvel  * @addr_limit: upper limit of the available memory area at @addr
389fee8ea12SArd Biesheuvel  *
390fee8ea12SArd Biesheuvel  * Load a device tree supplied by the machine or by the user  with the
391fee8ea12SArd Biesheuvel  * '-dtb' command line option, and put it at offset @addr in target
392fee8ea12SArd Biesheuvel  * memory.
393fee8ea12SArd Biesheuvel  *
394fee8ea12SArd Biesheuvel  * If @addr_limit contains a meaningful value (i.e., it is strictly greater
395fee8ea12SArd Biesheuvel  * than @addr), the device tree is only loaded if its size does not exceed
396fee8ea12SArd Biesheuvel  * the limit.
397fee8ea12SArd Biesheuvel  *
398fee8ea12SArd Biesheuvel  * Returns: the size of the device tree image on success,
399fee8ea12SArd Biesheuvel  *          0 if the image size exceeds the limit,
400fee8ea12SArd Biesheuvel  *          -1 on errors.
401a554ecb4Szhanghailiang  *
402a554ecb4Szhanghailiang  * Note: Must not be called unless have_dtb(binfo) is true.
403fee8ea12SArd Biesheuvel  */
404fee8ea12SArd Biesheuvel static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
405fee8ea12SArd Biesheuvel                     hwaddr addr_limit)
40653018216SPaolo Bonzini {
40753018216SPaolo Bonzini     void *fdt = NULL;
40853018216SPaolo Bonzini     int size, rc;
40970976c41SPeter Maydell     uint32_t acells, scells;
4109695200aSShannon Zhao     char *nodename;
4119695200aSShannon Zhao     unsigned int i;
4129695200aSShannon Zhao     hwaddr mem_base, mem_len;
41353018216SPaolo Bonzini 
4140fb79851SJohn Rigby     if (binfo->dtb_filename) {
4150fb79851SJohn Rigby         char *filename;
41653018216SPaolo Bonzini         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
41753018216SPaolo Bonzini         if (!filename) {
41853018216SPaolo Bonzini             fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
419c23045deSPeter Maydell             goto fail;
42053018216SPaolo Bonzini         }
42153018216SPaolo Bonzini 
42253018216SPaolo Bonzini         fdt = load_device_tree(filename, &size);
42353018216SPaolo Bonzini         if (!fdt) {
42453018216SPaolo Bonzini             fprintf(stderr, "Couldn't open dtb file %s\n", filename);
42553018216SPaolo Bonzini             g_free(filename);
426c23045deSPeter Maydell             goto fail;
42753018216SPaolo Bonzini         }
42853018216SPaolo Bonzini         g_free(filename);
429a554ecb4Szhanghailiang     } else {
4300fb79851SJohn Rigby         fdt = binfo->get_dtb(binfo, &size);
4310fb79851SJohn Rigby         if (!fdt) {
4320fb79851SJohn Rigby             fprintf(stderr, "Board was unable to create a dtb blob\n");
4330fb79851SJohn Rigby             goto fail;
4340fb79851SJohn Rigby         }
4350fb79851SJohn Rigby     }
43653018216SPaolo Bonzini 
437fee8ea12SArd Biesheuvel     if (addr_limit > addr && size > (addr_limit - addr)) {
438fee8ea12SArd Biesheuvel         /* Installing the device tree blob at addr would exceed addr_limit.
439fee8ea12SArd Biesheuvel          * Whether this constitutes failure is up to the caller to decide,
440fee8ea12SArd Biesheuvel          * so just return 0 as size, i.e., no error.
441fee8ea12SArd Biesheuvel          */
442fee8ea12SArd Biesheuvel         g_free(fdt);
443fee8ea12SArd Biesheuvel         return 0;
444fee8ea12SArd Biesheuvel     }
445fee8ea12SArd Biesheuvel 
44658e71097SEric Auger     acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells",
44758e71097SEric Auger                                    NULL, &error_fatal);
44858e71097SEric Auger     scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells",
44958e71097SEric Auger                                    NULL, &error_fatal);
45053018216SPaolo Bonzini     if (acells == 0 || scells == 0) {
45153018216SPaolo Bonzini         fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
452c23045deSPeter Maydell         goto fail;
45353018216SPaolo Bonzini     }
45453018216SPaolo Bonzini 
45570976c41SPeter Maydell     if (scells < 2 && binfo->ram_size >= (1ULL << 32)) {
45670976c41SPeter Maydell         /* This is user error so deserves a friendlier error message
45770976c41SPeter Maydell          * than the failure of setprop_sized_cells would provide
45870976c41SPeter Maydell          */
45953018216SPaolo Bonzini         fprintf(stderr, "qemu: dtb file not compatible with "
46053018216SPaolo Bonzini                 "RAM size > 4GB\n");
461c23045deSPeter Maydell         goto fail;
46253018216SPaolo Bonzini     }
46353018216SPaolo Bonzini 
4649695200aSShannon Zhao     if (nb_numa_nodes > 0) {
4659695200aSShannon Zhao         /*
4669695200aSShannon Zhao          * Turn the /memory node created before into a NOP node, then create
4679695200aSShannon Zhao          * /memory@addr nodes for all numa nodes respectively.
4689695200aSShannon Zhao          */
4699695200aSShannon Zhao         qemu_fdt_nop_node(fdt, "/memory");
4709695200aSShannon Zhao         mem_base = binfo->loader_start;
4719695200aSShannon Zhao         for (i = 0; i < nb_numa_nodes; i++) {
4729695200aSShannon Zhao             mem_len = numa_info[i].node_mem;
4739695200aSShannon Zhao             nodename = g_strdup_printf("/memory@%" PRIx64, mem_base);
4749695200aSShannon Zhao             qemu_fdt_add_subnode(fdt, nodename);
4759695200aSShannon Zhao             qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
4769695200aSShannon Zhao             rc = qemu_fdt_setprop_sized_cells(fdt, nodename, "reg",
4779695200aSShannon Zhao                                               acells, mem_base,
4789695200aSShannon Zhao                                               scells, mem_len);
4799695200aSShannon Zhao             if (rc < 0) {
4809695200aSShannon Zhao                 fprintf(stderr, "couldn't set %s/reg for node %d\n", nodename,
4819695200aSShannon Zhao                         i);
4829695200aSShannon Zhao                 goto fail;
4839695200aSShannon Zhao             }
4849695200aSShannon Zhao 
4859695200aSShannon Zhao             qemu_fdt_setprop_cell(fdt, nodename, "numa-node-id", i);
4869695200aSShannon Zhao             mem_base += mem_len;
4879695200aSShannon Zhao             g_free(nodename);
4889695200aSShannon Zhao         }
4899695200aSShannon Zhao     } else {
490*b77257d7SGuenter Roeck         Error *err = NULL;
491*b77257d7SGuenter Roeck 
492*b77257d7SGuenter Roeck         rc = fdt_path_offset(fdt, "/memory");
493*b77257d7SGuenter Roeck         if (rc < 0) {
494*b77257d7SGuenter Roeck             qemu_fdt_add_subnode(fdt, "/memory");
495*b77257d7SGuenter Roeck         }
496*b77257d7SGuenter Roeck 
497*b77257d7SGuenter Roeck         if (!qemu_fdt_getprop(fdt, "/memory", "device_type", NULL, &err)) {
498*b77257d7SGuenter Roeck             qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory");
499*b77257d7SGuenter Roeck         }
500*b77257d7SGuenter Roeck 
5015a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_sized_cells(fdt, "/memory", "reg",
50270976c41SPeter Maydell                                           acells, binfo->loader_start,
50370976c41SPeter Maydell                                           scells, binfo->ram_size);
50453018216SPaolo Bonzini         if (rc < 0) {
50553018216SPaolo Bonzini             fprintf(stderr, "couldn't set /memory/reg\n");
506c23045deSPeter Maydell             goto fail;
50753018216SPaolo Bonzini         }
5089695200aSShannon Zhao     }
50953018216SPaolo Bonzini 
510*b77257d7SGuenter Roeck     rc = fdt_path_offset(fdt, "/chosen");
511*b77257d7SGuenter Roeck     if (rc < 0) {
512*b77257d7SGuenter Roeck         qemu_fdt_add_subnode(fdt, "/chosen");
513*b77257d7SGuenter Roeck     }
514*b77257d7SGuenter Roeck 
51553018216SPaolo Bonzini     if (binfo->kernel_cmdline && *binfo->kernel_cmdline) {
5165a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
51753018216SPaolo Bonzini                                      binfo->kernel_cmdline);
51853018216SPaolo Bonzini         if (rc < 0) {
51953018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/bootargs\n");
520c23045deSPeter Maydell             goto fail;
52153018216SPaolo Bonzini         }
52253018216SPaolo Bonzini     }
52353018216SPaolo Bonzini 
52453018216SPaolo Bonzini     if (binfo->initrd_size) {
5255a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
52653018216SPaolo Bonzini                                    binfo->initrd_start);
52753018216SPaolo Bonzini         if (rc < 0) {
52853018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
529c23045deSPeter Maydell             goto fail;
53053018216SPaolo Bonzini         }
53153018216SPaolo Bonzini 
5325a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
53353018216SPaolo Bonzini                                    binfo->initrd_start + binfo->initrd_size);
53453018216SPaolo Bonzini         if (rc < 0) {
53553018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
536c23045deSPeter Maydell             goto fail;
53753018216SPaolo Bonzini         }
53853018216SPaolo Bonzini     }
5393b1cceb8SPeter Maydell 
5403b1cceb8SPeter Maydell     if (binfo->modify_dtb) {
5413b1cceb8SPeter Maydell         binfo->modify_dtb(binfo, fdt);
5423b1cceb8SPeter Maydell     }
5433b1cceb8SPeter Maydell 
5445a4348d1SPeter Crosthwaite     qemu_fdt_dumpdtb(fdt, size);
54553018216SPaolo Bonzini 
5464c4bf654SArd Biesheuvel     /* Put the DTB into the memory map as a ROM image: this will ensure
5474c4bf654SArd Biesheuvel      * the DTB is copied again upon reset, even if addr points into RAM.
5484c4bf654SArd Biesheuvel      */
5494c4bf654SArd Biesheuvel     rom_add_blob_fixed("dtb", fdt, size, addr);
55053018216SPaolo Bonzini 
551c23045deSPeter Maydell     g_free(fdt);
552c23045deSPeter Maydell 
553fee8ea12SArd Biesheuvel     return size;
554c23045deSPeter Maydell 
555c23045deSPeter Maydell fail:
556c23045deSPeter Maydell     g_free(fdt);
557c23045deSPeter Maydell     return -1;
55853018216SPaolo Bonzini }
55953018216SPaolo Bonzini 
56053018216SPaolo Bonzini static void do_cpu_reset(void *opaque)
56153018216SPaolo Bonzini {
56253018216SPaolo Bonzini     ARMCPU *cpu = opaque;
5634df81c6eSPeter Crosthwaite     CPUState *cs = CPU(cpu);
56453018216SPaolo Bonzini     CPUARMState *env = &cpu->env;
56553018216SPaolo Bonzini     const struct arm_boot_info *info = env->boot_info;
56653018216SPaolo Bonzini 
5674df81c6eSPeter Crosthwaite     cpu_reset(cs);
56853018216SPaolo Bonzini     if (info) {
56953018216SPaolo Bonzini         if (!info->is_linux) {
5709776f636SPeter Crosthwaite             int i;
57153018216SPaolo Bonzini             /* Jump to the entry point.  */
5724df81c6eSPeter Crosthwaite             uint64_t entry = info->entry;
5734df81c6eSPeter Crosthwaite 
5749776f636SPeter Crosthwaite             switch (info->endianness) {
5759776f636SPeter Crosthwaite             case ARM_ENDIANNESS_LE:
5769776f636SPeter Crosthwaite                 env->cp15.sctlr_el[1] &= ~SCTLR_E0E;
5779776f636SPeter Crosthwaite                 for (i = 1; i < 4; ++i) {
5789776f636SPeter Crosthwaite                     env->cp15.sctlr_el[i] &= ~SCTLR_EE;
5799776f636SPeter Crosthwaite                 }
5809776f636SPeter Crosthwaite                 env->uncached_cpsr &= ~CPSR_E;
5819776f636SPeter Crosthwaite                 break;
5829776f636SPeter Crosthwaite             case ARM_ENDIANNESS_BE8:
5839776f636SPeter Crosthwaite                 env->cp15.sctlr_el[1] |= SCTLR_E0E;
5849776f636SPeter Crosthwaite                 for (i = 1; i < 4; ++i) {
5859776f636SPeter Crosthwaite                     env->cp15.sctlr_el[i] |= SCTLR_EE;
5869776f636SPeter Crosthwaite                 }
5879776f636SPeter Crosthwaite                 env->uncached_cpsr |= CPSR_E;
5889776f636SPeter Crosthwaite                 break;
5899776f636SPeter Crosthwaite             case ARM_ENDIANNESS_BE32:
5909776f636SPeter Crosthwaite                 env->cp15.sctlr_el[1] |= SCTLR_B;
5919776f636SPeter Crosthwaite                 break;
5929776f636SPeter Crosthwaite             case ARM_ENDIANNESS_UNKNOWN:
5939776f636SPeter Crosthwaite                 break; /* Board's decision */
5949776f636SPeter Crosthwaite             default:
5959776f636SPeter Crosthwaite                 g_assert_not_reached();
5969776f636SPeter Crosthwaite             }
5979776f636SPeter Crosthwaite 
5984df81c6eSPeter Crosthwaite             if (!env->aarch64) {
59953018216SPaolo Bonzini                 env->thumb = info->entry & 1;
6004df81c6eSPeter Crosthwaite                 entry &= 0xfffffffe;
601a9047ec3SPeter Maydell             }
6024df81c6eSPeter Crosthwaite             cpu_set_pc(cs, entry);
60353018216SPaolo Bonzini         } else {
604c8e829b7SGreg Bellows             /* If we are booting Linux then we need to check whether we are
605c8e829b7SGreg Bellows              * booting into secure or non-secure state and adjust the state
606c8e829b7SGreg Bellows              * accordingly.  Out of reset, ARM is defined to be in secure state
607c8e829b7SGreg Bellows              * (SCR.NS = 0), we change that here if non-secure boot has been
608c8e829b7SGreg Bellows              * requested.
609c8e829b7SGreg Bellows              */
6105097227cSGreg Bellows             if (arm_feature(env, ARM_FEATURE_EL3)) {
6115097227cSGreg Bellows                 /* AArch64 is defined to come out of reset into EL3 if enabled.
6125097227cSGreg Bellows                  * If we are booting Linux then we need to adjust our EL as
6135097227cSGreg Bellows                  * Linux expects us to be in EL2 or EL1.  AArch32 resets into
6145097227cSGreg Bellows                  * SVC, which Linux expects, so no privilege/exception level to
6155097227cSGreg Bellows                  * adjust.
6165097227cSGreg Bellows                  */
6175097227cSGreg Bellows                 if (env->aarch64) {
61848d21a57SEdgar E. Iglesias                     env->cp15.scr_el3 |= SCR_RW;
6195097227cSGreg Bellows                     if (arm_feature(env, ARM_FEATURE_EL2)) {
62048d21a57SEdgar E. Iglesias                         env->cp15.hcr_el2 |= HCR_RW;
6215097227cSGreg Bellows                         env->pstate = PSTATE_MODE_EL2h;
6225097227cSGreg Bellows                     } else {
6235097227cSGreg Bellows                         env->pstate = PSTATE_MODE_EL1h;
6245097227cSGreg Bellows                     }
6255097227cSGreg Bellows                 }
6265097227cSGreg Bellows 
6275097227cSGreg Bellows                 /* Set to non-secure if not a secure boot */
628baf6b681SPeter Crosthwaite                 if (!info->secure_boot &&
629baf6b681SPeter Crosthwaite                     (cs != first_cpu || !info->secure_board_setup)) {
6305097227cSGreg Bellows                     /* Linux expects non-secure state */
631c8e829b7SGreg Bellows                     env->cp15.scr_el3 |= SCR_NS;
632c8e829b7SGreg Bellows                 }
6335097227cSGreg Bellows             }
634c8e829b7SGreg Bellows 
6354df81c6eSPeter Crosthwaite             if (cs == first_cpu) {
6364df81c6eSPeter Crosthwaite                 cpu_set_pc(cs, info->loader_start);
6374d9ebf75SMian M. Hamayun 
63883bfffecSPeter Maydell                 if (!have_dtb(info)) {
63953018216SPaolo Bonzini                     if (old_param) {
64053018216SPaolo Bonzini                         set_kernel_args_old(info);
64153018216SPaolo Bonzini                     } else {
64253018216SPaolo Bonzini                         set_kernel_args(info);
64353018216SPaolo Bonzini                     }
64453018216SPaolo Bonzini                 }
64553018216SPaolo Bonzini             } else {
64653018216SPaolo Bonzini                 info->secondary_cpu_reset_hook(cpu, info);
64753018216SPaolo Bonzini             }
64853018216SPaolo Bonzini         }
64953018216SPaolo Bonzini     }
65053018216SPaolo Bonzini }
65153018216SPaolo Bonzini 
65207abe45cSLaszlo Ersek /**
65307abe45cSLaszlo Ersek  * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
65407abe45cSLaszlo Ersek  *                          by key.
65507abe45cSLaszlo Ersek  * @fw_cfg:         The firmware config instance to store the data in.
65607abe45cSLaszlo Ersek  * @size_key:       The firmware config key to store the size of the loaded
65707abe45cSLaszlo Ersek  *                  data under, with fw_cfg_add_i32().
65807abe45cSLaszlo Ersek  * @data_key:       The firmware config key to store the loaded data under,
65907abe45cSLaszlo Ersek  *                  with fw_cfg_add_bytes().
66007abe45cSLaszlo Ersek  * @image_name:     The name of the image file to load. If it is NULL, the
66107abe45cSLaszlo Ersek  *                  function returns without doing anything.
66207abe45cSLaszlo Ersek  * @try_decompress: Whether the image should be decompressed (gunzipped) before
66307abe45cSLaszlo Ersek  *                  adding it to fw_cfg. If decompression fails, the image is
66407abe45cSLaszlo Ersek  *                  loaded as-is.
66507abe45cSLaszlo Ersek  *
66607abe45cSLaszlo Ersek  * In case of failure, the function prints an error message to stderr and the
66707abe45cSLaszlo Ersek  * process exits with status 1.
66807abe45cSLaszlo Ersek  */
66907abe45cSLaszlo Ersek static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
67007abe45cSLaszlo Ersek                                  uint16_t data_key, const char *image_name,
67107abe45cSLaszlo Ersek                                  bool try_decompress)
67207abe45cSLaszlo Ersek {
67307abe45cSLaszlo Ersek     size_t size = -1;
67407abe45cSLaszlo Ersek     uint8_t *data;
67507abe45cSLaszlo Ersek 
67607abe45cSLaszlo Ersek     if (image_name == NULL) {
67707abe45cSLaszlo Ersek         return;
67807abe45cSLaszlo Ersek     }
67907abe45cSLaszlo Ersek 
68007abe45cSLaszlo Ersek     if (try_decompress) {
68107abe45cSLaszlo Ersek         size = load_image_gzipped_buffer(image_name,
68207abe45cSLaszlo Ersek                                          LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
68307abe45cSLaszlo Ersek     }
68407abe45cSLaszlo Ersek 
68507abe45cSLaszlo Ersek     if (size == (size_t)-1) {
68607abe45cSLaszlo Ersek         gchar *contents;
68707abe45cSLaszlo Ersek         gsize length;
68807abe45cSLaszlo Ersek 
68907abe45cSLaszlo Ersek         if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
69007abe45cSLaszlo Ersek             fprintf(stderr, "failed to load \"%s\"\n", image_name);
69107abe45cSLaszlo Ersek             exit(1);
69207abe45cSLaszlo Ersek         }
69307abe45cSLaszlo Ersek         size = length;
69407abe45cSLaszlo Ersek         data = (uint8_t *)contents;
69507abe45cSLaszlo Ersek     }
69607abe45cSLaszlo Ersek 
69707abe45cSLaszlo Ersek     fw_cfg_add_i32(fw_cfg, size_key, size);
69807abe45cSLaszlo Ersek     fw_cfg_add_bytes(fw_cfg, data_key, data, size);
69907abe45cSLaszlo Ersek }
70007abe45cSLaszlo Ersek 
701d8b1ae42SPeter Maydell static int do_arm_linux_init(Object *obj, void *opaque)
702d8b1ae42SPeter Maydell {
703d8b1ae42SPeter Maydell     if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
704d8b1ae42SPeter Maydell         ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj);
705d8b1ae42SPeter Maydell         ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj);
706d8b1ae42SPeter Maydell         struct arm_boot_info *info = opaque;
707d8b1ae42SPeter Maydell 
708d8b1ae42SPeter Maydell         if (albifc->arm_linux_init) {
709d8b1ae42SPeter Maydell             albifc->arm_linux_init(albif, info->secure_boot);
710d8b1ae42SPeter Maydell         }
711d8b1ae42SPeter Maydell     }
712d8b1ae42SPeter Maydell     return 0;
713d8b1ae42SPeter Maydell }
714d8b1ae42SPeter Maydell 
7159776f636SPeter Crosthwaite static uint64_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,
7169776f636SPeter Crosthwaite                              uint64_t *lowaddr, uint64_t *highaddr,
7179776f636SPeter Crosthwaite                              int elf_machine)
7189776f636SPeter Crosthwaite {
7199776f636SPeter Crosthwaite     bool elf_is64;
7209776f636SPeter Crosthwaite     union {
7219776f636SPeter Crosthwaite         Elf32_Ehdr h32;
7229776f636SPeter Crosthwaite         Elf64_Ehdr h64;
7239776f636SPeter Crosthwaite     } elf_header;
7249776f636SPeter Crosthwaite     int data_swab = 0;
7259776f636SPeter Crosthwaite     bool big_endian;
7269776f636SPeter Crosthwaite     uint64_t ret = -1;
7279776f636SPeter Crosthwaite     Error *err = NULL;
7289776f636SPeter Crosthwaite 
7299776f636SPeter Crosthwaite 
7309776f636SPeter Crosthwaite     load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err);
7319776f636SPeter Crosthwaite     if (err) {
7329776f636SPeter Crosthwaite         return ret;
7339776f636SPeter Crosthwaite     }
7349776f636SPeter Crosthwaite 
7359776f636SPeter Crosthwaite     if (elf_is64) {
7369776f636SPeter Crosthwaite         big_endian = elf_header.h64.e_ident[EI_DATA] == ELFDATA2MSB;
7379776f636SPeter Crosthwaite         info->endianness = big_endian ? ARM_ENDIANNESS_BE8
7389776f636SPeter Crosthwaite                                       : ARM_ENDIANNESS_LE;
7399776f636SPeter Crosthwaite     } else {
7409776f636SPeter Crosthwaite         big_endian = elf_header.h32.e_ident[EI_DATA] == ELFDATA2MSB;
7419776f636SPeter Crosthwaite         if (big_endian) {
7429776f636SPeter Crosthwaite             if (bswap32(elf_header.h32.e_flags) & EF_ARM_BE8) {
7439776f636SPeter Crosthwaite                 info->endianness = ARM_ENDIANNESS_BE8;
7449776f636SPeter Crosthwaite             } else {
7459776f636SPeter Crosthwaite                 info->endianness = ARM_ENDIANNESS_BE32;
7469776f636SPeter Crosthwaite                 /* In BE32, the CPU has a different view of the per-byte
7479776f636SPeter Crosthwaite                  * address map than the rest of the system. BE32 ELF files
7489776f636SPeter Crosthwaite                  * are organised such that they can be programmed through
7499776f636SPeter Crosthwaite                  * the CPU's per-word byte-reversed view of the world. QEMU
7509776f636SPeter Crosthwaite                  * however loads ELF files independently of the CPU. So
7519776f636SPeter Crosthwaite                  * tell the ELF loader to byte reverse the data for us.
7529776f636SPeter Crosthwaite                  */
7539776f636SPeter Crosthwaite                 data_swab = 2;
7549776f636SPeter Crosthwaite             }
7559776f636SPeter Crosthwaite         } else {
7569776f636SPeter Crosthwaite             info->endianness = ARM_ENDIANNESS_LE;
7579776f636SPeter Crosthwaite         }
7589776f636SPeter Crosthwaite     }
7599776f636SPeter Crosthwaite 
7609776f636SPeter Crosthwaite     ret = load_elf(info->kernel_filename, NULL, NULL,
7619776f636SPeter Crosthwaite                    pentry, lowaddr, highaddr, big_endian, elf_machine,
7629776f636SPeter Crosthwaite                    1, data_swab);
7639776f636SPeter Crosthwaite     if (ret <= 0) {
7649776f636SPeter Crosthwaite         /* The header loaded but the image didn't */
7659776f636SPeter Crosthwaite         exit(1);
7669776f636SPeter Crosthwaite     }
7679776f636SPeter Crosthwaite 
7689776f636SPeter Crosthwaite     return ret;
7699776f636SPeter Crosthwaite }
7709776f636SPeter Crosthwaite 
771ac9d32e3SEric Auger static void arm_load_kernel_notify(Notifier *notifier, void *data)
77253018216SPaolo Bonzini {
773c6faa758SArd Biesheuvel     CPUState *cs;
77453018216SPaolo Bonzini     int kernel_size;
77553018216SPaolo Bonzini     int initrd_size;
77653018216SPaolo Bonzini     int is_linux = 0;
77792df8450SArd Biesheuvel     uint64_t elf_entry, elf_low_addr, elf_high_addr;
778da0af40dSPeter Maydell     int elf_machine;
7794d9ebf75SMian M. Hamayun     hwaddr entry, kernel_load_offset;
7804d9ebf75SMian M. Hamayun     static const ARMInsnFixup *primary_loader;
781ac9d32e3SEric Auger     ArmLoadKernelNotifier *n = DO_UPCAST(ArmLoadKernelNotifier,
782ac9d32e3SEric Auger                                          notifier, notifier);
783ac9d32e3SEric Auger     ARMCPU *cpu = n->cpu;
784ac9d32e3SEric Auger     struct arm_boot_info *info =
785ac9d32e3SEric Auger         container_of(n, struct arm_boot_info, load_kernel_notifier);
78653018216SPaolo Bonzini 
787baf6b681SPeter Crosthwaite     /* The board code is not supposed to set secure_board_setup unless
788baf6b681SPeter Crosthwaite      * running its code in secure mode is actually possible, and KVM
789baf6b681SPeter Crosthwaite      * doesn't support secure.
790baf6b681SPeter Crosthwaite      */
791baf6b681SPeter Crosthwaite     assert(!(info->secure_board_setup && kvm_enabled()));
792baf6b681SPeter Crosthwaite 
7934c8afda7SMichael Olbrich     info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb");
7944c8afda7SMichael Olbrich 
79553018216SPaolo Bonzini     /* Load the kernel.  */
79607abe45cSLaszlo Ersek     if (!info->kernel_filename || info->firmware_loaded) {
79769e7f76fSArd Biesheuvel 
79869e7f76fSArd Biesheuvel         if (have_dtb(info)) {
79907abe45cSLaszlo Ersek             /* If we have a device tree blob, but no kernel to supply it to (or
80007abe45cSLaszlo Ersek              * the kernel is supposed to be loaded by the bootloader), copy the
80107abe45cSLaszlo Ersek              * DTB to the base of RAM for the bootloader to pick up.
80269e7f76fSArd Biesheuvel              */
80369e7f76fSArd Biesheuvel             if (load_dtb(info->loader_start, info, 0) < 0) {
80469e7f76fSArd Biesheuvel                 exit(1);
80569e7f76fSArd Biesheuvel             }
80669e7f76fSArd Biesheuvel         }
80769e7f76fSArd Biesheuvel 
80807abe45cSLaszlo Ersek         if (info->kernel_filename) {
80907abe45cSLaszlo Ersek             FWCfgState *fw_cfg;
81007abe45cSLaszlo Ersek             bool try_decompressing_kernel;
81107abe45cSLaszlo Ersek 
81207abe45cSLaszlo Ersek             fw_cfg = fw_cfg_find();
81307abe45cSLaszlo Ersek             try_decompressing_kernel = arm_feature(&cpu->env,
81407abe45cSLaszlo Ersek                                                    ARM_FEATURE_AARCH64);
81507abe45cSLaszlo Ersek 
81607abe45cSLaszlo Ersek             /* Expose the kernel, the command line, and the initrd in fw_cfg.
81707abe45cSLaszlo Ersek              * We don't process them here at all, it's all left to the
81807abe45cSLaszlo Ersek              * firmware.
81907abe45cSLaszlo Ersek              */
82007abe45cSLaszlo Ersek             load_image_to_fw_cfg(fw_cfg,
82107abe45cSLaszlo Ersek                                  FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
82207abe45cSLaszlo Ersek                                  info->kernel_filename,
82307abe45cSLaszlo Ersek                                  try_decompressing_kernel);
82407abe45cSLaszlo Ersek             load_image_to_fw_cfg(fw_cfg,
82507abe45cSLaszlo Ersek                                  FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
82607abe45cSLaszlo Ersek                                  info->initrd_filename, false);
82707abe45cSLaszlo Ersek 
82807abe45cSLaszlo Ersek             if (info->kernel_cmdline) {
82907abe45cSLaszlo Ersek                 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
83007abe45cSLaszlo Ersek                                strlen(info->kernel_cmdline) + 1);
83107abe45cSLaszlo Ersek                 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
83207abe45cSLaszlo Ersek                                   info->kernel_cmdline);
83307abe45cSLaszlo Ersek             }
83407abe45cSLaszlo Ersek         }
83507abe45cSLaszlo Ersek 
83607abe45cSLaszlo Ersek         /* We will start from address 0 (typically a boot ROM image) in the
83707abe45cSLaszlo Ersek          * same way as hardware.
8389546dbabSPeter Maydell          */
8399546dbabSPeter Maydell         return;
84053018216SPaolo Bonzini     }
84153018216SPaolo Bonzini 
8424d9ebf75SMian M. Hamayun     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
8434d9ebf75SMian M. Hamayun         primary_loader = bootloader_aarch64;
8444d9ebf75SMian M. Hamayun         kernel_load_offset = KERNEL64_LOAD_ADDR;
845da0af40dSPeter Maydell         elf_machine = EM_AARCH64;
8464d9ebf75SMian M. Hamayun     } else {
8474d9ebf75SMian M. Hamayun         primary_loader = bootloader;
84810b8ec73SPeter Crosthwaite         if (!info->write_board_setup) {
84910b8ec73SPeter Crosthwaite             primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET;
85010b8ec73SPeter Crosthwaite         }
8514d9ebf75SMian M. Hamayun         kernel_load_offset = KERNEL_LOAD_ADDR;
852da0af40dSPeter Maydell         elf_machine = EM_ARM;
8534d9ebf75SMian M. Hamayun     }
8544d9ebf75SMian M. Hamayun 
85553018216SPaolo Bonzini     if (!info->secondary_cpu_reset_hook) {
85653018216SPaolo Bonzini         info->secondary_cpu_reset_hook = default_reset_secondary;
85753018216SPaolo Bonzini     }
85853018216SPaolo Bonzini     if (!info->write_secondary_boot) {
85953018216SPaolo Bonzini         info->write_secondary_boot = default_write_secondary;
86053018216SPaolo Bonzini     }
86153018216SPaolo Bonzini 
86253018216SPaolo Bonzini     if (info->nb_cpus == 0)
86353018216SPaolo Bonzini         info->nb_cpus = 1;
86453018216SPaolo Bonzini 
86553018216SPaolo Bonzini     /* We want to put the initrd far enough into RAM that when the
86653018216SPaolo Bonzini      * kernel is uncompressed it will not clobber the initrd. However
86753018216SPaolo Bonzini      * on boards without much RAM we must ensure that we still leave
86853018216SPaolo Bonzini      * enough room for a decent sized initrd, and on boards with large
86953018216SPaolo Bonzini      * amounts of RAM we must avoid the initrd being so far up in RAM
87053018216SPaolo Bonzini      * that it is outside lowmem and inaccessible to the kernel.
87153018216SPaolo Bonzini      * So for boards with less  than 256MB of RAM we put the initrd
87253018216SPaolo Bonzini      * halfway into RAM, and for boards with 256MB of RAM or more we put
87353018216SPaolo Bonzini      * the initrd at 128MB.
87453018216SPaolo Bonzini      */
87553018216SPaolo Bonzini     info->initrd_start = info->loader_start +
87653018216SPaolo Bonzini         MIN(info->ram_size / 2, 128 * 1024 * 1024);
87753018216SPaolo Bonzini 
87853018216SPaolo Bonzini     /* Assume that raw images are linux kernels, and ELF images are not.  */
8799776f636SPeter Crosthwaite     kernel_size = arm_load_elf(info, &elf_entry, &elf_low_addr,
8809776f636SPeter Crosthwaite                                &elf_high_addr, elf_machine);
88192df8450SArd Biesheuvel     if (kernel_size > 0 && have_dtb(info)) {
88292df8450SArd Biesheuvel         /* If there is still some room left at the base of RAM, try and put
88392df8450SArd Biesheuvel          * the DTB there like we do for images loaded with -bios or -pflash.
88492df8450SArd Biesheuvel          */
88592df8450SArd Biesheuvel         if (elf_low_addr > info->loader_start
88692df8450SArd Biesheuvel             || elf_high_addr < info->loader_start) {
88792df8450SArd Biesheuvel             /* Pass elf_low_addr as address limit to load_dtb if it may be
88892df8450SArd Biesheuvel              * pointing into RAM, otherwise pass '0' (no limit)
88992df8450SArd Biesheuvel              */
89092df8450SArd Biesheuvel             if (elf_low_addr < info->loader_start) {
89192df8450SArd Biesheuvel                 elf_low_addr = 0;
89292df8450SArd Biesheuvel             }
89392df8450SArd Biesheuvel             if (load_dtb(info->loader_start, info, elf_low_addr) < 0) {
89492df8450SArd Biesheuvel                 exit(1);
89592df8450SArd Biesheuvel             }
89692df8450SArd Biesheuvel         }
89792df8450SArd Biesheuvel     }
89853018216SPaolo Bonzini     entry = elf_entry;
89953018216SPaolo Bonzini     if (kernel_size < 0) {
90053018216SPaolo Bonzini         kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
90125bda50aSMax Filippov                                   &is_linux, NULL, NULL);
90253018216SPaolo Bonzini     }
9036f5d3cbeSRichard W.M. Jones     /* On aarch64, it's the bootloader's job to uncompress the kernel. */
9046f5d3cbeSRichard W.M. Jones     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
9056f5d3cbeSRichard W.M. Jones         entry = info->loader_start + kernel_load_offset;
9066f5d3cbeSRichard W.M. Jones         kernel_size = load_image_gzipped(info->kernel_filename, entry,
9076f5d3cbeSRichard W.M. Jones                                          info->ram_size - kernel_load_offset);
9086f5d3cbeSRichard W.M. Jones         is_linux = 1;
9096f5d3cbeSRichard W.M. Jones     }
91053018216SPaolo Bonzini     if (kernel_size < 0) {
9114d9ebf75SMian M. Hamayun         entry = info->loader_start + kernel_load_offset;
91253018216SPaolo Bonzini         kernel_size = load_image_targphys(info->kernel_filename, entry,
9134d9ebf75SMian M. Hamayun                                           info->ram_size - kernel_load_offset);
91453018216SPaolo Bonzini         is_linux = 1;
91553018216SPaolo Bonzini     }
91653018216SPaolo Bonzini     if (kernel_size < 0) {
91753018216SPaolo Bonzini         fprintf(stderr, "qemu: could not load kernel '%s'\n",
91853018216SPaolo Bonzini                 info->kernel_filename);
91953018216SPaolo Bonzini         exit(1);
92053018216SPaolo Bonzini     }
92153018216SPaolo Bonzini     info->entry = entry;
92253018216SPaolo Bonzini     if (is_linux) {
92347b1da81SPeter Maydell         uint32_t fixupcontext[FIXUP_MAX];
92447b1da81SPeter Maydell 
92553018216SPaolo Bonzini         if (info->initrd_filename) {
926fd76663eSSoren Brinkmann             initrd_size = load_ramdisk(info->initrd_filename,
927fd76663eSSoren Brinkmann                                        info->initrd_start,
928fd76663eSSoren Brinkmann                                        info->ram_size -
929fd76663eSSoren Brinkmann                                        info->initrd_start);
930fd76663eSSoren Brinkmann             if (initrd_size < 0) {
93153018216SPaolo Bonzini                 initrd_size = load_image_targphys(info->initrd_filename,
93253018216SPaolo Bonzini                                                   info->initrd_start,
93353018216SPaolo Bonzini                                                   info->ram_size -
93453018216SPaolo Bonzini                                                   info->initrd_start);
935fd76663eSSoren Brinkmann             }
93653018216SPaolo Bonzini             if (initrd_size < 0) {
93753018216SPaolo Bonzini                 fprintf(stderr, "qemu: could not load initrd '%s'\n",
93853018216SPaolo Bonzini                         info->initrd_filename);
93953018216SPaolo Bonzini                 exit(1);
94053018216SPaolo Bonzini             }
94153018216SPaolo Bonzini         } else {
94253018216SPaolo Bonzini             initrd_size = 0;
94353018216SPaolo Bonzini         }
94453018216SPaolo Bonzini         info->initrd_size = initrd_size;
94553018216SPaolo Bonzini 
94647b1da81SPeter Maydell         fixupcontext[FIXUP_BOARDID] = info->board_id;
94710b8ec73SPeter Crosthwaite         fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr;
94853018216SPaolo Bonzini 
94953018216SPaolo Bonzini         /* for device tree boot, we pass the DTB directly in r2. Otherwise
95053018216SPaolo Bonzini          * we point to the kernel args.
95153018216SPaolo Bonzini          */
95283bfffecSPeter Maydell         if (have_dtb(info)) {
95376e2aef3SAlexander Graf             hwaddr align;
95476e2aef3SAlexander Graf             hwaddr dtb_start;
95576e2aef3SAlexander Graf 
95676e2aef3SAlexander Graf             if (elf_machine == EM_AARCH64) {
95776e2aef3SAlexander Graf                 /*
95876e2aef3SAlexander Graf                  * Some AArch64 kernels on early bootup map the fdt region as
95976e2aef3SAlexander Graf                  *
96076e2aef3SAlexander Graf                  *   [ ALIGN_DOWN(fdt, 2MB) ... ALIGN_DOWN(fdt, 2MB) + 2MB ]
96176e2aef3SAlexander Graf                  *
96276e2aef3SAlexander Graf                  * Let's play safe and prealign it to 2MB to give us some space.
96353018216SPaolo Bonzini                  */
96476e2aef3SAlexander Graf                 align = 2 * 1024 * 1024;
96576e2aef3SAlexander Graf             } else {
96676e2aef3SAlexander Graf                 /*
96776e2aef3SAlexander Graf                  * Some 32bit kernels will trash anything in the 4K page the
96876e2aef3SAlexander Graf                  * initrd ends in, so make sure the DTB isn't caught up in that.
96976e2aef3SAlexander Graf                  */
97076e2aef3SAlexander Graf                 align = 4096;
97176e2aef3SAlexander Graf             }
97276e2aef3SAlexander Graf 
97376e2aef3SAlexander Graf             /* Place the DTB after the initrd in memory with alignment. */
97476e2aef3SAlexander Graf             dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size, align);
975fee8ea12SArd Biesheuvel             if (load_dtb(dtb_start, info, 0) < 0) {
97653018216SPaolo Bonzini                 exit(1);
97753018216SPaolo Bonzini             }
97847b1da81SPeter Maydell             fixupcontext[FIXUP_ARGPTR] = dtb_start;
97953018216SPaolo Bonzini         } else {
98047b1da81SPeter Maydell             fixupcontext[FIXUP_ARGPTR] = info->loader_start + KERNEL_ARGS_ADDR;
98153018216SPaolo Bonzini             if (info->ram_size >= (1ULL << 32)) {
98253018216SPaolo Bonzini                 fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
98353018216SPaolo Bonzini                         " Linux kernel using ATAGS (try passing a device tree"
98453018216SPaolo Bonzini                         " using -dtb)\n");
98553018216SPaolo Bonzini                 exit(1);
98653018216SPaolo Bonzini             }
98753018216SPaolo Bonzini         }
98847b1da81SPeter Maydell         fixupcontext[FIXUP_ENTRYPOINT] = entry;
98947b1da81SPeter Maydell 
99047b1da81SPeter Maydell         write_bootloader("bootloader", info->loader_start,
9914d9ebf75SMian M. Hamayun                          primary_loader, fixupcontext);
99247b1da81SPeter Maydell 
99353018216SPaolo Bonzini         if (info->nb_cpus > 1) {
99453018216SPaolo Bonzini             info->write_secondary_boot(cpu, info);
99553018216SPaolo Bonzini         }
99610b8ec73SPeter Crosthwaite         if (info->write_board_setup) {
99710b8ec73SPeter Crosthwaite             info->write_board_setup(cpu, info);
99810b8ec73SPeter Crosthwaite         }
999d8b1ae42SPeter Maydell 
1000d8b1ae42SPeter Maydell         /* Notify devices which need to fake up firmware initialization
1001d8b1ae42SPeter Maydell          * that we're doing a direct kernel boot.
1002d8b1ae42SPeter Maydell          */
1003d8b1ae42SPeter Maydell         object_child_foreach_recursive(object_get_root(),
1004d8b1ae42SPeter Maydell                                        do_arm_linux_init, info);
100553018216SPaolo Bonzini     }
100653018216SPaolo Bonzini     info->is_linux = is_linux;
100753018216SPaolo Bonzini 
1008c6faa758SArd Biesheuvel     for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
1009c6faa758SArd Biesheuvel         ARM_CPU(cs)->env.boot_info = info;
101053018216SPaolo Bonzini     }
101153018216SPaolo Bonzini }
1012ac9d32e3SEric Auger 
1013ac9d32e3SEric Auger void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
1014ac9d32e3SEric Auger {
101563a183edSEric Auger     CPUState *cs;
101663a183edSEric Auger 
1017ac9d32e3SEric Auger     info->load_kernel_notifier.cpu = cpu;
1018ac9d32e3SEric Auger     info->load_kernel_notifier.notifier.notify = arm_load_kernel_notify;
1019ac9d32e3SEric Auger     qemu_add_machine_init_done_notifier(&info->load_kernel_notifier.notifier);
102063a183edSEric Auger 
102163a183edSEric Auger     /* CPU objects (unlike devices) are not automatically reset on system
102263a183edSEric Auger      * reset, so we must always register a handler to do so. If we're
102363a183edSEric Auger      * actually loading a kernel, the handler is also responsible for
102463a183edSEric Auger      * arranging that we start it correctly.
102563a183edSEric Auger      */
102663a183edSEric Auger     for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
102763a183edSEric Auger         qemu_register_reset(do_cpu_reset, ARM_CPU(cs));
102863a183edSEric Auger     }
1029ac9d32e3SEric Auger }
1030d8b1ae42SPeter Maydell 
1031d8b1ae42SPeter Maydell static const TypeInfo arm_linux_boot_if_info = {
1032d8b1ae42SPeter Maydell     .name = TYPE_ARM_LINUX_BOOT_IF,
1033d8b1ae42SPeter Maydell     .parent = TYPE_INTERFACE,
1034d8b1ae42SPeter Maydell     .class_size = sizeof(ARMLinuxBootIfClass),
1035d8b1ae42SPeter Maydell };
1036d8b1ae42SPeter Maydell 
1037d8b1ae42SPeter Maydell static void arm_linux_boot_register_types(void)
1038d8b1ae42SPeter Maydell {
1039d8b1ae42SPeter Maydell     type_register_static(&arm_linux_boot_if_info);
1040d8b1ae42SPeter Maydell }
1041d8b1ae42SPeter Maydell 
1042d8b1ae42SPeter Maydell type_init(arm_linux_boot_register_types)
1043