linux/arch/m32r/boot/compressed/misc.c
<<
>>
Prefs
   1/*
   2 * arch/m32r/boot/compressed/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 *
   9 * Adapted for SH by Stuart Menefy, Aug 1999
  10 *
  11 * 2003-02-12:  Support M32R by Takeo Takahashi
  12 *              This is based on arch/sh/boot/compressed/misc.c.
  13 */
  14
  15#include <linux/string.h>
  16
  17/*
  18 * gzip declarations
  19 */
  20
  21#define OF(args)  args
  22#define STATIC static
  23
  24#undef memset
  25#undef memcpy
  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
  76static unsigned char *input_data;
  77static int input_len;
  78
  79static long bytes_out = 0;
  80static uch *output_data;
  81static unsigned long output_ptr = 0;
  82
  83#include "m32r_sio.c"
  84
  85static void *malloc(int size);
  86static void free(void *where);
  87
  88static unsigned long free_mem_ptr;
  89static unsigned long free_mem_end_ptr;
  90
  91#define HEAP_SIZE             0x10000
  92
  93#include "../../../../lib/inflate.c"
  94
  95static void *malloc(int size)
  96{
  97        void *p;
  98
  99        if (size <0) error("Malloc error");
 100        if (free_mem_ptr == 0) error("Memory error");
 101
 102        free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
 103
 104        p = (void *)free_mem_ptr;
 105        free_mem_ptr += size;
 106
 107        if (free_mem_ptr >= free_mem_end_ptr)
 108                error("Out of memory");
 109
 110        return p;
 111}
 112
 113static void free(void *where)
 114{       /* Don't care */
 115}
 116
 117static void gzip_mark(void **ptr)
 118{
 119        *ptr = (void *) free_mem_ptr;
 120}
 121
 122static void gzip_release(void **ptr)
 123{
 124        free_mem_ptr = (long) *ptr;
 125}
 126
 127void* memset(void* s, int c, size_t n)
 128{
 129        int i;
 130        char *ss = (char*)s;
 131
 132        for (i=0;i<n;i++) ss[i] = c;
 133        return s;
 134}
 135
 136void* memcpy(void* __dest, __const void* __src,
 137                            size_t __n)
 138{
 139        int i;
 140        char *d = (char *)__dest, *s = (char *)__src;
 141
 142        for (i=0;i<__n;i++) d[i] = s[i];
 143        return __dest;
 144}
 145
 146/* ===========================================================================
 147 * Fill the input buffer. This is called only when the buffer is empty
 148 * and at least one byte is really needed.
 149 */
 150static int fill_inbuf(void)
 151{
 152        if (insize != 0) {
 153                error("ran out of input data");
 154        }
 155
 156        inbuf = input_data;
 157        insize = input_len;
 158        inptr = 1;
 159        return inbuf[0];
 160}
 161
 162/* ===========================================================================
 163 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
 164 * (Used for the decompressed data only.)
 165 */
 166static void flush_window(void)
 167{
 168    ulg c = crc;         /* temporary variable */
 169    unsigned n;
 170    uch *in, *out, ch;
 171
 172    in = window;
 173    out = &output_data[output_ptr];
 174    for (n = 0; n < outcnt; n++) {
 175            ch = *out++ = *in++;
 176            c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
 177    }
 178    crc = c;
 179    bytes_out += (ulg)outcnt;
 180    output_ptr += (ulg)outcnt;
 181    outcnt = 0;
 182}
 183
 184static void error(char *x)
 185{
 186        puts("\n\n");
 187        puts(x);
 188        puts("\n\n -- System halted");
 189
 190        while(1);       /* Halt */
 191}
 192
 193/* return decompressed size */
 194void
 195decompress_kernel(int mmu_on, unsigned char *zimage_data,
 196                  unsigned int zimage_len, unsigned long heap)
 197{
 198        output_data = (unsigned char *)CONFIG_MEMORY_START + 0x2000
 199                + (mmu_on ? 0x80000000 : 0);
 200        free_mem_ptr = heap;
 201        free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
 202        input_data = zimage_data;
 203        input_len = zimage_len;
 204
 205        makecrc();
 206        puts("Uncompressing Linux... ");
 207        gunzip();
 208        puts("Ok, booting the kernel.\n");
 209}
 210