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