root / hw / arm_boot.c @ 368eb5d4
History | View | Annotate | Download (7.7 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 | 52b43737 | pbrook | #define WRITE_WORD(p, value) do { \ |
57 | 52b43737 | pbrook | stl_phys_notdirty(p, value); \ |
58 | 52b43737 | pbrook | p += 4; \
|
59 | 52b43737 | pbrook | } while (0) |
60 | 52b43737 | pbrook | |
61 | f93eb9ff | balrog | static void set_kernel_args(struct arm_boot_info *info, |
62 | 52b43737 | pbrook | int initrd_size, target_phys_addr_t base)
|
63 | 16406950 | pbrook | { |
64 | 52b43737 | pbrook | target_phys_addr_t p; |
65 | 16406950 | pbrook | |
66 | 52b43737 | pbrook | p = base + KERNEL_ARGS_ADDR; |
67 | 16406950 | pbrook | /* ATAG_CORE */
|
68 | 52b43737 | pbrook | WRITE_WORD(p, 5);
|
69 | 52b43737 | pbrook | WRITE_WORD(p, 0x54410001);
|
70 | 52b43737 | pbrook | WRITE_WORD(p, 1);
|
71 | 52b43737 | pbrook | WRITE_WORD(p, 0x1000);
|
72 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
73 | 16406950 | pbrook | /* ATAG_MEM */
|
74 | f93eb9ff | balrog | /* TODO: handle multiple chips on one ATAG list */
|
75 | 52b43737 | pbrook | WRITE_WORD(p, 4);
|
76 | 52b43737 | pbrook | WRITE_WORD(p, 0x54410002);
|
77 | 52b43737 | pbrook | WRITE_WORD(p, info->ram_size); |
78 | 52b43737 | pbrook | WRITE_WORD(p, info->loader_start); |
79 | 16406950 | pbrook | if (initrd_size) {
|
80 | 16406950 | pbrook | /* ATAG_INITRD2 */
|
81 | 52b43737 | pbrook | WRITE_WORD(p, 4);
|
82 | 52b43737 | pbrook | WRITE_WORD(p, 0x54420005);
|
83 | 52b43737 | pbrook | WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR); |
84 | 52b43737 | pbrook | WRITE_WORD(p, initrd_size); |
85 | 16406950 | pbrook | } |
86 | f93eb9ff | balrog | if (info->kernel_cmdline && *info->kernel_cmdline) {
|
87 | 16406950 | pbrook | /* ATAG_CMDLINE */
|
88 | 16406950 | pbrook | int cmdline_size;
|
89 | 16406950 | pbrook | |
90 | f93eb9ff | balrog | cmdline_size = strlen(info->kernel_cmdline); |
91 | 52b43737 | pbrook | cpu_physical_memory_write(p + 8, (void *)info->kernel_cmdline, |
92 | 52b43737 | pbrook | cmdline_size + 1);
|
93 | 16406950 | pbrook | cmdline_size = (cmdline_size >> 2) + 1; |
94 | 52b43737 | pbrook | WRITE_WORD(p, cmdline_size + 2);
|
95 | 52b43737 | pbrook | WRITE_WORD(p, 0x54410009);
|
96 | 52b43737 | pbrook | p += cmdline_size * 4;
|
97 | 16406950 | pbrook | } |
98 | f93eb9ff | balrog | if (info->atag_board) {
|
99 | f93eb9ff | balrog | /* ATAG_BOARD */
|
100 | f93eb9ff | balrog | int atag_board_len;
|
101 | 52b43737 | pbrook | uint8_t atag_board_buf[0x1000];
|
102 | f93eb9ff | balrog | |
103 | 52b43737 | pbrook | atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3; |
104 | 52b43737 | pbrook | WRITE_WORD(p, (atag_board_len + 8) >> 2); |
105 | 52b43737 | pbrook | WRITE_WORD(p, 0x414f4d50);
|
106 | 52b43737 | pbrook | cpu_physical_memory_write(p, atag_board_buf, atag_board_len); |
107 | f93eb9ff | balrog | p += atag_board_len; |
108 | f93eb9ff | balrog | } |
109 | 16406950 | pbrook | /* ATAG_END */
|
110 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
111 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
112 | 16406950 | pbrook | } |
113 | 16406950 | pbrook | |
114 | f93eb9ff | balrog | static void set_kernel_args_old(struct arm_boot_info *info, |
115 | 52b43737 | pbrook | int initrd_size, target_phys_addr_t base)
|
116 | 2b8f2d41 | balrog | { |
117 | 52b43737 | pbrook | target_phys_addr_t p; |
118 | 52b43737 | pbrook | const char *s; |
119 | 52b43737 | pbrook | |
120 | 2b8f2d41 | balrog | |
121 | 2b8f2d41 | balrog | /* see linux/include/asm-arm/setup.h */
|
122 | 52b43737 | pbrook | p = base + KERNEL_ARGS_ADDR; |
123 | 2b8f2d41 | balrog | /* page_size */
|
124 | 52b43737 | pbrook | WRITE_WORD(p, 4096);
|
125 | 2b8f2d41 | balrog | /* nr_pages */
|
126 | 52b43737 | pbrook | WRITE_WORD(p, info->ram_size / 4096);
|
127 | 2b8f2d41 | balrog | /* ramdisk_size */
|
128 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
129 | 2b8f2d41 | balrog | #define FLAG_READONLY 1 |
130 | 2b8f2d41 | balrog | #define FLAG_RDLOAD 4 |
131 | 2b8f2d41 | balrog | #define FLAG_RDPROMPT 8 |
132 | 2b8f2d41 | balrog | /* flags */
|
133 | 52b43737 | pbrook | WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT); |
134 | 2b8f2d41 | balrog | /* rootdev */
|
135 | 52b43737 | pbrook | WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */ |
136 | 2b8f2d41 | balrog | /* video_num_cols */
|
137 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
138 | 2b8f2d41 | balrog | /* video_num_rows */
|
139 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
140 | 2b8f2d41 | balrog | /* video_x */
|
141 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
142 | 2b8f2d41 | balrog | /* video_y */
|
143 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
144 | 2b8f2d41 | balrog | /* memc_control_reg */
|
145 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
146 | 2b8f2d41 | balrog | /* unsigned char sounddefault */
|
147 | 2b8f2d41 | balrog | /* unsigned char adfsdrives */
|
148 | 2b8f2d41 | balrog | /* unsigned char bytes_per_char_h */
|
149 | 2b8f2d41 | balrog | /* unsigned char bytes_per_char_v */
|
150 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
151 | 2b8f2d41 | balrog | /* pages_in_bank[4] */
|
152 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
153 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
154 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
155 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
156 | 2b8f2d41 | balrog | /* pages_in_vram */
|
157 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
158 | 2b8f2d41 | balrog | /* initrd_start */
|
159 | 2b8f2d41 | balrog | if (initrd_size)
|
160 | 52b43737 | pbrook | WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR); |
161 | 2b8f2d41 | balrog | else
|
162 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
163 | 2b8f2d41 | balrog | /* initrd_size */
|
164 | 52b43737 | pbrook | WRITE_WORD(p, initrd_size); |
165 | 2b8f2d41 | balrog | /* rd_start */
|
166 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
167 | 2b8f2d41 | balrog | /* system_rev */
|
168 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
169 | 2b8f2d41 | balrog | /* system_serial_low */
|
170 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
171 | 2b8f2d41 | balrog | /* system_serial_high */
|
172 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
173 | 2b8f2d41 | balrog | /* mem_fclk_21285 */
|
174 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
175 | 2b8f2d41 | balrog | /* zero unused fields */
|
176 | 52b43737 | pbrook | while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) { |
177 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
178 | 52b43737 | pbrook | } |
179 | 52b43737 | pbrook | s = info->kernel_cmdline; |
180 | 52b43737 | pbrook | if (s) {
|
181 | 52b43737 | pbrook | cpu_physical_memory_write(p, (void *)s, strlen(s) + 1); |
182 | 52b43737 | pbrook | } else {
|
183 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
184 | 52b43737 | pbrook | } |
185 | 2b8f2d41 | balrog | } |
186 | 2b8f2d41 | balrog | |
187 | f93eb9ff | balrog | void arm_load_kernel(CPUState *env, struct arm_boot_info *info) |
188 | 16406950 | pbrook | { |
189 | 16406950 | pbrook | int kernel_size;
|
190 | 16406950 | pbrook | int initrd_size;
|
191 | 16406950 | pbrook | int n;
|
192 | 1c7b3754 | pbrook | int is_linux = 0; |
193 | 1c7b3754 | pbrook | uint64_t elf_entry; |
194 | 1c7b3754 | pbrook | target_ulong entry; |
195 | 16406950 | pbrook | |
196 | 16406950 | pbrook | /* Load the kernel. */
|
197 | f93eb9ff | balrog | if (!info->kernel_filename) {
|
198 | 16406950 | pbrook | fprintf(stderr, "Kernel image must be specified\n");
|
199 | 16406950 | pbrook | exit(1);
|
200 | 16406950 | pbrook | } |
201 | daf90626 | pbrook | |
202 | f93eb9ff | balrog | if (!env->boot_info) {
|
203 | f93eb9ff | balrog | if (info->nb_cpus == 0) |
204 | f93eb9ff | balrog | info->nb_cpus = 1;
|
205 | f93eb9ff | balrog | env->boot_info = info; |
206 | a08d4367 | Jan Kiszka | qemu_register_reset(main_cpu_reset, env); |
207 | f3d6b95e | pbrook | } |
208 | f93eb9ff | balrog | |
209 | 1c7b3754 | pbrook | /* Assume that raw images are linux kernels, and ELF images are not. */
|
210 | f93eb9ff | balrog | kernel_size = load_elf(info->kernel_filename, 0, &elf_entry, NULL, NULL); |
211 | 1c7b3754 | pbrook | entry = elf_entry; |
212 | 1c7b3754 | pbrook | if (kernel_size < 0) { |
213 | 5a9154e0 | aliguori | kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
|
214 | 5a9154e0 | aliguori | &is_linux); |
215 | 1c7b3754 | pbrook | } |
216 | 1c7b3754 | pbrook | if (kernel_size < 0) { |
217 | f93eb9ff | balrog | entry = info->loader_start + KERNEL_LOAD_ADDR; |
218 | 3b760e04 | pbrook | kernel_size = load_image_targphys(info->kernel_filename, entry, |
219 | 3b760e04 | pbrook | ram_size - KERNEL_LOAD_ADDR); |
220 | 1c7b3754 | pbrook | is_linux = 1;
|
221 | 1c7b3754 | pbrook | } |
222 | 1c7b3754 | pbrook | if (kernel_size < 0) { |
223 | f93eb9ff | balrog | fprintf(stderr, "qemu: could not load kernel '%s'\n",
|
224 | f93eb9ff | balrog | info->kernel_filename); |
225 | 1c7b3754 | pbrook | exit(1);
|
226 | 1c7b3754 | pbrook | } |
227 | 1c7b3754 | pbrook | if (!is_linux) {
|
228 | 1c7b3754 | pbrook | /* Jump to the entry point. */
|
229 | daf90626 | pbrook | env->regs[15] = entry & 0xfffffffe; |
230 | daf90626 | pbrook | env->thumb = entry & 1;
|
231 | daf90626 | pbrook | } else {
|
232 | f93eb9ff | balrog | if (info->initrd_filename) {
|
233 | 3b760e04 | pbrook | initrd_size = load_image_targphys(info->initrd_filename, |
234 | 3b760e04 | pbrook | info->loader_start |
235 | 3b760e04 | pbrook | + INITRD_LOAD_ADDR, |
236 | 3b760e04 | pbrook | ram_size - INITRD_LOAD_ADDR); |
237 | daf90626 | pbrook | if (initrd_size < 0) { |
238 | daf90626 | pbrook | fprintf(stderr, "qemu: could not load initrd '%s'\n",
|
239 | f93eb9ff | balrog | info->initrd_filename); |
240 | daf90626 | pbrook | exit(1);
|
241 | daf90626 | pbrook | } |
242 | daf90626 | pbrook | } else {
|
243 | daf90626 | pbrook | initrd_size = 0;
|
244 | daf90626 | pbrook | } |
245 | f93eb9ff | balrog | bootloader[1] |= info->board_id & 0xff; |
246 | f93eb9ff | balrog | bootloader[2] |= (info->board_id >> 8) & 0xff; |
247 | f93eb9ff | balrog | bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
|
248 | 1c7b3754 | pbrook | bootloader[6] = entry;
|
249 | 52b43737 | pbrook | for (n = 0; n < sizeof(bootloader) / 4; n++) { |
250 | 52b43737 | pbrook | stl_phys_notdirty(info->loader_start + (n * 4), bootloader[n]);
|
251 | 52b43737 | pbrook | } |
252 | 52b43737 | pbrook | if (info->nb_cpus > 1) { |
253 | 52b43737 | pbrook | for (n = 0; n < sizeof(smpboot) / 4; n++) { |
254 | 52b43737 | pbrook | stl_phys_notdirty(info->smp_loader_start + (n * 4), smpboot[n]);
|
255 | 52b43737 | pbrook | } |
256 | 52b43737 | pbrook | } |
257 | 2b8f2d41 | balrog | if (old_param)
|
258 | 52b43737 | pbrook | set_kernel_args_old(info, initrd_size, info->loader_start); |
259 | 2b8f2d41 | balrog | else
|
260 | 52b43737 | pbrook | set_kernel_args(info, initrd_size, info->loader_start); |
261 | 16406950 | pbrook | } |
262 | 16406950 | pbrook | } |