aboutsummaryrefslogtreecommitdiffstats
path: root/arch/powerpc/boot/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/powerpc/boot/main.c')
-rw-r--r--arch/powerpc/boot/main.c369
1 files changed, 118 insertions, 251 deletions
diff --git a/arch/powerpc/boot/main.c b/arch/powerpc/boot/main.c
index 6f6b50d238b6..56b56a8d4b23 100644
--- a/arch/powerpc/boot/main.c
+++ b/arch/powerpc/boot/main.c
@@ -14,11 +14,10 @@
14#include "page.h" 14#include "page.h"
15#include "string.h" 15#include "string.h"
16#include "stdio.h" 16#include "stdio.h"
17#include "zlib.h"
18#include "ops.h" 17#include "ops.h"
18#include "gunzip_util.h"
19#include "flatdevtree.h" 19#include "flatdevtree.h"
20 20#include "reg.h"
21extern void flush_cache(void *, unsigned long);
22 21
23extern char _start[]; 22extern char _start[];
24extern char __bss_start[]; 23extern char __bss_start[];
@@ -30,304 +29,173 @@ extern char _initrd_end[];
30extern char _dtb_start[]; 29extern char _dtb_start[];
31extern char _dtb_end[]; 30extern char _dtb_end[];
32 31
32static struct gunzip_state gzstate;
33
33struct addr_range { 34struct addr_range {
34 unsigned long addr; 35 void *addr;
35 unsigned long size; 36 unsigned long size;
36 unsigned long memsize;
37}; 37};
38static struct addr_range vmlinux;
39static struct addr_range vmlinuz;
40static struct addr_range initrd;
41
42static unsigned long elfoffset;
43static int is_64bit;
44
45/* scratch space for gunzip; 46912 is from zlib_inflate_workspacesize() */
46static char scratch[46912];
47static char elfheader[256];
48 38
49typedef void (*kernel_entry_t)(unsigned long, unsigned long, void *); 39typedef void (*kernel_entry_t)(unsigned long, unsigned long, void *);
50 40
51#undef DEBUG 41#undef DEBUG
52 42
53#define HEAD_CRC 2 43static struct addr_range prep_kernel(void)
54#define EXTRA_FIELD 4
55#define ORIG_NAME 8
56#define COMMENT 0x10
57#define RESERVED 0xe0
58
59static void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
60{ 44{
61 z_stream s; 45 char elfheader[256];
62 int r, i, flags; 46 void *vmlinuz_addr = _vmlinux_start;
63 47 unsigned long vmlinuz_size = _vmlinux_end - _vmlinux_start;
64 /* skip header */ 48 void *addr = 0;
65 i = 10; 49 struct elf_info ei;
66 flags = src[3]; 50 int len;
67 if (src[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
68 printf("bad gzipped data\n\r");
69 exit();
70 }
71 if ((flags & EXTRA_FIELD) != 0)
72 i = 12 + src[10] + (src[11] << 8);
73 if ((flags & ORIG_NAME) != 0)
74 while (src[i++] != 0)
75 ;
76 if ((flags & COMMENT) != 0)
77 while (src[i++] != 0)
78 ;
79 if ((flags & HEAD_CRC) != 0)
80 i += 2;
81 if (i >= *lenp) {
82 printf("gunzip: ran out of data in header\n\r");
83 exit();
84 }
85 51
86 if (zlib_inflate_workspacesize() > sizeof(scratch)) { 52 /* gunzip the ELF header of the kernel */
87 printf("gunzip needs more mem\n"); 53 gunzip_start(&gzstate, vmlinuz_addr, vmlinuz_size);
88 exit(); 54 gunzip_exactly(&gzstate, elfheader, sizeof(elfheader));
89 } 55
90 memset(&s, 0, sizeof(s)); 56 if (!parse_elf64(elfheader, &ei) && !parse_elf32(elfheader, &ei))
91 s.workspace = scratch; 57 fatal("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
92 r = zlib_inflateInit2(&s, -MAX_WBITS); 58
93 if (r != Z_OK) { 59 if (platform_ops.image_hdr)
94 printf("inflateInit2 returned %d\n\r", r); 60 platform_ops.image_hdr(elfheader);
95 exit(); 61
96 } 62 /* We need to alloc the memsize: gzip will expand the kernel
97 s.next_in = src + i; 63 * text/data, then possible rubbish we don't care about. But
98 s.avail_in = *lenp - i; 64 * the kernel bss must be claimed (it will be zero'd by the
99 s.next_out = dst; 65 * kernel itself)
100 s.avail_out = dstlen; 66 */
101 r = zlib_inflate(&s, Z_FULL_FLUSH); 67 printf("Allocating 0x%lx bytes for kernel ...\n\r", ei.memsize);
102 if (r != Z_OK && r != Z_STREAM_END) { 68
103 printf("inflate returned %d msg: %s\n\r", r, s.msg); 69 if (platform_ops.vmlinux_alloc) {
104 exit(); 70 addr = platform_ops.vmlinux_alloc(ei.memsize);
71 } else {
72 if ((unsigned long)_start < ei.memsize)
73 fatal("Insufficient memory for kernel at address 0!"
74 " (_start=%p)\n\r", _start);
105 } 75 }
106 *lenp = s.next_out - (unsigned char *) dst;
107 zlib_inflateEnd(&s);
108}
109 76
110static int is_elf64(void *hdr) 77 /* Finally, gunzip the kernel */
111{ 78 printf("gunzipping (0x%p <- 0x%p:0x%p)...", addr,
112 Elf64_Ehdr *elf64 = hdr; 79 vmlinuz_addr, vmlinuz_addr+vmlinuz_size);
113 Elf64_Phdr *elf64ph; 80 /* discard up to the actual load data */
114 unsigned int i; 81 gunzip_discard(&gzstate, ei.elfoffset - sizeof(elfheader));
115 82 len = gunzip_finish(&gzstate, addr, ei.loadsize);
116 if (!(elf64->e_ident[EI_MAG0] == ELFMAG0 && 83 if (len != ei.loadsize)
117 elf64->e_ident[EI_MAG1] == ELFMAG1 && 84 fatal("ran out of data! only got 0x%x of 0x%lx bytes.\n\r",
118 elf64->e_ident[EI_MAG2] == ELFMAG2 && 85 len, ei.loadsize);
119 elf64->e_ident[EI_MAG3] == ELFMAG3 && 86 printf("done 0x%x bytes\n\r", len);
120 elf64->e_ident[EI_CLASS] == ELFCLASS64 &&
121 elf64->e_ident[EI_DATA] == ELFDATA2MSB &&
122 elf64->e_type == ET_EXEC &&
123 elf64->e_machine == EM_PPC64))
124 return 0;
125
126 elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
127 (unsigned long)elf64->e_phoff);
128 for (i = 0; i < (unsigned int)elf64->e_phnum; i++, elf64ph++)
129 if (elf64ph->p_type == PT_LOAD)
130 break;
131 if (i >= (unsigned int)elf64->e_phnum)
132 return 0;
133
134 elfoffset = (unsigned long)elf64ph->p_offset;
135 vmlinux.size = (unsigned long)elf64ph->p_filesz + elfoffset;
136 vmlinux.memsize = (unsigned long)elf64ph->p_memsz + elfoffset;
137
138 is_64bit = 1;
139 return 1;
140}
141 87
142static int is_elf32(void *hdr) 88 flush_cache(addr, ei.loadsize);
143{ 89
144 Elf32_Ehdr *elf32 = hdr; 90 return (struct addr_range){addr, ei.memsize};
145 Elf32_Phdr *elf32ph;
146 unsigned int i;
147
148 if (!(elf32->e_ident[EI_MAG0] == ELFMAG0 &&
149 elf32->e_ident[EI_MAG1] == ELFMAG1 &&
150 elf32->e_ident[EI_MAG2] == ELFMAG2 &&
151 elf32->e_ident[EI_MAG3] == ELFMAG3 &&
152 elf32->e_ident[EI_CLASS] == ELFCLASS32 &&
153 elf32->e_ident[EI_DATA] == ELFDATA2MSB &&
154 elf32->e_type == ET_EXEC &&
155 elf32->e_machine == EM_PPC))
156 return 0;
157
158 elf32 = (Elf32_Ehdr *)elfheader;
159 elf32ph = (Elf32_Phdr *) ((unsigned long)elf32 + elf32->e_phoff);
160 for (i = 0; i < elf32->e_phnum; i++, elf32ph++)
161 if (elf32ph->p_type == PT_LOAD)
162 break;
163 if (i >= elf32->e_phnum)
164 return 0;
165
166 elfoffset = elf32ph->p_offset;
167 vmlinux.size = elf32ph->p_filesz + elf32ph->p_offset;
168 vmlinux.memsize = elf32ph->p_memsz + elf32ph->p_offset;
169 return 1;
170} 91}
171 92
172static void prep_kernel(unsigned long a1, unsigned long a2) 93static struct addr_range prep_initrd(struct addr_range vmlinux, void *chosen,
94 unsigned long initrd_addr,
95 unsigned long initrd_size)
173{ 96{
174 int len; 97 /* If we have an image attached to us, it overrides anything
175 98 * supplied by the loader. */
176 vmlinuz.addr = (unsigned long)_vmlinux_start; 99 if (_initrd_end > _initrd_start) {
177 vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start); 100 printf("Attached initrd image at 0x%p-0x%p\n\r",
178 101 _initrd_start, _initrd_end);
179 /* gunzip the ELF header of the kernel */ 102 initrd_addr = (unsigned long)_initrd_start;
180 if (*(unsigned short *)vmlinuz.addr == 0x1f8b) { 103 initrd_size = _initrd_end - _initrd_start;
181 len = vmlinuz.size; 104 } else if (initrd_size > 0) {
182 gunzip(elfheader, sizeof(elfheader), 105 printf("Using loader supplied ramdisk at 0x%lx-0x%lx\n\r",
183 (unsigned char *)vmlinuz.addr, &len); 106 initrd_addr, initrd_addr + initrd_size);
184 } else
185 memcpy(elfheader, (const void *)vmlinuz.addr,
186 sizeof(elfheader));
187
188 if (!is_elf64(elfheader) && !is_elf32(elfheader)) {
189 printf("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
190 exit();
191 } 107 }
192 if (platform_ops.image_hdr)
193 platform_ops.image_hdr(elfheader);
194 108
195 /* We need to alloc the memsize plus the file offset since gzip 109 /* If there's no initrd at all, we're done */
196 * will expand the header (file offset), then the kernel, then 110 if (! initrd_size)
197 * possible rubbish we don't care about. But the kernel bss must 111 return (struct addr_range){0, 0};
198 * be claimed (it will be zero'd by the kernel itself)
199 */
200 printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux.memsize);
201 vmlinux.addr = (unsigned long)malloc(vmlinux.memsize);
202 if (vmlinux.addr == 0) {
203 printf("Can't allocate memory for kernel image !\n\r");
204 exit();
205 }
206 112
207 /* 113 /*
208 * Now find the initrd 114 * If the initrd is too low it will be clobbered when the
209 * 115 * kernel relocates to its final location. In this case,
210 * First see if we have an image attached to us. If so 116 * allocate a safer place and move it.
211 * allocate memory for it and copy it there.
212 */ 117 */
213 initrd.size = (unsigned long)(_initrd_end - _initrd_start); 118 if (initrd_addr < vmlinux.size) {
214 initrd.memsize = initrd.size; 119 void *old_addr = (void *)initrd_addr;
215 if (initrd.size > 0) { 120
216 printf("Allocating 0x%lx bytes for initrd ...\n\r", 121 printf("Allocating 0x%lx bytes for initrd ...\n\r",
217 initrd.size); 122 initrd_size);
218 initrd.addr = (unsigned long)malloc((u32)initrd.size); 123 initrd_addr = (unsigned long)malloc(initrd_size);
219 if (initrd.addr == 0) { 124 if (! initrd_addr)
220 printf("Can't allocate memory for initial " 125 fatal("Can't allocate memory for initial "
221 "ramdisk !\n\r"); 126 "ramdisk !\n\r");
222 exit(); 127 printf("Relocating initrd 0x%lx <- 0x%p (0x%lx bytes)\n\r",
223 } 128 initrd_addr, old_addr, initrd_size);
224 printf("initial ramdisk moving 0x%lx <- 0x%lx " 129 memmove((void *)initrd_addr, old_addr, initrd_size);
225 "(0x%lx bytes)\n\r", initrd.addr,
226 (unsigned long)_initrd_start, initrd.size);
227 memmove((void *)initrd.addr, (void *)_initrd_start,
228 initrd.size);
229 printf("initrd head: 0x%lx\n\r",
230 *((unsigned long *)initrd.addr));
231 } else if (a2 != 0) {
232 /* Otherwise, see if yaboot or another loader gave us an initrd */
233 initrd.addr = a1;
234 initrd.memsize = initrd.size = a2;
235 printf("Using loader supplied initrd at 0x%lx (0x%lx bytes)\n\r",
236 initrd.addr, initrd.size);
237 } 130 }
238 131
239 /* Eventually gunzip the kernel */ 132 printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd_addr));
240 if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
241 printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
242 vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size);
243 len = vmlinuz.size;
244 gunzip((void *)vmlinux.addr, vmlinux.memsize,
245 (unsigned char *)vmlinuz.addr, &len);
246 printf("done 0x%lx bytes\n\r", len);
247 } else {
248 memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,
249 vmlinuz.size);
250 }
251 133
252 /* Skip over the ELF header */ 134 /* Tell the kernel initrd address via device tree */
253#ifdef DEBUG 135 setprop_val(chosen, "linux,initrd-start", (u32)(initrd_addr));
254 printf("... skipping 0x%lx bytes of ELF header\n\r", 136 setprop_val(chosen, "linux,initrd-end", (u32)(initrd_addr+initrd_size));
255 elfoffset);
256#endif
257 vmlinux.addr += elfoffset;
258 137
259 flush_cache((void *)vmlinux.addr, vmlinux.size); 138 return (struct addr_range){(void *)initrd_addr, initrd_size};
260} 139}
261 140
262/* A buffer that may be edited by tools operating on a zImage binary so as to 141/* A buffer that may be edited by tools operating on a zImage binary so as to
263 * edit the command line passed to vmlinux (by setting /chosen/bootargs). 142 * edit the command line passed to vmlinux (by setting /chosen/bootargs).
264 * The buffer is put in it's own section so that tools may locate it easier. 143 * The buffer is put in it's own section so that tools may locate it easier.
265 */ 144 */
266static char builtin_cmdline[COMMAND_LINE_SIZE] 145static char cmdline[COMMAND_LINE_SIZE]
267 __attribute__((__section__("__builtin_cmdline"))); 146 __attribute__((__section__("__builtin_cmdline")));
268 147
269static void get_cmdline(char *buf, int size) 148static void prep_cmdline(void *chosen)
270{ 149{
271 void *devp; 150 if (cmdline[0] == '\0')
272 int len = strlen(builtin_cmdline); 151 getprop(chosen, "bootargs", cmdline, COMMAND_LINE_SIZE-1);
273
274 buf[0] = '\0';
275
276 if (len > 0) { /* builtin_cmdline overrides dt's /chosen/bootargs */
277 len = min(len, size-1);
278 strncpy(buf, builtin_cmdline, len);
279 buf[len] = '\0';
280 }
281 else if ((devp = finddevice("/chosen")))
282 getprop(devp, "bootargs", buf, size);
283}
284 152
285static void set_cmdline(char *buf) 153 printf("\n\rLinux/PowerPC load: %s", cmdline);
286{ 154 /* If possible, edit the command line */
287 void *devp; 155 if (console_ops.edit_cmdline)
156 console_ops.edit_cmdline(cmdline, COMMAND_LINE_SIZE);
157 printf("\n\r");
288 158
289 if ((devp = finddevice("/chosen"))) 159 /* Put the command line back into the devtree for the kernel */
290 setprop(devp, "bootargs", buf, strlen(buf) + 1); 160 setprop_str(chosen, "bootargs", cmdline);
291} 161}
292 162
293struct platform_ops platform_ops; 163struct platform_ops platform_ops;
294struct dt_ops dt_ops; 164struct dt_ops dt_ops;
295struct console_ops console_ops; 165struct console_ops console_ops;
166struct loader_info loader_info;
296 167
297void start(unsigned long a1, unsigned long a2, void *promptr, void *sp) 168void start(void)
298{ 169{
170 struct addr_range vmlinux, initrd;
299 kernel_entry_t kentry; 171 kernel_entry_t kentry;
300 char cmdline[COMMAND_LINE_SIZE];
301 unsigned long ft_addr = 0; 172 unsigned long ft_addr = 0;
173 void *chosen;
302 174
303 memset(__bss_start, 0, _end - __bss_start); 175 /* Do this first, because malloc() could clobber the loader's
304 memset(&platform_ops, 0, sizeof(platform_ops)); 176 * command line. Only use the loader command line if a
305 memset(&dt_ops, 0, sizeof(dt_ops)); 177 * built-in command line wasn't set by an external tool */
306 memset(&console_ops, 0, sizeof(console_ops)); 178 if ((loader_info.cmdline_len > 0) && (cmdline[0] == '\0'))
179 memmove(cmdline, loader_info.cmdline,
180 min(loader_info.cmdline_len, COMMAND_LINE_SIZE-1));
307 181
308 if (platform_init(promptr, _dtb_start, _dtb_end))
309 exit();
310 if (console_ops.open && (console_ops.open() < 0)) 182 if (console_ops.open && (console_ops.open() < 0))
311 exit(); 183 exit();
312 if (platform_ops.fixups) 184 if (platform_ops.fixups)
313 platform_ops.fixups(); 185 platform_ops.fixups();
314 186
315 printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r", 187 printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
316 _start, sp); 188 _start, get_sp());
317 189
318 prep_kernel(a1, a2); 190 /* Ensure that the device tree has a /chosen node */
191 chosen = finddevice("/chosen");
192 if (!chosen)
193 chosen = create_node(NULL, "chosen");
319 194
320 /* If cmdline came from zimage wrapper or if we can edit the one 195 vmlinux = prep_kernel();
321 * in the dt, print it out and edit it, if possible. 196 initrd = prep_initrd(vmlinux, chosen,
322 */ 197 loader_info.initrd_addr, loader_info.initrd_size);
323 if ((strlen(builtin_cmdline) > 0) || console_ops.edit_cmdline) { 198 prep_cmdline(chosen);
324 get_cmdline(cmdline, COMMAND_LINE_SIZE);
325 printf("\n\rLinux/PowerPC load: %s", cmdline);
326 if (console_ops.edit_cmdline)
327 console_ops.edit_cmdline(cmdline, COMMAND_LINE_SIZE);
328 printf("\n\r");
329 set_cmdline(cmdline);
330 }
331 199
332 printf("Finalizing device tree..."); 200 printf("Finalizing device tree...");
333 if (dt_ops.finalize) 201 if (dt_ops.finalize)
@@ -335,7 +203,7 @@ void start(unsigned long a1, unsigned long a2, void *promptr, void *sp)
335 if (ft_addr) 203 if (ft_addr)
336 printf(" flat tree at 0x%lx\n\r", ft_addr); 204 printf(" flat tree at 0x%lx\n\r", ft_addr);
337 else 205 else
338 printf(" using OF tree (promptr=%p)\n\r", promptr); 206 printf(" using OF tree (promptr=%p)\n\r", loader_info.promptr);
339 207
340 if (console_ops.close) 208 if (console_ops.close)
341 console_ops.close(); 209 console_ops.close();
@@ -344,10 +212,9 @@ void start(unsigned long a1, unsigned long a2, void *promptr, void *sp)
344 if (ft_addr) 212 if (ft_addr)
345 kentry(ft_addr, 0, NULL); 213 kentry(ft_addr, 0, NULL);
346 else 214 else
347 /* XXX initrd addr/size should be passed in properties */ 215 kentry((unsigned long)initrd.addr, initrd.size,
348 kentry(initrd.addr, initrd.size, promptr); 216 loader_info.promptr);
349 217
350 /* console closed so printf below may not work */ 218 /* console closed so printf in fatal below may not work */
351 printf("Error: Linux kernel returned to zImage boot wrapper!\n\r"); 219 fatal("Error: Linux kernel returned to zImage boot wrapper!\n\r");
352 exit();
353} 220}