| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
|  | 19 | unsigned int __machine_arch_type; | 
|  | 20 |  | 
| Rusty Russell | aa0d3bb | 2009-03-31 13:05:35 -0600 | [diff] [blame] | 21 | #include <linux/compiler.h>	/* for inline */ | 
| Nicolas Pitre | df4879f | 2011-09-13 21:42:55 -0400 | [diff] [blame] | 22 | #include <linux/types.h> | 
| Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 23 | #include <linux/linkage.h> | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 24 |  | 
|  | 25 | static void putstr(const char *ptr); | 
| Mark Brown | a2302b4 | 2010-03-10 19:10:28 +0100 | [diff] [blame] | 26 | extern void error(char *x); | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 27 |  | 
| Rob Herring | 387798b | 2012-09-06 13:41:12 -0500 | [diff] [blame] | 28 | #ifdef CONFIG_ARCH_MULTIPLATFORM | 
|  | 29 | static inline void putc(int c) {} | 
|  | 30 | static inline void flush(void) {} | 
|  | 31 | static inline void arch_decomp_setup(void) {} | 
|  | 32 | #else | 
| Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 33 | #include <mach/uncompress.h> | 
| Rob Herring | 387798b | 2012-09-06 13:41:12 -0500 | [diff] [blame] | 34 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 |  | 
|  | 36 | #ifdef CONFIG_DEBUG_ICEDCC | 
| Tony Lindgren | 7d95ded | 2006-09-20 13:03:34 +0100 | [diff] [blame] | 37 |  | 
| Stephen Boyd | dfad549 | 2011-03-23 22:46:15 +0100 | [diff] [blame] | 38 | #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7) | 
| Tony Lindgren | 7d95ded | 2006-09-20 13:03:34 +0100 | [diff] [blame] | 39 |  | 
|  | 40 | static 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 Lindgren | 200b7a8 | 2010-01-19 16:40:07 +0100 | [diff] [blame] | 53 |  | 
| Tony Lindgren | 200b7a8 | 2010-01-19 16:40:07 +0100 | [diff] [blame] | 54 |  | 
| Jean-Christop PLAGNIOL-VILLARD | c633c3c | 2009-02-25 04:20:40 +0100 | [diff] [blame] | 55 | #elif defined(CONFIG_CPU_XSCALE) | 
|  | 56 |  | 
|  | 57 | static 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 Lindgren | 7d95ded | 2006-09-20 13:03:34 +0100 | [diff] [blame] | 70 |  | 
|  | 71 | #else | 
|  | 72 |  | 
| Russell King | de4533a | 2006-03-28 10:34:05 +0100 | [diff] [blame] | 73 | static void icedcc_putc(int ch) | 
|  | 74 | { | 
|  | 75 | int status, i = 0x4000000; | 
|  | 76 |  | 
|  | 77 | do { | 
|  | 78 | if (--i < 0) | 
|  | 79 | return; | 
|  | 80 |  | 
| Uwe Zeisberger | b2556da | 2006-05-02 20:40:56 +0100 | [diff] [blame] | 81 | asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status)); | 
| Russell King | de4533a | 2006-03-28 10:34:05 +0100 | [diff] [blame] | 82 | } while (status & 2); | 
|  | 83 |  | 
| Uwe Zeisberger | b2556da | 2006-05-02 20:40:56 +0100 | [diff] [blame] | 84 | asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch)); | 
| Russell King | de4533a | 2006-03-28 10:34:05 +0100 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
| Tony Lindgren | 7d95ded | 2006-09-20 13:03:34 +0100 | [diff] [blame] | 87 | #endif | 
|  | 88 |  | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 89 | #define putc(ch)	icedcc_putc(ch) | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 90 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 |  | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 92 | static void putstr(const char *ptr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 94 | char c; | 
|  | 95 |  | 
|  | 96 | while ((c = *ptr++) != '\0') { | 
|  | 97 | if (c == '\n') | 
|  | 98 | putc('\r'); | 
|  | 99 | putc(c); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 101 |  | 
|  | 102 | flush(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | /* | 
| Nicolas Pitre | e40f1e9 | 2011-04-19 16:13:23 -0400 | [diff] [blame] | 106 | * gzip declarations | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | extern char input_data[]; | 
|  | 109 | extern char input_data_end[]; | 
|  | 110 |  | 
| Russell King | 5de813b | 2010-02-25 12:14:40 +0000 | [diff] [blame] | 111 | unsigned char *output_data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 |  | 
| Russell King | 5de813b | 2010-02-25 12:14:40 +0000 | [diff] [blame] | 113 | unsigned long free_mem_ptr; | 
|  | 114 | unsigned long free_mem_end_ptr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 |  | 
| Ben Dooks | f8c905d | 2005-11-08 22:43:05 +0000 | [diff] [blame] | 116 | #ifndef arch_error | 
|  | 117 | #define arch_error(x) | 
|  | 118 | #endif | 
|  | 119 |  | 
| Russell King | 5de813b | 2010-02-25 12:14:40 +0000 | [diff] [blame] | 120 | void error(char *x) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { | 
| Ben Dooks | f8c905d | 2005-11-08 22:43:05 +0000 | [diff] [blame] | 122 | arch_error(x); | 
|  | 123 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | putstr("\n\n"); | 
|  | 125 | putstr(x); | 
|  | 126 | putstr("\n\n -- System halted"); | 
|  | 127 |  | 
|  | 128 | while(1);	/* Halt */ | 
|  | 129 | } | 
|  | 130 |  | 
| Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 131 | asmlinkage void __div0(void) | 
|  | 132 | { | 
|  | 133 | error("Attempting division by 0!"); | 
|  | 134 | } | 
|  | 135 |  | 
| Nicolas Pitre | ccc1c7c | 2011-04-21 21:59:49 -0400 | [diff] [blame] | 136 | extern int do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x)); | 
| Russell King | 5de813b | 2010-02-25 12:14:40 +0000 | [diff] [blame] | 137 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 |  | 
| Nicolas Pitre | e40f1e9 | 2011-04-19 16:13:23 -0400 | [diff] [blame] | 139 | void | 
| Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 140 | decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p, | 
|  | 141 | unsigned long free_mem_ptr_end_p, | 
|  | 142 | int arch_id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { | 
| Nicolas Pitre | ccc1c7c | 2011-04-21 21:59:49 -0400 | [diff] [blame] | 144 | int ret; | 
| Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 145 |  | 
|  | 146 | output_data		= (unsigned char *)output_start; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | free_mem_ptr		= free_mem_ptr_p; | 
| Thomas Petazzoni | 2d6ffcc | 2008-07-25 01:45:44 -0700 | [diff] [blame] | 148 | free_mem_end_ptr	= free_mem_ptr_end_p; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | __machine_arch_type	= arch_id; | 
|  | 150 |  | 
|  | 151 | arch_decomp_setup(); | 
|  | 152 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | putstr("Uncompressing Linux..."); | 
| Nicolas Pitre | ccc1c7c | 2011-04-21 21:59:49 -0400 | [diff] [blame] | 154 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |