Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / arm / boot / compressed / misc.c
blob9b444022cb9bc76933f1fdba6dffb294723fd7ac
1 /*
2 * misc.c
3 *
4 * This is a collection of several routines from gzip-1.0.3
5 * adapted for Linux.
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
24 #define putstr printf
25 #else
27 static void putstr(const char *ptr);
29 #include <linux/compiler.h>
30 #include <asm/arch/uncompress.h>
32 #ifdef CONFIG_DEBUG_ICEDCC
34 #ifdef CONFIG_CPU_V6
36 static void icedcc_putc(int ch)
38 int status, i = 0x4000000;
40 do {
41 if (--i < 0)
42 return;
44 asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
45 } while (status & (1 << 29));
47 asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
50 #else
52 static void icedcc_putc(int ch)
54 int status, i = 0x4000000;
56 do {
57 if (--i < 0)
58 return;
60 asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
61 } while (status & 2);
63 asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
66 #endif
68 #define putc(ch) icedcc_putc(ch)
69 #define flush() do { } while (0)
70 #endif
72 static void putstr(const char *ptr)
74 char c;
76 while ((c = *ptr++) != '\0') {
77 if (c == '\n')
78 putc('\r');
79 putc(c);
82 flush();
85 #endif
87 #define __ptr_t void *
90 * Optimised C version of memzero for the ARM.
92 void __memzero (__ptr_t s, size_t n)
94 union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
95 int i;
97 u.vp = s;
99 for (i = n >> 5; i > 0; i--) {
100 *u.ulp++ = 0;
101 *u.ulp++ = 0;
102 *u.ulp++ = 0;
103 *u.ulp++ = 0;
104 *u.ulp++ = 0;
105 *u.ulp++ = 0;
106 *u.ulp++ = 0;
107 *u.ulp++ = 0;
110 if (n & 1 << 4) {
111 *u.ulp++ = 0;
112 *u.ulp++ = 0;
113 *u.ulp++ = 0;
114 *u.ulp++ = 0;
117 if (n & 1 << 3) {
118 *u.ulp++ = 0;
119 *u.ulp++ = 0;
122 if (n & 1 << 2)
123 *u.ulp++ = 0;
125 if (n & 1 << 1) {
126 *u.ucp++ = 0;
127 *u.ucp++ = 0;
130 if (n & 1)
131 *u.ucp++ = 0;
134 static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
135 size_t __n)
137 int i = 0;
138 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
140 for (i = __n >> 3; i > 0; i--) {
141 *d++ = *s++;
142 *d++ = *s++;
143 *d++ = *s++;
144 *d++ = *s++;
145 *d++ = *s++;
146 *d++ = *s++;
147 *d++ = *s++;
148 *d++ = *s++;
151 if (__n & 1 << 2) {
152 *d++ = *s++;
153 *d++ = *s++;
154 *d++ = *s++;
155 *d++ = *s++;
158 if (__n & 1 << 1) {
159 *d++ = *s++;
160 *d++ = *s++;
163 if (__n & 1)
164 *d++ = *s++;
166 return __dest;
170 * gzip delarations
172 #define OF(args) args
173 #define STATIC static
175 typedef unsigned char uch;
176 typedef unsigned short ush;
177 typedef unsigned long ulg;
179 #define WSIZE 0x8000 /* Window size must be at least 32k, */
180 /* and a power of two */
182 static uch *inbuf; /* input buffer */
183 static uch window[WSIZE]; /* Sliding window buffer */
185 static unsigned insize; /* valid bytes in inbuf */
186 static unsigned inptr; /* index of next byte to be processed in inbuf */
187 static unsigned outcnt; /* bytes in output buffer */
189 /* gzip flag byte */
190 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
191 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
192 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
193 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
194 #define COMMENT 0x10 /* bit 4 set: file comment present */
195 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
196 #define RESERVED 0xC0 /* bit 6,7: reserved */
198 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
200 /* Diagnostic functions */
201 #ifdef DEBUG
202 # define Assert(cond,msg) {if(!(cond)) error(msg);}
203 # define Trace(x) fprintf x
204 # define Tracev(x) {if (verbose) fprintf x ;}
205 # define Tracevv(x) {if (verbose>1) fprintf x ;}
206 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
207 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
208 #else
209 # define Assert(cond,msg)
210 # define Trace(x)
211 # define Tracev(x)
212 # define Tracevv(x)
213 # define Tracec(c,x)
214 # define Tracecv(c,x)
215 #endif
217 static int fill_inbuf(void);
218 static void flush_window(void);
219 static void error(char *m);
220 static void gzip_mark(void **);
221 static void gzip_release(void **);
223 extern char input_data[];
224 extern char input_data_end[];
226 static uch *output_data;
227 static ulg output_ptr;
228 static ulg bytes_out;
230 static void *malloc(int size);
231 static void free(void *where);
232 static void error(char *m);
233 static void gzip_mark(void **);
234 static void gzip_release(void **);
236 static void putstr(const char *);
238 extern int end;
239 static ulg free_mem_ptr;
240 static ulg free_mem_ptr_end;
242 #define HEAP_SIZE 0x3000
244 #include "../../../../lib/inflate.c"
246 #ifndef STANDALONE_DEBUG
247 static void *malloc(int size)
249 void *p;
251 if (size <0) error("Malloc error");
252 if (free_mem_ptr <= 0) error("Memory error");
254 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
256 p = (void *)free_mem_ptr;
257 free_mem_ptr += size;
259 if (free_mem_ptr >= free_mem_ptr_end)
260 error("Out of memory");
261 return p;
264 static void free(void *where)
265 { /* gzip_mark & gzip_release do the free */
268 static void gzip_mark(void **ptr)
270 arch_decomp_wdog();
271 *ptr = (void *) free_mem_ptr;
274 static void gzip_release(void **ptr)
276 arch_decomp_wdog();
277 free_mem_ptr = (long) *ptr;
279 #else
280 static void gzip_mark(void **ptr)
284 static void gzip_release(void **ptr)
287 #endif
289 /* ===========================================================================
290 * Fill the input buffer. This is called only when the buffer is empty
291 * and at least one byte is really needed.
293 int fill_inbuf(void)
295 if (insize != 0)
296 error("ran out of input data");
298 inbuf = input_data;
299 insize = &input_data_end[0] - &input_data[0];
301 inptr = 1;
302 return inbuf[0];
305 /* ===========================================================================
306 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
307 * (Used for the decompressed data only.)
309 void flush_window(void)
311 ulg c = crc;
312 unsigned n;
313 uch *in, *out, ch;
315 in = window;
316 out = &output_data[output_ptr];
317 for (n = 0; n < outcnt; n++) {
318 ch = *out++ = *in++;
319 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
321 crc = c;
322 bytes_out += (ulg)outcnt;
323 output_ptr += (ulg)outcnt;
324 outcnt = 0;
325 putstr(".");
328 #ifndef arch_error
329 #define arch_error(x)
330 #endif
332 static void error(char *x)
334 arch_error(x);
336 putstr("\n\n");
337 putstr(x);
338 putstr("\n\n -- System halted");
340 while(1); /* Halt */
343 #ifndef STANDALONE_DEBUG
346 decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
347 int arch_id)
349 output_data = (uch *)output_start; /* Points to kernel start */
350 free_mem_ptr = free_mem_ptr_p;
351 free_mem_ptr_end = free_mem_ptr_end_p;
352 __machine_arch_type = arch_id;
354 arch_decomp_setup();
356 makecrc();
357 putstr("Uncompressing Linux...");
358 gunzip();
359 putstr(" done, booting the kernel.\n");
360 return output_ptr;
362 #else
364 char output_buffer[1500*1024];
366 int main()
368 output_data = output_buffer;
370 makecrc();
371 putstr("Uncompressing Linux...");
372 gunzip();
373 putstr("done.\n");
374 return 0;
376 #endif