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 #ifdef STANDALONE_DEBUG
27 static void putstr(const char *ptr);
29 #include <linux/compiler.h>
30 #include <asm/arch/uncompress.h>
32 #ifdef CONFIG_DEBUG_ICEDCC
33 extern void icedcc_putc(int ch);
34 #define putc(ch) icedcc_putc(ch)
35 #define flush() do { } while (0)
38 static void putstr(const char *ptr)
42 while ((c = *ptr++) != '\0') {
53 #define __ptr_t void *
56 * Optimised C version of memzero for the ARM.
58 void __memzero (__ptr_t s, size_t n)
60 union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
65 for (i = n >> 5; i > 0; i--) {
100 static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
104 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
106 for (i = __n >> 3; i > 0; i--) {
138 #define OF(args) args
139 #define STATIC static
141 typedef unsigned char uch;
142 typedef unsigned short ush;
143 typedef unsigned long ulg;
145 #define WSIZE 0x8000 /* Window size must be at least 32k, */
146 /* and a power of two */
148 static uch *inbuf; /* input buffer */
149 static uch window[WSIZE]; /* Sliding window buffer */
151 static unsigned insize; /* valid bytes in inbuf */
152 static unsigned inptr; /* index of next byte to be processed in inbuf */
153 static unsigned outcnt; /* bytes in output buffer */
156 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
157 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
158 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
159 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
160 #define COMMENT 0x10 /* bit 4 set: file comment present */
161 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
162 #define RESERVED 0xC0 /* bit 6,7: reserved */
164 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
166 /* Diagnostic functions */
168 # define Assert(cond,msg) {if(!(cond)) error(msg);}
169 # define Trace(x) fprintf x
170 # define Tracev(x) {if (verbose) fprintf x ;}
171 # define Tracevv(x) {if (verbose>1) fprintf x ;}
172 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
173 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
175 # define Assert(cond,msg)
180 # define Tracecv(c,x)
183 static int fill_inbuf(void);
184 static void flush_window(void);
185 static void error(char *m);
186 static void gzip_mark(void **);
187 static void gzip_release(void **);
189 extern char input_data[];
190 extern char input_data_end[];
192 static uch *output_data;
193 static ulg output_ptr;
194 static ulg bytes_out;
196 static void *malloc(int size);
197 static void free(void *where);
198 static void error(char *m);
199 static void gzip_mark(void **);
200 static void gzip_release(void **);
202 static void putstr(const char *);
205 static ulg free_mem_ptr;
206 static ulg free_mem_ptr_end;
208 #define HEAP_SIZE 0x2000
210 #include "../../../../lib/inflate.c"
212 #ifndef STANDALONE_DEBUG
213 static void *malloc(int size)
217 if (size <0) error("Malloc error");
218 if (free_mem_ptr <= 0) error("Memory error");
220 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
222 p = (void *)free_mem_ptr;
223 free_mem_ptr += size;
225 if (free_mem_ptr >= free_mem_ptr_end)
226 error("Out of memory");
230 static void free(void *where)
231 { /* gzip_mark & gzip_release do the free */
234 static void gzip_mark(void **ptr)
237 *ptr = (void *) free_mem_ptr;
240 static void gzip_release(void **ptr)
243 free_mem_ptr = (long) *ptr;
246 static void gzip_mark(void **ptr)
250 static void gzip_release(void **ptr)
255 /* ===========================================================================
256 * Fill the input buffer. This is called only when the buffer is empty
257 * and at least one byte is really needed.
262 error("ran out of input data");
265 insize = &input_data_end[0] - &input_data[0];
271 /* ===========================================================================
272 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
273 * (Used for the decompressed data only.)
275 void flush_window(void)
282 out = &output_data[output_ptr];
283 for (n = 0; n < outcnt; n++) {
285 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
288 bytes_out += (ulg)outcnt;
289 output_ptr += (ulg)outcnt;
295 #define arch_error(x)
298 static void error(char *x)
304 putstr("\n\n -- System halted");
309 #ifndef STANDALONE_DEBUG
312 decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
315 output_data = (uch *)output_start; /* Points to kernel start */
316 free_mem_ptr = free_mem_ptr_p;
317 free_mem_ptr_end = free_mem_ptr_end_p;
318 __machine_arch_type = arch_id;
323 putstr("Uncompressing Linux...");
325 putstr(" done, booting the kernel.\n");
330 char output_buffer[1500*1024];
334 output_data = output_buffer;
337 putstr("Uncompressing Linux...");