root / hw / arm_boot.c @ 0d31cb99
History | View | Annotate | Download (7.2 kB)
1 | 5fafdf24 | ths | /*
|
---|---|---|---|
2 | 16406950 | pbrook | * ARM kernel loader.
|
3 | 16406950 | pbrook | *
|
4 | 9ee6e8bb | pbrook | * Copyright (c) 2006-2007 CodeSourcery.
|
5 | 16406950 | pbrook | * Written by Paul Brook
|
6 | 16406950 | pbrook | *
|
7 | 16406950 | pbrook | * This code is licenced under the GPL.
|
8 | 16406950 | pbrook | */
|
9 | 16406950 | pbrook | |
10 | 87ecb68b | pbrook | #include "hw.h" |
11 | 87ecb68b | pbrook | #include "arm-misc.h" |
12 | 87ecb68b | pbrook | #include "sysemu.h" |
13 | 16406950 | pbrook | |
14 | 16406950 | pbrook | #define KERNEL_ARGS_ADDR 0x100 |
15 | 16406950 | pbrook | #define KERNEL_LOAD_ADDR 0x00010000 |
16 | 16406950 | pbrook | #define INITRD_LOAD_ADDR 0x00800000 |
17 | 16406950 | pbrook | |
18 | 16406950 | pbrook | /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
|
19 | 16406950 | pbrook | static uint32_t bootloader[] = {
|
20 | 16406950 | pbrook | 0xe3a00000, /* mov r0, #0 */ |
21 | 16406950 | pbrook | 0xe3a01000, /* mov r1, #0x?? */ |
22 | 16406950 | pbrook | 0xe3811c00, /* orr r1, r1, #0x??00 */ |
23 | 16406950 | pbrook | 0xe59f2000, /* ldr r2, [pc, #0] */ |
24 | 16406950 | pbrook | 0xe59ff000, /* ldr pc, [pc, #0] */ |
25 | 16406950 | pbrook | 0, /* Address of kernel args. Set by integratorcp_init. */ |
26 | 16406950 | pbrook | 0 /* Kernel entry point. Set by integratorcp_init. */ |
27 | 16406950 | pbrook | }; |
28 | 16406950 | pbrook | |
29 | 9ee6e8bb | pbrook | /* Entry point for secondary CPUs. Enable interrupt controller and
|
30 | 9ee6e8bb | pbrook | Issue WFI until start address is written to system controller. */
|
31 | 9ee6e8bb | pbrook | static uint32_t smpboot[] = {
|
32 | 9ee6e8bb | pbrook | 0xe3a00201, /* mov r0, #0x10000000 */ |
33 | 9ee6e8bb | pbrook | 0xe3800601, /* orr r0, r0, #0x001000000 */ |
34 | 9ee6e8bb | pbrook | 0xe3a01001, /* mov r1, #1 */ |
35 | 9ee6e8bb | pbrook | 0xe5801100, /* str r1, [r0, #0x100] */ |
36 | 9ee6e8bb | pbrook | 0xe3a00201, /* mov r0, #0x10000000 */ |
37 | 9ee6e8bb | pbrook | 0xe3800030, /* orr r0, #0x30 */ |
38 | 9ee6e8bb | pbrook | 0xe320f003, /* wfi */ |
39 | 9ee6e8bb | pbrook | 0xe5901000, /* ldr r1, [r0] */ |
40 | 9ee6e8bb | pbrook | 0xe3110003, /* tst r1, #3 */ |
41 | 9ee6e8bb | pbrook | 0x1afffffb, /* bne <wfi> */ |
42 | 9ee6e8bb | pbrook | 0xe12fff11 /* bx r1 */ |
43 | 9ee6e8bb | pbrook | }; |
44 | 9ee6e8bb | pbrook | |
45 | f3d6b95e | pbrook | static void main_cpu_reset(void *opaque) |
46 | f3d6b95e | pbrook | { |
47 | f3d6b95e | pbrook | CPUState *env = opaque; |
48 | f3d6b95e | pbrook | |
49 | f3d6b95e | pbrook | cpu_reset(env); |
50 | f93eb9ff | balrog | if (env->boot_info)
|
51 | f93eb9ff | balrog | arm_load_kernel(env, env->boot_info); |
52 | 9ee6e8bb | pbrook | |
53 | 9ee6e8bb | pbrook | /* TODO: Reset secondary CPUs. */
|
54 | f3d6b95e | pbrook | } |
55 | f3d6b95e | pbrook | |
56 | f93eb9ff | balrog | static void set_kernel_args(struct arm_boot_info *info, |
57 | f93eb9ff | balrog | int initrd_size, void *base) |
58 | 16406950 | pbrook | { |
59 | 16406950 | pbrook | uint32_t *p; |
60 | 16406950 | pbrook | |
61 | f93eb9ff | balrog | p = (uint32_t *)(base + KERNEL_ARGS_ADDR); |
62 | 16406950 | pbrook | /* ATAG_CORE */
|
63 | 16406950 | pbrook | stl_raw(p++, 5);
|
64 | 16406950 | pbrook | stl_raw(p++, 0x54410001);
|
65 | 16406950 | pbrook | stl_raw(p++, 1);
|
66 | 16406950 | pbrook | stl_raw(p++, 0x1000);
|
67 | 16406950 | pbrook | stl_raw(p++, 0);
|
68 | 16406950 | pbrook | /* ATAG_MEM */
|
69 | f93eb9ff | balrog | /* TODO: handle multiple chips on one ATAG list */
|
70 | 16406950 | pbrook | stl_raw(p++, 4);
|
71 | 16406950 | pbrook | stl_raw(p++, 0x54410002);
|
72 | f93eb9ff | balrog | stl_raw(p++, info->ram_size); |
73 | f93eb9ff | balrog | stl_raw(p++, info->loader_start); |
74 | 16406950 | pbrook | if (initrd_size) {
|
75 | 16406950 | pbrook | /* ATAG_INITRD2 */
|
76 | 16406950 | pbrook | stl_raw(p++, 4);
|
77 | 16406950 | pbrook | stl_raw(p++, 0x54420005);
|
78 | f93eb9ff | balrog | stl_raw(p++, info->loader_start + INITRD_LOAD_ADDR); |
79 | 16406950 | pbrook | stl_raw(p++, initrd_size); |
80 | 16406950 | pbrook | } |
81 | f93eb9ff | balrog | if (info->kernel_cmdline && *info->kernel_cmdline) {
|
82 | 16406950 | pbrook | /* ATAG_CMDLINE */
|
83 | 16406950 | pbrook | int cmdline_size;
|
84 | 16406950 | pbrook | |
85 | f93eb9ff | balrog | cmdline_size = strlen(info->kernel_cmdline); |
86 | f93eb9ff | balrog | memcpy(p + 2, info->kernel_cmdline, cmdline_size + 1); |
87 | 16406950 | pbrook | cmdline_size = (cmdline_size >> 2) + 1; |
88 | 16406950 | pbrook | stl_raw(p++, cmdline_size + 2);
|
89 | 16406950 | pbrook | stl_raw(p++, 0x54410009);
|
90 | 16406950 | pbrook | p += cmdline_size; |
91 | 16406950 | pbrook | } |
92 | f93eb9ff | balrog | if (info->atag_board) {
|
93 | f93eb9ff | balrog | /* ATAG_BOARD */
|
94 | f93eb9ff | balrog | int atag_board_len;
|
95 | f93eb9ff | balrog | |
96 | f93eb9ff | balrog | atag_board_len = (info->atag_board(info, p + 2) + 3) >> 2; |
97 | f93eb9ff | balrog | stl_raw(p++, 2 + atag_board_len);
|
98 | f93eb9ff | balrog | stl_raw(p++, 0x414f4d50);
|
99 | f93eb9ff | balrog | p += atag_board_len; |
100 | f93eb9ff | balrog | } |
101 | 16406950 | pbrook | /* ATAG_END */
|
102 | 16406950 | pbrook | stl_raw(p++, 0);
|
103 | 16406950 | pbrook | stl_raw(p++, 0);
|
104 | 16406950 | pbrook | } |
105 | 16406950 | pbrook | |
106 | f93eb9ff | balrog | static void set_kernel_args_old(struct arm_boot_info *info, |
107 | f93eb9ff | balrog | int initrd_size, void *base) |
108 | 2b8f2d41 | balrog | { |
109 | 2b8f2d41 | balrog | uint32_t *p; |
110 | 2b8f2d41 | balrog | unsigned char *s; |
111 | 2b8f2d41 | balrog | |
112 | 2b8f2d41 | balrog | /* see linux/include/asm-arm/setup.h */
|
113 | f93eb9ff | balrog | p = (uint32_t *)(base + KERNEL_ARGS_ADDR); |
114 | 2b8f2d41 | balrog | /* page_size */
|
115 | 2b8f2d41 | balrog | stl_raw(p++, 4096);
|
116 | 2b8f2d41 | balrog | /* nr_pages */
|
117 | f93eb9ff | balrog | stl_raw(p++, info->ram_size / 4096);
|
118 | 2b8f2d41 | balrog | /* ramdisk_size */
|
119 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
120 | 2b8f2d41 | balrog | #define FLAG_READONLY 1 |
121 | 2b8f2d41 | balrog | #define FLAG_RDLOAD 4 |
122 | 2b8f2d41 | balrog | #define FLAG_RDPROMPT 8 |
123 | 2b8f2d41 | balrog | /* flags */
|
124 | 2b8f2d41 | balrog | stl_raw(p++, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT); |
125 | 2b8f2d41 | balrog | /* rootdev */
|
126 | 2b8f2d41 | balrog | stl_raw(p++, (31 << 8) | 0); /* /dev/mtdblock0 */ |
127 | 2b8f2d41 | balrog | /* video_num_cols */
|
128 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
129 | 2b8f2d41 | balrog | /* video_num_rows */
|
130 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
131 | 2b8f2d41 | balrog | /* video_x */
|
132 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
133 | 2b8f2d41 | balrog | /* video_y */
|
134 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
135 | 2b8f2d41 | balrog | /* memc_control_reg */
|
136 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
137 | 2b8f2d41 | balrog | /* unsigned char sounddefault */
|
138 | 2b8f2d41 | balrog | /* unsigned char adfsdrives */
|
139 | 2b8f2d41 | balrog | /* unsigned char bytes_per_char_h */
|
140 | 2b8f2d41 | balrog | /* unsigned char bytes_per_char_v */
|
141 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
142 | 2b8f2d41 | balrog | /* pages_in_bank[4] */
|
143 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
144 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
145 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
146 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
147 | 2b8f2d41 | balrog | /* pages_in_vram */
|
148 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
149 | 2b8f2d41 | balrog | /* initrd_start */
|
150 | 2b8f2d41 | balrog | if (initrd_size)
|
151 | f93eb9ff | balrog | stl_raw(p++, info->loader_start + INITRD_LOAD_ADDR); |
152 | 2b8f2d41 | balrog | else
|
153 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
154 | 2b8f2d41 | balrog | /* initrd_size */
|
155 | 2b8f2d41 | balrog | stl_raw(p++, initrd_size); |
156 | 2b8f2d41 | balrog | /* rd_start */
|
157 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
158 | 2b8f2d41 | balrog | /* system_rev */
|
159 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
160 | 2b8f2d41 | balrog | /* system_serial_low */
|
161 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
162 | 2b8f2d41 | balrog | /* system_serial_high */
|
163 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
164 | 2b8f2d41 | balrog | /* mem_fclk_21285 */
|
165 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
166 | 2b8f2d41 | balrog | /* zero unused fields */
|
167 | 2b8f2d41 | balrog | memset(p, 0, 256 + 1024 - |
168 | f93eb9ff | balrog | (p - ((uint32_t *)(base + KERNEL_ARGS_ADDR)))); |
169 | f93eb9ff | balrog | s = base + KERNEL_ARGS_ADDR + 256 + 1024; |
170 | f93eb9ff | balrog | if (info->kernel_cmdline)
|
171 | f93eb9ff | balrog | strcpy (s, info->kernel_cmdline); |
172 | 2b8f2d41 | balrog | else
|
173 | 2b8f2d41 | balrog | stb_raw(s, 0);
|
174 | 2b8f2d41 | balrog | } |
175 | 2b8f2d41 | balrog | |
176 | f93eb9ff | balrog | void arm_load_kernel(CPUState *env, struct arm_boot_info *info) |
177 | 16406950 | pbrook | { |
178 | 16406950 | pbrook | int kernel_size;
|
179 | 16406950 | pbrook | int initrd_size;
|
180 | 16406950 | pbrook | int n;
|
181 | 1c7b3754 | pbrook | int is_linux = 0; |
182 | 1c7b3754 | pbrook | uint64_t elf_entry; |
183 | 1c7b3754 | pbrook | target_ulong entry; |
184 | f93eb9ff | balrog | uint32_t pd; |
185 | f93eb9ff | balrog | void *loader_phys;
|
186 | 16406950 | pbrook | |
187 | 16406950 | pbrook | /* Load the kernel. */
|
188 | f93eb9ff | balrog | if (!info->kernel_filename) {
|
189 | 16406950 | pbrook | fprintf(stderr, "Kernel image must be specified\n");
|
190 | 16406950 | pbrook | exit(1);
|
191 | 16406950 | pbrook | } |
192 | daf90626 | pbrook | |
193 | f93eb9ff | balrog | if (!env->boot_info) {
|
194 | f93eb9ff | balrog | if (info->nb_cpus == 0) |
195 | f93eb9ff | balrog | info->nb_cpus = 1;
|
196 | f93eb9ff | balrog | env->boot_info = info; |
197 | f3d6b95e | pbrook | qemu_register_reset(main_cpu_reset, env); |
198 | f3d6b95e | pbrook | } |
199 | f93eb9ff | balrog | |
200 | f93eb9ff | balrog | pd = cpu_get_physical_page_desc(info->loader_start); |
201 | f93eb9ff | balrog | loader_phys = phys_ram_base + (pd & TARGET_PAGE_MASK) + |
202 | f93eb9ff | balrog | (info->loader_start & ~TARGET_PAGE_MASK); |
203 | f93eb9ff | balrog | |
204 | 1c7b3754 | pbrook | /* Assume that raw images are linux kernels, and ELF images are not. */
|
205 | f93eb9ff | balrog | kernel_size = load_elf(info->kernel_filename, 0, &elf_entry, NULL, NULL); |
206 | 1c7b3754 | pbrook | entry = elf_entry; |
207 | 1c7b3754 | pbrook | if (kernel_size < 0) { |
208 | f93eb9ff | balrog | kernel_size = load_uboot(info->kernel_filename, &entry, &is_linux); |
209 | 1c7b3754 | pbrook | } |
210 | 1c7b3754 | pbrook | if (kernel_size < 0) { |
211 | f93eb9ff | balrog | kernel_size = load_image(info->kernel_filename, |
212 | f93eb9ff | balrog | loader_phys + KERNEL_LOAD_ADDR); |
213 | f93eb9ff | balrog | entry = info->loader_start + KERNEL_LOAD_ADDR; |
214 | 1c7b3754 | pbrook | is_linux = 1;
|
215 | 1c7b3754 | pbrook | } |
216 | 1c7b3754 | pbrook | if (kernel_size < 0) { |
217 | f93eb9ff | balrog | fprintf(stderr, "qemu: could not load kernel '%s'\n",
|
218 | f93eb9ff | balrog | info->kernel_filename); |
219 | 1c7b3754 | pbrook | exit(1);
|
220 | 1c7b3754 | pbrook | } |
221 | 1c7b3754 | pbrook | if (!is_linux) {
|
222 | 1c7b3754 | pbrook | /* Jump to the entry point. */
|
223 | daf90626 | pbrook | env->regs[15] = entry & 0xfffffffe; |
224 | daf90626 | pbrook | env->thumb = entry & 1;
|
225 | daf90626 | pbrook | } else {
|
226 | f93eb9ff | balrog | if (info->initrd_filename) {
|
227 | f93eb9ff | balrog | initrd_size = load_image(info->initrd_filename, |
228 | f93eb9ff | balrog | loader_phys + INITRD_LOAD_ADDR); |
229 | daf90626 | pbrook | if (initrd_size < 0) { |
230 | daf90626 | pbrook | fprintf(stderr, "qemu: could not load initrd '%s'\n",
|
231 | f93eb9ff | balrog | info->initrd_filename); |
232 | daf90626 | pbrook | exit(1);
|
233 | daf90626 | pbrook | } |
234 | daf90626 | pbrook | } else {
|
235 | daf90626 | pbrook | initrd_size = 0;
|
236 | daf90626 | pbrook | } |
237 | f93eb9ff | balrog | bootloader[1] |= info->board_id & 0xff; |
238 | f93eb9ff | balrog | bootloader[2] |= (info->board_id >> 8) & 0xff; |
239 | f93eb9ff | balrog | bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
|
240 | 1c7b3754 | pbrook | bootloader[6] = entry;
|
241 | daf90626 | pbrook | for (n = 0; n < sizeof(bootloader) / 4; n++) |
242 | f93eb9ff | balrog | stl_raw(loader_phys + (n * 4), bootloader[n]);
|
243 | f93eb9ff | balrog | if (info->nb_cpus > 1) |
244 | f93eb9ff | balrog | for (n = 0; n < sizeof(smpboot) / 4; n++) |
245 | f93eb9ff | balrog | stl_raw(loader_phys + info->ram_size + (n * 4), smpboot[n]);
|
246 | 2b8f2d41 | balrog | if (old_param)
|
247 | f93eb9ff | balrog | set_kernel_args_old(info, initrd_size, loader_phys); |
248 | 2b8f2d41 | balrog | else
|
249 | f93eb9ff | balrog | set_kernel_args(info, initrd_size, loader_phys); |
250 | 16406950 | pbrook | } |
251 | 16406950 | pbrook | } |