blob: 2b1c22de558a6c93f4f2942790d62baa92e876d2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * misc.c
Ian Campbell818a08f2008-01-30 13:33:38 +01003 *
4 * This is a collection of several routines from gzip-1.0.3
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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
Ian Campbell818a08f2008-01-30 13:33:38 +010012/*
13 * we have to be careful, because no indirections are allowed here, and
14 * paravirt_ops is a kind of one. As it will only run in baremetal anyway,
15 * we just keep it from happening
16 */
Rusty Russelld3561b72006-12-07 02:14:07 +010017#undef CONFIG_PARAVIRT
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/linkage.h>
Brian Gerst7e7f3582006-01-08 01:04:54 -080019#include <linux/screen_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/io.h>
Eric W. Biederman968de4f2006-12-07 02:14:04 +010021#include <asm/page.h>
Vivek Goyale69f2022006-12-07 02:14:04 +010022#include <asm/boot.h>
Eric W. Biederman968de4f2006-12-07 02:14:04 +010023
24/* WARNING!!
25 * This code is compiled with -fPIC and it is relocated dynamically
26 * at run time, but no relocation processing is performed.
27 * This means that it is not safe to place pointers in static structures.
28 */
29
30/*
31 * Getting to provable safe in place decompression is hard.
Simon Arlott27b46d72007-10-20 01:13:56 +020032 * Worst case behaviours need to be analyzed.
Eric W. Biederman968de4f2006-12-07 02:14:04 +010033 * Background information:
34 *
35 * The file layout is:
36 * magic[2]
37 * method[1]
38 * flags[1]
39 * timestamp[4]
40 * extraflags[1]
41 * os[1]
42 * compressed data blocks[N]
43 * crc[4] orig_len[4]
44 *
45 * resulting in 18 bytes of non compressed data overhead.
46 *
47 * Files divided into blocks
48 * 1 bit (last block flag)
49 * 2 bits (block type)
50 *
51 * 1 block occurs every 32K -1 bytes or when there 50% compression has been achieved.
52 * The smallest block type encoding is always used.
53 *
54 * stored:
55 * 32 bits length in bytes.
56 *
57 * fixed:
58 * magic fixed tree.
59 * symbols.
60 *
61 * dynamic:
62 * dynamic tree encoding.
63 * symbols.
64 *
65 *
66 * The buffer for decompression in place is the length of the
67 * uncompressed data, plus a small amount extra to keep the algorithm safe.
68 * The compressed data is placed at the end of the buffer. The output
69 * pointer is placed at the start of the buffer and the input pointer
70 * is placed where the compressed data starts. Problems will occur
71 * when the output pointer overruns the input pointer.
72 *
73 * The output pointer can only overrun the input pointer if the input
74 * pointer is moving faster than the output pointer. A condition only
75 * triggered by data whose compressed form is larger than the uncompressed
76 * form.
77 *
78 * The worst case at the block level is a growth of the compressed data
79 * of 5 bytes per 32767 bytes.
80 *
81 * The worst case internal to a compressed block is very hard to figure.
82 * The worst case can at least be boundined by having one bit that represents
83 * 32764 bytes and then all of the rest of the bytes representing the very
84 * very last byte.
85 *
86 * All of which is enough to compute an amount of extra data that is required
87 * to be safe. To avoid problems at the block level allocating 5 extra bytes
88 * per 32767 bytes of data is sufficient. To avoind problems internal to a block
89 * adding an extra 32767 bytes (the worst case uncompressed block size) is
90 * sufficient, to ensure that in the worst case the decompressed data for
91 * block will stop the byte before the compressed data for a block begins.
92 * To avoid problems with the compressed data's meta information an extra 18
93 * bytes are needed. Leading to the formula:
94 *
95 * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
96 *
97 * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
98 * Adding 32768 instead of 32767 just makes for round numbers.
99 * Adding the decompressor_size is necessary as it musht live after all
100 * of the data as well. Last I measured the decompressor is about 14K.
Simon Arlott27b46d72007-10-20 01:13:56 +0200101 * 10K of actual data and 4K of bss.
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100102 *
103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/*
106 * gzip declarations
107 */
108
109#define OF(args) args
110#define STATIC static
111
112#undef memset
113#undef memcpy
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define memzero(s, n) memset ((s), 0, (n))
115
116typedef unsigned char uch;
117typedef unsigned short ush;
118typedef unsigned long ulg;
119
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100120#define WSIZE 0x80000000 /* Window size must be at least 32k,
121 * and a power of two
122 * We don't actually have a window just
123 * a huge output buffer so I report
124 * a 2G windows size, as that should
125 * always be larger than our output buffer.
126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100128static uch *inbuf; /* input buffer */
129static uch *window; /* Sliding window buffer, (and final output buffer) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100131static unsigned insize; /* valid bytes in inbuf */
132static unsigned inptr; /* index of next byte to be processed in inbuf */
133static unsigned outcnt; /* bytes in output buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/* gzip flag byte */
136#define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
137#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
138#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
139#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
140#define COMMENT 0x10 /* bit 4 set: file comment present */
141#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
142#define RESERVED 0xC0 /* bit 6,7: reserved */
143
144#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
145
146/* Diagnostic functions */
147#ifdef DEBUG
148# define Assert(cond,msg) {if(!(cond)) error(msg);}
149# define Trace(x) fprintf x
150# define Tracev(x) {if (verbose) fprintf x ;}
151# define Tracevv(x) {if (verbose>1) fprintf x ;}
152# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
153# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
154#else
155# define Assert(cond,msg)
156# define Trace(x)
157# define Tracev(x)
158# define Tracevv(x)
159# define Tracec(c,x)
160# define Tracecv(c,x)
161#endif
162
163static int fill_inbuf(void);
164static void flush_window(void);
165static void error(char *m);
166static void gzip_mark(void **);
167static void gzip_release(void **);
168
169/*
170 * This is set up by the setup-routine at boot-time
171 */
172static unsigned char *real_mode; /* Pointer to real-mode data */
173
174#define RM_EXT_MEM_K (*(unsigned short *)(real_mode + 0x2))
175#ifndef STANDARD_MEMORY_BIOS_CALL
176#define RM_ALT_MEM_K (*(unsigned long *)(real_mode + 0x1e0))
177#endif
178#define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
179
Carl-Daniel Hailfingerb79c4df72006-06-26 13:57:53 +0200180extern unsigned char input_data[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181extern int input_len;
182
183static long bytes_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185static void *malloc(int size);
186static void free(void *where);
187
Carl-Daniel Hailfingerb79c4df72006-06-26 13:57:53 +0200188static void *memset(void *s, int c, unsigned n);
189static void *memcpy(void *dest, const void *src, unsigned n);
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static void putstr(const char *);
192
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100193static unsigned long free_mem_ptr;
194static unsigned long free_mem_end_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Jeremy Fitzhardinge35c74222007-05-02 19:27:15 +0200196#define HEAP_SIZE 0x4000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198static char *vidmem = (char *)0xb8000;
199static int vidport;
200static int lines, cols;
201
202#ifdef CONFIG_X86_NUMAQ
Randy Dunlapd5d24482006-12-07 02:14:13 +0100203void *xquad_portio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204#endif
205
206#include "../../../../lib/inflate.c"
207
208static void *malloc(int size)
209{
210 void *p;
211
212 if (size <0) error("Malloc error");
213 if (free_mem_ptr <= 0) error("Memory error");
214
215 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
216
217 p = (void *)free_mem_ptr;
218 free_mem_ptr += size;
219
220 if (free_mem_ptr >= free_mem_end_ptr)
221 error("Out of memory");
222
223 return p;
224}
225
226static void free(void *where)
227{ /* Don't care */
228}
229
230static void gzip_mark(void **ptr)
231{
232 *ptr = (void *) free_mem_ptr;
233}
234
235static void gzip_release(void **ptr)
236{
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100237 free_mem_ptr = (unsigned long) *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240static void scroll(void)
241{
242 int i;
243
244 memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
245 for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
246 vidmem[i] = ' ';
247}
248
249static void putstr(const char *s)
250{
251 int x,y,pos;
252 char c;
253
Rusty Russella24e7852007-10-21 16:41:35 -0700254 if (RM_SCREEN_INFO.orig_video_mode == 0 && lines == 0 && cols == 0)
255 return;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 x = RM_SCREEN_INFO.orig_x;
258 y = RM_SCREEN_INFO.orig_y;
259
260 while ( ( c = *s++ ) != '\0' ) {
261 if ( c == '\n' ) {
262 x = 0;
263 if ( ++y >= lines ) {
264 scroll();
265 y--;
266 }
267 } else {
Ian Campbell818a08f2008-01-30 13:33:38 +0100268 vidmem [(x + cols * y) * 2] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if ( ++x >= cols ) {
270 x = 0;
271 if ( ++y >= lines ) {
272 scroll();
273 y--;
274 }
275 }
276 }
277 }
278
279 RM_SCREEN_INFO.orig_x = x;
280 RM_SCREEN_INFO.orig_y = y;
281
282 pos = (x + cols * y) * 2; /* Update cursor position */
Rene Hermanb02aae92008-01-30 13:30:05 +0100283 outb(14, vidport);
284 outb(0xff & (pos >> 9), vidport+1);
285 outb(15, vidport);
286 outb(0xff & (pos >> 1), vidport+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Carl-Daniel Hailfingerb79c4df72006-06-26 13:57:53 +0200289static void* memset(void* s, int c, unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 int i;
Jan Engelhardtade1af72008-01-30 13:33:23 +0100292 char *ss = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 for (i=0;i<n;i++) ss[i] = c;
295 return s;
296}
297
Carl-Daniel Hailfingerb79c4df72006-06-26 13:57:53 +0200298static void* memcpy(void* dest, const void* src, unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 int i;
Jan Engelhardtade1af72008-01-30 13:33:23 +0100301 const char *s = src;
302 char *d = dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Carl-Daniel Hailfingerb79c4df72006-06-26 13:57:53 +0200304 for (i=0;i<n;i++) d[i] = s[i];
305 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/* ===========================================================================
309 * Fill the input buffer. This is called only when the buffer is empty
310 * and at least one byte is really needed.
311 */
312static int fill_inbuf(void)
313{
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100314 error("ran out of input data");
315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318/* ===========================================================================
319 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
320 * (Used for the decompressed data only.)
321 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static void flush_window(void)
323{
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100324 /* With my window equal to my output buffer
325 * I only need to compute the crc here.
326 */
327 ulg c = crc; /* temporary variable */
328 unsigned n;
329 uch *in, ch;
330
331 in = window;
332 for (n = 0; n < outcnt; n++) {
333 ch = *in++;
334 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
335 }
336 crc = c;
337 bytes_out += (ulg)outcnt;
338 outcnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341static void error(char *x)
342{
343 putstr("\n\n");
344 putstr(x);
345 putstr("\n\n -- System halted");
346
Ingo Molnarff3cf852008-01-30 13:32:31 +0100347 while (1)
348 asm("hlt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Ian Campbell4c83d652008-01-30 13:33:38 +0100351asmlinkage void decompress_kernel(void *rmode, unsigned long heap,
Ian Campbell818a08f2008-01-30 13:33:38 +0100352 uch *input_data, unsigned long input_len,
353 uch *output)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 real_mode = rmode;
356
357 if (RM_SCREEN_INFO.orig_video_mode == 7) {
358 vidmem = (char *) 0xb0000;
359 vidport = 0x3b4;
360 } else {
361 vidmem = (char *) 0xb8000;
362 vidport = 0x3d4;
363 }
364
365 lines = RM_SCREEN_INFO.orig_video_lines;
366 cols = RM_SCREEN_INFO.orig_video_cols;
367
Ian Campbell818a08f2008-01-30 13:33:38 +0100368 window = output; /* Output buffer (Normally at 1M) */
Ian Campbell4c83d652008-01-30 13:33:38 +0100369 free_mem_ptr = heap; /* Heap */
370 free_mem_end_ptr = heap + HEAP_SIZE;
Ian Campbell818a08f2008-01-30 13:33:38 +0100371 inbuf = input_data; /* Input buffer */
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100372 insize = input_len;
373 inptr = 0;
374
Vivek Goyale69f2022006-12-07 02:14:04 +0100375 if ((u32)output & (CONFIG_PHYSICAL_ALIGN -1))
376 error("Destination address not CONFIG_PHYSICAL_ALIGN aligned");
Ian Campbell4c83d652008-01-30 13:33:38 +0100377 if (heap > ((-__PAGE_OFFSET-(512<<20)-1) & 0x7fffffff))
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100378 error("Destination address too large");
379#ifndef CONFIG_RELOCATABLE
Vivek Goyale69f2022006-12-07 02:14:04 +0100380 if ((u32)output != LOAD_PHYSICAL_ADDR)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100381 error("Wrong destination address");
382#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 makecrc();
Ian Campbell6b3c0422008-01-30 13:33:38 +0100385 putstr("\nDecompressing Linux... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 gunzip();
Ian Campbell6b3c0422008-01-30 13:33:38 +0100387 putstr("done.\nBooting the kernel.\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100388 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}