| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * | 
|  | 9 | * Modified for ARM Linux by Russell King | 
|  | 10 | * | 
|  | 11 | * Nicolas Pitre <nico@visuaide.com>  1999/04/14 : | 
|  | 12 | *  For this code to run directly from Flash, all constant variables must | 
|  | 13 | *  be marked with 'const' and all other variables initialized at run-time | 
|  | 14 | *  only.  This way all non constant variables will end up in the bss segment, | 
|  | 15 | *  which should point to addresses in RAM and cleared to 0 on start. | 
|  | 16 | *  This allows for a much quicker boot time. | 
|  | 17 | */ | 
|  | 18 |  | 
|  | 19 | unsigned int __machine_arch_type; | 
|  | 20 |  | 
|  | 21 | #include <linux/string.h> | 
|  | 22 |  | 
|  | 23 | #include <asm/arch/uncompress.h> | 
|  | 24 |  | 
|  | 25 | #ifdef STANDALONE_DEBUG | 
|  | 26 | #define putstr printf | 
|  | 27 | #endif | 
|  | 28 |  | 
|  | 29 | #ifdef CONFIG_DEBUG_ICEDCC | 
|  | 30 | #define putstr icedcc_putstr | 
|  | 31 | #define putc icedcc_putc | 
|  | 32 |  | 
| Al Viro | 942b6f6 | 2005-10-29 12:07:11 +0100 | [diff] [blame] | 33 | extern void icedcc_putc(int ch); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 |  | 
|  | 35 | static void | 
|  | 36 | icedcc_putstr(const char *ptr) | 
|  | 37 | { | 
|  | 38 | for (; *ptr != '\0'; ptr++) { | 
|  | 39 | icedcc_putc(*ptr); | 
|  | 40 | } | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | #endif | 
|  | 44 |  | 
|  | 45 | #define __ptr_t void * | 
|  | 46 |  | 
|  | 47 | /* | 
|  | 48 | * Optimised C version of memzero for the ARM. | 
|  | 49 | */ | 
|  | 50 | void __memzero (__ptr_t s, size_t n) | 
|  | 51 | { | 
|  | 52 | union { void *vp; unsigned long *ulp; unsigned char *ucp; } u; | 
|  | 53 | int i; | 
|  | 54 |  | 
|  | 55 | u.vp = s; | 
|  | 56 |  | 
|  | 57 | for (i = n >> 5; i > 0; i--) { | 
|  | 58 | *u.ulp++ = 0; | 
|  | 59 | *u.ulp++ = 0; | 
|  | 60 | *u.ulp++ = 0; | 
|  | 61 | *u.ulp++ = 0; | 
|  | 62 | *u.ulp++ = 0; | 
|  | 63 | *u.ulp++ = 0; | 
|  | 64 | *u.ulp++ = 0; | 
|  | 65 | *u.ulp++ = 0; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | if (n & 1 << 4) { | 
|  | 69 | *u.ulp++ = 0; | 
|  | 70 | *u.ulp++ = 0; | 
|  | 71 | *u.ulp++ = 0; | 
|  | 72 | *u.ulp++ = 0; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | if (n & 1 << 3) { | 
|  | 76 | *u.ulp++ = 0; | 
|  | 77 | *u.ulp++ = 0; | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | if (n & 1 << 2) | 
|  | 81 | *u.ulp++ = 0; | 
|  | 82 |  | 
|  | 83 | if (n & 1 << 1) { | 
|  | 84 | *u.ucp++ = 0; | 
|  | 85 | *u.ucp++ = 0; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | if (n & 1) | 
|  | 89 | *u.ucp++ = 0; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src, | 
|  | 93 | size_t __n) | 
|  | 94 | { | 
|  | 95 | int i = 0; | 
|  | 96 | unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src; | 
|  | 97 |  | 
|  | 98 | for (i = __n >> 3; i > 0; i--) { | 
|  | 99 | *d++ = *s++; | 
|  | 100 | *d++ = *s++; | 
|  | 101 | *d++ = *s++; | 
|  | 102 | *d++ = *s++; | 
|  | 103 | *d++ = *s++; | 
|  | 104 | *d++ = *s++; | 
|  | 105 | *d++ = *s++; | 
|  | 106 | *d++ = *s++; | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | if (__n & 1 << 2) { | 
|  | 110 | *d++ = *s++; | 
|  | 111 | *d++ = *s++; | 
|  | 112 | *d++ = *s++; | 
|  | 113 | *d++ = *s++; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | if (__n & 1 << 1) { | 
|  | 117 | *d++ = *s++; | 
|  | 118 | *d++ = *s++; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | if (__n & 1) | 
|  | 122 | *d++ = *s++; | 
|  | 123 |  | 
|  | 124 | return __dest; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | /* | 
|  | 128 | * gzip delarations | 
|  | 129 | */ | 
|  | 130 | #define OF(args)  args | 
|  | 131 | #define STATIC static | 
|  | 132 |  | 
|  | 133 | typedef unsigned char  uch; | 
|  | 134 | typedef unsigned short ush; | 
|  | 135 | typedef unsigned long  ulg; | 
|  | 136 |  | 
|  | 137 | #define WSIZE 0x8000		/* Window size must be at least 32k, */ | 
|  | 138 | /* and a power of two */ | 
|  | 139 |  | 
|  | 140 | static uch *inbuf;		/* input buffer */ | 
|  | 141 | static uch window[WSIZE];	/* Sliding window buffer */ | 
|  | 142 |  | 
|  | 143 | static unsigned insize;		/* valid bytes in inbuf */ | 
|  | 144 | static unsigned inptr;		/* index of next byte to be processed in inbuf */ | 
|  | 145 | static unsigned outcnt;		/* bytes in output buffer */ | 
|  | 146 |  | 
|  | 147 | /* gzip flag byte */ | 
|  | 148 | #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */ | 
|  | 149 | #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */ | 
|  | 150 | #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */ | 
|  | 151 | #define ORIG_NAME    0x08 /* bit 3 set: original file name present */ | 
|  | 152 | #define COMMENT      0x10 /* bit 4 set: file comment present */ | 
|  | 153 | #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */ | 
|  | 154 | #define RESERVED     0xC0 /* bit 6,7:   reserved */ | 
|  | 155 |  | 
|  | 156 | #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf()) | 
|  | 157 |  | 
|  | 158 | /* Diagnostic functions */ | 
|  | 159 | #ifdef DEBUG | 
|  | 160 | #  define Assert(cond,msg) {if(!(cond)) error(msg);} | 
|  | 161 | #  define Trace(x) fprintf x | 
|  | 162 | #  define Tracev(x) {if (verbose) fprintf x ;} | 
|  | 163 | #  define Tracevv(x) {if (verbose>1) fprintf x ;} | 
|  | 164 | #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;} | 
|  | 165 | #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;} | 
|  | 166 | #else | 
|  | 167 | #  define Assert(cond,msg) | 
|  | 168 | #  define Trace(x) | 
|  | 169 | #  define Tracev(x) | 
|  | 170 | #  define Tracevv(x) | 
|  | 171 | #  define Tracec(c,x) | 
|  | 172 | #  define Tracecv(c,x) | 
|  | 173 | #endif | 
|  | 174 |  | 
|  | 175 | static int  fill_inbuf(void); | 
|  | 176 | static void flush_window(void); | 
|  | 177 | static void error(char *m); | 
|  | 178 | static void gzip_mark(void **); | 
|  | 179 | static void gzip_release(void **); | 
|  | 180 |  | 
|  | 181 | extern char input_data[]; | 
|  | 182 | extern char input_data_end[]; | 
|  | 183 |  | 
|  | 184 | static uch *output_data; | 
|  | 185 | static ulg output_ptr; | 
|  | 186 | static ulg bytes_out; | 
|  | 187 |  | 
|  | 188 | static void *malloc(int size); | 
|  | 189 | static void free(void *where); | 
|  | 190 | static void error(char *m); | 
|  | 191 | static void gzip_mark(void **); | 
|  | 192 | static void gzip_release(void **); | 
|  | 193 |  | 
|  | 194 | static void putstr(const char *); | 
|  | 195 |  | 
|  | 196 | extern int end; | 
|  | 197 | static ulg free_mem_ptr; | 
|  | 198 | static ulg free_mem_ptr_end; | 
|  | 199 |  | 
|  | 200 | #define HEAP_SIZE 0x2000 | 
|  | 201 |  | 
|  | 202 | #include "../../../../lib/inflate.c" | 
|  | 203 |  | 
|  | 204 | #ifndef STANDALONE_DEBUG | 
|  | 205 | static void *malloc(int size) | 
|  | 206 | { | 
|  | 207 | void *p; | 
|  | 208 |  | 
|  | 209 | if (size <0) error("Malloc error"); | 
|  | 210 | if (free_mem_ptr <= 0) error("Memory error"); | 
|  | 211 |  | 
|  | 212 | free_mem_ptr = (free_mem_ptr + 3) & ~3;	/* Align */ | 
|  | 213 |  | 
|  | 214 | p = (void *)free_mem_ptr; | 
|  | 215 | free_mem_ptr += size; | 
|  | 216 |  | 
|  | 217 | if (free_mem_ptr >= free_mem_ptr_end) | 
|  | 218 | error("Out of memory"); | 
|  | 219 | return p; | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | static void free(void *where) | 
|  | 223 | { /* gzip_mark & gzip_release do the free */ | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | static void gzip_mark(void **ptr) | 
|  | 227 | { | 
|  | 228 | arch_decomp_wdog(); | 
|  | 229 | *ptr = (void *) free_mem_ptr; | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | static void gzip_release(void **ptr) | 
|  | 233 | { | 
|  | 234 | arch_decomp_wdog(); | 
|  | 235 | free_mem_ptr = (long) *ptr; | 
|  | 236 | } | 
|  | 237 | #else | 
|  | 238 | static void gzip_mark(void **ptr) | 
|  | 239 | { | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | static void gzip_release(void **ptr) | 
|  | 243 | { | 
|  | 244 | } | 
|  | 245 | #endif | 
|  | 246 |  | 
|  | 247 | /* =========================================================================== | 
|  | 248 | * Fill the input buffer. This is called only when the buffer is empty | 
|  | 249 | * and at least one byte is really needed. | 
|  | 250 | */ | 
|  | 251 | int fill_inbuf(void) | 
|  | 252 | { | 
|  | 253 | if (insize != 0) | 
|  | 254 | error("ran out of input data"); | 
|  | 255 |  | 
|  | 256 | inbuf = input_data; | 
|  | 257 | insize = &input_data_end[0] - &input_data[0]; | 
|  | 258 |  | 
|  | 259 | inptr = 1; | 
|  | 260 | return inbuf[0]; | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | /* =========================================================================== | 
|  | 264 | * Write the output window window[0..outcnt-1] and update crc and bytes_out. | 
|  | 265 | * (Used for the decompressed data only.) | 
|  | 266 | */ | 
|  | 267 | void flush_window(void) | 
|  | 268 | { | 
|  | 269 | ulg c = crc; | 
|  | 270 | unsigned n; | 
|  | 271 | uch *in, *out, ch; | 
|  | 272 |  | 
|  | 273 | in = window; | 
|  | 274 | out = &output_data[output_ptr]; | 
|  | 275 | for (n = 0; n < outcnt; n++) { | 
|  | 276 | ch = *out++ = *in++; | 
|  | 277 | c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8); | 
|  | 278 | } | 
|  | 279 | crc = c; | 
|  | 280 | bytes_out += (ulg)outcnt; | 
|  | 281 | output_ptr += (ulg)outcnt; | 
|  | 282 | outcnt = 0; | 
|  | 283 | putstr("."); | 
|  | 284 | } | 
|  | 285 |  | 
| Ben Dooks | f8c905d | 2005-11-08 22:43:05 +0000 | [diff] [blame] | 286 | #ifndef arch_error | 
|  | 287 | #define arch_error(x) | 
|  | 288 | #endif | 
|  | 289 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | static void error(char *x) | 
|  | 291 | { | 
| Ben Dooks | f8c905d | 2005-11-08 22:43:05 +0000 | [diff] [blame] | 292 | arch_error(x); | 
|  | 293 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | putstr("\n\n"); | 
|  | 295 | putstr(x); | 
|  | 296 | putstr("\n\n -- System halted"); | 
|  | 297 |  | 
|  | 298 | while(1);	/* Halt */ | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | #ifndef STANDALONE_DEBUG | 
|  | 302 |  | 
|  | 303 | ulg | 
|  | 304 | decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p, | 
|  | 305 | int arch_id) | 
|  | 306 | { | 
|  | 307 | output_data		= (uch *)output_start;	/* Points to kernel start */ | 
|  | 308 | free_mem_ptr		= free_mem_ptr_p; | 
|  | 309 | free_mem_ptr_end	= free_mem_ptr_end_p; | 
|  | 310 | __machine_arch_type	= arch_id; | 
|  | 311 |  | 
|  | 312 | arch_decomp_setup(); | 
|  | 313 |  | 
|  | 314 | makecrc(); | 
|  | 315 | putstr("Uncompressing Linux..."); | 
|  | 316 | gunzip(); | 
|  | 317 | putstr(" done, booting the kernel.\n"); | 
|  | 318 | return output_ptr; | 
|  | 319 | } | 
|  | 320 | #else | 
|  | 321 |  | 
|  | 322 | char output_buffer[1500*1024]; | 
|  | 323 |  | 
|  | 324 | int main() | 
|  | 325 | { | 
|  | 326 | output_data = output_buffer; | 
|  | 327 |  | 
|  | 328 | makecrc(); | 
|  | 329 | putstr("Uncompressing Linux..."); | 
|  | 330 | gunzip(); | 
|  | 331 | putstr("done.\n"); | 
|  | 332 | return 0; | 
|  | 333 | } | 
|  | 334 | #endif | 
|  | 335 |  |