]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/ppc64/boot/main.c
[PATCH] ppc64 boot: move gunzip function before use
[linux-2.6-omap-h63xx.git] / arch / ppc64 / 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 "prom.h"
18 #include "zlib.h"
19
20 extern void flush_cache(void *, unsigned long);
21
22
23 /* Value picked to match that used by yaboot */
24 #define PROG_START      0x01400000
25 #define RAM_END         (512<<20) // Fixme: use OF */
26 #define ONE_MB          0x100000
27
28 extern char _start[];
29 extern char _end[];
30 extern char _vmlinux_start[];
31 extern char _vmlinux_end[];
32 extern char _initrd_start[];
33 extern char _initrd_end[];
34
35 struct addr_range {
36         unsigned long addr;
37         unsigned long size;
38         unsigned long memsize;
39 };
40 static struct addr_range vmlinux = {0, 0, 0};
41 static struct addr_range vmlinuz = {0, 0, 0};
42 static struct addr_range initrd  = {0, 0, 0};
43
44 static char scratch[46912];     /* scratch space for gunzip, from zlib_inflate_workspacesize() */
45 static char elfheader[256];
46
47
48 typedef void (*kernel_entry_t)( unsigned long,
49                                 unsigned long,
50                                 void *,
51                                 void *);
52
53
54 #undef DEBUG
55
56 static unsigned long claim_base;
57
58 #define HEAD_CRC        2
59 #define EXTRA_FIELD     4
60 #define ORIG_NAME       8
61 #define COMMENT         0x10
62 #define RESERVED        0xe0
63
64 static void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
65 {
66         z_stream s;
67         int r, i, flags;
68
69         /* skip header */
70         i = 10;
71         flags = src[3];
72         if (src[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
73                 printf("bad gzipped data\n\r");
74                 exit();
75         }
76         if ((flags & EXTRA_FIELD) != 0)
77                 i = 12 + src[10] + (src[11] << 8);
78         if ((flags & ORIG_NAME) != 0)
79                 while (src[i++] != 0)
80                         ;
81         if ((flags & COMMENT) != 0)
82                 while (src[i++] != 0)
83                         ;
84         if ((flags & HEAD_CRC) != 0)
85                 i += 2;
86         if (i >= *lenp) {
87                 printf("gunzip: ran out of data in header\n\r");
88                 exit();
89         }
90
91         if (zlib_inflate_workspacesize() > sizeof(scratch)) {
92                 printf("gunzip needs more mem\n");
93                 exit();
94         }
95         memset(&s, 0, sizeof(s));
96         s.workspace = scratch;
97         r = zlib_inflateInit2(&s, -MAX_WBITS);
98         if (r != Z_OK) {
99                 printf("inflateInit2 returned %d\n\r", r);
100                 exit();
101         }
102         s.next_in = src + i;
103         s.avail_in = *lenp - i;
104         s.next_out = dst;
105         s.avail_out = dstlen;
106         r = zlib_inflate(&s, Z_FULL_FLUSH);
107         if (r != Z_OK && r != Z_STREAM_END) {
108                 printf("inflate returned %d msg: %s\n\r", r, s.msg);
109                 exit();
110         }
111         *lenp = s.next_out - (unsigned char *) dst;
112         zlib_inflateEnd(&s);
113 }
114
115 static unsigned long try_claim(unsigned long size)
116 {
117         unsigned long addr = 0;
118
119         for(; claim_base < RAM_END; claim_base += ONE_MB) {
120 #ifdef DEBUG
121                 printf("    trying: 0x%08lx\n\r", claim_base);
122 #endif
123                 addr = (unsigned long)claim(claim_base, size, 0);
124                 if ((void *)addr != (void *)-1)
125                         break;
126         }
127         if (addr == 0)
128                 return 0;
129         claim_base = PAGE_ALIGN(claim_base + size);
130         return addr;
131 }
132
133 void start(unsigned long a1, unsigned long a2, void *promptr)
134 {
135         unsigned long i;
136         int len;
137         kernel_entry_t kernel_entry;
138         Elf64_Ehdr *elf64;
139         Elf64_Phdr *elf64ph;
140
141         prom = (int (*)(void *)) promptr;
142         chosen_handle = finddevice("/chosen");
143         if (chosen_handle == (void *) -1)
144                 exit();
145         if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4)
146                 exit();
147         stderr = stdout;
148         if (getprop(chosen_handle, "stdin", &stdin, sizeof(stdin)) != 4)
149                 exit();
150
151         printf("\n\rzImage starting: loaded at 0x%lx\n\r", (unsigned long) _start);
152
153         /*
154          * The first available claim_base must be above the end of the
155          * the loaded kernel wrapper file (_start to _end includes the
156          * initrd image if it is present) and rounded up to a nice
157          * 1 MB boundary for good measure.
158          */
159
160         claim_base = _ALIGN_UP((unsigned long)_end, ONE_MB);
161
162 #if defined(PROG_START)
163         /*
164          * Maintain a "magic" minimum address. This keeps some older
165          * firmware platforms running.
166          */
167
168         if (claim_base < PROG_START)
169                 claim_base = PROG_START;
170 #endif
171
172         vmlinuz.addr = (unsigned long)_vmlinux_start;
173         vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
174
175         /* gunzip the ELF header of the kernel */
176         if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
177                 len = vmlinuz.size;
178                 gunzip(elfheader, sizeof(elfheader),
179                                 (unsigned char *)vmlinuz.addr, &len);
180         } else
181                 memcpy(elfheader, (const void *)vmlinuz.addr, sizeof(elfheader));
182
183         elf64 = (Elf64_Ehdr *)elfheader;
184         if ( elf64->e_ident[EI_MAG0]  != ELFMAG0        ||
185              elf64->e_ident[EI_MAG1]  != ELFMAG1        ||
186              elf64->e_ident[EI_MAG2]  != ELFMAG2        ||
187              elf64->e_ident[EI_MAG3]  != ELFMAG3        ||
188              elf64->e_ident[EI_CLASS] != ELFCLASS64     ||
189              elf64->e_ident[EI_DATA]  != ELFDATA2MSB    ||
190              elf64->e_type            != ET_EXEC        ||
191              elf64->e_machine         != EM_PPC64 )
192         {
193                 printf("Error: not a valid PPC64 ELF file!\n\r");
194                 exit();
195         }
196
197         elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
198                                 (unsigned long)elf64->e_phoff);
199         for(i=0; i < (unsigned int)elf64->e_phnum ;i++,elf64ph++) {
200                 if (elf64ph->p_type == PT_LOAD && elf64ph->p_offset != 0)
201                         break;
202         }
203         vmlinux.size = (unsigned long)elf64ph->p_filesz;
204         vmlinux.memsize = (unsigned long)elf64ph->p_memsz;
205         printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux.memsize);
206         vmlinux.addr = try_claim(vmlinux.memsize);
207         if (vmlinux.addr == 0) {
208                 printf("Can't allocate memory for kernel image !\n\r");
209                 exit();
210         }
211
212         /*
213          * Now we try to claim memory for the initrd (and copy it there)
214          */
215         initrd.size = (unsigned long)(_initrd_end - _initrd_start);
216         initrd.memsize = initrd.size;
217         if ( initrd.size > 0 ) {
218                 printf("Allocating 0x%lx bytes for initrd ...\n\r", initrd.size);
219                 initrd.addr = try_claim(initrd.size);
220                 if (initrd.addr == 0) {
221                         printf("Can't allocate memory for initial ramdisk !\n\r");
222                         exit();
223                 }
224                 a1 = initrd.addr;
225                 a2 = initrd.size;
226                 printf("initial ramdisk moving 0x%lx <- 0x%lx (0x%lx bytes)\n\r",
227                        initrd.addr, (unsigned long)_initrd_start, initrd.size);
228                 memmove((void *)initrd.addr, (void *)_initrd_start, initrd.size);
229                 printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd.addr));
230         }
231
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);
236                 len = vmlinuz.size;
237                 gunzip((void *)vmlinux.addr, vmlinux.memsize,
238                         (unsigned char *)vmlinuz.addr, &len);
239                 printf("done 0x%lx bytes\n\r", len);
240         } else {
241                 memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,vmlinuz.size);
242         }
243
244         /* Skip over the ELF header */
245 #ifdef DEBUG
246         printf("... skipping 0x%lx bytes of ELF header\n\r",
247                         (unsigned long)elf64ph->p_offset);
248 #endif
249         vmlinux.addr += (unsigned long)elf64ph->p_offset;
250
251         flush_cache((void *)vmlinux.addr, vmlinux.size);
252
253         kernel_entry = (kernel_entry_t)vmlinux.addr;
254 #ifdef DEBUG
255         printf( "kernel:\n\r"
256                 "        entry addr = 0x%lx\n\r"
257                 "        a1         = 0x%lx,\n\r"
258                 "        a2         = 0x%lx,\n\r"
259                 "        prom       = 0x%lx,\n\r"
260                 "        bi_recs    = 0x%lx,\n\r",
261                 (unsigned long)kernel_entry, a1, a2,
262                 (unsigned long)prom, NULL);
263 #endif
264
265         kernel_entry( a1, a2, prom, NULL );
266
267         printf("Error: Linux kernel returned to zImage bootloader!\n\r");
268
269         exit();
270 }
271