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