allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / arch / cris / arch-v10 / boot / compressed / misc.c
blobffb8d21b2f83f16c8020aabc5c90177b8083fb6e
1 /*
2 * misc.c
4 * $Id: misc.c,v 1.6 2003/10/27 08:04:31 starvik Exp $
5 *
6 * This is a collection of several routines from gzip-1.0.3
7 * adapted for Linux.
9 * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
10 * puts by Nick Holloway 1993, better puts by Martin Mares 1995
11 * adoptation for Linux/CRIS Axis Communications AB, 1999
15 /* where the piggybacked kernel image expects itself to live.
16 * it is the same address we use when we network load an uncompressed
17 * image into DRAM, and it is the address the kernel is linked to live
18 * at by vmlinux.lds.S
21 #define KERNEL_LOAD_ADR 0x40004000
24 #include <linux/types.h>
25 #include <asm/arch/svinto.h>
28 * gzip declarations
31 #define OF(args) args
32 #define STATIC static
34 void* memset(void* s, int c, size_t n);
35 void* memcpy(void* __dest, __const void* __src,
36 size_t __n);
38 #define memzero(s, n) memset ((s), 0, (n))
41 typedef unsigned char uch;
42 typedef unsigned short ush;
43 typedef unsigned long ulg;
45 #define WSIZE 0x8000 /* Window size must be at least 32k, */
46 /* and a power of two */
48 static uch *inbuf; /* input buffer */
49 static uch window[WSIZE]; /* Sliding window buffer */
51 unsigned inptr = 0; /* index of next byte to be processed in inbuf
52 * After decompression it will contain the
53 * compressed size, and head.S will read it.
56 static unsigned outcnt = 0; /* bytes in output buffer */
58 /* gzip flag byte */
59 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
60 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
61 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
62 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
63 #define COMMENT 0x10 /* bit 4 set: file comment present */
64 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
65 #define RESERVED 0xC0 /* bit 6,7: reserved */
67 #define get_byte() inbuf[inptr++]
69 /* Diagnostic functions */
70 #ifdef DEBUG
71 # define Assert(cond,msg) {if(!(cond)) error(msg);}
72 # define Trace(x) fprintf x
73 # define Tracev(x) {if (verbose) fprintf x ;}
74 # define Tracevv(x) {if (verbose>1) fprintf x ;}
75 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
76 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
77 #else
78 # define Assert(cond,msg)
79 # define Trace(x)
80 # define Tracev(x)
81 # define Tracevv(x)
82 # define Tracec(c,x)
83 # define Tracecv(c,x)
84 #endif
86 static int fill_inbuf(void);
87 static void flush_window(void);
88 static void error(char *m);
89 static void gzip_mark(void **);
90 static void gzip_release(void **);
92 extern char *input_data; /* lives in head.S */
94 static long bytes_out = 0;
95 static uch *output_data;
96 static unsigned long output_ptr = 0;
98 static void *malloc(int size);
99 static void free(void *where);
100 static void error(char *m);
101 static void gzip_mark(void **);
102 static void gzip_release(void **);
104 static void puts(const char *);
106 /* the "heap" is put directly after the BSS ends, at end */
108 extern int end;
109 static long free_mem_ptr = (long)&end;
111 #include "../../../../../lib/inflate.c"
113 static void *malloc(int size)
115 void *p;
117 if (size <0) error("Malloc error");
119 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
121 p = (void *)free_mem_ptr;
122 free_mem_ptr += size;
124 return p;
127 static void free(void *where)
128 { /* Don't care */
131 static void gzip_mark(void **ptr)
133 *ptr = (void *) free_mem_ptr;
136 static void gzip_release(void **ptr)
138 free_mem_ptr = (long) *ptr;
141 /* decompressor info and error messages to serial console */
143 static void
144 puts(const char *s)
146 #ifndef CONFIG_ETRAX_DEBUG_PORT_NULL
147 while(*s) {
148 #ifdef CONFIG_ETRAX_DEBUG_PORT0
149 while(!(*R_SERIAL0_STATUS & (1 << 5))) ;
150 *R_SERIAL0_TR_DATA = *s++;
151 #endif
152 #ifdef CONFIG_ETRAX_DEBUG_PORT1
153 while(!(*R_SERIAL1_STATUS & (1 << 5))) ;
154 *R_SERIAL1_TR_DATA = *s++;
155 #endif
156 #ifdef CONFIG_ETRAX_DEBUG_PORT2
157 while(!(*R_SERIAL2_STATUS & (1 << 5))) ;
158 *R_SERIAL2_TR_DATA = *s++;
159 #endif
160 #ifdef CONFIG_ETRAX_DEBUG_PORT3
161 while(!(*R_SERIAL3_STATUS & (1 << 5))) ;
162 *R_SERIAL3_TR_DATA = *s++;
163 #endif
165 #endif
168 void*
169 memset(void* s, int c, size_t n)
171 int i;
172 char *ss = (char*)s;
174 for (i=0;i<n;i++) ss[i] = c;
177 void*
178 memcpy(void* __dest, __const void* __src,
179 size_t __n)
181 int i;
182 char *d = (char *)__dest, *s = (char *)__src;
184 for (i=0;i<__n;i++) d[i] = s[i];
187 /* ===========================================================================
188 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
189 * (Used for the decompressed data only.)
192 static void
193 flush_window()
195 ulg c = crc; /* temporary variable */
196 unsigned n;
197 uch *in, *out, ch;
199 in = window;
200 out = &output_data[output_ptr];
201 for (n = 0; n < outcnt; n++) {
202 ch = *out++ = *in++;
203 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
205 crc = c;
206 bytes_out += (ulg)outcnt;
207 output_ptr += (ulg)outcnt;
208 outcnt = 0;
211 static void
212 error(char *x)
214 puts("\n\n");
215 puts(x);
216 puts("\n\n -- System halted\n");
218 while(1); /* Halt */
221 void
222 setup_normal_output_buffer()
224 output_data = (char *)KERNEL_LOAD_ADR;
227 void
228 decompress_kernel()
230 char revision;
232 /* input_data is set in head.S */
233 inbuf = input_data;
235 #ifdef CONFIG_ETRAX_DEBUG_PORT0
236 *R_SERIAL0_XOFF = 0;
237 *R_SERIAL0_BAUD = 0x99;
238 *R_SERIAL0_TR_CTRL = 0x40;
239 #endif
240 #ifdef CONFIG_ETRAX_DEBUG_PORT1
241 *R_SERIAL1_XOFF = 0;
242 *R_SERIAL1_BAUD = 0x99;
243 *R_SERIAL1_TR_CTRL = 0x40;
244 #endif
245 #ifdef CONFIG_ETRAX_DEBUG_PORT2
246 *R_GEN_CONFIG = 0x08;
247 *R_SERIAL2_XOFF = 0;
248 *R_SERIAL2_BAUD = 0x99;
249 *R_SERIAL2_TR_CTRL = 0x40;
250 #endif
251 #ifdef CONFIG_ETRAX_DEBUG_PORT3
252 *R_GEN_CONFIG = 0x100;
253 *R_SERIAL3_XOFF = 0;
254 *R_SERIAL3_BAUD = 0x99;
255 *R_SERIAL3_TR_CTRL = 0x40;
256 #endif
258 setup_normal_output_buffer();
260 makecrc();
262 __asm__ volatile ("move vr,%0" : "=rm" (revision));
263 if (revision < 10)
265 puts("You need an ETRAX 100LX to run linux 2.6\n");
266 while(1);
269 puts("Uncompressing Linux...\n");
270 gunzip();
271 puts("Done. Now booting the kernel.\n");