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