]> git.proxmox.com Git - qemu.git/blob - hw/arm_boot.c
Merge remote-tracking branch 'bonzini/nbd-for-anthony' into staging
[qemu.git] / hw / arm_boot.c
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 "hw.h"
11 #include "arm-misc.h"
12 #include "sysemu.h"
13 #include "loader.h"
14 #include "elf.h"
15
16 #define KERNEL_ARGS_ADDR 0x100
17 #define KERNEL_LOAD_ADDR 0x00010000
18 #define INITRD_LOAD_ADDR 0x00d00000
19
20 /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
21 static uint32_t bootloader[] = {
22 0xe3a00000, /* mov r0, #0 */
23 0xe59f1004, /* ldr r1, [pc, #4] */
24 0xe59f2004, /* ldr r2, [pc, #4] */
25 0xe59ff004, /* ldr pc, [pc, #4] */
26 0, /* Board ID */
27 0, /* Address of kernel args. Set by integratorcp_init. */
28 0 /* Kernel entry point. Set by integratorcp_init. */
29 };
30
31 /* Handling for secondary CPU boot in a multicore system.
32 * Unlike the uniprocessor/primary CPU boot, this is platform
33 * dependent. The default code here is based on the secondary
34 * CPU boot protocol used on realview/vexpress boards, with
35 * some parameterisation to increase its flexibility.
36 * QEMU platform models for which this code is not appropriate
37 * should override write_secondary_boot and secondary_cpu_reset_hook
38 * instead.
39 *
40 * This code enables the interrupt controllers for the secondary
41 * CPUs and then puts all the secondary CPUs into a loop waiting
42 * for an interprocessor interrupt and polling a configurable
43 * location for the kernel secondary CPU entry point.
44 */
45 static uint32_t smpboot[] = {
46 0xe59f201c, /* ldr r2, gic_cpu_if */
47 0xe59f001c, /* ldr r0, startaddr */
48 0xe3a01001, /* mov r1, #1 */
49 0xe5821000, /* str r1, [r2] */
50 0xe320f003, /* wfi */
51 0xe5901000, /* ldr r1, [r0] */
52 0xe1110001, /* tst r1, r1 */
53 0x0afffffb, /* beq <wfi> */
54 0xe12fff11, /* bx r1 */
55 0, /* gic_cpu_if: base address of GIC CPU interface */
56 0 /* bootreg: Boot register address is held here */
57 };
58
59 static void default_write_secondary(CPUState *env,
60 const struct arm_boot_info *info)
61 {
62 int n;
63 smpboot[ARRAY_SIZE(smpboot) - 1] = info->smp_bootreg_addr;
64 smpboot[ARRAY_SIZE(smpboot) - 2] = info->gic_cpu_if_addr;
65 for (n = 0; n < ARRAY_SIZE(smpboot); n++) {
66 smpboot[n] = tswap32(smpboot[n]);
67 }
68 rom_add_blob_fixed("smpboot", smpboot, sizeof(smpboot),
69 info->smp_loader_start);
70 }
71
72 static void default_reset_secondary(CPUState *env,
73 const struct arm_boot_info *info)
74 {
75 stl_phys_notdirty(info->smp_bootreg_addr, 0);
76 env->regs[15] = info->smp_loader_start;
77 }
78
79 #define WRITE_WORD(p, value) do { \
80 stl_phys_notdirty(p, value); \
81 p += 4; \
82 } while (0)
83
84 static void set_kernel_args(const struct arm_boot_info *info)
85 {
86 int initrd_size = info->initrd_size;
87 target_phys_addr_t base = info->loader_start;
88 target_phys_addr_t p;
89
90 p = base + KERNEL_ARGS_ADDR;
91 /* ATAG_CORE */
92 WRITE_WORD(p, 5);
93 WRITE_WORD(p, 0x54410001);
94 WRITE_WORD(p, 1);
95 WRITE_WORD(p, 0x1000);
96 WRITE_WORD(p, 0);
97 /* ATAG_MEM */
98 /* TODO: handle multiple chips on one ATAG list */
99 WRITE_WORD(p, 4);
100 WRITE_WORD(p, 0x54410002);
101 WRITE_WORD(p, info->ram_size);
102 WRITE_WORD(p, info->loader_start);
103 if (initrd_size) {
104 /* ATAG_INITRD2 */
105 WRITE_WORD(p, 4);
106 WRITE_WORD(p, 0x54420005);
107 WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR);
108 WRITE_WORD(p, initrd_size);
109 }
110 if (info->kernel_cmdline && *info->kernel_cmdline) {
111 /* ATAG_CMDLINE */
112 int cmdline_size;
113
114 cmdline_size = strlen(info->kernel_cmdline);
115 cpu_physical_memory_write(p + 8, (void *)info->kernel_cmdline,
116 cmdline_size + 1);
117 cmdline_size = (cmdline_size >> 2) + 1;
118 WRITE_WORD(p, cmdline_size + 2);
119 WRITE_WORD(p, 0x54410009);
120 p += cmdline_size * 4;
121 }
122 if (info->atag_board) {
123 /* ATAG_BOARD */
124 int atag_board_len;
125 uint8_t atag_board_buf[0x1000];
126
127 atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
128 WRITE_WORD(p, (atag_board_len + 8) >> 2);
129 WRITE_WORD(p, 0x414f4d50);
130 cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
131 p += atag_board_len;
132 }
133 /* ATAG_END */
134 WRITE_WORD(p, 0);
135 WRITE_WORD(p, 0);
136 }
137
138 static void set_kernel_args_old(const struct arm_boot_info *info)
139 {
140 target_phys_addr_t p;
141 const char *s;
142 int initrd_size = info->initrd_size;
143 target_phys_addr_t base = info->loader_start;
144
145 /* see linux/include/asm-arm/setup.h */
146 p = base + KERNEL_ARGS_ADDR;
147 /* page_size */
148 WRITE_WORD(p, 4096);
149 /* nr_pages */
150 WRITE_WORD(p, info->ram_size / 4096);
151 /* ramdisk_size */
152 WRITE_WORD(p, 0);
153 #define FLAG_READONLY 1
154 #define FLAG_RDLOAD 4
155 #define FLAG_RDPROMPT 8
156 /* flags */
157 WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
158 /* rootdev */
159 WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */
160 /* video_num_cols */
161 WRITE_WORD(p, 0);
162 /* video_num_rows */
163 WRITE_WORD(p, 0);
164 /* video_x */
165 WRITE_WORD(p, 0);
166 /* video_y */
167 WRITE_WORD(p, 0);
168 /* memc_control_reg */
169 WRITE_WORD(p, 0);
170 /* unsigned char sounddefault */
171 /* unsigned char adfsdrives */
172 /* unsigned char bytes_per_char_h */
173 /* unsigned char bytes_per_char_v */
174 WRITE_WORD(p, 0);
175 /* pages_in_bank[4] */
176 WRITE_WORD(p, 0);
177 WRITE_WORD(p, 0);
178 WRITE_WORD(p, 0);
179 WRITE_WORD(p, 0);
180 /* pages_in_vram */
181 WRITE_WORD(p, 0);
182 /* initrd_start */
183 if (initrd_size)
184 WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR);
185 else
186 WRITE_WORD(p, 0);
187 /* initrd_size */
188 WRITE_WORD(p, initrd_size);
189 /* rd_start */
190 WRITE_WORD(p, 0);
191 /* system_rev */
192 WRITE_WORD(p, 0);
193 /* system_serial_low */
194 WRITE_WORD(p, 0);
195 /* system_serial_high */
196 WRITE_WORD(p, 0);
197 /* mem_fclk_21285 */
198 WRITE_WORD(p, 0);
199 /* zero unused fields */
200 while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
201 WRITE_WORD(p, 0);
202 }
203 s = info->kernel_cmdline;
204 if (s) {
205 cpu_physical_memory_write(p, (void *)s, strlen(s) + 1);
206 } else {
207 WRITE_WORD(p, 0);
208 }
209 }
210
211 static void do_cpu_reset(void *opaque)
212 {
213 CPUState *env = opaque;
214 const struct arm_boot_info *info = env->boot_info;
215
216 cpu_reset(env);
217 if (info) {
218 if (!info->is_linux) {
219 /* Jump to the entry point. */
220 env->regs[15] = info->entry & 0xfffffffe;
221 env->thumb = info->entry & 1;
222 } else {
223 if (env == first_cpu) {
224 env->regs[15] = info->loader_start;
225 if (old_param) {
226 set_kernel_args_old(info);
227 } else {
228 set_kernel_args(info);
229 }
230 } else {
231 info->secondary_cpu_reset_hook(env, info);
232 }
233 }
234 }
235 }
236
237 void arm_load_kernel(CPUState *env, struct arm_boot_info *info)
238 {
239 int kernel_size;
240 int initrd_size;
241 int n;
242 int is_linux = 0;
243 uint64_t elf_entry;
244 target_phys_addr_t entry;
245 int big_endian;
246
247 /* Load the kernel. */
248 if (!info->kernel_filename) {
249 fprintf(stderr, "Kernel image must be specified\n");
250 exit(1);
251 }
252
253 if (!info->secondary_cpu_reset_hook) {
254 info->secondary_cpu_reset_hook = default_reset_secondary;
255 }
256 if (!info->write_secondary_boot) {
257 info->write_secondary_boot = default_write_secondary;
258 }
259
260 if (info->nb_cpus == 0)
261 info->nb_cpus = 1;
262
263 #ifdef TARGET_WORDS_BIGENDIAN
264 big_endian = 1;
265 #else
266 big_endian = 0;
267 #endif
268
269 /* Assume that raw images are linux kernels, and ELF images are not. */
270 kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry,
271 NULL, NULL, big_endian, ELF_MACHINE, 1);
272 entry = elf_entry;
273 if (kernel_size < 0) {
274 kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
275 &is_linux);
276 }
277 if (kernel_size < 0) {
278 entry = info->loader_start + KERNEL_LOAD_ADDR;
279 kernel_size = load_image_targphys(info->kernel_filename, entry,
280 ram_size - KERNEL_LOAD_ADDR);
281 is_linux = 1;
282 }
283 if (kernel_size < 0) {
284 fprintf(stderr, "qemu: could not load kernel '%s'\n",
285 info->kernel_filename);
286 exit(1);
287 }
288 info->entry = entry;
289 if (is_linux) {
290 if (info->initrd_filename) {
291 initrd_size = load_image_targphys(info->initrd_filename,
292 info->loader_start
293 + INITRD_LOAD_ADDR,
294 ram_size - INITRD_LOAD_ADDR);
295 if (initrd_size < 0) {
296 fprintf(stderr, "qemu: could not load initrd '%s'\n",
297 info->initrd_filename);
298 exit(1);
299 }
300 } else {
301 initrd_size = 0;
302 }
303 bootloader[4] = info->board_id;
304 bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
305 bootloader[6] = entry;
306 for (n = 0; n < sizeof(bootloader) / 4; n++) {
307 bootloader[n] = tswap32(bootloader[n]);
308 }
309 rom_add_blob_fixed("bootloader", bootloader, sizeof(bootloader),
310 info->loader_start);
311 if (info->nb_cpus > 1) {
312 info->write_secondary_boot(env, info);
313 }
314 info->initrd_size = initrd_size;
315 }
316 info->is_linux = is_linux;
317
318 for (; env; env = env->next_cpu) {
319 env->boot_info = info;
320 qemu_register_reset(do_cpu_reset, env);
321 }
322 }