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