Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * misc.c
0004  * 
0005  * This is a collection of several routines from gzip-1.0.3 
0006  * adapted for Linux.
0007  *
0008  * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
0009  *
0010  * Modified for ARM Linux by Russell King
0011  *
0012  * Nicolas Pitre <nico@visuaide.com>  1999/04/14 :
0013  *  For this code to run directly from Flash, all constant variables must
0014  *  be marked with 'const' and all other variables initialized at run-time 
0015  *  only.  This way all non constant variables will end up in the bss segment,
0016  *  which should point to addresses in RAM and cleared to 0 on start.
0017  *  This allows for a much quicker boot time.
0018  *
0019  * Modified for Alpha, from the ARM version, by Jay Estabrook 2003.
0020  */
0021 
0022 #include <linux/kernel.h>
0023 #include <linux/slab.h>
0024 
0025 #include <linux/uaccess.h>
0026 
0027 #define memzero(s,n)    memset ((s),0,(n))
0028 #define puts        srm_printk
0029 extern long srm_printk(const char *, ...)
0030      __attribute__ ((format (printf, 1, 2)));
0031 
0032 /*
0033  * gzip declarations
0034  */
0035 #define OF(args)  args
0036 #define STATIC static
0037 
0038 typedef unsigned char  uch;
0039 typedef unsigned short ush;
0040 typedef unsigned long  ulg;
0041 
0042 #define WSIZE 0x8000        /* Window size must be at least 32k, */
0043                 /* and a power of two */
0044 
0045 static uch *inbuf;      /* input buffer */
0046 static uch *window;     /* Sliding window buffer */
0047 
0048 static unsigned insize;     /* valid bytes in inbuf */
0049 static unsigned inptr;      /* index of next byte to be processed in inbuf */
0050 static unsigned outcnt;     /* bytes in output buffer */
0051 
0052 /* gzip flag byte */
0053 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
0054 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
0055 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
0056 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
0057 #define COMMENT      0x10 /* bit 4 set: file comment present */
0058 #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
0059 #define RESERVED     0xC0 /* bit 6,7:   reserved */
0060 
0061 #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
0062 
0063 /* Diagnostic functions */
0064 #ifdef DEBUG
0065 #  define Assert(cond,msg) {if(!(cond)) error(msg);}
0066 #  define Trace(x) fprintf x
0067 #  define Tracev(x) {if (verbose) fprintf x ;}
0068 #  define Tracevv(x) {if (verbose>1) fprintf x ;}
0069 #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
0070 #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
0071 #else
0072 #  define Assert(cond,msg)
0073 #  define Trace(x)
0074 #  define Tracev(x)
0075 #  define Tracevv(x)
0076 #  define Tracec(c,x)
0077 #  define Tracecv(c,x)
0078 #endif
0079 
0080 static int  fill_inbuf(void);
0081 static void flush_window(void);
0082 static void error(char *m);
0083 
0084 static char *input_data;
0085 static int  input_data_size;
0086 
0087 static uch *output_data;
0088 static ulg output_ptr;
0089 static ulg bytes_out;
0090 
0091 static void error(char *m);
0092 static void gzip_mark(void **);
0093 static void gzip_release(void **);
0094 
0095 extern int end;
0096 static ulg free_mem_ptr;
0097 static ulg free_mem_end_ptr;
0098 
0099 #define HEAP_SIZE 0x3000
0100 
0101 #include "../../../lib/inflate.c"
0102 
0103 /* ===========================================================================
0104  * Fill the input buffer. This is called only when the buffer is empty
0105  * and at least one byte is really needed.
0106  */
0107 int fill_inbuf(void)
0108 {
0109     if (insize != 0)
0110         error("ran out of input data");
0111 
0112     inbuf = input_data;
0113     insize = input_data_size;
0114 
0115     inptr = 1;
0116     return inbuf[0];
0117 }
0118 
0119 /* ===========================================================================
0120  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
0121  * (Used for the decompressed data only.)
0122  */
0123 void flush_window(void)
0124 {
0125     ulg c = crc;
0126     unsigned n;
0127     uch *in, *out, ch;
0128 
0129     in = window;
0130     out = &output_data[output_ptr];
0131     for (n = 0; n < outcnt; n++) {
0132         ch = *out++ = *in++;
0133         c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
0134     }
0135     crc = c;
0136     bytes_out += (ulg)outcnt;
0137     output_ptr += (ulg)outcnt;
0138     outcnt = 0;
0139 /*  puts("."); */
0140 }
0141 
0142 static void error(char *x)
0143 {
0144     puts("\n\n");
0145     puts(x);
0146     puts("\n\n -- System halted");
0147 
0148     while(1);   /* Halt */
0149 }
0150 
0151 unsigned int
0152 decompress_kernel(void *output_start,
0153           void *input_start,
0154           size_t ksize,
0155           size_t kzsize)
0156 {
0157     output_data     = (uch *)output_start;
0158     input_data      = (uch *)input_start;
0159     input_data_size     = kzsize; /* use compressed size */
0160 
0161     /* FIXME FIXME FIXME */
0162     free_mem_ptr        = (ulg)output_start + ksize;
0163     free_mem_end_ptr    = (ulg)output_start + ksize + 0x200000;
0164     /* FIXME FIXME FIXME */
0165 
0166     /* put in temp area to reduce initial footprint */
0167     window = malloc(WSIZE);
0168 
0169     makecrc();
0170 /*  puts("Uncompressing Linux..."); */
0171     gunzip();
0172 /*  puts(" done, booting the kernel.\n"); */
0173     return output_ptr;
0174 }