File: /usr/src/linux/arch/arm/boot/compressed/misc.c

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