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 "miscsetup.h"
24 #define memzero(s, n) memset ((s), 0, (n))
26 typedef unsigned char uch
;
27 typedef unsigned short ush
;
28 typedef unsigned long ulg
;
30 #define WSIZE 0x8000 /* Window size must be at least 32k, */
31 /* and a power of two */
33 static uch
*inbuf
; /* input buffer */
34 static uch window
[WSIZE
]; /* Sliding window buffer */
36 static unsigned insize
= 0; /* valid bytes in inbuf */
37 static unsigned inptr
= 0; /* index of next byte to be processed in inbuf */
38 static unsigned outcnt
= 0; /* bytes in output buffer */
41 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
42 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
43 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
44 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
45 #define COMMENT 0x10 /* bit 4 set: file comment present */
46 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
47 #define RESERVED 0xC0 /* bit 6,7: reserved */
49 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
51 /* Diagnostic functions */
53 # define Assert(cond,msg) {if(!(cond)) error(msg);}
54 # define Trace(x) fprintf x
55 # define Tracev(x) {if (verbose) fprintf x ;}
56 # define Tracevv(x) {if (verbose>1) fprintf x ;}
57 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
58 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
60 # define Assert(cond,msg)
68 static int fill_inbuf(void);
69 static void flush_window(void);
70 static void error(char *m
);
71 static void gzip_mark(void **);
72 static void gzip_release(void **);
75 * This is set up by the setup-routine at boot-time
77 static unsigned char *real_mode
; /* Pointer to real-mode data */
79 #define EXT_MEM_K (*(unsigned short *)(real_mode + 0x2))
80 #ifndef STANDARD_MEMORY_BIOS_CALL
81 #define ALT_MEM_K (*(unsigned long *)(real_mode + 0x1e0))
83 #define SCREEN_INFO (*(struct screen_info *)(real_mode+0))
85 extern char input_data
[];
88 static long bytes_out
= 0;
89 static uch
*output_data
;
90 static unsigned long output_ptr
= 0;
92 static void *malloc(int size
);
93 static void free(void *where
);
95 static void putstr(const char *);
98 static long free_mem_ptr
= (long)&end
;
99 static long free_mem_end_ptr
;
101 #define INPLACE_MOVE_ROUTINE 0x1000
102 #define LOW_BUFFER_START 0x2000
103 #define LOW_BUFFER_MAX 0x90000
104 #define HEAP_SIZE 0x3000
105 static unsigned int low_buffer_end
, low_buffer_size
;
106 static int high_loaded
=0;
107 static uch
*high_buffer_start
/* = (uch *)(((ulg)&end) + HEAP_SIZE)*/;
109 static char *vidmem
= (char *)0xb8000;
111 static int lines
, cols
;
113 #include "../../../../lib/inflate.c"
115 static void *malloc(int size
)
119 if (size
<0) error("Malloc error");
120 if (free_mem_ptr
<= 0) error("Memory error");
122 free_mem_ptr
= (free_mem_ptr
+ 3) & ~3; /* Align */
124 p
= (void *)free_mem_ptr
;
125 free_mem_ptr
+= size
;
127 if (free_mem_ptr
>= free_mem_end_ptr
)
128 error("Out of memory");
133 static void free(void *where
)
137 static void gzip_mark(void **ptr
)
139 *ptr
= (void *) free_mem_ptr
;
142 static void gzip_release(void **ptr
)
144 free_mem_ptr
= (long) *ptr
;
147 static void scroll(void)
151 memcpy ( vidmem
, vidmem
+ cols
* 2, ( lines
- 1 ) * cols
* 2 );
152 for ( i
= ( lines
- 1 ) * cols
* 2; i
< lines
* cols
* 2; i
+= 2 )
156 static void putstr(const char *s
)
161 x
= SCREEN_INFO
.orig_x
;
162 y
= SCREEN_INFO
.orig_y
;
164 while ( ( c
= *s
++ ) != '\0' ) {
167 if ( ++y
>= lines
) {
172 vidmem
[ ( x
+ cols
* y
) * 2 ] = c
;
175 if ( ++y
>= lines
) {
183 SCREEN_INFO
.orig_x
= x
;
184 SCREEN_INFO
.orig_y
= y
;
186 pos
= (x
+ cols
* y
) * 2; /* Update cursor position */
188 outb_p(0xff & (pos
>> 9), vidport
+1);
190 outb_p(0xff & (pos
>> 1), vidport
+1);
193 void* memset(void* s
, int c
, unsigned n
)
198 for (i
=0;i
<n
;i
++) ss
[i
] = c
;
202 void* memcpy(void* dest
, const void* src
, unsigned n
)
205 char *d
= (char *)dest
, *s
= (char *)src
;
207 for (i
=0;i
<n
;i
++) d
[i
] = s
[i
];
211 /* ===========================================================================
212 * Fill the input buffer. This is called only when the buffer is empty
213 * and at least one byte is really needed.
215 static int fill_inbuf(void)
218 error("ran out of input data");
227 /* ===========================================================================
228 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
229 * (Used for the decompressed data only.)
231 static void flush_window_low(void)
233 ulg c
= crc
; /* temporary variable */
238 out
= &output_data
[output_ptr
];
239 for (n
= 0; n
< outcnt
; n
++) {
241 c
= crc_32_tab
[((int)c
^ ch
) & 0xff] ^ (c
>> 8);
244 bytes_out
+= (ulg
)outcnt
;
245 output_ptr
+= (ulg
)outcnt
;
249 static void flush_window_high(void)
251 ulg c
= crc
; /* temporary variable */
255 for (n
= 0; n
< outcnt
; n
++) {
256 ch
= *output_data
++ = *in
++;
257 if ((ulg
)output_data
== low_buffer_end
) output_data
=high_buffer_start
;
258 c
= crc_32_tab
[((int)c
^ ch
) & 0xff] ^ (c
>> 8);
261 bytes_out
+= (ulg
)outcnt
;
265 static void flush_window(void)
267 if (high_loaded
) flush_window_high();
268 else flush_window_low();
271 static void error(char *x
)
275 putstr("\n\n -- System halted");
280 void setup_normal_output_buffer(void)
282 #ifdef STANDARD_MEMORY_BIOS_CALL
283 if (EXT_MEM_K
< 1024) error("Less than 2MB of memory");
285 if ((ALT_MEM_K
> EXT_MEM_K
? ALT_MEM_K
: EXT_MEM_K
) < 1024) error("Less than 2MB of memory");
287 output_data
= (char *)0x100000; /* Points to 1M */
288 free_mem_end_ptr
= (long)real_mode
;
292 uch
*low_buffer_start
; int lcount
;
293 uch
*high_buffer_start
; int hcount
;
296 void setup_output_buffer_if_we_run_high(struct moveparams
*mv
)
298 high_buffer_start
= (uch
*)(((ulg
)&end
) + HEAP_SIZE
);
299 #ifdef STANDARD_MEMORY_BIOS_CALL
300 if (EXT_MEM_K
< (3*1024)) error("Less than 4MB of memory");
302 if ((ALT_MEM_K
> EXT_MEM_K
? ALT_MEM_K
: EXT_MEM_K
) < (3*1024)) error("Less than 4MB of memory");
304 mv
->low_buffer_start
= output_data
= (char *)LOW_BUFFER_START
;
305 low_buffer_end
= ((unsigned int)real_mode
> LOW_BUFFER_MAX
306 ? LOW_BUFFER_MAX
: (unsigned int)real_mode
) & ~0xfff;
307 low_buffer_size
= low_buffer_end
- LOW_BUFFER_START
;
309 free_mem_end_ptr
= (long)high_buffer_start
;
310 if ( (0x100000 + low_buffer_size
) > ((ulg
)high_buffer_start
)) {
311 high_buffer_start
= (uch
*)(0x100000 + low_buffer_size
);
312 mv
->hcount
= 0; /* say: we need not to move high_buffer */
314 else mv
->hcount
= -1;
315 mv
->high_buffer_start
= high_buffer_start
;
318 void close_output_buffer_if_we_run_high(struct moveparams
*mv
)
320 if (bytes_out
> low_buffer_size
) {
321 mv
->lcount
= low_buffer_size
;
323 mv
->hcount
= bytes_out
- low_buffer_size
;
325 mv
->lcount
= bytes_out
;
330 int decompress_kernel(struct moveparams
*mv
, void *rmode
)
334 if (SCREEN_INFO
.orig_video_mode
== 7) {
335 vidmem
= (char *) 0xb0000;
338 vidmem
= (char *) 0xb8000;
342 lines
= SCREEN_INFO
.orig_video_lines
;
343 cols
= SCREEN_INFO
.orig_video_cols
;
345 if (free_mem_ptr
< 0x100000) setup_normal_output_buffer();
346 else setup_output_buffer_if_we_run_high(mv
);
349 putstr(".\nDecompressing Linux...");
351 putstr("done.\nBooting the kernel.\n");
352 if (high_loaded
) close_output_buffer_if_we_run_high(mv
);