xref: /openbmc/qemu/hw/arm/boot.c (revision 0fb79851c3dffa06de648d955ee2f2b47bfb96ce)
153018216SPaolo Bonzini /*
253018216SPaolo Bonzini  * ARM kernel loader.
353018216SPaolo Bonzini  *
453018216SPaolo Bonzini  * Copyright (c) 2006-2007 CodeSourcery.
553018216SPaolo Bonzini  * Written by Paul Brook
653018216SPaolo Bonzini  *
753018216SPaolo Bonzini  * This code is licensed under the GPL.
853018216SPaolo Bonzini  */
953018216SPaolo Bonzini 
1053018216SPaolo Bonzini #include "config.h"
1153018216SPaolo Bonzini #include "hw/hw.h"
12bd2be150SPeter Maydell #include "hw/arm/arm.h"
1353018216SPaolo Bonzini #include "sysemu/sysemu.h"
1453018216SPaolo Bonzini #include "hw/boards.h"
1553018216SPaolo Bonzini #include "hw/loader.h"
1653018216SPaolo Bonzini #include "elf.h"
1753018216SPaolo Bonzini #include "sysemu/device_tree.h"
1853018216SPaolo Bonzini #include "qemu/config-file.h"
1953018216SPaolo Bonzini 
2053018216SPaolo Bonzini #define KERNEL_ARGS_ADDR 0x100
2153018216SPaolo Bonzini #define KERNEL_LOAD_ADDR 0x00010000
2253018216SPaolo Bonzini 
2353018216SPaolo Bonzini /* The worlds second smallest bootloader.  Set r0-r2, then jump to kernel.  */
2453018216SPaolo Bonzini static uint32_t bootloader[] = {
2553018216SPaolo Bonzini   0xe3a00000, /* mov     r0, #0 */
2653018216SPaolo Bonzini   0xe59f1004, /* ldr     r1, [pc, #4] */
2753018216SPaolo Bonzini   0xe59f2004, /* ldr     r2, [pc, #4] */
2853018216SPaolo Bonzini   0xe59ff004, /* ldr     pc, [pc, #4] */
2953018216SPaolo Bonzini   0, /* Board ID */
3053018216SPaolo Bonzini   0, /* Address of kernel args.  Set by integratorcp_init.  */
3153018216SPaolo Bonzini   0  /* Kernel entry point.  Set by integratorcp_init.  */
3253018216SPaolo Bonzini };
3353018216SPaolo Bonzini 
3453018216SPaolo Bonzini /* Handling for secondary CPU boot in a multicore system.
3553018216SPaolo Bonzini  * Unlike the uniprocessor/primary CPU boot, this is platform
3653018216SPaolo Bonzini  * dependent. The default code here is based on the secondary
3753018216SPaolo Bonzini  * CPU boot protocol used on realview/vexpress boards, with
3853018216SPaolo Bonzini  * some parameterisation to increase its flexibility.
3953018216SPaolo Bonzini  * QEMU platform models for which this code is not appropriate
4053018216SPaolo Bonzini  * should override write_secondary_boot and secondary_cpu_reset_hook
4153018216SPaolo Bonzini  * instead.
4253018216SPaolo Bonzini  *
4353018216SPaolo Bonzini  * This code enables the interrupt controllers for the secondary
4453018216SPaolo Bonzini  * CPUs and then puts all the secondary CPUs into a loop waiting
4553018216SPaolo Bonzini  * for an interprocessor interrupt and polling a configurable
4653018216SPaolo Bonzini  * location for the kernel secondary CPU entry point.
4753018216SPaolo Bonzini  */
4853018216SPaolo Bonzini #define DSB_INSN 0xf57ff04f
4953018216SPaolo Bonzini #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
5053018216SPaolo Bonzini 
5153018216SPaolo Bonzini static uint32_t smpboot[] = {
5253018216SPaolo Bonzini   0xe59f2028, /* ldr r2, gic_cpu_if */
5353018216SPaolo Bonzini   0xe59f0028, /* ldr r0, startaddr */
5453018216SPaolo Bonzini   0xe3a01001, /* mov r1, #1 */
5553018216SPaolo Bonzini   0xe5821000, /* str r1, [r2] - set GICC_CTLR.Enable */
5653018216SPaolo Bonzini   0xe3a010ff, /* mov r1, #0xff */
5753018216SPaolo Bonzini   0xe5821004, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
5853018216SPaolo Bonzini   DSB_INSN,   /* dsb */
5953018216SPaolo Bonzini   0xe320f003, /* wfi */
6053018216SPaolo Bonzini   0xe5901000, /* ldr     r1, [r0] */
6153018216SPaolo Bonzini   0xe1110001, /* tst     r1, r1 */
6253018216SPaolo Bonzini   0x0afffffb, /* beq     <wfi> */
6353018216SPaolo Bonzini   0xe12fff11, /* bx      r1 */
6453018216SPaolo Bonzini   0,          /* gic_cpu_if: base address of GIC CPU interface */
6553018216SPaolo Bonzini   0           /* bootreg: Boot register address is held here */
6653018216SPaolo Bonzini };
6753018216SPaolo Bonzini 
6853018216SPaolo Bonzini static void default_write_secondary(ARMCPU *cpu,
6953018216SPaolo Bonzini                                     const struct arm_boot_info *info)
7053018216SPaolo Bonzini {
7153018216SPaolo Bonzini     int n;
7253018216SPaolo Bonzini     smpboot[ARRAY_SIZE(smpboot) - 1] = info->smp_bootreg_addr;
7353018216SPaolo Bonzini     smpboot[ARRAY_SIZE(smpboot) - 2] = info->gic_cpu_if_addr;
7453018216SPaolo Bonzini     for (n = 0; n < ARRAY_SIZE(smpboot); n++) {
7553018216SPaolo Bonzini         /* Replace DSB with the pre-v7 DSB if necessary. */
7653018216SPaolo Bonzini         if (!arm_feature(&cpu->env, ARM_FEATURE_V7) &&
7753018216SPaolo Bonzini             smpboot[n] == DSB_INSN) {
7853018216SPaolo Bonzini             smpboot[n] = CP15_DSB_INSN;
7953018216SPaolo Bonzini         }
8053018216SPaolo Bonzini         smpboot[n] = tswap32(smpboot[n]);
8153018216SPaolo Bonzini     }
8253018216SPaolo Bonzini     rom_add_blob_fixed("smpboot", smpboot, sizeof(smpboot),
8353018216SPaolo Bonzini                        info->smp_loader_start);
8453018216SPaolo Bonzini }
8553018216SPaolo Bonzini 
8653018216SPaolo Bonzini static void default_reset_secondary(ARMCPU *cpu,
8753018216SPaolo Bonzini                                     const struct arm_boot_info *info)
8853018216SPaolo Bonzini {
8953018216SPaolo Bonzini     CPUARMState *env = &cpu->env;
9053018216SPaolo Bonzini 
9153018216SPaolo Bonzini     stl_phys_notdirty(info->smp_bootreg_addr, 0);
9253018216SPaolo Bonzini     env->regs[15] = info->smp_loader_start;
9353018216SPaolo Bonzini }
9453018216SPaolo Bonzini 
9553018216SPaolo Bonzini #define WRITE_WORD(p, value) do { \
9653018216SPaolo Bonzini     stl_phys_notdirty(p, value);  \
9753018216SPaolo Bonzini     p += 4;                       \
9853018216SPaolo Bonzini } while (0)
9953018216SPaolo Bonzini 
10053018216SPaolo Bonzini static void set_kernel_args(const struct arm_boot_info *info)
10153018216SPaolo Bonzini {
10253018216SPaolo Bonzini     int initrd_size = info->initrd_size;
10353018216SPaolo Bonzini     hwaddr base = info->loader_start;
10453018216SPaolo Bonzini     hwaddr p;
10553018216SPaolo Bonzini 
10653018216SPaolo Bonzini     p = base + KERNEL_ARGS_ADDR;
10753018216SPaolo Bonzini     /* ATAG_CORE */
10853018216SPaolo Bonzini     WRITE_WORD(p, 5);
10953018216SPaolo Bonzini     WRITE_WORD(p, 0x54410001);
11053018216SPaolo Bonzini     WRITE_WORD(p, 1);
11153018216SPaolo Bonzini     WRITE_WORD(p, 0x1000);
11253018216SPaolo Bonzini     WRITE_WORD(p, 0);
11353018216SPaolo Bonzini     /* ATAG_MEM */
11453018216SPaolo Bonzini     /* TODO: handle multiple chips on one ATAG list */
11553018216SPaolo Bonzini     WRITE_WORD(p, 4);
11653018216SPaolo Bonzini     WRITE_WORD(p, 0x54410002);
11753018216SPaolo Bonzini     WRITE_WORD(p, info->ram_size);
11853018216SPaolo Bonzini     WRITE_WORD(p, info->loader_start);
11953018216SPaolo Bonzini     if (initrd_size) {
12053018216SPaolo Bonzini         /* ATAG_INITRD2 */
12153018216SPaolo Bonzini         WRITE_WORD(p, 4);
12253018216SPaolo Bonzini         WRITE_WORD(p, 0x54420005);
12353018216SPaolo Bonzini         WRITE_WORD(p, info->initrd_start);
12453018216SPaolo Bonzini         WRITE_WORD(p, initrd_size);
12553018216SPaolo Bonzini     }
12653018216SPaolo Bonzini     if (info->kernel_cmdline && *info->kernel_cmdline) {
12753018216SPaolo Bonzini         /* ATAG_CMDLINE */
12853018216SPaolo Bonzini         int cmdline_size;
12953018216SPaolo Bonzini 
13053018216SPaolo Bonzini         cmdline_size = strlen(info->kernel_cmdline);
131e1fe50dcSStefan Weil         cpu_physical_memory_write(p + 8, info->kernel_cmdline,
13253018216SPaolo Bonzini                                   cmdline_size + 1);
13353018216SPaolo Bonzini         cmdline_size = (cmdline_size >> 2) + 1;
13453018216SPaolo Bonzini         WRITE_WORD(p, cmdline_size + 2);
13553018216SPaolo Bonzini         WRITE_WORD(p, 0x54410009);
13653018216SPaolo Bonzini         p += cmdline_size * 4;
13753018216SPaolo Bonzini     }
13853018216SPaolo Bonzini     if (info->atag_board) {
13953018216SPaolo Bonzini         /* ATAG_BOARD */
14053018216SPaolo Bonzini         int atag_board_len;
14153018216SPaolo Bonzini         uint8_t atag_board_buf[0x1000];
14253018216SPaolo Bonzini 
14353018216SPaolo Bonzini         atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
14453018216SPaolo Bonzini         WRITE_WORD(p, (atag_board_len + 8) >> 2);
14553018216SPaolo Bonzini         WRITE_WORD(p, 0x414f4d50);
14653018216SPaolo Bonzini         cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
14753018216SPaolo Bonzini         p += atag_board_len;
14853018216SPaolo Bonzini     }
14953018216SPaolo Bonzini     /* ATAG_END */
15053018216SPaolo Bonzini     WRITE_WORD(p, 0);
15153018216SPaolo Bonzini     WRITE_WORD(p, 0);
15253018216SPaolo Bonzini }
15353018216SPaolo Bonzini 
15453018216SPaolo Bonzini static void set_kernel_args_old(const struct arm_boot_info *info)
15553018216SPaolo Bonzini {
15653018216SPaolo Bonzini     hwaddr p;
15753018216SPaolo Bonzini     const char *s;
15853018216SPaolo Bonzini     int initrd_size = info->initrd_size;
15953018216SPaolo Bonzini     hwaddr base = info->loader_start;
16053018216SPaolo Bonzini 
16153018216SPaolo Bonzini     /* see linux/include/asm-arm/setup.h */
16253018216SPaolo Bonzini     p = base + KERNEL_ARGS_ADDR;
16353018216SPaolo Bonzini     /* page_size */
16453018216SPaolo Bonzini     WRITE_WORD(p, 4096);
16553018216SPaolo Bonzini     /* nr_pages */
16653018216SPaolo Bonzini     WRITE_WORD(p, info->ram_size / 4096);
16753018216SPaolo Bonzini     /* ramdisk_size */
16853018216SPaolo Bonzini     WRITE_WORD(p, 0);
16953018216SPaolo Bonzini #define FLAG_READONLY	1
17053018216SPaolo Bonzini #define FLAG_RDLOAD	4
17153018216SPaolo Bonzini #define FLAG_RDPROMPT	8
17253018216SPaolo Bonzini     /* flags */
17353018216SPaolo Bonzini     WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
17453018216SPaolo Bonzini     /* rootdev */
17553018216SPaolo Bonzini     WRITE_WORD(p, (31 << 8) | 0);	/* /dev/mtdblock0 */
17653018216SPaolo Bonzini     /* video_num_cols */
17753018216SPaolo Bonzini     WRITE_WORD(p, 0);
17853018216SPaolo Bonzini     /* video_num_rows */
17953018216SPaolo Bonzini     WRITE_WORD(p, 0);
18053018216SPaolo Bonzini     /* video_x */
18153018216SPaolo Bonzini     WRITE_WORD(p, 0);
18253018216SPaolo Bonzini     /* video_y */
18353018216SPaolo Bonzini     WRITE_WORD(p, 0);
18453018216SPaolo Bonzini     /* memc_control_reg */
18553018216SPaolo Bonzini     WRITE_WORD(p, 0);
18653018216SPaolo Bonzini     /* unsigned char sounddefault */
18753018216SPaolo Bonzini     /* unsigned char adfsdrives */
18853018216SPaolo Bonzini     /* unsigned char bytes_per_char_h */
18953018216SPaolo Bonzini     /* unsigned char bytes_per_char_v */
19053018216SPaolo Bonzini     WRITE_WORD(p, 0);
19153018216SPaolo Bonzini     /* pages_in_bank[4] */
19253018216SPaolo Bonzini     WRITE_WORD(p, 0);
19353018216SPaolo Bonzini     WRITE_WORD(p, 0);
19453018216SPaolo Bonzini     WRITE_WORD(p, 0);
19553018216SPaolo Bonzini     WRITE_WORD(p, 0);
19653018216SPaolo Bonzini     /* pages_in_vram */
19753018216SPaolo Bonzini     WRITE_WORD(p, 0);
19853018216SPaolo Bonzini     /* initrd_start */
19953018216SPaolo Bonzini     if (initrd_size) {
20053018216SPaolo Bonzini         WRITE_WORD(p, info->initrd_start);
20153018216SPaolo Bonzini     } else {
20253018216SPaolo Bonzini         WRITE_WORD(p, 0);
20353018216SPaolo Bonzini     }
20453018216SPaolo Bonzini     /* initrd_size */
20553018216SPaolo Bonzini     WRITE_WORD(p, initrd_size);
20653018216SPaolo Bonzini     /* rd_start */
20753018216SPaolo Bonzini     WRITE_WORD(p, 0);
20853018216SPaolo Bonzini     /* system_rev */
20953018216SPaolo Bonzini     WRITE_WORD(p, 0);
21053018216SPaolo Bonzini     /* system_serial_low */
21153018216SPaolo Bonzini     WRITE_WORD(p, 0);
21253018216SPaolo Bonzini     /* system_serial_high */
21353018216SPaolo Bonzini     WRITE_WORD(p, 0);
21453018216SPaolo Bonzini     /* mem_fclk_21285 */
21553018216SPaolo Bonzini     WRITE_WORD(p, 0);
21653018216SPaolo Bonzini     /* zero unused fields */
21753018216SPaolo Bonzini     while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
21853018216SPaolo Bonzini         WRITE_WORD(p, 0);
21953018216SPaolo Bonzini     }
22053018216SPaolo Bonzini     s = info->kernel_cmdline;
22153018216SPaolo Bonzini     if (s) {
222e1fe50dcSStefan Weil         cpu_physical_memory_write(p, s, strlen(s) + 1);
22353018216SPaolo Bonzini     } else {
22453018216SPaolo Bonzini         WRITE_WORD(p, 0);
22553018216SPaolo Bonzini     }
22653018216SPaolo Bonzini }
22753018216SPaolo Bonzini 
22853018216SPaolo Bonzini static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo)
22953018216SPaolo Bonzini {
23053018216SPaolo Bonzini     void *fdt = NULL;
23153018216SPaolo Bonzini     int size, rc;
23270976c41SPeter Maydell     uint32_t acells, scells;
23353018216SPaolo Bonzini 
234*0fb79851SJohn Rigby     if (binfo->dtb_filename) {
235*0fb79851SJohn Rigby         char *filename;
23653018216SPaolo Bonzini         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
23753018216SPaolo Bonzini         if (!filename) {
23853018216SPaolo Bonzini             fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
239c23045deSPeter Maydell             goto fail;
24053018216SPaolo Bonzini         }
24153018216SPaolo Bonzini 
24253018216SPaolo Bonzini         fdt = load_device_tree(filename, &size);
24353018216SPaolo Bonzini         if (!fdt) {
24453018216SPaolo Bonzini             fprintf(stderr, "Couldn't open dtb file %s\n", filename);
24553018216SPaolo Bonzini             g_free(filename);
246c23045deSPeter Maydell             goto fail;
24753018216SPaolo Bonzini         }
24853018216SPaolo Bonzini         g_free(filename);
249*0fb79851SJohn Rigby     } else if (binfo->get_dtb) {
250*0fb79851SJohn Rigby         fdt = binfo->get_dtb(binfo, &size);
251*0fb79851SJohn Rigby         if (!fdt) {
252*0fb79851SJohn Rigby             fprintf(stderr, "Board was unable to create a dtb blob\n");
253*0fb79851SJohn Rigby             goto fail;
254*0fb79851SJohn Rigby         }
255*0fb79851SJohn Rigby     }
25653018216SPaolo Bonzini 
25753018216SPaolo Bonzini     acells = qemu_devtree_getprop_cell(fdt, "/", "#address-cells");
25853018216SPaolo Bonzini     scells = qemu_devtree_getprop_cell(fdt, "/", "#size-cells");
25953018216SPaolo Bonzini     if (acells == 0 || scells == 0) {
26053018216SPaolo Bonzini         fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
261c23045deSPeter Maydell         goto fail;
26253018216SPaolo Bonzini     }
26353018216SPaolo Bonzini 
26470976c41SPeter Maydell     if (scells < 2 && binfo->ram_size >= (1ULL << 32)) {
26570976c41SPeter Maydell         /* This is user error so deserves a friendlier error message
26670976c41SPeter Maydell          * than the failure of setprop_sized_cells would provide
26770976c41SPeter Maydell          */
26853018216SPaolo Bonzini         fprintf(stderr, "qemu: dtb file not compatible with "
26953018216SPaolo Bonzini                 "RAM size > 4GB\n");
270c23045deSPeter Maydell         goto fail;
27153018216SPaolo Bonzini     }
27253018216SPaolo Bonzini 
27370976c41SPeter Maydell     rc = qemu_devtree_setprop_sized_cells(fdt, "/memory", "reg",
27470976c41SPeter Maydell                                           acells, binfo->loader_start,
27570976c41SPeter Maydell                                           scells, binfo->ram_size);
27653018216SPaolo Bonzini     if (rc < 0) {
27753018216SPaolo Bonzini         fprintf(stderr, "couldn't set /memory/reg\n");
278c23045deSPeter Maydell         goto fail;
27953018216SPaolo Bonzini     }
28053018216SPaolo Bonzini 
28153018216SPaolo Bonzini     if (binfo->kernel_cmdline && *binfo->kernel_cmdline) {
28253018216SPaolo Bonzini         rc = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
28353018216SPaolo Bonzini                                           binfo->kernel_cmdline);
28453018216SPaolo Bonzini         if (rc < 0) {
28553018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/bootargs\n");
286c23045deSPeter Maydell             goto fail;
28753018216SPaolo Bonzini         }
28853018216SPaolo Bonzini     }
28953018216SPaolo Bonzini 
29053018216SPaolo Bonzini     if (binfo->initrd_size) {
29153018216SPaolo Bonzini         rc = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
29253018216SPaolo Bonzini                 binfo->initrd_start);
29353018216SPaolo Bonzini         if (rc < 0) {
29453018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
295c23045deSPeter Maydell             goto fail;
29653018216SPaolo Bonzini         }
29753018216SPaolo Bonzini 
29853018216SPaolo Bonzini         rc = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
29953018216SPaolo Bonzini                     binfo->initrd_start + binfo->initrd_size);
30053018216SPaolo Bonzini         if (rc < 0) {
30153018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
302c23045deSPeter Maydell             goto fail;
30353018216SPaolo Bonzini         }
30453018216SPaolo Bonzini     }
3053b1cceb8SPeter Maydell 
3063b1cceb8SPeter Maydell     if (binfo->modify_dtb) {
3073b1cceb8SPeter Maydell         binfo->modify_dtb(binfo, fdt);
3083b1cceb8SPeter Maydell     }
3093b1cceb8SPeter Maydell 
3102acafb1aSJohn Rigby     qemu_devtree_dumpdtb(fdt, size);
31153018216SPaolo Bonzini 
31253018216SPaolo Bonzini     cpu_physical_memory_write(addr, fdt, size);
31353018216SPaolo Bonzini 
314c23045deSPeter Maydell     g_free(fdt);
315c23045deSPeter Maydell 
31653018216SPaolo Bonzini     return 0;
317c23045deSPeter Maydell 
318c23045deSPeter Maydell fail:
319c23045deSPeter Maydell     g_free(fdt);
320c23045deSPeter Maydell     return -1;
32153018216SPaolo Bonzini }
32253018216SPaolo Bonzini 
32353018216SPaolo Bonzini static void do_cpu_reset(void *opaque)
32453018216SPaolo Bonzini {
32553018216SPaolo Bonzini     ARMCPU *cpu = opaque;
32653018216SPaolo Bonzini     CPUARMState *env = &cpu->env;
32753018216SPaolo Bonzini     const struct arm_boot_info *info = env->boot_info;
32853018216SPaolo Bonzini 
32953018216SPaolo Bonzini     cpu_reset(CPU(cpu));
33053018216SPaolo Bonzini     if (info) {
33153018216SPaolo Bonzini         if (!info->is_linux) {
33253018216SPaolo Bonzini             /* Jump to the entry point.  */
33353018216SPaolo Bonzini             env->regs[15] = info->entry & 0xfffffffe;
33453018216SPaolo Bonzini             env->thumb = info->entry & 1;
33553018216SPaolo Bonzini         } else {
336182735efSAndreas Färber             if (CPU(cpu) == first_cpu) {
33753018216SPaolo Bonzini                 env->regs[15] = info->loader_start;
33853018216SPaolo Bonzini                 if (!info->dtb_filename) {
33953018216SPaolo Bonzini                     if (old_param) {
34053018216SPaolo Bonzini                         set_kernel_args_old(info);
34153018216SPaolo Bonzini                     } else {
34253018216SPaolo Bonzini                         set_kernel_args(info);
34353018216SPaolo Bonzini                     }
34453018216SPaolo Bonzini                 }
34553018216SPaolo Bonzini             } else {
34653018216SPaolo Bonzini                 info->secondary_cpu_reset_hook(cpu, info);
34753018216SPaolo Bonzini             }
34853018216SPaolo Bonzini         }
34953018216SPaolo Bonzini     }
35053018216SPaolo Bonzini }
35153018216SPaolo Bonzini 
35253018216SPaolo Bonzini void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
35353018216SPaolo Bonzini {
354182735efSAndreas Färber     CPUState *cs = CPU(cpu);
35553018216SPaolo Bonzini     int kernel_size;
35653018216SPaolo Bonzini     int initrd_size;
35753018216SPaolo Bonzini     int n;
35853018216SPaolo Bonzini     int is_linux = 0;
35953018216SPaolo Bonzini     uint64_t elf_entry;
36053018216SPaolo Bonzini     hwaddr entry;
36153018216SPaolo Bonzini     int big_endian;
36253018216SPaolo Bonzini 
36353018216SPaolo Bonzini     /* Load the kernel.  */
36453018216SPaolo Bonzini     if (!info->kernel_filename) {
3659546dbabSPeter Maydell         /* If no kernel specified, do nothing; we will start from address 0
3669546dbabSPeter Maydell          * (typically a boot ROM image) in the same way as hardware.
3679546dbabSPeter Maydell          */
3689546dbabSPeter Maydell         return;
36953018216SPaolo Bonzini     }
37053018216SPaolo Bonzini 
3712ff3de68SMarkus Armbruster     info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb");
37253018216SPaolo Bonzini 
37353018216SPaolo Bonzini     if (!info->secondary_cpu_reset_hook) {
37453018216SPaolo Bonzini         info->secondary_cpu_reset_hook = default_reset_secondary;
37553018216SPaolo Bonzini     }
37653018216SPaolo Bonzini     if (!info->write_secondary_boot) {
37753018216SPaolo Bonzini         info->write_secondary_boot = default_write_secondary;
37853018216SPaolo Bonzini     }
37953018216SPaolo Bonzini 
38053018216SPaolo Bonzini     if (info->nb_cpus == 0)
38153018216SPaolo Bonzini         info->nb_cpus = 1;
38253018216SPaolo Bonzini 
38353018216SPaolo Bonzini #ifdef TARGET_WORDS_BIGENDIAN
38453018216SPaolo Bonzini     big_endian = 1;
38553018216SPaolo Bonzini #else
38653018216SPaolo Bonzini     big_endian = 0;
38753018216SPaolo Bonzini #endif
38853018216SPaolo Bonzini 
38953018216SPaolo Bonzini     /* We want to put the initrd far enough into RAM that when the
39053018216SPaolo Bonzini      * kernel is uncompressed it will not clobber the initrd. However
39153018216SPaolo Bonzini      * on boards without much RAM we must ensure that we still leave
39253018216SPaolo Bonzini      * enough room for a decent sized initrd, and on boards with large
39353018216SPaolo Bonzini      * amounts of RAM we must avoid the initrd being so far up in RAM
39453018216SPaolo Bonzini      * that it is outside lowmem and inaccessible to the kernel.
39553018216SPaolo Bonzini      * So for boards with less  than 256MB of RAM we put the initrd
39653018216SPaolo Bonzini      * halfway into RAM, and for boards with 256MB of RAM or more we put
39753018216SPaolo Bonzini      * the initrd at 128MB.
39853018216SPaolo Bonzini      */
39953018216SPaolo Bonzini     info->initrd_start = info->loader_start +
40053018216SPaolo Bonzini         MIN(info->ram_size / 2, 128 * 1024 * 1024);
40153018216SPaolo Bonzini 
40253018216SPaolo Bonzini     /* Assume that raw images are linux kernels, and ELF images are not.  */
40353018216SPaolo Bonzini     kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry,
40453018216SPaolo Bonzini                            NULL, NULL, big_endian, ELF_MACHINE, 1);
40553018216SPaolo Bonzini     entry = elf_entry;
40653018216SPaolo Bonzini     if (kernel_size < 0) {
40753018216SPaolo Bonzini         kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
40853018216SPaolo Bonzini                                   &is_linux);
40953018216SPaolo Bonzini     }
41053018216SPaolo Bonzini     if (kernel_size < 0) {
41153018216SPaolo Bonzini         entry = info->loader_start + KERNEL_LOAD_ADDR;
41253018216SPaolo Bonzini         kernel_size = load_image_targphys(info->kernel_filename, entry,
41353018216SPaolo Bonzini                                           info->ram_size - KERNEL_LOAD_ADDR);
41453018216SPaolo Bonzini         is_linux = 1;
41553018216SPaolo Bonzini     }
41653018216SPaolo Bonzini     if (kernel_size < 0) {
41753018216SPaolo Bonzini         fprintf(stderr, "qemu: could not load kernel '%s'\n",
41853018216SPaolo Bonzini                 info->kernel_filename);
41953018216SPaolo Bonzini         exit(1);
42053018216SPaolo Bonzini     }
42153018216SPaolo Bonzini     info->entry = entry;
42253018216SPaolo Bonzini     if (is_linux) {
42353018216SPaolo Bonzini         if (info->initrd_filename) {
424fd76663eSSoren Brinkmann             initrd_size = load_ramdisk(info->initrd_filename,
425fd76663eSSoren Brinkmann                                        info->initrd_start,
426fd76663eSSoren Brinkmann                                        info->ram_size -
427fd76663eSSoren Brinkmann                                        info->initrd_start);
428fd76663eSSoren Brinkmann             if (initrd_size < 0) {
42953018216SPaolo Bonzini                 initrd_size = load_image_targphys(info->initrd_filename,
43053018216SPaolo Bonzini                                                   info->initrd_start,
43153018216SPaolo Bonzini                                                   info->ram_size -
43253018216SPaolo Bonzini                                                   info->initrd_start);
433fd76663eSSoren Brinkmann             }
43453018216SPaolo Bonzini             if (initrd_size < 0) {
43553018216SPaolo Bonzini                 fprintf(stderr, "qemu: could not load initrd '%s'\n",
43653018216SPaolo Bonzini                         info->initrd_filename);
43753018216SPaolo Bonzini                 exit(1);
43853018216SPaolo Bonzini             }
43953018216SPaolo Bonzini         } else {
44053018216SPaolo Bonzini             initrd_size = 0;
44153018216SPaolo Bonzini         }
44253018216SPaolo Bonzini         info->initrd_size = initrd_size;
44353018216SPaolo Bonzini 
44453018216SPaolo Bonzini         bootloader[4] = info->board_id;
44553018216SPaolo Bonzini 
44653018216SPaolo Bonzini         /* for device tree boot, we pass the DTB directly in r2. Otherwise
44753018216SPaolo Bonzini          * we point to the kernel args.
44853018216SPaolo Bonzini          */
449*0fb79851SJohn Rigby         if (info->dtb_filename || info->get_dtb) {
45053018216SPaolo Bonzini             /* Place the DTB after the initrd in memory. Note that some
45153018216SPaolo Bonzini              * kernels will trash anything in the 4K page the initrd
45253018216SPaolo Bonzini              * ends in, so make sure the DTB isn't caught up in that.
45353018216SPaolo Bonzini              */
45453018216SPaolo Bonzini             hwaddr dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size,
45553018216SPaolo Bonzini                                              4096);
45653018216SPaolo Bonzini             if (load_dtb(dtb_start, info)) {
45753018216SPaolo Bonzini                 exit(1);
45853018216SPaolo Bonzini             }
45953018216SPaolo Bonzini             bootloader[5] = dtb_start;
46053018216SPaolo Bonzini         } else {
46153018216SPaolo Bonzini             bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
46253018216SPaolo Bonzini             if (info->ram_size >= (1ULL << 32)) {
46353018216SPaolo Bonzini                 fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
46453018216SPaolo Bonzini                         " Linux kernel using ATAGS (try passing a device tree"
46553018216SPaolo Bonzini                         " using -dtb)\n");
46653018216SPaolo Bonzini                 exit(1);
46753018216SPaolo Bonzini             }
46853018216SPaolo Bonzini         }
46953018216SPaolo Bonzini         bootloader[6] = entry;
47053018216SPaolo Bonzini         for (n = 0; n < sizeof(bootloader) / 4; n++) {
47153018216SPaolo Bonzini             bootloader[n] = tswap32(bootloader[n]);
47253018216SPaolo Bonzini         }
47353018216SPaolo Bonzini         rom_add_blob_fixed("bootloader", bootloader, sizeof(bootloader),
47453018216SPaolo Bonzini                            info->loader_start);
47553018216SPaolo Bonzini         if (info->nb_cpus > 1) {
47653018216SPaolo Bonzini             info->write_secondary_boot(cpu, info);
47753018216SPaolo Bonzini         }
47853018216SPaolo Bonzini     }
47953018216SPaolo Bonzini     info->is_linux = is_linux;
48053018216SPaolo Bonzini 
481bdc44640SAndreas Färber     for (; cs; cs = CPU_NEXT(cs)) {
482182735efSAndreas Färber         cpu = ARM_CPU(cs);
483182735efSAndreas Färber         cpu->env.boot_info = info;
48453018216SPaolo Bonzini         qemu_register_reset(do_cpu_reset, cpu);
48553018216SPaolo Bonzini     }
48653018216SPaolo Bonzini }
487