Merge ../linus
[pandora-kernel.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/screen_info.h>
15 #include <asm/io.h>
16 #include <asm/page.h>
17
18 /*
19  * gzip declarations
20  */
21
22 #define OF(args)  args
23 #define STATIC static
24
25 #undef memset
26 #undef memcpy
27 #define memzero(s, n)     memset ((s), 0, (n))
28
29 typedef unsigned char  uch;
30 typedef unsigned short ush;
31 typedef unsigned long  ulg;
32
33 #define WSIZE 0x8000            /* Window size must be at least 32k, */
34                                 /* and a power of two */
35
36 static uch *inbuf;           /* input buffer */
37 static uch window[WSIZE];    /* Sliding window buffer */
38
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 */
42
43 /* gzip flag byte */
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 */
51
52 #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
53                 
54 /* Diagnostic functions */
55 #ifdef DEBUG
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 ;}
62 #else
63 #  define Assert(cond,msg)
64 #  define Trace(x)
65 #  define Tracev(x)
66 #  define Tracevv(x)
67 #  define Tracec(c,x)
68 #  define Tracecv(c,x)
69 #endif
70
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 **);
76   
77 /*
78  * This is set up by the setup-routine at boot-time
79  */
80 static unsigned char *real_mode; /* Pointer to real-mode data */
81
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))
85 #endif
86 #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
87
88 extern unsigned char input_data[];
89 extern int input_len;
90
91 static long bytes_out = 0;
92 static uch *output_data;
93 static unsigned long output_ptr = 0;
94
95 static void *malloc(int size);
96 static void free(void *where);
97
98 static void *memset(void *s, int c, unsigned n);
99 static void *memcpy(void *dest, const void *src, unsigned n);
100
101 static void putstr(const char *);
102
103 extern int end;
104 static long free_mem_ptr = (long)&end;
105 static long free_mem_end_ptr;
106
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)*/;
114
115 static char *vidmem = (char *)0xb8000;
116 static int vidport;
117 static int lines, cols;
118
119 #ifdef CONFIG_X86_NUMAQ
120 static void * xquad_portio = NULL;
121 #endif
122
123 #include "../../../../lib/inflate.c"
124
125 static void *malloc(int size)
126 {
127         void *p;
128
129         if (size <0) error("Malloc error");
130         if (free_mem_ptr <= 0) error("Memory error");
131
132         free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
133
134         p = (void *)free_mem_ptr;
135         free_mem_ptr += size;
136
137         if (free_mem_ptr >= free_mem_end_ptr)
138                 error("Out of memory");
139
140         return p;
141 }
142
143 static void free(void *where)
144 {       /* Don't care */
145 }
146
147 static void gzip_mark(void **ptr)
148 {
149         *ptr = (void *) free_mem_ptr;
150 }
151
152 static void gzip_release(void **ptr)
153 {
154         free_mem_ptr = (long) *ptr;
155 }
156  
157 static void scroll(void)
158 {
159         int i;
160
161         memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
162         for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
163                 vidmem[i] = ' ';
164 }
165
166 static void putstr(const char *s)
167 {
168         int x,y,pos;
169         char c;
170
171         x = RM_SCREEN_INFO.orig_x;
172         y = RM_SCREEN_INFO.orig_y;
173
174         while ( ( c = *s++ ) != '\0' ) {
175                 if ( c == '\n' ) {
176                         x = 0;
177                         if ( ++y >= lines ) {
178                                 scroll();
179                                 y--;
180                         }
181                 } else {
182                         vidmem [ ( x + cols * y ) * 2 ] = c; 
183                         if ( ++x >= cols ) {
184                                 x = 0;
185                                 if ( ++y >= lines ) {
186                                         scroll();
187                                         y--;
188                                 }
189                         }
190                 }
191         }
192
193         RM_SCREEN_INFO.orig_x = x;
194         RM_SCREEN_INFO.orig_y = y;
195
196         pos = (x + cols * y) * 2;       /* Update cursor position */
197         outb_p(14, vidport);
198         outb_p(0xff & (pos >> 9), vidport+1);
199         outb_p(15, vidport);
200         outb_p(0xff & (pos >> 1), vidport+1);
201 }
202
203 static void* memset(void* s, int c, unsigned n)
204 {
205         int i;
206         char *ss = (char*)s;
207
208         for (i=0;i<n;i++) ss[i] = c;
209         return s;
210 }
211
212 static void* memcpy(void* dest, const void* src, unsigned n)
213 {
214         int i;
215         char *d = (char *)dest, *s = (char *)src;
216
217         for (i=0;i<n;i++) d[i] = s[i];
218         return dest;
219 }
220
221 /* ===========================================================================
222  * Fill the input buffer. This is called only when the buffer is empty
223  * and at least one byte is really needed.
224  */
225 static int fill_inbuf(void)
226 {
227         if (insize != 0) {
228                 error("ran out of input data");
229         }
230
231         inbuf = input_data;
232         insize = input_len;
233         inptr = 1;
234         return inbuf[0];
235 }
236
237 /* ===========================================================================
238  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
239  * (Used for the decompressed data only.)
240  */
241 static void flush_window_low(void)
242 {
243     ulg c = crc;         /* temporary variable */
244     unsigned n;
245     uch *in, *out, ch;
246     
247     in = window;
248     out = &output_data[output_ptr]; 
249     for (n = 0; n < outcnt; n++) {
250             ch = *out++ = *in++;
251             c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
252     }
253     crc = c;
254     bytes_out += (ulg)outcnt;
255     output_ptr += (ulg)outcnt;
256     outcnt = 0;
257 }
258
259 static void flush_window_high(void)
260 {
261     ulg c = crc;         /* temporary variable */
262     unsigned n;
263     uch *in,  ch;
264     in = window;
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);
269     }
270     crc = c;
271     bytes_out += (ulg)outcnt;
272     outcnt = 0;
273 }
274
275 static void flush_window(void)
276 {
277         if (high_loaded) flush_window_high();
278         else flush_window_low();
279 }
280
281 static void error(char *x)
282 {
283         putstr("\n\n");
284         putstr(x);
285         putstr("\n\n -- System halted");
286
287         while(1);       /* Halt */
288 }
289
290 #define STACK_SIZE (4096)
291
292 long user_stack [STACK_SIZE];
293
294 struct {
295         long * a;
296         short b;
297         } stack_start = { & user_stack [STACK_SIZE] , __BOOT_DS };
298
299 static void setup_normal_output_buffer(void)
300 {
301 #ifdef STANDARD_MEMORY_BIOS_CALL
302         if (RM_EXT_MEM_K < 1024) error("Less than 2MB of memory");
303 #else
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");
305 #endif
306         output_data = (unsigned char *)__PHYSICAL_START; /* Normally Points to 1M */
307         free_mem_end_ptr = (long)real_mode;
308 }
309
310 struct moveparams {
311         uch *low_buffer_start;  int lcount;
312         uch *high_buffer_start; int hcount;
313 };
314
315 static void setup_output_buffer_if_we_run_high(struct moveparams *mv)
316 {
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");
320 #else
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");
322 #endif  
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;
327         high_loaded = 1;
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 */
332         }
333         else mv->hcount = -1;
334         mv->high_buffer_start = high_buffer_start;
335 }
336
337 static void close_output_buffer_if_we_run_high(struct moveparams *mv)
338 {
339         if (bytes_out > low_buffer_size) {
340                 mv->lcount = low_buffer_size;
341                 if (mv->hcount)
342                         mv->hcount = bytes_out - low_buffer_size;
343         } else {
344                 mv->lcount = bytes_out;
345                 mv->hcount = 0;
346         }
347 }
348
349 asmlinkage int decompress_kernel(struct moveparams *mv, void *rmode)
350 {
351         real_mode = rmode;
352
353         if (RM_SCREEN_INFO.orig_video_mode == 7) {
354                 vidmem = (char *) 0xb0000;
355                 vidport = 0x3b4;
356         } else {
357                 vidmem = (char *) 0xb8000;
358                 vidport = 0x3d4;
359         }
360
361         lines = RM_SCREEN_INFO.orig_video_lines;
362         cols = RM_SCREEN_INFO.orig_video_cols;
363
364         if (free_mem_ptr < 0x100000) setup_normal_output_buffer();
365         else setup_output_buffer_if_we_run_high(mv);
366
367         makecrc();
368         putstr("Uncompressing Linux... ");
369         gunzip();
370         putstr("Ok, booting the kernel.\n");
371         if (high_loaded) close_output_buffer_if_we_run_high(mv);
372         return high_loaded;
373 }