root/arch/i386/boot/compressed/misc.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. malloc
  2. free
  3. gzip_mark
  4. gzip_release
  5. scroll
  6. puts
  7. memset
  8. memcpy
  9. fill_inbuf
  10. flush_window
  11. error
  12. gzip_mark
  13. gzip_release
  14. main
  15. decompress_kernel

   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  */
  10 
  11 #include <string.h>
  12 
  13 #include <asm/segment.h>
  14 #include <asm/io.h>
  15 
  16 /*
  17  * gzip declarations
  18  */
  19 
  20 #define OF(args)  args
  21 #define STATIC static
  22 
  23 #define memzero(s, n)     memset ((s), 0, (n))
  24 
  25 typedef unsigned char  uch;
  26 typedef unsigned short ush;
  27 typedef unsigned long  ulg;
  28 
  29 #define WSIZE 0x8000            /* Window size must be at least 32k, */
  30                                 /* and a power of two */
  31 
  32 static uch *inbuf;           /* input buffer */
  33 static uch window[WSIZE];    /* Sliding window buffer */
  34 
  35 static unsigned insize = 0;  /* valid bytes in inbuf */
  36 static unsigned inptr = 0;   /* index of next byte to be processed in inbuf */
  37 static unsigned outcnt = 0;  /* bytes in output buffer */
  38 
  39 /* gzip flag byte */
  40 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
  41 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  42 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
  43 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
  44 #define COMMENT      0x10 /* bit 4 set: file comment present */
  45 #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
  46 #define RESERVED     0xC0 /* bit 6,7:   reserved */
  47 
  48 #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
  49                 
  50 /* Diagnostic functions */
  51 #ifdef DEBUG
  52 #  define Assert(cond,msg) {if(!(cond)) error(msg);}
  53 #  define Trace(x) fprintf x
  54 #  define Tracev(x) {if (verbose) fprintf x ;}
  55 #  define Tracevv(x) {if (verbose>1) fprintf x ;}
  56 #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
  57 #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
  58 #else
  59 #  define Assert(cond,msg)
  60 #  define Trace(x)
  61 #  define Tracev(x)
  62 #  define Tracevv(x)
  63 #  define Tracec(c,x)
  64 #  define Tracecv(c,x)
  65 #endif
  66 
  67 static int  fill_inbuf(void);
  68 static void flush_window(void);
  69 static void error(char *m);
  70 static void gzip_mark(void **);
  71 static void gzip_release(void **);
  72   
  73 /*
  74  * These are set up by the setup-routine at boot-time:
  75  */
  76 
  77 struct screen_info {
  78         unsigned char  orig_x;
  79         unsigned char  orig_y;
  80         unsigned char  unused1[2];
  81         unsigned short orig_video_page;
  82         unsigned char  orig_video_mode;
  83         unsigned char  orig_video_cols;
  84         unsigned short unused2;
  85         unsigned short orig_video_ega_bx;
  86         unsigned short unused3;
  87         unsigned char  orig_video_lines;
  88         unsigned char  orig_video_isVGA;
  89 };
  90 
  91 /*
  92  * This is set up by the setup-routine at boot-time
  93  */
  94 #define EXT_MEM_K (*(unsigned short *)0x90002)
  95 #define DRIVE_INFO (*(struct drive_info *)0x90080)
  96 #define SCREEN_INFO (*(struct screen_info *)0x90000)
  97 #define RAMDISK_SIZE (*(unsigned short *)0x901F8)
  98 #define ORIG_ROOT_DEV (*(unsigned short *)0x901FC)
  99 #define AUX_DEVICE_INFO (*(unsigned char *)0x901FF)
 100 
 101 extern char input_data[];
 102 extern int input_len;
 103 
 104 static long bytes_out = 0;
 105 static uch *output_data;
 106 static unsigned long output_ptr = 0;
 107  
 108 static void *malloc(int size);
 109 static void free(void *where);
 110 static void error(char *m);
 111 static void gzip_mark(void **);
 112 static void gzip_release(void **);
 113  
 114 #ifndef STANDALONE_DEBUG
 115 static void puts(const char *);
 116   
 117 extern int end;
 118 static long free_mem_ptr = (long)&end;
 119 
 120 static char *vidmem = (char *)0xb8000;
 121 static int vidport;
 122 static int lines, cols;
 123 
 124 #include "../../../../lib/inflate.c"
 125 
 126 static void *malloc(int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 127 {
 128         void *p;
 129 
 130         if (size <0) error("Malloc error\n");
 131         if (free_mem_ptr <= 0) error("Memory error\n");
 132 
 133         free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
 134 
 135         p = (void *)free_mem_ptr;
 136         free_mem_ptr += size;
 137 
 138         if (free_mem_ptr >= 0x90000)
 139                 error("\nOut of memory\n");
 140 
 141         return p;
 142 }
 143 
 144 static void free(void *where)
     /* [previous][next][first][last][top][bottom][index][help] */
 145 {       /* Don't care */
 146 }
 147 
 148 static void gzip_mark(void **ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 149 {
 150         *ptr = (void *) free_mem_ptr;
 151 }
 152 
 153 static void gzip_release(void **ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 154 {
 155         free_mem_ptr = (long) *ptr;
 156 }
 157  
 158 static void scroll()
     /* [previous][next][first][last][top][bottom][index][help] */
 159 {
 160         int i;
 161 
 162         memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
 163         for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
 164                 vidmem[i] = ' ';
 165 }
 166 
 167 static void puts(const char *s)
     /* [previous][next][first][last][top][bottom][index][help] */
 168 {
 169         int x,y,pos;
 170         char c;
 171 
 172         x = SCREEN_INFO.orig_x;
 173         y = SCREEN_INFO.orig_y;
 174 
 175         while ( ( c = *s++ ) != '\0' ) {
 176                 if ( c == '\n' ) {
 177                         x = 0;
 178                         if ( ++y >= lines ) {
 179                                 scroll();
 180                                 y--;
 181                         }
 182                 } else {
 183                         vidmem [ ( x + cols * y ) * 2 ] = c; 
 184                         if ( ++x >= cols ) {
 185                                 x = 0;
 186                                 if ( ++y >= lines ) {
 187                                         scroll();
 188                                         y--;
 189                                 }
 190                         }
 191                 }
 192         }
 193 
 194         SCREEN_INFO.orig_x = x;
 195         SCREEN_INFO.orig_y = y;
 196 
 197         pos = (x + cols * y) * 2;       /* Update cursor position */
 198         outb_p(14, vidport);
 199         outb_p(0xff & (pos >> 9), vidport+1);
 200         outb_p(15, vidport);
 201         outb_p(0xff & (pos >> 1), vidport+1);
 202 }
 203 
 204 __ptr_t memset(__ptr_t s, int c, size_t n)
     /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206         int i;
 207         char *ss = (char*)s;
 208 
 209         for (i=0;i<n;i++) ss[i] = c;
 210 }
 211 
 212 __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
     /* [previous][next][first][last][top][bottom][index][help] */
 213                             size_t __n)
 214 {
 215         int i;
 216         char *d = (char *)__dest, *s = (char *)__src;
 217 
 218         for (i=0;i<__n;i++) d[i] = s[i];
 219 }
 220 #endif
 221 
 222 /* ===========================================================================
 223  * Fill the input buffer. This is called only when the buffer is empty
 224  * and at least one byte is really needed.
 225  */
 226 static int fill_inbuf()
     /* [previous][next][first][last][top][bottom][index][help] */
 227 {
 228         if (insize != 0) {
 229                 error("ran out of input data\n");
 230         }
 231 
 232         inbuf = input_data;
 233         insize = input_len;
 234         inptr = 1;
 235         return inbuf[0];
 236 }
 237 
 238 /* ===========================================================================
 239  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
 240  * (Used for the decompressed data only.)
 241  */
 242 static void flush_window()
     /* [previous][next][first][last][top][bottom][index][help] */
 243 {
 244     ulg c = crc;         /* temporary variable */
 245     unsigned n;
 246     uch *in, *out, ch;
 247     
 248     in = window;
 249     out = &output_data[output_ptr]; 
 250     for (n = 0; n < outcnt; n++) {
 251             ch = *out++ = *in++;
 252             c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
 253     }
 254     crc = c;
 255     bytes_out += (ulg)outcnt;
 256     output_ptr += (ulg)outcnt;
 257     outcnt = 0;
 258 }
 259 
 260 static void error(char *x)
     /* [previous][next][first][last][top][bottom][index][help] */
 261 {
 262         puts("\n\n");
 263         puts(x);
 264         puts("\n\n -- System halted");
 265 
 266         while(1);       /* Halt */
 267 }
 268 
 269 #define STACK_SIZE (4096)
 270 
 271 long user_stack [STACK_SIZE];
 272 
 273 struct {
 274         long * a;
 275         short b;
 276         } stack_start = { & user_stack [STACK_SIZE] , KERNEL_DS };
 277 
 278 #ifdef STANDALONE_DEBUG
 279 
 280 static void gzip_mark(void **ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 281 {
 282 }
 283 
 284 static void gzip_release(void **ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 285 {
 286 }
 287 
 288 char output_buffer[1024 * 800];
 289 
 290 int
 291 main(argc, argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 292         int     argc;
 293         char    **argv;
 294 {
 295         output_data = output_buffer;
 296 
 297         makecrc();
 298         puts("Uncompressing Linux...");
 299         gunzip();
 300         puts("done.\n");
 301         return 0;
 302 }
 303 
 304 #else
 305 
 306 void decompress_kernel()
     /* [previous][next][first][last][top][bottom][index][help] */
 307 {
 308         if (SCREEN_INFO.orig_video_mode == 7) {
 309                 vidmem = (char *) 0xb0000;
 310                 vidport = 0x3b4;
 311         } else {
 312                 vidmem = (char *) 0xb8000;
 313                 vidport = 0x3d4;
 314         }
 315 
 316         lines = SCREEN_INFO.orig_video_lines;
 317         cols = SCREEN_INFO.orig_video_cols;
 318 
 319         if (EXT_MEM_K < 1024) error("<2M of mem\n");
 320 
 321         output_data = (char *)0x100000; /* Points to 1M */
 322         makecrc();
 323         puts("Uncompressing Linux...");
 324         gunzip();
 325         puts("done.\nNow booting the kernel\n");
 326 }
 327 #endif
 328 
 329 
 330 
 331 

/* [previous][next][first][last][top][bottom][index][help] */