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 (512<<20) // Fixme: use OF */
27 #define ONE_MB 0x100000
29 static char *avail_ram
;
30 static char *begin_avail
, *end_avail
;
31 static char *avail_high
;
32 static unsigned int heap_use
;
33 static unsigned int heap_max
;
37 extern char _vmlinux_start
[];
38 extern char _vmlinux_end
[];
39 extern char _initrd_start
[];
40 extern char _initrd_end
[];
41 extern unsigned long vmlinux_filesize
;
42 extern unsigned long vmlinux_memsize
;
47 unsigned long memsize
;
49 static struct addr_range vmlinux
= {0, 0, 0};
50 static struct addr_range vmlinuz
= {0, 0, 0};
51 static struct addr_range initrd
= {0, 0, 0};
53 static char scratch
[128<<10]; /* 128kB of scratch space for gunzip */
55 typedef void (*kernel_entry_t
)( unsigned long,
63 static unsigned long claim_base
;
65 static unsigned long try_claim(unsigned long size
)
67 unsigned long addr
= 0;
69 for(; claim_base
< RAM_END
; claim_base
+= ONE_MB
) {
71 printf(" trying: 0x%08lx\n\r", claim_base
);
73 addr
= (unsigned long)claim(claim_base
, size
, 0);
74 if ((void *)addr
!= (void *)-1)
79 claim_base
= PAGE_ALIGN(claim_base
+ size
);
83 void start(unsigned long a1
, unsigned long a2
, void *promptr
)
86 kernel_entry_t kernel_entry
;
90 prom
= (int (*)(void *)) promptr
;
91 chosen_handle
= finddevice("/chosen");
92 if (chosen_handle
== (void *) -1)
94 if (getprop(chosen_handle
, "stdout", &stdout
, sizeof(stdout
)) != 4)
97 if (getprop(chosen_handle
, "stdin", &stdin
, sizeof(stdin
)) != 4)
100 printf("\n\rzImage starting: loaded at 0x%lx\n\r", (unsigned long) _start
);
103 * The first available claim_base must be above the end of the
104 * the loaded kernel wrapper file (_start to _end includes the
105 * initrd image if it is present) and rounded up to a nice
106 * 1 MB boundary for good measure.
109 claim_base
= _ALIGN_UP((unsigned long)_end
, ONE_MB
);
111 #if defined(PROG_START)
113 * Maintain a "magic" minimum address. This keeps some older
114 * firmware platforms running.
117 if (claim_base
< PROG_START
)
118 claim_base
= PROG_START
;
122 * Now we try to claim some memory for the kernel itself
123 * our "vmlinux_memsize" is the memory footprint in RAM, _HOWEVER_, what
124 * our Makefile stuffs in is an image containing all sort of junk including
125 * an ELF header. We need to do some calculations here to find the right
126 * size... In practice we add 1Mb, that is enough, but we should really
127 * consider fixing the Makefile to put a _raw_ kernel in there !
129 vmlinux_memsize
+= ONE_MB
;
130 printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux_memsize
);
131 vmlinux
.addr
= try_claim(vmlinux_memsize
);
132 if (vmlinux
.addr
== 0) {
133 printf("Can't allocate memory for kernel image !\n\r");
136 vmlinuz
.addr
= (unsigned long)_vmlinux_start
;
137 vmlinuz
.size
= (unsigned long)(_vmlinux_end
- _vmlinux_start
);
138 vmlinux
.size
= PAGE_ALIGN(vmlinux_filesize
);
139 vmlinux
.memsize
= vmlinux_memsize
;
142 * Now we try to claim memory for the initrd (and copy it there)
144 initrd
.size
= (unsigned long)(_initrd_end
- _initrd_start
);
145 initrd
.memsize
= initrd
.size
;
146 if ( initrd
.size
> 0 ) {
147 printf("Allocating 0x%lx bytes for initrd ...\n\r", initrd
.size
);
148 initrd
.addr
= try_claim(initrd
.size
);
149 if (initrd
.addr
== 0) {
150 printf("Can't allocate memory for initial ramdisk !\n\r");
155 printf("initial ramdisk moving 0x%lx <- 0x%lx (0x%lx bytes)\n\r",
156 initrd
.addr
, (unsigned long)_initrd_start
, initrd
.size
);
157 memmove((void *)initrd
.addr
, (void *)_initrd_start
, initrd
.size
);
158 printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd
.addr
));
161 /* Eventually gunzip the kernel */
162 if (*(unsigned short *)vmlinuz
.addr
== 0x1f8b) {
165 begin_avail
= avail_high
= avail_ram
;
166 end_avail
= scratch
+ sizeof(scratch
);
167 printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
168 vmlinux
.addr
, vmlinuz
.addr
, vmlinuz
.addr
+vmlinuz
.size
);
170 gunzip((void *)vmlinux
.addr
, vmlinux
.size
,
171 (unsigned char *)vmlinuz
.addr
, &len
);
172 printf("done 0x%lx bytes\n\r", len
);
173 printf("0x%x bytes of heap consumed, max in use 0x%x\n\r",
174 (unsigned)(avail_high
- begin_avail
), heap_max
);
176 memmove((void *)vmlinux
.addr
,(void *)vmlinuz
.addr
,vmlinuz
.size
);
179 /* Skip over the ELF header */
180 elf64
= (Elf64_Ehdr
*)vmlinux
.addr
;
181 if ( elf64
->e_ident
[EI_MAG0
] != ELFMAG0
||
182 elf64
->e_ident
[EI_MAG1
] != ELFMAG1
||
183 elf64
->e_ident
[EI_MAG2
] != ELFMAG2
||
184 elf64
->e_ident
[EI_MAG3
] != ELFMAG3
||
185 elf64
->e_ident
[EI_CLASS
] != ELFCLASS64
||
186 elf64
->e_ident
[EI_DATA
] != ELFDATA2MSB
||
187 elf64
->e_type
!= ET_EXEC
||
188 elf64
->e_machine
!= EM_PPC64
)
190 printf("Error: not a valid PPC64 ELF file!\n\r");
194 elf64ph
= (Elf64_Phdr
*)((unsigned long)elf64
+
195 (unsigned long)elf64
->e_phoff
);
196 for(i
=0; i
< (unsigned int)elf64
->e_phnum
;i
++,elf64ph
++) {
197 if (elf64ph
->p_type
== PT_LOAD
&& elf64ph
->p_offset
!= 0)
201 printf("... skipping 0x%lx bytes of ELF header\n\r",
202 (unsigned long)elf64ph
->p_offset
);
204 vmlinux
.addr
+= (unsigned long)elf64ph
->p_offset
;
205 vmlinux
.size
-= (unsigned long)elf64ph
->p_offset
;
207 flush_cache((void *)vmlinux
.addr
, vmlinux
.size
);
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 static 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
;