]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/i386/boot/compressed/misc.c
[PATCH] relocatable kernel: Kallsyms generate relocatable symbols
[net-next-2.6.git] / arch / i386 / boot / compressed / misc.c
CommitLineData
1da177e4
LT
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>
7e7f358c 14#include <linux/screen_info.h>
1da177e4
LT
15#include <asm/io.h>
16
17/*
18 * gzip declarations
19 */
20
21#define OF(args) args
22#define STATIC static
23
24#undef memset
25#undef memcpy
1da177e4
LT
26#define memzero(s, n) memset ((s), 0, (n))
27
28typedef unsigned char uch;
29typedef unsigned short ush;
30typedef unsigned long ulg;
31
32#define WSIZE 0x8000 /* Window size must be at least 32k, */
33 /* and a power of two */
34
35static uch *inbuf; /* input buffer */
36static uch window[WSIZE]; /* Sliding window buffer */
37
38static unsigned insize = 0; /* valid bytes in inbuf */
39static unsigned inptr = 0; /* index of next byte to be processed in inbuf */
40static unsigned outcnt = 0; /* bytes in output buffer */
41
42/* gzip flag byte */
43#define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
44#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
45#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
46#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
47#define COMMENT 0x10 /* bit 4 set: file comment present */
48#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
49#define RESERVED 0xC0 /* bit 6,7: reserved */
50
51#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
52
53/* Diagnostic functions */
54#ifdef DEBUG
55# define Assert(cond,msg) {if(!(cond)) error(msg);}
56# define Trace(x) fprintf x
57# define Tracev(x) {if (verbose) fprintf x ;}
58# define Tracevv(x) {if (verbose>1) fprintf x ;}
59# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
60# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
61#else
62# define Assert(cond,msg)
63# define Trace(x)
64# define Tracev(x)
65# define Tracevv(x)
66# define Tracec(c,x)
67# define Tracecv(c,x)
68#endif
69
70static int fill_inbuf(void);
71static void flush_window(void);
72static void error(char *m);
73static void gzip_mark(void **);
74static void gzip_release(void **);
75
76/*
77 * This is set up by the setup-routine at boot-time
78 */
79static unsigned char *real_mode; /* Pointer to real-mode data */
80
81#define RM_EXT_MEM_K (*(unsigned short *)(real_mode + 0x2))
82#ifndef STANDARD_MEMORY_BIOS_CALL
83#define RM_ALT_MEM_K (*(unsigned long *)(real_mode + 0x1e0))
84#endif
85#define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
86
b79c4df7 87extern unsigned char input_data[];
1da177e4
LT
88extern int input_len;
89
90static long bytes_out = 0;
91static uch *output_data;
92static unsigned long output_ptr = 0;
93
94static void *malloc(int size);
95static void free(void *where);
96
b79c4df7
CDH
97static void *memset(void *s, int c, unsigned n);
98static void *memcpy(void *dest, const void *src, unsigned n);
99
1da177e4
LT
100static void putstr(const char *);
101
102extern int end;
103static long free_mem_ptr = (long)&end;
104static long free_mem_end_ptr;
105
106#define INPLACE_MOVE_ROUTINE 0x1000
107#define LOW_BUFFER_START 0x2000
108#define LOW_BUFFER_MAX 0x90000
109#define HEAP_SIZE 0x3000
110static unsigned int low_buffer_end, low_buffer_size;
111static int high_loaded =0;
112static uch *high_buffer_start /* = (uch *)(((ulg)&end) + HEAP_SIZE)*/;
113
114static char *vidmem = (char *)0xb8000;
115static int vidport;
116static int lines, cols;
117
118#ifdef CONFIG_X86_NUMAQ
119static void * xquad_portio = NULL;
120#endif
121
122#include "../../../../lib/inflate.c"
123
124static void *malloc(int size)
125{
126 void *p;
127
128 if (size <0) error("Malloc error");
129 if (free_mem_ptr <= 0) error("Memory error");
130
131 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
132
133 p = (void *)free_mem_ptr;
134 free_mem_ptr += size;
135
136 if (free_mem_ptr >= free_mem_end_ptr)
137 error("Out of memory");
138
139 return p;
140}
141
142static void free(void *where)
143{ /* Don't care */
144}
145
146static void gzip_mark(void **ptr)
147{
148 *ptr = (void *) free_mem_ptr;
149}
150
151static void gzip_release(void **ptr)
152{
153 free_mem_ptr = (long) *ptr;
154}
155
156static void scroll(void)
157{
158 int i;
159
160 memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
161 for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
162 vidmem[i] = ' ';
163}
164
165static void putstr(const char *s)
166{
167 int x,y,pos;
168 char c;
169
170 x = RM_SCREEN_INFO.orig_x;
171 y = RM_SCREEN_INFO.orig_y;
172
173 while ( ( c = *s++ ) != '\0' ) {
174 if ( c == '\n' ) {
175 x = 0;
176 if ( ++y >= lines ) {
177 scroll();
178 y--;
179 }
180 } else {
181 vidmem [ ( x + cols * y ) * 2 ] = c;
182 if ( ++x >= cols ) {
183 x = 0;
184 if ( ++y >= lines ) {
185 scroll();
186 y--;
187 }
188 }
189 }
190 }
191
192 RM_SCREEN_INFO.orig_x = x;
193 RM_SCREEN_INFO.orig_y = y;
194
195 pos = (x + cols * y) * 2; /* Update cursor position */
196 outb_p(14, vidport);
197 outb_p(0xff & (pos >> 9), vidport+1);
198 outb_p(15, vidport);
199 outb_p(0xff & (pos >> 1), vidport+1);
200}
201
b79c4df7 202static void* memset(void* s, int c, unsigned n)
1da177e4
LT
203{
204 int i;
205 char *ss = (char*)s;
206
207 for (i=0;i<n;i++) ss[i] = c;
208 return s;
209}
210
b79c4df7 211static void* memcpy(void* dest, const void* src, unsigned n)
1da177e4
LT
212{
213 int i;
b79c4df7 214 char *d = (char *)dest, *s = (char *)src;
1da177e4 215
b79c4df7
CDH
216 for (i=0;i<n;i++) d[i] = s[i];
217 return dest;
1da177e4
LT
218}
219
220/* ===========================================================================
221 * Fill the input buffer. This is called only when the buffer is empty
222 * and at least one byte is really needed.
223 */
224static int fill_inbuf(void)
225{
226 if (insize != 0) {
227 error("ran out of input data");
228 }
229
230 inbuf = input_data;
231 insize = input_len;
232 inptr = 1;
233 return inbuf[0];
234}
235
236/* ===========================================================================
237 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
238 * (Used for the decompressed data only.)
239 */
240static void flush_window_low(void)
241{
242 ulg c = crc; /* temporary variable */
243 unsigned n;
244 uch *in, *out, ch;
245
246 in = window;
247 out = &output_data[output_ptr];
248 for (n = 0; n < outcnt; n++) {
249 ch = *out++ = *in++;
250 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
251 }
252 crc = c;
253 bytes_out += (ulg)outcnt;
254 output_ptr += (ulg)outcnt;
255 outcnt = 0;
256}
257
258static void flush_window_high(void)
259{
260 ulg c = crc; /* temporary variable */
261 unsigned n;
262 uch *in, ch;
263 in = window;
264 for (n = 0; n < outcnt; n++) {
265 ch = *output_data++ = *in++;
266 if ((ulg)output_data == low_buffer_end) output_data=high_buffer_start;
267 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
268 }
269 crc = c;
270 bytes_out += (ulg)outcnt;
271 outcnt = 0;
272}
273
274static void flush_window(void)
275{
276 if (high_loaded) flush_window_high();
277 else flush_window_low();
278}
279
280static void error(char *x)
281{
282 putstr("\n\n");
283 putstr(x);
284 putstr("\n\n -- System halted");
285
286 while(1); /* Halt */
287}
288
289#define STACK_SIZE (4096)
290
291long user_stack [STACK_SIZE];
292
293struct {
294 long * a;
295 short b;
296 } stack_start = { & user_stack [STACK_SIZE] , __BOOT_DS };
297
298static void setup_normal_output_buffer(void)
299{
300#ifdef STANDARD_MEMORY_BIOS_CALL
301 if (RM_EXT_MEM_K < 1024) error("Less than 2MB of memory");
302#else
303 if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) < 1024) error("Less than 2MB of memory");
304#endif
2a43f3ed 305 output_data = (unsigned char *)CONFIG_PHYSICAL_START; /* Normally Points to 1M */
1da177e4
LT
306 free_mem_end_ptr = (long)real_mode;
307}
308
309struct moveparams {
310 uch *low_buffer_start; int lcount;
311 uch *high_buffer_start; int hcount;
312};
313
314static void setup_output_buffer_if_we_run_high(struct moveparams *mv)
315{
316 high_buffer_start = (uch *)(((ulg)&end) + HEAP_SIZE);
317#ifdef STANDARD_MEMORY_BIOS_CALL
318 if (RM_EXT_MEM_K < (3*1024)) error("Less than 4MB of memory");
319#else
b79c4df7 320 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");
1da177e4 321#endif
b79c4df7 322 mv->low_buffer_start = output_data = (unsigned char *)LOW_BUFFER_START;
1da177e4
LT
323 low_buffer_end = ((unsigned int)real_mode > LOW_BUFFER_MAX
324 ? LOW_BUFFER_MAX : (unsigned int)real_mode) & ~0xfff;
325 low_buffer_size = low_buffer_end - LOW_BUFFER_START;
326 high_loaded = 1;
327 free_mem_end_ptr = (long)high_buffer_start;
2a43f3ed
EB
328 if ( (CONFIG_PHYSICAL_START + low_buffer_size) > ((ulg)high_buffer_start)) {
329 high_buffer_start = (uch *)(CONFIG_PHYSICAL_START + low_buffer_size);
1da177e4
LT
330 mv->hcount = 0; /* say: we need not to move high_buffer */
331 }
332 else mv->hcount = -1;
333 mv->high_buffer_start = high_buffer_start;
334}
335
336static void close_output_buffer_if_we_run_high(struct moveparams *mv)
337{
338 if (bytes_out > low_buffer_size) {
339 mv->lcount = low_buffer_size;
340 if (mv->hcount)
341 mv->hcount = bytes_out - low_buffer_size;
342 } else {
343 mv->lcount = bytes_out;
344 mv->hcount = 0;
345 }
346}
347
1da177e4
LT
348asmlinkage int decompress_kernel(struct moveparams *mv, void *rmode)
349{
350 real_mode = rmode;
351
352 if (RM_SCREEN_INFO.orig_video_mode == 7) {
353 vidmem = (char *) 0xb0000;
354 vidport = 0x3b4;
355 } else {
356 vidmem = (char *) 0xb8000;
357 vidport = 0x3d4;
358 }
359
360 lines = RM_SCREEN_INFO.orig_video_lines;
361 cols = RM_SCREEN_INFO.orig_video_cols;
362
363 if (free_mem_ptr < 0x100000) setup_normal_output_buffer();
364 else setup_output_buffer_if_we_run_high(mv);
365
366 makecrc();
367 putstr("Uncompressing Linux... ");
368 gunzip();
369 putstr("Ok, booting the kernel.\n");
370 if (high_loaded) close_output_buffer_if_we_run_high(mv);
371 return high_loaded;
372}