4 * This is a collection of several routines from gzip-1.0.3
7 * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
9 * Modified for ARM Linux by Russell King
11 * Nicolas Pitre <nico@visuaide.com> 1999/04/14 :
12 * For this code to run directly from Flash, all constant variables must
13 * be marked with 'const' and all other variables initialized at run-time
14 * only. This way all non constant variables will end up in the bss segment,
15 * which should point to addresses in RAM and cleared to 0 on start.
16 * This allows for a much quicker boot time.
19 unsigned int __machine_arch_type;
21 #include <linux/string.h>
23 #include <asm/arch/uncompress.h>
25 #ifdef STANDALONE_DEBUG
29 #ifdef CONFIG_DEBUG_ICEDCC
30 #define putstr icedcc_putstr
31 #define putc icedcc_putc
33 extern void icedcc_putc(int ch);
36 icedcc_putstr(const char *ptr)
38 for (; *ptr != '\0'; ptr++) {
45 #define __ptr_t void *
48 * Optimised C version of memzero for the ARM.
50 void __memzero (__ptr_t s, size_t n)
52 union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
57 for (i = n >> 5; i > 0; i--) {
92 static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
96 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
98 for (i = __n >> 3; i > 0; i--) {
130 #define OF(args) args
131 #define STATIC static
133 typedef unsigned char uch;
134 typedef unsigned short ush;
135 typedef unsigned long ulg;
137 #define WSIZE 0x8000 /* Window size must be at least 32k, */
138 /* and a power of two */
140 static uch *inbuf; /* input buffer */
141 static uch window[WSIZE]; /* Sliding window buffer */
143 static unsigned insize; /* valid bytes in inbuf */
144 static unsigned inptr; /* index of next byte to be processed in inbuf */
145 static unsigned outcnt; /* bytes in output buffer */
148 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
149 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
150 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
151 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
152 #define COMMENT 0x10 /* bit 4 set: file comment present */
153 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
154 #define RESERVED 0xC0 /* bit 6,7: reserved */
156 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
158 /* Diagnostic functions */
160 # define Assert(cond,msg) {if(!(cond)) error(msg);}
161 # define Trace(x) fprintf x
162 # define Tracev(x) {if (verbose) fprintf x ;}
163 # define Tracevv(x) {if (verbose>1) fprintf x ;}
164 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
165 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
167 # define Assert(cond,msg)
172 # define Tracecv(c,x)
175 static int fill_inbuf(void);
176 static void flush_window(void);
177 static void error(char *m);
178 static void gzip_mark(void **);
179 static void gzip_release(void **);
181 extern char input_data[];
182 extern char input_data_end[];
184 static uch *output_data;
185 static ulg output_ptr;
186 static ulg bytes_out;
188 static void *malloc(int size);
189 static void free(void *where);
190 static void error(char *m);
191 static void gzip_mark(void **);
192 static void gzip_release(void **);
194 static void putstr(const char *);
197 static ulg free_mem_ptr;
198 static ulg free_mem_ptr_end;
200 #define HEAP_SIZE 0x2000
202 #include "../../../../lib/inflate.c"
204 #ifndef STANDALONE_DEBUG
205 static void *malloc(int size)
209 if (size <0) error("Malloc error");
210 if (free_mem_ptr <= 0) error("Memory error");
212 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
214 p = (void *)free_mem_ptr;
215 free_mem_ptr += size;
217 if (free_mem_ptr >= free_mem_ptr_end)
218 error("Out of memory");
222 static void free(void *where)
223 { /* gzip_mark & gzip_release do the free */
226 static void gzip_mark(void **ptr)
229 *ptr = (void *) free_mem_ptr;
232 static void gzip_release(void **ptr)
235 free_mem_ptr = (long) *ptr;
238 static void gzip_mark(void **ptr)
242 static void gzip_release(void **ptr)
247 /* ===========================================================================
248 * Fill the input buffer. This is called only when the buffer is empty
249 * and at least one byte is really needed.
254 error("ran out of input data");
257 insize = &input_data_end[0] - &input_data[0];
263 /* ===========================================================================
264 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
265 * (Used for the decompressed data only.)
267 void flush_window(void)
274 out = &output_data[output_ptr];
275 for (n = 0; n < outcnt; n++) {
277 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
280 bytes_out += (ulg)outcnt;
281 output_ptr += (ulg)outcnt;
287 #define arch_error(x)
290 static void error(char *x)
296 putstr("\n\n -- System halted");
301 #ifndef STANDALONE_DEBUG
304 decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
307 output_data = (uch *)output_start; /* Points to kernel start */
308 free_mem_ptr = free_mem_ptr_p;
309 free_mem_ptr_end = free_mem_ptr_end_p;
310 __machine_arch_type = arch_id;
315 putstr("Uncompressing Linux...");
317 putstr(" done, booting the kernel.\n");
322 char output_buffer[1500*1024];
326 output_data = output_buffer;
329 putstr("Uncompressing Linux...");