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.
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 (256<<20) // Fixme: use OF */
28 static char *avail_ram;
29 static char *begin_avail, *end_avail;
30 static char *avail_high;
31 static unsigned int heap_use;
32 static unsigned int heap_max;
35 extern char _vmlinux_start[];
36 extern char _vmlinux_end[];
37 extern char _initrd_start[];
38 extern char _initrd_end[];
39 extern unsigned long vmlinux_filesize;
40 extern unsigned long vmlinux_memsize;
45 unsigned long memsize;
47 static struct addr_range vmlinux = {0, 0, 0};
48 static struct addr_range vmlinuz = {0, 0, 0};
49 static struct addr_range initrd = {0, 0, 0};
51 static char scratch[128<<10]; /* 128kB of scratch space for gunzip */
53 typedef void (*kernel_entry_t)( unsigned long,
61 static unsigned long claim_base = PROG_START;
63 static unsigned long try_claim(unsigned long size)
65 unsigned long addr = 0;
67 for(; claim_base < RAM_END; claim_base += 0x100000) {
69 printf(" trying: 0x%08lx\n\r", claim_base);
71 addr = (unsigned long)claim(claim_base, size, 0);
72 if ((void *)addr != (void *)-1)
77 claim_base = PAGE_ALIGN(claim_base + size);
81 void start(unsigned long a1, unsigned long a2, void *promptr)
84 kernel_entry_t kernel_entry;
88 prom = (int (*)(void *)) promptr;
89 chosen_handle = finddevice("/chosen");
90 if (chosen_handle == (void *) -1)
92 if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4)
95 if (getprop(chosen_handle, "stdin", &stdin, sizeof(stdin)) != 4)
98 printf("\n\rzImage starting: loaded at 0x%x\n\r", (unsigned)_start);
101 * Now we try to claim some memory for the kernel itself
102 * our "vmlinux_memsize" is the memory footprint in RAM, _HOWEVER_, what
103 * our Makefile stuffs in is an image containing all sort of junk including
104 * an ELF header. We need to do some calculations here to find the right
105 * size... In practice we add 1Mb, that is enough, but we should really
106 * consider fixing the Makefile to put a _raw_ kernel in there !
108 vmlinux_memsize += 0x100000;
109 printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux_memsize);
110 vmlinux.addr = try_claim(vmlinux_memsize);
111 if (vmlinux.addr == 0) {
112 printf("Can't allocate memory for kernel image !\n\r");
115 vmlinuz.addr = (unsigned long)_vmlinux_start;
116 vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
117 vmlinux.size = PAGE_ALIGN(vmlinux_filesize);
118 vmlinux.memsize = vmlinux_memsize;
121 * Now we try to claim memory for the initrd (and copy it there)
123 initrd.size = (unsigned long)(_initrd_end - _initrd_start);
124 initrd.memsize = initrd.size;
125 if ( initrd.size > 0 ) {
126 printf("Allocating 0x%lx bytes for initrd ...\n\r", initrd.size);
127 initrd.addr = try_claim(initrd.size);
128 if (initrd.addr == 0) {
129 printf("Can't allocate memory for initial ramdisk !\n\r");
134 printf("initial ramdisk moving 0x%lx <- 0x%lx (0x%lx bytes)\n\r",
135 initrd.addr, (unsigned long)_initrd_start, initrd.size);
136 memmove((void *)initrd.addr, (void *)_initrd_start, initrd.size);
137 printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd.addr));
140 /* Eventually gunzip the kernel */
141 if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
144 begin_avail = avail_high = avail_ram;
145 end_avail = scratch + sizeof(scratch);
146 printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
147 vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size);
149 gunzip((void *)vmlinux.addr, vmlinux.size,
150 (unsigned char *)vmlinuz.addr, &len);
151 printf("done 0x%lx bytes\n\r", len);
152 printf("0x%x bytes of heap consumed, max in use 0x%x\n\r",
153 (unsigned)(avail_high - begin_avail), heap_max);
155 memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,vmlinuz.size);
158 /* Skip over the ELF header */
159 elf64 = (Elf64_Ehdr *)vmlinux.addr;
160 if ( elf64->e_ident[EI_MAG0] != ELFMAG0 ||
161 elf64->e_ident[EI_MAG1] != ELFMAG1 ||
162 elf64->e_ident[EI_MAG2] != ELFMAG2 ||
163 elf64->e_ident[EI_MAG3] != ELFMAG3 ||
164 elf64->e_ident[EI_CLASS] != ELFCLASS64 ||
165 elf64->e_ident[EI_DATA] != ELFDATA2MSB ||
166 elf64->e_type != ET_EXEC ||
167 elf64->e_machine != EM_PPC64 )
169 printf("Error: not a valid PPC64 ELF file!\n\r");
173 elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
174 (unsigned long)elf64->e_phoff);
175 for(i=0; i < (unsigned int)elf64->e_phnum ;i++,elf64ph++) {
176 if (elf64ph->p_type == PT_LOAD && elf64ph->p_offset != 0)
180 printf("... skipping 0x%lx bytes of ELF header\n\r",
181 (unsigned long)elf64ph->p_offset);
183 vmlinux.addr += (unsigned long)elf64ph->p_offset;
184 vmlinux.size -= (unsigned long)elf64ph->p_offset;
186 flush_cache((void *)vmlinux.addr, vmlinux.size);
188 kernel_entry = (kernel_entry_t)vmlinux.addr;
190 printf( "kernel:\n\r"
191 " entry addr = 0x%lx\n\r"
195 " bi_recs = 0x%lx,\n\r",
196 (unsigned long)kernel_entry, a1, a2,
197 (unsigned long)prom, NULL);
200 kernel_entry( a1, a2, prom, NULL );
202 printf("Error: Linux kernel returned to zImage bootloader!\n\r");
210 struct memchunk *next;
213 static struct memchunk *freechunks;
215 void *zalloc(void *x, unsigned items, unsigned size)
218 struct memchunk **mpp, *mp;
221 size = _ALIGN(size, sizeof(struct memchunk));
223 if (heap_use > heap_max)
225 for (mpp = &freechunks; (mp = *mpp) != 0; mpp = &mp->next) {
226 if (mp->size == size) {
233 if (avail_ram > avail_high)
234 avail_high = avail_ram;
235 if (avail_ram > end_avail) {
236 printf("oops... out of memory\n\r");
242 void zfree(void *x, void *addr, unsigned nb)
244 struct memchunk *mp = addr;
246 nb = _ALIGN(nb, sizeof(struct memchunk));
248 if (avail_ram == addr + nb) {
253 mp->next = freechunks;
258 #define EXTRA_FIELD 4
261 #define RESERVED 0xe0
265 static void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
273 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
274 printf("bad gzipped data\n\r");
277 if ((flags & EXTRA_FIELD) != 0)
278 i = 12 + src[10] + (src[11] << 8);
279 if ((flags & ORIG_NAME) != 0)
280 while (src[i++] != 0)
282 if ((flags & COMMENT) != 0)
283 while (src[i++] != 0)
285 if ((flags & HEAD_CRC) != 0)
288 printf("gunzip: ran out of data in header\n\r");
294 r = inflateInit2(&s, -MAX_WBITS);
296 printf("inflateInit2 returned %d\n\r", r);
300 s.avail_in = *lenp - i;
302 s.avail_out = dstlen;
303 r = inflate(&s, Z_FINISH);
304 if (r != Z_OK && r != Z_STREAM_END) {
305 printf("inflate returned %d msg: %s\n\r", r, s.msg);
308 *lenp = s.next_out - (unsigned char *) dst;