]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/boot/main.c
[POWERPC] zImage: Add more flexible gunzip convenience functions
[linux-2.6-omap-h63xx.git] / arch / powerpc / boot / main.c
1 /*
2  * Copyright (C) Paul Mackerras 1997.
3  *
4  * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner.
5  *
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.
10  */
11 #include <stdarg.h>
12 #include <stddef.h>
13 #include "elf.h"
14 #include "page.h"
15 #include "string.h"
16 #include "stdio.h"
17 #include "ops.h"
18 #include "gunzip_util.h"
19 #include "flatdevtree.h"
20
21 extern void flush_cache(void *, unsigned long);
22
23 extern char _start[];
24 extern char __bss_start[];
25 extern char _end[];
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[];
32
33 static struct gunzip_state gzstate;
34
35 struct addr_range {
36         unsigned long addr;
37         unsigned long size;
38         unsigned long memsize;
39 };
40 static struct addr_range vmlinux;
41 static struct addr_range vmlinuz;
42 static struct addr_range initrd;
43
44 static unsigned long elfoffset;
45 static int is_64bit;
46
47 static char elfheader[256];
48
49 typedef void (*kernel_entry_t)(unsigned long, unsigned long, void *);
50
51 #undef DEBUG
52
53 static int is_elf64(void *hdr)
54 {
55         Elf64_Ehdr *elf64 = hdr;
56         Elf64_Phdr *elf64ph;
57         unsigned int i;
58
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))
67                 return 0;
68
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)
73                         break;
74         if (i >= (unsigned int)elf64->e_phnum)
75                 return 0;
76
77         elfoffset = (unsigned long)elf64ph->p_offset;
78         vmlinux.size = (unsigned long)elf64ph->p_filesz;
79         vmlinux.memsize = (unsigned long)elf64ph->p_memsz;
80
81         is_64bit = 1;
82         return 1;
83 }
84
85 static int is_elf32(void *hdr)
86 {
87         Elf32_Ehdr *elf32 = hdr;
88         Elf32_Phdr *elf32ph;
89         unsigned int i;
90
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))
99                 return 0;
100
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)
105                         break;
106         if (i >= elf32->e_phnum)
107                 return 0;
108
109         elfoffset = elf32ph->p_offset;
110         vmlinux.size = elf32ph->p_filesz;
111         vmlinux.memsize = elf32ph->p_memsz;
112         return 1;
113 }
114
115 static void prep_kernel(unsigned long a1, unsigned long a2)
116 {
117         int len;
118
119         vmlinuz.addr = (unsigned long)_vmlinux_start;
120         vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
121
122         /* gunzip the ELF header of the kernel */
123         gunzip_start(&gzstate, (void *)vmlinuz.addr, vmlinuz.size);
124         gunzip_exactly(&gzstate, elfheader, sizeof(elfheader));
125
126         if (!is_elf64(elfheader) && !is_elf32(elfheader)) {
127                 printf("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
128                 exit();
129         }
130         if (platform_ops.image_hdr)
131                 platform_ops.image_hdr(elfheader);
132
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
136          * kernel itself)
137          */
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");
142                 exit();
143         }
144
145         /*
146          * Now find the initrd
147          *
148          * First see if we have an image attached to us.  If so
149          * allocate memory for it and copy it there.
150          */
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",
155                        initrd.size);
156                 initrd.addr = (unsigned long)malloc((u32)initrd.size);
157                 if (initrd.addr == 0) {
158                         printf("Can't allocate memory for initial "
159                                         "ramdisk !\n\r");
160                         exit();
161                 }
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,
166                         initrd.size);
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 */
171                 initrd.addr = a1;
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);
175         }
176
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,
183                             vmlinux.memsize);
184         printf("done 0x%lx bytes\n\r", len);
185
186         flush_cache((void *)vmlinux.addr, vmlinux.size);
187 }
188
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.
192  */
193 static char builtin_cmdline[COMMAND_LINE_SIZE]
194         __attribute__((__section__("__builtin_cmdline")));
195
196 static void get_cmdline(char *buf, int size)
197 {
198         void *devp;
199         int len = strlen(builtin_cmdline);
200
201         buf[0] = '\0';
202
203         if (len > 0) { /* builtin_cmdline overrides dt's /chosen/bootargs */
204                 len = min(len, size-1);
205                 strncpy(buf, builtin_cmdline, len);
206                 buf[len] = '\0';
207         }
208         else if ((devp = finddevice("/chosen")))
209                 getprop(devp, "bootargs", buf, size);
210 }
211
212 static void set_cmdline(char *buf)
213 {
214         void *devp;
215
216         if ((devp = finddevice("/chosen")))
217                 setprop(devp, "bootargs", buf, strlen(buf) + 1);
218 }
219
220 struct platform_ops platform_ops;
221 struct dt_ops dt_ops;
222 struct console_ops console_ops;
223
224 void start(unsigned long a1, unsigned long a2, void *promptr, void *sp)
225 {
226         kernel_entry_t kentry;
227         char cmdline[COMMAND_LINE_SIZE];
228         unsigned long ft_addr = 0;
229
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));
234
235         if (platform_init(promptr, _dtb_start, _dtb_end))
236                 exit();
237         if (console_ops.open && (console_ops.open() < 0))
238                 exit();
239         if (platform_ops.fixups)
240                 platform_ops.fixups();
241
242         printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
243                _start, sp);
244
245         prep_kernel(a1, a2);
246
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.
249          */
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);
255                 printf("\n\r");
256                 set_cmdline(cmdline);
257         }
258
259         printf("Finalizing device tree...");
260         if (dt_ops.finalize)
261                 ft_addr = dt_ops.finalize();
262         if (ft_addr)
263                 printf(" flat tree at 0x%lx\n\r", ft_addr);
264         else
265                 printf(" using OF tree (promptr=%p)\n\r", promptr);
266
267         if (console_ops.close)
268                 console_ops.close();
269
270         kentry = (kernel_entry_t) vmlinux.addr;
271         if (ft_addr)
272                 kentry(ft_addr, 0, NULL);
273         else
274                 /* XXX initrd addr/size should be passed in properties */
275                 kentry(initrd.addr, initrd.size, promptr);
276
277         /* console closed so printf below may not work */
278         printf("Error: Linux kernel returned to zImage boot wrapper!\n\r");
279         exit();
280 }