This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.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
12 #define __KERNGLUE__
13
14 #include <stdarg.h>
15 #include <stddef.h>
16 #include "elf.h"
17 #include "page.h"
18 #include "string.h"
19 #include "stdio.h"
20 #include "prom.h"
21 #include "zlib.h"
22
23 extern void flush_cache(void *, unsigned long);
24
25
26 /* Value picked to match that used by yaboot */
27 #define PROG_START      0x01400000      /* only used on 64-bit systems */
28 #define RAM_END         (512<<20)       /* Fixme: use OF */
29 #define ONE_MB          0x100000
30
31 extern char _start[];
32 extern char __bss_start[];
33 extern char _end[];
34 extern char _vmlinux_start[];
35 extern char _vmlinux_end[];
36 extern char _initrd_start[];
37 extern char _initrd_end[];
38
39 struct addr_range {
40         unsigned long addr;
41         unsigned long size;
42         unsigned long memsize;
43 };
44 static struct addr_range vmlinux;
45 static struct addr_range vmlinuz;
46 static struct addr_range initrd;
47
48 static unsigned long elfoffset;
49
50 static char scratch[46912];     /* scratch space for gunzip, from zlib_inflate_workspacesize() */
51 static char elfheader[256];
52
53
54 typedef void (*kernel_entry_t)( unsigned long,
55                                 unsigned long,
56                                 void *,
57                                 void *);
58
59
60 #undef DEBUG
61
62 static unsigned long claim_base;
63
64 #define HEAD_CRC        2
65 #define EXTRA_FIELD     4
66 #define ORIG_NAME       8
67 #define COMMENT         0x10
68 #define RESERVED        0xe0
69
70 static void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
71 {
72         z_stream s;
73         int r, i, flags;
74
75         /* skip header */
76         i = 10;
77         flags = src[3];
78         if (src[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
79                 printf("bad gzipped data\n\r");
80                 exit();
81         }
82         if ((flags & EXTRA_FIELD) != 0)
83                 i = 12 + src[10] + (src[11] << 8);
84         if ((flags & ORIG_NAME) != 0)
85                 while (src[i++] != 0)
86                         ;
87         if ((flags & COMMENT) != 0)
88                 while (src[i++] != 0)
89                         ;
90         if ((flags & HEAD_CRC) != 0)
91                 i += 2;
92         if (i >= *lenp) {
93                 printf("gunzip: ran out of data in header\n\r");
94                 exit();
95         }
96
97         if (zlib_inflate_workspacesize() > sizeof(scratch)) {
98                 printf("gunzip needs more mem\n");
99                 exit();
100         }
101         memset(&s, 0, sizeof(s));
102         s.workspace = scratch;
103         r = zlib_inflateInit2(&s, -MAX_WBITS);
104         if (r != Z_OK) {
105                 printf("inflateInit2 returned %d\n\r", r);
106                 exit();
107         }
108         s.next_in = src + i;
109         s.avail_in = *lenp - i;
110         s.next_out = dst;
111         s.avail_out = dstlen;
112         r = zlib_inflate(&s, Z_FULL_FLUSH);
113         if (r != Z_OK && r != Z_STREAM_END) {
114                 printf("inflate returned %d msg: %s\n\r", r, s.msg);
115                 exit();
116         }
117         *lenp = s.next_out - (unsigned char *) dst;
118         zlib_inflateEnd(&s);
119 }
120
121 static unsigned long try_claim(unsigned long size)
122 {
123         unsigned long addr = 0;
124
125         for(; claim_base < RAM_END; claim_base += ONE_MB) {
126 #ifdef DEBUG
127                 printf("    trying: 0x%08lx\n\r", claim_base);
128 #endif
129                 addr = (unsigned long)claim(claim_base, size, 0);
130                 if ((void *)addr != (void *)-1)
131                         break;
132         }
133         if (addr == 0)
134                 return 0;
135         claim_base = PAGE_ALIGN(claim_base + size);
136         return addr;
137 }
138
139 static int is_elf64(void *hdr)
140 {
141         Elf64_Ehdr *elf64 = hdr;
142         Elf64_Phdr *elf64ph;
143         unsigned int i;
144
145         if (!(elf64->e_ident[EI_MAG0]  == ELFMAG0       &&
146               elf64->e_ident[EI_MAG1]  == ELFMAG1       &&
147               elf64->e_ident[EI_MAG2]  == ELFMAG2       &&
148               elf64->e_ident[EI_MAG3]  == ELFMAG3       &&
149               elf64->e_ident[EI_CLASS] == ELFCLASS64    &&
150               elf64->e_ident[EI_DATA]  == ELFDATA2MSB   &&
151               elf64->e_type            == ET_EXEC       &&
152               elf64->e_machine         == EM_PPC64))
153                 return 0;
154
155         elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
156                                  (unsigned long)elf64->e_phoff);
157         for (i = 0; i < (unsigned int)elf64->e_phnum; i++, elf64ph++)
158                 if (elf64ph->p_type == PT_LOAD)
159                         break;
160         if (i >= (unsigned int)elf64->e_phnum)
161                 return 0;
162
163         elfoffset = (unsigned long)elf64ph->p_offset;
164         vmlinux.size = (unsigned long)elf64ph->p_filesz + elfoffset;
165         vmlinux.memsize = (unsigned long)elf64ph->p_memsz + elfoffset;
166
167 #if defined(PROG_START)
168         /*
169          * Maintain a "magic" minimum address. This keeps some older
170          * firmware platforms running.
171          */
172
173         if (claim_base < PROG_START)
174                 claim_base = PROG_START;
175 #endif
176
177         return 1;
178 }
179
180 static int is_elf32(void *hdr)
181 {
182         Elf32_Ehdr *elf32 = hdr;
183         Elf32_Phdr *elf32ph;
184         unsigned int i;
185
186         if (!(elf32->e_ident[EI_MAG0]  == ELFMAG0       &&
187               elf32->e_ident[EI_MAG1]  == ELFMAG1       &&
188               elf32->e_ident[EI_MAG2]  == ELFMAG2       &&
189               elf32->e_ident[EI_MAG3]  == ELFMAG3       &&
190               elf32->e_ident[EI_CLASS] == ELFCLASS32    &&
191               elf32->e_ident[EI_DATA]  == ELFDATA2MSB   &&
192               elf32->e_type            == ET_EXEC       &&
193               elf32->e_machine         == EM_PPC))
194                 return 0;
195
196         elf32 = (Elf32_Ehdr *)elfheader;
197         elf32ph = (Elf32_Phdr *) ((unsigned long)elf32 + elf32->e_phoff);
198         for (i = 0; i < elf32->e_phnum; i++, elf32ph++)
199                 if (elf32ph->p_type == PT_LOAD)
200                         break;
201         if (i >= elf32->e_phnum)
202                 return 0;
203
204         elfoffset = elf32ph->p_offset;
205         vmlinux.size = elf32ph->p_filesz + elf32ph->p_offset;
206         vmlinux.memsize = elf32ph->p_memsz + elf32ph->p_offset;
207         return 1;
208 }
209
210 void start(unsigned long a1, unsigned long a2, void *promptr, void *sp)
211 {
212         int len;
213         kernel_entry_t kernel_entry;
214
215         memset(__bss_start, 0, _end - __bss_start);
216
217         prom = (int (*)(void *)) promptr;
218         chosen_handle = finddevice("/chosen");
219         if (chosen_handle == (void *) -1)
220                 exit();
221         if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4)
222                 exit();
223
224         printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r", _start, sp);
225
226         /*
227          * The first available claim_base must be above the end of the
228          * the loaded kernel wrapper file (_start to _end includes the
229          * initrd image if it is present) and rounded up to a nice
230          * 1 MB boundary for good measure.
231          */
232
233         claim_base = _ALIGN_UP((unsigned long)_end, ONE_MB);
234
235         vmlinuz.addr = (unsigned long)_vmlinux_start;
236         vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
237
238         /* gunzip the ELF header of the kernel */
239         if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
240                 len = vmlinuz.size;
241                 gunzip(elfheader, sizeof(elfheader),
242                                 (unsigned char *)vmlinuz.addr, &len);
243         } else
244                 memcpy(elfheader, (const void *)vmlinuz.addr, sizeof(elfheader));
245
246         if (!is_elf64(elfheader) && !is_elf32(elfheader)) {
247                 printf("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
248                 exit();
249         }
250
251         /* We need to claim the memsize plus the file offset since gzip
252          * will expand the header (file offset), then the kernel, then
253          * possible rubbish we don't care about. But the kernel bss must
254          * be claimed (it will be zero'd by the kernel itself)
255          */
256         printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux.memsize);
257         vmlinux.addr = try_claim(vmlinux.memsize);
258         if (vmlinux.addr == 0) {
259                 printf("Can't allocate memory for kernel image !\n\r");
260                 exit();
261         }
262
263         /*
264          * Now we try to claim memory for the initrd (and copy it there)
265          */
266         initrd.size = (unsigned long)(_initrd_end - _initrd_start);
267         initrd.memsize = initrd.size;
268         if ( initrd.size > 0 ) {
269                 printf("Allocating 0x%lx bytes for initrd ...\n\r", initrd.size);
270                 initrd.addr = try_claim(initrd.size);
271                 if (initrd.addr == 0) {
272                         printf("Can't allocate memory for initial ramdisk !\n\r");
273                         exit();
274                 }
275                 a1 = initrd.addr;
276                 a2 = initrd.size;
277                 printf("initial ramdisk moving 0x%lx <- 0x%lx (0x%lx bytes)\n\r",
278                        initrd.addr, (unsigned long)_initrd_start, initrd.size);
279                 memmove((void *)initrd.addr, (void *)_initrd_start, initrd.size);
280                 printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd.addr));
281         }
282
283         /* Eventually gunzip the kernel */
284         if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
285                 printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
286                        vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size);
287                 len = vmlinuz.size;
288                 gunzip((void *)vmlinux.addr, vmlinux.memsize,
289                         (unsigned char *)vmlinuz.addr, &len);
290                 printf("done 0x%lx bytes\n\r", len);
291         } else {
292                 memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,vmlinuz.size);
293         }
294
295         /* Skip over the ELF header */
296 #ifdef DEBUG
297         printf("... skipping 0x%lx bytes of ELF header\n\r",
298                         elfoffset);
299 #endif
300         vmlinux.addr += elfoffset;
301
302         flush_cache((void *)vmlinux.addr, vmlinux.size);
303
304         kernel_entry = (kernel_entry_t)vmlinux.addr;
305 #ifdef DEBUG
306         printf( "kernel:\n\r"
307                 "        entry addr = 0x%lx\n\r"
308                 "        a1         = 0x%lx,\n\r"
309                 "        a2         = 0x%lx,\n\r"
310                 "        prom       = 0x%lx,\n\r"
311                 "        bi_recs    = 0x%lx,\n\r",
312                 (unsigned long)kernel_entry, a1, a2,
313                 (unsigned long)prom, NULL);
314 #endif
315
316         kernel_entry(a1, a2, prom, NULL);
317
318         printf("Error: Linux kernel returned to zImage bootloader!\n\r");
319
320         exit();
321 }
322