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