blob: df899834d84ed688a8e77e90e0b584789aba6243 [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
Rob Herring387798b2012-09-06 13:41:12 -050028#ifdef CONFIG_ARCH_MULTIPLATFORM
29static inline void putc(int c) {}
30static inline void flush(void) {}
31static inline void arch_decomp_setup(void) {}
32#else
Russell Kinga09e64f2008-08-05 16:14:15 +010033#include <mach/uncompress.h>
Rob Herring387798b2012-09-06 13:41:12 -050034#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#ifdef CONFIG_DEBUG_ICEDCC
Tony Lindgren7d95ded2006-09-20 13:03:34 +010037
Stephen Boyddfad5492011-03-23 22:46:15 +010038#if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7)
Tony Lindgren7d95ded2006-09-20 13:03:34 +010039
40static void icedcc_putc(int ch)
41{
42 int status, i = 0x4000000;
43
44 do {
45 if (--i < 0)
46 return;
47
48 asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
49 } while (status & (1 << 29));
50
51 asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
52}
Tony Lindgren200b7a82010-01-19 16:40:07 +010053
Tony Lindgren200b7a82010-01-19 16:40:07 +010054
Jean-Christop PLAGNIOL-VILLARDc633c3c2009-02-25 04:20:40 +010055#elif defined(CONFIG_CPU_XSCALE)
56
57static void icedcc_putc(int ch)
58{
59 int status, i = 0x4000000;
60
61 do {
62 if (--i < 0)
63 return;
64
65 asm volatile ("mrc p14, 0, %0, c14, c0, 0" : "=r" (status));
66 } while (status & (1 << 28));
67
68 asm("mcr p14, 0, %0, c8, c0, 0" : : "r" (ch));
69}
Tony Lindgren7d95ded2006-09-20 13:03:34 +010070
71#else
72
Russell Kingde4533a2006-03-28 10:34:05 +010073static void icedcc_putc(int ch)
74{
75 int status, i = 0x4000000;
76
77 do {
78 if (--i < 0)
79 return;
80
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010081 asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
Russell Kingde4533a2006-03-28 10:34:05 +010082 } while (status & 2);
83
Uwe Zeisbergerb2556da2006-05-02 20:40:56 +010084 asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
Russell Kingde4533a2006-03-28 10:34:05 +010085}
86
Tony Lindgren7d95ded2006-09-20 13:03:34 +010087#endif
88
Russell Kinga0815682006-03-28 10:24:33 +010089#define putc(ch) icedcc_putc(ch)
Russell Kinga0815682006-03-28 10:24:33 +010090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Russell Kinga0815682006-03-28 10:24:33 +010092static void putstr(const char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Russell Kinga0815682006-03-28 10:24:33 +010094 char c;
95
96 while ((c = *ptr++) != '\0') {
97 if (c == '\n')
98 putc('\r');
99 putc(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
Russell Kinga0815682006-03-28 10:24:33 +0100101
102 flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
Nicolas Pitree40f1e92011-04-19 16:13:23 -0400106 * gzip declarations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108extern char input_data[];
109extern char input_data_end[];
110
Russell King5de813b2010-02-25 12:14:40 +0000111unsigned char *output_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Russell King5de813b2010-02-25 12:14:40 +0000113unsigned long free_mem_ptr;
114unsigned long free_mem_end_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Ben Dooksf8c905d2005-11-08 22:43:05 +0000116#ifndef arch_error
117#define arch_error(x)
118#endif
119
Russell King5de813b2010-02-25 12:14:40 +0000120void error(char *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Ben Dooksf8c905d2005-11-08 22:43:05 +0000122 arch_error(x);
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 putstr("\n\n");
125 putstr(x);
126 putstr("\n\n -- System halted");
127
128 while(1); /* Halt */
129}
130
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800131asmlinkage void __div0(void)
132{
133 error("Attempting division by 0!");
134}
135
Nicolas Pitreccc1c7c2011-04-21 21:59:49 -0400136extern int do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x));
Russell King5de813b2010-02-25 12:14:40 +0000137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Nicolas Pitree40f1e92011-04-19 16:13:23 -0400139void
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800140decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
141 unsigned long free_mem_ptr_end_p,
142 int arch_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Nicolas Pitreccc1c7c2011-04-21 21:59:49 -0400144 int ret;
Albin Tonnerree7db7b42010-01-08 14:42:43 -0800145
146 output_data = (unsigned char *)output_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 free_mem_ptr = free_mem_ptr_p;
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700148 free_mem_end_ptr = free_mem_ptr_end_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 __machine_arch_type = arch_id;
150
151 arch_decomp_setup();
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 putstr("Uncompressing Linux...");
Nicolas Pitreccc1c7c2011-04-21 21:59:49 -0400154 ret = do_decompress(input_data, input_data_end - input_data,
155 output_data, error);
156 if (ret)
157 error("decompressor returned an error");
158 else
159 putstr(" done, booting the kernel.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}