Initial commit
[wrt350n-kernel.git] / arch / h8300 / boot / compressed / misc.c
blob845074588af0aacf98d9b16d2d63590669c65b04
1 /*
2 * arch/h8300/boot/compressed/misc.c
4 * This is a collection of several routines from gzip-1.0.3
5 * adapted for Linux.
7 * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
9 * Adapted for h8300 by Yoshinori Sato 2006
12 #include <asm/uaccess.h>
15 * gzip declarations
18 #define OF(args) args
19 #define STATIC static
21 #undef memset
22 #undef memcpy
23 #define memzero(s, n) memset ((s), 0, (n))
25 typedef unsigned char uch;
26 typedef unsigned short ush;
27 typedef unsigned long ulg;
29 #define WSIZE 0x8000 /* Window size must be at least 32k, */
30 /* and a power of two */
32 static uch *inbuf; /* input buffer */
33 static uch window[WSIZE]; /* Sliding window buffer */
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 */
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 */
48 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
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
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 **);
73 extern char input_data[];
74 extern int input_len;
76 static long bytes_out = 0;
77 static uch *output_data;
78 static unsigned long output_ptr = 0;
80 static void *malloc(int size);
81 static void free(void *where);
82 static void error(char *m);
83 static void gzip_mark(void **);
84 static void gzip_release(void **);
86 int puts(const char *);
88 extern int _text; /* Defined in vmlinux.lds.S */
89 extern int _end;
90 static unsigned long free_mem_ptr;
91 static unsigned long free_mem_end_ptr;
93 #define HEAP_SIZE 0x10000
95 #include "../../../../lib/inflate.c"
97 #define SCR *((volatile unsigned char *)0xffff8a)
98 #define TDR *((volatile unsigned char *)0xffff8b)
99 #define SSR *((volatile unsigned char *)0xffff8c)
101 static void *malloc(int size)
103 void *p;
105 if (size <0) error("Malloc error");
106 if (free_mem_ptr == 0) error("Memory error");
108 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
110 p = (void *)free_mem_ptr;
111 free_mem_ptr += size;
113 if (free_mem_ptr >= free_mem_end_ptr)
114 error("Out of memory");
116 return p;
119 static void free(void *where)
120 { /* Don't care */
123 static void gzip_mark(void **ptr)
125 *ptr = (void *) free_mem_ptr;
128 static void gzip_release(void **ptr)
130 free_mem_ptr = (long) *ptr;
133 int puts(const char *s)
135 return 0;
138 void* memset(void* s, int c, size_t n)
140 int i;
141 char *ss = (char*)s;
143 for (i=0;i<n;i++) ss[i] = c;
144 return s;
147 void* memcpy(void* __dest, __const void* __src,
148 size_t __n)
150 int i;
151 char *d = (char *)__dest, *s = (char *)__src;
153 for (i=0;i<__n;i++) d[i] = s[i];
154 return __dest;
157 /* ===========================================================================
158 * Fill the input buffer. This is called only when the buffer is empty
159 * and at least one byte is really needed.
161 static int fill_inbuf(void)
163 if (insize != 0) {
164 error("ran out of input data");
167 inbuf = input_data;
168 insize = input_len;
169 inptr = 1;
170 return inbuf[0];
173 /* ===========================================================================
174 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
175 * (Used for the decompressed data only.)
177 static void flush_window(void)
179 ulg c = crc; /* temporary variable */
180 unsigned n;
181 uch *in, *out, ch;
183 in = window;
184 out = &output_data[output_ptr];
185 for (n = 0; n < outcnt; n++) {
186 ch = *out++ = *in++;
187 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
189 crc = c;
190 bytes_out += (ulg)outcnt;
191 output_ptr += (ulg)outcnt;
192 outcnt = 0;
195 static void error(char *x)
197 puts("\n\n");
198 puts(x);
199 puts("\n\n -- System halted");
201 while(1); /* Halt */
204 #define STACK_SIZE (4096)
205 long user_stack [STACK_SIZE];
206 long* stack_start = &user_stack[STACK_SIZE];
208 void decompress_kernel(void)
210 output_data = 0;
211 output_ptr = (unsigned long)0x400000;
212 free_mem_ptr = (unsigned long)&_end;
213 free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
215 makecrc();
216 puts("Uncompressing Linux... ");
217 gunzip();
218 puts("Ok, booting the kernel.\n");