root / hw / arm_boot.c @ 7f27bae6
History | View | Annotate | Download (6.5 kB)
1 | 16406950 | pbrook | /*
|
---|---|---|---|
2 | 16406950 | pbrook | * ARM kernel loader.
|
3 | 16406950 | pbrook | *
|
4 | 16406950 | pbrook | * Copyright (c) 2006 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 | 16406950 | pbrook | #include "vl.h" |
11 | 16406950 | pbrook | |
12 | 16406950 | pbrook | #define KERNEL_ARGS_ADDR 0x100 |
13 | 16406950 | pbrook | #define KERNEL_LOAD_ADDR 0x00010000 |
14 | 16406950 | pbrook | #define INITRD_LOAD_ADDR 0x00800000 |
15 | 16406950 | pbrook | |
16 | 16406950 | pbrook | /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
|
17 | 16406950 | pbrook | static uint32_t bootloader[] = {
|
18 | 16406950 | pbrook | 0xe3a00000, /* mov r0, #0 */ |
19 | 16406950 | pbrook | 0xe3a01000, /* mov r1, #0x?? */ |
20 | 16406950 | pbrook | 0xe3811c00, /* orr r1, r1, #0x??00 */ |
21 | 16406950 | pbrook | 0xe59f2000, /* ldr r2, [pc, #0] */ |
22 | 16406950 | pbrook | 0xe59ff000, /* ldr pc, [pc, #0] */ |
23 | 16406950 | pbrook | 0, /* Address of kernel args. Set by integratorcp_init. */ |
24 | 16406950 | pbrook | 0 /* Kernel entry point. Set by integratorcp_init. */ |
25 | 16406950 | pbrook | }; |
26 | 16406950 | pbrook | |
27 | f3d6b95e | pbrook | static void main_cpu_reset(void *opaque) |
28 | f3d6b95e | pbrook | { |
29 | f3d6b95e | pbrook | CPUState *env = opaque; |
30 | f3d6b95e | pbrook | |
31 | f3d6b95e | pbrook | cpu_reset(env); |
32 | f3d6b95e | pbrook | if (env->kernel_filename)
|
33 | f3d6b95e | pbrook | arm_load_kernel(env, env->ram_size, env->kernel_filename, |
34 | f3d6b95e | pbrook | env->kernel_cmdline, env->initrd_filename, |
35 | 9d551997 | balrog | env->board_id, env->loader_start); |
36 | f3d6b95e | pbrook | } |
37 | f3d6b95e | pbrook | |
38 | 16406950 | pbrook | static void set_kernel_args(uint32_t ram_size, int initrd_size, |
39 | 9d551997 | balrog | const char *kernel_cmdline, |
40 | 9d551997 | balrog | target_phys_addr_t loader_start) |
41 | 16406950 | pbrook | { |
42 | 16406950 | pbrook | uint32_t *p; |
43 | 16406950 | pbrook | |
44 | 16406950 | pbrook | p = (uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR); |
45 | 16406950 | pbrook | /* ATAG_CORE */
|
46 | 16406950 | pbrook | stl_raw(p++, 5);
|
47 | 16406950 | pbrook | stl_raw(p++, 0x54410001);
|
48 | 16406950 | pbrook | stl_raw(p++, 1);
|
49 | 16406950 | pbrook | stl_raw(p++, 0x1000);
|
50 | 16406950 | pbrook | stl_raw(p++, 0);
|
51 | 16406950 | pbrook | /* ATAG_MEM */
|
52 | 16406950 | pbrook | stl_raw(p++, 4);
|
53 | 16406950 | pbrook | stl_raw(p++, 0x54410002);
|
54 | 16406950 | pbrook | stl_raw(p++, ram_size); |
55 | 9d551997 | balrog | stl_raw(p++, loader_start); |
56 | 16406950 | pbrook | if (initrd_size) {
|
57 | 16406950 | pbrook | /* ATAG_INITRD2 */
|
58 | 16406950 | pbrook | stl_raw(p++, 4);
|
59 | 16406950 | pbrook | stl_raw(p++, 0x54420005);
|
60 | 9d551997 | balrog | stl_raw(p++, loader_start + INITRD_LOAD_ADDR); |
61 | 16406950 | pbrook | stl_raw(p++, initrd_size); |
62 | 16406950 | pbrook | } |
63 | 16406950 | pbrook | if (kernel_cmdline && *kernel_cmdline) {
|
64 | 16406950 | pbrook | /* ATAG_CMDLINE */
|
65 | 16406950 | pbrook | int cmdline_size;
|
66 | 16406950 | pbrook | |
67 | 16406950 | pbrook | cmdline_size = strlen(kernel_cmdline); |
68 | 16406950 | pbrook | memcpy (p + 2, kernel_cmdline, cmdline_size + 1); |
69 | 16406950 | pbrook | cmdline_size = (cmdline_size >> 2) + 1; |
70 | 16406950 | pbrook | stl_raw(p++, cmdline_size + 2);
|
71 | 16406950 | pbrook | stl_raw(p++, 0x54410009);
|
72 | 16406950 | pbrook | p += cmdline_size; |
73 | 16406950 | pbrook | } |
74 | 16406950 | pbrook | /* ATAG_END */
|
75 | 16406950 | pbrook | stl_raw(p++, 0);
|
76 | 16406950 | pbrook | stl_raw(p++, 0);
|
77 | 16406950 | pbrook | } |
78 | 16406950 | pbrook | |
79 | 2b8f2d41 | balrog | static void set_kernel_args_old(uint32_t ram_size, int initrd_size, |
80 | 2b8f2d41 | balrog | const char *kernel_cmdline, |
81 | 2b8f2d41 | balrog | target_phys_addr_t loader_start) |
82 | 2b8f2d41 | balrog | { |
83 | 2b8f2d41 | balrog | uint32_t *p; |
84 | 2b8f2d41 | balrog | unsigned char *s; |
85 | 2b8f2d41 | balrog | |
86 | 2b8f2d41 | balrog | /* see linux/include/asm-arm/setup.h */
|
87 | 2b8f2d41 | balrog | p = (uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR); |
88 | 2b8f2d41 | balrog | /* page_size */
|
89 | 2b8f2d41 | balrog | stl_raw(p++, 4096);
|
90 | 2b8f2d41 | balrog | /* nr_pages */
|
91 | 2b8f2d41 | balrog | stl_raw(p++, ram_size / 4096);
|
92 | 2b8f2d41 | balrog | /* ramdisk_size */
|
93 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
94 | 2b8f2d41 | balrog | #define FLAG_READONLY 1 |
95 | 2b8f2d41 | balrog | #define FLAG_RDLOAD 4 |
96 | 2b8f2d41 | balrog | #define FLAG_RDPROMPT 8 |
97 | 2b8f2d41 | balrog | /* flags */
|
98 | 2b8f2d41 | balrog | stl_raw(p++, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT); |
99 | 2b8f2d41 | balrog | /* rootdev */
|
100 | 2b8f2d41 | balrog | stl_raw(p++, (31 << 8) | 0); /* /dev/mtdblock0 */ |
101 | 2b8f2d41 | balrog | /* video_num_cols */
|
102 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
103 | 2b8f2d41 | balrog | /* video_num_rows */
|
104 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
105 | 2b8f2d41 | balrog | /* video_x */
|
106 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
107 | 2b8f2d41 | balrog | /* video_y */
|
108 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
109 | 2b8f2d41 | balrog | /* memc_control_reg */
|
110 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
111 | 2b8f2d41 | balrog | /* unsigned char sounddefault */
|
112 | 2b8f2d41 | balrog | /* unsigned char adfsdrives */
|
113 | 2b8f2d41 | balrog | /* unsigned char bytes_per_char_h */
|
114 | 2b8f2d41 | balrog | /* unsigned char bytes_per_char_v */
|
115 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
116 | 2b8f2d41 | balrog | /* pages_in_bank[4] */
|
117 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
118 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
119 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
120 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
121 | 2b8f2d41 | balrog | /* pages_in_vram */
|
122 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
123 | 2b8f2d41 | balrog | /* initrd_start */
|
124 | 2b8f2d41 | balrog | if (initrd_size)
|
125 | 2b8f2d41 | balrog | stl_raw(p++, loader_start + INITRD_LOAD_ADDR); |
126 | 2b8f2d41 | balrog | else
|
127 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
128 | 2b8f2d41 | balrog | /* initrd_size */
|
129 | 2b8f2d41 | balrog | stl_raw(p++, initrd_size); |
130 | 2b8f2d41 | balrog | /* rd_start */
|
131 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
132 | 2b8f2d41 | balrog | /* system_rev */
|
133 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
134 | 2b8f2d41 | balrog | /* system_serial_low */
|
135 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
136 | 2b8f2d41 | balrog | /* system_serial_high */
|
137 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
138 | 2b8f2d41 | balrog | /* mem_fclk_21285 */
|
139 | 2b8f2d41 | balrog | stl_raw(p++, 0);
|
140 | 2b8f2d41 | balrog | /* zero unused fields */
|
141 | 2b8f2d41 | balrog | memset(p, 0, 256 + 1024 - |
142 | 2b8f2d41 | balrog | (p - ((uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR)))); |
143 | 2b8f2d41 | balrog | s = phys_ram_base + KERNEL_ARGS_ADDR + 256 + 1024; |
144 | 2b8f2d41 | balrog | if (kernel_cmdline)
|
145 | 2b8f2d41 | balrog | strcpy (s, kernel_cmdline); |
146 | 2b8f2d41 | balrog | else
|
147 | 2b8f2d41 | balrog | stb_raw(s, 0);
|
148 | 2b8f2d41 | balrog | } |
149 | 2b8f2d41 | balrog | |
150 | daf90626 | pbrook | void arm_load_kernel(CPUState *env, int ram_size, const char *kernel_filename, |
151 | 16406950 | pbrook | const char *kernel_cmdline, const char *initrd_filename, |
152 | 9d551997 | balrog | int board_id, target_phys_addr_t loader_start)
|
153 | 16406950 | pbrook | { |
154 | 16406950 | pbrook | int kernel_size;
|
155 | 16406950 | pbrook | int initrd_size;
|
156 | 16406950 | pbrook | int n;
|
157 | 1c7b3754 | pbrook | int is_linux = 0; |
158 | 1c7b3754 | pbrook | uint64_t elf_entry; |
159 | 1c7b3754 | pbrook | target_ulong entry; |
160 | 16406950 | pbrook | |
161 | 16406950 | pbrook | /* Load the kernel. */
|
162 | 16406950 | pbrook | if (!kernel_filename) {
|
163 | 16406950 | pbrook | fprintf(stderr, "Kernel image must be specified\n");
|
164 | 16406950 | pbrook | exit(1);
|
165 | 16406950 | pbrook | } |
166 | daf90626 | pbrook | |
167 | f3d6b95e | pbrook | if (!env->kernel_filename) {
|
168 | f3d6b95e | pbrook | env->ram_size = ram_size; |
169 | f3d6b95e | pbrook | env->kernel_filename = kernel_filename; |
170 | f3d6b95e | pbrook | env->kernel_cmdline = kernel_cmdline; |
171 | f3d6b95e | pbrook | env->initrd_filename = initrd_filename; |
172 | f3d6b95e | pbrook | env->board_id = board_id; |
173 | 9d551997 | balrog | env->loader_start = loader_start; |
174 | f3d6b95e | pbrook | qemu_register_reset(main_cpu_reset, env); |
175 | f3d6b95e | pbrook | } |
176 | 1c7b3754 | pbrook | /* Assume that raw images are linux kernels, and ELF images are not. */
|
177 | 74287114 | ths | kernel_size = load_elf(kernel_filename, 0, &elf_entry, NULL, NULL); |
178 | 1c7b3754 | pbrook | entry = elf_entry; |
179 | 1c7b3754 | pbrook | if (kernel_size < 0) { |
180 | 1c7b3754 | pbrook | kernel_size = load_uboot(kernel_filename, &entry, &is_linux); |
181 | 1c7b3754 | pbrook | } |
182 | 1c7b3754 | pbrook | if (kernel_size < 0) { |
183 | 1c7b3754 | pbrook | kernel_size = load_image(kernel_filename, |
184 | 1c7b3754 | pbrook | phys_ram_base + KERNEL_LOAD_ADDR); |
185 | 9d551997 | balrog | entry = loader_start + KERNEL_LOAD_ADDR; |
186 | 1c7b3754 | pbrook | is_linux = 1;
|
187 | 1c7b3754 | pbrook | } |
188 | 1c7b3754 | pbrook | if (kernel_size < 0) { |
189 | 1c7b3754 | pbrook | fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
|
190 | 1c7b3754 | pbrook | exit(1);
|
191 | 1c7b3754 | pbrook | } |
192 | 1c7b3754 | pbrook | if (!is_linux) {
|
193 | 1c7b3754 | pbrook | /* Jump to the entry point. */
|
194 | daf90626 | pbrook | env->regs[15] = entry & 0xfffffffe; |
195 | daf90626 | pbrook | env->thumb = entry & 1;
|
196 | daf90626 | pbrook | } else {
|
197 | daf90626 | pbrook | if (initrd_filename) {
|
198 | daf90626 | pbrook | initrd_size = load_image(initrd_filename, |
199 | daf90626 | pbrook | phys_ram_base + INITRD_LOAD_ADDR); |
200 | daf90626 | pbrook | if (initrd_size < 0) { |
201 | daf90626 | pbrook | fprintf(stderr, "qemu: could not load initrd '%s'\n",
|
202 | daf90626 | pbrook | initrd_filename); |
203 | daf90626 | pbrook | exit(1);
|
204 | daf90626 | pbrook | } |
205 | daf90626 | pbrook | } else {
|
206 | daf90626 | pbrook | initrd_size = 0;
|
207 | daf90626 | pbrook | } |
208 | daf90626 | pbrook | bootloader[1] |= board_id & 0xff; |
209 | daf90626 | pbrook | bootloader[2] |= (board_id >> 8) & 0xff; |
210 | 9d551997 | balrog | bootloader[5] = loader_start + KERNEL_ARGS_ADDR;
|
211 | 1c7b3754 | pbrook | bootloader[6] = entry;
|
212 | daf90626 | pbrook | for (n = 0; n < sizeof(bootloader) / 4; n++) |
213 | daf90626 | pbrook | stl_raw(phys_ram_base + (n * 4), bootloader[n]);
|
214 | 2b8f2d41 | balrog | if (old_param)
|
215 | 2b8f2d41 | balrog | set_kernel_args_old(ram_size, initrd_size, |
216 | 2b8f2d41 | balrog | kernel_cmdline, loader_start); |
217 | 2b8f2d41 | balrog | else
|
218 | 2b8f2d41 | balrog | set_kernel_args(ram_size, initrd_size, |
219 | 2b8f2d41 | balrog | kernel_cmdline, loader_start); |
220 | 16406950 | pbrook | } |
221 | 16406950 | pbrook | } |