2 * arch/m32r/boot/compressed/misc.c
4 * This is a collection of several routines from gzip-1.0.3
7 * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
9 * Adapted for SH by Stuart Menefy, Aug 1999
11 * Modified to use standard LinuxSH BIOS by Greg Banks 7Jul2000
13 * 2003-02-12: Support M32R by Takeo Takahashi
14 * This is based on arch/sh/boot/compressed/misc.c.
17 #include <linux/config.h>
18 #include <linux/string.h>
29 #define memzero(s, n) memset ((s), 0, (n))
31 typedef unsigned char uch;
32 typedef unsigned short ush;
33 typedef unsigned long ulg;
35 #define WSIZE 0x8000 /* Window size must be at least 32k, */
36 /* and a power of two */
38 static uch *inbuf; /* input buffer */
39 static uch window[WSIZE]; /* Sliding window buffer */
41 static unsigned insize; /* valid bytes in inbuf */
42 static unsigned inptr; /* index of next byte to be processed in inbuf */
43 static unsigned outcnt; /* bytes in output buffer */
46 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
47 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
48 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
49 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
50 #define COMMENT 0x10 /* bit 4 set: file comment present */
51 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
52 #define RESERVED 0xC0 /* bit 6,7: reserved */
54 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
56 /* Diagnostic functions */
58 # define Assert(cond,msg) {if(!(cond)) error(msg);}
59 # define Trace(x) fprintf x
60 # define Tracev(x) {if (verbose) fprintf x ;}
61 # define Tracevv(x) {if (verbose>1) fprintf x ;}
62 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
63 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
65 # define Assert(cond,msg)
73 static int fill_inbuf(void);
74 static void flush_window(void);
75 static void error(char *m);
76 static void gzip_mark(void **);
77 static void gzip_release(void **);
79 extern char input_data[];
82 static long bytes_out;
83 static uch *output_data;
84 static unsigned long output_ptr;
87 static void *malloc(int size);
88 static void free(void *where);
89 static void error(char *m);
90 static void gzip_mark(void **);
91 static void gzip_release(void **);
93 extern int puts(const char *);
95 extern int _text; /* Defined in vmlinux.lds.S */
97 static unsigned long free_mem_ptr;
98 static unsigned long free_mem_end_ptr;
100 #define HEAP_SIZE 0x10000
102 #include "../../../../lib/inflate.c"
104 static void *malloc(int size)
108 if (size <0) error("Malloc error\n");
109 if (free_mem_ptr == 0) error("Memory error\n");
111 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
113 p = (void *)free_mem_ptr;
114 free_mem_ptr += size;
116 if (free_mem_ptr >= free_mem_end_ptr)
117 error("\nOut of memory\n");
122 static void free(void *where)
126 static void gzip_mark(void **ptr)
128 *ptr = (void *) free_mem_ptr;
131 static void gzip_release(void **ptr)
133 free_mem_ptr = (long) *ptr;
136 void* memset(void* s, int c, size_t n)
141 for (i=0;i<n;i++) ss[i] = c;
145 void* memcpy(void* __dest, __const void* __src,
149 char *d = (char *)__dest, *s = (char *)__src;
151 for (i=0;i<__n;i++) d[i] = s[i];
155 /* ===========================================================================
156 * Fill the input buffer. This is called only when the buffer is empty
157 * and at least one byte is really needed.
159 static int fill_inbuf(void)
162 error("ran out of input data\n");
171 /* ===========================================================================
172 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
173 * (Used for the decompressed data only.)
175 static void flush_window(void)
177 ulg c = crc; /* temporary variable */
182 out = &output_data[output_ptr];
183 for (n = 0; n < outcnt; n++) {
185 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
188 bytes_out += (ulg)outcnt;
189 output_ptr += (ulg)outcnt;
193 static void error(char *x)
197 puts("\n\n -- System halted");
202 #define STACK_SIZE (4096)
203 long user_stack [STACK_SIZE];
204 long* stack_start = &user_stack[STACK_SIZE];
206 /* return decompressed size */
207 long decompress_kernel(void)
214 output_ptr = CONFIG_MEMORY_START + 0x2000;
215 free_mem_ptr = (unsigned long)&_end;
216 free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
219 puts("Uncompressing Linux... ");
221 puts("Ok, booting the kernel.\n");