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