4 * This is a collection of several routines from gzip-1.0.3
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
12 #include <linux/linkage.h>
13 #include <linux/vmalloc.h>
14 #include <linux/screen_info.h>
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.
25 * Getting to provable safe in place decompression is hard.
26 * Worst case behaviours need to be analized.
27 * Background information:
36 * compressed data blocks[N]
39 * resulting in 18 bytes of non compressed data overhead.
41 * Files divided into blocks
42 * 1 bit (last block flag)
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.
49 * 32 bits length in bytes.
56 * dynamic tree encoding.
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.
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
72 * The worst case at the block level is a growth of the compressed data
73 * of 5 bytes per 32767 bytes.
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
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:
89 * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
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.
103 #define OF(args) args
104 #define STATIC static
108 #define memzero(s, n) memset ((s), 0, (n))
110 typedef unsigned char uch;
111 typedef unsigned short ush;
112 typedef unsigned long ulg;
114 #define WSIZE 0x80000000 /* Window size must be at least 32k,
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.
122 static uch *inbuf; /* input buffer */
123 static uch *window; /* Sliding window buffer, (and final output buffer) */
125 static unsigned insize; /* valid bytes in inbuf */
126 static unsigned inptr; /* index of next byte to be processed in inbuf */
127 static unsigned outcnt; /* bytes in output buffer */
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 */
138 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
140 /* Diagnostic functions */
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 ;}
149 # define Assert(cond,msg)
154 # define Tracecv(c,x)
157 static int fill_inbuf(void);
158 static void flush_window(void);
159 static void error(char *m);
160 static void gzip_mark(void **);
161 static void gzip_release(void **);
164 * This is set up by the setup-routine at boot-time
166 static unsigned char *real_mode; /* Pointer to real-mode data */
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))
172 #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
174 extern unsigned char input_data[];
175 extern int input_len;
177 static long bytes_out = 0;
179 static void *malloc(int size);
180 static void free(void *where);
182 static void *memset(void *s, int c, unsigned n);
183 static void *memcpy(void *dest, const void *src, unsigned n);
185 static void putstr(const char *);
187 static unsigned long free_mem_ptr;
188 static unsigned long free_mem_end_ptr;
190 #define HEAP_SIZE 0x3000
192 static char *vidmem = (char *)0xb8000;
194 static int lines, cols;
196 #ifdef CONFIG_X86_NUMAQ
197 static void * xquad_portio = NULL;
200 #include "../../../../lib/inflate.c"
202 static void *malloc(int size)
206 if (size <0) error("Malloc error");
207 if (free_mem_ptr <= 0) error("Memory error");
209 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
211 p = (void *)free_mem_ptr;
212 free_mem_ptr += size;
214 if (free_mem_ptr >= free_mem_end_ptr)
215 error("Out of memory");
220 static void free(void *where)
224 static void gzip_mark(void **ptr)
226 *ptr = (void *) free_mem_ptr;
229 static void gzip_release(void **ptr)
231 free_mem_ptr = (unsigned long) *ptr;
234 static void scroll(void)
238 memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
239 for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
243 static void putstr(const char *s)
248 x = RM_SCREEN_INFO.orig_x;
249 y = RM_SCREEN_INFO.orig_y;
251 while ( ( c = *s++ ) != '\0' ) {
254 if ( ++y >= lines ) {
259 vidmem [ ( x + cols * y ) * 2 ] = c;
262 if ( ++y >= lines ) {
270 RM_SCREEN_INFO.orig_x = x;
271 RM_SCREEN_INFO.orig_y = y;
273 pos = (x + cols * y) * 2; /* Update cursor position */
275 outb_p(0xff & (pos >> 9), vidport+1);
277 outb_p(0xff & (pos >> 1), vidport+1);
280 static void* memset(void* s, int c, unsigned n)
285 for (i=0;i<n;i++) ss[i] = c;
289 static void* memcpy(void* dest, const void* src, unsigned n)
292 char *d = (char *)dest, *s = (char *)src;
294 for (i=0;i<n;i++) d[i] = s[i];
298 /* ===========================================================================
299 * Fill the input buffer. This is called only when the buffer is empty
300 * and at least one byte is really needed.
302 static int fill_inbuf(void)
304 error("ran out of input data");
308 /* ===========================================================================
309 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
310 * (Used for the decompressed data only.)
312 static void flush_window(void)
314 /* With my window equal to my output buffer
315 * I only need to compute the crc here.
317 ulg c = crc; /* temporary variable */
322 for (n = 0; n < outcnt; n++) {
324 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
327 bytes_out += (ulg)outcnt;
331 static void error(char *x)
335 putstr("\n\n -- System halted");
340 asmlinkage void decompress_kernel(void *rmode, unsigned long end,
341 uch *input_data, unsigned long input_len, uch *output)
345 if (RM_SCREEN_INFO.orig_video_mode == 7) {
346 vidmem = (char *) 0xb0000;
349 vidmem = (char *) 0xb8000;
353 lines = RM_SCREEN_INFO.orig_video_lines;
354 cols = RM_SCREEN_INFO.orig_video_cols;
356 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 */
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");
373 putstr("Uncompressing Linux... ");
375 putstr("Ok, booting the kernel.\n");