blob: 3fc08413fff05803a3f555de749917b56962b966 [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>
Russell Kinga09e64f2008-08-05 16:14:15 +010030#include <mach/uncompress.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#ifdef CONFIG_DEBUG_ICEDCC
Tony Lindgren7d95ded2006-09-20 13:03:34 +010033
34#ifdef CONFIG_CPU_V6
35
36static void icedcc_putc(int ch)
37{
38 int status, i = 0x4000000;
39
40 do {
41 if (--i < 0)
42 return;
43
44 asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
45 } while (status & (1 << 29));
46
47 asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
48}
49
50#else
51
Russell Kingde4533a2006-03-28 10:34:05 +010052static void icedcc_putc(int ch)
53{
54 int status, i = 0x4000000;
55
56 do {
57 if (--i < 0)
58 return;
59
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010060 asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
Russell Kingde4533a2006-03-28 10:34:05 +010061 } while (status & 2);
62
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010063 asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
Russell Kingde4533a2006-03-28 10:34:05 +010064}
65
Tony Lindgren7d95ded2006-09-20 13:03:34 +010066#endif
67
Russell Kinga0815682006-03-28 10:24:33 +010068#define putc(ch) icedcc_putc(ch)
69#define flush() do { } while (0)
70#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Russell Kinga0815682006-03-28 10:24:33 +010072static void putstr(const char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Russell Kinga0815682006-03-28 10:24:33 +010074 char c;
75
76 while ((c = *ptr++) != '\0') {
77 if (c == '\n')
78 putc('\r');
79 putc(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Russell Kinga0815682006-03-28 10:24:33 +010081
82 flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85#endif
86
87#define __ptr_t void *
88
Russell King59f0cb02008-10-27 11:24:09 +000089#define memzero(s,n) __memzero(s,n)
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * Optimised C version of memzero for the ARM.
93 */
94void __memzero (__ptr_t s, size_t n)
95{
96 union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
97 int i;
98
99 u.vp = s;
100
101 for (i = n >> 5; i > 0; i--) {
102 *u.ulp++ = 0;
103 *u.ulp++ = 0;
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 }
111
112 if (n & 1 << 4) {
113 *u.ulp++ = 0;
114 *u.ulp++ = 0;
115 *u.ulp++ = 0;
116 *u.ulp++ = 0;
117 }
118
119 if (n & 1 << 3) {
120 *u.ulp++ = 0;
121 *u.ulp++ = 0;
122 }
123
124 if (n & 1 << 2)
125 *u.ulp++ = 0;
126
127 if (n & 1 << 1) {
128 *u.ucp++ = 0;
129 *u.ucp++ = 0;
130 }
131
132 if (n & 1)
133 *u.ucp++ = 0;
134}
135
136static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
137 size_t __n)
138{
139 int i = 0;
140 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
141
142 for (i = __n >> 3; i > 0; i--) {
143 *d++ = *s++;
144 *d++ = *s++;
145 *d++ = *s++;
146 *d++ = *s++;
147 *d++ = *s++;
148 *d++ = *s++;
149 *d++ = *s++;
150 *d++ = *s++;
151 }
152
153 if (__n & 1 << 2) {
154 *d++ = *s++;
155 *d++ = *s++;
156 *d++ = *s++;
157 *d++ = *s++;
158 }
159
160 if (__n & 1 << 1) {
161 *d++ = *s++;
162 *d++ = *s++;
163 }
164
165 if (__n & 1)
166 *d++ = *s++;
167
168 return __dest;
169}
170
171/*
172 * gzip delarations
173 */
174#define OF(args) args
175#define STATIC static
176
177typedef unsigned char uch;
178typedef unsigned short ush;
179typedef unsigned long ulg;
180
181#define WSIZE 0x8000 /* Window size must be at least 32k, */
182 /* and a power of two */
183
184static uch *inbuf; /* input buffer */
185static uch window[WSIZE]; /* Sliding window buffer */
186
187static unsigned insize; /* valid bytes in inbuf */
188static unsigned inptr; /* index of next byte to be processed in inbuf */
189static unsigned outcnt; /* bytes in output buffer */
190
191/* gzip flag byte */
192#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
193#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
194#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
195#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
196#define COMMENT 0x10 /* bit 4 set: file comment present */
197#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
198#define RESERVED 0xC0 /* bit 6,7: reserved */
199
200#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
201
202/* Diagnostic functions */
203#ifdef DEBUG
204# define Assert(cond,msg) {if(!(cond)) error(msg);}
205# define Trace(x) fprintf x
206# define Tracev(x) {if (verbose) fprintf x ;}
207# define Tracevv(x) {if (verbose>1) fprintf x ;}
208# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
209# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
210#else
211# define Assert(cond,msg)
212# define Trace(x)
213# define Tracev(x)
214# define Tracevv(x)
215# define Tracec(c,x)
216# define Tracecv(c,x)
217#endif
218
219static int fill_inbuf(void);
220static void flush_window(void);
221static void error(char *m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223extern char input_data[];
224extern char input_data_end[];
225
226static uch *output_data;
227static ulg output_ptr;
228static ulg bytes_out;
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static void error(char *m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232static void putstr(const char *);
233
234extern int end;
235static ulg free_mem_ptr;
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700236static ulg free_mem_end_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700238#ifdef STANDALONE_DEBUG
239#define NO_INFLATE_MALLOC
240#endif
241
242#define ARCH_HAS_DECOMP_WDOG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244#include "../../../../lib/inflate.c"
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/* ===========================================================================
247 * Fill the input buffer. This is called only when the buffer is empty
248 * and at least one byte is really needed.
249 */
250int fill_inbuf(void)
251{
252 if (insize != 0)
253 error("ran out of input data");
254
255 inbuf = input_data;
256 insize = &input_data_end[0] - &input_data[0];
257
258 inptr = 1;
259 return inbuf[0];
260}
261
262/* ===========================================================================
263 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
264 * (Used for the decompressed data only.)
265 */
266void flush_window(void)
267{
268 ulg c = crc;
269 unsigned n;
270 uch *in, *out, ch;
271
272 in = window;
273 out = &output_data[output_ptr];
274 for (n = 0; n < outcnt; n++) {
275 ch = *out++ = *in++;
276 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
277 }
278 crc = c;
279 bytes_out += (ulg)outcnt;
280 output_ptr += (ulg)outcnt;
281 outcnt = 0;
282 putstr(".");
283}
284
Ben Dooksf8c905d2005-11-08 22:43:05 +0000285#ifndef arch_error
286#define arch_error(x)
287#endif
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static void error(char *x)
290{
Ben Dooksf8c905d2005-11-08 22:43:05 +0000291 arch_error(x);
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 putstr("\n\n");
294 putstr(x);
295 putstr("\n\n -- System halted");
296
297 while(1); /* Halt */
298}
299
300#ifndef STANDALONE_DEBUG
301
302ulg
303decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
304 int arch_id)
305{
306 output_data = (uch *)output_start; /* Points to kernel start */
307 free_mem_ptr = free_mem_ptr_p;
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700308 free_mem_end_ptr = free_mem_ptr_end_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 __machine_arch_type = arch_id;
310
311 arch_decomp_setup();
312
313 makecrc();
314 putstr("Uncompressing Linux...");
315 gunzip();
316 putstr(" done, booting the kernel.\n");
317 return output_ptr;
318}
319#else
320
321char output_buffer[1500*1024];
322
323int main()
324{
325 output_data = output_buffer;
326
327 makecrc();
328 putstr("Uncompressing Linux...");
329 gunzip();
330 putstr("done.\n");
331 return 0;
332}
333#endif
334