1
2
3
4
5
6
7
8
9
10#include "qemu/osdep.h"
11#include "qapi/error.h"
12#include <libfdt.h>
13#include "hw/hw.h"
14#include "hw/arm/arm.h"
15#include "hw/arm/linux-boot-if.h"
16#include "sysemu/kvm.h"
17#include "sysemu/sysemu.h"
18#include "sysemu/numa.h"
19#include "hw/boards.h"
20#include "hw/loader.h"
21#include "elf.h"
22#include "sysemu/device_tree.h"
23#include "qemu/config-file.h"
24#include "exec/address-spaces.h"
25
26
27
28
29
30#define KERNEL_ARGS_ADDR 0x100
31#define KERNEL_LOAD_ADDR 0x00010000
32#define KERNEL64_LOAD_ADDR 0x00080000
33
34#define ARM64_TEXT_OFFSET_OFFSET 8
35#define ARM64_MAGIC_OFFSET 56
36
37typedef enum {
38 FIXUP_NONE = 0,
39 FIXUP_TERMINATOR,
40 FIXUP_BOARDID,
41 FIXUP_BOARD_SETUP,
42 FIXUP_ARGPTR,
43 FIXUP_ENTRYPOINT,
44 FIXUP_GIC_CPU_IF,
45 FIXUP_BOOTREG,
46 FIXUP_DSB,
47 FIXUP_MAX,
48} FixupType;
49
50typedef struct ARMInsnFixup {
51 uint32_t insn;
52 FixupType fixup;
53} ARMInsnFixup;
54
55static const ARMInsnFixup bootloader_aarch64[] = {
56 { 0x580000c0 },
57 { 0xaa1f03e1 },
58 { 0xaa1f03e2 },
59 { 0xaa1f03e3 },
60 { 0x58000084 },
61 { 0xd61f0080 },
62 { 0, FIXUP_ARGPTR },
63 { 0 },
64 { 0, FIXUP_ENTRYPOINT },
65 { 0 },
66 { 0, FIXUP_TERMINATOR }
67};
68
69
70
71
72
73
74
75static const ARMInsnFixup bootloader[] = {
76 { 0xe28fe004 },
77 { 0xe51ff004 },
78 { 0, FIXUP_BOARD_SETUP },
79#define BOOTLOADER_NO_BOARD_SETUP_OFFSET 3
80 { 0xe3a00000 },
81 { 0xe59f1004 },
82 { 0xe59f2004 },
83 { 0xe59ff004 },
84 { 0, FIXUP_BOARDID },
85 { 0, FIXUP_ARGPTR },
86 { 0, FIXUP_ENTRYPOINT },
87 { 0, FIXUP_TERMINATOR }
88};
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104#define DSB_INSN 0xf57ff04f
105#define CP15_DSB_INSN 0xee070f9a
106
107static const ARMInsnFixup smpboot[] = {
108 { 0xe59f2028 },
109 { 0xe59f0028 },
110 { 0xe3a01001 },
111 { 0xe5821000 },
112 { 0xe3a010ff },
113 { 0xe5821004 },
114 { 0, FIXUP_DSB },
115 { 0xe320f003 },
116 { 0xe5901000 },
117 { 0xe1110001 },
118 { 0x0afffffb },
119 { 0xe12fff11 },
120 { 0, FIXUP_GIC_CPU_IF },
121 { 0, FIXUP_BOOTREG },
122 { 0, FIXUP_TERMINATOR }
123};
124
125static void write_bootloader(const char *name, hwaddr addr,
126 const ARMInsnFixup *insns, uint32_t *fixupcontext)
127{
128
129
130
131
132
133 int i, len;
134 uint32_t *code;
135
136 len = 0;
137 while (insns[len].fixup != FIXUP_TERMINATOR) {
138 len++;
139 }
140
141 code = g_new0(uint32_t, len);
142
143 for (i = 0; i < len; i++) {
144 uint32_t insn = insns[i].insn;
145 FixupType fixup = insns[i].fixup;
146
147 switch (fixup) {
148 case FIXUP_NONE:
149 break;
150 case FIXUP_BOARDID:
151 case FIXUP_BOARD_SETUP:
152 case FIXUP_ARGPTR:
153 case FIXUP_ENTRYPOINT:
154 case FIXUP_GIC_CPU_IF:
155 case FIXUP_BOOTREG:
156 case FIXUP_DSB:
157 insn = fixupcontext[fixup];
158 break;
159 default:
160 abort();
161 }
162 code[i] = tswap32(insn);
163 }
164
165 rom_add_blob_fixed(name, code, len * sizeof(uint32_t), addr);
166
167 g_free(code);
168}
169
170static void default_write_secondary(ARMCPU *cpu,
171 const struct arm_boot_info *info)
172{
173 uint32_t fixupcontext[FIXUP_MAX];
174
175 fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr;
176 fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr;
177 if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
178 fixupcontext[FIXUP_DSB] = DSB_INSN;
179 } else {
180 fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
181 }
182
183 write_bootloader("smpboot", info->smp_loader_start,
184 smpboot, fixupcontext);
185}
186
187void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,
188 const struct arm_boot_info *info,
189 hwaddr mvbar_addr)
190{
191 int n;
192 uint32_t mvbar_blob[] = {
193
194
195
196
197 0xeafffffe,
198 0xeafffffe,
199 0xe1b0f00e,
200 0xeafffffe,
201 0xeafffffe,
202 0xeafffffe,
203 0xeafffffe,
204 0xeafffffe,
205 };
206 uint32_t board_setup_blob[] = {
207
208 0xe3a00e00 + (mvbar_addr >> 4),
209 0xee0c0f30,
210 0xee110f11,
211 0xe3800031,
212 0xee010f11,
213 0xe1a0100e,
214 0xe1600070,
215 0xe1a0f001,
216 };
217
218
219 assert((mvbar_addr & 0x1f) == 0 && (mvbar_addr >> 4) < 0x100);
220
221
222 assert((mvbar_addr + sizeof(mvbar_blob) <= info->board_setup_addr)
223 || (info->board_setup_addr + sizeof(board_setup_blob) <= mvbar_addr));
224
225 for (n = 0; n < ARRAY_SIZE(mvbar_blob); n++) {
226 mvbar_blob[n] = tswap32(mvbar_blob[n]);
227 }
228 rom_add_blob_fixed("board-setup-mvbar", mvbar_blob, sizeof(mvbar_blob),
229 mvbar_addr);
230
231 for (n = 0; n < ARRAY_SIZE(board_setup_blob); n++) {
232 board_setup_blob[n] = tswap32(board_setup_blob[n]);
233 }
234 rom_add_blob_fixed("board-setup", board_setup_blob,
235 sizeof(board_setup_blob), info->board_setup_addr);
236}
237
238static void default_reset_secondary(ARMCPU *cpu,
239 const struct arm_boot_info *info)
240{
241 CPUState *cs = CPU(cpu);
242
243 address_space_stl_notdirty(&address_space_memory, info->smp_bootreg_addr,
244 0, MEMTXATTRS_UNSPECIFIED, NULL);
245 cpu_set_pc(cs, info->smp_loader_start);
246}
247
248static inline bool have_dtb(const struct arm_boot_info *info)
249{
250 return info->dtb_filename || info->get_dtb;
251}
252
253#define WRITE_WORD(p, value) do { \
254 address_space_stl_notdirty(&address_space_memory, p, value, \
255 MEMTXATTRS_UNSPECIFIED, NULL); \
256 p += 4; \
257} while (0)
258
259static void set_kernel_args(const struct arm_boot_info *info)
260{
261 int initrd_size = info->initrd_size;
262 hwaddr base = info->loader_start;
263 hwaddr p;
264
265 p = base + KERNEL_ARGS_ADDR;
266
267 WRITE_WORD(p, 5);
268 WRITE_WORD(p, 0x54410001);
269 WRITE_WORD(p, 1);
270 WRITE_WORD(p, 0x1000);
271 WRITE_WORD(p, 0);
272
273
274 WRITE_WORD(p, 4);
275 WRITE_WORD(p, 0x54410002);
276 WRITE_WORD(p, info->ram_size);
277 WRITE_WORD(p, info->loader_start);
278 if (initrd_size) {
279
280 WRITE_WORD(p, 4);
281 WRITE_WORD(p, 0x54420005);
282 WRITE_WORD(p, info->initrd_start);
283 WRITE_WORD(p, initrd_size);
284 }
285 if (info->kernel_cmdline && *info->kernel_cmdline) {
286
287 int cmdline_size;
288
289 cmdline_size = strlen(info->kernel_cmdline);
290 cpu_physical_memory_write(p + 8, info->kernel_cmdline,
291 cmdline_size + 1);
292 cmdline_size = (cmdline_size >> 2) + 1;
293 WRITE_WORD(p, cmdline_size + 2);
294 WRITE_WORD(p, 0x54410009);
295 p += cmdline_size * 4;
296 }
297 if (info->atag_board) {
298
299 int atag_board_len;
300 uint8_t atag_board_buf[0x1000];
301
302 atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
303 WRITE_WORD(p, (atag_board_len + 8) >> 2);
304 WRITE_WORD(p, 0x414f4d50);
305 cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
306 p += atag_board_len;
307 }
308
309 WRITE_WORD(p, 0);
310 WRITE_WORD(p, 0);
311}
312
313static void set_kernel_args_old(const struct arm_boot_info *info)
314{
315 hwaddr p;
316 const char *s;
317 int initrd_size = info->initrd_size;
318 hwaddr base = info->loader_start;
319
320
321 p = base + KERNEL_ARGS_ADDR;
322
323 WRITE_WORD(p, 4096);
324
325 WRITE_WORD(p, info->ram_size / 4096);
326
327 WRITE_WORD(p, 0);
328#define FLAG_READONLY 1
329#define FLAG_RDLOAD 4
330#define FLAG_RDPROMPT 8
331
332 WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
333
334 WRITE_WORD(p, (31 << 8) | 0);
335
336 WRITE_WORD(p, 0);
337
338 WRITE_WORD(p, 0);
339
340 WRITE_WORD(p, 0);
341
342 WRITE_WORD(p, 0);
343
344 WRITE_WORD(p, 0);
345
346
347
348
349 WRITE_WORD(p, 0);
350
351 WRITE_WORD(p, 0);
352 WRITE_WORD(p, 0);
353 WRITE_WORD(p, 0);
354 WRITE_WORD(p, 0);
355
356 WRITE_WORD(p, 0);
357
358 if (initrd_size) {
359 WRITE_WORD(p, info->initrd_start);
360 } else {
361 WRITE_WORD(p, 0);
362 }
363
364 WRITE_WORD(p, initrd_size);
365
366 WRITE_WORD(p, 0);
367
368 WRITE_WORD(p, 0);
369
370 WRITE_WORD(p, 0);
371
372 WRITE_WORD(p, 0);
373
374 WRITE_WORD(p, 0);
375
376 while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
377 WRITE_WORD(p, 0);
378 }
379 s = info->kernel_cmdline;
380 if (s) {
381 cpu_physical_memory_write(p, s, strlen(s) + 1);
382 } else {
383 WRITE_WORD(p, 0);
384 }
385}
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
408 hwaddr addr_limit)
409{
410 void *fdt = NULL;
411 int size, rc;
412 uint32_t acells, scells;
413 char *nodename;
414 unsigned int i;
415 hwaddr mem_base, mem_len;
416
417 if (binfo->dtb_filename) {
418 char *filename;
419 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
420 if (!filename) {
421 fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
422 goto fail;
423 }
424
425 fdt = load_device_tree(filename, &size);
426 if (!fdt) {
427 fprintf(stderr, "Couldn't open dtb file %s\n", filename);
428 g_free(filename);
429 goto fail;
430 }
431 g_free(filename);
432 } else {
433 fdt = binfo->get_dtb(binfo, &size);
434 if (!fdt) {
435 fprintf(stderr, "Board was unable to create a dtb blob\n");
436 goto fail;
437 }
438 }
439
440 if (addr_limit > addr && size > (addr_limit - addr)) {
441
442
443
444
445 g_free(fdt);
446 return 0;
447 }
448
449 acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells",
450 NULL, &error_fatal);
451 scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells",
452 NULL, &error_fatal);
453 if (acells == 0 || scells == 0) {
454 fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
455 goto fail;
456 }
457
458 if (scells < 2 && binfo->ram_size >= (1ULL << 32)) {
459
460
461
462 fprintf(stderr, "qemu: dtb file not compatible with "
463 "RAM size > 4GB\n");
464 goto fail;
465 }
466
467 if (nb_numa_nodes > 0) {
468
469
470
471
472 qemu_fdt_nop_node(fdt, "/memory");
473 mem_base = binfo->loader_start;
474 for (i = 0; i < nb_numa_nodes; i++) {
475 mem_len = numa_info[i].node_mem;
476 nodename = g_strdup_printf("/memory@%" PRIx64, mem_base);
477 qemu_fdt_add_subnode(fdt, nodename);
478 qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
479 rc = qemu_fdt_setprop_sized_cells(fdt, nodename, "reg",
480 acells, mem_base,
481 scells, mem_len);
482 if (rc < 0) {
483 fprintf(stderr, "couldn't set %s/reg for node %d\n", nodename,
484 i);
485 goto fail;
486 }
487
488 qemu_fdt_setprop_cell(fdt, nodename, "numa-node-id", i);
489 mem_base += mem_len;
490 g_free(nodename);
491 }
492 } else {
493 Error *err = NULL;
494
495 rc = fdt_path_offset(fdt, "/memory");
496 if (rc < 0) {
497 qemu_fdt_add_subnode(fdt, "/memory");
498 }
499
500 if (!qemu_fdt_getprop(fdt, "/memory", "device_type", NULL, &err)) {
501 qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory");
502 }
503
504 rc = qemu_fdt_setprop_sized_cells(fdt, "/memory", "reg",
505 acells, binfo->loader_start,
506 scells, binfo->ram_size);
507 if (rc < 0) {
508 fprintf(stderr, "couldn't set /memory/reg\n");
509 goto fail;
510 }
511 }
512
513 rc = fdt_path_offset(fdt, "/chosen");
514 if (rc < 0) {
515 qemu_fdt_add_subnode(fdt, "/chosen");
516 }
517
518 if (binfo->kernel_cmdline && *binfo->kernel_cmdline) {
519 rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
520 binfo->kernel_cmdline);
521 if (rc < 0) {
522 fprintf(stderr, "couldn't set /chosen/bootargs\n");
523 goto fail;
524 }
525 }
526
527 if (binfo->initrd_size) {
528 rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
529 binfo->initrd_start);
530 if (rc < 0) {
531 fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
532 goto fail;
533 }
534
535 rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
536 binfo->initrd_start + binfo->initrd_size);
537 if (rc < 0) {
538 fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
539 goto fail;
540 }
541 }
542
543 if (binfo->modify_dtb) {
544 binfo->modify_dtb(binfo, fdt);
545 }
546
547 qemu_fdt_dumpdtb(fdt, size);
548
549
550
551
552 rom_add_blob_fixed("dtb", fdt, size, addr);
553
554 g_free(fdt);
555
556 return size;
557
558fail:
559 g_free(fdt);
560 return -1;
561}
562
563static void do_cpu_reset(void *opaque)
564{
565 ARMCPU *cpu = opaque;
566 CPUState *cs = CPU(cpu);
567 CPUARMState *env = &cpu->env;
568 const struct arm_boot_info *info = env->boot_info;
569
570 cpu_reset(cs);
571 if (info) {
572 if (!info->is_linux) {
573 int i;
574
575 uint64_t entry = info->entry;
576
577 switch (info->endianness) {
578 case ARM_ENDIANNESS_LE:
579 env->cp15.sctlr_el[1] &= ~SCTLR_E0E;
580 for (i = 1; i < 4; ++i) {
581 env->cp15.sctlr_el[i] &= ~SCTLR_EE;
582 }
583 env->uncached_cpsr &= ~CPSR_E;
584 break;
585 case ARM_ENDIANNESS_BE8:
586 env->cp15.sctlr_el[1] |= SCTLR_E0E;
587 for (i = 1; i < 4; ++i) {
588 env->cp15.sctlr_el[i] |= SCTLR_EE;
589 }
590 env->uncached_cpsr |= CPSR_E;
591 break;
592 case ARM_ENDIANNESS_BE32:
593 env->cp15.sctlr_el[1] |= SCTLR_B;
594 break;
595 case ARM_ENDIANNESS_UNKNOWN:
596 break;
597 default:
598 g_assert_not_reached();
599 }
600
601 if (!env->aarch64) {
602 env->thumb = info->entry & 1;
603 entry &= 0xfffffffe;
604 }
605 cpu_set_pc(cs, entry);
606 } else {
607
608
609
610
611
612
613 if (arm_feature(env, ARM_FEATURE_EL3)) {
614
615
616
617
618
619
620 if (env->aarch64) {
621 env->cp15.scr_el3 |= SCR_RW;
622 if (arm_feature(env, ARM_FEATURE_EL2)) {
623 env->cp15.hcr_el2 |= HCR_RW;
624 env->pstate = PSTATE_MODE_EL2h;
625 } else {
626 env->pstate = PSTATE_MODE_EL1h;
627 }
628 }
629
630
631 if (!info->secure_boot &&
632 (cs != first_cpu || !info->secure_board_setup)) {
633
634 env->cp15.scr_el3 |= SCR_NS;
635 }
636 }
637
638 if (cs == first_cpu) {
639 cpu_set_pc(cs, info->loader_start);
640
641 if (!have_dtb(info)) {
642 if (old_param) {
643 set_kernel_args_old(info);
644 } else {
645 set_kernel_args(info);
646 }
647 }
648 } else {
649 info->secondary_cpu_reset_hook(cpu, info);
650 }
651 }
652 }
653}
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
673 uint16_t data_key, const char *image_name,
674 bool try_decompress)
675{
676 size_t size = -1;
677 uint8_t *data;
678
679 if (image_name == NULL) {
680 return;
681 }
682
683 if (try_decompress) {
684 size = load_image_gzipped_buffer(image_name,
685 LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
686 }
687
688 if (size == (size_t)-1) {
689 gchar *contents;
690 gsize length;
691
692 if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
693 fprintf(stderr, "failed to load \"%s\"\n", image_name);
694 exit(1);
695 }
696 size = length;
697 data = (uint8_t *)contents;
698 }
699
700 fw_cfg_add_i32(fw_cfg, size_key, size);
701 fw_cfg_add_bytes(fw_cfg, data_key, data, size);
702}
703
704static int do_arm_linux_init(Object *obj, void *opaque)
705{
706 if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
707 ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj);
708 ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj);
709 struct arm_boot_info *info = opaque;
710
711 if (albifc->arm_linux_init) {
712 albifc->arm_linux_init(albif, info->secure_boot);
713 }
714 }
715 return 0;
716}
717
718static uint64_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,
719 uint64_t *lowaddr, uint64_t *highaddr,
720 int elf_machine)
721{
722 bool elf_is64;
723 union {
724 Elf32_Ehdr h32;
725 Elf64_Ehdr h64;
726 } elf_header;
727 int data_swab = 0;
728 bool big_endian;
729 uint64_t ret = -1;
730 Error *err = NULL;
731
732
733 load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err);
734 if (err) {
735 return ret;
736 }
737
738 if (elf_is64) {
739 big_endian = elf_header.h64.e_ident[EI_DATA] == ELFDATA2MSB;
740 info->endianness = big_endian ? ARM_ENDIANNESS_BE8
741 : ARM_ENDIANNESS_LE;
742 } else {
743 big_endian = elf_header.h32.e_ident[EI_DATA] == ELFDATA2MSB;
744 if (big_endian) {
745 if (bswap32(elf_header.h32.e_flags) & EF_ARM_BE8) {
746 info->endianness = ARM_ENDIANNESS_BE8;
747 } else {
748 info->endianness = ARM_ENDIANNESS_BE32;
749
750
751
752
753
754
755
756 data_swab = 2;
757 }
758 } else {
759 info->endianness = ARM_ENDIANNESS_LE;
760 }
761 }
762
763 ret = load_elf(info->kernel_filename, NULL, NULL,
764 pentry, lowaddr, highaddr, big_endian, elf_machine,
765 1, data_swab);
766 if (ret <= 0) {
767
768 exit(1);
769 }
770
771 return ret;
772}
773
774static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base,
775 hwaddr *entry)
776{
777 hwaddr kernel_load_offset = KERNEL64_LOAD_ADDR;
778 uint8_t *buffer;
779 int size;
780
781
782 size = load_image_gzipped_buffer(filename, LOAD_IMAGE_MAX_GUNZIP_BYTES,
783 &buffer);
784
785 if (size < 0) {
786 gsize len;
787
788
789 if (!g_file_get_contents(filename, (char **)&buffer, &len, NULL)) {
790 return -1;
791 }
792 size = len;
793 }
794
795
796 if (memcmp(buffer + ARM64_MAGIC_OFFSET, "ARM\x64", 4) == 0) {
797 uint64_t hdrvals[2];
798
799
800
801
802
803 memcpy(&hdrvals, buffer + ARM64_TEXT_OFFSET_OFFSET, sizeof(hdrvals));
804 if (hdrvals[1] != 0) {
805 kernel_load_offset = le64_to_cpu(hdrvals[0]);
806 }
807 }
808
809 *entry = mem_base + kernel_load_offset;
810 rom_add_blob_fixed(filename, buffer, size, *entry);
811
812 g_free(buffer);
813
814 return size;
815}
816
817static void arm_load_kernel_notify(Notifier *notifier, void *data)
818{
819 CPUState *cs;
820 int kernel_size;
821 int initrd_size;
822 int is_linux = 0;
823 uint64_t elf_entry, elf_low_addr, elf_high_addr;
824 int elf_machine;
825 hwaddr entry;
826 static const ARMInsnFixup *primary_loader;
827 ArmLoadKernelNotifier *n = DO_UPCAST(ArmLoadKernelNotifier,
828 notifier, notifier);
829 ARMCPU *cpu = n->cpu;
830 struct arm_boot_info *info =
831 container_of(n, struct arm_boot_info, load_kernel_notifier);
832
833
834
835
836
837 assert(!(info->secure_board_setup && kvm_enabled()));
838
839 info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb");
840
841
842 if (!info->kernel_filename || info->firmware_loaded) {
843
844 if (have_dtb(info)) {
845
846
847
848
849 if (load_dtb(info->loader_start, info, 0) < 0) {
850 exit(1);
851 }
852 }
853
854 if (info->kernel_filename) {
855 FWCfgState *fw_cfg;
856 bool try_decompressing_kernel;
857
858 fw_cfg = fw_cfg_find();
859 try_decompressing_kernel = arm_feature(&cpu->env,
860 ARM_FEATURE_AARCH64);
861
862
863
864
865
866 load_image_to_fw_cfg(fw_cfg,
867 FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
868 info->kernel_filename,
869 try_decompressing_kernel);
870 load_image_to_fw_cfg(fw_cfg,
871 FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
872 info->initrd_filename, false);
873
874 if (info->kernel_cmdline) {
875 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
876 strlen(info->kernel_cmdline) + 1);
877 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
878 info->kernel_cmdline);
879 }
880 }
881
882
883
884
885 return;
886 }
887
888 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
889 primary_loader = bootloader_aarch64;
890 elf_machine = EM_AARCH64;
891 } else {
892 primary_loader = bootloader;
893 if (!info->write_board_setup) {
894 primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET;
895 }
896 elf_machine = EM_ARM;
897 }
898
899 if (!info->secondary_cpu_reset_hook) {
900 info->secondary_cpu_reset_hook = default_reset_secondary;
901 }
902 if (!info->write_secondary_boot) {
903 info->write_secondary_boot = default_write_secondary;
904 }
905
906 if (info->nb_cpus == 0)
907 info->nb_cpus = 1;
908
909
910
911
912
913
914
915
916
917
918
919 info->initrd_start = info->loader_start +
920 MIN(info->ram_size / 2, 128 * 1024 * 1024);
921
922
923 kernel_size = arm_load_elf(info, &elf_entry, &elf_low_addr,
924 &elf_high_addr, elf_machine);
925 if (kernel_size > 0 && have_dtb(info)) {
926
927
928
929 if (elf_low_addr > info->loader_start
930 || elf_high_addr < info->loader_start) {
931
932
933
934 if (elf_low_addr < info->loader_start) {
935 elf_low_addr = 0;
936 }
937 if (load_dtb(info->loader_start, info, elf_low_addr) < 0) {
938 exit(1);
939 }
940 }
941 }
942 entry = elf_entry;
943 if (kernel_size < 0) {
944 kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
945 &is_linux, NULL, NULL);
946 }
947 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
948 kernel_size = load_aarch64_image(info->kernel_filename,
949 info->loader_start, &entry);
950 is_linux = 1;
951 } else if (kernel_size < 0) {
952
953 entry = info->loader_start + KERNEL_LOAD_ADDR;
954 kernel_size = load_image_targphys(info->kernel_filename, entry,
955 info->ram_size - KERNEL_LOAD_ADDR);
956 is_linux = 1;
957 }
958 if (kernel_size < 0) {
959 fprintf(stderr, "qemu: could not load kernel '%s'\n",
960 info->kernel_filename);
961 exit(1);
962 }
963 info->entry = entry;
964 if (is_linux) {
965 uint32_t fixupcontext[FIXUP_MAX];
966
967 if (info->initrd_filename) {
968 initrd_size = load_ramdisk(info->initrd_filename,
969 info->initrd_start,
970 info->ram_size -
971 info->initrd_start);
972 if (initrd_size < 0) {
973 initrd_size = load_image_targphys(info->initrd_filename,
974 info->initrd_start,
975 info->ram_size -
976 info->initrd_start);
977 }
978 if (initrd_size < 0) {
979 fprintf(stderr, "qemu: could not load initrd '%s'\n",
980 info->initrd_filename);
981 exit(1);
982 }
983 } else {
984 initrd_size = 0;
985 }
986 info->initrd_size = initrd_size;
987
988 fixupcontext[FIXUP_BOARDID] = info->board_id;
989 fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr;
990
991
992
993
994 if (have_dtb(info)) {
995 hwaddr align;
996 hwaddr dtb_start;
997
998 if (elf_machine == EM_AARCH64) {
999
1000
1001
1002
1003
1004
1005
1006 align = 2 * 1024 * 1024;
1007 } else {
1008
1009
1010
1011
1012 align = 4096;
1013 }
1014
1015
1016 dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size, align);
1017 if (load_dtb(dtb_start, info, 0) < 0) {
1018 exit(1);
1019 }
1020 fixupcontext[FIXUP_ARGPTR] = dtb_start;
1021 } else {
1022 fixupcontext[FIXUP_ARGPTR] = info->loader_start + KERNEL_ARGS_ADDR;
1023 if (info->ram_size >= (1ULL << 32)) {
1024 fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
1025 " Linux kernel using ATAGS (try passing a device tree"
1026 " using -dtb)\n");
1027 exit(1);
1028 }
1029 }
1030 fixupcontext[FIXUP_ENTRYPOINT] = entry;
1031
1032 write_bootloader("bootloader", info->loader_start,
1033 primary_loader, fixupcontext);
1034
1035 if (info->nb_cpus > 1) {
1036 info->write_secondary_boot(cpu, info);
1037 }
1038 if (info->write_board_setup) {
1039 info->write_board_setup(cpu, info);
1040 }
1041
1042
1043
1044
1045 object_child_foreach_recursive(object_get_root(),
1046 do_arm_linux_init, info);
1047 }
1048 info->is_linux = is_linux;
1049
1050 for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
1051 ARM_CPU(cs)->env.boot_info = info;
1052 }
1053}
1054
1055void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
1056{
1057 CPUState *cs;
1058
1059 info->load_kernel_notifier.cpu = cpu;
1060 info->load_kernel_notifier.notifier.notify = arm_load_kernel_notify;
1061 qemu_add_machine_init_done_notifier(&info->load_kernel_notifier.notifier);
1062
1063
1064
1065
1066
1067
1068 for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
1069 qemu_register_reset(do_cpu_reset, ARM_CPU(cs));
1070 }
1071}
1072
1073static const TypeInfo arm_linux_boot_if_info = {
1074 .name = TYPE_ARM_LINUX_BOOT_IF,
1075 .parent = TYPE_INTERFACE,
1076 .class_size = sizeof(ARMLinuxBootIfClass),
1077};
1078
1079static void arm_linux_boot_register_types(void)
1080{
1081 type_register_static(&arm_linux_boot_if_info);
1082}
1083
1084type_init(arm_linux_boot_register_types)
1085