blob: 832d37236c59fdc92c852166d64a6b0c6efc71d5 [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
Russell Kinga0815682006-03-28 10:24:33 +010029
30static void putstr(const char *ptr);
Mark Browna2302b42010-03-10 19:10:28 +010031extern void error(char *x);
Russell Kinga0815682006-03-28 10:24:33 +010032
Russell Kinga09e64f2008-08-05 16:14:15 +010033#include <mach/uncompress.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#ifdef CONFIG_DEBUG_ICEDCC
Tony Lindgren7d95ded2006-09-20 13:03:34 +010036
Stephen Boyddfad5492011-03-23 22:46:15 +010037#if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7)
Tony Lindgren7d95ded2006-09-20 13:03:34 +010038
39static void icedcc_putc(int ch)
40{
41 int status, i = 0x4000000;
42
43 do {
44 if (--i < 0)
45 return;
46
47 asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
48 } while (status & (1 << 29));
49
50 asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
51}
Tony Lindgren200b7a82010-01-19 16:40:07 +010052
Tony Lindgren200b7a82010-01-19 16:40:07 +010053
Jean-Christop PLAGNIOL-VILLARDc633c3c2009-02-25 04:20:40 +010054#elif defined(CONFIG_CPU_XSCALE)
55
56static void icedcc_putc(int ch)
57{
58 int status, i = 0x4000000;
59
60 do {
61 if (--i < 0)
62 return;
63
64 asm volatile ("mrc p14, 0, %0, c14, c0, 0" : "=r" (status));
65 } while (status & (1 << 28));
66
67 asm("mcr p14, 0, %0, c8, c0, 0" : : "r" (ch));
68}
Tony Lindgren7d95ded2006-09-20 13:03:34 +010069
70#else
71
Russell Kingde4533a2006-03-28 10:34:05 +010072static void icedcc_putc(int ch)
73{
74 int status, i = 0x4000000;
75
76 do {
77 if (--i < 0)
78 return;
79
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010080 asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
Russell Kingde4533a2006-03-28 10:34:05 +010081 } while (status & 2);
82
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010083 asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
Russell Kingde4533a2006-03-28 10:34:05 +010084}
85
Tony Lindgren7d95ded2006-09-20 13:03:34 +010086#endif
87
Russell Kinga0815682006-03-28 10:24:33 +010088#define putc(ch) icedcc_putc(ch)
Russell Kinga0815682006-03-28 10:24:33 +010089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Russell Kinga0815682006-03-28 10:24:33 +010091static void putstr(const char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Russell Kinga0815682006-03-28 10:24:33 +010093 char c;
94
95 while ((c = *ptr++) != '\0') {
96 if (c == '\n')
97 putc('\r');
98 putc(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
Russell Kinga0815682006-03-28 10:24:33 +0100100
101 flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Russell King5de813b2010-02-25 12:14:40 +0000105void *memcpy(void *__dest, __const void *__src, size_t __n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 int i = 0;
108 unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
109
110 for (i = __n >> 3; i > 0; i--) {
111 *d++ = *s++;
112 *d++ = *s++;
113 *d++ = *s++;
114 *d++ = *s++;
115 *d++ = *s++;
116 *d++ = *s++;
117 *d++ = *s++;
118 *d++ = *s++;
119 }
120
121 if (__n & 1 << 2) {
122 *d++ = *s++;
123 *d++ = *s++;
124 *d++ = *s++;
125 *d++ = *s++;
126 }
127
128 if (__n & 1 << 1) {
129 *d++ = *s++;
130 *d++ = *s++;
131 }
132
133 if (__n & 1)
134 *d++ = *s++;
135
136 return __dest;
137}
138
139/*
Nicolas Pitree40f1e92011-04-19 16:13:23 -0400140 * gzip declarations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142extern char input_data[];
143extern char input_data_end[];
144
Russell King5de813b2010-02-25 12:14:40 +0000145unsigned char *output_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Russell King5de813b2010-02-25 12:14:40 +0000147unsigned long free_mem_ptr;
148unsigned long free_mem_end_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Ben Dooksf8c905d2005-11-08 22:43:05 +0000150#ifndef arch_error
151#define arch_error(x)
152#endif
153
Russell King5de813b2010-02-25 12:14:40 +0000154void error(char *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Ben Dooksf8c905d2005-11-08 22:43:05 +0000156 arch_error(x);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 putstr("\n\n");
159 putstr(x);
160 putstr("\n\n -- System halted");
161
162 while(1); /* Halt */
163}
164
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800165asmlinkage void __div0(void)
166{
167 error("Attempting division by 0!");
168}
169
Nicolas Pitreccc1c7c2011-04-21 21:59:49 -0400170extern int do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x));
Russell King5de813b2010-02-25 12:14:40 +0000171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Nicolas Pitree40f1e92011-04-19 16:13:23 -0400173void
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800174decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
175 unsigned long free_mem_ptr_end_p,
176 int arch_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Nicolas Pitreccc1c7c2011-04-21 21:59:49 -0400178 int ret;
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800179
180 output_data = (unsigned char *)output_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 free_mem_ptr = free_mem_ptr_p;
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700182 free_mem_end_ptr = free_mem_ptr_end_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 __machine_arch_type = arch_id;
184
185 arch_decomp_setup();
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 putstr("Uncompressing Linux...");
Nicolas Pitreccc1c7c2011-04-21 21:59:49 -0400188 ret = do_decompress(input_data, input_data_end - input_data,
189 output_data, error);
190 if (ret)
191 error("decompressor returned an error");
192 else
193 putstr(" done, booting the kernel.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}