MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / arm / boot / compressed / misc.c
blob5fed2b04d6f40d29693770e4fdcecdaefdbd3421
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 #if 0 // mask by Victor Yu. 06-08-2007
28 static void putstr(const char *ptr);
29 #endif
31 #include <linux/compiler.h>
32 #include <asm/arch/uncompress.h>
34 #ifdef CONFIG_DEBUG_ICEDCC
36 #ifdef CONFIG_CPU_V6
38 static void icedcc_putc(int ch)
40 int status, i = 0x4000000;
42 do {
43 if (--i < 0)
44 return;
46 asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
47 } while (status & (1 << 29));
49 asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
52 #else
54 static void icedcc_putc(int ch)
56 int status, i = 0x4000000;
58 do {
59 if (--i < 0)
60 return;
62 asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
63 } while (status & 2);
65 asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
68 #endif
70 #define putc(ch) icedcc_putc(ch)
71 #define flush() do { } while (0)
72 #endif
74 #if 0 // mask by Vitor Yu. 06-08-2007
75 static void putstr(const char *ptr)
77 char c;
79 while ((c = *ptr++) != '\0') {
80 if (c == '\n')
81 putc('\r');
82 putc(c);
85 flush();
87 #endif
89 #endif
91 #define __ptr_t void *
94 * Optimised C version of memzero for the ARM.
96 void __memzero (__ptr_t s, size_t n)
98 union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
99 int i;
101 u.vp = s;
103 for (i = n >> 5; i > 0; i--) {
104 *u.ulp++ = 0;
105 *u.ulp++ = 0;
106 *u.ulp++ = 0;
107 *u.ulp++ = 0;
108 *u.ulp++ = 0;
109 *u.ulp++ = 0;
110 *u.ulp++ = 0;
111 *u.ulp++ = 0;
114 if (n & 1 << 4) {
115 *u.ulp++ = 0;
116 *u.ulp++ = 0;
117 *u.ulp++ = 0;
118 *u.ulp++ = 0;
121 if (n & 1 << 3) {
122 *u.ulp++ = 0;
123 *u.ulp++ = 0;
126 if (n & 1 << 2)
127 *u.ulp++ = 0;
129 if (n & 1 << 1) {
130 *u.ucp++ = 0;
131 *u.ucp++ = 0;
134 if (n & 1)
135 *u.ucp++ = 0;
138 static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
139 size_t __n)
141 int i = 0;
142 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
144 for (i = __n >> 3; i > 0; i--) {
145 *d++ = *s++;
146 *d++ = *s++;
147 *d++ = *s++;
148 *d++ = *s++;
149 *d++ = *s++;
150 *d++ = *s++;
151 *d++ = *s++;
152 *d++ = *s++;
155 if (__n & 1 << 2) {
156 *d++ = *s++;
157 *d++ = *s++;
158 *d++ = *s++;
159 *d++ = *s++;
162 if (__n & 1 << 1) {
163 *d++ = *s++;
164 *d++ = *s++;
167 if (__n & 1)
168 *d++ = *s++;
170 return __dest;
174 * gzip delarations
176 #define OF(args) args
177 #define STATIC static
179 typedef unsigned char uch;
180 typedef unsigned short ush;
181 typedef unsigned long ulg;
183 #define WSIZE 0x8000 /* Window size must be at least 32k, */
184 /* and a power of two */
186 static uch *inbuf; /* input buffer */
187 static uch window[WSIZE]; /* Sliding window buffer */
189 static unsigned insize; /* valid bytes in inbuf */
190 static unsigned inptr; /* index of next byte to be processed in inbuf */
191 static unsigned outcnt; /* bytes in output buffer */
193 /* gzip flag byte */
194 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
195 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
196 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
197 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
198 #define COMMENT 0x10 /* bit 4 set: file comment present */
199 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
200 #define RESERVED 0xC0 /* bit 6,7: reserved */
202 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
204 /* Diagnostic functions */
205 #ifdef DEBUG
206 # define Assert(cond,msg) {if(!(cond)) error(msg);}
207 # define Trace(x) fprintf x
208 # define Tracev(x) {if (verbose) fprintf x ;}
209 # define Tracevv(x) {if (verbose>1) fprintf x ;}
210 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
211 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
212 #else
213 # define Assert(cond,msg)
214 # define Trace(x)
215 # define Tracev(x)
216 # define Tracevv(x)
217 # define Tracec(c,x)
218 # define Tracecv(c,x)
219 #endif
221 static int fill_inbuf(void);
222 static void flush_window(void);
223 static void error(char *m);
224 static void gzip_mark(void **);
225 static void gzip_release(void **);
227 extern char input_data[];
228 extern char input_data_end[];
230 static uch *output_data;
231 static ulg output_ptr;
232 static ulg bytes_out;
234 static void *malloc(int size);
235 static void free(void *where);
236 static void error(char *m);
237 static void gzip_mark(void **);
238 static void gzip_release(void **);
240 static void putstr(const char *);
242 extern int end;
243 static ulg free_mem_ptr;
244 static ulg free_mem_ptr_end;
246 #define HEAP_SIZE 0x2000
248 #include "../../../../lib/inflate.c"
250 #ifndef STANDALONE_DEBUG
251 static void *malloc(int size)
253 void *p;
255 if (size <0) error("Malloc error");
256 if (free_mem_ptr <= 0) error("Memory error");
258 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
260 p = (void *)free_mem_ptr;
261 free_mem_ptr += size;
263 if (free_mem_ptr >= free_mem_ptr_end)
264 error("Out of memory");
265 return p;
268 static void free(void *where)
269 { /* gzip_mark & gzip_release do the free */
272 static void gzip_mark(void **ptr)
274 arch_decomp_wdog();
275 *ptr = (void *) free_mem_ptr;
278 static void gzip_release(void **ptr)
280 arch_decomp_wdog();
281 free_mem_ptr = (long) *ptr;
283 #else
284 static void gzip_mark(void **ptr)
288 static void gzip_release(void **ptr)
291 #endif
293 /* ===========================================================================
294 * Fill the input buffer. This is called only when the buffer is empty
295 * and at least one byte is really needed.
297 int fill_inbuf(void)
299 if (insize != 0)
300 error("ran out of input data");
302 inbuf = input_data;
303 insize = &input_data_end[0] - &input_data[0];
305 inptr = 1;
306 return inbuf[0];
309 /* ===========================================================================
310 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
311 * (Used for the decompressed data only.)
313 void flush_window(void)
315 ulg c = crc;
316 unsigned n;
317 uch *in, *out, ch;
319 in = window;
320 out = &output_data[output_ptr];
321 for (n = 0; n < outcnt; n++) {
322 ch = *out++ = *in++;
323 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
325 crc = c;
326 bytes_out += (ulg)outcnt;
327 output_ptr += (ulg)outcnt;
328 outcnt = 0;
329 putstr(".");
332 #ifndef arch_error
333 #define arch_error(x)
334 #endif
336 static void error(char *x)
338 arch_error(x);
340 putstr("\n\n");
341 putstr(x);
342 putstr("\n\n -- System halted");
344 while(1); /* Halt */
347 #ifndef STANDALONE_DEBUG
350 decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
351 int arch_id)
353 output_data = (uch *)output_start; /* Points to kernel start */
354 free_mem_ptr = free_mem_ptr_p;
355 free_mem_ptr_end = free_mem_ptr_end_p;
356 __machine_arch_type = arch_id;
358 arch_decomp_setup();
360 makecrc();
361 putstr("Uncompressing Linux...");
362 gunzip();
363 putstr(" done, booting the kernel.\n");
364 return output_ptr;
366 #else
368 char output_buffer[1500*1024];
370 int main()
372 output_data = output_buffer;
374 makecrc();
375 putstr("Uncompressing Linux...");
376 gunzip();
377 putstr("done.\n");
378 return 0;
380 #endif