This commit was manufactured by cvs2svn to create tag
[linux-2.6.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 #define __KERNGLUE__
12 #include "ppc32-types.h"
13 #include "zlib.h"
14 #include <linux/elf.h>
15 #include <linux/string.h>
16 #include <asm/processor.h>
17 #include <asm/page.h>
18 #include <asm/bootinfo.h>
19
20 extern void *finddevice(const char *);
21 extern int getprop(void *, const char *, void *, int);
22 extern void printk(char *fmt, ...);
23 extern void printf(const char *fmt, ...);
24 extern int sprintf(char *buf, const char *fmt, ...);
25 void gunzip(void *, int, unsigned char *, int *);
26 void *claim(unsigned int, unsigned int, unsigned int);
27 void flush_cache(void *, unsigned long);
28 void pause(void);
29 extern void exit(void);
30
31 unsigned long strlen(const char *s);
32 void *memmove(void *dest, const void *src, unsigned long n);
33 void *memcpy(void *dest, const void *src, unsigned long n);
34
35 /* Value picked to match that used by yaboot */
36 #define PROG_START      0x01400000
37 #define RAM_END         (256<<20) // Fixme: use OF */
38
39 char *avail_ram;
40 char *begin_avail, *end_avail;
41 char *avail_high;
42 unsigned int heap_use;
43 unsigned int heap_max;
44
45 extern char _start[];
46 extern char _vmlinux_start[];
47 extern char _vmlinux_end[];
48 extern char _initrd_start[];
49 extern char _initrd_end[];
50 extern unsigned long vmlinux_filesize;
51 extern unsigned long vmlinux_memsize;
52
53 struct addr_range {
54         unsigned long addr;
55         unsigned long size;
56         unsigned long memsize;
57 };
58 struct addr_range vmlinux = {0, 0, 0};
59 struct addr_range vmlinuz = {0, 0, 0};
60 struct addr_range initrd  = {0, 0, 0};
61
62 static char scratch[128<<10];   /* 128kB of scratch space for gunzip */
63
64 typedef void (*kernel_entry_t)( unsigned long,
65                                 unsigned long,
66                                 void *,
67                                 void *);
68
69
70 int (*prom)(void *);
71
72 void *chosen_handle;
73 void *stdin;
74 void *stdout;
75 void *stderr;
76
77 #define DEBUG
78
79 static unsigned long claim_base = PROG_START;
80
81 static unsigned long try_claim(unsigned long size)
82 {
83         unsigned long addr = 0;
84
85         for(; claim_base < RAM_END; claim_base += 0x100000) {
86 #ifdef DEBUG
87                 printf("    trying: 0x%08lx\n\r", claim_base);
88 #endif
89                 addr = (unsigned long)claim(claim_base, size, 0);
90                 if ((void *)addr != (void *)-1)
91                         break;
92         }
93         if (addr == 0)
94                 return 0;
95         claim_base = PAGE_ALIGN(claim_base + size);
96         return addr;
97 }
98
99 void start(unsigned long a1, unsigned long a2, void *promptr)
100 {
101         unsigned long i;
102         kernel_entry_t kernel_entry;
103         Elf64_Ehdr *elf64;
104         Elf64_Phdr *elf64ph;
105
106         prom = (int (*)(void *)) promptr;
107         chosen_handle = finddevice("/chosen");
108         if (chosen_handle == (void *) -1)
109                 exit();
110         if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4)
111                 exit();
112         stderr = stdout;
113         if (getprop(chosen_handle, "stdin", &stdin, sizeof(stdin)) != 4)
114                 exit();
115
116         printf("zImage starting: loaded at 0x%x\n\r", (unsigned)_start);
117
118         /*
119          * Now we try to claim some memory for the kernel itself
120          * our "vmlinux_memsize" is the memory footprint in RAM, _HOWEVER_, what
121          * our Makefile stuffs in is an image containing all sort of junk including
122          * an ELF header. We need to do some calculations here to find the right
123          * size... In practice we add 1Mb, that is enough, but we should really
124          * consider fixing the Makefile to put a _raw_ kernel in there !
125          */
126         vmlinux_memsize += 0x100000;
127         printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux_memsize);
128         vmlinux.addr = try_claim(vmlinux_memsize);
129         if (vmlinux.addr == 0) {
130                 printf("Can't allocate memory for kernel image !\n\r");
131                 exit();
132         }
133         vmlinuz.addr = (unsigned long)_vmlinux_start;
134         vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
135         vmlinux.size = PAGE_ALIGN(vmlinux_filesize);
136         vmlinux.memsize = vmlinux_memsize;
137
138         /*
139          * Now we try to claim memory for the initrd (and copy it there)
140          */
141         initrd.size = (unsigned long)(_initrd_end - _initrd_start);
142         initrd.memsize = initrd.size;
143         if ( initrd.size > 0 ) {
144                 printf("Allocating 0x%lx bytes for initrd ...\n\r", initrd.size);
145                 initrd.addr = try_claim(initrd.size);
146                 if (initrd.addr == 0) {
147                         printf("Can't allocate memory for initial ramdisk !\n\r");
148                         exit();
149                 }
150                 a1 = initrd.addr;
151                 a2 = initrd.size;
152                 printf("initial ramdisk moving 0x%lx <- 0x%lx (%lx bytes)\n\r",
153                        initrd.addr, (unsigned long)_initrd_start, initrd.size);
154                 memmove((void *)initrd.addr, (void *)_initrd_start, initrd.size);
155                 printf("initrd head: 0x%lx\n", *((u32 *)initrd.addr));
156         }
157
158         /* Eventually gunzip the kernel */
159         if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
160                 int len;
161                 avail_ram = scratch;
162                 begin_avail = avail_high = avail_ram;
163                 end_avail = scratch + sizeof(scratch);
164                 printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
165                        vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size);
166                 len = vmlinuz.size;
167                 gunzip((void *)vmlinux.addr, vmlinux.size,
168                         (unsigned char *)vmlinuz.addr, &len);
169                 printf("done 0x%lx bytes\n\r", len);
170                 printf("0x%x bytes of heap consumed, max in use 0x%\n\r",
171                        (unsigned)(avail_high - begin_avail), heap_max);
172         } else {
173                 memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,vmlinuz.size);
174         }
175
176         /* Skip over the ELF header */
177         elf64 = (Elf64_Ehdr *)vmlinux.addr;
178         if ( elf64->e_ident[EI_MAG0]  != ELFMAG0        ||
179              elf64->e_ident[EI_MAG1]  != ELFMAG1        ||
180              elf64->e_ident[EI_MAG2]  != ELFMAG2        ||
181              elf64->e_ident[EI_MAG3]  != ELFMAG3        ||
182              elf64->e_ident[EI_CLASS] != ELFCLASS64     ||
183              elf64->e_ident[EI_DATA]  != ELFDATA2MSB    ||
184              elf64->e_type            != ET_EXEC        ||
185              elf64->e_machine         != EM_PPC64 )
186         {
187                 printf("Error: not a valid PPC64 ELF file!\n\r");
188                 exit();
189         }
190
191         elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
192                                 (unsigned long)elf64->e_phoff);
193         for(i=0; i < (unsigned int)elf64->e_phnum ;i++,elf64ph++) {
194                 if (elf64ph->p_type == PT_LOAD && elf64ph->p_offset != 0)
195                         break;
196         }
197 #ifdef DEBUG
198         printf("... skipping 0x%lx bytes of ELF header\n\r",
199                         (unsigned long)elf64ph->p_offset);
200 #endif
201         vmlinux.addr += (unsigned long)elf64ph->p_offset;
202         vmlinux.size -= (unsigned long)elf64ph->p_offset;
203
204         flush_cache((void *)vmlinux.addr, vmlinux.memsize);
205
206         if (a1)
207                 printf("initrd head: 0x%lx\n\r", *((u32 *)initrd.addr));
208
209         kernel_entry = (kernel_entry_t)vmlinux.addr;
210 #ifdef DEBUG
211         printf( "kernel:\n\r"
212                 "        entry addr = 0x%lx\n\r"
213                 "        a1         = 0x%lx,\n\r"
214                 "        a2         = 0x%lx,\n\r"
215                 "        prom       = 0x%lx,\n\r"
216                 "        bi_recs    = 0x%lx,\n\r",
217                 (unsigned long)kernel_entry, a1, a2,
218                 (unsigned long)prom, NULL);
219 #endif
220
221         kernel_entry( a1, a2, prom, NULL );
222
223         printf("Error: Linux kernel returned to zImage bootloader!\n\r");
224
225         exit();
226 }
227
228 struct memchunk {
229         unsigned int size;
230         unsigned int pad;
231         struct memchunk *next;
232 };
233
234 static struct memchunk *freechunks;
235
236 void *zalloc(void *x, unsigned items, unsigned size)
237 {
238         void *p;
239         struct memchunk **mpp, *mp;
240
241         size *= items;
242         size = _ALIGN(size, sizeof(struct memchunk));
243         heap_use += size;
244         if (heap_use > heap_max)
245                 heap_max = heap_use;
246         for (mpp = &freechunks; (mp = *mpp) != 0; mpp = &mp->next) {
247                 if (mp->size == size) {
248                         *mpp = mp->next;
249                         return mp;
250                 }
251         }
252         p = avail_ram;
253         avail_ram += size;
254         if (avail_ram > avail_high)
255                 avail_high = avail_ram;
256         if (avail_ram > end_avail) {
257                 printf("oops... out of memory\n\r");
258                 pause();
259         }
260         return p;
261 }
262
263 void zfree(void *x, void *addr, unsigned nb)
264 {
265         struct memchunk *mp = addr;
266
267         nb = _ALIGN(nb, sizeof(struct memchunk));
268         heap_use -= nb;
269         if (avail_ram == addr + nb) {
270                 avail_ram = addr;
271                 return;
272         }
273         mp->size = nb;
274         mp->next = freechunks;
275         freechunks = mp;
276 }
277
278 #define HEAD_CRC        2
279 #define EXTRA_FIELD     4
280 #define ORIG_NAME       8
281 #define COMMENT         0x10
282 #define RESERVED        0xe0
283
284 #define DEFLATED        8
285
286 void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
287 {
288         z_stream s;
289         int r, i, flags;
290
291         /* skip header */
292         i = 10;
293         flags = src[3];
294         if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
295                 printf("bad gzipped data\n\r");
296                 exit();
297         }
298         if ((flags & EXTRA_FIELD) != 0)
299                 i = 12 + src[10] + (src[11] << 8);
300         if ((flags & ORIG_NAME) != 0)
301                 while (src[i++] != 0)
302                         ;
303         if ((flags & COMMENT) != 0)
304                 while (src[i++] != 0)
305                         ;
306         if ((flags & HEAD_CRC) != 0)
307                 i += 2;
308         if (i >= *lenp) {
309                 printf("gunzip: ran out of data in header\n\r");
310                 exit();
311         }
312
313         s.zalloc = zalloc;
314         s.zfree = zfree;
315         r = inflateInit2(&s, -MAX_WBITS);
316         if (r != Z_OK) {
317                 printf("inflateInit2 returned %d\n\r", r);
318                 exit();
319         }
320         s.next_in = src + i;
321         s.avail_in = *lenp - i;
322         s.next_out = dst;
323         s.avail_out = dstlen;
324         r = inflate(&s, Z_FINISH);
325         if (r != Z_OK && r != Z_STREAM_END) {
326                 printf("inflate returned %d msg: %s\n\r", r, s.msg);
327                 exit();
328         }
329         *lenp = s.next_out - (unsigned char *) dst;
330         inflateEnd(&s);
331 }
332