11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * misc.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This is a collection of several routines from gzip-1.0.3 51da177e4SLinus Torvalds * adapted for Linux. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Modified for ARM Linux by Russell King 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Nicolas Pitre <nico@visuaide.com> 1999/04/14 : 121da177e4SLinus Torvalds * For this code to run directly from Flash, all constant variables must 131da177e4SLinus Torvalds * be marked with 'const' and all other variables initialized at run-time 141da177e4SLinus Torvalds * only. This way all non constant variables will end up in the bss segment, 151da177e4SLinus Torvalds * which should point to addresses in RAM and cleared to 0 on start. 161da177e4SLinus Torvalds * This allows for a much quicker boot time. 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds unsigned int __machine_arch_type; 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include <linux/string.h> 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <asm/arch/uncompress.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #ifdef STANDALONE_DEBUG 261da177e4SLinus Torvalds #define putstr printf 271da177e4SLinus Torvalds #endif 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds #ifdef CONFIG_DEBUG_ICEDCC 301da177e4SLinus Torvalds #define putstr icedcc_putstr 311da177e4SLinus Torvalds #define putc icedcc_putc 321da177e4SLinus Torvalds 33*942b6f62SAl Viro extern void icedcc_putc(int ch); 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds static void 361da177e4SLinus Torvalds icedcc_putstr(const char *ptr) 371da177e4SLinus Torvalds { 381da177e4SLinus Torvalds for (; *ptr != '\0'; ptr++) { 391da177e4SLinus Torvalds icedcc_putc(*ptr); 401da177e4SLinus Torvalds } 411da177e4SLinus Torvalds } 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #endif 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #define __ptr_t void * 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds /* 481da177e4SLinus Torvalds * Optimised C version of memzero for the ARM. 491da177e4SLinus Torvalds */ 501da177e4SLinus Torvalds void __memzero (__ptr_t s, size_t n) 511da177e4SLinus Torvalds { 521da177e4SLinus Torvalds union { void *vp; unsigned long *ulp; unsigned char *ucp; } u; 531da177e4SLinus Torvalds int i; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds u.vp = s; 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds for (i = n >> 5; i > 0; i--) { 581da177e4SLinus Torvalds *u.ulp++ = 0; 591da177e4SLinus Torvalds *u.ulp++ = 0; 601da177e4SLinus Torvalds *u.ulp++ = 0; 611da177e4SLinus Torvalds *u.ulp++ = 0; 621da177e4SLinus Torvalds *u.ulp++ = 0; 631da177e4SLinus Torvalds *u.ulp++ = 0; 641da177e4SLinus Torvalds *u.ulp++ = 0; 651da177e4SLinus Torvalds *u.ulp++ = 0; 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds if (n & 1 << 4) { 691da177e4SLinus Torvalds *u.ulp++ = 0; 701da177e4SLinus Torvalds *u.ulp++ = 0; 711da177e4SLinus Torvalds *u.ulp++ = 0; 721da177e4SLinus Torvalds *u.ulp++ = 0; 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds if (n & 1 << 3) { 761da177e4SLinus Torvalds *u.ulp++ = 0; 771da177e4SLinus Torvalds *u.ulp++ = 0; 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds if (n & 1 << 2) 811da177e4SLinus Torvalds *u.ulp++ = 0; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds if (n & 1 << 1) { 841da177e4SLinus Torvalds *u.ucp++ = 0; 851da177e4SLinus Torvalds *u.ucp++ = 0; 861da177e4SLinus Torvalds } 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds if (n & 1) 891da177e4SLinus Torvalds *u.ucp++ = 0; 901da177e4SLinus Torvalds } 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src, 931da177e4SLinus Torvalds size_t __n) 941da177e4SLinus Torvalds { 951da177e4SLinus Torvalds int i = 0; 961da177e4SLinus Torvalds unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds for (i = __n >> 3; i > 0; i--) { 991da177e4SLinus Torvalds *d++ = *s++; 1001da177e4SLinus Torvalds *d++ = *s++; 1011da177e4SLinus Torvalds *d++ = *s++; 1021da177e4SLinus Torvalds *d++ = *s++; 1031da177e4SLinus Torvalds *d++ = *s++; 1041da177e4SLinus Torvalds *d++ = *s++; 1051da177e4SLinus Torvalds *d++ = *s++; 1061da177e4SLinus Torvalds *d++ = *s++; 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds if (__n & 1 << 2) { 1101da177e4SLinus Torvalds *d++ = *s++; 1111da177e4SLinus Torvalds *d++ = *s++; 1121da177e4SLinus Torvalds *d++ = *s++; 1131da177e4SLinus Torvalds *d++ = *s++; 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds if (__n & 1 << 1) { 1171da177e4SLinus Torvalds *d++ = *s++; 1181da177e4SLinus Torvalds *d++ = *s++; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds if (__n & 1) 1221da177e4SLinus Torvalds *d++ = *s++; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds return __dest; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /* 1281da177e4SLinus Torvalds * gzip delarations 1291da177e4SLinus Torvalds */ 1301da177e4SLinus Torvalds #define OF(args) args 1311da177e4SLinus Torvalds #define STATIC static 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds typedef unsigned char uch; 1341da177e4SLinus Torvalds typedef unsigned short ush; 1351da177e4SLinus Torvalds typedef unsigned long ulg; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds #define WSIZE 0x8000 /* Window size must be at least 32k, */ 1381da177e4SLinus Torvalds /* and a power of two */ 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds static uch *inbuf; /* input buffer */ 1411da177e4SLinus Torvalds static uch window[WSIZE]; /* Sliding window buffer */ 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds static unsigned insize; /* valid bytes in inbuf */ 1441da177e4SLinus Torvalds static unsigned inptr; /* index of next byte to be processed in inbuf */ 1451da177e4SLinus Torvalds static unsigned outcnt; /* bytes in output buffer */ 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* gzip flag byte */ 1481da177e4SLinus Torvalds #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ 1491da177e4SLinus Torvalds #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */ 1501da177e4SLinus Torvalds #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ 1511da177e4SLinus Torvalds #define ORIG_NAME 0x08 /* bit 3 set: original file name present */ 1521da177e4SLinus Torvalds #define COMMENT 0x10 /* bit 4 set: file comment present */ 1531da177e4SLinus Torvalds #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */ 1541da177e4SLinus Torvalds #define RESERVED 0xC0 /* bit 6,7: reserved */ 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf()) 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* Diagnostic functions */ 1591da177e4SLinus Torvalds #ifdef DEBUG 1601da177e4SLinus Torvalds # define Assert(cond,msg) {if(!(cond)) error(msg);} 1611da177e4SLinus Torvalds # define Trace(x) fprintf x 1621da177e4SLinus Torvalds # define Tracev(x) {if (verbose) fprintf x ;} 1631da177e4SLinus Torvalds # define Tracevv(x) {if (verbose>1) fprintf x ;} 1641da177e4SLinus Torvalds # define Tracec(c,x) {if (verbose && (c)) fprintf x ;} 1651da177e4SLinus Torvalds # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;} 1661da177e4SLinus Torvalds #else 1671da177e4SLinus Torvalds # define Assert(cond,msg) 1681da177e4SLinus Torvalds # define Trace(x) 1691da177e4SLinus Torvalds # define Tracev(x) 1701da177e4SLinus Torvalds # define Tracevv(x) 1711da177e4SLinus Torvalds # define Tracec(c,x) 1721da177e4SLinus Torvalds # define Tracecv(c,x) 1731da177e4SLinus Torvalds #endif 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds static int fill_inbuf(void); 1761da177e4SLinus Torvalds static void flush_window(void); 1771da177e4SLinus Torvalds static void error(char *m); 1781da177e4SLinus Torvalds static void gzip_mark(void **); 1791da177e4SLinus Torvalds static void gzip_release(void **); 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds extern char input_data[]; 1821da177e4SLinus Torvalds extern char input_data_end[]; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds static uch *output_data; 1851da177e4SLinus Torvalds static ulg output_ptr; 1861da177e4SLinus Torvalds static ulg bytes_out; 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds static void *malloc(int size); 1891da177e4SLinus Torvalds static void free(void *where); 1901da177e4SLinus Torvalds static void error(char *m); 1911da177e4SLinus Torvalds static void gzip_mark(void **); 1921da177e4SLinus Torvalds static void gzip_release(void **); 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds static void putstr(const char *); 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds extern int end; 1971da177e4SLinus Torvalds static ulg free_mem_ptr; 1981da177e4SLinus Torvalds static ulg free_mem_ptr_end; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds #define HEAP_SIZE 0x2000 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds #include "../../../../lib/inflate.c" 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds #ifndef STANDALONE_DEBUG 2051da177e4SLinus Torvalds static void *malloc(int size) 2061da177e4SLinus Torvalds { 2071da177e4SLinus Torvalds void *p; 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds if (size <0) error("Malloc error"); 2101da177e4SLinus Torvalds if (free_mem_ptr <= 0) error("Memory error"); 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */ 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds p = (void *)free_mem_ptr; 2151da177e4SLinus Torvalds free_mem_ptr += size; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds if (free_mem_ptr >= free_mem_ptr_end) 2181da177e4SLinus Torvalds error("Out of memory"); 2191da177e4SLinus Torvalds return p; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds static void free(void *where) 2231da177e4SLinus Torvalds { /* gzip_mark & gzip_release do the free */ 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds static void gzip_mark(void **ptr) 2271da177e4SLinus Torvalds { 2281da177e4SLinus Torvalds arch_decomp_wdog(); 2291da177e4SLinus Torvalds *ptr = (void *) free_mem_ptr; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds static void gzip_release(void **ptr) 2331da177e4SLinus Torvalds { 2341da177e4SLinus Torvalds arch_decomp_wdog(); 2351da177e4SLinus Torvalds free_mem_ptr = (long) *ptr; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds #else 2381da177e4SLinus Torvalds static void gzip_mark(void **ptr) 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds static void gzip_release(void **ptr) 2431da177e4SLinus Torvalds { 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds #endif 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds /* =========================================================================== 2481da177e4SLinus Torvalds * Fill the input buffer. This is called only when the buffer is empty 2491da177e4SLinus Torvalds * and at least one byte is really needed. 2501da177e4SLinus Torvalds */ 2511da177e4SLinus Torvalds int fill_inbuf(void) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds if (insize != 0) 2541da177e4SLinus Torvalds error("ran out of input data"); 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds inbuf = input_data; 2571da177e4SLinus Torvalds insize = &input_data_end[0] - &input_data[0]; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds inptr = 1; 2601da177e4SLinus Torvalds return inbuf[0]; 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds /* =========================================================================== 2641da177e4SLinus Torvalds * Write the output window window[0..outcnt-1] and update crc and bytes_out. 2651da177e4SLinus Torvalds * (Used for the decompressed data only.) 2661da177e4SLinus Torvalds */ 2671da177e4SLinus Torvalds void flush_window(void) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds ulg c = crc; 2701da177e4SLinus Torvalds unsigned n; 2711da177e4SLinus Torvalds uch *in, *out, ch; 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds in = window; 2741da177e4SLinus Torvalds out = &output_data[output_ptr]; 2751da177e4SLinus Torvalds for (n = 0; n < outcnt; n++) { 2761da177e4SLinus Torvalds ch = *out++ = *in++; 2771da177e4SLinus Torvalds c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8); 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds crc = c; 2801da177e4SLinus Torvalds bytes_out += (ulg)outcnt; 2811da177e4SLinus Torvalds output_ptr += (ulg)outcnt; 2821da177e4SLinus Torvalds outcnt = 0; 2831da177e4SLinus Torvalds putstr("."); 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds static void error(char *x) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds putstr("\n\n"); 2891da177e4SLinus Torvalds putstr(x); 2901da177e4SLinus Torvalds putstr("\n\n -- System halted"); 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds while(1); /* Halt */ 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds #ifndef STANDALONE_DEBUG 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds ulg 2981da177e4SLinus Torvalds decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p, 2991da177e4SLinus Torvalds int arch_id) 3001da177e4SLinus Torvalds { 3011da177e4SLinus Torvalds output_data = (uch *)output_start; /* Points to kernel start */ 3021da177e4SLinus Torvalds free_mem_ptr = free_mem_ptr_p; 3031da177e4SLinus Torvalds free_mem_ptr_end = free_mem_ptr_end_p; 3041da177e4SLinus Torvalds __machine_arch_type = arch_id; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds arch_decomp_setup(); 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds makecrc(); 3091da177e4SLinus Torvalds putstr("Uncompressing Linux..."); 3101da177e4SLinus Torvalds gunzip(); 3111da177e4SLinus Torvalds putstr(" done, booting the kernel.\n"); 3121da177e4SLinus Torvalds return output_ptr; 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds #else 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds char output_buffer[1500*1024]; 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds int main() 3191da177e4SLinus Torvalds { 3201da177e4SLinus Torvalds output_data = output_buffer; 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds makecrc(); 3231da177e4SLinus Torvalds putstr("Uncompressing Linux..."); 3241da177e4SLinus Torvalds gunzip(); 3251da177e4SLinus Torvalds putstr("done.\n"); 3261da177e4SLinus Torvalds return 0; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds #endif 3291da177e4SLinus Torvalds 330