4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
16 #define KERNEL_ARGS_ADDR 0x100
17 #define KERNEL_LOAD_ADDR 0x00010000
18 #define INITRD_LOAD_ADDR 0x00d00000
20 /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
21 static uint32_t bootloader
[] = {
22 0xe3a00000, /* mov r0, #0 */
23 0xe3a01000, /* mov r1, #0x?? */
24 0xe3811c00, /* orr r1, r1, #0x??00 */
25 0xe59f2000, /* ldr r2, [pc, #0] */
26 0xe59ff000, /* ldr pc, [pc, #0] */
27 0, /* Address of kernel args. Set by integratorcp_init. */
28 0 /* Kernel entry point. Set by integratorcp_init. */
31 /* Entry point for secondary CPUs. Enable interrupt controller and
32 Issue WFI until start address is written to system controller. */
33 static uint32_t smpboot
[] = {
34 0xe59f201c, /* ldr r2, privbase */
35 0xe59f001c, /* ldr r0, startaddr */
36 0xe3a01001, /* mov r1, #1 */
37 0xe5821100, /* str r1, [r2, #256] */
39 0xe5901000, /* ldr r1, [r0] */
40 0xe1110001, /* tst r1, r1 */
41 0x0afffffb, /* beq <wfi> */
42 0xe12fff11, /* bx r1 */
43 0, /* privbase: Private memory region base address. */
44 0 /* bootreg: Boot register address is held here */
47 #define WRITE_WORD(p, value) do { \
48 stl_phys_notdirty(p, value); \
52 static void set_kernel_args(const struct arm_boot_info
*info
,
53 int initrd_size
, target_phys_addr_t base
)
57 p
= base
+ KERNEL_ARGS_ADDR
;
60 WRITE_WORD(p
, 0x54410001);
62 WRITE_WORD(p
, 0x1000);
65 /* TODO: handle multiple chips on one ATAG list */
67 WRITE_WORD(p
, 0x54410002);
68 WRITE_WORD(p
, info
->ram_size
);
69 WRITE_WORD(p
, info
->loader_start
);
73 WRITE_WORD(p
, 0x54420005);
74 WRITE_WORD(p
, info
->loader_start
+ INITRD_LOAD_ADDR
);
75 WRITE_WORD(p
, initrd_size
);
77 if (info
->kernel_cmdline
&& *info
->kernel_cmdline
) {
81 cmdline_size
= strlen(info
->kernel_cmdline
);
82 cpu_physical_memory_write(p
+ 8, (void *)info
->kernel_cmdline
,
84 cmdline_size
= (cmdline_size
>> 2) + 1;
85 WRITE_WORD(p
, cmdline_size
+ 2);
86 WRITE_WORD(p
, 0x54410009);
87 p
+= cmdline_size
* 4;
89 if (info
->atag_board
) {
92 uint8_t atag_board_buf
[0x1000];
94 atag_board_len
= (info
->atag_board(info
, atag_board_buf
) + 3) & ~3;
95 WRITE_WORD(p
, (atag_board_len
+ 8) >> 2);
96 WRITE_WORD(p
, 0x414f4d50);
97 cpu_physical_memory_write(p
, atag_board_buf
, atag_board_len
);
105 static void set_kernel_args_old(const struct arm_boot_info
*info
,
106 int initrd_size
, target_phys_addr_t base
)
108 target_phys_addr_t p
;
112 /* see linux/include/asm-arm/setup.h */
113 p
= base
+ KERNEL_ARGS_ADDR
;
117 WRITE_WORD(p
, info
->ram_size
/ 4096);
120 #define FLAG_READONLY 1
121 #define FLAG_RDLOAD 4
122 #define FLAG_RDPROMPT 8
124 WRITE_WORD(p
, FLAG_READONLY
| FLAG_RDLOAD
| FLAG_RDPROMPT
);
126 WRITE_WORD(p
, (31 << 8) | 0); /* /dev/mtdblock0 */
135 /* memc_control_reg */
137 /* unsigned char sounddefault */
138 /* unsigned char adfsdrives */
139 /* unsigned char bytes_per_char_h */
140 /* unsigned char bytes_per_char_v */
142 /* pages_in_bank[4] */
151 WRITE_WORD(p
, info
->loader_start
+ INITRD_LOAD_ADDR
);
155 WRITE_WORD(p
, initrd_size
);
160 /* system_serial_low */
162 /* system_serial_high */
166 /* zero unused fields */
167 while (p
< base
+ KERNEL_ARGS_ADDR
+ 256 + 1024) {
170 s
= info
->kernel_cmdline
;
172 cpu_physical_memory_write(p
, (void *)s
, strlen(s
) + 1);
178 static void do_cpu_reset(void *opaque
)
180 CPUState
*env
= opaque
;
181 const struct arm_boot_info
*info
= env
->boot_info
;
185 if (!info
->is_linux
) {
186 /* Jump to the entry point. */
187 env
->regs
[15] = info
->entry
& 0xfffffffe;
188 env
->thumb
= info
->entry
& 1;
190 if (env
== first_cpu
) {
191 env
->regs
[15] = info
->loader_start
;
193 set_kernel_args_old(info
, info
->initrd_size
,
196 set_kernel_args(info
, info
->initrd_size
,
200 stl_phys_notdirty(info
->smp_bootreg_addr
, 0);
201 env
->regs
[15] = info
->smp_loader_start
;
207 void arm_load_kernel(CPUState
*env
, struct arm_boot_info
*info
)
214 target_phys_addr_t entry
;
217 /* Load the kernel. */
218 if (!info
->kernel_filename
) {
219 fprintf(stderr
, "Kernel image must be specified\n");
223 if (info
->nb_cpus
== 0)
226 #ifdef TARGET_WORDS_BIGENDIAN
232 /* Assume that raw images are linux kernels, and ELF images are not. */
233 kernel_size
= load_elf(info
->kernel_filename
, NULL
, NULL
, &elf_entry
,
234 NULL
, NULL
, big_endian
, ELF_MACHINE
, 1);
236 if (kernel_size
< 0) {
237 kernel_size
= load_uimage(info
->kernel_filename
, &entry
, NULL
,
240 if (kernel_size
< 0) {
241 entry
= info
->loader_start
+ KERNEL_LOAD_ADDR
;
242 kernel_size
= load_image_targphys(info
->kernel_filename
, entry
,
243 ram_size
- KERNEL_LOAD_ADDR
);
246 if (kernel_size
< 0) {
247 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
248 info
->kernel_filename
);
253 if (info
->initrd_filename
) {
254 initrd_size
= load_image_targphys(info
->initrd_filename
,
257 ram_size
- INITRD_LOAD_ADDR
);
258 if (initrd_size
< 0) {
259 fprintf(stderr
, "qemu: could not load initrd '%s'\n",
260 info
->initrd_filename
);
266 bootloader
[1] |= info
->board_id
& 0xff;
267 bootloader
[2] |= (info
->board_id
>> 8) & 0xff;
268 bootloader
[5] = info
->loader_start
+ KERNEL_ARGS_ADDR
;
269 bootloader
[6] = entry
;
270 for (n
= 0; n
< sizeof(bootloader
) / 4; n
++) {
271 bootloader
[n
] = tswap32(bootloader
[n
]);
273 rom_add_blob_fixed("bootloader", bootloader
, sizeof(bootloader
),
275 if (info
->nb_cpus
> 1) {
276 smpboot
[ARRAY_SIZE(smpboot
) - 1] = info
->smp_bootreg_addr
;
277 smpboot
[ARRAY_SIZE(smpboot
) - 2] = info
->smp_priv_base
;
278 for (n
= 0; n
< ARRAY_SIZE(smpboot
); n
++) {
279 smpboot
[n
] = tswap32(smpboot
[n
]);
281 rom_add_blob_fixed("smpboot", smpboot
, sizeof(smpboot
),
282 info
->smp_loader_start
);
284 info
->initrd_size
= initrd_size
;
286 info
->is_linux
= is_linux
;
288 for (; env
; env
= env
->next_cpu
) {
289 env
->boot_info
= info
;
290 qemu_register_reset(do_cpu_reset
, env
);