MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / arch / arm / boot / compressed / misc.c
blob23434b56786a124f9592c824875744b2b7e16446
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 #include <asm/arch/uncompress.h>
25 #ifdef STANDALONE_DEBUG
26 #define putstr printf
27 #endif
29 #ifdef CONFIG_DEBUG_ICEDCC
30 #define putstr icedcc_putstr
31 #define putc icedcc_putc
33 extern void idedcc_putc(int ch);
35 static void
36 icedcc_putstr(const char *ptr)
38 for (; *ptr != '\0'; ptr++) {
39 icedcc_putc(*ptr);
43 #endif
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;
53 int i;
55 u.vp = s;
57 for (i = n >> 5; i > 0; i--) {
58 *u.ulp++ = 0;
59 *u.ulp++ = 0;
60 *u.ulp++ = 0;
61 *u.ulp++ = 0;
62 *u.ulp++ = 0;
63 *u.ulp++ = 0;
64 *u.ulp++ = 0;
65 *u.ulp++ = 0;
68 if (n & 1 << 4) {
69 *u.ulp++ = 0;
70 *u.ulp++ = 0;
71 *u.ulp++ = 0;
72 *u.ulp++ = 0;
75 if (n & 1 << 3) {
76 *u.ulp++ = 0;
77 *u.ulp++ = 0;
80 if (n & 1 << 2)
81 *u.ulp++ = 0;
83 if (n & 1 << 1) {
84 *u.ucp++ = 0;
85 *u.ucp++ = 0;
88 if (n & 1)
89 *u.ucp++ = 0;
92 static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
93 size_t __n)
95 int i = 0;
96 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
98 for (i = __n >> 3; i > 0; i--) {
99 *d++ = *s++;
100 *d++ = *s++;
101 *d++ = *s++;
102 *d++ = *s++;
103 *d++ = *s++;
104 *d++ = *s++;
105 *d++ = *s++;
106 *d++ = *s++;
109 if (__n & 1 << 2) {
110 *d++ = *s++;
111 *d++ = *s++;
112 *d++ = *s++;
113 *d++ = *s++;
116 if (__n & 1 << 1) {
117 *d++ = *s++;
118 *d++ = *s++;
121 if (__n & 1)
122 *d++ = *s++;
124 return __dest;
128 * gzip delarations
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 */
147 /* gzip flag byte */
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 */
159 #ifdef DEBUG
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 ;}
166 #else
167 # define Assert(cond,msg)
168 # define Trace(x)
169 # define Tracev(x)
170 # define Tracevv(x)
171 # define Tracec(c,x)
172 # define Tracecv(c,x)
173 #endif
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 *);
196 extern int end;
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)
207 void *p;
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");
219 return p;
222 static void free(void *where)
223 { /* gzip_mark & gzip_release do the free */
226 static void gzip_mark(void **ptr)
228 arch_decomp_wdog();
229 *ptr = (void *) free_mem_ptr;
232 static void gzip_release(void **ptr)
234 arch_decomp_wdog();
235 free_mem_ptr = (long) *ptr;
237 #else
238 static void gzip_mark(void **ptr)
242 static void gzip_release(void **ptr)
245 #endif
247 /* ===========================================================================
248 * Fill the input buffer. This is called only when the buffer is empty
249 * and at least one byte is really needed.
251 int fill_inbuf(void)
253 if (insize != 0)
254 error("ran out of input data");
256 inbuf = input_data;
257 insize = &input_data_end[0] - &input_data[0];
259 inptr = 1;
260 return inbuf[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)
269 ulg c = crc;
270 unsigned n;
271 uch *in, *out, ch;
273 in = window;
274 out = &output_data[output_ptr];
275 for (n = 0; n < outcnt; n++) {
276 ch = *out++ = *in++;
277 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
279 crc = c;
280 bytes_out += (ulg)outcnt;
281 output_ptr += (ulg)outcnt;
282 outcnt = 0;
283 putstr(".");
286 static void error(char *x)
288 putstr("\n\n");
289 putstr(x);
290 putstr("\n\n -- System halted");
292 while(1); /* Halt */
295 #ifndef STANDALONE_DEBUG
298 decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
299 int arch_id)
301 output_data = (uch *)output_start; /* Points to kernel start */
302 free_mem_ptr = free_mem_ptr_p;
303 free_mem_ptr_end = free_mem_ptr_end_p;
304 __machine_arch_type = arch_id;
306 arch_decomp_setup();
308 makecrc();
309 putstr("Uncompressing Linux...");
310 gunzip();
311 putstr(" done, booting the kernel.\n");
312 return output_ptr;
314 #else
316 char output_buffer[1500*1024];
318 int main()
320 output_data = output_buffer;
322 makecrc();
323 putstr("Uncompressing Linux...");
324 gunzip();
325 putstr("done.\n");
326 return 0;
328 #endif