blob: 8e2a8fca5ed205bcbfda7faafa1eb0d7628a863b [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
Rusty Russellaa0d3bb2009-03-31 13:05:35 -060021#include <linux/compiler.h> /* for inline */
Nicolas Pitredf4879f2011-09-13 21:42:55 -040022#include <linux/types.h>
Albin Tonnerree7db7b42010-01-08 14:42:43 -080023#include <linux/linkage.h>
Russell Kinga0815682006-03-28 10:24:33 +010024
25static void putstr(const char *ptr);
Mark Browna2302b42010-03-10 19:10:28 +010026extern void error(char *x);
Russell Kinga0815682006-03-28 10:24:33 +010027
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/uncompress.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#ifdef CONFIG_DEBUG_ICEDCC
Tony Lindgren7d95ded2006-09-20 13:03:34 +010031
Stephen Boyddfad5492011-03-23 22:46:15 +010032#if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7)
Tony Lindgren7d95ded2006-09-20 13:03:34 +010033
34static void icedcc_putc(int ch)
35{
36 int status, i = 0x4000000;
37
38 do {
39 if (--i < 0)
40 return;
41
42 asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
43 } while (status & (1 << 29));
44
45 asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
46}
Tony Lindgren200b7a82010-01-19 16:40:07 +010047
Tony Lindgren200b7a82010-01-19 16:40:07 +010048
Jean-Christop PLAGNIOL-VILLARDc633c3c2009-02-25 04:20:40 +010049#elif defined(CONFIG_CPU_XSCALE)
50
51static void icedcc_putc(int ch)
52{
53 int status, i = 0x4000000;
54
55 do {
56 if (--i < 0)
57 return;
58
59 asm volatile ("mrc p14, 0, %0, c14, c0, 0" : "=r" (status));
60 } while (status & (1 << 28));
61
62 asm("mcr p14, 0, %0, c8, c0, 0" : : "r" (ch));
63}
Tony Lindgren7d95ded2006-09-20 13:03:34 +010064
65#else
66
Russell Kingde4533a2006-03-28 10:34:05 +010067static void icedcc_putc(int ch)
68{
69 int status, i = 0x4000000;
70
71 do {
72 if (--i < 0)
73 return;
74
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010075 asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
Russell Kingde4533a2006-03-28 10:34:05 +010076 } while (status & 2);
77
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010078 asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
Russell Kingde4533a2006-03-28 10:34:05 +010079}
80
Tony Lindgren7d95ded2006-09-20 13:03:34 +010081#endif
82
Russell Kinga0815682006-03-28 10:24:33 +010083#define putc(ch) icedcc_putc(ch)
Russell Kinga0815682006-03-28 10:24:33 +010084#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Russell Kinga0815682006-03-28 10:24:33 +010086static void putstr(const char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Russell Kinga0815682006-03-28 10:24:33 +010088 char c;
89
90 while ((c = *ptr++) != '\0') {
91 if (c == '\n')
92 putc('\r');
93 putc(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
Russell Kinga0815682006-03-28 10:24:33 +010095
96 flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
Nicolas Pitree40f1e92011-04-19 16:13:23 -0400100 * gzip declarations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102extern char input_data[];
103extern char input_data_end[];
104
Russell King5de813b2010-02-25 12:14:40 +0000105unsigned char *output_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Russell King5de813b2010-02-25 12:14:40 +0000107unsigned long free_mem_ptr;
108unsigned long free_mem_end_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Ben Dooksf8c905d2005-11-08 22:43:05 +0000110#ifndef arch_error
111#define arch_error(x)
112#endif
113
Russell King5de813b2010-02-25 12:14:40 +0000114void error(char *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Ben Dooksf8c905d2005-11-08 22:43:05 +0000116 arch_error(x);
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 putstr("\n\n");
119 putstr(x);
120 putstr("\n\n -- System halted");
121
122 while(1); /* Halt */
123}
124
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800125asmlinkage void __div0(void)
126{
127 error("Attempting division by 0!");
128}
129
Nicolas Pitreccc1c7c2011-04-21 21:59:49 -0400130extern int do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x));
Russell King5de813b2010-02-25 12:14:40 +0000131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Nicolas Pitree40f1e92011-04-19 16:13:23 -0400133void
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800134decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
135 unsigned long free_mem_ptr_end_p,
136 int arch_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Nicolas Pitreccc1c7c2011-04-21 21:59:49 -0400138 int ret;
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800139
140 output_data = (unsigned char *)output_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 free_mem_ptr = free_mem_ptr_p;
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700142 free_mem_end_ptr = free_mem_ptr_end_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 __machine_arch_type = arch_id;
144
145 arch_decomp_setup();
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 putstr("Uncompressing Linux...");
Nicolas Pitreccc1c7c2011-04-21 21:59:49 -0400148 ret = do_decompress(input_data, input_data_end - input_data,
149 output_data, error);
150 if (ret)
151 error("decompressor returned an error");
152 else
153 putstr(" done, booting the kernel.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}