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;
50 if (env->kernel_filename)
51 arm_load_kernel(env, env->ram_size, env->kernel_filename,
52 env->kernel_cmdline, env->initrd_filename,
53 env->board_id, env->loader_start);
55 /* TODO: Reset secondary CPUs. */
58 static void set_kernel_args(uint32_t ram_size, int initrd_size,
59 const char *kernel_cmdline,
60 target_phys_addr_t loader_start)
64 p = (uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR);
67 stl_raw(p++, 0x54410001);
73 stl_raw(p++, 0x54410002);
74 stl_raw(p++, ram_size);
75 stl_raw(p++, loader_start);
79 stl_raw(p++, 0x54420005);
80 stl_raw(p++, loader_start + INITRD_LOAD_ADDR);
81 stl_raw(p++, initrd_size);
83 if (kernel_cmdline && *kernel_cmdline) {
87 cmdline_size = strlen(kernel_cmdline);
88 memcpy (p + 2, kernel_cmdline, cmdline_size + 1);
89 cmdline_size = (cmdline_size >> 2) + 1;
90 stl_raw(p++, cmdline_size + 2);
91 stl_raw(p++, 0x54410009);
99 static void set_kernel_args_old(uint32_t ram_size, int initrd_size,
100 const char *kernel_cmdline,
101 target_phys_addr_t loader_start)
106 /* see linux/include/asm-arm/setup.h */
107 p = (uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR);
111 stl_raw(p++, ram_size / 4096);
114 #define FLAG_READONLY 1
115 #define FLAG_RDLOAD 4
116 #define FLAG_RDPROMPT 8
118 stl_raw(p++, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
120 stl_raw(p++, (31 << 8) | 0); /* /dev/mtdblock0 */
129 /* memc_control_reg */
131 /* unsigned char sounddefault */
132 /* unsigned char adfsdrives */
133 /* unsigned char bytes_per_char_h */
134 /* unsigned char bytes_per_char_v */
136 /* pages_in_bank[4] */
145 stl_raw(p++, loader_start + INITRD_LOAD_ADDR);
149 stl_raw(p++, initrd_size);
154 /* system_serial_low */
156 /* system_serial_high */
160 /* zero unused fields */
161 memset(p, 0, 256 + 1024 -
162 (p - ((uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR))));
163 s = phys_ram_base + KERNEL_ARGS_ADDR + 256 + 1024;
165 strcpy (s, kernel_cmdline);
170 void arm_load_kernel(CPUState *env, int ram_size, const char *kernel_filename,
171 const char *kernel_cmdline, const char *initrd_filename,
172 int board_id, target_phys_addr_t loader_start)
181 /* Load the kernel. */
182 if (!kernel_filename) {
183 fprintf(stderr, "Kernel image must be specified\n");
187 if (!env->kernel_filename) {
188 env->ram_size = ram_size;
189 env->kernel_filename = kernel_filename;
190 env->kernel_cmdline = kernel_cmdline;
191 env->initrd_filename = initrd_filename;
192 env->board_id = board_id;
193 env->loader_start = loader_start;
194 qemu_register_reset(main_cpu_reset, env);
196 /* Assume that raw images are linux kernels, and ELF images are not. */
197 kernel_size = load_elf(kernel_filename, 0, &elf_entry, NULL, NULL);
199 if (kernel_size < 0) {
200 kernel_size = load_uboot(kernel_filename, &entry, &is_linux);
202 if (kernel_size < 0) {
203 kernel_size = load_image(kernel_filename,
204 phys_ram_base + KERNEL_LOAD_ADDR);
205 entry = loader_start + KERNEL_LOAD_ADDR;
208 if (kernel_size < 0) {
209 fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
213 /* Jump to the entry point. */
214 env->regs[15] = entry & 0xfffffffe;
215 env->thumb = entry & 1;
217 if (initrd_filename) {
218 initrd_size = load_image(initrd_filename,
219 phys_ram_base + INITRD_LOAD_ADDR);
220 if (initrd_size < 0) {
221 fprintf(stderr, "qemu: could not load initrd '%s'\n",
228 bootloader[1] |= board_id & 0xff;
229 bootloader[2] |= (board_id >> 8) & 0xff;
230 bootloader[5] = loader_start + KERNEL_ARGS_ADDR;
231 bootloader[6] = entry;
232 for (n = 0; n < sizeof(bootloader) / 4; n++)
233 stl_raw(phys_ram_base + (n * 4), bootloader[n]);
234 for (n = 0; n < sizeof(smpboot) / 4; n++)
235 stl_raw(phys_ram_base + ram_size + (n * 4), smpboot[n]);
237 set_kernel_args_old(ram_size, initrd_size,
238 kernel_cmdline, loader_start);
240 set_kernel_args(ram_size, initrd_size,
241 kernel_cmdline, loader_start);