xref: /openbmc/linux/arch/arm/boot/compressed/misc.c (revision a081568d7016061ed848696984e3acf1ba0b3054)
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 #ifdef STANDALONE_DEBUG
241da177e4SLinus Torvalds #define putstr printf
25*a081568dSRussell King #else
26*a081568dSRussell King 
27*a081568dSRussell King static void putstr(const char *ptr);
28*a081568dSRussell King 
29*a081568dSRussell King #include <linux/compiler.h>
30*a081568dSRussell King #include <asm/arch/uncompress.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #ifdef CONFIG_DEBUG_ICEDCC
33942b6f62SAl Viro extern void icedcc_putc(int ch);
34*a081568dSRussell King #define putc(ch)	icedcc_putc(ch)
35*a081568dSRussell King #define flush()	do { } while (0)
36*a081568dSRussell King #endif
371da177e4SLinus Torvalds 
38*a081568dSRussell King static void putstr(const char *ptr)
391da177e4SLinus Torvalds {
40*a081568dSRussell King 	char c;
41*a081568dSRussell King 
42*a081568dSRussell King 	while ((c = *ptr++) != '\0') {
43*a081568dSRussell King 		if (c == '\n')
44*a081568dSRussell King 			putc('\r');
45*a081568dSRussell King 		putc(c);
461da177e4SLinus Torvalds 	}
47*a081568dSRussell King 
48*a081568dSRussell King 	flush();
491da177e4SLinus Torvalds }
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #endif
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #define __ptr_t void *
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds  * Optimised C version of memzero for the ARM.
571da177e4SLinus Torvalds  */
581da177e4SLinus Torvalds void __memzero (__ptr_t s, size_t n)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
611da177e4SLinus Torvalds 	int i;
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 	u.vp = s;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	for (i = n >> 5; i > 0; i--) {
661da177e4SLinus Torvalds 		*u.ulp++ = 0;
671da177e4SLinus Torvalds 		*u.ulp++ = 0;
681da177e4SLinus Torvalds 		*u.ulp++ = 0;
691da177e4SLinus Torvalds 		*u.ulp++ = 0;
701da177e4SLinus Torvalds 		*u.ulp++ = 0;
711da177e4SLinus Torvalds 		*u.ulp++ = 0;
721da177e4SLinus Torvalds 		*u.ulp++ = 0;
731da177e4SLinus Torvalds 		*u.ulp++ = 0;
741da177e4SLinus Torvalds 	}
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	if (n & 1 << 4) {
771da177e4SLinus Torvalds 		*u.ulp++ = 0;
781da177e4SLinus Torvalds 		*u.ulp++ = 0;
791da177e4SLinus Torvalds 		*u.ulp++ = 0;
801da177e4SLinus Torvalds 		*u.ulp++ = 0;
811da177e4SLinus Torvalds 	}
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	if (n & 1 << 3) {
841da177e4SLinus Torvalds 		*u.ulp++ = 0;
851da177e4SLinus Torvalds 		*u.ulp++ = 0;
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	if (n & 1 << 2)
891da177e4SLinus Torvalds 		*u.ulp++ = 0;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	if (n & 1 << 1) {
921da177e4SLinus Torvalds 		*u.ucp++ = 0;
931da177e4SLinus Torvalds 		*u.ucp++ = 0;
941da177e4SLinus Torvalds 	}
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	if (n & 1)
971da177e4SLinus Torvalds 		*u.ucp++ = 0;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
1011da177e4SLinus Torvalds 			    size_t __n)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds 	int i = 0;
1041da177e4SLinus Torvalds 	unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	for (i = __n >> 3; i > 0; i--) {
1071da177e4SLinus Torvalds 		*d++ = *s++;
1081da177e4SLinus Torvalds 		*d++ = *s++;
1091da177e4SLinus Torvalds 		*d++ = *s++;
1101da177e4SLinus Torvalds 		*d++ = *s++;
1111da177e4SLinus Torvalds 		*d++ = *s++;
1121da177e4SLinus Torvalds 		*d++ = *s++;
1131da177e4SLinus Torvalds 		*d++ = *s++;
1141da177e4SLinus Torvalds 		*d++ = *s++;
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	if (__n & 1 << 2) {
1181da177e4SLinus Torvalds 		*d++ = *s++;
1191da177e4SLinus Torvalds 		*d++ = *s++;
1201da177e4SLinus Torvalds 		*d++ = *s++;
1211da177e4SLinus Torvalds 		*d++ = *s++;
1221da177e4SLinus Torvalds 	}
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	if (__n & 1 << 1) {
1251da177e4SLinus Torvalds 		*d++ = *s++;
1261da177e4SLinus Torvalds 		*d++ = *s++;
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	if (__n & 1)
1301da177e4SLinus Torvalds 		*d++ = *s++;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	return __dest;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds /*
1361da177e4SLinus Torvalds  * gzip delarations
1371da177e4SLinus Torvalds  */
1381da177e4SLinus Torvalds #define OF(args)  args
1391da177e4SLinus Torvalds #define STATIC static
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds typedef unsigned char  uch;
1421da177e4SLinus Torvalds typedef unsigned short ush;
1431da177e4SLinus Torvalds typedef unsigned long  ulg;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds #define WSIZE 0x8000		/* Window size must be at least 32k, */
1461da177e4SLinus Torvalds 				/* and a power of two */
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds static uch *inbuf;		/* input buffer */
1491da177e4SLinus Torvalds static uch window[WSIZE];	/* Sliding window buffer */
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds static unsigned insize;		/* valid bytes in inbuf */
1521da177e4SLinus Torvalds static unsigned inptr;		/* index of next byte to be processed in inbuf */
1531da177e4SLinus Torvalds static unsigned outcnt;		/* bytes in output buffer */
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds /* gzip flag byte */
1561da177e4SLinus Torvalds #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
1571da177e4SLinus Torvalds #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
1581da177e4SLinus Torvalds #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
1591da177e4SLinus Torvalds #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
1601da177e4SLinus Torvalds #define COMMENT      0x10 /* bit 4 set: file comment present */
1611da177e4SLinus Torvalds #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
1621da177e4SLinus Torvalds #define RESERVED     0xC0 /* bit 6,7:   reserved */
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds /* Diagnostic functions */
1671da177e4SLinus Torvalds #ifdef DEBUG
1681da177e4SLinus Torvalds #  define Assert(cond,msg) {if(!(cond)) error(msg);}
1691da177e4SLinus Torvalds #  define Trace(x) fprintf x
1701da177e4SLinus Torvalds #  define Tracev(x) {if (verbose) fprintf x ;}
1711da177e4SLinus Torvalds #  define Tracevv(x) {if (verbose>1) fprintf x ;}
1721da177e4SLinus Torvalds #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
1731da177e4SLinus Torvalds #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
1741da177e4SLinus Torvalds #else
1751da177e4SLinus Torvalds #  define Assert(cond,msg)
1761da177e4SLinus Torvalds #  define Trace(x)
1771da177e4SLinus Torvalds #  define Tracev(x)
1781da177e4SLinus Torvalds #  define Tracevv(x)
1791da177e4SLinus Torvalds #  define Tracec(c,x)
1801da177e4SLinus Torvalds #  define Tracecv(c,x)
1811da177e4SLinus Torvalds #endif
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds static int  fill_inbuf(void);
1841da177e4SLinus Torvalds static void flush_window(void);
1851da177e4SLinus Torvalds static void error(char *m);
1861da177e4SLinus Torvalds static void gzip_mark(void **);
1871da177e4SLinus Torvalds static void gzip_release(void **);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds extern char input_data[];
1901da177e4SLinus Torvalds extern char input_data_end[];
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds static uch *output_data;
1931da177e4SLinus Torvalds static ulg output_ptr;
1941da177e4SLinus Torvalds static ulg bytes_out;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds static void *malloc(int size);
1971da177e4SLinus Torvalds static void free(void *where);
1981da177e4SLinus Torvalds static void error(char *m);
1991da177e4SLinus Torvalds static void gzip_mark(void **);
2001da177e4SLinus Torvalds static void gzip_release(void **);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds static void putstr(const char *);
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds extern int end;
2051da177e4SLinus Torvalds static ulg free_mem_ptr;
2061da177e4SLinus Torvalds static ulg free_mem_ptr_end;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds #define HEAP_SIZE 0x2000
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds #include "../../../../lib/inflate.c"
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds #ifndef STANDALONE_DEBUG
2131da177e4SLinus Torvalds static void *malloc(int size)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	void *p;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	if (size <0) error("Malloc error");
2181da177e4SLinus Torvalds 	if (free_mem_ptr <= 0) error("Memory error");
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	free_mem_ptr = (free_mem_ptr + 3) & ~3;	/* Align */
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	p = (void *)free_mem_ptr;
2231da177e4SLinus Torvalds 	free_mem_ptr += size;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	if (free_mem_ptr >= free_mem_ptr_end)
2261da177e4SLinus Torvalds 		error("Out of memory");
2271da177e4SLinus Torvalds 	return p;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds static void free(void *where)
2311da177e4SLinus Torvalds { /* gzip_mark & gzip_release do the free */
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static void gzip_mark(void **ptr)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	arch_decomp_wdog();
2371da177e4SLinus Torvalds 	*ptr = (void *) free_mem_ptr;
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds static void gzip_release(void **ptr)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	arch_decomp_wdog();
2431da177e4SLinus Torvalds 	free_mem_ptr = (long) *ptr;
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds #else
2461da177e4SLinus Torvalds static void gzip_mark(void **ptr)
2471da177e4SLinus Torvalds {
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds static void gzip_release(void **ptr)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds #endif
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds /* ===========================================================================
2561da177e4SLinus Torvalds  * Fill the input buffer. This is called only when the buffer is empty
2571da177e4SLinus Torvalds  * and at least one byte is really needed.
2581da177e4SLinus Torvalds  */
2591da177e4SLinus Torvalds int fill_inbuf(void)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	if (insize != 0)
2621da177e4SLinus Torvalds 		error("ran out of input data");
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	inbuf = input_data;
2651da177e4SLinus Torvalds 	insize = &input_data_end[0] - &input_data[0];
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	inptr = 1;
2681da177e4SLinus Torvalds 	return inbuf[0];
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds /* ===========================================================================
2721da177e4SLinus Torvalds  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
2731da177e4SLinus Torvalds  * (Used for the decompressed data only.)
2741da177e4SLinus Torvalds  */
2751da177e4SLinus Torvalds void flush_window(void)
2761da177e4SLinus Torvalds {
2771da177e4SLinus Torvalds 	ulg c = crc;
2781da177e4SLinus Torvalds 	unsigned n;
2791da177e4SLinus Torvalds 	uch *in, *out, ch;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	in = window;
2821da177e4SLinus Torvalds 	out = &output_data[output_ptr];
2831da177e4SLinus Torvalds 	for (n = 0; n < outcnt; n++) {
2841da177e4SLinus Torvalds 		ch = *out++ = *in++;
2851da177e4SLinus Torvalds 		c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
2861da177e4SLinus Torvalds 	}
2871da177e4SLinus Torvalds 	crc = c;
2881da177e4SLinus Torvalds 	bytes_out += (ulg)outcnt;
2891da177e4SLinus Torvalds 	output_ptr += (ulg)outcnt;
2901da177e4SLinus Torvalds 	outcnt = 0;
2911da177e4SLinus Torvalds 	putstr(".");
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
294f8c905d3SBen Dooks #ifndef arch_error
295f8c905d3SBen Dooks #define arch_error(x)
296f8c905d3SBen Dooks #endif
297f8c905d3SBen Dooks 
2981da177e4SLinus Torvalds static void error(char *x)
2991da177e4SLinus Torvalds {
300f8c905d3SBen Dooks 	arch_error(x);
301f8c905d3SBen Dooks 
3021da177e4SLinus Torvalds 	putstr("\n\n");
3031da177e4SLinus Torvalds 	putstr(x);
3041da177e4SLinus Torvalds 	putstr("\n\n -- System halted");
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	while(1);	/* Halt */
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds #ifndef STANDALONE_DEBUG
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds ulg
3121da177e4SLinus Torvalds decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
3131da177e4SLinus Torvalds 		  int arch_id)
3141da177e4SLinus Torvalds {
3151da177e4SLinus Torvalds 	output_data		= (uch *)output_start;	/* Points to kernel start */
3161da177e4SLinus Torvalds 	free_mem_ptr		= free_mem_ptr_p;
3171da177e4SLinus Torvalds 	free_mem_ptr_end	= free_mem_ptr_end_p;
3181da177e4SLinus Torvalds 	__machine_arch_type	= arch_id;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	arch_decomp_setup();
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	makecrc();
3231da177e4SLinus Torvalds 	putstr("Uncompressing Linux...");
3241da177e4SLinus Torvalds 	gunzip();
3251da177e4SLinus Torvalds 	putstr(" done, booting the kernel.\n");
3261da177e4SLinus Torvalds 	return output_ptr;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds #else
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds char output_buffer[1500*1024];
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds int main()
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	output_data = output_buffer;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	makecrc();
3371da177e4SLinus Torvalds 	putstr("Uncompressing Linux...");
3381da177e4SLinus Torvalds 	gunzip();
3391da177e4SLinus Torvalds 	putstr("done.\n");
3401da177e4SLinus Torvalds 	return 0;
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds #endif
3431da177e4SLinus Torvalds 
344