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