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/tty.h>
29 * Why do we do this? Don't ask me..
31 * Incomprehensible are the ways of bootloaders.
33 static void* memset(void *, int, size_t);
34 static void* memcpy(void *, __const void *, size_t);
35 #define memzero(s, n) memset ((s), 0, (n))
37 typedef unsigned char uch;
38 typedef unsigned short ush;
39 typedef unsigned long ulg;
41 #define WSIZE 0x8000 /* Window size must be at least 32k, */
42 /* and a power of two */
44 static uch *inbuf; /* input buffer */
45 static uch window[WSIZE]; /* Sliding window buffer */
47 static unsigned insize = 0; /* valid bytes in inbuf */
48 static unsigned inptr = 0; /* index of next byte to be processed in inbuf */
49 static unsigned outcnt = 0; /* bytes in output buffer */
52 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
53 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
54 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
55 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
56 #define COMMENT 0x10 /* bit 4 set: file comment present */
57 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
58 #define RESERVED 0xC0 /* bit 6,7: reserved */
60 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
62 /* Diagnostic functions */
64 # define Assert(cond,msg) {if(!(cond)) error(msg);}
65 # define Trace(x) fprintf x
66 # define Tracev(x) {if (verbose) fprintf x ;}
67 # define Tracevv(x) {if (verbose>1) fprintf x ;}
68 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
69 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
71 # define Assert(cond,msg)
79 static int fill_inbuf(void);
80 static void flush_window(void);
81 static void error(char *m);
82 static void gzip_mark(void **);
83 static void gzip_release(void **);
86 * This is set up by the setup-routine at boot-time
88 static unsigned char *real_mode; /* Pointer to real-mode data */
90 #define RM_EXT_MEM_K (*(unsigned short *)(real_mode + 0x2))
91 #ifndef STANDARD_MEMORY_BIOS_CALL
92 #define RM_ALT_MEM_K (*(unsigned long *)(real_mode + 0x1e0))
94 #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
96 extern char input_data[];
99 static long bytes_out = 0;
100 static uch *output_data;
101 static unsigned long output_ptr = 0;
103 static void *malloc(int size);
104 static void free(void *where);
106 static void putstr(const char *);
109 static long free_mem_ptr = (long)&end;
110 static long free_mem_end_ptr;
112 #define INPLACE_MOVE_ROUTINE 0x1000
113 #define LOW_BUFFER_START 0x2000
114 #define LOW_BUFFER_MAX 0x90000
115 #define HEAP_SIZE 0x3000
116 static unsigned int low_buffer_end, low_buffer_size;
117 static int high_loaded =0;
118 static uch *high_buffer_start /* = (uch *)(((ulg)&end) + HEAP_SIZE)*/;
120 static char *vidmem = (char *)0xb8000;
122 static int lines, cols;
124 #ifdef CONFIG_X86_NUMAQ
125 static void * xquad_portio = NULL;
128 #include "../../../../lib/inflate.c"
130 static void *malloc(int size)
134 if (size <0) error("Malloc error");
135 if (free_mem_ptr <= 0) error("Memory error");
137 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
139 p = (void *)free_mem_ptr;
140 free_mem_ptr += size;
142 if (free_mem_ptr >= free_mem_end_ptr)
143 error("Out of memory");
148 static void free(void *where)
152 static void gzip_mark(void **ptr)
154 *ptr = (void *) free_mem_ptr;
157 static void gzip_release(void **ptr)
159 free_mem_ptr = (long) *ptr;
162 static void scroll(void)
166 memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
167 for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
171 static void putstr(const char *s)
176 x = RM_SCREEN_INFO.orig_x;
177 y = RM_SCREEN_INFO.orig_y;
179 while ( ( c = *s++ ) != '\0' ) {
182 if ( ++y >= lines ) {
187 vidmem [ ( x + cols * y ) * 2 ] = c;
190 if ( ++y >= lines ) {
198 RM_SCREEN_INFO.orig_x = x;
199 RM_SCREEN_INFO.orig_y = y;
201 pos = (x + cols * y) * 2; /* Update cursor position */
203 outb_p(0xff & (pos >> 9), vidport+1);
205 outb_p(0xff & (pos >> 1), vidport+1);
208 static void* memset(void* s, int c, size_t n)
213 for (i=0;i<n;i++) ss[i] = c;
217 static void* memcpy(void* __dest, __const void* __src,
221 char *d = (char *)__dest, *s = (char *)__src;
223 for (i=0;i<__n;i++) d[i] = s[i];
227 /* ===========================================================================
228 * Fill the input buffer. This is called only when the buffer is empty
229 * and at least one byte is really needed.
231 static int fill_inbuf(void)
234 error("ran out of input data");
243 /* ===========================================================================
244 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
245 * (Used for the decompressed data only.)
247 static void flush_window_low(void)
249 ulg c = crc; /* temporary variable */
254 out = &output_data[output_ptr];
255 for (n = 0; n < outcnt; n++) {
257 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
260 bytes_out += (ulg)outcnt;
261 output_ptr += (ulg)outcnt;
265 static void flush_window_high(void)
267 ulg c = crc; /* temporary variable */
271 for (n = 0; n < outcnt; n++) {
272 ch = *output_data++ = *in++;
273 if ((ulg)output_data == low_buffer_end) output_data=high_buffer_start;
274 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
277 bytes_out += (ulg)outcnt;
281 static void flush_window(void)
283 if (high_loaded) flush_window_high();
284 else flush_window_low();
287 static void error(char *x)
291 putstr("\n\n -- System halted");
296 #define STACK_SIZE (4096)
298 long user_stack [STACK_SIZE];
303 } stack_start = { & user_stack [STACK_SIZE] , __BOOT_DS };
305 static void setup_normal_output_buffer(void)
307 #ifdef STANDARD_MEMORY_BIOS_CALL
308 if (RM_EXT_MEM_K < 1024) error("Less than 2MB of memory");
310 if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) < 1024) error("Less than 2MB of memory");
312 output_data = (char *)__PHYSICAL_START; /* Normally Points to 1M */
313 free_mem_end_ptr = (long)real_mode;
317 uch *low_buffer_start; int lcount;
318 uch *high_buffer_start; int hcount;
321 static void setup_output_buffer_if_we_run_high(struct moveparams *mv)
323 high_buffer_start = (uch *)(((ulg)&end) + HEAP_SIZE);
324 #ifdef STANDARD_MEMORY_BIOS_CALL
325 if (RM_EXT_MEM_K < (3*1024)) error("Less than 4MB of memory");
327 if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) <
329 error("Less than 4MB of memory");
331 mv->low_buffer_start = output_data = (char *)LOW_BUFFER_START;
332 low_buffer_end = ((unsigned int)real_mode > LOW_BUFFER_MAX
333 ? LOW_BUFFER_MAX : (unsigned int)real_mode) & ~0xfff;
334 low_buffer_size = low_buffer_end - LOW_BUFFER_START;
336 free_mem_end_ptr = (long)high_buffer_start;
337 if ( (__PHYSICAL_START + low_buffer_size) > ((ulg)high_buffer_start)) {
338 high_buffer_start = (uch *)(__PHYSICAL_START + low_buffer_size);
339 mv->hcount = 0; /* say: we need not to move high_buffer */
341 else mv->hcount = -1;
342 mv->high_buffer_start = high_buffer_start;
345 static void close_output_buffer_if_we_run_high(struct moveparams *mv)
347 if (bytes_out > low_buffer_size) {
348 mv->lcount = low_buffer_size;
350 mv->hcount = bytes_out - low_buffer_size;
352 mv->lcount = bytes_out;
357 asmlinkage int decompress_kernel(struct moveparams *mv, void *rmode)
361 if (RM_SCREEN_INFO.orig_video_mode == 7) {
362 vidmem = (char *) 0xb0000;
365 vidmem = (char *) 0xb8000;
369 lines = RM_SCREEN_INFO.orig_video_lines;
370 cols = RM_SCREEN_INFO.orig_video_cols;
372 if (free_mem_ptr < 0x100000) setup_normal_output_buffer();
373 else setup_output_buffer_if_we_run_high(mv);
376 putstr("Uncompressing Linux... ");
378 putstr("Ok, booting the kernel.\n");
379 if (high_loaded) close_output_buffer_if_we_run_high(mv);