Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / sh64 / boot / compressed / misc.c
blob89dbf45df3c862908c9aa9c16d68236585d0e310
1 /*
2 * arch/shmedia/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 SHmedia from sh by Stuart Menefy, May 2002
12 #include <linux/config.h>
13 #include <asm/uaccess.h>
15 /* cache.c */
16 #define CACHE_ENABLE 0
17 #define CACHE_DISABLE 1
18 int cache_control(unsigned int command);
21 * gzip declarations
24 #define OF(args) args
25 #define STATIC static
27 #undef memset
28 #undef memcpy
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 = 0; /* valid bytes in inbuf */
42 static unsigned inptr = 0; /* index of next byte to be processed in inbuf */
43 static unsigned outcnt = 0; /* bytes in output buffer */
45 /* gzip flag byte */
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 */
57 #ifdef DEBUG
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 ;}
64 #else
65 # define Assert(cond,msg)
66 # define Trace(x)
67 # define Tracev(x)
68 # define Tracevv(x)
69 # define Tracec(c,x)
70 # define Tracecv(c,x)
71 #endif
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[];
80 extern int input_len;
82 static long bytes_out = 0;
83 static uch *output_data;
84 static unsigned long output_ptr = 0;
86 static void *malloc(int size);
87 static void free(void *where);
88 static void error(char *m);
89 static void gzip_mark(void **);
90 static void gzip_release(void **);
92 static void puts(const char *);
94 extern int _text; /* Defined in vmlinux.lds.S */
95 extern int _end;
96 static unsigned long free_mem_ptr;
97 static unsigned long free_mem_end_ptr;
99 #define HEAP_SIZE 0x10000
101 #include "../../../../lib/inflate.c"
103 static void *malloc(int size)
105 void *p;
107 if (size < 0)
108 error("Malloc error\n");
109 if (free_mem_ptr == 0)
110 error("Memory error\n");
112 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
114 p = (void *) free_mem_ptr;
115 free_mem_ptr += size;
117 if (free_mem_ptr >= free_mem_end_ptr)
118 error("\nOut of memory\n");
120 return p;
123 static void free(void *where)
124 { /* Don't care */
127 static void gzip_mark(void **ptr)
129 *ptr = (void *) free_mem_ptr;
132 static void gzip_release(void **ptr)
134 free_mem_ptr = (long) *ptr;
137 void puts(const char *s)
141 void *memset(void *s, int c, size_t n)
143 int i;
144 char *ss = (char *) s;
146 for (i = 0; i < n; i++)
147 ss[i] = c;
148 return s;
151 void *memcpy(void *__dest, __const void *__src, size_t __n)
153 int i;
154 char *d = (char *) __dest, *s = (char *) __src;
156 for (i = 0; i < __n; i++)
157 d[i] = s[i];
158 return __dest;
161 /* ===========================================================================
162 * Fill the input buffer. This is called only when the buffer is empty
163 * and at least one byte is really needed.
165 static int fill_inbuf(void)
167 if (insize != 0) {
168 error("ran out of input data\n");
171 inbuf = input_data;
172 insize = input_len;
173 inptr = 1;
174 return inbuf[0];
177 /* ===========================================================================
178 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
179 * (Used for the decompressed data only.)
181 static void flush_window(void)
183 ulg c = crc; /* temporary variable */
184 unsigned n;
185 uch *in, *out, ch;
187 in = window;
188 out = &output_data[output_ptr];
189 for (n = 0; n < outcnt; n++) {
190 ch = *out++ = *in++;
191 c = crc_32_tab[((int) c ^ ch) & 0xff] ^ (c >> 8);
193 crc = c;
194 bytes_out += (ulg) outcnt;
195 output_ptr += (ulg) outcnt;
196 outcnt = 0;
197 puts(".");
200 static void error(char *x)
202 puts("\n\n");
203 puts(x);
204 puts("\n\n -- System halted");
206 while (1) ; /* Halt */
209 #define STACK_SIZE (4096)
210 long __attribute__ ((aligned(8))) user_stack[STACK_SIZE];
211 long *stack_start = &user_stack[STACK_SIZE];
213 void decompress_kernel(void)
215 output_data = (uch *) (CONFIG_MEMORY_START + 0x2000);
216 free_mem_ptr = (unsigned long) &_end;
217 free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
219 makecrc();
220 puts("Uncompressing Linux... ");
221 cache_control(CACHE_ENABLE);
222 gunzip();
223 puts("\n");
225 #if 0
226 /* When booting from ROM may want to do something like this if the
227 * boot loader doesn't.
230 /* Set up the parameters and command line */
232 volatile unsigned int *parambase =
233 (int *) (CONFIG_MEMORY_START + 0x1000);
235 parambase[0] = 0x1; /* MOUNT_ROOT_RDONLY */
236 parambase[1] = 0x0; /* RAMDISK_FLAGS */
237 parambase[2] = 0x0200; /* ORIG_ROOT_DEV */
238 parambase[3] = 0x0; /* LOADER_TYPE */
239 parambase[4] = 0x0; /* INITRD_START */
240 parambase[5] = 0x0; /* INITRD_SIZE */
241 parambase[6] = 0;
243 strcpy((char *) ((int) parambase + 0x100),
244 "console=ttySC0,38400");
246 #endif
248 puts("Ok, booting the kernel.\n");
250 cache_control(CACHE_DISABLE);