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>
16 #ifdef STANDARD_MEMORY_BIOS_CALL
17 #undef STANDARD_MEMORY_BIOS_CALL
31 * Why do we do this? Don't ask me..
33 * Incomprehensible are the ways of bootloaders.
35 static void* memset(void *, int, size_t);
36 static void* memcpy(void *, __const void *, size_t);
37 #define memzero(s, n) memset ((s), 0, (n))
39 typedef unsigned char uch;
40 typedef unsigned short ush;
41 typedef unsigned long ulg;
43 #define WSIZE 0x8000 /* Window size must be at least 32k, */
44 /* and a power of two */
46 static uch *inbuf; /* input buffer */
47 static uch window[WSIZE]; /* Sliding window buffer */
49 static unsigned insize = 0; /* valid bytes in inbuf */
50 static unsigned inptr = 0; /* index of next byte to be processed in inbuf */
51 static unsigned outcnt = 0; /* bytes in output buffer */
54 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
55 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
56 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
57 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
58 #define COMMENT 0x10 /* bit 4 set: file comment present */
59 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
60 #define RESERVED 0xC0 /* bit 6,7: reserved */
62 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
64 /* Diagnostic functions */
66 # define Assert(cond,msg) {if(!(cond)) error(msg);}
67 # define Trace(x) fprintf x
68 # define Tracev(x) {if (verbose) fprintf x ;}
69 # define Tracevv(x) {if (verbose>1) fprintf x ;}
70 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
71 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
73 # define Assert(cond,msg)
81 static int fill_inbuf(void);
82 static void flush_window(void);
83 static void error(char *m);
84 static void gzip_mark(void **);
85 static void gzip_release(void **);
88 * This is set up by the setup-routine at boot-time
90 static unsigned char *real_mode; /* Pointer to real-mode data */
92 #define EXT_MEM_K (*(unsigned short *)(real_mode + 0x2))
93 #ifndef STANDARD_MEMORY_BIOS_CALL
94 #define ALT_MEM_K (*(unsigned long *)(real_mode + 0x1e0))
96 #define SCREEN_INFO (*(struct screen_info *)(real_mode+0))
98 extern char input_data[];
101 static long bytes_out = 0;
102 static uch *output_data;
103 static unsigned long output_ptr = 0;
105 static void *malloc(int size);
106 static void free(void *where);
108 static void puts(const char *);
111 static long free_mem_ptr = (long)&end;
112 static long free_mem_end_ptr;
114 #define INPLACE_MOVE_ROUTINE 0x1000
115 #define LOW_BUFFER_START 0x2000
116 #define LOW_BUFFER_MAX 0x90000
117 #define HEAP_SIZE 0x3000
118 static unsigned int low_buffer_end, low_buffer_size;
119 static int high_loaded =0;
120 static uch *high_buffer_start /* = (uch *)(((ulg)&end) + HEAP_SIZE)*/;
122 static char *vidmem = (char *)0xa0000;
123 static int lines, cols;
125 #ifdef CONFIG_X86_NUMAQ
126 static void * xquad_portio = NULL;
129 #include "../../../../lib/inflate.c"
131 static void *malloc(int size)
135 if (size <0) error("Malloc error");
136 if (free_mem_ptr <= 0) error("Memory error");
138 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
140 p = (void *)free_mem_ptr;
141 free_mem_ptr += size;
143 if (free_mem_ptr >= free_mem_end_ptr)
144 error("Out of memory");
149 static void free(void *where)
153 static void gzip_mark(void **ptr)
155 *ptr = (void *) free_mem_ptr;
158 static void gzip_release(void **ptr)
160 free_mem_ptr = (long) *ptr;
163 static void scroll(void)
167 memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
168 for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
172 static void puts(const char *s)
177 x = SCREEN_INFO.orig_x;
178 y = SCREEN_INFO.orig_y;
180 while ( ( c = *s++ ) != '\0' ) {
183 if ( ++y >= lines ) {
188 vidmem [ ( x + cols * y ) * 2 ] = c;
191 if ( ++y >= lines ) {
199 SCREEN_INFO.orig_x = x;
200 SCREEN_INFO.orig_y = y;
202 pos = x + cols * y; /* Update cursor position */
203 while (!(inb_p(0x60) & 4));
205 outb_p(pos & 0xff, 0x60);
206 outb_p((pos >> 8) & 0xff, 0x60);
209 static void* memset(void* s, int c, size_t n)
214 for (i=0;i<n;i++) ss[i] = c;
218 static void* memcpy(void* __dest, __const void* __src,
222 char *d = (char *)__dest, *s = (char *)__src;
224 for (i=0;i<__n;i++) d[i] = s[i];
228 /* ===========================================================================
229 * Fill the input buffer. This is called only when the buffer is empty
230 * and at least one byte is really needed.
232 static int fill_inbuf(void)
235 error("ran out of input data");
244 /* ===========================================================================
245 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
246 * (Used for the decompressed data only.)
248 static void flush_window_low(void)
250 ulg c = crc; /* temporary variable */
255 out = &output_data[output_ptr];
256 for (n = 0; n < outcnt; n++) {
258 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
261 bytes_out += (ulg)outcnt;
262 output_ptr += (ulg)outcnt;
266 static void flush_window_high(void)
268 ulg c = crc; /* temporary variable */
272 for (n = 0; n < outcnt; n++) {
273 ch = *output_data++ = *in++;
274 if ((ulg)output_data == low_buffer_end) output_data=high_buffer_start;
275 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
278 bytes_out += (ulg)outcnt;
282 static void flush_window(void)
284 if (high_loaded) flush_window_high();
285 else flush_window_low();
288 static void error(char *x)
292 puts("\n\n -- System halted");
297 #define STACK_SIZE (4096)
299 long user_stack [STACK_SIZE];
304 } stack_start = { & user_stack [STACK_SIZE] , __BOOT_DS };
306 static void setup_normal_output_buffer(void)
308 #ifdef STANDARD_MEMORY_BIOS_CALL
309 if (EXT_MEM_K < 1024) error("Less than 2MB of memory");
311 if ((ALT_MEM_K > EXT_MEM_K ? ALT_MEM_K : EXT_MEM_K) < 1024) error("Less than 2MB of memory");
313 output_data = (char *)0x100000; /* Points to 1M */
314 free_mem_end_ptr = (long)real_mode;
318 uch *low_buffer_start; int lcount;
319 uch *high_buffer_start; int hcount;
322 static void setup_output_buffer_if_we_run_high(struct moveparams *mv)
324 high_buffer_start = (uch *)(((ulg)&end) + HEAP_SIZE);
325 #ifdef STANDARD_MEMORY_BIOS_CALL
326 if (EXT_MEM_K < (3*1024)) error("Less than 4MB of memory");
328 if ((ALT_MEM_K > EXT_MEM_K ? ALT_MEM_K : EXT_MEM_K) < (3*1024)) error("Less than 4MB of memory");
330 mv->low_buffer_start = output_data = (char *)LOW_BUFFER_START;
331 low_buffer_end = ((unsigned int)real_mode > LOW_BUFFER_MAX
332 ? LOW_BUFFER_MAX : (unsigned int)real_mode) & ~0xfff;
333 low_buffer_size = low_buffer_end - LOW_BUFFER_START;
335 free_mem_end_ptr = (long)high_buffer_start;
336 if ( (0x100000 + low_buffer_size) > ((ulg)high_buffer_start)) {
337 high_buffer_start = (uch *)(0x100000 + low_buffer_size);
338 mv->hcount = 0; /* say: we need not to move high_buffer */
340 else mv->hcount = -1;
341 mv->high_buffer_start = high_buffer_start;
344 static void close_output_buffer_if_we_run_high(struct moveparams *mv)
346 if (bytes_out > low_buffer_size) {
347 mv->lcount = low_buffer_size;
349 mv->hcount = bytes_out - low_buffer_size;
351 mv->lcount = bytes_out;
357 asmlinkage int decompress_kernel(struct moveparams *mv, void *rmode)
361 vidmem = (char *)(((unsigned int)SCREEN_INFO.orig_video_page) << 4);
363 lines = SCREEN_INFO.orig_video_lines;
364 cols = SCREEN_INFO.orig_video_cols;
366 if (free_mem_ptr < 0x100000) setup_normal_output_buffer();
367 else setup_output_buffer_if_we_run_high(mv);
370 puts("Uncompressing Linux... ");
372 puts("Ok, booting the kernel.\n");
373 if (high_loaded) close_output_buffer_if_we_run_high(mv);
377 /* We don't actually check for stack overflows this early. */
378 __asm__(".globl mcount ; mcount: ret\n");