xref: /linux/arch/arm/boot/compressed/misc.c (revision a33f32244d8550da8b4a26e277ce07d5c6d158b5)
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 #define _LINUX_STRING_H_
22 
23 #include <linux/compiler.h>	/* for inline */
24 #include <linux/types.h>	/* for size_t */
25 #include <linux/stddef.h>	/* for NULL */
26 #include <linux/linkage.h>
27 #include <asm/string.h>
28 
29 #include <asm/unaligned.h>
30 
31 #ifdef STANDALONE_DEBUG
32 #define putstr printf
33 #else
34 
35 static void putstr(const char *ptr);
36 extern void error(char *x);
37 
38 #include <mach/uncompress.h>
39 
40 #ifdef CONFIG_DEBUG_ICEDCC
41 
42 #ifdef CONFIG_CPU_V6
43 
44 static void icedcc_putc(int ch)
45 {
46 	int status, i = 0x4000000;
47 
48 	do {
49 		if (--i < 0)
50 			return;
51 
52 		asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
53 	} while (status & (1 << 29));
54 
55 	asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
56 }
57 
58 #elif defined(CONFIG_CPU_V7)
59 
60 static void icedcc_putc(int ch)
61 {
62 	asm(
63 	"wait:	mrc	p14, 0, pc, c0, c1, 0			\n\
64 		bcs	wait					\n\
65 		mcr     p14, 0, %0, c0, c5, 0			"
66 	: : "r" (ch));
67 }
68 
69 #elif defined(CONFIG_CPU_XSCALE)
70 
71 static void icedcc_putc(int ch)
72 {
73 	int status, i = 0x4000000;
74 
75 	do {
76 		if (--i < 0)
77 			return;
78 
79 		asm volatile ("mrc p14, 0, %0, c14, c0, 0" : "=r" (status));
80 	} while (status & (1 << 28));
81 
82 	asm("mcr p14, 0, %0, c8, c0, 0" : : "r" (ch));
83 }
84 
85 #else
86 
87 static void icedcc_putc(int ch)
88 {
89 	int status, i = 0x4000000;
90 
91 	do {
92 		if (--i < 0)
93 			return;
94 
95 		asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
96 	} while (status & 2);
97 
98 	asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
99 }
100 
101 #endif
102 
103 #define putc(ch)	icedcc_putc(ch)
104 #endif
105 
106 static void putstr(const char *ptr)
107 {
108 	char c;
109 
110 	while ((c = *ptr++) != '\0') {
111 		if (c == '\n')
112 			putc('\r');
113 		putc(c);
114 	}
115 
116 	flush();
117 }
118 
119 #endif
120 
121 void *memcpy(void *__dest, __const void *__src, size_t __n)
122 {
123 	int i = 0;
124 	unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
125 
126 	for (i = __n >> 3; i > 0; i--) {
127 		*d++ = *s++;
128 		*d++ = *s++;
129 		*d++ = *s++;
130 		*d++ = *s++;
131 		*d++ = *s++;
132 		*d++ = *s++;
133 		*d++ = *s++;
134 		*d++ = *s++;
135 	}
136 
137 	if (__n & 1 << 2) {
138 		*d++ = *s++;
139 		*d++ = *s++;
140 		*d++ = *s++;
141 		*d++ = *s++;
142 	}
143 
144 	if (__n & 1 << 1) {
145 		*d++ = *s++;
146 		*d++ = *s++;
147 	}
148 
149 	if (__n & 1)
150 		*d++ = *s++;
151 
152 	return __dest;
153 }
154 
155 /*
156  * gzip delarations
157  */
158 extern char input_data[];
159 extern char input_data_end[];
160 
161 unsigned char *output_data;
162 unsigned long output_ptr;
163 
164 unsigned long free_mem_ptr;
165 unsigned long free_mem_end_ptr;
166 
167 #ifndef arch_error
168 #define arch_error(x)
169 #endif
170 
171 void error(char *x)
172 {
173 	arch_error(x);
174 
175 	putstr("\n\n");
176 	putstr(x);
177 	putstr("\n\n -- System halted");
178 
179 	while(1);	/* Halt */
180 }
181 
182 asmlinkage void __div0(void)
183 {
184 	error("Attempting division by 0!");
185 }
186 
187 extern void do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x));
188 
189 #ifndef STANDALONE_DEBUG
190 
191 unsigned long
192 decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
193 		unsigned long free_mem_ptr_end_p,
194 		int arch_id)
195 {
196 	unsigned char *tmp;
197 
198 	output_data		= (unsigned char *)output_start;
199 	free_mem_ptr		= free_mem_ptr_p;
200 	free_mem_end_ptr	= free_mem_ptr_end_p;
201 	__machine_arch_type	= arch_id;
202 
203 	arch_decomp_setup();
204 
205 	tmp = (unsigned char *) (((unsigned long)input_data_end) - 4);
206 	output_ptr = get_unaligned_le32(tmp);
207 
208 	putstr("Uncompressing Linux...");
209 	do_decompress(input_data, input_data_end - input_data,
210 			output_data, error);
211 	putstr(" done, booting the kernel.\n");
212 	return output_ptr;
213 }
214 #else
215 
216 char output_buffer[1500*1024];
217 
218 int main()
219 {
220 	output_data = output_buffer;
221 
222 	putstr("Uncompressing Linux...");
223 	decompress(input_data, input_data_end - input_data,
224 			NULL, NULL, output_data, NULL, error);
225 	putstr("done.\n");
226 	return 0;
227 }
228 #endif
229