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