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