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.
18 #include "gunzip_util.h"
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[];
33 static struct gunzip_state gzstate;
38 unsigned long memsize;
40 static struct addr_range vmlinux;
41 static struct addr_range vmlinuz;
42 static struct addr_range initrd;
44 static unsigned long elfoffset;
47 static char elfheader[256];
49 typedef void (*kernel_entry_t)(unsigned long, unsigned long, void *);
53 static int is_elf64(void *hdr)
55 Elf64_Ehdr *elf64 = hdr;
59 if (!(elf64->e_ident[EI_MAG0] == ELFMAG0 &&
60 elf64->e_ident[EI_MAG1] == ELFMAG1 &&
61 elf64->e_ident[EI_MAG2] == ELFMAG2 &&
62 elf64->e_ident[EI_MAG3] == ELFMAG3 &&
63 elf64->e_ident[EI_CLASS] == ELFCLASS64 &&
64 elf64->e_ident[EI_DATA] == ELFDATA2MSB &&
65 elf64->e_type == ET_EXEC &&
66 elf64->e_machine == EM_PPC64))
69 elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
70 (unsigned long)elf64->e_phoff);
71 for (i = 0; i < (unsigned int)elf64->e_phnum; i++, elf64ph++)
72 if (elf64ph->p_type == PT_LOAD)
74 if (i >= (unsigned int)elf64->e_phnum)
77 elfoffset = (unsigned long)elf64ph->p_offset;
78 vmlinux.size = (unsigned long)elf64ph->p_filesz;
79 vmlinux.memsize = (unsigned long)elf64ph->p_memsz;
85 static int is_elf32(void *hdr)
87 Elf32_Ehdr *elf32 = hdr;
91 if (!(elf32->e_ident[EI_MAG0] == ELFMAG0 &&
92 elf32->e_ident[EI_MAG1] == ELFMAG1 &&
93 elf32->e_ident[EI_MAG2] == ELFMAG2 &&
94 elf32->e_ident[EI_MAG3] == ELFMAG3 &&
95 elf32->e_ident[EI_CLASS] == ELFCLASS32 &&
96 elf32->e_ident[EI_DATA] == ELFDATA2MSB &&
97 elf32->e_type == ET_EXEC &&
98 elf32->e_machine == EM_PPC))
101 elf32 = (Elf32_Ehdr *)elfheader;
102 elf32ph = (Elf32_Phdr *) ((unsigned long)elf32 + elf32->e_phoff);
103 for (i = 0; i < elf32->e_phnum; i++, elf32ph++)
104 if (elf32ph->p_type == PT_LOAD)
106 if (i >= elf32->e_phnum)
109 elfoffset = elf32ph->p_offset;
110 vmlinux.size = elf32ph->p_filesz;
111 vmlinux.memsize = elf32ph->p_memsz;
115 static void prep_kernel(unsigned long a1, unsigned long a2)
119 vmlinuz.addr = (unsigned long)_vmlinux_start;
120 vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
122 /* gunzip the ELF header of the kernel */
123 gunzip_start(&gzstate, (void *)vmlinuz.addr, vmlinuz.size);
124 gunzip_exactly(&gzstate, elfheader, sizeof(elfheader));
126 if (!is_elf64(elfheader) && !is_elf32(elfheader)) {
127 printf("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
130 if (platform_ops.image_hdr)
131 platform_ops.image_hdr(elfheader);
133 /* We need to alloc the memsize: gzip will expand the kernel
134 * text/data, then possible rubbish we don't care about. But
135 * the kernel bss must be claimed (it will be zero'd by the
138 printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux.memsize);
139 vmlinux.addr = (unsigned long)malloc(vmlinux.memsize);
140 if (vmlinux.addr == 0) {
141 printf("Can't allocate memory for kernel image !\n\r");
146 * Now find the initrd
148 * First see if we have an image attached to us. If so
149 * allocate memory for it and copy it there.
151 initrd.size = (unsigned long)(_initrd_end - _initrd_start);
152 initrd.memsize = initrd.size;
153 if (initrd.size > 0) {
154 printf("Allocating 0x%lx bytes for initrd ...\n\r",
156 initrd.addr = (unsigned long)malloc((u32)initrd.size);
157 if (initrd.addr == 0) {
158 printf("Can't allocate memory for initial "
162 printf("initial ramdisk moving 0x%lx <- 0x%lx "
163 "(0x%lx bytes)\n\r", initrd.addr,
164 (unsigned long)_initrd_start, initrd.size);
165 memmove((void *)initrd.addr, (void *)_initrd_start,
167 printf("initrd head: 0x%lx\n\r",
168 *((unsigned long *)initrd.addr));
169 } else if (a2 != 0) {
170 /* Otherwise, see if yaboot or another loader gave us an initrd */
172 initrd.memsize = initrd.size = a2;
173 printf("Using loader supplied initrd at 0x%lx (0x%lx bytes)\n\r",
174 initrd.addr, initrd.size);
177 /* Eventually gunzip the kernel */
178 printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
179 vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size);
180 /* discard up to the actual load data */
181 gunzip_discard(&gzstate, elfoffset - sizeof(elfheader));
182 len = gunzip_finish(&gzstate, (void *)vmlinux.addr,
184 printf("done 0x%lx bytes\n\r", len);
186 flush_cache((void *)vmlinux.addr, vmlinux.size);
189 /* A buffer that may be edited by tools operating on a zImage binary so as to
190 * edit the command line passed to vmlinux (by setting /chosen/bootargs).
191 * The buffer is put in it's own section so that tools may locate it easier.
193 static char builtin_cmdline[COMMAND_LINE_SIZE]
194 __attribute__((__section__("__builtin_cmdline")));
196 static void get_cmdline(char *buf, int size)
199 int len = strlen(builtin_cmdline);
203 if (len > 0) { /* builtin_cmdline overrides dt's /chosen/bootargs */
204 len = min(len, size-1);
205 strncpy(buf, builtin_cmdline, len);
208 else if ((devp = finddevice("/chosen")))
209 getprop(devp, "bootargs", buf, size);
212 static void set_cmdline(char *buf)
216 if ((devp = finddevice("/chosen")))
217 setprop(devp, "bootargs", buf, strlen(buf) + 1);
220 struct platform_ops platform_ops;
221 struct dt_ops dt_ops;
222 struct console_ops console_ops;
224 void start(unsigned long a1, unsigned long a2, void *promptr, void *sp)
226 kernel_entry_t kentry;
227 char cmdline[COMMAND_LINE_SIZE];
228 unsigned long ft_addr = 0;
230 memset(__bss_start, 0, _end - __bss_start);
231 memset(&platform_ops, 0, sizeof(platform_ops));
232 memset(&dt_ops, 0, sizeof(dt_ops));
233 memset(&console_ops, 0, sizeof(console_ops));
235 if (platform_init(promptr, _dtb_start, _dtb_end))
237 if (console_ops.open && (console_ops.open() < 0))
239 if (platform_ops.fixups)
240 platform_ops.fixups();
242 printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
247 /* If cmdline came from zimage wrapper or if we can edit the one
248 * in the dt, print it out and edit it, if possible.
250 if ((strlen(builtin_cmdline) > 0) || console_ops.edit_cmdline) {
251 get_cmdline(cmdline, COMMAND_LINE_SIZE);
252 printf("\n\rLinux/PowerPC load: %s", cmdline);
253 if (console_ops.edit_cmdline)
254 console_ops.edit_cmdline(cmdline, COMMAND_LINE_SIZE);
256 set_cmdline(cmdline);
259 printf("Finalizing device tree...");
261 ft_addr = dt_ops.finalize();
263 printf(" flat tree at 0x%lx\n\r", ft_addr);
265 printf(" using OF tree (promptr=%p)\n\r", promptr);
267 if (console_ops.close)
270 kentry = (kernel_entry_t) vmlinux.addr;
272 kentry(ft_addr, 0, NULL);
274 /* XXX initrd addr/size should be passed in properties */
275 kentry(initrd.addr, initrd.size, promptr);
277 /* console closed so printf below may not work */
278 printf("Error: Linux kernel returned to zImage boot wrapper!\n\r");