blob: 4eac24e95a10d5d8f3e03d7e69a61837387611ed [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 * puts by Nick Holloway 1993, better puts by Martin Mares 1995
9 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
10 */
11
12#include <linux/linkage.h>
13#include <linux/vmalloc.h>
Brian Gerst7e7f3582006-01-08 01:04:54 -080014#include <linux/screen_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/io.h>
Eric W. Biederman968de4f2006-12-07 02:14:04 +010016#include <asm/page.h>
17
18/* WARNING!!
19 * This code is compiled with -fPIC and it is relocated dynamically
20 * at run time, but no relocation processing is performed.
21 * This means that it is not safe to place pointers in static structures.
22 */
23
24/*
25 * Getting to provable safe in place decompression is hard.
26 * Worst case behaviours need to be analized.
27 * Background information:
28 *
29 * The file layout is:
30 * magic[2]
31 * method[1]
32 * flags[1]
33 * timestamp[4]
34 * extraflags[1]
35 * os[1]
36 * compressed data blocks[N]
37 * crc[4] orig_len[4]
38 *
39 * resulting in 18 bytes of non compressed data overhead.
40 *
41 * Files divided into blocks
42 * 1 bit (last block flag)
43 * 2 bits (block type)
44 *
45 * 1 block occurs every 32K -1 bytes or when there 50% compression has been achieved.
46 * The smallest block type encoding is always used.
47 *
48 * stored:
49 * 32 bits length in bytes.
50 *
51 * fixed:
52 * magic fixed tree.
53 * symbols.
54 *
55 * dynamic:
56 * dynamic tree encoding.
57 * symbols.
58 *
59 *
60 * The buffer for decompression in place is the length of the
61 * uncompressed data, plus a small amount extra to keep the algorithm safe.
62 * The compressed data is placed at the end of the buffer. The output
63 * pointer is placed at the start of the buffer and the input pointer
64 * is placed where the compressed data starts. Problems will occur
65 * when the output pointer overruns the input pointer.
66 *
67 * The output pointer can only overrun the input pointer if the input
68 * pointer is moving faster than the output pointer. A condition only
69 * triggered by data whose compressed form is larger than the uncompressed
70 * form.
71 *
72 * The worst case at the block level is a growth of the compressed data
73 * of 5 bytes per 32767 bytes.
74 *
75 * The worst case internal to a compressed block is very hard to figure.
76 * The worst case can at least be boundined by having one bit that represents
77 * 32764 bytes and then all of the rest of the bytes representing the very
78 * very last byte.
79 *
80 * All of which is enough to compute an amount of extra data that is required
81 * to be safe. To avoid problems at the block level allocating 5 extra bytes
82 * per 32767 bytes of data is sufficient. To avoind problems internal to a block
83 * adding an extra 32767 bytes (the worst case uncompressed block size) is
84 * sufficient, to ensure that in the worst case the decompressed data for
85 * block will stop the byte before the compressed data for a block begins.
86 * To avoid problems with the compressed data's meta information an extra 18
87 * bytes are needed. Leading to the formula:
88 *
89 * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
90 *
91 * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
92 * Adding 32768 instead of 32767 just makes for round numbers.
93 * Adding the decompressor_size is necessary as it musht live after all
94 * of the data as well. Last I measured the decompressor is about 14K.
95 * 10K of actuall data and 4K of bss.
96 *
97 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
100 * gzip declarations
101 */
102
103#define OF(args) args
104#define STATIC static
105
106#undef memset
107#undef memcpy
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define memzero(s, n) memset ((s), 0, (n))
109
110typedef unsigned char uch;
111typedef unsigned short ush;
112typedef unsigned long ulg;
113
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100114#define WSIZE 0x80000000 /* Window size must be at least 32k,
115 * and a power of two
116 * We don't actually have a window just
117 * a huge output buffer so I report
118 * a 2G windows size, as that should
119 * always be larger than our output buffer.
120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100122static uch *inbuf; /* input buffer */
123static uch *window; /* Sliding window buffer, (and final output buffer) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100125static unsigned insize; /* valid bytes in inbuf */
126static unsigned inptr; /* index of next byte to be processed in inbuf */
127static unsigned outcnt; /* bytes in output buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/* gzip flag byte */
130#define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
131#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
132#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
133#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
134#define COMMENT 0x10 /* bit 4 set: file comment present */
135#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
136#define RESERVED 0xC0 /* bit 6,7: reserved */
137
138#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
139
140/* Diagnostic functions */
141#ifdef DEBUG
142# define Assert(cond,msg) {if(!(cond)) error(msg);}
143# define Trace(x) fprintf x
144# define Tracev(x) {if (verbose) fprintf x ;}
145# define Tracevv(x) {if (verbose>1) fprintf x ;}
146# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
147# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
148#else
149# define Assert(cond,msg)
150# define Trace(x)
151# define Tracev(x)
152# define Tracevv(x)
153# define Tracec(c,x)
154# define Tracecv(c,x)
155#endif
156
157static int fill_inbuf(void);
158static void flush_window(void);
159static void error(char *m);
160static void gzip_mark(void **);
161static void gzip_release(void **);
162
163/*
164 * This is set up by the setup-routine at boot-time
165 */
166static unsigned char *real_mode; /* Pointer to real-mode data */
167
168#define RM_EXT_MEM_K (*(unsigned short *)(real_mode + 0x2))
169#ifndef STANDARD_MEMORY_BIOS_CALL
170#define RM_ALT_MEM_K (*(unsigned long *)(real_mode + 0x1e0))
171#endif
172#define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
173
Carl-Daniel Hailfingerb79c4df72006-06-26 13:57:53 +0200174extern unsigned char input_data[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175extern int input_len;
176
177static long bytes_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179static void *malloc(int size);
180static void free(void *where);
181
Carl-Daniel Hailfingerb79c4df72006-06-26 13:57:53 +0200182static void *memset(void *s, int c, unsigned n);
183static void *memcpy(void *dest, const void *src, unsigned n);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static void putstr(const char *);
186
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100187static unsigned long free_mem_ptr;
188static unsigned long free_mem_end_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#define HEAP_SIZE 0x3000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192static char *vidmem = (char *)0xb8000;
193static int vidport;
194static int lines, cols;
195
196#ifdef CONFIG_X86_NUMAQ
197static void * xquad_portio = NULL;
198#endif
199
200#include "../../../../lib/inflate.c"
201
202static void *malloc(int size)
203{
204 void *p;
205
206 if (size <0) error("Malloc error");
207 if (free_mem_ptr <= 0) error("Memory error");
208
209 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
210
211 p = (void *)free_mem_ptr;
212 free_mem_ptr += size;
213
214 if (free_mem_ptr >= free_mem_end_ptr)
215 error("Out of memory");
216
217 return p;
218}
219
220static void free(void *where)
221{ /* Don't care */
222}
223
224static void gzip_mark(void **ptr)
225{
226 *ptr = (void *) free_mem_ptr;
227}
228
229static void gzip_release(void **ptr)
230{
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100231 free_mem_ptr = (unsigned long) *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234static void scroll(void)
235{
236 int i;
237
238 memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
239 for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
240 vidmem[i] = ' ';
241}
242
243static void putstr(const char *s)
244{
245 int x,y,pos;
246 char c;
247
248 x = RM_SCREEN_INFO.orig_x;
249 y = RM_SCREEN_INFO.orig_y;
250
251 while ( ( c = *s++ ) != '\0' ) {
252 if ( c == '\n' ) {
253 x = 0;
254 if ( ++y >= lines ) {
255 scroll();
256 y--;
257 }
258 } else {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100259 vidmem [ ( x + cols * y ) * 2 ] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if ( ++x >= cols ) {
261 x = 0;
262 if ( ++y >= lines ) {
263 scroll();
264 y--;
265 }
266 }
267 }
268 }
269
270 RM_SCREEN_INFO.orig_x = x;
271 RM_SCREEN_INFO.orig_y = y;
272
273 pos = (x + cols * y) * 2; /* Update cursor position */
274 outb_p(14, vidport);
275 outb_p(0xff & (pos >> 9), vidport+1);
276 outb_p(15, vidport);
277 outb_p(0xff & (pos >> 1), vidport+1);
278}
279
Carl-Daniel Hailfingerb79c4df72006-06-26 13:57:53 +0200280static void* memset(void* s, int c, unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 int i;
283 char *ss = (char*)s;
284
285 for (i=0;i<n;i++) ss[i] = c;
286 return s;
287}
288
Carl-Daniel Hailfingerb79c4df72006-06-26 13:57:53 +0200289static void* memcpy(void* dest, const void* src, unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 int i;
Carl-Daniel Hailfingerb79c4df72006-06-26 13:57:53 +0200292 char *d = (char *)dest, *s = (char *)src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Carl-Daniel Hailfingerb79c4df72006-06-26 13:57:53 +0200294 for (i=0;i<n;i++) d[i] = s[i];
295 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298/* ===========================================================================
299 * Fill the input buffer. This is called only when the buffer is empty
300 * and at least one byte is really needed.
301 */
302static int fill_inbuf(void)
303{
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100304 error("ran out of input data");
305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/* ===========================================================================
309 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
310 * (Used for the decompressed data only.)
311 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static void flush_window(void)
313{
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100314 /* With my window equal to my output buffer
315 * I only need to compute the crc here.
316 */
317 ulg c = crc; /* temporary variable */
318 unsigned n;
319 uch *in, ch;
320
321 in = window;
322 for (n = 0; n < outcnt; n++) {
323 ch = *in++;
324 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
325 }
326 crc = c;
327 bytes_out += (ulg)outcnt;
328 outcnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331static void error(char *x)
332{
333 putstr("\n\n");
334 putstr(x);
335 putstr("\n\n -- System halted");
336
337 while(1); /* Halt */
338}
339
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100340asmlinkage void decompress_kernel(void *rmode, unsigned long end,
341 uch *input_data, unsigned long input_len, uch *output)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 real_mode = rmode;
344
345 if (RM_SCREEN_INFO.orig_video_mode == 7) {
346 vidmem = (char *) 0xb0000;
347 vidport = 0x3b4;
348 } else {
349 vidmem = (char *) 0xb8000;
350 vidport = 0x3d4;
351 }
352
353 lines = RM_SCREEN_INFO.orig_video_lines;
354 cols = RM_SCREEN_INFO.orig_video_cols;
355
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100356 window = output; /* Output buffer (Normally at 1M) */
357 free_mem_ptr = end; /* Heap */
358 free_mem_end_ptr = end + HEAP_SIZE;
359 inbuf = input_data; /* Input buffer */
360 insize = input_len;
361 inptr = 0;
362
363 if (((u32)output - CONFIG_PHYSICAL_START) & 0x3fffff)
364 error("Destination address not 4M aligned");
365 if (end > ((-__PAGE_OFFSET-(512 <<20)-1) & 0x7fffffff))
366 error("Destination address too large");
367#ifndef CONFIG_RELOCATABLE
368 if ((u32)output != CONFIG_PHYSICAL_START)
369 error("Wrong destination address");
370#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 makecrc();
373 putstr("Uncompressing Linux... ");
374 gunzip();
375 putstr("Ok, booting the kernel.\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100376 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}