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>
27 #define memzero(s, n) memset ((s), 0, (n))
29 typedef unsigned char uch;
30 typedef unsigned short ush;
31 typedef unsigned long ulg;
33 #define WSIZE 0x8000 /* Window size must be at least 32k, */
34 /* and a power of two */
36 static uch *inbuf; /* input buffer */
37 static uch window[WSIZE]; /* Sliding window buffer */
39 static unsigned insize = 0; /* valid bytes in inbuf */
40 static unsigned inptr = 0; /* index of next byte to be processed in inbuf */
41 static unsigned outcnt = 0; /* bytes in output buffer */
44 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
45 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
46 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
47 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
48 #define COMMENT 0x10 /* bit 4 set: file comment present */
49 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
50 #define RESERVED 0xC0 /* bit 6,7: reserved */
52 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
54 /* Diagnostic functions */
56 # define Assert(cond,msg) {if(!(cond)) error(msg);}
57 # define Trace(x) fprintf x
58 # define Tracev(x) {if (verbose) fprintf x ;}
59 # define Tracevv(x) {if (verbose>1) fprintf x ;}
60 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
61 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
63 # define Assert(cond,msg)
71 static int fill_inbuf(void);
72 static void flush_window(void);
73 static void error(char *m);
74 static void gzip_mark(void **);
75 static void gzip_release(void **);
78 * This is set up by the setup-routine at boot-time
80 static unsigned char *real_mode; /* Pointer to real-mode data */
82 #define RM_EXT_MEM_K (*(unsigned short *)(real_mode + 0x2))
83 #ifndef STANDARD_MEMORY_BIOS_CALL
84 #define RM_ALT_MEM_K (*(unsigned long *)(real_mode + 0x1e0))
86 #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
88 extern unsigned char input_data[];
91 static long bytes_out = 0;
92 static uch *output_data;
93 static unsigned long output_ptr = 0;
95 static void *malloc(int size);
96 static void free(void *where);
98 static void *memset(void *s, int c, unsigned n);
99 static void *memcpy(void *dest, const void *src, unsigned n);
101 static void putstr(const char *);
104 static long free_mem_ptr = (long)&end;
105 static long free_mem_end_ptr;
107 #define INPLACE_MOVE_ROUTINE 0x1000
108 #define LOW_BUFFER_START 0x2000
109 #define LOW_BUFFER_MAX 0x90000
110 #define HEAP_SIZE 0x3000
111 static unsigned int low_buffer_end, low_buffer_size;
112 static int high_loaded =0;
113 static uch *high_buffer_start /* = (uch *)(((ulg)&end) + HEAP_SIZE)*/;
115 static char *vidmem = (char *)0xb8000;
117 static int lines, cols;
119 #ifdef CONFIG_X86_NUMAQ
120 static void * xquad_portio = NULL;
123 #include "../../../../lib/inflate.c"
125 static void *malloc(int size)
129 if (size <0) error("Malloc error");
130 if (free_mem_ptr <= 0) error("Memory error");
132 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
134 p = (void *)free_mem_ptr;
135 free_mem_ptr += size;
137 if (free_mem_ptr >= free_mem_end_ptr)
138 error("Out of memory");
143 static void free(void *where)
147 static void gzip_mark(void **ptr)
149 *ptr = (void *) free_mem_ptr;
152 static void gzip_release(void **ptr)
154 free_mem_ptr = (long) *ptr;
157 static void scroll(void)
161 memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
162 for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
166 static void putstr(const char *s)
171 x = RM_SCREEN_INFO.orig_x;
172 y = RM_SCREEN_INFO.orig_y;
174 while ( ( c = *s++ ) != '\0' ) {
177 if ( ++y >= lines ) {
182 vidmem [ ( x + cols * y ) * 2 ] = c;
185 if ( ++y >= lines ) {
193 RM_SCREEN_INFO.orig_x = x;
194 RM_SCREEN_INFO.orig_y = y;
196 pos = (x + cols * y) * 2; /* Update cursor position */
198 outb_p(0xff & (pos >> 9), vidport+1);
200 outb_p(0xff & (pos >> 1), vidport+1);
203 static void* memset(void* s, int c, unsigned n)
208 for (i=0;i<n;i++) ss[i] = c;
212 static void* memcpy(void* dest, const void* src, unsigned n)
215 char *d = (char *)dest, *s = (char *)src;
217 for (i=0;i<n;i++) d[i] = s[i];
221 /* ===========================================================================
222 * Fill the input buffer. This is called only when the buffer is empty
223 * and at least one byte is really needed.
225 static int fill_inbuf(void)
228 error("ran out of input data");
237 /* ===========================================================================
238 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
239 * (Used for the decompressed data only.)
241 static void flush_window_low(void)
243 ulg c = crc; /* temporary variable */
248 out = &output_data[output_ptr];
249 for (n = 0; n < outcnt; n++) {
251 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
254 bytes_out += (ulg)outcnt;
255 output_ptr += (ulg)outcnt;
259 static void flush_window_high(void)
261 ulg c = crc; /* temporary variable */
265 for (n = 0; n < outcnt; n++) {
266 ch = *output_data++ = *in++;
267 if ((ulg)output_data == low_buffer_end) output_data=high_buffer_start;
268 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
271 bytes_out += (ulg)outcnt;
275 static void flush_window(void)
277 if (high_loaded) flush_window_high();
278 else flush_window_low();
281 static void error(char *x)
285 putstr("\n\n -- System halted");
290 #define STACK_SIZE (4096)
292 long user_stack [STACK_SIZE];
297 } stack_start = { & user_stack [STACK_SIZE] , __BOOT_DS };
299 static void setup_normal_output_buffer(void)
301 #ifdef STANDARD_MEMORY_BIOS_CALL
302 if (RM_EXT_MEM_K < 1024) error("Less than 2MB of memory");
304 if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) < 1024) error("Less than 2MB of memory");
306 output_data = (unsigned char *)__PHYSICAL_START; /* Normally Points to 1M */
307 free_mem_end_ptr = (long)real_mode;
311 uch *low_buffer_start; int lcount;
312 uch *high_buffer_start; int hcount;
315 static void setup_output_buffer_if_we_run_high(struct moveparams *mv)
317 high_buffer_start = (uch *)(((ulg)&end) + HEAP_SIZE);
318 #ifdef STANDARD_MEMORY_BIOS_CALL
319 if (RM_EXT_MEM_K < (3*1024)) error("Less than 4MB of memory");
321 if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) < (3*1024)) error("Less than 4MB of memory");
323 mv->low_buffer_start = output_data = (unsigned char *)LOW_BUFFER_START;
324 low_buffer_end = ((unsigned int)real_mode > LOW_BUFFER_MAX
325 ? LOW_BUFFER_MAX : (unsigned int)real_mode) & ~0xfff;
326 low_buffer_size = low_buffer_end - LOW_BUFFER_START;
328 free_mem_end_ptr = (long)high_buffer_start;
329 if ( (__PHYSICAL_START + low_buffer_size) > ((ulg)high_buffer_start)) {
330 high_buffer_start = (uch *)(__PHYSICAL_START + low_buffer_size);
331 mv->hcount = 0; /* say: we need not to move high_buffer */
333 else mv->hcount = -1;
334 mv->high_buffer_start = high_buffer_start;
337 static void close_output_buffer_if_we_run_high(struct moveparams *mv)
339 if (bytes_out > low_buffer_size) {
340 mv->lcount = low_buffer_size;
342 mv->hcount = bytes_out - low_buffer_size;
344 mv->lcount = bytes_out;
349 asmlinkage int decompress_kernel(struct moveparams *mv, void *rmode)
353 if (RM_SCREEN_INFO.orig_video_mode == 7) {
354 vidmem = (char *) 0xb0000;
357 vidmem = (char *) 0xb8000;
361 lines = RM_SCREEN_INFO.orig_video_lines;
362 cols = RM_SCREEN_INFO.orig_video_cols;
364 if (free_mem_ptr < 0x100000) setup_normal_output_buffer();
365 else setup_output_buffer_if_we_run_high(mv);
368 putstr("Uncompressing Linux... ");
370 putstr("Ok, booting the kernel.\n");
371 if (high_loaded) close_output_buffer_if_we_run_high(mv);