blob: 56a0d116d27147445a4f69aa18728074f431ef9a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
19unsigned int __machine_arch_type;
20
Albin Tonnerree7db7b42010-01-08 14:42:43 -080021#define _LINUX_STRING_H_
22
Rusty Russellaa0d3bb2009-03-31 13:05:35 -060023#include <linux/compiler.h> /* for inline */
24#include <linux/types.h> /* for size_t */
25#include <linux/stddef.h> /* for NULL */
26#include <asm/string.h>
Albin Tonnerree7db7b42010-01-08 14:42:43 -080027#include <linux/linkage.h>
28
29#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#ifdef STANDALONE_DEBUG
32#define putstr printf
Russell Kinga0815682006-03-28 10:24:33 +010033#else
34
35static void putstr(const char *ptr);
36
Russell Kinga09e64f2008-08-05 16:14:15 +010037#include <mach/uncompress.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#ifdef CONFIG_DEBUG_ICEDCC
Tony Lindgren7d95ded2006-09-20 13:03:34 +010040
41#ifdef CONFIG_CPU_V6
42
43static void icedcc_putc(int ch)
44{
45 int status, i = 0x4000000;
46
47 do {
48 if (--i < 0)
49 return;
50
51 asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
52 } while (status & (1 << 29));
53
54 asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
55}
Tony Lindgren200b7a82010-01-19 16:40:07 +010056
57#elif defined(CONFIG_CPU_V7)
58
59static void icedcc_putc(int ch)
60{
61 asm(
62 "wait: mrc p14, 0, pc, c0, c1, 0 \n\
63 bcs wait \n\
64 mcr p14, 0, %0, c0, c5, 0 "
65 : : "r" (ch));
66}
67
Jean-Christop PLAGNIOL-VILLARDc633c3c2009-02-25 04:20:40 +010068#elif defined(CONFIG_CPU_XSCALE)
69
70static void icedcc_putc(int ch)
71{
72 int status, i = 0x4000000;
73
74 do {
75 if (--i < 0)
76 return;
77
78 asm volatile ("mrc p14, 0, %0, c14, c0, 0" : "=r" (status));
79 } while (status & (1 << 28));
80
81 asm("mcr p14, 0, %0, c8, c0, 0" : : "r" (ch));
82}
Tony Lindgren7d95ded2006-09-20 13:03:34 +010083
84#else
85
Russell Kingde4533a2006-03-28 10:34:05 +010086static void icedcc_putc(int ch)
87{
88 int status, i = 0x4000000;
89
90 do {
91 if (--i < 0)
92 return;
93
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010094 asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
Russell Kingde4533a2006-03-28 10:34:05 +010095 } while (status & 2);
96
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010097 asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
Russell Kingde4533a2006-03-28 10:34:05 +010098}
99
Tony Lindgren7d95ded2006-09-20 13:03:34 +0100100#endif
101
Russell Kinga0815682006-03-28 10:24:33 +0100102#define putc(ch) icedcc_putc(ch)
Russell Kinga0815682006-03-28 10:24:33 +0100103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Russell Kinga0815682006-03-28 10:24:33 +0100105static void putstr(const char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Russell Kinga0815682006-03-28 10:24:33 +0100107 char c;
108
109 while ((c = *ptr++) != '\0') {
110 if (c == '\n')
111 putc('\r');
112 putc(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Russell Kinga0815682006-03-28 10:24:33 +0100114
115 flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118#endif
119
120#define __ptr_t void *
121
Russell King59f0cb02008-10-27 11:24:09 +0000122#define memzero(s,n) __memzero(s,n)
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
125 * Optimised C version of memzero for the ARM.
126 */
127void __memzero (__ptr_t s, size_t n)
128{
129 union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
130 int i;
131
132 u.vp = s;
133
134 for (i = n >> 5; i > 0; i--) {
135 *u.ulp++ = 0;
136 *u.ulp++ = 0;
137 *u.ulp++ = 0;
138 *u.ulp++ = 0;
139 *u.ulp++ = 0;
140 *u.ulp++ = 0;
141 *u.ulp++ = 0;
142 *u.ulp++ = 0;
143 }
144
145 if (n & 1 << 4) {
146 *u.ulp++ = 0;
147 *u.ulp++ = 0;
148 *u.ulp++ = 0;
149 *u.ulp++ = 0;
150 }
151
152 if (n & 1 << 3) {
153 *u.ulp++ = 0;
154 *u.ulp++ = 0;
155 }
156
157 if (n & 1 << 2)
158 *u.ulp++ = 0;
159
160 if (n & 1 << 1) {
161 *u.ucp++ = 0;
162 *u.ucp++ = 0;
163 }
164
165 if (n & 1)
166 *u.ucp++ = 0;
167}
168
169static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
170 size_t __n)
171{
172 int i = 0;
173 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
174
175 for (i = __n >> 3; i > 0; i--) {
176 *d++ = *s++;
177 *d++ = *s++;
178 *d++ = *s++;
179 *d++ = *s++;
180 *d++ = *s++;
181 *d++ = *s++;
182 *d++ = *s++;
183 *d++ = *s++;
184 }
185
186 if (__n & 1 << 2) {
187 *d++ = *s++;
188 *d++ = *s++;
189 *d++ = *s++;
190 *d++ = *s++;
191 }
192
193 if (__n & 1 << 1) {
194 *d++ = *s++;
195 *d++ = *s++;
196 }
197
198 if (__n & 1)
199 *d++ = *s++;
200
201 return __dest;
202}
203
204/*
205 * gzip delarations
206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207#define STATIC static
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/* Diagnostic functions */
210#ifdef DEBUG
211# define Assert(cond,msg) {if(!(cond)) error(msg);}
212# define Trace(x) fprintf x
213# define Tracev(x) {if (verbose) fprintf x ;}
214# define Tracevv(x) {if (verbose>1) fprintf x ;}
215# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
216# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
217#else
218# define Assert(cond,msg)
219# define Trace(x)
220# define Tracev(x)
221# define Tracevv(x)
222# define Tracec(c,x)
223# define Tracecv(c,x)
224#endif
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static void error(char *m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228extern char input_data[];
229extern char input_data_end[];
230
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800231static unsigned char *output_data;
232static unsigned long output_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static void error(char *m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236static void putstr(const char *);
237
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800238static unsigned long free_mem_ptr;
239static unsigned long free_mem_end_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700241#ifdef STANDALONE_DEBUG
242#define NO_INFLATE_MALLOC
243#endif
244
245#define ARCH_HAS_DECOMP_WDOG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800247#ifdef CONFIG_KERNEL_GZIP
248#include "../../../../lib/decompress_inflate.c"
249#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800251#ifdef CONFIG_KERNEL_LZO
252#include "../../../../lib/decompress_unlzo.c"
253#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Ben Dooksf8c905d2005-11-08 22:43:05 +0000255#ifndef arch_error
256#define arch_error(x)
257#endif
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static void error(char *x)
260{
Ben Dooksf8c905d2005-11-08 22:43:05 +0000261 arch_error(x);
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 putstr("\n\n");
264 putstr(x);
265 putstr("\n\n -- System halted");
266
267 while(1); /* Halt */
268}
269
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800270asmlinkage void __div0(void)
271{
272 error("Attempting division by 0!");
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#ifndef STANDALONE_DEBUG
276
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800277unsigned long
278decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
279 unsigned long free_mem_ptr_end_p,
280 int arch_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800282 unsigned char *tmp;
283
284 output_data = (unsigned char *)output_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 free_mem_ptr = free_mem_ptr_p;
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700286 free_mem_end_ptr = free_mem_ptr_end_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 __machine_arch_type = arch_id;
288
289 arch_decomp_setup();
290
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800291 tmp = (unsigned char *) (((unsigned long)input_data_end) - 4);
292 output_ptr = get_unaligned_le32(tmp);
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 putstr("Uncompressing Linux...");
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800295 decompress(input_data, input_data_end - input_data,
296 NULL, NULL, output_data, NULL, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 putstr(" done, booting the kernel.\n");
298 return output_ptr;
299}
300#else
301
302char output_buffer[1500*1024];
303
304int main()
305{
306 output_data = output_buffer;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 putstr("Uncompressing Linux...");
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800309 decompress(input_data, input_data_end - input_data,
310 NULL, NULL, output_data, NULL, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 putstr("done.\n");
312 return 0;
313}
314#endif