x86: unify variable names in arch/x86/boot/compressed/misc_??.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / boot / compressed / misc_32.c
blob2b1c22de558a6c93f4f2942790d62baa92e876d2
1 /*
2 * misc.c
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
13 * we have to be careful, because no indirections are allowed here, and
14 * paravirt_ops is a kind of one. As it will only run in baremetal anyway,
15 * we just keep it from happening
17 #undef CONFIG_PARAVIRT
18 #include <linux/linkage.h>
19 #include <linux/screen_info.h>
20 #include <asm/io.h>
21 #include <asm/page.h>
22 #include <asm/boot.h>
24 /* WARNING!!
25 * This code is compiled with -fPIC and it is relocated dynamically
26 * at run time, but no relocation processing is performed.
27 * This means that it is not safe to place pointers in static structures.
31 * Getting to provable safe in place decompression is hard.
32 * Worst case behaviours need to be analyzed.
33 * Background information:
35 * The file layout is:
36 * magic[2]
37 * method[1]
38 * flags[1]
39 * timestamp[4]
40 * extraflags[1]
41 * os[1]
42 * compressed data blocks[N]
43 * crc[4] orig_len[4]
45 * resulting in 18 bytes of non compressed data overhead.
47 * Files divided into blocks
48 * 1 bit (last block flag)
49 * 2 bits (block type)
51 * 1 block occurs every 32K -1 bytes or when there 50% compression has been achieved.
52 * The smallest block type encoding is always used.
54 * stored:
55 * 32 bits length in bytes.
57 * fixed:
58 * magic fixed tree.
59 * symbols.
61 * dynamic:
62 * dynamic tree encoding.
63 * symbols.
66 * The buffer for decompression in place is the length of the
67 * uncompressed data, plus a small amount extra to keep the algorithm safe.
68 * The compressed data is placed at the end of the buffer. The output
69 * pointer is placed at the start of the buffer and the input pointer
70 * is placed where the compressed data starts. Problems will occur
71 * when the output pointer overruns the input pointer.
73 * The output pointer can only overrun the input pointer if the input
74 * pointer is moving faster than the output pointer. A condition only
75 * triggered by data whose compressed form is larger than the uncompressed
76 * form.
78 * The worst case at the block level is a growth of the compressed data
79 * of 5 bytes per 32767 bytes.
81 * The worst case internal to a compressed block is very hard to figure.
82 * The worst case can at least be boundined by having one bit that represents
83 * 32764 bytes and then all of the rest of the bytes representing the very
84 * very last byte.
86 * All of which is enough to compute an amount of extra data that is required
87 * to be safe. To avoid problems at the block level allocating 5 extra bytes
88 * per 32767 bytes of data is sufficient. To avoind problems internal to a block
89 * adding an extra 32767 bytes (the worst case uncompressed block size) is
90 * sufficient, to ensure that in the worst case the decompressed data for
91 * block will stop the byte before the compressed data for a block begins.
92 * To avoid problems with the compressed data's meta information an extra 18
93 * bytes are needed. Leading to the formula:
95 * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
97 * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
98 * Adding 32768 instead of 32767 just makes for round numbers.
99 * Adding the decompressor_size is necessary as it musht live after all
100 * of the data as well. Last I measured the decompressor is about 14K.
101 * 10K of actual data and 4K of bss.
106 * gzip declarations
109 #define OF(args) args
110 #define STATIC static
112 #undef memset
113 #undef memcpy
114 #define memzero(s, n) memset ((s), 0, (n))
116 typedef unsigned char uch;
117 typedef unsigned short ush;
118 typedef unsigned long ulg;
120 #define WSIZE 0x80000000 /* Window size must be at least 32k,
121 * and a power of two
122 * We don't actually have a window just
123 * a huge output buffer so I report
124 * a 2G windows size, as that should
125 * always be larger than our output buffer.
128 static uch *inbuf; /* input buffer */
129 static uch *window; /* Sliding window buffer, (and final output buffer) */
131 static unsigned insize; /* valid bytes in inbuf */
132 static unsigned inptr; /* index of next byte to be processed in inbuf */
133 static unsigned outcnt; /* bytes in output buffer */
135 /* gzip flag byte */
136 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
137 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
138 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
139 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
140 #define COMMENT 0x10 /* bit 4 set: file comment present */
141 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
142 #define RESERVED 0xC0 /* bit 6,7: reserved */
144 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
146 /* Diagnostic functions */
147 #ifdef DEBUG
148 # define Assert(cond,msg) {if(!(cond)) error(msg);}
149 # define Trace(x) fprintf x
150 # define Tracev(x) {if (verbose) fprintf x ;}
151 # define Tracevv(x) {if (verbose>1) fprintf x ;}
152 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
153 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
154 #else
155 # define Assert(cond,msg)
156 # define Trace(x)
157 # define Tracev(x)
158 # define Tracevv(x)
159 # define Tracec(c,x)
160 # define Tracecv(c,x)
161 #endif
163 static int fill_inbuf(void);
164 static void flush_window(void);
165 static void error(char *m);
166 static void gzip_mark(void **);
167 static void gzip_release(void **);
170 * This is set up by the setup-routine at boot-time
172 static unsigned char *real_mode; /* Pointer to real-mode data */
174 #define RM_EXT_MEM_K (*(unsigned short *)(real_mode + 0x2))
175 #ifndef STANDARD_MEMORY_BIOS_CALL
176 #define RM_ALT_MEM_K (*(unsigned long *)(real_mode + 0x1e0))
177 #endif
178 #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
180 extern unsigned char input_data[];
181 extern int input_len;
183 static long bytes_out = 0;
185 static void *malloc(int size);
186 static void free(void *where);
188 static void *memset(void *s, int c, unsigned n);
189 static void *memcpy(void *dest, const void *src, unsigned n);
191 static void putstr(const char *);
193 static unsigned long free_mem_ptr;
194 static unsigned long free_mem_end_ptr;
196 #define HEAP_SIZE 0x4000
198 static char *vidmem = (char *)0xb8000;
199 static int vidport;
200 static int lines, cols;
202 #ifdef CONFIG_X86_NUMAQ
203 void *xquad_portio;
204 #endif
206 #include "../../../../lib/inflate.c"
208 static void *malloc(int size)
210 void *p;
212 if (size <0) error("Malloc error");
213 if (free_mem_ptr <= 0) error("Memory error");
215 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
217 p = (void *)free_mem_ptr;
218 free_mem_ptr += size;
220 if (free_mem_ptr >= free_mem_end_ptr)
221 error("Out of memory");
223 return p;
226 static void free(void *where)
227 { /* Don't care */
230 static void gzip_mark(void **ptr)
232 *ptr = (void *) free_mem_ptr;
235 static void gzip_release(void **ptr)
237 free_mem_ptr = (unsigned long) *ptr;
240 static void scroll(void)
242 int i;
244 memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
245 for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
246 vidmem[i] = ' ';
249 static void putstr(const char *s)
251 int x,y,pos;
252 char c;
254 if (RM_SCREEN_INFO.orig_video_mode == 0 && lines == 0 && cols == 0)
255 return;
257 x = RM_SCREEN_INFO.orig_x;
258 y = RM_SCREEN_INFO.orig_y;
260 while ( ( c = *s++ ) != '\0' ) {
261 if ( c == '\n' ) {
262 x = 0;
263 if ( ++y >= lines ) {
264 scroll();
265 y--;
267 } else {
268 vidmem [(x + cols * y) * 2] = c;
269 if ( ++x >= cols ) {
270 x = 0;
271 if ( ++y >= lines ) {
272 scroll();
273 y--;
279 RM_SCREEN_INFO.orig_x = x;
280 RM_SCREEN_INFO.orig_y = y;
282 pos = (x + cols * y) * 2; /* Update cursor position */
283 outb(14, vidport);
284 outb(0xff & (pos >> 9), vidport+1);
285 outb(15, vidport);
286 outb(0xff & (pos >> 1), vidport+1);
289 static void* memset(void* s, int c, unsigned n)
291 int i;
292 char *ss = s;
294 for (i=0;i<n;i++) ss[i] = c;
295 return s;
298 static void* memcpy(void* dest, const void* src, unsigned n)
300 int i;
301 const char *s = src;
302 char *d = dest;
304 for (i=0;i<n;i++) d[i] = s[i];
305 return dest;
308 /* ===========================================================================
309 * Fill the input buffer. This is called only when the buffer is empty
310 * and at least one byte is really needed.
312 static int fill_inbuf(void)
314 error("ran out of input data");
315 return 0;
318 /* ===========================================================================
319 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
320 * (Used for the decompressed data only.)
322 static void flush_window(void)
324 /* With my window equal to my output buffer
325 * I only need to compute the crc here.
327 ulg c = crc; /* temporary variable */
328 unsigned n;
329 uch *in, ch;
331 in = window;
332 for (n = 0; n < outcnt; n++) {
333 ch = *in++;
334 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
336 crc = c;
337 bytes_out += (ulg)outcnt;
338 outcnt = 0;
341 static void error(char *x)
343 putstr("\n\n");
344 putstr(x);
345 putstr("\n\n -- System halted");
347 while (1)
348 asm("hlt");
351 asmlinkage void decompress_kernel(void *rmode, unsigned long heap,
352 uch *input_data, unsigned long input_len,
353 uch *output)
355 real_mode = rmode;
357 if (RM_SCREEN_INFO.orig_video_mode == 7) {
358 vidmem = (char *) 0xb0000;
359 vidport = 0x3b4;
360 } else {
361 vidmem = (char *) 0xb8000;
362 vidport = 0x3d4;
365 lines = RM_SCREEN_INFO.orig_video_lines;
366 cols = RM_SCREEN_INFO.orig_video_cols;
368 window = output; /* Output buffer (Normally at 1M) */
369 free_mem_ptr = heap; /* Heap */
370 free_mem_end_ptr = heap + HEAP_SIZE;
371 inbuf = input_data; /* Input buffer */
372 insize = input_len;
373 inptr = 0;
375 if ((u32)output & (CONFIG_PHYSICAL_ALIGN -1))
376 error("Destination address not CONFIG_PHYSICAL_ALIGN aligned");
377 if (heap > ((-__PAGE_OFFSET-(512<<20)-1) & 0x7fffffff))
378 error("Destination address too large");
379 #ifndef CONFIG_RELOCATABLE
380 if ((u32)output != LOAD_PHYSICAL_ADDR)
381 error("Wrong destination address");
382 #endif
384 makecrc();
385 putstr("\nDecompressing Linux... ");
386 gunzip();
387 putstr("done.\nBooting the kernel.\n");
388 return;