xref: /openbmc/linux/arch/arm/boot/compressed/misc.c (revision e40f1e9fb342a2e38fae164861a8cff248ceb87b)
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 
21e7db7b42SAlbin Tonnerre #define _LINUX_STRING_H_
22e7db7b42SAlbin Tonnerre 
23aa0d3bb7SRusty Russell #include <linux/compiler.h>	/* for inline */
24aa0d3bb7SRusty Russell #include <linux/types.h>	/* for size_t */
25aa0d3bb7SRusty Russell #include <linux/stddef.h>	/* for NULL */
26e7db7b42SAlbin Tonnerre #include <linux/linkage.h>
275de813b6SRussell King #include <asm/string.h>
28e7db7b42SAlbin Tonnerre 
29a081568dSRussell King 
30a081568dSRussell King static void putstr(const char *ptr);
31a2302b45SMark Brown extern void error(char *x);
32a081568dSRussell King 
33a09e64fbSRussell King #include <mach/uncompress.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #ifdef CONFIG_DEBUG_ICEDCC
367d95ded9STony Lindgren 
37e399b1a4SRussell King #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K)
387d95ded9STony Lindgren 
397d95ded9STony Lindgren static void icedcc_putc(int ch)
407d95ded9STony Lindgren {
417d95ded9STony Lindgren 	int status, i = 0x4000000;
427d95ded9STony Lindgren 
437d95ded9STony Lindgren 	do {
447d95ded9STony Lindgren 		if (--i < 0)
457d95ded9STony Lindgren 			return;
467d95ded9STony Lindgren 
477d95ded9STony Lindgren 		asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
487d95ded9STony Lindgren 	} while (status & (1 << 29));
497d95ded9STony Lindgren 
507d95ded9STony Lindgren 	asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
517d95ded9STony Lindgren }
52200b7a8dSTony Lindgren 
53200b7a8dSTony Lindgren #elif defined(CONFIG_CPU_V7)
54200b7a8dSTony Lindgren 
55200b7a8dSTony Lindgren static void icedcc_putc(int ch)
56200b7a8dSTony Lindgren {
57200b7a8dSTony Lindgren 	asm(
58200b7a8dSTony Lindgren 	"wait:	mrc	p14, 0, pc, c0, c1, 0			\n\
59200b7a8dSTony Lindgren 		bcs	wait					\n\
60200b7a8dSTony Lindgren 		mcr     p14, 0, %0, c0, c5, 0			"
61200b7a8dSTony Lindgren 	: : "r" (ch));
62200b7a8dSTony Lindgren }
63200b7a8dSTony Lindgren 
64c633c3cfSJean-Christop PLAGNIOL-VILLARD #elif defined(CONFIG_CPU_XSCALE)
65c633c3cfSJean-Christop PLAGNIOL-VILLARD 
66c633c3cfSJean-Christop PLAGNIOL-VILLARD static void icedcc_putc(int ch)
67c633c3cfSJean-Christop PLAGNIOL-VILLARD {
68c633c3cfSJean-Christop PLAGNIOL-VILLARD 	int status, i = 0x4000000;
69c633c3cfSJean-Christop PLAGNIOL-VILLARD 
70c633c3cfSJean-Christop PLAGNIOL-VILLARD 	do {
71c633c3cfSJean-Christop PLAGNIOL-VILLARD 		if (--i < 0)
72c633c3cfSJean-Christop PLAGNIOL-VILLARD 			return;
73c633c3cfSJean-Christop PLAGNIOL-VILLARD 
74c633c3cfSJean-Christop PLAGNIOL-VILLARD 		asm volatile ("mrc p14, 0, %0, c14, c0, 0" : "=r" (status));
75c633c3cfSJean-Christop PLAGNIOL-VILLARD 	} while (status & (1 << 28));
76c633c3cfSJean-Christop PLAGNIOL-VILLARD 
77c633c3cfSJean-Christop PLAGNIOL-VILLARD 	asm("mcr p14, 0, %0, c8, c0, 0" : : "r" (ch));
78c633c3cfSJean-Christop PLAGNIOL-VILLARD }
797d95ded9STony Lindgren 
807d95ded9STony Lindgren #else
817d95ded9STony Lindgren 
82de4533a0SRussell King static void icedcc_putc(int ch)
83de4533a0SRussell King {
84de4533a0SRussell King 	int status, i = 0x4000000;
85de4533a0SRussell King 
86de4533a0SRussell King 	do {
87de4533a0SRussell King 		if (--i < 0)
88de4533a0SRussell King 			return;
89de4533a0SRussell King 
90b2556da5SUwe Zeisberger 		asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
91de4533a0SRussell King 	} while (status & 2);
92de4533a0SRussell King 
93b2556da5SUwe Zeisberger 	asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
94de4533a0SRussell King }
95de4533a0SRussell King 
967d95ded9STony Lindgren #endif
977d95ded9STony Lindgren 
98a081568dSRussell King #define putc(ch)	icedcc_putc(ch)
99a081568dSRussell King #endif
1001da177e4SLinus Torvalds 
101a081568dSRussell King static void putstr(const char *ptr)
1021da177e4SLinus Torvalds {
103a081568dSRussell King 	char c;
104a081568dSRussell King 
105a081568dSRussell King 	while ((c = *ptr++) != '\0') {
106a081568dSRussell King 		if (c == '\n')
107a081568dSRussell King 			putc('\r');
108a081568dSRussell King 		putc(c);
1091da177e4SLinus Torvalds 	}
110a081568dSRussell King 
111a081568dSRussell King 	flush();
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 
1155de813b6SRussell King void *memcpy(void *__dest, __const void *__src, size_t __n)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	int i = 0;
1181da177e4SLinus Torvalds 	unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	for (i = __n >> 3; i > 0; i--) {
1211da177e4SLinus Torvalds 		*d++ = *s++;
1221da177e4SLinus Torvalds 		*d++ = *s++;
1231da177e4SLinus Torvalds 		*d++ = *s++;
1241da177e4SLinus Torvalds 		*d++ = *s++;
1251da177e4SLinus Torvalds 		*d++ = *s++;
1261da177e4SLinus Torvalds 		*d++ = *s++;
1271da177e4SLinus Torvalds 		*d++ = *s++;
1281da177e4SLinus Torvalds 		*d++ = *s++;
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	if (__n & 1 << 2) {
1321da177e4SLinus Torvalds 		*d++ = *s++;
1331da177e4SLinus Torvalds 		*d++ = *s++;
1341da177e4SLinus Torvalds 		*d++ = *s++;
1351da177e4SLinus Torvalds 		*d++ = *s++;
1361da177e4SLinus Torvalds 	}
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	if (__n & 1 << 1) {
1391da177e4SLinus Torvalds 		*d++ = *s++;
1401da177e4SLinus Torvalds 		*d++ = *s++;
1411da177e4SLinus Torvalds 	}
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	if (__n & 1)
1441da177e4SLinus Torvalds 		*d++ = *s++;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	return __dest;
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /*
150*e40f1e9fSNicolas Pitre  * gzip declarations
1511da177e4SLinus Torvalds  */
1521da177e4SLinus Torvalds extern char input_data[];
1531da177e4SLinus Torvalds extern char input_data_end[];
1541da177e4SLinus Torvalds 
1555de813b6SRussell King unsigned char *output_data;
1561da177e4SLinus Torvalds 
1575de813b6SRussell King unsigned long free_mem_ptr;
1585de813b6SRussell King unsigned long free_mem_end_ptr;
1591da177e4SLinus Torvalds 
160f8c905d3SBen Dooks #ifndef arch_error
161f8c905d3SBen Dooks #define arch_error(x)
162f8c905d3SBen Dooks #endif
163f8c905d3SBen Dooks 
1645de813b6SRussell King void error(char *x)
1651da177e4SLinus Torvalds {
166f8c905d3SBen Dooks 	arch_error(x);
167f8c905d3SBen Dooks 
1681da177e4SLinus Torvalds 	putstr("\n\n");
1691da177e4SLinus Torvalds 	putstr(x);
1701da177e4SLinus Torvalds 	putstr("\n\n -- System halted");
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	while(1);	/* Halt */
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
175e7db7b42SAlbin Tonnerre asmlinkage void __div0(void)
176e7db7b42SAlbin Tonnerre {
177e7db7b42SAlbin Tonnerre 	error("Attempting division by 0!");
178e7db7b42SAlbin Tonnerre }
179e7db7b42SAlbin Tonnerre 
1805de813b6SRussell King extern void do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x));
1815de813b6SRussell King 
1821da177e4SLinus Torvalds 
183*e40f1e9fSNicolas Pitre void
184e7db7b42SAlbin Tonnerre decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
185e7db7b42SAlbin Tonnerre 		unsigned long free_mem_ptr_end_p,
1861da177e4SLinus Torvalds 		int arch_id)
1871da177e4SLinus Torvalds {
188e7db7b42SAlbin Tonnerre 	output_data		= (unsigned char *)output_start;
1891da177e4SLinus Torvalds 	free_mem_ptr		= free_mem_ptr_p;
1902d6ffccaSThomas Petazzoni 	free_mem_end_ptr	= free_mem_ptr_end_p;
1911da177e4SLinus Torvalds 	__machine_arch_type	= arch_id;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	arch_decomp_setup();
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	putstr("Uncompressing Linux...");
1965de813b6SRussell King 	do_decompress(input_data, input_data_end - input_data,
1975de813b6SRussell King 			output_data, error);
1981da177e4SLinus Torvalds 	putstr(" done, booting the kernel.\n");
1991da177e4SLinus Torvalds }
200