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