blob: 2df38263124c2676e0b0ec62a95de05a1e2ad891 [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 */
Albin Tonnerree7db7b42010-01-08 14:42:43 -080026#include <linux/linkage.h>
Russell King5de813b2010-02-25 12:14:40 +000027#include <asm/string.h>
Albin Tonnerree7db7b42010-01-08 14:42:43 -080028
29#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Russell Kinga0815682006-03-28 10:24:33 +010031
32static void putstr(const char *ptr);
Mark Browna2302b42010-03-10 19:10:28 +010033extern void error(char *x);
Russell Kinga0815682006-03-28 10:24:33 +010034
Russell Kinga09e64f2008-08-05 16:14:15 +010035#include <mach/uncompress.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#ifdef CONFIG_DEBUG_ICEDCC
Tony Lindgren7d95ded2006-09-20 13:03:34 +010038
Stephen Boyddfad5492011-03-23 22:46:15 +010039#if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7)
Tony Lindgren7d95ded2006-09-20 13:03:34 +010040
41static void icedcc_putc(int ch)
42{
43 int status, i = 0x4000000;
44
45 do {
46 if (--i < 0)
47 return;
48
49 asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
50 } while (status & (1 << 29));
51
52 asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
53}
Tony Lindgren200b7a82010-01-19 16:40:07 +010054
Tony Lindgren200b7a82010-01-19 16:40:07 +010055
Jean-Christop PLAGNIOL-VILLARDc633c3c2009-02-25 04:20:40 +010056#elif defined(CONFIG_CPU_XSCALE)
57
58static void icedcc_putc(int ch)
59{
60 int status, i = 0x4000000;
61
62 do {
63 if (--i < 0)
64 return;
65
66 asm volatile ("mrc p14, 0, %0, c14, c0, 0" : "=r" (status));
67 } while (status & (1 << 28));
68
69 asm("mcr p14, 0, %0, c8, c0, 0" : : "r" (ch));
70}
Tony Lindgren7d95ded2006-09-20 13:03:34 +010071
72#else
73
Russell Kingde4533a2006-03-28 10:34:05 +010074static void icedcc_putc(int ch)
75{
76 int status, i = 0x4000000;
77
78 do {
79 if (--i < 0)
80 return;
81
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010082 asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
Russell Kingde4533a2006-03-28 10:34:05 +010083 } while (status & 2);
84
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010085 asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
Russell Kingde4533a2006-03-28 10:34:05 +010086}
87
Tony Lindgren7d95ded2006-09-20 13:03:34 +010088#endif
89
Russell Kinga0815682006-03-28 10:24:33 +010090#define putc(ch) icedcc_putc(ch)
Russell Kinga0815682006-03-28 10:24:33 +010091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Russell Kinga0815682006-03-28 10:24:33 +010093static void putstr(const char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Russell Kinga0815682006-03-28 10:24:33 +010095 char c;
96
97 while ((c = *ptr++) != '\0') {
98 if (c == '\n')
99 putc('\r');
100 putc(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Russell Kinga0815682006-03-28 10:24:33 +0100102
103 flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Russell King5de813b2010-02-25 12:14:40 +0000107void *memcpy(void *__dest, __const void *__src, size_t __n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 int i = 0;
110 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
111
112 for (i = __n >> 3; i > 0; i--) {
113 *d++ = *s++;
114 *d++ = *s++;
115 *d++ = *s++;
116 *d++ = *s++;
117 *d++ = *s++;
118 *d++ = *s++;
119 *d++ = *s++;
120 *d++ = *s++;
121 }
122
123 if (__n & 1 << 2) {
124 *d++ = *s++;
125 *d++ = *s++;
126 *d++ = *s++;
127 *d++ = *s++;
128 }
129
130 if (__n & 1 << 1) {
131 *d++ = *s++;
132 *d++ = *s++;
133 }
134
135 if (__n & 1)
136 *d++ = *s++;
137
138 return __dest;
139}
140
141/*
142 * gzip delarations
143 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144extern char input_data[];
145extern char input_data_end[];
146
Russell King5de813b2010-02-25 12:14:40 +0000147unsigned char *output_data;
148unsigned long output_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Russell King5de813b2010-02-25 12:14:40 +0000150unsigned long free_mem_ptr;
151unsigned long free_mem_end_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Ben Dooksf8c905d2005-11-08 22:43:05 +0000153#ifndef arch_error
154#define arch_error(x)
155#endif
156
Russell King5de813b2010-02-25 12:14:40 +0000157void error(char *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Ben Dooksf8c905d2005-11-08 22:43:05 +0000159 arch_error(x);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 putstr("\n\n");
162 putstr(x);
163 putstr("\n\n -- System halted");
164
165 while(1); /* Halt */
166}
167
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800168asmlinkage void __div0(void)
169{
170 error("Attempting division by 0!");
171}
172
Russell King5de813b2010-02-25 12:14:40 +0000173extern void do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x));
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800176unsigned long
177decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
178 unsigned long free_mem_ptr_end_p,
179 int arch_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800181 unsigned char *tmp;
182
183 output_data = (unsigned char *)output_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 free_mem_ptr = free_mem_ptr_p;
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700185 free_mem_end_ptr = free_mem_ptr_end_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 __machine_arch_type = arch_id;
187
188 arch_decomp_setup();
189
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800190 tmp = (unsigned char *) (((unsigned long)input_data_end) - 4);
191 output_ptr = get_unaligned_le32(tmp);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 putstr("Uncompressing Linux...");
Russell King5de813b2010-02-25 12:14:40 +0000194 do_decompress(input_data, input_data_end - input_data,
195 output_data, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 putstr(" done, booting the kernel.\n");
197 return output_ptr;
198}