blob: ace3fb5835d9278833a0c539b8fff0b74e8af885 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#ifdef STANDALONE_DEBUG
24#define putstr printf
Russell Kinga0815682006-03-28 10:24:33 +010025#else
26
27static void putstr(const char *ptr);
28
29#include <linux/compiler.h>
30#include <asm/arch/uncompress.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#ifdef CONFIG_DEBUG_ICEDCC
Russell Kingde4533a2006-03-28 10:34:05 +010033static void icedcc_putc(int ch)
34{
35 int status, i = 0x4000000;
36
37 do {
38 if (--i < 0)
39 return;
40
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010041 asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
Russell Kingde4533a2006-03-28 10:34:05 +010042 } while (status & 2);
43
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010044 asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
Russell Kingde4533a2006-03-28 10:34:05 +010045}
46
Russell Kinga0815682006-03-28 10:24:33 +010047#define putc(ch) icedcc_putc(ch)
48#define flush() do { } while (0)
49#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Russell Kinga0815682006-03-28 10:24:33 +010051static void putstr(const char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Russell Kinga0815682006-03-28 10:24:33 +010053 char c;
54
55 while ((c = *ptr++) != '\0') {
56 if (c == '\n')
57 putc('\r');
58 putc(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
Russell Kinga0815682006-03-28 10:24:33 +010060
61 flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64#endif
65
66#define __ptr_t void *
67
68/*
69 * Optimised C version of memzero for the ARM.
70 */
71void __memzero (__ptr_t s, size_t n)
72{
73 union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
74 int i;
75
76 u.vp = s;
77
78 for (i = n >> 5; i > 0; i--) {
79 *u.ulp++ = 0;
80 *u.ulp++ = 0;
81 *u.ulp++ = 0;
82 *u.ulp++ = 0;
83 *u.ulp++ = 0;
84 *u.ulp++ = 0;
85 *u.ulp++ = 0;
86 *u.ulp++ = 0;
87 }
88
89 if (n & 1 << 4) {
90 *u.ulp++ = 0;
91 *u.ulp++ = 0;
92 *u.ulp++ = 0;
93 *u.ulp++ = 0;
94 }
95
96 if (n & 1 << 3) {
97 *u.ulp++ = 0;
98 *u.ulp++ = 0;
99 }
100
101 if (n & 1 << 2)
102 *u.ulp++ = 0;
103
104 if (n & 1 << 1) {
105 *u.ucp++ = 0;
106 *u.ucp++ = 0;
107 }
108
109 if (n & 1)
110 *u.ucp++ = 0;
111}
112
113static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
114 size_t __n)
115{
116 int i = 0;
117 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
118
119 for (i = __n >> 3; i > 0; i--) {
120 *d++ = *s++;
121 *d++ = *s++;
122 *d++ = *s++;
123 *d++ = *s++;
124 *d++ = *s++;
125 *d++ = *s++;
126 *d++ = *s++;
127 *d++ = *s++;
128 }
129
130 if (__n & 1 << 2) {
131 *d++ = *s++;
132 *d++ = *s++;
133 *d++ = *s++;
134 *d++ = *s++;
135 }
136
137 if (__n & 1 << 1) {
138 *d++ = *s++;
139 *d++ = *s++;
140 }
141
142 if (__n & 1)
143 *d++ = *s++;
144
145 return __dest;
146}
147
148/*
149 * gzip delarations
150 */
151#define OF(args) args
152#define STATIC static
153
154typedef unsigned char uch;
155typedef unsigned short ush;
156typedef unsigned long ulg;
157
158#define WSIZE 0x8000 /* Window size must be at least 32k, */
159 /* and a power of two */
160
161static uch *inbuf; /* input buffer */
162static uch window[WSIZE]; /* Sliding window buffer */
163
164static unsigned insize; /* valid bytes in inbuf */
165static unsigned inptr; /* index of next byte to be processed in inbuf */
166static unsigned outcnt; /* bytes in output buffer */
167
168/* gzip flag byte */
169#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
170#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
171#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
172#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
173#define COMMENT 0x10 /* bit 4 set: file comment present */
174#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
175#define RESERVED 0xC0 /* bit 6,7: reserved */
176
177#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
178
179/* Diagnostic functions */
180#ifdef DEBUG
181# define Assert(cond,msg) {if(!(cond)) error(msg);}
182# define Trace(x) fprintf x
183# define Tracev(x) {if (verbose) fprintf x ;}
184# define Tracevv(x) {if (verbose>1) fprintf x ;}
185# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
186# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
187#else
188# define Assert(cond,msg)
189# define Trace(x)
190# define Tracev(x)
191# define Tracevv(x)
192# define Tracec(c,x)
193# define Tracecv(c,x)
194#endif
195
196static int fill_inbuf(void);
197static void flush_window(void);
198static void error(char *m);
199static void gzip_mark(void **);
200static void gzip_release(void **);
201
202extern char input_data[];
203extern char input_data_end[];
204
205static uch *output_data;
206static ulg output_ptr;
207static ulg bytes_out;
208
209static void *malloc(int size);
210static void free(void *where);
211static void error(char *m);
212static void gzip_mark(void **);
213static void gzip_release(void **);
214
215static void putstr(const char *);
216
217extern int end;
218static ulg free_mem_ptr;
219static ulg free_mem_ptr_end;
220
221#define HEAP_SIZE 0x2000
222
223#include "../../../../lib/inflate.c"
224
225#ifndef STANDALONE_DEBUG
226static void *malloc(int size)
227{
228 void *p;
229
230 if (size <0) error("Malloc error");
231 if (free_mem_ptr <= 0) error("Memory error");
232
233 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
234
235 p = (void *)free_mem_ptr;
236 free_mem_ptr += size;
237
238 if (free_mem_ptr >= free_mem_ptr_end)
239 error("Out of memory");
240 return p;
241}
242
243static void free(void *where)
244{ /* gzip_mark & gzip_release do the free */
245}
246
247static void gzip_mark(void **ptr)
248{
249 arch_decomp_wdog();
250 *ptr = (void *) free_mem_ptr;
251}
252
253static void gzip_release(void **ptr)
254{
255 arch_decomp_wdog();
256 free_mem_ptr = (long) *ptr;
257}
258#else
259static void gzip_mark(void **ptr)
260{
261}
262
263static void gzip_release(void **ptr)
264{
265}
266#endif
267
268/* ===========================================================================
269 * Fill the input buffer. This is called only when the buffer is empty
270 * and at least one byte is really needed.
271 */
272int fill_inbuf(void)
273{
274 if (insize != 0)
275 error("ran out of input data");
276
277 inbuf = input_data;
278 insize = &input_data_end[0] - &input_data[0];
279
280 inptr = 1;
281 return inbuf[0];
282}
283
284/* ===========================================================================
285 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
286 * (Used for the decompressed data only.)
287 */
288void flush_window(void)
289{
290 ulg c = crc;
291 unsigned n;
292 uch *in, *out, ch;
293
294 in = window;
295 out = &output_data[output_ptr];
296 for (n = 0; n < outcnt; n++) {
297 ch = *out++ = *in++;
298 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
299 }
300 crc = c;
301 bytes_out += (ulg)outcnt;
302 output_ptr += (ulg)outcnt;
303 outcnt = 0;
304 putstr(".");
305}
306
Ben Dooksf8c905d2005-11-08 22:43:05 +0000307#ifndef arch_error
308#define arch_error(x)
309#endif
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311static void error(char *x)
312{
Ben Dooksf8c905d2005-11-08 22:43:05 +0000313 arch_error(x);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 putstr("\n\n");
316 putstr(x);
317 putstr("\n\n -- System halted");
318
319 while(1); /* Halt */
320}
321
322#ifndef STANDALONE_DEBUG
323
324ulg
325decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
326 int arch_id)
327{
328 output_data = (uch *)output_start; /* Points to kernel start */
329 free_mem_ptr = free_mem_ptr_p;
330 free_mem_ptr_end = free_mem_ptr_end_p;
331 __machine_arch_type = arch_id;
332
333 arch_decomp_setup();
334
335 makecrc();
336 putstr("Uncompressing Linux...");
337 gunzip();
338 putstr(" done, booting the kernel.\n");
339 return output_ptr;
340}
341#else
342
343char output_buffer[1500*1024];
344
345int main()
346{
347 output_data = output_buffer;
348
349 makecrc();
350 putstr("Uncompressing Linux...");
351 gunzip();
352 putstr("done.\n");
353 return 0;
354}
355#endif
356