4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
14 #define KERNEL_ARGS_ADDR 0x100
15 #define KERNEL_LOAD_ADDR 0x00010000
16 #define INITRD_LOAD_ADDR 0x00800000
18 /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
19 static uint32_t bootloader
[] = {
20 0xe3a00000, /* mov r0, #0 */
21 0xe3a01000, /* mov r1, #0x?? */
22 0xe3811c00, /* orr r1, r1, #0x??00 */
23 0xe59f2000, /* ldr r2, [pc, #0] */
24 0xe59ff000, /* ldr pc, [pc, #0] */
25 0, /* Address of kernel args. Set by integratorcp_init. */
26 0 /* Kernel entry point. Set by integratorcp_init. */
29 /* Entry point for secondary CPUs. Enable interrupt controller and
30 Issue WFI until start address is written to system controller. */
31 static uint32_t smpboot
[] = {
32 0xe3a00201, /* mov r0, #0x10000000 */
33 0xe3800601, /* orr r0, r0, #0x001000000 */
34 0xe3a01001, /* mov r1, #1 */
35 0xe5801100, /* str r1, [r0, #0x100] */
36 0xe3a00201, /* mov r0, #0x10000000 */
37 0xe3800030, /* orr r0, #0x30 */
39 0xe5901000, /* ldr r1, [r0] */
40 0xe3110003, /* tst r1, #3 */
41 0x1afffffb, /* bne <wfi> */
42 0xe12fff11 /* bx r1 */
45 static void main_cpu_reset(void *opaque
)
47 CPUState
*env
= opaque
;
51 arm_load_kernel(env
, env
->boot_info
);
53 /* TODO: Reset secondary CPUs. */
56 static void set_kernel_args(struct arm_boot_info
*info
,
57 int initrd_size
, void *base
)
61 p
= (uint32_t *)(base
+ KERNEL_ARGS_ADDR
);
64 stl_raw(p
++, 0x54410001);
69 /* TODO: handle multiple chips on one ATAG list */
71 stl_raw(p
++, 0x54410002);
72 stl_raw(p
++, info
->ram_size
);
73 stl_raw(p
++, info
->loader_start
);
77 stl_raw(p
++, 0x54420005);
78 stl_raw(p
++, info
->loader_start
+ INITRD_LOAD_ADDR
);
79 stl_raw(p
++, initrd_size
);
81 if (info
->kernel_cmdline
&& *info
->kernel_cmdline
) {
85 cmdline_size
= strlen(info
->kernel_cmdline
);
86 memcpy(p
+ 2, info
->kernel_cmdline
, cmdline_size
+ 1);
87 cmdline_size
= (cmdline_size
>> 2) + 1;
88 stl_raw(p
++, cmdline_size
+ 2);
89 stl_raw(p
++, 0x54410009);
92 if (info
->atag_board
) {
96 atag_board_len
= (info
->atag_board(info
, p
+ 2) + 3) >> 2;
97 stl_raw(p
++, 2 + atag_board_len
);
98 stl_raw(p
++, 0x414f4d50);
106 static void set_kernel_args_old(struct arm_boot_info
*info
,
107 int initrd_size
, void *base
)
112 /* see linux/include/asm-arm/setup.h */
113 p
= (uint32_t *)(base
+ KERNEL_ARGS_ADDR
);
117 stl_raw(p
++, info
->ram_size
/ 4096);
120 #define FLAG_READONLY 1
121 #define FLAG_RDLOAD 4
122 #define FLAG_RDPROMPT 8
124 stl_raw(p
++, FLAG_READONLY
| FLAG_RDLOAD
| FLAG_RDPROMPT
);
126 stl_raw(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 stl_raw(p
++, info
->loader_start
+ INITRD_LOAD_ADDR
);
155 stl_raw(p
++, initrd_size
);
160 /* system_serial_low */
162 /* system_serial_high */
166 /* zero unused fields */
167 memset(p
, 0, 256 + 1024 -
168 (p
- ((uint32_t *)(base
+ KERNEL_ARGS_ADDR
))));
169 s
= base
+ KERNEL_ARGS_ADDR
+ 256 + 1024;
170 if (info
->kernel_cmdline
)
171 strcpy (s
, info
->kernel_cmdline
);
176 void arm_load_kernel(CPUState
*env
, struct arm_boot_info
*info
)
187 /* Load the kernel. */
188 if (!info
->kernel_filename
) {
189 fprintf(stderr
, "Kernel image must be specified\n");
193 if (!env
->boot_info
) {
194 if (info
->nb_cpus
== 0)
196 env
->boot_info
= info
;
197 qemu_register_reset(main_cpu_reset
, env
);
200 pd
= cpu_get_physical_page_desc(info
->loader_start
);
201 loader_phys
= phys_ram_base
+ (pd
& TARGET_PAGE_MASK
) +
202 (info
->loader_start
& ~TARGET_PAGE_MASK
);
204 /* Assume that raw images are linux kernels, and ELF images are not. */
205 kernel_size
= load_elf(info
->kernel_filename
, 0, &elf_entry
, NULL
, NULL
);
207 if (kernel_size
< 0) {
208 kernel_size
= load_uboot(info
->kernel_filename
, &entry
, &is_linux
);
210 if (kernel_size
< 0) {
211 kernel_size
= load_image(info
->kernel_filename
,
212 loader_phys
+ KERNEL_LOAD_ADDR
);
213 entry
= info
->loader_start
+ KERNEL_LOAD_ADDR
;
216 if (kernel_size
< 0) {
217 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
218 info
->kernel_filename
);
222 /* Jump to the entry point. */
223 env
->regs
[15] = entry
& 0xfffffffe;
224 env
->thumb
= entry
& 1;
226 if (info
->initrd_filename
) {
227 initrd_size
= load_image(info
->initrd_filename
,
228 loader_phys
+ INITRD_LOAD_ADDR
);
229 if (initrd_size
< 0) {
230 fprintf(stderr
, "qemu: could not load initrd '%s'\n",
231 info
->initrd_filename
);
237 bootloader
[1] |= info
->board_id
& 0xff;
238 bootloader
[2] |= (info
->board_id
>> 8) & 0xff;
239 bootloader
[5] = info
->loader_start
+ KERNEL_ARGS_ADDR
;
240 bootloader
[6] = entry
;
241 for (n
= 0; n
< sizeof(bootloader
) / 4; n
++)
242 stl_raw(loader_phys
+ (n
* 4), bootloader
[n
]);
243 if (info
->nb_cpus
> 1)
244 for (n
= 0; n
< sizeof(smpboot
) / 4; n
++)
245 stl_raw(loader_phys
+ info
->ram_size
+ (n
* 4), smpboot
[n
]);
247 set_kernel_args_old(info
, initrd_size
, loader_phys
);
249 set_kernel_args(info
, initrd_size
, loader_phys
);