xref: /openbmc/qemu/hw/arm/boot.c (revision d73abd6d)
1 /*
2  * ARM kernel loader.
3  *
4  * Copyright (c) 2006-2007 CodeSourcery.
5  * Written by Paul Brook
6  *
7  * This code is licensed under the GPL.
8  */
9 
10 #include "config.h"
11 #include "hw/hw.h"
12 #include "hw/arm/arm.h"
13 #include "hw/arm/linux-boot-if.h"
14 #include "sysemu/sysemu.h"
15 #include "hw/boards.h"
16 #include "hw/loader.h"
17 #include "elf.h"
18 #include "sysemu/device_tree.h"
19 #include "qemu/config-file.h"
20 #include "exec/address-spaces.h"
21 
22 /* Kernel boot protocol is specified in the kernel docs
23  * Documentation/arm/Booting and Documentation/arm64/booting.txt
24  * They have different preferred image load offsets from system RAM base.
25  */
26 #define KERNEL_ARGS_ADDR 0x100
27 #define KERNEL_LOAD_ADDR 0x00010000
28 #define KERNEL64_LOAD_ADDR 0x00080000
29 
30 typedef enum {
31     FIXUP_NONE = 0,     /* do nothing */
32     FIXUP_TERMINATOR,   /* end of insns */
33     FIXUP_BOARDID,      /* overwrite with board ID number */
34     FIXUP_BOARD_SETUP,  /* overwrite with board specific setup code address */
35     FIXUP_ARGPTR,       /* overwrite with pointer to kernel args */
36     FIXUP_ENTRYPOINT,   /* overwrite with kernel entry point */
37     FIXUP_GIC_CPU_IF,   /* overwrite with GIC CPU interface address */
38     FIXUP_BOOTREG,      /* overwrite with boot register address */
39     FIXUP_DSB,          /* overwrite with correct DSB insn for cpu */
40     FIXUP_MAX,
41 } FixupType;
42 
43 typedef struct ARMInsnFixup {
44     uint32_t insn;
45     FixupType fixup;
46 } ARMInsnFixup;
47 
48 static const ARMInsnFixup bootloader_aarch64[] = {
49     { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
50     { 0xaa1f03e1 }, /* mov x1, xzr */
51     { 0xaa1f03e2 }, /* mov x2, xzr */
52     { 0xaa1f03e3 }, /* mov x3, xzr */
53     { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
54     { 0xd61f0080 }, /* br x4      ; Jump to the kernel entry point */
55     { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */
56     { 0 }, /* .word @DTB Higher 32-bits */
57     { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */
58     { 0 }, /* .word @Kernel Entry Higher 32-bits */
59     { 0, FIXUP_TERMINATOR }
60 };
61 
62 /* A very small bootloader: call the board-setup code (if needed),
63  * set r0-r2, then jump to the kernel.
64  * If we're not calling boot setup code then we don't copy across
65  * the first BOOTLOADER_NO_BOARD_SETUP_OFFSET insns in this array.
66  */
67 
68 static const ARMInsnFixup bootloader[] = {
69     { 0xe28fe008 }, /* add     lr, pc, #8 */
70     { 0xe51ff004 }, /* ldr     pc, [pc, #-4] */
71     { 0, FIXUP_BOARD_SETUP },
72 #define BOOTLOADER_NO_BOARD_SETUP_OFFSET 3
73     { 0xe3a00000 }, /* mov     r0, #0 */
74     { 0xe59f1004 }, /* ldr     r1, [pc, #4] */
75     { 0xe59f2004 }, /* ldr     r2, [pc, #4] */
76     { 0xe59ff004 }, /* ldr     pc, [pc, #4] */
77     { 0, FIXUP_BOARDID },
78     { 0, FIXUP_ARGPTR },
79     { 0, FIXUP_ENTRYPOINT },
80     { 0, FIXUP_TERMINATOR }
81 };
82 
83 /* Handling for secondary CPU boot in a multicore system.
84  * Unlike the uniprocessor/primary CPU boot, this is platform
85  * dependent. The default code here is based on the secondary
86  * CPU boot protocol used on realview/vexpress boards, with
87  * some parameterisation to increase its flexibility.
88  * QEMU platform models for which this code is not appropriate
89  * should override write_secondary_boot and secondary_cpu_reset_hook
90  * instead.
91  *
92  * This code enables the interrupt controllers for the secondary
93  * CPUs and then puts all the secondary CPUs into a loop waiting
94  * for an interprocessor interrupt and polling a configurable
95  * location for the kernel secondary CPU entry point.
96  */
97 #define DSB_INSN 0xf57ff04f
98 #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
99 
100 static const ARMInsnFixup smpboot[] = {
101     { 0xe59f2028 }, /* ldr r2, gic_cpu_if */
102     { 0xe59f0028 }, /* ldr r0, bootreg_addr */
103     { 0xe3a01001 }, /* mov r1, #1 */
104     { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */
105     { 0xe3a010ff }, /* mov r1, #0xff */
106     { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
107     { 0, FIXUP_DSB },   /* dsb */
108     { 0xe320f003 }, /* wfi */
109     { 0xe5901000 }, /* ldr     r1, [r0] */
110     { 0xe1110001 }, /* tst     r1, r1 */
111     { 0x0afffffb }, /* beq     <wfi> */
112     { 0xe12fff11 }, /* bx      r1 */
113     { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */
114     { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */
115     { 0, FIXUP_TERMINATOR }
116 };
117 
118 static void write_bootloader(const char *name, hwaddr addr,
119                              const ARMInsnFixup *insns, uint32_t *fixupcontext)
120 {
121     /* Fix up the specified bootloader fragment and write it into
122      * guest memory using rom_add_blob_fixed(). fixupcontext is
123      * an array giving the values to write in for the fixup types
124      * which write a value into the code array.
125      */
126     int i, len;
127     uint32_t *code;
128 
129     len = 0;
130     while (insns[len].fixup != FIXUP_TERMINATOR) {
131         len++;
132     }
133 
134     code = g_new0(uint32_t, len);
135 
136     for (i = 0; i < len; i++) {
137         uint32_t insn = insns[i].insn;
138         FixupType fixup = insns[i].fixup;
139 
140         switch (fixup) {
141         case FIXUP_NONE:
142             break;
143         case FIXUP_BOARDID:
144         case FIXUP_BOARD_SETUP:
145         case FIXUP_ARGPTR:
146         case FIXUP_ENTRYPOINT:
147         case FIXUP_GIC_CPU_IF:
148         case FIXUP_BOOTREG:
149         case FIXUP_DSB:
150             insn = fixupcontext[fixup];
151             break;
152         default:
153             abort();
154         }
155         code[i] = tswap32(insn);
156     }
157 
158     rom_add_blob_fixed(name, code, len * sizeof(uint32_t), addr);
159 
160     g_free(code);
161 }
162 
163 static void default_write_secondary(ARMCPU *cpu,
164                                     const struct arm_boot_info *info)
165 {
166     uint32_t fixupcontext[FIXUP_MAX];
167 
168     fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr;
169     fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr;
170     if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
171         fixupcontext[FIXUP_DSB] = DSB_INSN;
172     } else {
173         fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
174     }
175 
176     write_bootloader("smpboot", info->smp_loader_start,
177                      smpboot, fixupcontext);
178 }
179 
180 static void default_reset_secondary(ARMCPU *cpu,
181                                     const struct arm_boot_info *info)
182 {
183     CPUState *cs = CPU(cpu);
184 
185     address_space_stl_notdirty(&address_space_memory, info->smp_bootreg_addr,
186                                0, MEMTXATTRS_UNSPECIFIED, NULL);
187     cpu_set_pc(cs, info->smp_loader_start);
188 }
189 
190 static inline bool have_dtb(const struct arm_boot_info *info)
191 {
192     return info->dtb_filename || info->get_dtb;
193 }
194 
195 #define WRITE_WORD(p, value) do { \
196     address_space_stl_notdirty(&address_space_memory, p, value, \
197                                MEMTXATTRS_UNSPECIFIED, NULL);  \
198     p += 4;                       \
199 } while (0)
200 
201 static void set_kernel_args(const struct arm_boot_info *info)
202 {
203     int initrd_size = info->initrd_size;
204     hwaddr base = info->loader_start;
205     hwaddr p;
206 
207     p = base + KERNEL_ARGS_ADDR;
208     /* ATAG_CORE */
209     WRITE_WORD(p, 5);
210     WRITE_WORD(p, 0x54410001);
211     WRITE_WORD(p, 1);
212     WRITE_WORD(p, 0x1000);
213     WRITE_WORD(p, 0);
214     /* ATAG_MEM */
215     /* TODO: handle multiple chips on one ATAG list */
216     WRITE_WORD(p, 4);
217     WRITE_WORD(p, 0x54410002);
218     WRITE_WORD(p, info->ram_size);
219     WRITE_WORD(p, info->loader_start);
220     if (initrd_size) {
221         /* ATAG_INITRD2 */
222         WRITE_WORD(p, 4);
223         WRITE_WORD(p, 0x54420005);
224         WRITE_WORD(p, info->initrd_start);
225         WRITE_WORD(p, initrd_size);
226     }
227     if (info->kernel_cmdline && *info->kernel_cmdline) {
228         /* ATAG_CMDLINE */
229         int cmdline_size;
230 
231         cmdline_size = strlen(info->kernel_cmdline);
232         cpu_physical_memory_write(p + 8, info->kernel_cmdline,
233                                   cmdline_size + 1);
234         cmdline_size = (cmdline_size >> 2) + 1;
235         WRITE_WORD(p, cmdline_size + 2);
236         WRITE_WORD(p, 0x54410009);
237         p += cmdline_size * 4;
238     }
239     if (info->atag_board) {
240         /* ATAG_BOARD */
241         int atag_board_len;
242         uint8_t atag_board_buf[0x1000];
243 
244         atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
245         WRITE_WORD(p, (atag_board_len + 8) >> 2);
246         WRITE_WORD(p, 0x414f4d50);
247         cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
248         p += atag_board_len;
249     }
250     /* ATAG_END */
251     WRITE_WORD(p, 0);
252     WRITE_WORD(p, 0);
253 }
254 
255 static void set_kernel_args_old(const struct arm_boot_info *info)
256 {
257     hwaddr p;
258     const char *s;
259     int initrd_size = info->initrd_size;
260     hwaddr base = info->loader_start;
261 
262     /* see linux/include/asm-arm/setup.h */
263     p = base + KERNEL_ARGS_ADDR;
264     /* page_size */
265     WRITE_WORD(p, 4096);
266     /* nr_pages */
267     WRITE_WORD(p, info->ram_size / 4096);
268     /* ramdisk_size */
269     WRITE_WORD(p, 0);
270 #define FLAG_READONLY	1
271 #define FLAG_RDLOAD	4
272 #define FLAG_RDPROMPT	8
273     /* flags */
274     WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
275     /* rootdev */
276     WRITE_WORD(p, (31 << 8) | 0);	/* /dev/mtdblock0 */
277     /* video_num_cols */
278     WRITE_WORD(p, 0);
279     /* video_num_rows */
280     WRITE_WORD(p, 0);
281     /* video_x */
282     WRITE_WORD(p, 0);
283     /* video_y */
284     WRITE_WORD(p, 0);
285     /* memc_control_reg */
286     WRITE_WORD(p, 0);
287     /* unsigned char sounddefault */
288     /* unsigned char adfsdrives */
289     /* unsigned char bytes_per_char_h */
290     /* unsigned char bytes_per_char_v */
291     WRITE_WORD(p, 0);
292     /* pages_in_bank[4] */
293     WRITE_WORD(p, 0);
294     WRITE_WORD(p, 0);
295     WRITE_WORD(p, 0);
296     WRITE_WORD(p, 0);
297     /* pages_in_vram */
298     WRITE_WORD(p, 0);
299     /* initrd_start */
300     if (initrd_size) {
301         WRITE_WORD(p, info->initrd_start);
302     } else {
303         WRITE_WORD(p, 0);
304     }
305     /* initrd_size */
306     WRITE_WORD(p, initrd_size);
307     /* rd_start */
308     WRITE_WORD(p, 0);
309     /* system_rev */
310     WRITE_WORD(p, 0);
311     /* system_serial_low */
312     WRITE_WORD(p, 0);
313     /* system_serial_high */
314     WRITE_WORD(p, 0);
315     /* mem_fclk_21285 */
316     WRITE_WORD(p, 0);
317     /* zero unused fields */
318     while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
319         WRITE_WORD(p, 0);
320     }
321     s = info->kernel_cmdline;
322     if (s) {
323         cpu_physical_memory_write(p, s, strlen(s) + 1);
324     } else {
325         WRITE_WORD(p, 0);
326     }
327 }
328 
329 /**
330  * load_dtb() - load a device tree binary image into memory
331  * @addr:       the address to load the image at
332  * @binfo:      struct describing the boot environment
333  * @addr_limit: upper limit of the available memory area at @addr
334  *
335  * Load a device tree supplied by the machine or by the user  with the
336  * '-dtb' command line option, and put it at offset @addr in target
337  * memory.
338  *
339  * If @addr_limit contains a meaningful value (i.e., it is strictly greater
340  * than @addr), the device tree is only loaded if its size does not exceed
341  * the limit.
342  *
343  * Returns: the size of the device tree image on success,
344  *          0 if the image size exceeds the limit,
345  *          -1 on errors.
346  *
347  * Note: Must not be called unless have_dtb(binfo) is true.
348  */
349 static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
350                     hwaddr addr_limit)
351 {
352     void *fdt = NULL;
353     int size, rc;
354     uint32_t acells, scells;
355 
356     if (binfo->dtb_filename) {
357         char *filename;
358         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
359         if (!filename) {
360             fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
361             goto fail;
362         }
363 
364         fdt = load_device_tree(filename, &size);
365         if (!fdt) {
366             fprintf(stderr, "Couldn't open dtb file %s\n", filename);
367             g_free(filename);
368             goto fail;
369         }
370         g_free(filename);
371     } else {
372         fdt = binfo->get_dtb(binfo, &size);
373         if (!fdt) {
374             fprintf(stderr, "Board was unable to create a dtb blob\n");
375             goto fail;
376         }
377     }
378 
379     if (addr_limit > addr && size > (addr_limit - addr)) {
380         /* Installing the device tree blob at addr would exceed addr_limit.
381          * Whether this constitutes failure is up to the caller to decide,
382          * so just return 0 as size, i.e., no error.
383          */
384         g_free(fdt);
385         return 0;
386     }
387 
388     acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells");
389     scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells");
390     if (acells == 0 || scells == 0) {
391         fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
392         goto fail;
393     }
394 
395     if (scells < 2 && binfo->ram_size >= (1ULL << 32)) {
396         /* This is user error so deserves a friendlier error message
397          * than the failure of setprop_sized_cells would provide
398          */
399         fprintf(stderr, "qemu: dtb file not compatible with "
400                 "RAM size > 4GB\n");
401         goto fail;
402     }
403 
404     rc = qemu_fdt_setprop_sized_cells(fdt, "/memory", "reg",
405                                       acells, binfo->loader_start,
406                                       scells, binfo->ram_size);
407     if (rc < 0) {
408         fprintf(stderr, "couldn't set /memory/reg\n");
409         goto fail;
410     }
411 
412     if (binfo->kernel_cmdline && *binfo->kernel_cmdline) {
413         rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
414                                      binfo->kernel_cmdline);
415         if (rc < 0) {
416             fprintf(stderr, "couldn't set /chosen/bootargs\n");
417             goto fail;
418         }
419     }
420 
421     if (binfo->initrd_size) {
422         rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
423                                    binfo->initrd_start);
424         if (rc < 0) {
425             fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
426             goto fail;
427         }
428 
429         rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
430                                    binfo->initrd_start + binfo->initrd_size);
431         if (rc < 0) {
432             fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
433             goto fail;
434         }
435     }
436 
437     if (binfo->modify_dtb) {
438         binfo->modify_dtb(binfo, fdt);
439     }
440 
441     qemu_fdt_dumpdtb(fdt, size);
442 
443     /* Put the DTB into the memory map as a ROM image: this will ensure
444      * the DTB is copied again upon reset, even if addr points into RAM.
445      */
446     rom_add_blob_fixed("dtb", fdt, size, addr);
447 
448     g_free(fdt);
449 
450     return size;
451 
452 fail:
453     g_free(fdt);
454     return -1;
455 }
456 
457 static void do_cpu_reset(void *opaque)
458 {
459     ARMCPU *cpu = opaque;
460     CPUState *cs = CPU(cpu);
461     CPUARMState *env = &cpu->env;
462     const struct arm_boot_info *info = env->boot_info;
463 
464     cpu_reset(cs);
465     if (info) {
466         if (!info->is_linux) {
467             /* Jump to the entry point.  */
468             uint64_t entry = info->entry;
469 
470             if (!env->aarch64) {
471                 env->thumb = info->entry & 1;
472                 entry &= 0xfffffffe;
473             }
474             cpu_set_pc(cs, entry);
475         } else {
476             /* If we are booting Linux then we need to check whether we are
477              * booting into secure or non-secure state and adjust the state
478              * accordingly.  Out of reset, ARM is defined to be in secure state
479              * (SCR.NS = 0), we change that here if non-secure boot has been
480              * requested.
481              */
482             if (arm_feature(env, ARM_FEATURE_EL3)) {
483                 /* AArch64 is defined to come out of reset into EL3 if enabled.
484                  * If we are booting Linux then we need to adjust our EL as
485                  * Linux expects us to be in EL2 or EL1.  AArch32 resets into
486                  * SVC, which Linux expects, so no privilege/exception level to
487                  * adjust.
488                  */
489                 if (env->aarch64) {
490                     if (arm_feature(env, ARM_FEATURE_EL2)) {
491                         env->pstate = PSTATE_MODE_EL2h;
492                     } else {
493                         env->pstate = PSTATE_MODE_EL1h;
494                     }
495                 }
496 
497                 /* Set to non-secure if not a secure boot */
498                 if (!info->secure_boot) {
499                     /* Linux expects non-secure state */
500                     env->cp15.scr_el3 |= SCR_NS;
501                 }
502             }
503 
504             if (cs == first_cpu) {
505                 cpu_set_pc(cs, info->loader_start);
506 
507                 if (!have_dtb(info)) {
508                     if (old_param) {
509                         set_kernel_args_old(info);
510                     } else {
511                         set_kernel_args(info);
512                     }
513                 }
514             } else {
515                 info->secondary_cpu_reset_hook(cpu, info);
516             }
517         }
518     }
519 }
520 
521 /**
522  * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
523  *                          by key.
524  * @fw_cfg:         The firmware config instance to store the data in.
525  * @size_key:       The firmware config key to store the size of the loaded
526  *                  data under, with fw_cfg_add_i32().
527  * @data_key:       The firmware config key to store the loaded data under,
528  *                  with fw_cfg_add_bytes().
529  * @image_name:     The name of the image file to load. If it is NULL, the
530  *                  function returns without doing anything.
531  * @try_decompress: Whether the image should be decompressed (gunzipped) before
532  *                  adding it to fw_cfg. If decompression fails, the image is
533  *                  loaded as-is.
534  *
535  * In case of failure, the function prints an error message to stderr and the
536  * process exits with status 1.
537  */
538 static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
539                                  uint16_t data_key, const char *image_name,
540                                  bool try_decompress)
541 {
542     size_t size = -1;
543     uint8_t *data;
544 
545     if (image_name == NULL) {
546         return;
547     }
548 
549     if (try_decompress) {
550         size = load_image_gzipped_buffer(image_name,
551                                          LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
552     }
553 
554     if (size == (size_t)-1) {
555         gchar *contents;
556         gsize length;
557 
558         if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
559             fprintf(stderr, "failed to load \"%s\"\n", image_name);
560             exit(1);
561         }
562         size = length;
563         data = (uint8_t *)contents;
564     }
565 
566     fw_cfg_add_i32(fw_cfg, size_key, size);
567     fw_cfg_add_bytes(fw_cfg, data_key, data, size);
568 }
569 
570 static int do_arm_linux_init(Object *obj, void *opaque)
571 {
572     if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
573         ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj);
574         ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj);
575         struct arm_boot_info *info = opaque;
576 
577         if (albifc->arm_linux_init) {
578             albifc->arm_linux_init(albif, info->secure_boot);
579         }
580     }
581     return 0;
582 }
583 
584 static void arm_load_kernel_notify(Notifier *notifier, void *data)
585 {
586     CPUState *cs;
587     int kernel_size;
588     int initrd_size;
589     int is_linux = 0;
590     uint64_t elf_entry, elf_low_addr, elf_high_addr;
591     int elf_machine;
592     hwaddr entry, kernel_load_offset;
593     int big_endian;
594     static const ARMInsnFixup *primary_loader;
595     ArmLoadKernelNotifier *n = DO_UPCAST(ArmLoadKernelNotifier,
596                                          notifier, notifier);
597     ARMCPU *cpu = n->cpu;
598     struct arm_boot_info *info =
599         container_of(n, struct arm_boot_info, load_kernel_notifier);
600 
601     /* Load the kernel.  */
602     if (!info->kernel_filename || info->firmware_loaded) {
603 
604         if (have_dtb(info)) {
605             /* If we have a device tree blob, but no kernel to supply it to (or
606              * the kernel is supposed to be loaded by the bootloader), copy the
607              * DTB to the base of RAM for the bootloader to pick up.
608              */
609             if (load_dtb(info->loader_start, info, 0) < 0) {
610                 exit(1);
611             }
612         }
613 
614         if (info->kernel_filename) {
615             FWCfgState *fw_cfg;
616             bool try_decompressing_kernel;
617 
618             fw_cfg = fw_cfg_find();
619             try_decompressing_kernel = arm_feature(&cpu->env,
620                                                    ARM_FEATURE_AARCH64);
621 
622             /* Expose the kernel, the command line, and the initrd in fw_cfg.
623              * We don't process them here at all, it's all left to the
624              * firmware.
625              */
626             load_image_to_fw_cfg(fw_cfg,
627                                  FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
628                                  info->kernel_filename,
629                                  try_decompressing_kernel);
630             load_image_to_fw_cfg(fw_cfg,
631                                  FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
632                                  info->initrd_filename, false);
633 
634             if (info->kernel_cmdline) {
635                 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
636                                strlen(info->kernel_cmdline) + 1);
637                 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
638                                   info->kernel_cmdline);
639             }
640         }
641 
642         /* We will start from address 0 (typically a boot ROM image) in the
643          * same way as hardware.
644          */
645         return;
646     }
647 
648     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
649         primary_loader = bootloader_aarch64;
650         kernel_load_offset = KERNEL64_LOAD_ADDR;
651         elf_machine = EM_AARCH64;
652     } else {
653         primary_loader = bootloader;
654         if (!info->write_board_setup) {
655             primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET;
656         }
657         kernel_load_offset = KERNEL_LOAD_ADDR;
658         elf_machine = EM_ARM;
659     }
660 
661     info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb");
662 
663     if (!info->secondary_cpu_reset_hook) {
664         info->secondary_cpu_reset_hook = default_reset_secondary;
665     }
666     if (!info->write_secondary_boot) {
667         info->write_secondary_boot = default_write_secondary;
668     }
669 
670     if (info->nb_cpus == 0)
671         info->nb_cpus = 1;
672 
673 #ifdef TARGET_WORDS_BIGENDIAN
674     big_endian = 1;
675 #else
676     big_endian = 0;
677 #endif
678 
679     /* We want to put the initrd far enough into RAM that when the
680      * kernel is uncompressed it will not clobber the initrd. However
681      * on boards without much RAM we must ensure that we still leave
682      * enough room for a decent sized initrd, and on boards with large
683      * amounts of RAM we must avoid the initrd being so far up in RAM
684      * that it is outside lowmem and inaccessible to the kernel.
685      * So for boards with less  than 256MB of RAM we put the initrd
686      * halfway into RAM, and for boards with 256MB of RAM or more we put
687      * the initrd at 128MB.
688      */
689     info->initrd_start = info->loader_start +
690         MIN(info->ram_size / 2, 128 * 1024 * 1024);
691 
692     /* Assume that raw images are linux kernels, and ELF images are not.  */
693     kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry,
694                            &elf_low_addr, &elf_high_addr, big_endian,
695                            elf_machine, 1);
696     if (kernel_size > 0 && have_dtb(info)) {
697         /* If there is still some room left at the base of RAM, try and put
698          * the DTB there like we do for images loaded with -bios or -pflash.
699          */
700         if (elf_low_addr > info->loader_start
701             || elf_high_addr < info->loader_start) {
702             /* Pass elf_low_addr as address limit to load_dtb if it may be
703              * pointing into RAM, otherwise pass '0' (no limit)
704              */
705             if (elf_low_addr < info->loader_start) {
706                 elf_low_addr = 0;
707             }
708             if (load_dtb(info->loader_start, info, elf_low_addr) < 0) {
709                 exit(1);
710             }
711         }
712     }
713     entry = elf_entry;
714     if (kernel_size < 0) {
715         kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
716                                   &is_linux, NULL, NULL);
717     }
718     /* On aarch64, it's the bootloader's job to uncompress the kernel. */
719     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
720         entry = info->loader_start + kernel_load_offset;
721         kernel_size = load_image_gzipped(info->kernel_filename, entry,
722                                          info->ram_size - kernel_load_offset);
723         is_linux = 1;
724     }
725     if (kernel_size < 0) {
726         entry = info->loader_start + kernel_load_offset;
727         kernel_size = load_image_targphys(info->kernel_filename, entry,
728                                           info->ram_size - kernel_load_offset);
729         is_linux = 1;
730     }
731     if (kernel_size < 0) {
732         fprintf(stderr, "qemu: could not load kernel '%s'\n",
733                 info->kernel_filename);
734         exit(1);
735     }
736     info->entry = entry;
737     if (is_linux) {
738         uint32_t fixupcontext[FIXUP_MAX];
739 
740         if (info->initrd_filename) {
741             initrd_size = load_ramdisk(info->initrd_filename,
742                                        info->initrd_start,
743                                        info->ram_size -
744                                        info->initrd_start);
745             if (initrd_size < 0) {
746                 initrd_size = load_image_targphys(info->initrd_filename,
747                                                   info->initrd_start,
748                                                   info->ram_size -
749                                                   info->initrd_start);
750             }
751             if (initrd_size < 0) {
752                 fprintf(stderr, "qemu: could not load initrd '%s'\n",
753                         info->initrd_filename);
754                 exit(1);
755             }
756         } else {
757             initrd_size = 0;
758         }
759         info->initrd_size = initrd_size;
760 
761         fixupcontext[FIXUP_BOARDID] = info->board_id;
762         fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr;
763 
764         /* for device tree boot, we pass the DTB directly in r2. Otherwise
765          * we point to the kernel args.
766          */
767         if (have_dtb(info)) {
768             hwaddr align;
769             hwaddr dtb_start;
770 
771             if (elf_machine == EM_AARCH64) {
772                 /*
773                  * Some AArch64 kernels on early bootup map the fdt region as
774                  *
775                  *   [ ALIGN_DOWN(fdt, 2MB) ... ALIGN_DOWN(fdt, 2MB) + 2MB ]
776                  *
777                  * Let's play safe and prealign it to 2MB to give us some space.
778                  */
779                 align = 2 * 1024 * 1024;
780             } else {
781                 /*
782                  * Some 32bit kernels will trash anything in the 4K page the
783                  * initrd ends in, so make sure the DTB isn't caught up in that.
784                  */
785                 align = 4096;
786             }
787 
788             /* Place the DTB after the initrd in memory with alignment. */
789             dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size, align);
790             if (load_dtb(dtb_start, info, 0) < 0) {
791                 exit(1);
792             }
793             fixupcontext[FIXUP_ARGPTR] = dtb_start;
794         } else {
795             fixupcontext[FIXUP_ARGPTR] = info->loader_start + KERNEL_ARGS_ADDR;
796             if (info->ram_size >= (1ULL << 32)) {
797                 fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
798                         " Linux kernel using ATAGS (try passing a device tree"
799                         " using -dtb)\n");
800                 exit(1);
801             }
802         }
803         fixupcontext[FIXUP_ENTRYPOINT] = entry;
804 
805         write_bootloader("bootloader", info->loader_start,
806                          primary_loader, fixupcontext);
807 
808         if (info->nb_cpus > 1) {
809             info->write_secondary_boot(cpu, info);
810         }
811         if (info->write_board_setup) {
812             info->write_board_setup(cpu, info);
813         }
814 
815         /* Notify devices which need to fake up firmware initialization
816          * that we're doing a direct kernel boot.
817          */
818         object_child_foreach_recursive(object_get_root(),
819                                        do_arm_linux_init, info);
820     }
821     info->is_linux = is_linux;
822 
823     for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
824         ARM_CPU(cs)->env.boot_info = info;
825     }
826 }
827 
828 void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
829 {
830     CPUState *cs;
831 
832     info->load_kernel_notifier.cpu = cpu;
833     info->load_kernel_notifier.notifier.notify = arm_load_kernel_notify;
834     qemu_add_machine_init_done_notifier(&info->load_kernel_notifier.notifier);
835 
836     /* CPU objects (unlike devices) are not automatically reset on system
837      * reset, so we must always register a handler to do so. If we're
838      * actually loading a kernel, the handler is also responsible for
839      * arranging that we start it correctly.
840      */
841     for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
842         qemu_register_reset(do_cpu_reset, ARM_CPU(cs));
843     }
844 }
845 
846 static const TypeInfo arm_linux_boot_if_info = {
847     .name = TYPE_ARM_LINUX_BOOT_IF,
848     .parent = TYPE_INTERFACE,
849     .class_size = sizeof(ARMLinuxBootIfClass),
850 };
851 
852 static void arm_linux_boot_register_types(void)
853 {
854     type_register_static(&arm_linux_boot_if_info);
855 }
856 
857 type_init(arm_linux_boot_register_types)
858