blob: 3d1f4a255b485eb52864e3fefb16c5da9cef37e1 [file] [log] [blame]
ths5fafdf22007-09-16 21:08:06 +00001/*
pbrook16406952006-04-27 23:15:07 +00002 * ARM kernel loader.
3 *
pbrook9ee6e8b2007-11-11 00:04:49 +00004 * Copyright (c) 2006-2007 CodeSourcery.
pbrook16406952006-04-27 23:15:07 +00005 * Written by Paul Brook
6 *
Matthew Fernandez8e31bf32011-06-26 12:21:35 +10007 * This code is licensed under the GPL.
pbrook16406952006-04-27 23:15:07 +00008 */
9
Grant Likely412beee2012-03-02 11:56:38 +000010#include "config.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010011#include "hw/hw.h"
Peter Maydellbd2be152013-04-09 15:26:55 +010012#include "hw/arm/arm.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010013#include "sysemu/sysemu.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010014#include "hw/boards.h"
15#include "hw/loader.h"
Blue Swirlca20cf32009-09-20 14:58:02 +000016#include "elf.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010017#include "sysemu/device_tree.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/config-file.h"
Edgar E. Iglesias2198a122013-11-28 10:13:41 +010019#include "exec/address-spaces.h"
pbrook16406952006-04-27 23:15:07 +000020
Mian M. Hamayun4d9ebf72013-12-17 19:42:30 +000021/* Kernel boot protocol is specified in the kernel docs
22 * Documentation/arm/Booting and Documentation/arm64/booting.txt
23 * They have different preferred image load offsets from system RAM base.
24 */
pbrook16406952006-04-27 23:15:07 +000025#define KERNEL_ARGS_ADDR 0x100
26#define KERNEL_LOAD_ADDR 0x00010000
Mian M. Hamayun4d9ebf72013-12-17 19:42:30 +000027#define KERNEL64_LOAD_ADDR 0x00080000
pbrook16406952006-04-27 23:15:07 +000028
Peter Maydell47b1da82013-12-17 19:42:30 +000029typedef enum {
30 FIXUP_NONE = 0, /* do nothing */
31 FIXUP_TERMINATOR, /* end of insns */
32 FIXUP_BOARDID, /* overwrite with board ID number */
33 FIXUP_ARGPTR, /* overwrite with pointer to kernel args */
34 FIXUP_ENTRYPOINT, /* overwrite with kernel entry point */
35 FIXUP_GIC_CPU_IF, /* overwrite with GIC CPU interface address */
36 FIXUP_BOOTREG, /* overwrite with boot register address */
37 FIXUP_DSB, /* overwrite with correct DSB insn for cpu */
38 FIXUP_MAX,
39} FixupType;
40
41typedef struct ARMInsnFixup {
42 uint32_t insn;
43 FixupType fixup;
44} ARMInsnFixup;
45
Mian M. Hamayun4d9ebf72013-12-17 19:42:30 +000046static const ARMInsnFixup bootloader_aarch64[] = {
47 { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
48 { 0xaa1f03e1 }, /* mov x1, xzr */
49 { 0xaa1f03e2 }, /* mov x2, xzr */
50 { 0xaa1f03e3 }, /* mov x3, xzr */
51 { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
52 { 0xd61f0080 }, /* br x4 ; Jump to the kernel entry point */
53 { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */
54 { 0 }, /* .word @DTB Higher 32-bits */
55 { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */
56 { 0 }, /* .word @Kernel Entry Higher 32-bits */
57 { 0, FIXUP_TERMINATOR }
58};
59
pbrook16406952006-04-27 23:15:07 +000060/* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
Peter Maydell47b1da82013-12-17 19:42:30 +000061static const ARMInsnFixup bootloader[] = {
62 { 0xe3a00000 }, /* mov r0, #0 */
63 { 0xe59f1004 }, /* ldr r1, [pc, #4] */
64 { 0xe59f2004 }, /* ldr r2, [pc, #4] */
65 { 0xe59ff004 }, /* ldr pc, [pc, #4] */
66 { 0, FIXUP_BOARDID },
67 { 0, FIXUP_ARGPTR },
68 { 0, FIXUP_ENTRYPOINT },
69 { 0, FIXUP_TERMINATOR }
pbrook16406952006-04-27 23:15:07 +000070};
71
Mark Langsdorf9d5ba9b2012-01-26 11:43:48 +000072/* Handling for secondary CPU boot in a multicore system.
73 * Unlike the uniprocessor/primary CPU boot, this is platform
74 * dependent. The default code here is based on the secondary
75 * CPU boot protocol used on realview/vexpress boards, with
76 * some parameterisation to increase its flexibility.
77 * QEMU platform models for which this code is not appropriate
78 * should override write_secondary_boot and secondary_cpu_reset_hook
79 * instead.
80 *
81 * This code enables the interrupt controllers for the secondary
82 * CPUs and then puts all the secondary CPUs into a loop waiting
83 * for an interprocessor interrupt and polling a configurable
84 * location for the kernel secondary CPU entry point.
85 */
Peter Maydellbf471f72012-12-11 11:30:37 +000086#define DSB_INSN 0xf57ff04f
87#define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
88
Peter Maydell47b1da82013-12-17 19:42:30 +000089static const ARMInsnFixup smpboot[] = {
90 { 0xe59f2028 }, /* ldr r2, gic_cpu_if */
91 { 0xe59f0028 }, /* ldr r0, bootreg_addr */
92 { 0xe3a01001 }, /* mov r1, #1 */
93 { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */
94 { 0xe3a010ff }, /* mov r1, #0xff */
95 { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
96 { 0, FIXUP_DSB }, /* dsb */
97 { 0xe320f003 }, /* wfi */
98 { 0xe5901000 }, /* ldr r1, [r0] */
99 { 0xe1110001 }, /* tst r1, r1 */
100 { 0x0afffffb }, /* beq <wfi> */
101 { 0xe12fff11 }, /* bx r1 */
102 { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */
103 { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */
104 { 0, FIXUP_TERMINATOR }
pbrook9ee6e8b2007-11-11 00:04:49 +0000105};
106
Peter Maydell47b1da82013-12-17 19:42:30 +0000107static void write_bootloader(const char *name, hwaddr addr,
108 const ARMInsnFixup *insns, uint32_t *fixupcontext)
109{
110 /* Fix up the specified bootloader fragment and write it into
111 * guest memory using rom_add_blob_fixed(). fixupcontext is
112 * an array giving the values to write in for the fixup types
113 * which write a value into the code array.
114 */
115 int i, len;
116 uint32_t *code;
117
118 len = 0;
119 while (insns[len].fixup != FIXUP_TERMINATOR) {
120 len++;
121 }
122
123 code = g_new0(uint32_t, len);
124
125 for (i = 0; i < len; i++) {
126 uint32_t insn = insns[i].insn;
127 FixupType fixup = insns[i].fixup;
128
129 switch (fixup) {
130 case FIXUP_NONE:
131 break;
132 case FIXUP_BOARDID:
133 case FIXUP_ARGPTR:
134 case FIXUP_ENTRYPOINT:
135 case FIXUP_GIC_CPU_IF:
136 case FIXUP_BOOTREG:
137 case FIXUP_DSB:
138 insn = fixupcontext[fixup];
139 break;
140 default:
141 abort();
142 }
143 code[i] = tswap32(insn);
144 }
145
146 rom_add_blob_fixed(name, code, len * sizeof(uint32_t), addr);
147
148 g_free(code);
149}
150
Andreas Färber9543b0c2012-05-14 00:08:10 +0200151static void default_write_secondary(ARMCPU *cpu,
Mark Langsdorf9d5ba9b2012-01-26 11:43:48 +0000152 const struct arm_boot_info *info)
153{
Peter Maydell47b1da82013-12-17 19:42:30 +0000154 uint32_t fixupcontext[FIXUP_MAX];
155
156 fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr;
157 fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr;
158 if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
159 fixupcontext[FIXUP_DSB] = DSB_INSN;
160 } else {
161 fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
Mark Langsdorf9d5ba9b2012-01-26 11:43:48 +0000162 }
Peter Maydell47b1da82013-12-17 19:42:30 +0000163
164 write_bootloader("smpboot", info->smp_loader_start,
165 smpboot, fixupcontext);
Mark Langsdorf9d5ba9b2012-01-26 11:43:48 +0000166}
167
Andreas Färber5d309322012-05-14 01:05:40 +0200168static void default_reset_secondary(ARMCPU *cpu,
Mark Langsdorf9d5ba9b2012-01-26 11:43:48 +0000169 const struct arm_boot_info *info)
170{
Andreas Färber5d309322012-05-14 01:05:40 +0200171 CPUARMState *env = &cpu->env;
172
Edgar E. Iglesias2198a122013-11-28 10:13:41 +0100173 stl_phys_notdirty(&address_space_memory, info->smp_bootreg_addr, 0);
Mark Langsdorf9d5ba9b2012-01-26 11:43:48 +0000174 env->regs[15] = info->smp_loader_start;
175}
176
Peter Maydell83bfffe2014-01-31 14:47:32 +0000177static inline bool have_dtb(const struct arm_boot_info *info)
178{
179 return info->dtb_filename || info->get_dtb;
180}
181
pbrook52b43732009-04-09 17:19:47 +0000182#define WRITE_WORD(p, value) do { \
Edgar E. Iglesias2198a122013-11-28 10:13:41 +0100183 stl_phys_notdirty(&address_space_memory, p, value); \
pbrook52b43732009-04-09 17:19:47 +0000184 p += 4; \
185} while (0)
pbrook16406952006-04-27 23:15:07 +0000186
Stefan Weil761c9eb2012-01-29 08:52:15 +0100187static void set_kernel_args(const struct arm_boot_info *info)
pbrook52b43732009-04-09 17:19:47 +0000188{
Stefan Weil761c9eb2012-01-29 08:52:15 +0100189 int initrd_size = info->initrd_size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200190 hwaddr base = info->loader_start;
191 hwaddr p;
pbrook52b43732009-04-09 17:19:47 +0000192
193 p = base + KERNEL_ARGS_ADDR;
pbrook16406952006-04-27 23:15:07 +0000194 /* ATAG_CORE */
pbrook52b43732009-04-09 17:19:47 +0000195 WRITE_WORD(p, 5);
196 WRITE_WORD(p, 0x54410001);
197 WRITE_WORD(p, 1);
198 WRITE_WORD(p, 0x1000);
199 WRITE_WORD(p, 0);
pbrook16406952006-04-27 23:15:07 +0000200 /* ATAG_MEM */
balrogf93eb9f2008-04-14 20:27:51 +0000201 /* TODO: handle multiple chips on one ATAG list */
pbrook52b43732009-04-09 17:19:47 +0000202 WRITE_WORD(p, 4);
203 WRITE_WORD(p, 0x54410002);
204 WRITE_WORD(p, info->ram_size);
205 WRITE_WORD(p, info->loader_start);
pbrook16406952006-04-27 23:15:07 +0000206 if (initrd_size) {
207 /* ATAG_INITRD2 */
pbrook52b43732009-04-09 17:19:47 +0000208 WRITE_WORD(p, 4);
209 WRITE_WORD(p, 0x54420005);
Peter Maydellfc53b7d2012-10-26 16:29:38 +0100210 WRITE_WORD(p, info->initrd_start);
pbrook52b43732009-04-09 17:19:47 +0000211 WRITE_WORD(p, initrd_size);
pbrook16406952006-04-27 23:15:07 +0000212 }
balrogf93eb9f2008-04-14 20:27:51 +0000213 if (info->kernel_cmdline && *info->kernel_cmdline) {
pbrook16406952006-04-27 23:15:07 +0000214 /* ATAG_CMDLINE */
215 int cmdline_size;
216
balrogf93eb9f2008-04-14 20:27:51 +0000217 cmdline_size = strlen(info->kernel_cmdline);
Stefan Weile1fe50d2013-04-12 20:53:58 +0200218 cpu_physical_memory_write(p + 8, info->kernel_cmdline,
pbrook52b43732009-04-09 17:19:47 +0000219 cmdline_size + 1);
pbrook16406952006-04-27 23:15:07 +0000220 cmdline_size = (cmdline_size >> 2) + 1;
pbrook52b43732009-04-09 17:19:47 +0000221 WRITE_WORD(p, cmdline_size + 2);
222 WRITE_WORD(p, 0x54410009);
223 p += cmdline_size * 4;
pbrook16406952006-04-27 23:15:07 +0000224 }
balrogf93eb9f2008-04-14 20:27:51 +0000225 if (info->atag_board) {
226 /* ATAG_BOARD */
227 int atag_board_len;
pbrook52b43732009-04-09 17:19:47 +0000228 uint8_t atag_board_buf[0x1000];
balrogf93eb9f2008-04-14 20:27:51 +0000229
pbrook52b43732009-04-09 17:19:47 +0000230 atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
231 WRITE_WORD(p, (atag_board_len + 8) >> 2);
232 WRITE_WORD(p, 0x414f4d50);
233 cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
balrogf93eb9f2008-04-14 20:27:51 +0000234 p += atag_board_len;
235 }
pbrook16406952006-04-27 23:15:07 +0000236 /* ATAG_END */
pbrook52b43732009-04-09 17:19:47 +0000237 WRITE_WORD(p, 0);
238 WRITE_WORD(p, 0);
pbrook16406952006-04-27 23:15:07 +0000239}
240
Stefan Weil761c9eb2012-01-29 08:52:15 +0100241static void set_kernel_args_old(const struct arm_boot_info *info)
balrog2b8f2d42007-07-27 22:08:46 +0000242{
Avi Kivitya8170e52012-10-23 12:30:10 +0200243 hwaddr p;
pbrook52b43732009-04-09 17:19:47 +0000244 const char *s;
Stefan Weil761c9eb2012-01-29 08:52:15 +0100245 int initrd_size = info->initrd_size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200246 hwaddr base = info->loader_start;
balrog2b8f2d42007-07-27 22:08:46 +0000247
248 /* see linux/include/asm-arm/setup.h */
pbrook52b43732009-04-09 17:19:47 +0000249 p = base + KERNEL_ARGS_ADDR;
balrog2b8f2d42007-07-27 22:08:46 +0000250 /* page_size */
pbrook52b43732009-04-09 17:19:47 +0000251 WRITE_WORD(p, 4096);
balrog2b8f2d42007-07-27 22:08:46 +0000252 /* nr_pages */
pbrook52b43732009-04-09 17:19:47 +0000253 WRITE_WORD(p, info->ram_size / 4096);
balrog2b8f2d42007-07-27 22:08:46 +0000254 /* ramdisk_size */
pbrook52b43732009-04-09 17:19:47 +0000255 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000256#define FLAG_READONLY 1
257#define FLAG_RDLOAD 4
258#define FLAG_RDPROMPT 8
259 /* flags */
pbrook52b43732009-04-09 17:19:47 +0000260 WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
balrog2b8f2d42007-07-27 22:08:46 +0000261 /* rootdev */
pbrook52b43732009-04-09 17:19:47 +0000262 WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */
balrog2b8f2d42007-07-27 22:08:46 +0000263 /* video_num_cols */
pbrook52b43732009-04-09 17:19:47 +0000264 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000265 /* video_num_rows */
pbrook52b43732009-04-09 17:19:47 +0000266 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000267 /* video_x */
pbrook52b43732009-04-09 17:19:47 +0000268 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000269 /* video_y */
pbrook52b43732009-04-09 17:19:47 +0000270 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000271 /* memc_control_reg */
pbrook52b43732009-04-09 17:19:47 +0000272 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000273 /* unsigned char sounddefault */
274 /* unsigned char adfsdrives */
275 /* unsigned char bytes_per_char_h */
276 /* unsigned char bytes_per_char_v */
pbrook52b43732009-04-09 17:19:47 +0000277 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000278 /* pages_in_bank[4] */
pbrook52b43732009-04-09 17:19:47 +0000279 WRITE_WORD(p, 0);
280 WRITE_WORD(p, 0);
281 WRITE_WORD(p, 0);
282 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000283 /* pages_in_vram */
pbrook52b43732009-04-09 17:19:47 +0000284 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000285 /* initrd_start */
Peter Maydellfc53b7d2012-10-26 16:29:38 +0100286 if (initrd_size) {
287 WRITE_WORD(p, info->initrd_start);
288 } else {
pbrook52b43732009-04-09 17:19:47 +0000289 WRITE_WORD(p, 0);
Peter Maydellfc53b7d2012-10-26 16:29:38 +0100290 }
balrog2b8f2d42007-07-27 22:08:46 +0000291 /* initrd_size */
pbrook52b43732009-04-09 17:19:47 +0000292 WRITE_WORD(p, initrd_size);
balrog2b8f2d42007-07-27 22:08:46 +0000293 /* rd_start */
pbrook52b43732009-04-09 17:19:47 +0000294 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000295 /* system_rev */
pbrook52b43732009-04-09 17:19:47 +0000296 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000297 /* system_serial_low */
pbrook52b43732009-04-09 17:19:47 +0000298 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000299 /* system_serial_high */
pbrook52b43732009-04-09 17:19:47 +0000300 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000301 /* mem_fclk_21285 */
pbrook52b43732009-04-09 17:19:47 +0000302 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000303 /* zero unused fields */
pbrook52b43732009-04-09 17:19:47 +0000304 while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
305 WRITE_WORD(p, 0);
306 }
307 s = info->kernel_cmdline;
308 if (s) {
Stefan Weile1fe50d2013-04-12 20:53:58 +0200309 cpu_physical_memory_write(p, s, strlen(s) + 1);
pbrook52b43732009-04-09 17:19:47 +0000310 } else {
311 WRITE_WORD(p, 0);
312 }
balrog2b8f2d42007-07-27 22:08:46 +0000313}
314
Avi Kivitya8170e52012-10-23 12:30:10 +0200315static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo)
Grant Likely412beee2012-03-02 11:56:38 +0000316{
Grant Likely412beee2012-03-02 11:56:38 +0000317 void *fdt = NULL;
Grant Likely412beee2012-03-02 11:56:38 +0000318 int size, rc;
Peter Maydell70976c42013-07-16 13:25:06 +0100319 uint32_t acells, scells;
Grant Likely412beee2012-03-02 11:56:38 +0000320
John Rigby0fb79852013-11-22 17:17:10 +0000321 if (binfo->dtb_filename) {
322 char *filename;
323 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
324 if (!filename) {
325 fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
326 goto fail;
327 }
Grant Likely412beee2012-03-02 11:56:38 +0000328
John Rigby0fb79852013-11-22 17:17:10 +0000329 fdt = load_device_tree(filename, &size);
330 if (!fdt) {
331 fprintf(stderr, "Couldn't open dtb file %s\n", filename);
332 g_free(filename);
333 goto fail;
334 }
Grant Likely412beee2012-03-02 11:56:38 +0000335 g_free(filename);
John Rigby0fb79852013-11-22 17:17:10 +0000336 } else if (binfo->get_dtb) {
337 fdt = binfo->get_dtb(binfo, &size);
338 if (!fdt) {
339 fprintf(stderr, "Board was unable to create a dtb blob\n");
340 goto fail;
341 }
Grant Likely412beee2012-03-02 11:56:38 +0000342 }
Grant Likely412beee2012-03-02 11:56:38 +0000343
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000344 acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells");
345 scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells");
Peter Maydell9bfa6592012-07-20 13:34:50 +0100346 if (acells == 0 || scells == 0) {
347 fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
Peter Maydellc23045d2013-06-25 18:34:13 +0100348 goto fail;
Peter Maydell9bfa6592012-07-20 13:34:50 +0100349 }
350
Peter Maydell70976c42013-07-16 13:25:06 +0100351 if (scells < 2 && binfo->ram_size >= (1ULL << 32)) {
352 /* This is user error so deserves a friendlier error message
353 * than the failure of setprop_sized_cells would provide
354 */
Peter Maydell9bfa6592012-07-20 13:34:50 +0100355 fprintf(stderr, "qemu: dtb file not compatible with "
356 "RAM size > 4GB\n");
Peter Maydellc23045d2013-06-25 18:34:13 +0100357 goto fail;
Peter Maydell9bfa6592012-07-20 13:34:50 +0100358 }
359
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000360 rc = qemu_fdt_setprop_sized_cells(fdt, "/memory", "reg",
361 acells, binfo->loader_start,
362 scells, binfo->ram_size);
Grant Likely412beee2012-03-02 11:56:38 +0000363 if (rc < 0) {
364 fprintf(stderr, "couldn't set /memory/reg\n");
Peter Maydellc23045d2013-06-25 18:34:13 +0100365 goto fail;
Grant Likely412beee2012-03-02 11:56:38 +0000366 }
367
Peter A. G. Crosthwaite5e879752012-06-17 15:35:36 +0000368 if (binfo->kernel_cmdline && *binfo->kernel_cmdline) {
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000369 rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
370 binfo->kernel_cmdline);
Peter A. G. Crosthwaite5e879752012-06-17 15:35:36 +0000371 if (rc < 0) {
372 fprintf(stderr, "couldn't set /chosen/bootargs\n");
Peter Maydellc23045d2013-06-25 18:34:13 +0100373 goto fail;
Peter A. G. Crosthwaite5e879752012-06-17 15:35:36 +0000374 }
Grant Likely412beee2012-03-02 11:56:38 +0000375 }
376
377 if (binfo->initrd_size) {
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000378 rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
379 binfo->initrd_start);
Grant Likely412beee2012-03-02 11:56:38 +0000380 if (rc < 0) {
381 fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
Peter Maydellc23045d2013-06-25 18:34:13 +0100382 goto fail;
Grant Likely412beee2012-03-02 11:56:38 +0000383 }
384
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000385 rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
386 binfo->initrd_start + binfo->initrd_size);
Grant Likely412beee2012-03-02 11:56:38 +0000387 if (rc < 0) {
388 fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
Peter Maydellc23045d2013-06-25 18:34:13 +0100389 goto fail;
Grant Likely412beee2012-03-02 11:56:38 +0000390 }
391 }
Peter Maydell3b1cceb2013-07-16 13:25:10 +0100392
393 if (binfo->modify_dtb) {
394 binfo->modify_dtb(binfo, fdt);
395 }
396
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000397 qemu_fdt_dumpdtb(fdt, size);
Grant Likely412beee2012-03-02 11:56:38 +0000398
399 cpu_physical_memory_write(addr, fdt, size);
400
Peter Maydellc23045d2013-06-25 18:34:13 +0100401 g_free(fdt);
402
Grant Likely412beee2012-03-02 11:56:38 +0000403 return 0;
Peter Maydellc23045d2013-06-25 18:34:13 +0100404
405fail:
406 g_free(fdt);
407 return -1;
Grant Likely412beee2012-03-02 11:56:38 +0000408}
409
Adam Lackorzynski6ed221b2011-03-05 13:51:45 +0100410static void do_cpu_reset(void *opaque)
Paul Brookf2d74972009-11-11 18:07:53 +0000411{
Andreas Färber351d5662012-05-05 12:40:39 +0200412 ARMCPU *cpu = opaque;
413 CPUARMState *env = &cpu->env;
Stefan Weil462a8bc2011-06-23 17:53:48 +0200414 const struct arm_boot_info *info = env->boot_info;
Paul Brookf2d74972009-11-11 18:07:53 +0000415
Andreas Färber351d5662012-05-05 12:40:39 +0200416 cpu_reset(CPU(cpu));
Paul Brookf2d74972009-11-11 18:07:53 +0000417 if (info) {
418 if (!info->is_linux) {
419 /* Jump to the entry point. */
420 env->regs[15] = info->entry & 0xfffffffe;
421 env->thumb = info->entry & 1;
422 } else {
Andreas Färber182735e2013-05-29 22:29:20 +0200423 if (CPU(cpu) == first_cpu) {
Mian M. Hamayun4d9ebf72013-12-17 19:42:30 +0000424 if (env->aarch64) {
425 env->pc = info->loader_start;
426 } else {
427 env->regs[15] = info->loader_start;
428 }
429
Peter Maydell83bfffe2014-01-31 14:47:32 +0000430 if (!have_dtb(info)) {
Grant Likely412beee2012-03-02 11:56:38 +0000431 if (old_param) {
432 set_kernel_args_old(info);
433 } else {
434 set_kernel_args(info);
435 }
Adam Lackorzynski6ed221b2011-03-05 13:51:45 +0100436 }
Paul Brookf2d74972009-11-11 18:07:53 +0000437 } else {
Andreas Färber5d309322012-05-14 01:05:40 +0200438 info->secondary_cpu_reset_hook(cpu, info);
Paul Brookf2d74972009-11-11 18:07:53 +0000439 }
440 }
441 }
Paul Brookf2d74972009-11-11 18:07:53 +0000442}
443
Andreas Färber3aaa8df2012-05-14 02:39:57 +0200444void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
pbrook16406952006-04-27 23:15:07 +0000445{
Andreas Färber182735e2013-05-29 22:29:20 +0200446 CPUState *cs = CPU(cpu);
pbrook16406952006-04-27 23:15:07 +0000447 int kernel_size;
448 int initrd_size;
pbrook1c7b3752007-03-06 23:52:01 +0000449 int is_linux = 0;
450 uint64_t elf_entry;
Peter Maydellda0af402014-03-21 18:44:36 +0000451 int elf_machine;
Mian M. Hamayun4d9ebf72013-12-17 19:42:30 +0000452 hwaddr entry, kernel_load_offset;
Blue Swirlca20cf32009-09-20 14:58:02 +0000453 int big_endian;
Mian M. Hamayun4d9ebf72013-12-17 19:42:30 +0000454 static const ARMInsnFixup *primary_loader;
pbrook16406952006-04-27 23:15:07 +0000455
456 /* Load the kernel. */
balrogf93eb9f2008-04-14 20:27:51 +0000457 if (!info->kernel_filename) {
Peter Maydell9546dba2013-10-25 15:44:38 +0100458 /* If no kernel specified, do nothing; we will start from address 0
459 * (typically a boot ROM image) in the same way as hardware.
460 */
461 return;
pbrook16406952006-04-27 23:15:07 +0000462 }
pbrookdaf90622007-01-16 18:54:31 +0000463
Mian M. Hamayun4d9ebf72013-12-17 19:42:30 +0000464 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
465 primary_loader = bootloader_aarch64;
466 kernel_load_offset = KERNEL64_LOAD_ADDR;
Peter Maydellda0af402014-03-21 18:44:36 +0000467 elf_machine = EM_AARCH64;
Mian M. Hamayun4d9ebf72013-12-17 19:42:30 +0000468 } else {
469 primary_loader = bootloader;
470 kernel_load_offset = KERNEL_LOAD_ADDR;
Peter Maydellda0af402014-03-21 18:44:36 +0000471 elf_machine = EM_ARM;
Mian M. Hamayun4d9ebf72013-12-17 19:42:30 +0000472 }
473
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200474 info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb");
Grant Likely412beee2012-03-02 11:56:38 +0000475
Mark Langsdorf9d5ba9b2012-01-26 11:43:48 +0000476 if (!info->secondary_cpu_reset_hook) {
477 info->secondary_cpu_reset_hook = default_reset_secondary;
478 }
479 if (!info->write_secondary_boot) {
480 info->write_secondary_boot = default_write_secondary;
481 }
482
Paul Brookf2d74972009-11-11 18:07:53 +0000483 if (info->nb_cpus == 0)
484 info->nb_cpus = 1;
balrogf93eb9f2008-04-14 20:27:51 +0000485
Blue Swirlca20cf32009-09-20 14:58:02 +0000486#ifdef TARGET_WORDS_BIGENDIAN
487 big_endian = 1;
488#else
489 big_endian = 0;
490#endif
491
Peter Maydellfc53b7d2012-10-26 16:29:38 +0100492 /* We want to put the initrd far enough into RAM that when the
493 * kernel is uncompressed it will not clobber the initrd. However
494 * on boards without much RAM we must ensure that we still leave
495 * enough room for a decent sized initrd, and on boards with large
496 * amounts of RAM we must avoid the initrd being so far up in RAM
497 * that it is outside lowmem and inaccessible to the kernel.
498 * So for boards with less than 256MB of RAM we put the initrd
499 * halfway into RAM, and for boards with 256MB of RAM or more we put
500 * the initrd at 128MB.
501 */
502 info->initrd_start = info->loader_start +
503 MIN(info->ram_size / 2, 128 * 1024 * 1024);
504
pbrook1c7b3752007-03-06 23:52:01 +0000505 /* Assume that raw images are linux kernels, and ELF images are not. */
Aurelien Jarno409dbce2010-03-14 21:20:59 +0100506 kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry,
Peter Maydellda0af402014-03-21 18:44:36 +0000507 NULL, NULL, big_endian, elf_machine, 1);
pbrook1c7b3752007-03-06 23:52:01 +0000508 entry = elf_entry;
509 if (kernel_size < 0) {
aliguori5a9154e2008-11-20 22:14:40 +0000510 kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
511 &is_linux);
pbrook1c7b3752007-03-06 23:52:01 +0000512 }
513 if (kernel_size < 0) {
Mian M. Hamayun4d9ebf72013-12-17 19:42:30 +0000514 entry = info->loader_start + kernel_load_offset;
pbrook3b760e02009-04-09 17:30:32 +0000515 kernel_size = load_image_targphys(info->kernel_filename, entry,
Mian M. Hamayun4d9ebf72013-12-17 19:42:30 +0000516 info->ram_size - kernel_load_offset);
pbrook1c7b3752007-03-06 23:52:01 +0000517 is_linux = 1;
518 }
519 if (kernel_size < 0) {
balrogf93eb9f2008-04-14 20:27:51 +0000520 fprintf(stderr, "qemu: could not load kernel '%s'\n",
521 info->kernel_filename);
pbrook1c7b3752007-03-06 23:52:01 +0000522 exit(1);
523 }
Paul Brookf2d74972009-11-11 18:07:53 +0000524 info->entry = entry;
525 if (is_linux) {
Peter Maydell47b1da82013-12-17 19:42:30 +0000526 uint32_t fixupcontext[FIXUP_MAX];
527
balrogf93eb9f2008-04-14 20:27:51 +0000528 if (info->initrd_filename) {
Soren Brinkmannfd766632013-07-08 15:40:02 -0700529 initrd_size = load_ramdisk(info->initrd_filename,
530 info->initrd_start,
531 info->ram_size -
532 info->initrd_start);
533 if (initrd_size < 0) {
534 initrd_size = load_image_targphys(info->initrd_filename,
535 info->initrd_start,
536 info->ram_size -
537 info->initrd_start);
538 }
pbrookdaf90622007-01-16 18:54:31 +0000539 if (initrd_size < 0) {
540 fprintf(stderr, "qemu: could not load initrd '%s'\n",
balrogf93eb9f2008-04-14 20:27:51 +0000541 info->initrd_filename);
pbrookdaf90622007-01-16 18:54:31 +0000542 exit(1);
543 }
544 } else {
545 initrd_size = 0;
546 }
Grant Likely412beee2012-03-02 11:56:38 +0000547 info->initrd_size = initrd_size;
548
Peter Maydell47b1da82013-12-17 19:42:30 +0000549 fixupcontext[FIXUP_BOARDID] = info->board_id;
Grant Likely412beee2012-03-02 11:56:38 +0000550
551 /* for device tree boot, we pass the DTB directly in r2. Otherwise
552 * we point to the kernel args.
553 */
Peter Maydell83bfffe2014-01-31 14:47:32 +0000554 if (have_dtb(info)) {
Peter Maydell98ed8052013-01-24 19:02:28 +0000555 /* Place the DTB after the initrd in memory. Note that some
556 * kernels will trash anything in the 4K page the initrd
557 * ends in, so make sure the DTB isn't caught up in that.
558 */
559 hwaddr dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size,
560 4096);
Grant Likely412beee2012-03-02 11:56:38 +0000561 if (load_dtb(dtb_start, info)) {
562 exit(1);
563 }
Peter Maydell47b1da82013-12-17 19:42:30 +0000564 fixupcontext[FIXUP_ARGPTR] = dtb_start;
Grant Likely412beee2012-03-02 11:56:38 +0000565 } else {
Peter Maydell47b1da82013-12-17 19:42:30 +0000566 fixupcontext[FIXUP_ARGPTR] = info->loader_start + KERNEL_ARGS_ADDR;
Peter Maydell38714812012-07-20 13:34:50 +0100567 if (info->ram_size >= (1ULL << 32)) {
568 fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
569 " Linux kernel using ATAGS (try passing a device tree"
570 " using -dtb)\n");
571 exit(1);
572 }
Grant Likely412beee2012-03-02 11:56:38 +0000573 }
Peter Maydell47b1da82013-12-17 19:42:30 +0000574 fixupcontext[FIXUP_ENTRYPOINT] = entry;
575
576 write_bootloader("bootloader", info->loader_start,
Mian M. Hamayun4d9ebf72013-12-17 19:42:30 +0000577 primary_loader, fixupcontext);
Peter Maydell47b1da82013-12-17 19:42:30 +0000578
pbrook52b43732009-04-09 17:19:47 +0000579 if (info->nb_cpus > 1) {
Andreas Färber9543b0c2012-05-14 00:08:10 +0200580 info->write_secondary_boot(cpu, info);
pbrook52b43732009-04-09 17:19:47 +0000581 }
pbrook16406952006-04-27 23:15:07 +0000582 }
Paul Brookf2d74972009-11-11 18:07:53 +0000583 info->is_linux = is_linux;
Adam Lackorzynski6ed221b2011-03-05 13:51:45 +0100584
Andreas Färberbdc44642013-06-24 23:50:24 +0200585 for (; cs; cs = CPU_NEXT(cs)) {
Andreas Färber182735e2013-05-29 22:29:20 +0200586 cpu = ARM_CPU(cs);
587 cpu->env.boot_info = info;
Andreas Färber351d5662012-05-05 12:40:39 +0200588 qemu_register_reset(do_cpu_reset, cpu);
Adam Lackorzynski6ed221b2011-03-05 13:51:45 +0100589 }
pbrook16406952006-04-27 23:15:07 +0000590}