]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * misc.c | |
818a08f8 IC |
3 | * |
4 | * This is a collection of several routines from gzip-1.0.3 | |
1da177e4 LT |
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 | ||
818a08f8 IC |
12 | /* |
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 | |
16 | */ | |
d3561b7f | 17 | #undef CONFIG_PARAVIRT |
1da177e4 | 18 | #include <linux/linkage.h> |
7e7f358c | 19 | #include <linux/screen_info.h> |
1da177e4 | 20 | #include <asm/io.h> |
968de4f0 | 21 | #include <asm/page.h> |
e69f202d | 22 | #include <asm/boot.h> |
968de4f0 EB |
23 | |
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. | |
28 | */ | |
29 | ||
30 | /* | |
31 | * Getting to provable safe in place decompression is hard. | |
27b46d76 | 32 | * Worst case behaviours need to be analyzed. |
968de4f0 EB |
33 | * Background information: |
34 | * | |
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] | |
44 | * | |
45 | * resulting in 18 bytes of non compressed data overhead. | |
46 | * | |
47 | * Files divided into blocks | |
48 | * 1 bit (last block flag) | |
49 | * 2 bits (block type) | |
50 | * | |
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. | |
53 | * | |
54 | * stored: | |
55 | * 32 bits length in bytes. | |
56 | * | |
57 | * fixed: | |
58 | * magic fixed tree. | |
59 | * symbols. | |
60 | * | |
61 | * dynamic: | |
62 | * dynamic tree encoding. | |
63 | * symbols. | |
64 | * | |
65 | * | |
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. | |
72 | * | |
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. | |
77 | * | |
78 | * The worst case at the block level is a growth of the compressed data | |
79 | * of 5 bytes per 32767 bytes. | |
80 | * | |
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. | |
85 | * | |
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: | |
94 | * | |
95 | * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size. | |
96 | * | |
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. | |
27b46d76 | 101 | * 10K of actual data and 4K of bss. |
968de4f0 EB |
102 | * |
103 | */ | |
1da177e4 LT |
104 | |
105 | /* | |
106 | * gzip declarations | |
107 | */ | |
108 | ||
109 | #define OF(args) args | |
110 | #define STATIC static | |
111 | ||
112 | #undef memset | |
113 | #undef memcpy | |
1da177e4 LT |
114 | #define memzero(s, n) memset ((s), 0, (n)) |
115 | ||
116 | typedef unsigned char uch; | |
117 | typedef unsigned short ush; | |
118 | typedef unsigned long ulg; | |
119 | ||
968de4f0 EB |
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. | |
126 | */ | |
1da177e4 | 127 | |
968de4f0 EB |
128 | static uch *inbuf; /* input buffer */ |
129 | static uch *window; /* Sliding window buffer, (and final output buffer) */ | |
1da177e4 | 130 | |
968de4f0 EB |
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 */ | |
1da177e4 LT |
134 | |
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 */ | |
143 | ||
144 | #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf()) | |
145 | ||
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 | |
162 | ||
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 **); | |
168 | ||
169 | /* | |
170 | * This is set up by the setup-routine at boot-time | |
171 | */ | |
172 | static unsigned char *real_mode; /* Pointer to real-mode data */ | |
173 | ||
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)) | |
179 | ||
b79c4df7 | 180 | extern unsigned char input_data[]; |
1da177e4 LT |
181 | extern int input_len; |
182 | ||
183 | static long bytes_out = 0; | |
1da177e4 LT |
184 | |
185 | static void *malloc(int size); | |
186 | static void free(void *where); | |
187 | ||
b79c4df7 CDH |
188 | static void *memset(void *s, int c, unsigned n); |
189 | static void *memcpy(void *dest, const void *src, unsigned n); | |
190 | ||
1da177e4 LT |
191 | static void putstr(const char *); |
192 | ||
968de4f0 EB |
193 | static unsigned long free_mem_ptr; |
194 | static unsigned long free_mem_end_ptr; | |
1da177e4 | 195 | |
35c74226 | 196 | #define HEAP_SIZE 0x4000 |
1da177e4 LT |
197 | |
198 | static char *vidmem = (char *)0xb8000; | |
199 | static int vidport; | |
200 | static int lines, cols; | |
201 | ||
202 | #ifdef CONFIG_X86_NUMAQ | |
d5d2448d | 203 | void *xquad_portio; |
1da177e4 LT |
204 | #endif |
205 | ||
206 | #include "../../../../lib/inflate.c" | |
207 | ||
208 | static void *malloc(int size) | |
209 | { | |
210 | void *p; | |
211 | ||
212 | if (size <0) error("Malloc error"); | |
213 | if (free_mem_ptr <= 0) error("Memory error"); | |
214 | ||
215 | free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */ | |
216 | ||
217 | p = (void *)free_mem_ptr; | |
218 | free_mem_ptr += size; | |
219 | ||
220 | if (free_mem_ptr >= free_mem_end_ptr) | |
221 | error("Out of memory"); | |
222 | ||
223 | return p; | |
224 | } | |
225 | ||
226 | static void free(void *where) | |
227 | { /* Don't care */ | |
228 | } | |
229 | ||
230 | static void gzip_mark(void **ptr) | |
231 | { | |
232 | *ptr = (void *) free_mem_ptr; | |
233 | } | |
234 | ||
235 | static void gzip_release(void **ptr) | |
236 | { | |
968de4f0 | 237 | free_mem_ptr = (unsigned long) *ptr; |
1da177e4 LT |
238 | } |
239 | ||
240 | static void scroll(void) | |
241 | { | |
242 | int i; | |
243 | ||
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] = ' '; | |
247 | } | |
248 | ||
249 | static void putstr(const char *s) | |
250 | { | |
251 | int x,y,pos; | |
252 | char c; | |
253 | ||
a24e7851 RR |
254 | if (RM_SCREEN_INFO.orig_video_mode == 0 && lines == 0 && cols == 0) |
255 | return; | |
256 | ||
1da177e4 LT |
257 | x = RM_SCREEN_INFO.orig_x; |
258 | y = RM_SCREEN_INFO.orig_y; | |
259 | ||
260 | while ( ( c = *s++ ) != '\0' ) { | |
261 | if ( c == '\n' ) { | |
262 | x = 0; | |
263 | if ( ++y >= lines ) { | |
264 | scroll(); | |
265 | y--; | |
266 | } | |
267 | } else { | |
818a08f8 | 268 | vidmem [(x + cols * y) * 2] = c; |
1da177e4 LT |
269 | if ( ++x >= cols ) { |
270 | x = 0; | |
271 | if ( ++y >= lines ) { | |
272 | scroll(); | |
273 | y--; | |
274 | } | |
275 | } | |
276 | } | |
277 | } | |
278 | ||
279 | RM_SCREEN_INFO.orig_x = x; | |
280 | RM_SCREEN_INFO.orig_y = y; | |
281 | ||
282 | pos = (x + cols * y) * 2; /* Update cursor position */ | |
b02aae9c RH |
283 | outb(14, vidport); |
284 | outb(0xff & (pos >> 9), vidport+1); | |
285 | outb(15, vidport); | |
286 | outb(0xff & (pos >> 1), vidport+1); | |
1da177e4 LT |
287 | } |
288 | ||
b79c4df7 | 289 | static void* memset(void* s, int c, unsigned n) |
1da177e4 LT |
290 | { |
291 | int i; | |
ade1af77 | 292 | char *ss = s; |
1da177e4 LT |
293 | |
294 | for (i=0;i<n;i++) ss[i] = c; | |
295 | return s; | |
296 | } | |
297 | ||
b79c4df7 | 298 | static void* memcpy(void* dest, const void* src, unsigned n) |
1da177e4 LT |
299 | { |
300 | int i; | |
ade1af77 JE |
301 | const char *s = src; |
302 | char *d = dest; | |
1da177e4 | 303 | |
b79c4df7 CDH |
304 | for (i=0;i<n;i++) d[i] = s[i]; |
305 | return dest; | |
1da177e4 LT |
306 | } |
307 | ||
308 | /* =========================================================================== | |
309 | * Fill the input buffer. This is called only when the buffer is empty | |
310 | * and at least one byte is really needed. | |
311 | */ | |
312 | static int fill_inbuf(void) | |
313 | { | |
968de4f0 EB |
314 | error("ran out of input data"); |
315 | return 0; | |
1da177e4 LT |
316 | } |
317 | ||
318 | /* =========================================================================== | |
319 | * Write the output window window[0..outcnt-1] and update crc and bytes_out. | |
320 | * (Used for the decompressed data only.) | |
321 | */ | |
1da177e4 LT |
322 | static void flush_window(void) |
323 | { | |
968de4f0 EB |
324 | /* With my window equal to my output buffer |
325 | * I only need to compute the crc here. | |
326 | */ | |
327 | ulg c = crc; /* temporary variable */ | |
328 | unsigned n; | |
329 | uch *in, ch; | |
330 | ||
331 | in = window; | |
332 | for (n = 0; n < outcnt; n++) { | |
333 | ch = *in++; | |
334 | c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8); | |
335 | } | |
336 | crc = c; | |
337 | bytes_out += (ulg)outcnt; | |
338 | outcnt = 0; | |
1da177e4 LT |
339 | } |
340 | ||
341 | static void error(char *x) | |
342 | { | |
343 | putstr("\n\n"); | |
344 | putstr(x); | |
345 | putstr("\n\n -- System halted"); | |
346 | ||
ff3cf856 IM |
347 | while (1) |
348 | asm("hlt"); | |
1da177e4 LT |
349 | } |
350 | ||
968de4f0 | 351 | asmlinkage void decompress_kernel(void *rmode, unsigned long end, |
818a08f8 IC |
352 | uch *input_data, unsigned long input_len, |
353 | uch *output) | |
1da177e4 LT |
354 | { |
355 | real_mode = rmode; | |
356 | ||
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; | |
363 | } | |
364 | ||
365 | lines = RM_SCREEN_INFO.orig_video_lines; | |
366 | cols = RM_SCREEN_INFO.orig_video_cols; | |
367 | ||
818a08f8 IC |
368 | window = output; /* Output buffer (Normally at 1M) */ |
369 | free_mem_ptr = end; /* Heap */ | |
968de4f0 | 370 | free_mem_end_ptr = end + HEAP_SIZE; |
818a08f8 | 371 | inbuf = input_data; /* Input buffer */ |
968de4f0 EB |
372 | insize = input_len; |
373 | inptr = 0; | |
374 | ||
e69f202d VG |
375 | if ((u32)output & (CONFIG_PHYSICAL_ALIGN -1)) |
376 | error("Destination address not CONFIG_PHYSICAL_ALIGN aligned"); | |
968de4f0 EB |
377 | if (end > ((-__PAGE_OFFSET-(512 <<20)-1) & 0x7fffffff)) |
378 | error("Destination address too large"); | |
379 | #ifndef CONFIG_RELOCATABLE | |
e69f202d | 380 | if ((u32)output != LOAD_PHYSICAL_ADDR) |
968de4f0 EB |
381 | error("Wrong destination address"); |
382 | #endif | |
1da177e4 LT |
383 | |
384 | makecrc(); | |
6b3c0426 | 385 | putstr("\nDecompressing Linux... "); |
1da177e4 | 386 | gunzip(); |
6b3c0426 | 387 | putstr("done.\nBooting the kernel.\n"); |
968de4f0 | 388 | return; |
1da177e4 | 389 | } |