x86: arch/x86/mm/init_32.c cleanup
[wrt350n-kernel.git] / arch / sh / boot / compressed / misc_32.c
blobadcea31e663eade000c375f47badafa14189caf8
1 /*
2 * arch/sh/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 SH by Stuart Menefy, Aug 1999
11 * Modified to use standard LinuxSH BIOS by Greg Banks 7Jul2000
14 #include <asm/uaccess.h>
15 #include <asm/addrspace.h>
16 #include <asm/page.h>
17 #ifdef CONFIG_SH_STANDARD_BIOS
18 #include <asm/sh_bios.h>
19 #endif
22 * gzip declarations
25 #define OF(args) args
26 #define STATIC static
28 #undef memset
29 #undef memcpy
30 #define memzero(s, n) memset ((s), 0, (n))
32 typedef unsigned char uch;
33 typedef unsigned short ush;
34 typedef unsigned long ulg;
36 #define WSIZE 0x8000 /* Window size must be at least 32k, */
37 /* and a power of two */
39 static uch *inbuf; /* input buffer */
40 static uch window[WSIZE]; /* Sliding window buffer */
42 static unsigned insize = 0; /* valid bytes in inbuf */
43 static unsigned inptr = 0; /* index of next byte to be processed in inbuf */
44 static unsigned outcnt = 0; /* bytes in output buffer */
46 /* gzip flag byte */
47 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
48 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
49 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
50 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
51 #define COMMENT 0x10 /* bit 4 set: file comment present */
52 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
53 #define RESERVED 0xC0 /* bit 6,7: reserved */
55 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
57 /* Diagnostic functions */
58 #ifdef DEBUG
59 # define Assert(cond,msg) {if(!(cond)) error(msg);}
60 # define Trace(x) fprintf x
61 # define Tracev(x) {if (verbose) fprintf x ;}
62 # define Tracevv(x) {if (verbose>1) fprintf x ;}
63 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
64 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
65 #else
66 # define Assert(cond,msg)
67 # define Trace(x)
68 # define Tracev(x)
69 # define Tracevv(x)
70 # define Tracec(c,x)
71 # define Tracecv(c,x)
72 #endif
74 static int fill_inbuf(void);
75 static void flush_window(void);
76 static void error(char *m);
77 static void gzip_mark(void **);
78 static void gzip_release(void **);
80 extern char input_data[];
81 extern int input_len;
83 static long bytes_out = 0;
84 static uch *output_data;
85 static unsigned long output_ptr = 0;
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 int puts(const char *);
95 extern int _text; /* Defined in vmlinux.lds.S */
96 extern int _end;
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)
106 void *p;
108 if (size <0) error("Malloc error");
109 if (free_mem_ptr == 0) error("Memory error");
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("Out of memory");
119 return p;
122 static void free(void *where)
123 { /* Don't care */
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 #ifdef CONFIG_SH_STANDARD_BIOS
137 size_t strlen(const char *s)
139 int i = 0;
141 while (*s++)
142 i++;
143 return i;
146 int puts(const char *s)
148 int len = strlen(s);
149 sh_bios_console_write(s, len);
150 return len;
152 #else
153 int puts(const char *s)
155 /* This should be updated to use the sh-sci routines */
156 return 0;
158 #endif
160 void* memset(void* s, int c, size_t n)
162 int i;
163 char *ss = (char*)s;
165 for (i=0;i<n;i++) ss[i] = c;
166 return s;
169 void* memcpy(void* __dest, __const void* __src,
170 size_t __n)
172 int i;
173 char *d = (char *)__dest, *s = (char *)__src;
175 for (i=0;i<__n;i++) d[i] = s[i];
176 return __dest;
179 /* ===========================================================================
180 * Fill the input buffer. This is called only when the buffer is empty
181 * and at least one byte is really needed.
183 static int fill_inbuf(void)
185 if (insize != 0) {
186 error("ran out of input data");
189 inbuf = input_data;
190 insize = input_len;
191 inptr = 1;
192 return inbuf[0];
195 /* ===========================================================================
196 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
197 * (Used for the decompressed data only.)
199 static void flush_window(void)
201 ulg c = crc; /* temporary variable */
202 unsigned n;
203 uch *in, *out, ch;
205 in = window;
206 out = &output_data[output_ptr];
207 for (n = 0; n < outcnt; n++) {
208 ch = *out++ = *in++;
209 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
211 crc = c;
212 bytes_out += (ulg)outcnt;
213 output_ptr += (ulg)outcnt;
214 outcnt = 0;
217 static void error(char *x)
219 puts("\n\n");
220 puts(x);
221 puts("\n\n -- System halted");
223 while(1); /* Halt */
226 #define STACK_SIZE (4096)
227 long user_stack [STACK_SIZE];
228 long* stack_start = &user_stack[STACK_SIZE];
230 void decompress_kernel(void)
232 output_data = 0;
233 output_ptr = PHYSADDR((unsigned long)&_text+PAGE_SIZE);
234 #ifdef CONFIG_29BIT
235 output_ptr |= P2SEG;
236 #endif
237 free_mem_ptr = (unsigned long)&_end;
238 free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
240 makecrc();
241 puts("Uncompressing Linux... ");
242 gunzip();
243 puts("Ok, booting the kernel.\n");