blob: 23434b56786a124f9592c824875744b2b7e16446 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * misc.c
3 *
4 * This is a collection of several routines from gzip-1.0.3
5 * adapted for Linux.
6 *
7 * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
8 *
9 * Modified for ARM Linux by Russell King
10 *
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.
17 */
18
19unsigned int __machine_arch_type;
20
21#include <linux/string.h>
22
23#include <asm/arch/uncompress.h>
24
25#ifdef STANDALONE_DEBUG
26#define putstr printf
27#endif
28
29#ifdef CONFIG_DEBUG_ICEDCC
30#define putstr icedcc_putstr
31#define putc icedcc_putc
32
33extern void idedcc_putc(int ch);
34
35static void
36icedcc_putstr(const char *ptr)
37{
38 for (; *ptr != '\0'; ptr++) {
39 icedcc_putc(*ptr);
40 }
41}
42
43#endif
44
45#define __ptr_t void *
46
47/*
48 * Optimised C version of memzero for the ARM.
49 */
50void __memzero (__ptr_t s, size_t n)
51{
52 union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
53 int i;
54
55 u.vp = s;
56
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;
66 }
67
68 if (n & 1 << 4) {
69 *u.ulp++ = 0;
70 *u.ulp++ = 0;
71 *u.ulp++ = 0;
72 *u.ulp++ = 0;
73 }
74
75 if (n & 1 << 3) {
76 *u.ulp++ = 0;
77 *u.ulp++ = 0;
78 }
79
80 if (n & 1 << 2)
81 *u.ulp++ = 0;
82
83 if (n & 1 << 1) {
84 *u.ucp++ = 0;
85 *u.ucp++ = 0;
86 }
87
88 if (n & 1)
89 *u.ucp++ = 0;
90}
91
92static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
93 size_t __n)
94{
95 int i = 0;
96 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
97
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++;
107 }
108
109 if (__n & 1 << 2) {
110 *d++ = *s++;
111 *d++ = *s++;
112 *d++ = *s++;
113 *d++ = *s++;
114 }
115
116 if (__n & 1 << 1) {
117 *d++ = *s++;
118 *d++ = *s++;
119 }
120
121 if (__n & 1)
122 *d++ = *s++;
123
124 return __dest;
125}
126
127/*
128 * gzip delarations
129 */
130#define OF(args) args
131#define STATIC static
132
133typedef unsigned char uch;
134typedef unsigned short ush;
135typedef unsigned long ulg;
136
137#define WSIZE 0x8000 /* Window size must be at least 32k, */
138 /* and a power of two */
139
140static uch *inbuf; /* input buffer */
141static uch window[WSIZE]; /* Sliding window buffer */
142
143static unsigned insize; /* valid bytes in inbuf */
144static unsigned inptr; /* index of next byte to be processed in inbuf */
145static unsigned outcnt; /* bytes in output buffer */
146
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 */
155
156#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
157
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
174
175static int fill_inbuf(void);
176static void flush_window(void);
177static void error(char *m);
178static void gzip_mark(void **);
179static void gzip_release(void **);
180
181extern char input_data[];
182extern char input_data_end[];
183
184static uch *output_data;
185static ulg output_ptr;
186static ulg bytes_out;
187
188static void *malloc(int size);
189static void free(void *where);
190static void error(char *m);
191static void gzip_mark(void **);
192static void gzip_release(void **);
193
194static void putstr(const char *);
195
196extern int end;
197static ulg free_mem_ptr;
198static ulg free_mem_ptr_end;
199
200#define HEAP_SIZE 0x2000
201
202#include "../../../../lib/inflate.c"
203
204#ifndef STANDALONE_DEBUG
205static void *malloc(int size)
206{
207 void *p;
208
209 if (size <0) error("Malloc error");
210 if (free_mem_ptr <= 0) error("Memory error");
211
212 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
213
214 p = (void *)free_mem_ptr;
215 free_mem_ptr += size;
216
217 if (free_mem_ptr >= free_mem_ptr_end)
218 error("Out of memory");
219 return p;
220}
221
222static void free(void *where)
223{ /* gzip_mark & gzip_release do the free */
224}
225
226static void gzip_mark(void **ptr)
227{
228 arch_decomp_wdog();
229 *ptr = (void *) free_mem_ptr;
230}
231
232static void gzip_release(void **ptr)
233{
234 arch_decomp_wdog();
235 free_mem_ptr = (long) *ptr;
236}
237#else
238static void gzip_mark(void **ptr)
239{
240}
241
242static void gzip_release(void **ptr)
243{
244}
245#endif
246
247/* ===========================================================================
248 * Fill the input buffer. This is called only when the buffer is empty
249 * and at least one byte is really needed.
250 */
251int fill_inbuf(void)
252{
253 if (insize != 0)
254 error("ran out of input data");
255
256 inbuf = input_data;
257 insize = &input_data_end[0] - &input_data[0];
258
259 inptr = 1;
260 return inbuf[0];
261}
262
263/* ===========================================================================
264 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
265 * (Used for the decompressed data only.)
266 */
267void flush_window(void)
268{
269 ulg c = crc;
270 unsigned n;
271 uch *in, *out, ch;
272
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);
278 }
279 crc = c;
280 bytes_out += (ulg)outcnt;
281 output_ptr += (ulg)outcnt;
282 outcnt = 0;
283 putstr(".");
284}
285
286static void error(char *x)
287{
288 putstr("\n\n");
289 putstr(x);
290 putstr("\n\n -- System halted");
291
292 while(1); /* Halt */
293}
294
295#ifndef STANDALONE_DEBUG
296
297ulg
298decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
299 int arch_id)
300{
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;
305
306 arch_decomp_setup();
307
308 makecrc();
309 putstr("Uncompressing Linux...");
310 gunzip();
311 putstr(" done, booting the kernel.\n");
312 return output_ptr;
313}
314#else
315
316char output_buffer[1500*1024];
317
318int main()
319{
320 output_data = output_buffer;
321
322 makecrc();
323 putstr("Uncompressing Linux...");
324 gunzip();
325 putstr("done.\n");
326 return 0;
327}
328#endif
329