MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / i386 / boot / compressed / misc.c
blob1636ac48fbbe37664e460e4082e5ef69208685ca
1 /*
2 * misc.c
3 *
4 * This is a collection of several routines from gzip-1.0.3
5 * adapted for Linux.
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>
15 #include <asm/io.h>
16 #include <asm/page.h>
18 #undef __BOOT_CS
19 #define __BOOT_CS 0x10
20 #undef __BOOT_DS
21 #define __BOOT_DS 0x18
24 * gzip declarations
27 #define OF(args) args
28 #define STATIC static
30 #undef memset
31 #undef memcpy
32 #define memzero(s, n) memset ((s), 0, (n))
34 typedef unsigned char uch;
35 typedef unsigned short ush;
36 typedef unsigned long ulg;
38 #define WSIZE 0x8000 /* Window size must be at least 32k, */
39 /* and a power of two */
41 static uch *inbuf; /* input buffer */
42 static uch window[WSIZE]; /* Sliding window buffer */
44 static unsigned insize = 0; /* valid bytes in inbuf */
45 static unsigned inptr = 0; /* index of next byte to be processed in inbuf */
46 static unsigned outcnt = 0; /* bytes in output buffer */
48 /* gzip flag byte */
49 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
50 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
51 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
52 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
53 #define COMMENT 0x10 /* bit 4 set: file comment present */
54 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
55 #define RESERVED 0xC0 /* bit 6,7: reserved */
57 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
59 /* Diagnostic functions */
60 #ifdef DEBUG
61 # define Assert(cond,msg) {if(!(cond)) error(msg);}
62 # define Trace(x) fprintf x
63 # define Tracev(x) {if (verbose) fprintf x ;}
64 # define Tracevv(x) {if (verbose>1) fprintf x ;}
65 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
66 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
67 #else
68 # define Assert(cond,msg)
69 # define Trace(x)
70 # define Tracev(x)
71 # define Tracevv(x)
72 # define Tracec(c,x)
73 # define Tracecv(c,x)
74 #endif
76 static int fill_inbuf(void);
77 static void flush_window(void);
78 static void error(char *m);
79 static void gzip_mark(void **);
80 static void gzip_release(void **);
83 * This is set up by the setup-routine at boot-time
85 static unsigned char *real_mode; /* Pointer to real-mode data */
87 #define RM_EXT_MEM_K (*(unsigned short *)(real_mode + 0x2))
88 #ifndef STANDARD_MEMORY_BIOS_CALL
89 #define RM_ALT_MEM_K (*(unsigned long *)(real_mode + 0x1e0))
90 #endif
91 #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
93 extern unsigned char input_data[];
94 extern int input_len;
96 static long bytes_out = 0;
97 static uch *output_data;
98 static unsigned long output_ptr = 0;
100 static void *malloc(int size);
101 static void free(void *where);
103 static void *memset(void *s, int c, unsigned n);
104 static void *memcpy(void *dest, const void *src, unsigned n);
106 static void putstr(const char *);
108 extern int end;
109 static long free_mem_ptr = (long)&end;
110 static long free_mem_end_ptr;
112 #define INPLACE_MOVE_ROUTINE 0x1000
113 #define LOW_BUFFER_START 0x2000
114 #define LOW_BUFFER_MAX 0x90000
115 #define HEAP_SIZE 0x3000
116 static unsigned int low_buffer_end, low_buffer_size;
117 static int high_loaded =0;
118 static uch *high_buffer_start /* = (uch *)(((ulg)&end) + HEAP_SIZE)*/;
120 static char *vidmem = (char *)0xb8000;
121 static int vidport;
122 static int lines, cols;
124 #ifdef CONFIG_X86_NUMAQ
125 static void * xquad_portio = NULL;
126 #endif
128 #include "../../../../lib/inflate.c"
130 static void *malloc(int size)
132 void *p;
134 if (size <0) error("Malloc error");
135 if (free_mem_ptr <= 0) error("Memory error");
137 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
139 p = (void *)free_mem_ptr;
140 free_mem_ptr += size;
142 if (free_mem_ptr >= free_mem_end_ptr)
143 error("Out of memory");
145 return p;
148 static void free(void *where)
149 { /* Don't care */
152 static void gzip_mark(void **ptr)
154 *ptr = (void *) free_mem_ptr;
157 static void gzip_release(void **ptr)
159 free_mem_ptr = (long) *ptr;
162 static void scroll(void)
164 int i;
166 memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
167 for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
168 vidmem[i] = ' ';
171 static void putstr(const char *s)
173 int x,y,pos;
174 char c;
176 x = RM_SCREEN_INFO.orig_x;
177 y = RM_SCREEN_INFO.orig_y;
179 while ( ( c = *s++ ) != '\0' ) {
180 if ( c == '\n' ) {
181 x = 0;
182 if ( ++y >= lines ) {
183 scroll();
184 y--;
186 } else {
187 vidmem [ ( x + cols * y ) * 2 ] = c;
188 if ( ++x >= cols ) {
189 x = 0;
190 if ( ++y >= lines ) {
191 scroll();
192 y--;
198 RM_SCREEN_INFO.orig_x = x;
199 RM_SCREEN_INFO.orig_y = y;
201 pos = (x + cols * y) * 2; /* Update cursor position */
202 outb_p(14, vidport);
203 outb_p(0xff & (pos >> 9), vidport+1);
204 outb_p(15, vidport);
205 outb_p(0xff & (pos >> 1), vidport+1);
208 static void* memset(void* s, int c, unsigned n)
210 int i;
211 char *ss = (char*)s;
213 for (i=0;i<n;i++) ss[i] = c;
214 return s;
217 static void* memcpy(void* dest, const void* src, unsigned n)
219 int i;
220 char *d = (char *)dest, *s = (char *)src;
222 for (i=0;i<n;i++) d[i] = s[i];
223 return dest;
226 /* ===========================================================================
227 * Fill the input buffer. This is called only when the buffer is empty
228 * and at least one byte is really needed.
230 static int fill_inbuf(void)
232 if (insize != 0) {
233 error("ran out of input data");
236 inbuf = input_data;
237 insize = input_len;
238 inptr = 1;
239 return inbuf[0];
242 /* ===========================================================================
243 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
244 * (Used for the decompressed data only.)
246 static void flush_window_low(void)
248 ulg c = crc; /* temporary variable */
249 unsigned n;
250 uch *in, *out, ch;
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);
258 crc = c;
259 bytes_out += (ulg)outcnt;
260 output_ptr += (ulg)outcnt;
261 outcnt = 0;
264 static void flush_window_high(void)
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);
275 crc = c;
276 bytes_out += (ulg)outcnt;
277 outcnt = 0;
280 static void flush_window(void)
282 if (high_loaded) flush_window_high();
283 else flush_window_low();
286 static void error(char *x)
288 putstr("\n\n");
289 putstr(x);
290 putstr("\n\n -- System halted");
292 while(1); /* Halt */
295 #define STACK_SIZE (4096)
297 long user_stack [STACK_SIZE];
299 struct {
300 long * a;
301 short b;
302 } stack_start = { & user_stack [STACK_SIZE] , __BOOT_DS };
304 static void setup_normal_output_buffer(void)
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 = (unsigned char *)__PHYSICAL_START; /* Normally Points to 1M */
312 free_mem_end_ptr = (long)real_mode;
315 struct moveparams {
316 uch *low_buffer_start; int lcount;
317 uch *high_buffer_start; int hcount;
320 static void setup_output_buffer_if_we_run_high(struct moveparams *mv)
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) < (3*1024)) error("Less than 4MB of memory");
327 #endif
328 mv->low_buffer_start = output_data = (unsigned char *)LOW_BUFFER_START;
329 low_buffer_end = ((unsigned int)real_mode > LOW_BUFFER_MAX
330 ? LOW_BUFFER_MAX : (unsigned int)real_mode) & ~0xfff;
331 low_buffer_size = low_buffer_end - LOW_BUFFER_START;
332 high_loaded = 1;
333 free_mem_end_ptr = (long)high_buffer_start;
334 if ( (__PHYSICAL_START + low_buffer_size) > ((ulg)high_buffer_start)) {
335 high_buffer_start = (uch *)(__PHYSICAL_START + low_buffer_size);
336 mv->hcount = 0; /* say: we need not to move high_buffer */
338 else mv->hcount = -1;
339 mv->high_buffer_start = high_buffer_start;
342 static void close_output_buffer_if_we_run_high(struct moveparams *mv)
344 if (bytes_out > low_buffer_size) {
345 mv->lcount = low_buffer_size;
346 if (mv->hcount)
347 mv->hcount = bytes_out - low_buffer_size;
348 } else {
349 mv->lcount = bytes_out;
350 mv->hcount = 0;
354 asmlinkage int decompress_kernel(struct moveparams *mv, void *rmode)
356 real_mode = rmode;
358 if (RM_SCREEN_INFO.orig_video_mode == 7) {
359 vidmem = (char *) 0xb0000;
360 vidport = 0x3b4;
361 } else {
362 vidmem = (char *) 0xb8000;
363 vidport = 0x3d4;
366 lines = RM_SCREEN_INFO.orig_video_lines;
367 cols = RM_SCREEN_INFO.orig_video_cols;
369 if (free_mem_ptr < 0x100000) setup_normal_output_buffer();
370 else setup_output_buffer_if_we_run_high(mv);
372 makecrc();
373 putstr("Uncompressing Linux... ");
374 gunzip();
375 putstr("Ok, booting the kernel.\n");
376 if (high_loaded) close_output_buffer_if_we_run_high(mv);
377 return high_loaded;