| 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 |  | 
| Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 21 | #define _LINUX_STRING_H_ | 
|  | 22 |  | 
| Rusty Russell | aa0d3bb | 2009-03-31 13:05:35 -0600 | [diff] [blame] | 23 | #include <linux/compiler.h>	/* for inline */ | 
|  | 24 | #include <linux/types.h>	/* for size_t */ | 
|  | 25 | #include <linux/stddef.h>	/* for NULL */ | 
| Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 26 | #include <linux/linkage.h> | 
| Russell King | 5de813b | 2010-02-25 12:14:40 +0000 | [diff] [blame] | 27 | #include <asm/string.h> | 
| Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 28 |  | 
|  | 29 | #include <asm/unaligned.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #ifdef STANDALONE_DEBUG | 
|  | 32 | #define putstr printf | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 33 | #else | 
|  | 34 |  | 
|  | 35 | static void putstr(const char *ptr); | 
| Mark Brown | a2302b4 | 2010-03-10 19:10:28 +0100 | [diff] [blame] | 36 | extern void error(char *x); | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 37 |  | 
| Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 38 | #include <mach/uncompress.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 |  | 
|  | 40 | #ifdef CONFIG_DEBUG_ICEDCC | 
| Tony Lindgren | 7d95ded | 2006-09-20 13:03:34 +0100 | [diff] [blame] | 41 |  | 
|  | 42 | #ifdef CONFIG_CPU_V6 | 
|  | 43 |  | 
|  | 44 | static void icedcc_putc(int ch) | 
|  | 45 | { | 
|  | 46 | int status, i = 0x4000000; | 
|  | 47 |  | 
|  | 48 | do { | 
|  | 49 | if (--i < 0) | 
|  | 50 | return; | 
|  | 51 |  | 
|  | 52 | asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status)); | 
|  | 53 | } while (status & (1 << 29)); | 
|  | 54 |  | 
|  | 55 | asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch)); | 
|  | 56 | } | 
| Tony Lindgren | 200b7a8 | 2010-01-19 16:40:07 +0100 | [diff] [blame] | 57 |  | 
|  | 58 | #elif defined(CONFIG_CPU_V7) | 
|  | 59 |  | 
|  | 60 | static void icedcc_putc(int ch) | 
|  | 61 | { | 
|  | 62 | asm( | 
|  | 63 | "wait:	mrc	p14, 0, pc, c0, c1, 0			\n\ | 
|  | 64 | bcs	wait					\n\ | 
|  | 65 | mcr     p14, 0, %0, c0, c5, 0			" | 
|  | 66 | : : "r" (ch)); | 
|  | 67 | } | 
|  | 68 |  | 
| Jean-Christop PLAGNIOL-VILLARD | c633c3c | 2009-02-25 04:20:40 +0100 | [diff] [blame] | 69 | #elif defined(CONFIG_CPU_XSCALE) | 
|  | 70 |  | 
|  | 71 | static void icedcc_putc(int ch) | 
|  | 72 | { | 
|  | 73 | int status, i = 0x4000000; | 
|  | 74 |  | 
|  | 75 | do { | 
|  | 76 | if (--i < 0) | 
|  | 77 | return; | 
|  | 78 |  | 
|  | 79 | asm volatile ("mrc p14, 0, %0, c14, c0, 0" : "=r" (status)); | 
|  | 80 | } while (status & (1 << 28)); | 
|  | 81 |  | 
|  | 82 | asm("mcr p14, 0, %0, c8, c0, 0" : : "r" (ch)); | 
|  | 83 | } | 
| Tony Lindgren | 7d95ded | 2006-09-20 13:03:34 +0100 | [diff] [blame] | 84 |  | 
|  | 85 | #else | 
|  | 86 |  | 
| Russell King | de4533a | 2006-03-28 10:34:05 +0100 | [diff] [blame] | 87 | static void icedcc_putc(int ch) | 
|  | 88 | { | 
|  | 89 | int status, i = 0x4000000; | 
|  | 90 |  | 
|  | 91 | do { | 
|  | 92 | if (--i < 0) | 
|  | 93 | return; | 
|  | 94 |  | 
| Uwe Zeisberger | b2556da | 2006-05-02 20:40:56 +0100 | [diff] [blame] | 95 | asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status)); | 
| Russell King | de4533a | 2006-03-28 10:34:05 +0100 | [diff] [blame] | 96 | } while (status & 2); | 
|  | 97 |  | 
| Uwe Zeisberger | b2556da | 2006-05-02 20:40:56 +0100 | [diff] [blame] | 98 | asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch)); | 
| Russell King | de4533a | 2006-03-28 10:34:05 +0100 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Tony Lindgren | 7d95ded | 2006-09-20 13:03:34 +0100 | [diff] [blame] | 101 | #endif | 
|  | 102 |  | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 103 | #define putc(ch)	icedcc_putc(ch) | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 104 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 |  | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 106 | static void putstr(const char *ptr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 108 | char c; | 
|  | 109 |  | 
|  | 110 | while ((c = *ptr++) != '\0') { | 
|  | 111 | if (c == '\n') | 
|  | 112 | putc('\r'); | 
|  | 113 | putc(c); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } | 
| Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 115 |  | 
|  | 116 | flush(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
|  | 119 | #endif | 
|  | 120 |  | 
| Russell King | 5de813b | 2010-02-25 12:14:40 +0000 | [diff] [blame] | 121 | void *memcpy(void *__dest, __const void *__src, size_t __n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { | 
|  | 123 | int i = 0; | 
|  | 124 | unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src; | 
|  | 125 |  | 
|  | 126 | for (i = __n >> 3; i > 0; i--) { | 
|  | 127 | *d++ = *s++; | 
|  | 128 | *d++ = *s++; | 
|  | 129 | *d++ = *s++; | 
|  | 130 | *d++ = *s++; | 
|  | 131 | *d++ = *s++; | 
|  | 132 | *d++ = *s++; | 
|  | 133 | *d++ = *s++; | 
|  | 134 | *d++ = *s++; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | if (__n & 1 << 2) { | 
|  | 138 | *d++ = *s++; | 
|  | 139 | *d++ = *s++; | 
|  | 140 | *d++ = *s++; | 
|  | 141 | *d++ = *s++; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | if (__n & 1 << 1) { | 
|  | 145 | *d++ = *s++; | 
|  | 146 | *d++ = *s++; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | if (__n & 1) | 
|  | 150 | *d++ = *s++; | 
|  | 151 |  | 
|  | 152 | return __dest; | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | /* | 
|  | 156 | * gzip delarations | 
|  | 157 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | extern char input_data[]; | 
|  | 159 | extern char input_data_end[]; | 
|  | 160 |  | 
| Russell King | 5de813b | 2010-02-25 12:14:40 +0000 | [diff] [blame] | 161 | unsigned char *output_data; | 
|  | 162 | unsigned long output_ptr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 |  | 
| Russell King | 5de813b | 2010-02-25 12:14:40 +0000 | [diff] [blame] | 164 | unsigned long free_mem_ptr; | 
|  | 165 | unsigned long free_mem_end_ptr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 |  | 
| Ben Dooks | f8c905d | 2005-11-08 22:43:05 +0000 | [diff] [blame] | 167 | #ifndef arch_error | 
|  | 168 | #define arch_error(x) | 
|  | 169 | #endif | 
|  | 170 |  | 
| Russell King | 5de813b | 2010-02-25 12:14:40 +0000 | [diff] [blame] | 171 | void error(char *x) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { | 
| Ben Dooks | f8c905d | 2005-11-08 22:43:05 +0000 | [diff] [blame] | 173 | arch_error(x); | 
|  | 174 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | putstr("\n\n"); | 
|  | 176 | putstr(x); | 
|  | 177 | putstr("\n\n -- System halted"); | 
|  | 178 |  | 
|  | 179 | while(1);	/* Halt */ | 
|  | 180 | } | 
|  | 181 |  | 
| Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 182 | asmlinkage void __div0(void) | 
|  | 183 | { | 
|  | 184 | error("Attempting division by 0!"); | 
|  | 185 | } | 
|  | 186 |  | 
| Russell King | 5de813b | 2010-02-25 12:14:40 +0000 | [diff] [blame] | 187 | extern void do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x)); | 
|  | 188 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | #ifndef STANDALONE_DEBUG | 
|  | 190 |  | 
| Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 191 | unsigned long | 
|  | 192 | decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p, | 
|  | 193 | unsigned long free_mem_ptr_end_p, | 
|  | 194 | int arch_id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { | 
| Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 196 | unsigned char *tmp; | 
|  | 197 |  | 
|  | 198 | output_data		= (unsigned char *)output_start; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | free_mem_ptr		= free_mem_ptr_p; | 
| Thomas Petazzoni | 2d6ffcc | 2008-07-25 01:45:44 -0700 | [diff] [blame] | 200 | free_mem_end_ptr	= free_mem_ptr_end_p; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | __machine_arch_type	= arch_id; | 
|  | 202 |  | 
|  | 203 | arch_decomp_setup(); | 
|  | 204 |  | 
| Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 205 | tmp = (unsigned char *) (((unsigned long)input_data_end) - 4); | 
|  | 206 | output_ptr = get_unaligned_le32(tmp); | 
|  | 207 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | putstr("Uncompressing Linux..."); | 
| Russell King | 5de813b | 2010-02-25 12:14:40 +0000 | [diff] [blame] | 209 | do_decompress(input_data, input_data_end - input_data, | 
|  | 210 | output_data, error); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | putstr(" done, booting the kernel.\n"); | 
|  | 212 | return output_ptr; | 
|  | 213 | } | 
|  | 214 | #else | 
|  | 215 |  | 
|  | 216 | char output_buffer[1500*1024]; | 
|  | 217 |  | 
|  | 218 | int main() | 
|  | 219 | { | 
|  | 220 | output_data = output_buffer; | 
|  | 221 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | putstr("Uncompressing Linux..."); | 
| Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 223 | decompress(input_data, input_data_end - input_data, | 
|  | 224 | NULL, NULL, output_data, NULL, error); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | putstr("done.\n"); | 
|  | 226 | return 0; | 
|  | 227 | } | 
|  | 228 | #endif |