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