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