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.
12 #include "ppc32-types.h"
14 #include <linux/elf.h>
15 #include <linux/string.h>
16 #include <asm/processor.h>
18 #include <asm/bootinfo.h>
20 extern void *finddevice(const char *);
21 extern int getprop(void *, const char *, void *, int);
22 extern void printk(char *fmt, ...);
23 extern void printf(const char *fmt, ...);
24 extern int sprintf(char *buf, const char *fmt, ...);
25 void gunzip(void *, int, unsigned char *, int *);
26 void *claim(unsigned int, unsigned int, unsigned int);
27 void flush_cache(void *, unsigned long);
29 extern void exit(void);
31 unsigned long strlen(const char *s);
32 void *memmove(void *dest, const void *src, unsigned long n);
33 void *memcpy(void *dest, const void *src, unsigned long n);
35 /* Value picked to match that used by yaboot */
36 #define PROG_START 0x01400000
37 #define RAM_END (256<<20) // Fixme: use OF */
40 char *begin_avail, *end_avail;
42 unsigned int heap_use;
43 unsigned int heap_max;
46 extern char _vmlinux_start[];
47 extern char _vmlinux_end[];
48 extern char _initrd_start[];
49 extern char _initrd_end[];
50 extern unsigned long vmlinux_filesize;
51 extern unsigned long vmlinux_memsize;
56 unsigned long memsize;
58 struct addr_range vmlinux = {0, 0, 0};
59 struct addr_range vmlinuz = {0, 0, 0};
60 struct addr_range initrd = {0, 0, 0};
62 static char scratch[128<<10]; /* 128kB of scratch space for gunzip */
64 typedef void (*kernel_entry_t)( unsigned long,
79 static unsigned long claim_base = PROG_START;
81 static unsigned long try_claim(unsigned long size)
83 unsigned long addr = 0;
85 for(; claim_base < RAM_END; claim_base += 0x100000) {
87 printf(" trying: 0x%08lx\n\r", claim_base);
89 addr = (unsigned long)claim(claim_base, size, 0);
90 if ((void *)addr != (void *)-1)
95 claim_base = PAGE_ALIGN(claim_base + size);
99 void start(unsigned long a1, unsigned long a2, void *promptr)
102 kernel_entry_t kernel_entry;
106 prom = (int (*)(void *)) promptr;
107 chosen_handle = finddevice("/chosen");
108 if (chosen_handle == (void *) -1)
110 if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4)
113 if (getprop(chosen_handle, "stdin", &stdin, sizeof(stdin)) != 4)
116 printf("zImage starting: loaded at 0x%x\n\r", (unsigned)_start);
119 * Now we try to claim some memory for the kernel itself
120 * our "vmlinux_memsize" is the memory footprint in RAM, _HOWEVER_, what
121 * our Makefile stuffs in is an image containing all sort of junk including
122 * an ELF header. We need to do some calculations here to find the right
123 * size... In practice we add 1Mb, that is enough, but we should really
124 * consider fixing the Makefile to put a _raw_ kernel in there !
126 vmlinux_memsize += 0x100000;
127 printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux_memsize);
128 vmlinux.addr = try_claim(vmlinux_memsize);
129 if (vmlinux.addr == 0) {
130 printf("Can't allocate memory for kernel image !\n\r");
133 vmlinuz.addr = (unsigned long)_vmlinux_start;
134 vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
135 vmlinux.size = PAGE_ALIGN(vmlinux_filesize);
136 vmlinux.memsize = vmlinux_memsize;
139 * Now we try to claim memory for the initrd (and copy it there)
141 initrd.size = (unsigned long)(_initrd_end - _initrd_start);
142 initrd.memsize = initrd.size;
143 if ( initrd.size > 0 ) {
144 printf("Allocating 0x%lx bytes for initrd ...\n\r", initrd.size);
145 initrd.addr = try_claim(initrd.size);
146 if (initrd.addr == 0) {
147 printf("Can't allocate memory for initial ramdisk !\n\r");
152 printf("initial ramdisk moving 0x%lx <- 0x%lx (%lx bytes)\n\r",
153 initrd.addr, (unsigned long)_initrd_start, initrd.size);
154 memmove((void *)initrd.addr, (void *)_initrd_start, initrd.size);
155 printf("initrd head: 0x%lx\n", *((u32 *)initrd.addr));
158 /* Eventually gunzip the kernel */
159 if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
162 begin_avail = avail_high = avail_ram;
163 end_avail = scratch + sizeof(scratch);
164 printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
165 vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size);
167 gunzip((void *)vmlinux.addr, vmlinux.size,
168 (unsigned char *)vmlinuz.addr, &len);
169 printf("done 0x%lx bytes\n\r", len);
170 printf("0x%x bytes of heap consumed, max in use 0x%\n\r",
171 (unsigned)(avail_high - begin_avail), heap_max);
173 memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,vmlinuz.size);
176 /* Skip over the ELF header */
177 elf64 = (Elf64_Ehdr *)vmlinux.addr;
178 if ( elf64->e_ident[EI_MAG0] != ELFMAG0 ||
179 elf64->e_ident[EI_MAG1] != ELFMAG1 ||
180 elf64->e_ident[EI_MAG2] != ELFMAG2 ||
181 elf64->e_ident[EI_MAG3] != ELFMAG3 ||
182 elf64->e_ident[EI_CLASS] != ELFCLASS64 ||
183 elf64->e_ident[EI_DATA] != ELFDATA2MSB ||
184 elf64->e_type != ET_EXEC ||
185 elf64->e_machine != EM_PPC64 )
187 printf("Error: not a valid PPC64 ELF file!\n\r");
191 elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
192 (unsigned long)elf64->e_phoff);
193 for(i=0; i < (unsigned int)elf64->e_phnum ;i++,elf64ph++) {
194 if (elf64ph->p_type == PT_LOAD && elf64ph->p_offset != 0)
198 printf("... skipping 0x%lx bytes of ELF header\n\r",
199 (unsigned long)elf64ph->p_offset);
201 vmlinux.addr += (unsigned long)elf64ph->p_offset;
202 vmlinux.size -= (unsigned long)elf64ph->p_offset;
204 flush_cache((void *)vmlinux.addr, vmlinux.memsize);
207 printf("initrd head: 0x%lx\n\r", *((u32 *)initrd.addr));
209 kernel_entry = (kernel_entry_t)vmlinux.addr;
211 printf( "kernel:\n\r"
212 " entry addr = 0x%lx\n\r"
216 " bi_recs = 0x%lx,\n\r",
217 (unsigned long)kernel_entry, a1, a2,
218 (unsigned long)prom, NULL);
221 kernel_entry( a1, a2, prom, NULL );
223 printf("Error: Linux kernel returned to zImage bootloader!\n\r");
231 struct memchunk *next;
234 static struct memchunk *freechunks;
236 void *zalloc(void *x, unsigned items, unsigned size)
239 struct memchunk **mpp, *mp;
242 size = _ALIGN(size, sizeof(struct memchunk));
244 if (heap_use > heap_max)
246 for (mpp = &freechunks; (mp = *mpp) != 0; mpp = &mp->next) {
247 if (mp->size == size) {
254 if (avail_ram > avail_high)
255 avail_high = avail_ram;
256 if (avail_ram > end_avail) {
257 printf("oops... out of memory\n\r");
263 void zfree(void *x, void *addr, unsigned nb)
265 struct memchunk *mp = addr;
267 nb = _ALIGN(nb, sizeof(struct memchunk));
269 if (avail_ram == addr + nb) {
274 mp->next = freechunks;
279 #define EXTRA_FIELD 4
282 #define RESERVED 0xe0
286 void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
294 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
295 printf("bad gzipped data\n\r");
298 if ((flags & EXTRA_FIELD) != 0)
299 i = 12 + src[10] + (src[11] << 8);
300 if ((flags & ORIG_NAME) != 0)
301 while (src[i++] != 0)
303 if ((flags & COMMENT) != 0)
304 while (src[i++] != 0)
306 if ((flags & HEAD_CRC) != 0)
309 printf("gunzip: ran out of data in header\n\r");
315 r = inflateInit2(&s, -MAX_WBITS);
317 printf("inflateInit2 returned %d\n\r", r);
321 s.avail_in = *lenp - i;
323 s.avail_out = dstlen;
324 r = inflate(&s, Z_FINISH);
325 if (r != Z_OK && r != Z_STREAM_END) {
326 printf("inflate returned %d msg: %s\n\r", r, s.msg);
329 *lenp = s.next_out - (unsigned char *) dst;