blob: e0dde24a72cebda62a135314b8c819230b4fd09a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Olaf Heringdecd3002005-08-08 13:24:38 +100011#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "zlib.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Olaf Heringdecd3002005-08-08 13:24:38 +100020extern void flush_cache(void *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/* Value picked to match that used by yaboot */
24#define PROG_START 0x01400000
Mark Bellon3cc747e2005-09-06 15:50:02 -070025#define RAM_END (512<<20) // Fixme: use OF */
26#define ONE_MB 0x100000
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028extern char _start[];
Olaf Hering9b0cbe92005-10-28 17:46:45 -070029extern char __bss_start[];
Mark Bellon3cc747e2005-09-06 15:50:02 -070030extern char _end[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070031extern char _vmlinux_start[];
32extern char _vmlinux_end[];
33extern char _initrd_start[];
34extern char _initrd_end[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36struct addr_range {
37 unsigned long addr;
38 unsigned long size;
39 unsigned long memsize;
40};
Olaf Heringafbe8c42005-10-28 17:46:47 -070041static struct addr_range vmlinux;
42static struct addr_range vmlinuz;
43static struct addr_range initrd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Olaf Hering70540362005-10-28 17:46:38 -070045static char scratch[46912]; /* scratch space for gunzip, from zlib_inflate_workspacesize() */
Olaf Hering8a76baf2005-10-28 17:46:40 -070046static char elfheader[256];
Olaf Hering70540362005-10-28 17:46:38 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49typedef void (*kernel_entry_t)( unsigned long,
50 unsigned long,
51 void *,
52 void *);
53
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#undef DEBUG
56
Mark Bellon3cc747e2005-09-06 15:50:02 -070057static unsigned long claim_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Olaf Hering6bcc20b2005-10-28 17:46:41 -070059#define HEAD_CRC 2
60#define EXTRA_FIELD 4
61#define ORIG_NAME 8
62#define COMMENT 0x10
63#define RESERVED 0xe0
64
65static void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
66{
67 z_stream s;
68 int r, i, flags;
69
70 /* skip header */
71 i = 10;
72 flags = src[3];
73 if (src[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
74 printf("bad gzipped data\n\r");
75 exit();
76 }
77 if ((flags & EXTRA_FIELD) != 0)
78 i = 12 + src[10] + (src[11] << 8);
79 if ((flags & ORIG_NAME) != 0)
80 while (src[i++] != 0)
81 ;
82 if ((flags & COMMENT) != 0)
83 while (src[i++] != 0)
84 ;
85 if ((flags & HEAD_CRC) != 0)
86 i += 2;
87 if (i >= *lenp) {
88 printf("gunzip: ran out of data in header\n\r");
89 exit();
90 }
91
92 if (zlib_inflate_workspacesize() > sizeof(scratch)) {
93 printf("gunzip needs more mem\n");
94 exit();
95 }
96 memset(&s, 0, sizeof(s));
97 s.workspace = scratch;
98 r = zlib_inflateInit2(&s, -MAX_WBITS);
99 if (r != Z_OK) {
100 printf("inflateInit2 returned %d\n\r", r);
101 exit();
102 }
103 s.next_in = src + i;
104 s.avail_in = *lenp - i;
105 s.next_out = dst;
106 s.avail_out = dstlen;
107 r = zlib_inflate(&s, Z_FULL_FLUSH);
108 if (r != Z_OK && r != Z_STREAM_END) {
109 printf("inflate returned %d msg: %s\n\r", r, s.msg);
110 exit();
111 }
112 *lenp = s.next_out - (unsigned char *) dst;
113 zlib_inflateEnd(&s);
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static unsigned long try_claim(unsigned long size)
117{
118 unsigned long addr = 0;
119
Mark Bellon3cc747e2005-09-06 15:50:02 -0700120 for(; claim_base < RAM_END; claim_base += ONE_MB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#ifdef DEBUG
122 printf(" trying: 0x%08lx\n\r", claim_base);
123#endif
124 addr = (unsigned long)claim(claim_base, size, 0);
125 if ((void *)addr != (void *)-1)
126 break;
127 }
128 if (addr == 0)
129 return 0;
130 claim_base = PAGE_ALIGN(claim_base + size);
131 return addr;
132}
133
Olaf Hering7e658112005-10-28 17:46:50 -0700134void start(unsigned long a1, unsigned long a2, void *promptr, void *sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 unsigned long i;
Olaf Hering8a76baf2005-10-28 17:46:40 -0700137 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 kernel_entry_t kernel_entry;
139 Elf64_Ehdr *elf64;
140 Elf64_Phdr *elf64ph;
141
Olaf Hering9b0cbe92005-10-28 17:46:45 -0700142 memset(__bss_start, 0, _end - __bss_start);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 prom = (int (*)(void *)) promptr;
145 chosen_handle = finddevice("/chosen");
146 if (chosen_handle == (void *) -1)
147 exit();
148 if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4)
149 exit();
150 stderr = stdout;
151 if (getprop(chosen_handle, "stdin", &stdin, sizeof(stdin)) != 4)
152 exit();
153
Olaf Hering7e658112005-10-28 17:46:50 -0700154 printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r", _start, sp);
Mark Bellon3cc747e2005-09-06 15:50:02 -0700155
156 /*
157 * The first available claim_base must be above the end of the
158 * the loaded kernel wrapper file (_start to _end includes the
159 * initrd image if it is present) and rounded up to a nice
160 * 1 MB boundary for good measure.
161 */
162
163 claim_base = _ALIGN_UP((unsigned long)_end, ONE_MB);
164
165#if defined(PROG_START)
166 /*
167 * Maintain a "magic" minimum address. This keeps some older
168 * firmware platforms running.
169 */
170
171 if (claim_base < PROG_START)
172 claim_base = PROG_START;
173#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Olaf Hering8a76baf2005-10-28 17:46:40 -0700175 vmlinuz.addr = (unsigned long)_vmlinux_start;
176 vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
177
178 /* gunzip the ELF header of the kernel */
179 if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
180 len = vmlinuz.size;
181 gunzip(elfheader, sizeof(elfheader),
182 (unsigned char *)vmlinuz.addr, &len);
183 } else
184 memcpy(elfheader, (const void *)vmlinuz.addr, sizeof(elfheader));
185
186 elf64 = (Elf64_Ehdr *)elfheader;
187 if ( elf64->e_ident[EI_MAG0] != ELFMAG0 ||
188 elf64->e_ident[EI_MAG1] != ELFMAG1 ||
189 elf64->e_ident[EI_MAG2] != ELFMAG2 ||
190 elf64->e_ident[EI_MAG3] != ELFMAG3 ||
191 elf64->e_ident[EI_CLASS] != ELFCLASS64 ||
192 elf64->e_ident[EI_DATA] != ELFDATA2MSB ||
193 elf64->e_type != ET_EXEC ||
194 elf64->e_machine != EM_PPC64 )
195 {
196 printf("Error: not a valid PPC64 ELF file!\n\r");
197 exit();
198 }
199
200 elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
201 (unsigned long)elf64->e_phoff);
202 for(i=0; i < (unsigned int)elf64->e_phnum ;i++,elf64ph++) {
203 if (elf64ph->p_type == PT_LOAD && elf64ph->p_offset != 0)
204 break;
205 }
Benjamin Herrenschmidtc8e3c8b2005-11-07 00:57:58 -0800206 vmlinux.size = (unsigned long)elf64ph->p_filesz +
207 (unsigned long)elf64ph->p_offset;
208 /* We need to claim the memsize plus the file offset since gzip
209 * will expand the header (file offset), then the kernel, then
210 * possible rubbish we don't care about. But the kernel bss must
211 * be claimed (it will be zero'd by the kernel itself)
212 */
213 vmlinux.memsize = (unsigned long)elf64ph->p_memsz +
214 (unsigned long)elf64ph->p_offset;
Olaf Hering8a76baf2005-10-28 17:46:40 -0700215 printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux.memsize);
216 vmlinux.addr = try_claim(vmlinux.memsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (vmlinux.addr == 0) {
218 printf("Can't allocate memory for kernel image !\n\r");
219 exit();
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /*
223 * Now we try to claim memory for the initrd (and copy it there)
224 */
225 initrd.size = (unsigned long)(_initrd_end - _initrd_start);
226 initrd.memsize = initrd.size;
227 if ( initrd.size > 0 ) {
228 printf("Allocating 0x%lx bytes for initrd ...\n\r", initrd.size);
229 initrd.addr = try_claim(initrd.size);
230 if (initrd.addr == 0) {
231 printf("Can't allocate memory for initial ramdisk !\n\r");
232 exit();
233 }
234 a1 = initrd.addr;
235 a2 = initrd.size;
Olaf Heringb1bdfbd2005-06-28 21:01:46 +1000236 printf("initial ramdisk moving 0x%lx <- 0x%lx (0x%lx bytes)\n\r",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 initrd.addr, (unsigned long)_initrd_start, initrd.size);
238 memmove((void *)initrd.addr, (void *)_initrd_start, initrd.size);
Olaf Heringb1bdfbd2005-06-28 21:01:46 +1000239 printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
242 /* Eventually gunzip the kernel */
243 if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
245 vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size);
246 len = vmlinuz.size;
Olaf Hering8a76baf2005-10-28 17:46:40 -0700247 gunzip((void *)vmlinux.addr, vmlinux.memsize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 (unsigned char *)vmlinuz.addr, &len);
249 printf("done 0x%lx bytes\n\r", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 } else {
251 memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,vmlinuz.size);
252 }
253
254 /* Skip over the ELF header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#ifdef DEBUG
256 printf("... skipping 0x%lx bytes of ELF header\n\r",
257 (unsigned long)elf64ph->p_offset);
258#endif
259 vmlinux.addr += (unsigned long)elf64ph->p_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 flush_cache((void *)vmlinux.addr, vmlinux.size);
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 kernel_entry = (kernel_entry_t)vmlinux.addr;
264#ifdef DEBUG
265 printf( "kernel:\n\r"
266 " entry addr = 0x%lx\n\r"
267 " a1 = 0x%lx,\n\r"
268 " a2 = 0x%lx,\n\r"
269 " prom = 0x%lx,\n\r"
270 " bi_recs = 0x%lx,\n\r",
271 (unsigned long)kernel_entry, a1, a2,
272 (unsigned long)prom, NULL);
273#endif
274
275 kernel_entry( a1, a2, prom, NULL );
276
277 printf("Error: Linux kernel returned to zImage bootloader!\n\r");
278
279 exit();
280}
281