vserver 2.0 rc7
[linux-2.6.git] / arch / i386 / boot / compressed / misc.c
1 /*
2  * misc.c
3  * 
4  * This is a collection of several routines from gzip-1.0.3 
5  * adapted for Linux.
6  *
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
10  */
11
12 #include <linux/linkage.h>
13 #include <linux/vmalloc.h>
14 #include <linux/tty.h>
15 #include <asm/io.h>
16
17 /*
18  * gzip declarations
19  */
20
21 #define OF(args)  args
22 #define STATIC static
23
24 #undef memset
25 #undef memcpy
26
27 /*
28  * Why do we do this? Don't ask me..
29  *
30  * Incomprehensible are the ways of bootloaders.
31  */
32 static void* memset(void *, int, size_t);
33 static void* memcpy(void *, __const void *, size_t);
34 #define memzero(s, n)     memset ((s), 0, (n))
35
36 typedef unsigned char  uch;
37 typedef unsigned short ush;
38 typedef unsigned long  ulg;
39
40 #define WSIZE 0x8000            /* Window size must be at least 32k, */
41                                 /* and a power of two */
42
43 static uch *inbuf;           /* input buffer */
44 static uch window[WSIZE];    /* Sliding window buffer */
45
46 static unsigned insize = 0;  /* valid bytes in inbuf */
47 static unsigned inptr = 0;   /* index of next byte to be processed in inbuf */
48 static unsigned outcnt = 0;  /* bytes in output buffer */
49
50 /* gzip flag byte */
51 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ASCII text */
52 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
53 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
54 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
55 #define COMMENT      0x10 /* bit 4 set: file comment present */
56 #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
57 #define RESERVED     0xC0 /* bit 6,7:   reserved */
58
59 #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
60                 
61 /* Diagnostic functions */
62 #ifdef DEBUG
63 #  define Assert(cond,msg) {if(!(cond)) error(msg);}
64 #  define Trace(x) fprintf x
65 #  define Tracev(x) {if (verbose) fprintf x ;}
66 #  define Tracevv(x) {if (verbose>1) fprintf x ;}
67 #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
68 #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
69 #else
70 #  define Assert(cond,msg)
71 #  define Trace(x)
72 #  define Tracev(x)
73 #  define Tracevv(x)
74 #  define Tracec(c,x)
75 #  define Tracecv(c,x)
76 #endif
77
78 static int  fill_inbuf(void);
79 static void flush_window(void);
80 static void error(char *m);
81 static void gzip_mark(void **);
82 static void gzip_release(void **);
83   
84 /*
85  * This is set up by the setup-routine at boot-time
86  */
87 static unsigned char *real_mode; /* Pointer to real-mode data */
88
89 #define RM_EXT_MEM_K   (*(unsigned short *)(real_mode + 0x2))
90 #ifndef STANDARD_MEMORY_BIOS_CALL
91 #define RM_ALT_MEM_K   (*(unsigned long *)(real_mode + 0x1e0))
92 #endif
93 #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
94
95 extern char input_data[];
96 extern int input_len;
97
98 static long bytes_out = 0;
99 static uch *output_data;
100 static unsigned long output_ptr = 0;
101
102 static void *malloc(int size);
103 static void free(void *where);
104
105 static void putstr(const char *);
106
107 extern int end;
108 static long free_mem_ptr = (long)&end;
109 static long free_mem_end_ptr;
110
111 #define INPLACE_MOVE_ROUTINE  0x1000
112 #define LOW_BUFFER_START      0x2000
113 #define LOW_BUFFER_MAX       0x90000
114 #define HEAP_SIZE             0x3000
115 static unsigned int low_buffer_end, low_buffer_size;
116 static int high_loaded =0;
117 static uch *high_buffer_start /* = (uch *)(((ulg)&end) + HEAP_SIZE)*/;
118
119 static char *vidmem = (char *)0xb8000;
120 static int vidport;
121 static int lines, cols;
122
123 #ifdef CONFIG_X86_NUMAQ
124 static void * xquad_portio = NULL;
125 #endif
126
127 #include "../../../../lib/inflate.c"
128
129 static void *malloc(int size)
130 {
131         void *p;
132
133         if (size <0) error("Malloc error");
134         if (free_mem_ptr <= 0) error("Memory error");
135
136         free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
137
138         p = (void *)free_mem_ptr;
139         free_mem_ptr += size;
140
141         if (free_mem_ptr >= free_mem_end_ptr)
142                 error("Out of memory");
143
144         return p;
145 }
146
147 static void free(void *where)
148 {       /* Don't care */
149 }
150
151 static void gzip_mark(void **ptr)
152 {
153         *ptr = (void *) free_mem_ptr;
154 }
155
156 static void gzip_release(void **ptr)
157 {
158         free_mem_ptr = (long) *ptr;
159 }
160  
161 static void scroll(void)
162 {
163         int i;
164
165         memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
166         for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
167                 vidmem[i] = ' ';
168 }
169
170 static void putstr(const char *s)
171 {
172         int x,y,pos;
173         char c;
174
175         x = RM_SCREEN_INFO.orig_x;
176         y = RM_SCREEN_INFO.orig_y;
177
178         while ( ( c = *s++ ) != '\0' ) {
179                 if ( c == '\n' ) {
180                         x = 0;
181                         if ( ++y >= lines ) {
182                                 scroll();
183                                 y--;
184                         }
185                 } else {
186                         vidmem [ ( x + cols * y ) * 2 ] = c; 
187                         if ( ++x >= cols ) {
188                                 x = 0;
189                                 if ( ++y >= lines ) {
190                                         scroll();
191                                         y--;
192                                 }
193                         }
194                 }
195         }
196
197         RM_SCREEN_INFO.orig_x = x;
198         RM_SCREEN_INFO.orig_y = y;
199
200         pos = (x + cols * y) * 2;       /* Update cursor position */
201         outb_p(14, vidport);
202         outb_p(0xff & (pos >> 9), vidport+1);
203         outb_p(15, vidport);
204         outb_p(0xff & (pos >> 1), vidport+1);
205 }
206
207 static void* memset(void* s, int c, size_t n)
208 {
209         int i;
210         char *ss = (char*)s;
211
212         for (i=0;i<n;i++) ss[i] = c;
213         return s;
214 }
215
216 static void* memcpy(void* __dest, __const void* __src,
217                             size_t __n)
218 {
219         int i;
220         char *d = (char *)__dest, *s = (char *)__src;
221
222         for (i=0;i<__n;i++) d[i] = s[i];
223         return __dest;
224 }
225
226 /* ===========================================================================
227  * Fill the input buffer. This is called only when the buffer is empty
228  * and at least one byte is really needed.
229  */
230 static int fill_inbuf(void)
231 {
232         if (insize != 0) {
233                 error("ran out of input data");
234         }
235
236         inbuf = input_data;
237         insize = input_len;
238         inptr = 1;
239         return inbuf[0];
240 }
241
242 /* ===========================================================================
243  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
244  * (Used for the decompressed data only.)
245  */
246 static void flush_window_low(void)
247 {
248     ulg c = crc;         /* temporary variable */
249     unsigned n;
250     uch *in, *out, ch;
251     
252     in = window;
253     out = &output_data[output_ptr]; 
254     for (n = 0; n < outcnt; n++) {
255             ch = *out++ = *in++;
256             c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
257     }
258     crc = c;
259     bytes_out += (ulg)outcnt;
260     output_ptr += (ulg)outcnt;
261     outcnt = 0;
262 }
263
264 static void flush_window_high(void)
265 {
266     ulg c = crc;         /* temporary variable */
267     unsigned n;
268     uch *in,  ch;
269     in = window;
270     for (n = 0; n < outcnt; n++) {
271         ch = *output_data++ = *in++;
272         if ((ulg)output_data == low_buffer_end) output_data=high_buffer_start;
273         c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
274     }
275     crc = c;
276     bytes_out += (ulg)outcnt;
277     outcnt = 0;
278 }
279
280 static void flush_window(void)
281 {
282         if (high_loaded) flush_window_high();
283         else flush_window_low();
284 }
285
286 static void error(char *x)
287 {
288         putstr("\n\n");
289         putstr(x);
290         putstr("\n\n -- System halted");
291
292         while(1);       /* Halt */
293 }
294
295 #define STACK_SIZE (4096)
296
297 long user_stack [STACK_SIZE];
298
299 struct {
300         long * a;
301         short b;
302         } stack_start = { & user_stack [STACK_SIZE] , __BOOT_DS };
303
304 static void setup_normal_output_buffer(void)
305 {
306 #ifdef STANDARD_MEMORY_BIOS_CALL
307         if (RM_EXT_MEM_K < 1024) error("Less than 2MB of memory");
308 #else
309         if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) < 1024) error("Less than 2MB of memory");
310 #endif
311         output_data = (char *)0x100000; /* Points to 1M */
312         free_mem_end_ptr = (long)real_mode;
313 }
314
315 struct moveparams {
316         uch *low_buffer_start;  int lcount;
317         uch *high_buffer_start; int hcount;
318 };
319
320 static void setup_output_buffer_if_we_run_high(struct moveparams *mv)
321 {
322         high_buffer_start = (uch *)(((ulg)&end) + HEAP_SIZE);
323 #ifdef STANDARD_MEMORY_BIOS_CALL
324         if (RM_EXT_MEM_K < (3*1024)) error("Less than 4MB of memory");
325 #else
326         if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) <
327                         (3*1024))
328                 error("Less than 4MB of memory");
329 #endif  
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;
334         high_loaded = 1;
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 */
339         }
340         else mv->hcount = -1;
341         mv->high_buffer_start = high_buffer_start;
342 }
343
344 static void close_output_buffer_if_we_run_high(struct moveparams *mv)
345 {
346         if (bytes_out > low_buffer_size) {
347                 mv->lcount = low_buffer_size;
348                 if (mv->hcount)
349                         mv->hcount = bytes_out - low_buffer_size;
350         } else {
351                 mv->lcount = bytes_out;
352                 mv->hcount = 0;
353         }
354 }
355
356
357 asmlinkage int decompress_kernel(struct moveparams *mv, void *rmode)
358 {
359         real_mode = rmode;
360
361         if (RM_SCREEN_INFO.orig_video_mode == 7) {
362                 vidmem = (char *) 0xb0000;
363                 vidport = 0x3b4;
364         } else {
365                 vidmem = (char *) 0xb8000;
366                 vidport = 0x3d4;
367         }
368
369         lines = RM_SCREEN_INFO.orig_video_lines;
370         cols = RM_SCREEN_INFO.orig_video_cols;
371
372         if (free_mem_ptr < 0x100000) setup_normal_output_buffer();
373         else setup_output_buffer_if_we_run_high(mv);
374
375         makecrc();
376         putstr("Uncompressing Linux... ");
377         gunzip();
378         putstr("Ok, booting the kernel.\n");
379         if (high_loaded) close_output_buffer_if_we_run_high(mv);
380         return high_loaded;
381 }