root / hw / arm_boot.c @ 70cca6d8
History | View | Annotate | Download (8.3 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 | ca20cf32 | Blue Swirl | #include "loader.h" |
14 | ca20cf32 | Blue Swirl | #include "elf.h" |
15 | 16406950 | pbrook | |
16 | 16406950 | pbrook | #define KERNEL_ARGS_ADDR 0x100 |
17 | 16406950 | pbrook | #define KERNEL_LOAD_ADDR 0x00010000 |
18 | 16406950 | pbrook | #define INITRD_LOAD_ADDR 0x00800000 |
19 | 16406950 | pbrook | |
20 | 16406950 | pbrook | /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
|
21 | 16406950 | pbrook | static uint32_t bootloader[] = {
|
22 | 16406950 | pbrook | 0xe3a00000, /* mov r0, #0 */ |
23 | 16406950 | pbrook | 0xe3a01000, /* mov r1, #0x?? */ |
24 | 16406950 | pbrook | 0xe3811c00, /* orr r1, r1, #0x??00 */ |
25 | 16406950 | pbrook | 0xe59f2000, /* ldr r2, [pc, #0] */ |
26 | 16406950 | pbrook | 0xe59ff000, /* ldr pc, [pc, #0] */ |
27 | 16406950 | pbrook | 0, /* Address of kernel args. Set by integratorcp_init. */ |
28 | 16406950 | pbrook | 0 /* Kernel entry point. Set by integratorcp_init. */ |
29 | 16406950 | pbrook | }; |
30 | 16406950 | pbrook | |
31 | 9ee6e8bb | pbrook | /* Entry point for secondary CPUs. Enable interrupt controller and
|
32 | 9ee6e8bb | pbrook | Issue WFI until start address is written to system controller. */
|
33 | 9ee6e8bb | pbrook | static uint32_t smpboot[] = {
|
34 | f7c70325 | Paul Brook | 0xe59f0020, /* ldr r0, privbase */ |
35 | 9ee6e8bb | pbrook | 0xe3a01001, /* mov r1, #1 */ |
36 | 9ee6e8bb | pbrook | 0xe5801100, /* str r1, [r0, #0x100] */ |
37 | 9ee6e8bb | pbrook | 0xe3a00201, /* mov r0, #0x10000000 */ |
38 | 9ee6e8bb | pbrook | 0xe3800030, /* orr r0, #0x30 */ |
39 | 9ee6e8bb | pbrook | 0xe320f003, /* wfi */ |
40 | 9ee6e8bb | pbrook | 0xe5901000, /* ldr r1, [r0] */ |
41 | be0f204a | Paul Brook | 0xe1110001, /* tst r1, r1 */ |
42 | be0f204a | Paul Brook | 0x0afffffb, /* beq <wfi> */ |
43 | f7c70325 | Paul Brook | 0xe12fff11, /* bx r1 */ |
44 | f7c70325 | Paul Brook | 0 /* privbase: Private memory region base address. */ |
45 | 9ee6e8bb | pbrook | }; |
46 | 9ee6e8bb | pbrook | |
47 | 52b43737 | pbrook | #define WRITE_WORD(p, value) do { \ |
48 | 52b43737 | pbrook | stl_phys_notdirty(p, value); \ |
49 | 52b43737 | pbrook | p += 4; \
|
50 | 52b43737 | pbrook | } while (0) |
51 | 52b43737 | pbrook | |
52 | f93eb9ff | balrog | static void set_kernel_args(struct arm_boot_info *info, |
53 | c227f099 | Anthony Liguori | int initrd_size, target_phys_addr_t base)
|
54 | 16406950 | pbrook | { |
55 | c227f099 | Anthony Liguori | target_phys_addr_t p; |
56 | 16406950 | pbrook | |
57 | 52b43737 | pbrook | p = base + KERNEL_ARGS_ADDR; |
58 | 16406950 | pbrook | /* ATAG_CORE */
|
59 | 52b43737 | pbrook | WRITE_WORD(p, 5);
|
60 | 52b43737 | pbrook | WRITE_WORD(p, 0x54410001);
|
61 | 52b43737 | pbrook | WRITE_WORD(p, 1);
|
62 | 52b43737 | pbrook | WRITE_WORD(p, 0x1000);
|
63 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
64 | 16406950 | pbrook | /* ATAG_MEM */
|
65 | f93eb9ff | balrog | /* TODO: handle multiple chips on one ATAG list */
|
66 | 52b43737 | pbrook | WRITE_WORD(p, 4);
|
67 | 52b43737 | pbrook | WRITE_WORD(p, 0x54410002);
|
68 | 52b43737 | pbrook | WRITE_WORD(p, info->ram_size); |
69 | 52b43737 | pbrook | WRITE_WORD(p, info->loader_start); |
70 | 16406950 | pbrook | if (initrd_size) {
|
71 | 16406950 | pbrook | /* ATAG_INITRD2 */
|
72 | 52b43737 | pbrook | WRITE_WORD(p, 4);
|
73 | 52b43737 | pbrook | WRITE_WORD(p, 0x54420005);
|
74 | 52b43737 | pbrook | WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR); |
75 | 52b43737 | pbrook | WRITE_WORD(p, initrd_size); |
76 | 16406950 | pbrook | } |
77 | f93eb9ff | balrog | if (info->kernel_cmdline && *info->kernel_cmdline) {
|
78 | 16406950 | pbrook | /* ATAG_CMDLINE */
|
79 | 16406950 | pbrook | int cmdline_size;
|
80 | 16406950 | pbrook | |
81 | f93eb9ff | balrog | cmdline_size = strlen(info->kernel_cmdline); |
82 | 52b43737 | pbrook | cpu_physical_memory_write(p + 8, (void *)info->kernel_cmdline, |
83 | 52b43737 | pbrook | cmdline_size + 1);
|
84 | 16406950 | pbrook | cmdline_size = (cmdline_size >> 2) + 1; |
85 | 52b43737 | pbrook | WRITE_WORD(p, cmdline_size + 2);
|
86 | 52b43737 | pbrook | WRITE_WORD(p, 0x54410009);
|
87 | 52b43737 | pbrook | p += cmdline_size * 4;
|
88 | 16406950 | pbrook | } |
89 | f93eb9ff | balrog | if (info->atag_board) {
|
90 | f93eb9ff | balrog | /* ATAG_BOARD */
|
91 | f93eb9ff | balrog | int atag_board_len;
|
92 | 52b43737 | pbrook | uint8_t atag_board_buf[0x1000];
|
93 | f93eb9ff | balrog | |
94 | 52b43737 | pbrook | atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3; |
95 | 52b43737 | pbrook | WRITE_WORD(p, (atag_board_len + 8) >> 2); |
96 | 52b43737 | pbrook | WRITE_WORD(p, 0x414f4d50);
|
97 | 52b43737 | pbrook | cpu_physical_memory_write(p, atag_board_buf, atag_board_len); |
98 | f93eb9ff | balrog | p += atag_board_len; |
99 | f93eb9ff | balrog | } |
100 | 16406950 | pbrook | /* ATAG_END */
|
101 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
102 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
103 | 16406950 | pbrook | } |
104 | 16406950 | pbrook | |
105 | f93eb9ff | balrog | static void set_kernel_args_old(struct arm_boot_info *info, |
106 | c227f099 | Anthony Liguori | int initrd_size, target_phys_addr_t base)
|
107 | 2b8f2d41 | balrog | { |
108 | c227f099 | Anthony Liguori | target_phys_addr_t p; |
109 | 52b43737 | pbrook | const char *s; |
110 | 52b43737 | pbrook | |
111 | 2b8f2d41 | balrog | |
112 | 2b8f2d41 | balrog | /* see linux/include/asm-arm/setup.h */
|
113 | 52b43737 | pbrook | p = base + KERNEL_ARGS_ADDR; |
114 | 2b8f2d41 | balrog | /* page_size */
|
115 | 52b43737 | pbrook | WRITE_WORD(p, 4096);
|
116 | 2b8f2d41 | balrog | /* nr_pages */
|
117 | 52b43737 | pbrook | WRITE_WORD(p, info->ram_size / 4096);
|
118 | 2b8f2d41 | balrog | /* ramdisk_size */
|
119 | 52b43737 | pbrook | WRITE_WORD(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 | 52b43737 | pbrook | WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT); |
125 | 2b8f2d41 | balrog | /* rootdev */
|
126 | 52b43737 | pbrook | WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */ |
127 | 2b8f2d41 | balrog | /* video_num_cols */
|
128 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
129 | 2b8f2d41 | balrog | /* video_num_rows */
|
130 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
131 | 2b8f2d41 | balrog | /* video_x */
|
132 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
133 | 2b8f2d41 | balrog | /* video_y */
|
134 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
135 | 2b8f2d41 | balrog | /* memc_control_reg */
|
136 | 52b43737 | pbrook | WRITE_WORD(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 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
142 | 2b8f2d41 | balrog | /* pages_in_bank[4] */
|
143 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
144 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
145 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
146 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
147 | 2b8f2d41 | balrog | /* pages_in_vram */
|
148 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
149 | 2b8f2d41 | balrog | /* initrd_start */
|
150 | 2b8f2d41 | balrog | if (initrd_size)
|
151 | 52b43737 | pbrook | WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR); |
152 | 2b8f2d41 | balrog | else
|
153 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
154 | 2b8f2d41 | balrog | /* initrd_size */
|
155 | 52b43737 | pbrook | WRITE_WORD(p, initrd_size); |
156 | 2b8f2d41 | balrog | /* rd_start */
|
157 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
158 | 2b8f2d41 | balrog | /* system_rev */
|
159 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
160 | 2b8f2d41 | balrog | /* system_serial_low */
|
161 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
162 | 2b8f2d41 | balrog | /* system_serial_high */
|
163 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
164 | 2b8f2d41 | balrog | /* mem_fclk_21285 */
|
165 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
166 | 2b8f2d41 | balrog | /* zero unused fields */
|
167 | 52b43737 | pbrook | while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) { |
168 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
169 | 52b43737 | pbrook | } |
170 | 52b43737 | pbrook | s = info->kernel_cmdline; |
171 | 52b43737 | pbrook | if (s) {
|
172 | 52b43737 | pbrook | cpu_physical_memory_write(p, (void *)s, strlen(s) + 1); |
173 | 52b43737 | pbrook | } else {
|
174 | 52b43737 | pbrook | WRITE_WORD(p, 0);
|
175 | 52b43737 | pbrook | } |
176 | 2b8f2d41 | balrog | } |
177 | 2b8f2d41 | balrog | |
178 | f2d74978 | Paul Brook | static void main_cpu_reset(void *opaque) |
179 | f2d74978 | Paul Brook | { |
180 | f2d74978 | Paul Brook | CPUState *env = opaque; |
181 | f2d74978 | Paul Brook | struct arm_boot_info *info = env->boot_info;
|
182 | f2d74978 | Paul Brook | |
183 | f2d74978 | Paul Brook | cpu_reset(env); |
184 | f2d74978 | Paul Brook | if (info) {
|
185 | f2d74978 | Paul Brook | if (!info->is_linux) {
|
186 | f2d74978 | Paul Brook | /* Jump to the entry point. */
|
187 | f2d74978 | Paul Brook | env->regs[15] = info->entry & 0xfffffffe; |
188 | f2d74978 | Paul Brook | env->thumb = info->entry & 1;
|
189 | f2d74978 | Paul Brook | } else {
|
190 | f2d74978 | Paul Brook | if (old_param) {
|
191 | f2d74978 | Paul Brook | set_kernel_args_old(info, info->initrd_size, |
192 | f2d74978 | Paul Brook | info->loader_start); |
193 | f2d74978 | Paul Brook | } else {
|
194 | f2d74978 | Paul Brook | set_kernel_args(info, info->initrd_size, info->loader_start); |
195 | f2d74978 | Paul Brook | } |
196 | f2d74978 | Paul Brook | } |
197 | f2d74978 | Paul Brook | } |
198 | f2d74978 | Paul Brook | /* TODO: Reset secondary CPUs. */
|
199 | f2d74978 | Paul Brook | } |
200 | f2d74978 | Paul Brook | |
201 | f93eb9ff | balrog | void arm_load_kernel(CPUState *env, struct arm_boot_info *info) |
202 | 16406950 | pbrook | { |
203 | 16406950 | pbrook | int kernel_size;
|
204 | 16406950 | pbrook | int initrd_size;
|
205 | 16406950 | pbrook | int n;
|
206 | 1c7b3754 | pbrook | int is_linux = 0; |
207 | 1c7b3754 | pbrook | uint64_t elf_entry; |
208 | c227f099 | Anthony Liguori | target_phys_addr_t entry; |
209 | ca20cf32 | Blue Swirl | int big_endian;
|
210 | 16406950 | pbrook | |
211 | 16406950 | pbrook | /* Load the kernel. */
|
212 | f93eb9ff | balrog | if (!info->kernel_filename) {
|
213 | 16406950 | pbrook | fprintf(stderr, "Kernel image must be specified\n");
|
214 | 16406950 | pbrook | exit(1);
|
215 | 16406950 | pbrook | } |
216 | daf90626 | pbrook | |
217 | f2d74978 | Paul Brook | if (info->nb_cpus == 0) |
218 | f2d74978 | Paul Brook | info->nb_cpus = 1;
|
219 | f2d74978 | Paul Brook | env->boot_info = info; |
220 | f93eb9ff | balrog | |
221 | ca20cf32 | Blue Swirl | #ifdef TARGET_WORDS_BIGENDIAN
|
222 | ca20cf32 | Blue Swirl | big_endian = 1;
|
223 | ca20cf32 | Blue Swirl | #else
|
224 | ca20cf32 | Blue Swirl | big_endian = 0;
|
225 | ca20cf32 | Blue Swirl | #endif
|
226 | ca20cf32 | Blue Swirl | |
227 | 1c7b3754 | pbrook | /* Assume that raw images are linux kernels, and ELF images are not. */
|
228 | ca20cf32 | Blue Swirl | kernel_size = load_elf(info->kernel_filename, 0, &elf_entry, NULL, NULL, |
229 | ca20cf32 | Blue Swirl | big_endian, ELF_MACHINE, 1);
|
230 | 1c7b3754 | pbrook | entry = elf_entry; |
231 | 1c7b3754 | pbrook | if (kernel_size < 0) { |
232 | 5a9154e0 | aliguori | kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
|
233 | 5a9154e0 | aliguori | &is_linux); |
234 | 1c7b3754 | pbrook | } |
235 | 1c7b3754 | pbrook | if (kernel_size < 0) { |
236 | f93eb9ff | balrog | entry = info->loader_start + KERNEL_LOAD_ADDR; |
237 | 3b760e04 | pbrook | kernel_size = load_image_targphys(info->kernel_filename, entry, |
238 | 3b760e04 | pbrook | ram_size - KERNEL_LOAD_ADDR); |
239 | 1c7b3754 | pbrook | is_linux = 1;
|
240 | 1c7b3754 | pbrook | } |
241 | 1c7b3754 | pbrook | if (kernel_size < 0) { |
242 | f93eb9ff | balrog | fprintf(stderr, "qemu: could not load kernel '%s'\n",
|
243 | f93eb9ff | balrog | info->kernel_filename); |
244 | 1c7b3754 | pbrook | exit(1);
|
245 | 1c7b3754 | pbrook | } |
246 | f2d74978 | Paul Brook | info->entry = entry; |
247 | f2d74978 | Paul Brook | if (is_linux) {
|
248 | f93eb9ff | balrog | if (info->initrd_filename) {
|
249 | 3b760e04 | pbrook | initrd_size = load_image_targphys(info->initrd_filename, |
250 | 3b760e04 | pbrook | info->loader_start |
251 | 3b760e04 | pbrook | + INITRD_LOAD_ADDR, |
252 | 3b760e04 | pbrook | ram_size - INITRD_LOAD_ADDR); |
253 | daf90626 | pbrook | if (initrd_size < 0) { |
254 | daf90626 | pbrook | fprintf(stderr, "qemu: could not load initrd '%s'\n",
|
255 | f93eb9ff | balrog | info->initrd_filename); |
256 | daf90626 | pbrook | exit(1);
|
257 | daf90626 | pbrook | } |
258 | daf90626 | pbrook | } else {
|
259 | daf90626 | pbrook | initrd_size = 0;
|
260 | daf90626 | pbrook | } |
261 | f93eb9ff | balrog | bootloader[1] |= info->board_id & 0xff; |
262 | f93eb9ff | balrog | bootloader[2] |= (info->board_id >> 8) & 0xff; |
263 | f93eb9ff | balrog | bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
|
264 | 1c7b3754 | pbrook | bootloader[6] = entry;
|
265 | 52b43737 | pbrook | for (n = 0; n < sizeof(bootloader) / 4; n++) { |
266 | f2d74978 | Paul Brook | bootloader[n] = tswap32(bootloader[n]); |
267 | 52b43737 | pbrook | } |
268 | f2d74978 | Paul Brook | rom_add_blob_fixed("bootloader", bootloader, sizeof(bootloader), |
269 | f2d74978 | Paul Brook | info->loader_start); |
270 | 52b43737 | pbrook | if (info->nb_cpus > 1) { |
271 | f7c70325 | Paul Brook | smpboot[10] = info->smp_priv_base;
|
272 | 52b43737 | pbrook | for (n = 0; n < sizeof(smpboot) / 4; n++) { |
273 | f2d74978 | Paul Brook | smpboot[n] = tswap32(smpboot[n]); |
274 | 52b43737 | pbrook | } |
275 | f2d74978 | Paul Brook | rom_add_blob_fixed("smpboot", smpboot, sizeof(smpboot), |
276 | f2d74978 | Paul Brook | info->smp_loader_start); |
277 | 52b43737 | pbrook | } |
278 | f2d74978 | Paul Brook | info->initrd_size = initrd_size; |
279 | 16406950 | pbrook | } |
280 | f2d74978 | Paul Brook | info->is_linux = is_linux; |
281 | f2d74978 | Paul Brook | qemu_register_reset(main_cpu_reset, env); |
282 | 16406950 | pbrook | } |