Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6-block.git] / arch / arm / boot / compressed / misc.c
CommitLineData
1da177e4
LT
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
19unsigned int __machine_arch_type;
20
e7db7b42
AT
21#define _LINUX_STRING_H_
22
aa0d3bb7
RR
23#include <linux/compiler.h> /* for inline */
24#include <linux/types.h> /* for size_t */
25#include <linux/stddef.h> /* for NULL */
e7db7b42 26#include <linux/linkage.h>
5de813b6 27#include <asm/string.h>
e7db7b42
AT
28
29#include <asm/unaligned.h>
1da177e4 30
1da177e4
LT
31#ifdef STANDALONE_DEBUG
32#define putstr printf
a081568d 33#else
1da177e4 34
a081568d 35static void putstr(const char *ptr);
a2302b45 36extern void error(char *x);
a081568d 37
a09e64fb 38#include <mach/uncompress.h>
1da177e4 39
a081568d 40#ifdef CONFIG_DEBUG_ICEDCC
7d95ded9
TL
41
42#ifdef CONFIG_CPU_V6
43
44static 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}
200b7a8d
TL
57
58#elif defined(CONFIG_CPU_V7)
59
60static 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
c633c3cf
JCPV
69#elif defined(CONFIG_CPU_XSCALE)
70
71static 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}
7d95ded9
TL
84
85#else
86
de4533a0
RK
87static void icedcc_putc(int ch)
88{
89 int status, i = 0x4000000;
90
91 do {
92 if (--i < 0)
93 return;
94
b2556da5 95 asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
de4533a0
RK
96 } while (status & 2);
97
b2556da5 98 asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
de4533a0
RK
99}
100
7d95ded9
TL
101#endif
102
a081568d 103#define putc(ch) icedcc_putc(ch)
a081568d 104#endif
1da177e4 105
a081568d 106static void putstr(const char *ptr)
1da177e4 107{
a081568d
RK
108 char c;
109
110 while ((c = *ptr++) != '\0') {
111 if (c == '\n')
112 putc('\r');
113 putc(c);
1da177e4 114 }
a081568d
RK
115
116 flush();
1da177e4
LT
117}
118
119#endif
120
5de813b6 121void *memcpy(void *__dest, __const void *__src, size_t __n)
1da177e4
LT
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 */
1da177e4
LT
158extern char input_data[];
159extern char input_data_end[];
160
5de813b6
RK
161unsigned char *output_data;
162unsigned long output_ptr;
1da177e4 163
5de813b6
RK
164unsigned long free_mem_ptr;
165unsigned long free_mem_end_ptr;
1da177e4 166
f8c905d3
BD
167#ifndef arch_error
168#define arch_error(x)
169#endif
170
5de813b6 171void error(char *x)
1da177e4 172{
f8c905d3
BD
173 arch_error(x);
174
1da177e4
LT
175 putstr("\n\n");
176 putstr(x);
177 putstr("\n\n -- System halted");
178
179 while(1); /* Halt */
180}
181
e7db7b42
AT
182asmlinkage void __div0(void)
183{
184 error("Attempting division by 0!");
185}
186
5de813b6
RK
187extern void do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x));
188
1da177e4
LT
189#ifndef STANDALONE_DEBUG
190
e7db7b42
AT
191unsigned long
192decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
193 unsigned long free_mem_ptr_end_p,
194 int arch_id)
1da177e4 195{
e7db7b42
AT
196 unsigned char *tmp;
197
198 output_data = (unsigned char *)output_start;
1da177e4 199 free_mem_ptr = free_mem_ptr_p;
2d6ffcca 200 free_mem_end_ptr = free_mem_ptr_end_p;
1da177e4
LT
201 __machine_arch_type = arch_id;
202
203 arch_decomp_setup();
204
e7db7b42
AT
205 tmp = (unsigned char *) (((unsigned long)input_data_end) - 4);
206 output_ptr = get_unaligned_le32(tmp);
207
1da177e4 208 putstr("Uncompressing Linux...");
5de813b6
RK
209 do_decompress(input_data, input_data_end - input_data,
210 output_data, error);
1da177e4
LT
211 putstr(" done, booting the kernel.\n");
212 return output_ptr;
213}
214#else
215
216char output_buffer[1500*1024];
217
218int main()
219{
220 output_data = output_buffer;
221
1da177e4 222 putstr("Uncompressing Linux...");
e7db7b42
AT
223 decompress(input_data, input_data_end - input_data,
224 NULL, NULL, output_data, NULL, error);
1da177e4
LT
225 putstr("done.\n");
226 return 0;
227}
228#endif