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