[PATCH] i386: Relocatable kernel support
[powerpc.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 /* WARNING!!
19  * This code is compiled with -fPIC and it is relocated dynamically
20  * at run time, but no relocation processing is performed.
21  * This means that it is not safe to place pointers in static structures.
22  */
23
24 /*
25  * Getting to provable safe in place decompression is hard.
26  * Worst case behaviours need to be analized.
27  * Background information:
28  *
29  * The file layout is:
30  *    magic[2]
31  *    method[1]
32  *    flags[1]
33  *    timestamp[4]
34  *    extraflags[1]
35  *    os[1]
36  *    compressed data blocks[N]
37  *    crc[4] orig_len[4]
38  *
39  * resulting in 18 bytes of non compressed data overhead.
40  *
41  * Files divided into blocks
42  * 1 bit (last block flag)
43  * 2 bits (block type)
44  *
45  * 1 block occurs every 32K -1 bytes or when there 50% compression has been achieved.
46  * The smallest block type encoding is always used.
47  *
48  * stored:
49  *    32 bits length in bytes.
50  *
51  * fixed:
52  *    magic fixed tree.
53  *    symbols.
54  *
55  * dynamic:
56  *    dynamic tree encoding.
57  *    symbols.
58  *
59  *
60  * The buffer for decompression in place is the length of the
61  * uncompressed data, plus a small amount extra to keep the algorithm safe.
62  * The compressed data is placed at the end of the buffer.  The output
63  * pointer is placed at the start of the buffer and the input pointer
64  * is placed where the compressed data starts.  Problems will occur
65  * when the output pointer overruns the input pointer.
66  *
67  * The output pointer can only overrun the input pointer if the input
68  * pointer is moving faster than the output pointer.  A condition only
69  * triggered by data whose compressed form is larger than the uncompressed
70  * form.
71  *
72  * The worst case at the block level is a growth of the compressed data
73  * of 5 bytes per 32767 bytes.
74  *
75  * The worst case internal to a compressed block is very hard to figure.
76  * The worst case can at least be boundined by having one bit that represents
77  * 32764 bytes and then all of the rest of the bytes representing the very
78  * very last byte.
79  *
80  * All of which is enough to compute an amount of extra data that is required
81  * to be safe.  To avoid problems at the block level allocating 5 extra bytes
82  * per 32767 bytes of data is sufficient.  To avoind problems internal to a block
83  * adding an extra 32767 bytes (the worst case uncompressed block size) is
84  * sufficient, to ensure that in the worst case the decompressed data for
85  * block will stop the byte before the compressed data for a block begins.
86  * To avoid problems with the compressed data's meta information an extra 18
87  * bytes are needed.  Leading to the formula:
88  *
89  * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
90  *
91  * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
92  * Adding 32768 instead of 32767 just makes for round numbers.
93  * Adding the decompressor_size is necessary as it musht live after all
94  * of the data as well.  Last I measured the decompressor is about 14K.
95  * 10K of actuall data and 4K of bss.
96  *
97  */
98
99 /*
100  * gzip declarations
101  */
102
103 #define OF(args)  args
104 #define STATIC static
105
106 #undef memset
107 #undef memcpy
108 #define memzero(s, n)     memset ((s), 0, (n))
109
110 typedef unsigned char  uch;
111 typedef unsigned short ush;
112 typedef unsigned long  ulg;
113
114 #define WSIZE 0x80000000        /* Window size must be at least 32k,
115                                  * and a power of two
116                                  * We don't actually have a window just
117                                  * a huge output buffer so I report
118                                  * a 2G windows size, as that should
119                                  * always be larger than our output buffer.
120                                  */
121
122 static uch *inbuf;      /* input buffer */
123 static uch *window;     /* Sliding window buffer, (and final output buffer) */
124
125 static unsigned insize;  /* valid bytes in inbuf */
126 static unsigned inptr;   /* index of next byte to be processed in inbuf */
127 static unsigned outcnt;  /* bytes in output buffer */
128
129 /* gzip flag byte */
130 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ASCII text */
131 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
132 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
133 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
134 #define COMMENT      0x10 /* bit 4 set: file comment present */
135 #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
136 #define RESERVED     0xC0 /* bit 6,7:   reserved */
137
138 #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
139                 
140 /* Diagnostic functions */
141 #ifdef DEBUG
142 #  define Assert(cond,msg) {if(!(cond)) error(msg);}
143 #  define Trace(x) fprintf x
144 #  define Tracev(x) {if (verbose) fprintf x ;}
145 #  define Tracevv(x) {if (verbose>1) fprintf x ;}
146 #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
147 #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
148 #else
149 #  define Assert(cond,msg)
150 #  define Trace(x)
151 #  define Tracev(x)
152 #  define Tracevv(x)
153 #  define Tracec(c,x)
154 #  define Tracecv(c,x)
155 #endif
156
157 static int  fill_inbuf(void);
158 static void flush_window(void);
159 static void error(char *m);
160 static void gzip_mark(void **);
161 static void gzip_release(void **);
162   
163 /*
164  * This is set up by the setup-routine at boot-time
165  */
166 static unsigned char *real_mode; /* Pointer to real-mode data */
167
168 #define RM_EXT_MEM_K   (*(unsigned short *)(real_mode + 0x2))
169 #ifndef STANDARD_MEMORY_BIOS_CALL
170 #define RM_ALT_MEM_K   (*(unsigned long *)(real_mode + 0x1e0))
171 #endif
172 #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
173
174 extern unsigned char input_data[];
175 extern int input_len;
176
177 static long bytes_out = 0;
178
179 static void *malloc(int size);
180 static void free(void *where);
181
182 static void *memset(void *s, int c, unsigned n);
183 static void *memcpy(void *dest, const void *src, unsigned n);
184
185 static void putstr(const char *);
186
187 static unsigned long free_mem_ptr;
188 static unsigned long free_mem_end_ptr;
189
190 #define HEAP_SIZE             0x3000
191
192 static char *vidmem = (char *)0xb8000;
193 static int vidport;
194 static int lines, cols;
195
196 #ifdef CONFIG_X86_NUMAQ
197 static void * xquad_portio = NULL;
198 #endif
199
200 #include "../../../../lib/inflate.c"
201
202 static void *malloc(int size)
203 {
204         void *p;
205
206         if (size <0) error("Malloc error");
207         if (free_mem_ptr <= 0) error("Memory error");
208
209         free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
210
211         p = (void *)free_mem_ptr;
212         free_mem_ptr += size;
213
214         if (free_mem_ptr >= free_mem_end_ptr)
215                 error("Out of memory");
216
217         return p;
218 }
219
220 static void free(void *where)
221 {       /* Don't care */
222 }
223
224 static void gzip_mark(void **ptr)
225 {
226         *ptr = (void *) free_mem_ptr;
227 }
228
229 static void gzip_release(void **ptr)
230 {
231         free_mem_ptr = (unsigned long) *ptr;
232 }
233  
234 static void scroll(void)
235 {
236         int i;
237
238         memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
239         for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
240                 vidmem[i] = ' ';
241 }
242
243 static void putstr(const char *s)
244 {
245         int x,y,pos;
246         char c;
247
248         x = RM_SCREEN_INFO.orig_x;
249         y = RM_SCREEN_INFO.orig_y;
250
251         while ( ( c = *s++ ) != '\0' ) {
252                 if ( c == '\n' ) {
253                         x = 0;
254                         if ( ++y >= lines ) {
255                                 scroll();
256                                 y--;
257                         }
258                 } else {
259                         vidmem [ ( x + cols * y ) * 2 ] = c;
260                         if ( ++x >= cols ) {
261                                 x = 0;
262                                 if ( ++y >= lines ) {
263                                         scroll();
264                                         y--;
265                                 }
266                         }
267                 }
268         }
269
270         RM_SCREEN_INFO.orig_x = x;
271         RM_SCREEN_INFO.orig_y = y;
272
273         pos = (x + cols * y) * 2;       /* Update cursor position */
274         outb_p(14, vidport);
275         outb_p(0xff & (pos >> 9), vidport+1);
276         outb_p(15, vidport);
277         outb_p(0xff & (pos >> 1), vidport+1);
278 }
279
280 static void* memset(void* s, int c, unsigned n)
281 {
282         int i;
283         char *ss = (char*)s;
284
285         for (i=0;i<n;i++) ss[i] = c;
286         return s;
287 }
288
289 static void* memcpy(void* dest, const void* src, unsigned n)
290 {
291         int i;
292         char *d = (char *)dest, *s = (char *)src;
293
294         for (i=0;i<n;i++) d[i] = s[i];
295         return dest;
296 }
297
298 /* ===========================================================================
299  * Fill the input buffer. This is called only when the buffer is empty
300  * and at least one byte is really needed.
301  */
302 static int fill_inbuf(void)
303 {
304         error("ran out of input data");
305         return 0;
306 }
307
308 /* ===========================================================================
309  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
310  * (Used for the decompressed data only.)
311  */
312 static void flush_window(void)
313 {
314         /* With my window equal to my output buffer
315          * I only need to compute the crc here.
316          */
317         ulg c = crc;         /* temporary variable */
318         unsigned n;
319         uch *in, ch;
320
321         in = window;
322         for (n = 0; n < outcnt; n++) {
323                 ch = *in++;
324                 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
325         }
326         crc = c;
327         bytes_out += (ulg)outcnt;
328         outcnt = 0;
329 }
330
331 static void error(char *x)
332 {
333         putstr("\n\n");
334         putstr(x);
335         putstr("\n\n -- System halted");
336
337         while(1);       /* Halt */
338 }
339
340 asmlinkage void decompress_kernel(void *rmode, unsigned long end,
341                         uch *input_data, unsigned long input_len, uch *output)
342 {
343         real_mode = rmode;
344
345         if (RM_SCREEN_INFO.orig_video_mode == 7) {
346                 vidmem = (char *) 0xb0000;
347                 vidport = 0x3b4;
348         } else {
349                 vidmem = (char *) 0xb8000;
350                 vidport = 0x3d4;
351         }
352
353         lines = RM_SCREEN_INFO.orig_video_lines;
354         cols = RM_SCREEN_INFO.orig_video_cols;
355
356         window = output;        /* Output buffer (Normally at 1M) */
357         free_mem_ptr     = end; /* Heap  */
358         free_mem_end_ptr = end + HEAP_SIZE;
359         inbuf  = input_data;    /* Input buffer */
360         insize = input_len;
361         inptr  = 0;
362
363         if (((u32)output - CONFIG_PHYSICAL_START) & 0x3fffff)
364                 error("Destination address not 4M aligned");
365         if (end > ((-__PAGE_OFFSET-(512 <<20)-1) & 0x7fffffff))
366                 error("Destination address too large");
367 #ifndef CONFIG_RELOCATABLE
368         if ((u32)output != CONFIG_PHYSICAL_START)
369                 error("Wrong destination address");
370 #endif
371
372         makecrc();
373         putstr("Uncompressing Linux... ");
374         gunzip();
375         putstr("Ok, booting the kernel.\n");
376         return;
377 }