2 * Copyright (C) Paul Mackerras 1997.
4 * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
19 #include "flatdevtree.h"
21 extern void flush_cache(void *, unsigned long);
24 extern char __bss_start[];
26 extern char _vmlinux_start[];
27 extern char _vmlinux_end[];
28 extern char _initrd_start[];
29 extern char _initrd_end[];
30 extern char _dtb_start[];
31 extern char _dtb_end[];
36 unsigned long memsize;
38 static struct addr_range vmlinux;
39 static struct addr_range vmlinuz;
40 static struct addr_range initrd;
42 static unsigned long elfoffset;
45 /* scratch space for gunzip; 46912 is from zlib_inflate_workspacesize() */
46 static char scratch[46912];
47 static char elfheader[256];
49 typedef void (*kernel_entry_t)(unsigned long, unsigned long, void *);
59 static void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
67 if (src[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
68 printf("bad gzipped data\n\r");
71 if ((flags & EXTRA_FIELD) != 0)
72 i = 12 + src[10] + (src[11] << 8);
73 if ((flags & ORIG_NAME) != 0)
76 if ((flags & COMMENT) != 0)
79 if ((flags & HEAD_CRC) != 0)
82 printf("gunzip: ran out of data in header\n\r");
86 if (zlib_inflate_workspacesize() > sizeof(scratch)) {
87 printf("gunzip needs more mem\n");
90 memset(&s, 0, sizeof(s));
91 s.workspace = scratch;
92 r = zlib_inflateInit2(&s, -MAX_WBITS);
94 printf("inflateInit2 returned %d\n\r", r);
98 s.avail_in = *lenp - i;
100 s.avail_out = dstlen;
101 r = zlib_inflate(&s, Z_FULL_FLUSH);
102 if (r != Z_OK && r != Z_STREAM_END) {
103 printf("inflate returned %d msg: %s\n\r", r, s.msg);
106 *lenp = s.next_out - (unsigned char *) dst;
110 static int is_elf64(void *hdr)
112 Elf64_Ehdr *elf64 = hdr;
116 if (!(elf64->e_ident[EI_MAG0] == ELFMAG0 &&
117 elf64->e_ident[EI_MAG1] == ELFMAG1 &&
118 elf64->e_ident[EI_MAG2] == ELFMAG2 &&
119 elf64->e_ident[EI_MAG3] == ELFMAG3 &&
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))
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)
131 if (i >= (unsigned int)elf64->e_phnum)
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;
142 static int is_elf32(void *hdr)
144 Elf32_Ehdr *elf32 = hdr;
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))
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)
163 if (i >= elf32->e_phnum)
166 elfoffset = elf32ph->p_offset;
167 vmlinux.size = elf32ph->p_filesz + elf32ph->p_offset;
168 vmlinux.memsize = elf32ph->p_memsz + elf32ph->p_offset;
172 static void prep_kernel(unsigned long *a1, unsigned long *a2)
176 vmlinuz.addr = (unsigned long)_vmlinux_start;
177 vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
179 /* gunzip the ELF header of the kernel */
180 if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
182 gunzip(elfheader, sizeof(elfheader),
183 (unsigned char *)vmlinuz.addr, &len);
185 memcpy(elfheader, (const void *)vmlinuz.addr,
188 if (!is_elf64(elfheader) && !is_elf32(elfheader)) {
189 printf("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
192 if (platform_ops.image_hdr)
193 platform_ops.image_hdr(elfheader);
195 /* We need to alloc the memsize plus the file offset since gzip
196 * will expand the header (file offset), then the kernel, then
197 * possible rubbish we don't care about. But the kernel bss must
198 * be claimed (it will be zero'd by the kernel itself)
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");
208 * Now we try to alloc memory for the initrd (and copy it there)
210 initrd.size = (unsigned long)(_initrd_end - _initrd_start);
211 initrd.memsize = initrd.size;
212 if ( initrd.size > 0 ) {
213 printf("Allocating 0x%lx bytes for initrd ...\n\r",
215 initrd.addr = (unsigned long)malloc((u32)initrd.size);
216 if (initrd.addr == 0) {
217 printf("Can't allocate memory for initial "
223 printf("initial ramdisk moving 0x%lx <- 0x%lx "
224 "(0x%lx bytes)\n\r", initrd.addr,
225 (unsigned long)_initrd_start, initrd.size);
226 memmove((void *)initrd.addr, (void *)_initrd_start,
228 printf("initrd head: 0x%lx\n\r",
229 *((unsigned long *)initrd.addr));
232 /* Eventually gunzip the kernel */
233 if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
234 printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
235 vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size);
237 gunzip((void *)vmlinux.addr, vmlinux.memsize,
238 (unsigned char *)vmlinuz.addr, &len);
239 printf("done 0x%lx bytes\n\r", len);
241 memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,
245 /* Skip over the ELF header */
247 printf("... skipping 0x%lx bytes of ELF header\n\r",
250 vmlinux.addr += elfoffset;
252 flush_cache((void *)vmlinux.addr, vmlinux.size);
255 /* A buffer that may be edited by tools operating on a zImage binary so as to
256 * edit the command line passed to vmlinux (by setting /chosen/bootargs).
257 * The buffer is put in it's own section so that tools may locate it easier.
259 static char builtin_cmdline[COMMAND_LINE_SIZE]
260 __attribute__((__section__("__builtin_cmdline")));
262 static void get_cmdline(char *buf, int size)
265 int len = strlen(builtin_cmdline);
269 if (len > 0) { /* builtin_cmdline overrides dt's /chosen/bootargs */
270 len = min(len, size-1);
271 strncpy(buf, builtin_cmdline, len);
274 else if ((devp = finddevice("/chosen")))
275 getprop(devp, "bootargs", buf, size);
278 static void set_cmdline(char *buf)
282 if ((devp = finddevice("/chosen")))
283 setprop(devp, "bootargs", buf, strlen(buf) + 1);
286 struct platform_ops platform_ops;
287 struct dt_ops dt_ops;
288 struct console_ops console_ops;
290 void start(unsigned long a1, unsigned long a2, void *promptr, void *sp)
292 kernel_entry_t kentry;
293 char cmdline[COMMAND_LINE_SIZE];
295 memset(__bss_start, 0, _end - __bss_start);
296 memset(&platform_ops, 0, sizeof(platform_ops));
297 memset(&dt_ops, 0, sizeof(dt_ops));
298 memset(&console_ops, 0, sizeof(console_ops));
300 if (platform_init(promptr, _dtb_start, _dtb_end))
302 if (console_ops.open && (console_ops.open() < 0))
304 if (platform_ops.fixups)
305 platform_ops.fixups();
307 printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
310 prep_kernel(&a1, &a2);
312 /* If cmdline came from zimage wrapper or if we can edit the one
313 * in the dt, print it out and edit it, if possible.
315 if ((strlen(builtin_cmdline) > 0) || console_ops.edit_cmdline) {
316 get_cmdline(cmdline, COMMAND_LINE_SIZE);
317 printf("\n\rLinux/PowerPC load: %s", cmdline);
318 if (console_ops.edit_cmdline)
319 console_ops.edit_cmdline(cmdline, COMMAND_LINE_SIZE);
321 set_cmdline(cmdline);
324 if (console_ops.close)
327 kentry = (kernel_entry_t) vmlinux.addr;
328 if (_dtb_end > _dtb_start) {
330 kentry(dt_ops.ft_addr(), 0, NULL);
333 /* XXX initrd addr/size should be passed in properties */
334 kentry(a1, a2, promptr);
336 /* console closed so printf below may not work */
337 printf("Error: Linux kernel returned to zImage boot wrapper!\n\r");