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 */ |
| 26 | #include <asm/string.h> |
Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 27 | #include <linux/linkage.h> |
| 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); |
| 36 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 37 | #include <mach/uncompress.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #ifdef CONFIG_DEBUG_ICEDCC |
Tony Lindgren | 7d95ded | 2006-09-20 13:03:34 +0100 | [diff] [blame] | 40 | |
| 41 | #ifdef CONFIG_CPU_V6 |
| 42 | |
| 43 | static void icedcc_putc(int ch) |
| 44 | { |
| 45 | int status, i = 0x4000000; |
| 46 | |
| 47 | do { |
| 48 | if (--i < 0) |
| 49 | return; |
| 50 | |
| 51 | asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status)); |
| 52 | } while (status & (1 << 29)); |
| 53 | |
| 54 | asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch)); |
| 55 | } |
Jean-Christop PLAGNIOL-VILLARD | c633c3c | 2009-02-25 04:20:40 +0100 | [diff] [blame] | 56 | #elif defined(CONFIG_CPU_XSCALE) |
| 57 | |
| 58 | static void icedcc_putc(int ch) |
| 59 | { |
| 60 | int status, i = 0x4000000; |
| 61 | |
| 62 | do { |
| 63 | if (--i < 0) |
| 64 | return; |
| 65 | |
| 66 | asm volatile ("mrc p14, 0, %0, c14, c0, 0" : "=r" (status)); |
| 67 | } while (status & (1 << 28)); |
| 68 | |
| 69 | asm("mcr p14, 0, %0, c8, c0, 0" : : "r" (ch)); |
| 70 | } |
Tony Lindgren | 7d95ded | 2006-09-20 13:03:34 +0100 | [diff] [blame] | 71 | |
| 72 | #else |
| 73 | |
Russell King | de4533a | 2006-03-28 10:34:05 +0100 | [diff] [blame] | 74 | static void icedcc_putc(int ch) |
| 75 | { |
| 76 | int status, i = 0x4000000; |
| 77 | |
| 78 | do { |
| 79 | if (--i < 0) |
| 80 | return; |
| 81 | |
Uwe Zeisberger | b2556da | 2006-05-02 20:40:56 +0100 | [diff] [blame] | 82 | asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status)); |
Russell King | de4533a | 2006-03-28 10:34:05 +0100 | [diff] [blame] | 83 | } while (status & 2); |
| 84 | |
Uwe Zeisberger | b2556da | 2006-05-02 20:40:56 +0100 | [diff] [blame] | 85 | asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch)); |
Russell King | de4533a | 2006-03-28 10:34:05 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Tony Lindgren | 7d95ded | 2006-09-20 13:03:34 +0100 | [diff] [blame] | 88 | #endif |
| 89 | |
Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 90 | #define putc(ch) icedcc_putc(ch) |
| 91 | #define flush() do { } while (0) |
| 92 | #endif |
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 | static void putstr(const char *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 96 | char c; |
| 97 | |
| 98 | while ((c = *ptr++) != '\0') { |
| 99 | if (c == '\n') |
| 100 | putc('\r'); |
| 101 | putc(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
Russell King | a081568 | 2006-03-28 10:24:33 +0100 | [diff] [blame] | 103 | |
| 104 | flush(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | #endif |
| 108 | |
| 109 | #define __ptr_t void * |
| 110 | |
Russell King | 59f0cb0 | 2008-10-27 11:24:09 +0000 | [diff] [blame] | 111 | #define memzero(s,n) __memzero(s,n) |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | /* |
| 114 | * Optimised C version of memzero for the ARM. |
| 115 | */ |
| 116 | void __memzero (__ptr_t s, size_t n) |
| 117 | { |
| 118 | union { void *vp; unsigned long *ulp; unsigned char *ucp; } u; |
| 119 | int i; |
| 120 | |
| 121 | u.vp = s; |
| 122 | |
| 123 | for (i = n >> 5; i > 0; i--) { |
| 124 | *u.ulp++ = 0; |
| 125 | *u.ulp++ = 0; |
| 126 | *u.ulp++ = 0; |
| 127 | *u.ulp++ = 0; |
| 128 | *u.ulp++ = 0; |
| 129 | *u.ulp++ = 0; |
| 130 | *u.ulp++ = 0; |
| 131 | *u.ulp++ = 0; |
| 132 | } |
| 133 | |
| 134 | if (n & 1 << 4) { |
| 135 | *u.ulp++ = 0; |
| 136 | *u.ulp++ = 0; |
| 137 | *u.ulp++ = 0; |
| 138 | *u.ulp++ = 0; |
| 139 | } |
| 140 | |
| 141 | if (n & 1 << 3) { |
| 142 | *u.ulp++ = 0; |
| 143 | *u.ulp++ = 0; |
| 144 | } |
| 145 | |
| 146 | if (n & 1 << 2) |
| 147 | *u.ulp++ = 0; |
| 148 | |
| 149 | if (n & 1 << 1) { |
| 150 | *u.ucp++ = 0; |
| 151 | *u.ucp++ = 0; |
| 152 | } |
| 153 | |
| 154 | if (n & 1) |
| 155 | *u.ucp++ = 0; |
| 156 | } |
| 157 | |
| 158 | static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src, |
| 159 | size_t __n) |
| 160 | { |
| 161 | int i = 0; |
| 162 | unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src; |
| 163 | |
| 164 | for (i = __n >> 3; i > 0; i--) { |
| 165 | *d++ = *s++; |
| 166 | *d++ = *s++; |
| 167 | *d++ = *s++; |
| 168 | *d++ = *s++; |
| 169 | *d++ = *s++; |
| 170 | *d++ = *s++; |
| 171 | *d++ = *s++; |
| 172 | *d++ = *s++; |
| 173 | } |
| 174 | |
| 175 | if (__n & 1 << 2) { |
| 176 | *d++ = *s++; |
| 177 | *d++ = *s++; |
| 178 | *d++ = *s++; |
| 179 | *d++ = *s++; |
| 180 | } |
| 181 | |
| 182 | if (__n & 1 << 1) { |
| 183 | *d++ = *s++; |
| 184 | *d++ = *s++; |
| 185 | } |
| 186 | |
| 187 | if (__n & 1) |
| 188 | *d++ = *s++; |
| 189 | |
| 190 | return __dest; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * gzip delarations |
| 195 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | #define STATIC static |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | /* Diagnostic functions */ |
| 199 | #ifdef DEBUG |
| 200 | # define Assert(cond,msg) {if(!(cond)) error(msg);} |
| 201 | # define Trace(x) fprintf x |
| 202 | # define Tracev(x) {if (verbose) fprintf x ;} |
| 203 | # define Tracevv(x) {if (verbose>1) fprintf x ;} |
| 204 | # define Tracec(c,x) {if (verbose && (c)) fprintf x ;} |
| 205 | # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;} |
| 206 | #else |
| 207 | # define Assert(cond,msg) |
| 208 | # define Trace(x) |
| 209 | # define Tracev(x) |
| 210 | # define Tracevv(x) |
| 211 | # define Tracec(c,x) |
| 212 | # define Tracecv(c,x) |
| 213 | #endif |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | static void error(char *m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | extern char input_data[]; |
| 218 | extern char input_data_end[]; |
| 219 | |
Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 220 | static unsigned char *output_data; |
| 221 | static unsigned long output_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | static void error(char *m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | static void putstr(const char *); |
| 226 | |
Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 227 | static unsigned long free_mem_ptr; |
| 228 | static unsigned long free_mem_end_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Thomas Petazzoni | 2d6ffcc | 2008-07-25 01:45:44 -0700 | [diff] [blame] | 230 | #ifdef STANDALONE_DEBUG |
| 231 | #define NO_INFLATE_MALLOC |
| 232 | #endif |
| 233 | |
| 234 | #define ARCH_HAS_DECOMP_WDOG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 236 | #ifdef CONFIG_KERNEL_GZIP |
| 237 | #include "../../../../lib/decompress_inflate.c" |
| 238 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 240 | #ifdef CONFIG_KERNEL_LZO |
| 241 | #include "../../../../lib/decompress_unlzo.c" |
| 242 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Ben Dooks | f8c905d | 2005-11-08 22:43:05 +0000 | [diff] [blame] | 244 | #ifndef arch_error |
| 245 | #define arch_error(x) |
| 246 | #endif |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | static void error(char *x) |
| 249 | { |
Ben Dooks | f8c905d | 2005-11-08 22:43:05 +0000 | [diff] [blame] | 250 | arch_error(x); |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | putstr("\n\n"); |
| 253 | putstr(x); |
| 254 | putstr("\n\n -- System halted"); |
| 255 | |
| 256 | while(1); /* Halt */ |
| 257 | } |
| 258 | |
Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 259 | asmlinkage void __div0(void) |
| 260 | { |
| 261 | error("Attempting division by 0!"); |
| 262 | } |
| 263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | #ifndef STANDALONE_DEBUG |
| 265 | |
Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 266 | unsigned long |
| 267 | decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p, |
| 268 | unsigned long free_mem_ptr_end_p, |
| 269 | int arch_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 271 | unsigned char *tmp; |
| 272 | |
| 273 | output_data = (unsigned char *)output_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | free_mem_ptr = free_mem_ptr_p; |
Thomas Petazzoni | 2d6ffcc | 2008-07-25 01:45:44 -0700 | [diff] [blame] | 275 | free_mem_end_ptr = free_mem_ptr_end_p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | __machine_arch_type = arch_id; |
| 277 | |
| 278 | arch_decomp_setup(); |
| 279 | |
Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 280 | tmp = (unsigned char *) (((unsigned long)input_data_end) - 4); |
| 281 | output_ptr = get_unaligned_le32(tmp); |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | putstr("Uncompressing Linux..."); |
Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 284 | decompress(input_data, input_data_end - input_data, |
| 285 | NULL, NULL, output_data, NULL, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | putstr(" done, booting the kernel.\n"); |
| 287 | return output_ptr; |
| 288 | } |
| 289 | #else |
| 290 | |
| 291 | char output_buffer[1500*1024]; |
| 292 | |
| 293 | int main() |
| 294 | { |
| 295 | output_data = output_buffer; |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | putstr("Uncompressing Linux..."); |
Albin Tonnerre | e7db7b4 | 2010-01-08 14:42:43 -0800 | [diff] [blame] | 298 | decompress(input_data, input_data_end - input_data, |
| 299 | NULL, NULL, output_data, NULL, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | putstr("done.\n"); |
| 301 | return 0; |
| 302 | } |
| 303 | #endif |