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 static void icedcc_putc(int ch)
35 int status, i = 0x4000000;
41 asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
44 asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
47 #define putc(ch) icedcc_putc(ch)
48 #define flush() do { } while (0)
51 static void putstr(const char *ptr)
55 while ((c = *ptr++) != '\0') {
66 #define __ptr_t void *
69 * Optimised C version of memzero for the ARM.
71 void __memzero (__ptr_t s, size_t n)
73 union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
78 for (i = n >> 5; i > 0; i--) {
113 static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
117 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
119 for (i = __n >> 3; i > 0; i--) {
151 #define OF(args) args
152 #define STATIC static
154 typedef unsigned char uch;
155 typedef unsigned short ush;
156 typedef unsigned long ulg;
158 #define WSIZE 0x8000 /* Window size must be at least 32k, */
159 /* and a power of two */
161 static uch *inbuf; /* input buffer */
162 static uch window[WSIZE]; /* Sliding window buffer */
164 static unsigned insize; /* valid bytes in inbuf */
165 static unsigned inptr; /* index of next byte to be processed in inbuf */
166 static unsigned outcnt; /* bytes in output buffer */
169 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
170 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
171 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
172 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
173 #define COMMENT 0x10 /* bit 4 set: file comment present */
174 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
175 #define RESERVED 0xC0 /* bit 6,7: reserved */
177 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
179 /* Diagnostic functions */
181 # define Assert(cond,msg) {if(!(cond)) error(msg);}
182 # define Trace(x) fprintf x
183 # define Tracev(x) {if (verbose) fprintf x ;}
184 # define Tracevv(x) {if (verbose>1) fprintf x ;}
185 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
186 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
188 # define Assert(cond,msg)
193 # define Tracecv(c,x)
196 static int fill_inbuf(void);
197 static void flush_window(void);
198 static void error(char *m);
199 static void gzip_mark(void **);
200 static void gzip_release(void **);
202 extern char input_data[];
203 extern char input_data_end[];
205 static uch *output_data;
206 static ulg output_ptr;
207 static ulg bytes_out;
209 static void *malloc(int size);
210 static void free(void *where);
211 static void error(char *m);
212 static void gzip_mark(void **);
213 static void gzip_release(void **);
215 static void putstr(const char *);
218 static ulg free_mem_ptr;
219 static ulg free_mem_ptr_end;
221 #define HEAP_SIZE 0x2000
223 #include "../../../../lib/inflate.c"
225 #ifndef STANDALONE_DEBUG
226 static void *malloc(int size)
230 if (size <0) error("Malloc error");
231 if (free_mem_ptr <= 0) error("Memory error");
233 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
235 p = (void *)free_mem_ptr;
236 free_mem_ptr += size;
238 if (free_mem_ptr >= free_mem_ptr_end)
239 error("Out of memory");
243 static void free(void *where)
244 { /* gzip_mark & gzip_release do the free */
247 static void gzip_mark(void **ptr)
250 *ptr = (void *) free_mem_ptr;
253 static void gzip_release(void **ptr)
256 free_mem_ptr = (long) *ptr;
259 static void gzip_mark(void **ptr)
263 static void gzip_release(void **ptr)
268 /* ===========================================================================
269 * Fill the input buffer. This is called only when the buffer is empty
270 * and at least one byte is really needed.
275 error("ran out of input data");
278 insize = &input_data_end[0] - &input_data[0];
284 /* ===========================================================================
285 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
286 * (Used for the decompressed data only.)
288 void flush_window(void)
295 out = &output_data[output_ptr];
296 for (n = 0; n < outcnt; n++) {
298 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
301 bytes_out += (ulg)outcnt;
302 output_ptr += (ulg)outcnt;
308 #define arch_error(x)
311 static void error(char *x)
317 putstr("\n\n -- System halted");
322 #ifndef STANDALONE_DEBUG
325 decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
328 output_data = (uch *)output_start; /* Points to kernel start */
329 free_mem_ptr = free_mem_ptr_p;
330 free_mem_ptr_end = free_mem_ptr_end_p;
331 __machine_arch_type = arch_id;
336 putstr("Uncompressing Linux...");
338 putstr(" done, booting the kernel.\n");
343 char output_buffer[1500*1024];
347 output_data = output_buffer;
350 putstr("Uncompressing Linux...");