allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / arch / arm26 / boot / compressed / misc.c
blob0714d19c577636f655889726efbeabb887cfca24
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/kernel.h>
23 #include <asm/uaccess.h>
24 #include "uncompress.h"
26 #ifdef STANDALONE_DEBUG
27 #define puts printf
28 #endif
30 #define __ptr_t void *
33 * Optimised C version of memzero for the ARM.
35 void __memzero (__ptr_t s, size_t n)
37 union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
38 int i;
40 u.vp = s;
42 for (i = n >> 5; i > 0; i--) {
43 *u.ulp++ = 0;
44 *u.ulp++ = 0;
45 *u.ulp++ = 0;
46 *u.ulp++ = 0;
47 *u.ulp++ = 0;
48 *u.ulp++ = 0;
49 *u.ulp++ = 0;
50 *u.ulp++ = 0;
53 if (n & 1 << 4) {
54 *u.ulp++ = 0;
55 *u.ulp++ = 0;
56 *u.ulp++ = 0;
57 *u.ulp++ = 0;
60 if (n & 1 << 3) {
61 *u.ulp++ = 0;
62 *u.ulp++ = 0;
65 if (n & 1 << 2)
66 *u.ulp++ = 0;
68 if (n & 1 << 1) {
69 *u.ucp++ = 0;
70 *u.ucp++ = 0;
73 if (n & 1)
74 *u.ucp++ = 0;
77 static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
78 size_t __n)
80 int i = 0;
81 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
83 for (i = __n >> 3; i > 0; i--) {
84 *d++ = *s++;
85 *d++ = *s++;
86 *d++ = *s++;
87 *d++ = *s++;
88 *d++ = *s++;
89 *d++ = *s++;
90 *d++ = *s++;
91 *d++ = *s++;
94 if (__n & 1 << 2) {
95 *d++ = *s++;
96 *d++ = *s++;
97 *d++ = *s++;
98 *d++ = *s++;
101 if (__n & 1 << 1) {
102 *d++ = *s++;
103 *d++ = *s++;
106 if (__n & 1)
107 *d++ = *s++;
109 return __dest;
113 * gzip delarations
115 #define OF(args) args
116 #define STATIC static
118 typedef unsigned char uch;
119 typedef unsigned short ush;
120 typedef unsigned long ulg;
122 #define WSIZE 0x8000 /* Window size must be at least 32k, */
123 /* and a power of two */
125 static uch *inbuf; /* input buffer */
126 static uch window[WSIZE]; /* Sliding window buffer */
128 static unsigned insize; /* valid bytes in inbuf */
129 static unsigned inptr; /* index of next byte to be processed in inbuf */
130 static unsigned outcnt; /* bytes in output buffer */
132 /* gzip flag byte */
133 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
134 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
135 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
136 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
137 #define COMMENT 0x10 /* bit 4 set: file comment present */
138 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
139 #define RESERVED 0xC0 /* bit 6,7: reserved */
141 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
143 /* Diagnostic functions */
144 #ifdef DEBUG
145 # define Assert(cond,msg) {if(!(cond)) error(msg);}
146 # define Trace(x) fprintf x
147 # define Tracev(x) {if (verbose) fprintf x ;}
148 # define Tracevv(x) {if (verbose>1) fprintf x ;}
149 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
150 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
151 #else
152 # define Assert(cond,msg)
153 # define Trace(x)
154 # define Tracev(x)
155 # define Tracevv(x)
156 # define Tracec(c,x)
157 # define Tracecv(c,x)
158 #endif
160 static int fill_inbuf(void);
161 static void flush_window(void);
162 static void error(char *m);
163 static void gzip_mark(void **);
164 static void gzip_release(void **);
166 extern char input_data[];
167 extern char input_data_end[];
169 static uch *output_data;
170 static ulg output_ptr;
171 static ulg bytes_out;
173 static void *malloc(int size);
174 static void free(void *where);
175 static void error(char *m);
176 static void gzip_mark(void **);
177 static void gzip_release(void **);
179 static void puts(const char *);
181 extern int end;
182 static ulg free_mem_ptr;
183 static ulg free_mem_ptr_end;
185 #define HEAP_SIZE 0x3000
187 #include "../../../../lib/inflate.c"
189 #ifndef STANDALONE_DEBUG
190 static void *malloc(int size)
192 void *p;
194 if (size <0) error("Malloc error");
195 if (free_mem_ptr <= 0) error("Memory error");
197 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
199 p = (void *)free_mem_ptr;
200 free_mem_ptr += size;
202 if (free_mem_ptr >= free_mem_ptr_end)
203 error("Out of memory");
204 return p;
207 static void free(void *where)
208 { /* gzip_mark & gzip_release do the free */
211 static void gzip_mark(void **ptr)
213 arch_decomp_wdog();
214 *ptr = (void *) free_mem_ptr;
217 static void gzip_release(void **ptr)
219 arch_decomp_wdog();
220 free_mem_ptr = (long) *ptr;
222 #else
223 static void gzip_mark(void **ptr)
227 static void gzip_release(void **ptr)
230 #endif
232 /* ===========================================================================
233 * Fill the input buffer. This is called only when the buffer is empty
234 * and at least one byte is really needed.
236 int fill_inbuf(void)
238 if (insize != 0)
239 error("ran out of input data");
241 inbuf = input_data;
242 insize = &input_data_end[0] - &input_data[0];
244 inptr = 1;
245 return inbuf[0];
248 /* ===========================================================================
249 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
250 * (Used for the decompressed data only.)
252 void flush_window(void)
254 ulg c = crc;
255 unsigned n;
256 uch *in, *out, ch;
258 in = window;
259 out = &output_data[output_ptr];
260 for (n = 0; n < outcnt; n++) {
261 ch = *out++ = *in++;
262 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
264 crc = c;
265 bytes_out += (ulg)outcnt;
266 output_ptr += (ulg)outcnt;
267 outcnt = 0;
268 puts(".");
271 static void error(char *x)
273 int ptr;
275 puts("\n\n");
276 puts(x);
277 puts("\n\n -- System halted");
279 while(1); /* Halt */
282 #ifndef STANDALONE_DEBUG
285 decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
286 int arch_id)
288 output_data = (uch *)output_start; /* Points to kernel start */
289 free_mem_ptr = free_mem_ptr_p;
290 free_mem_ptr_end = free_mem_ptr_end_p;
291 __machine_arch_type = arch_id;
293 arch_decomp_setup();
295 makecrc();
296 puts("Uncompressing Linux...");
297 gunzip();
298 puts(" done, booting the kernel.\n");
299 return output_ptr;
301 #else
303 char output_buffer[1500*1024];
305 int main()
307 output_data = output_buffer;
309 makecrc();
310 puts("Uncompressing Linux...");
311 gunzip();
312 puts("done.\n");
313 return 0;
315 #endif