Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * arch/sh64/boot/compressed/misc.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * Adapted for SHmedia from sh by Stuart Menefy, May 2002 |
| 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <asm/uaccess.h> |
| 13 | |
| 14 | /* cache.c */ |
| 15 | #define CACHE_ENABLE 0 |
| 16 | #define CACHE_DISABLE 1 |
| 17 | int cache_control(unsigned int command); |
| 18 | |
| 19 | /* |
| 20 | * gzip declarations |
| 21 | */ |
| 22 | |
| 23 | #define OF(args) args |
| 24 | #define STATIC static |
| 25 | |
| 26 | #undef memset |
| 27 | #undef memcpy |
| 28 | #define memzero(s, n) memset ((s), 0, (n)) |
| 29 | |
| 30 | typedef unsigned char uch; |
| 31 | typedef unsigned short ush; |
| 32 | typedef unsigned long ulg; |
| 33 | |
| 34 | #define WSIZE 0x8000 /* Window size must be at least 32k, */ |
| 35 | /* and a power of two */ |
| 36 | |
| 37 | static uch *inbuf; /* input buffer */ |
| 38 | static uch window[WSIZE]; /* Sliding window buffer */ |
| 39 | |
| 40 | static unsigned insize = 0; /* valid bytes in inbuf */ |
| 41 | static unsigned inptr = 0; /* index of next byte to be processed in inbuf */ |
| 42 | static unsigned outcnt = 0; /* bytes in output buffer */ |
| 43 | |
| 44 | /* gzip flag byte */ |
| 45 | #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */ |
| 46 | #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */ |
| 47 | #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ |
| 48 | #define ORIG_NAME 0x08 /* bit 3 set: original file name present */ |
| 49 | #define COMMENT 0x10 /* bit 4 set: file comment present */ |
| 50 | #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */ |
| 51 | #define RESERVED 0xC0 /* bit 6,7: reserved */ |
| 52 | |
| 53 | #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf()) |
| 54 | |
| 55 | /* Diagnostic functions */ |
| 56 | #ifdef DEBUG |
| 57 | # define Assert(cond,msg) {if(!(cond)) error(msg);} |
| 58 | # define Trace(x) fprintf x |
| 59 | # define Tracev(x) {if (verbose) fprintf x ;} |
| 60 | # define Tracevv(x) {if (verbose>1) fprintf x ;} |
| 61 | # define Tracec(c,x) {if (verbose && (c)) fprintf x ;} |
| 62 | # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;} |
| 63 | #else |
| 64 | # define Assert(cond,msg) |
| 65 | # define Trace(x) |
| 66 | # define Tracev(x) |
| 67 | # define Tracevv(x) |
| 68 | # define Tracec(c,x) |
| 69 | # define Tracecv(c,x) |
| 70 | #endif |
| 71 | |
| 72 | static int fill_inbuf(void); |
| 73 | static void flush_window(void); |
| 74 | static void error(char *m); |
| 75 | static void gzip_mark(void **); |
| 76 | static void gzip_release(void **); |
| 77 | |
| 78 | extern char input_data[]; |
| 79 | extern int input_len; |
| 80 | |
| 81 | static long bytes_out = 0; |
| 82 | static uch *output_data; |
| 83 | static unsigned long output_ptr = 0; |
| 84 | |
| 85 | static void *malloc(int size); |
| 86 | static void free(void *where); |
| 87 | static void error(char *m); |
| 88 | static void gzip_mark(void **); |
| 89 | static void gzip_release(void **); |
| 90 | |
| 91 | static void puts(const char *); |
| 92 | |
| 93 | extern int _text; /* Defined in vmlinux.lds.S */ |
| 94 | extern int _end; |
| 95 | static unsigned long free_mem_ptr; |
| 96 | static unsigned long free_mem_end_ptr; |
| 97 | |
| 98 | #define HEAP_SIZE 0x10000 |
| 99 | |
| 100 | #include "../../../../lib/inflate.c" |
| 101 | |
| 102 | static void *malloc(int size) |
| 103 | { |
| 104 | void *p; |
| 105 | |
| 106 | if (size < 0) |
| 107 | error("Malloc error\n"); |
| 108 | if (free_mem_ptr == 0) |
| 109 | error("Memory error\n"); |
| 110 | |
| 111 | free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */ |
| 112 | |
| 113 | p = (void *) free_mem_ptr; |
| 114 | free_mem_ptr += size; |
| 115 | |
| 116 | if (free_mem_ptr >= free_mem_end_ptr) |
| 117 | error("\nOut of memory\n"); |
| 118 | |
| 119 | return p; |
| 120 | } |
| 121 | |
| 122 | static void free(void *where) |
| 123 | { /* Don't care */ |
| 124 | } |
| 125 | |
| 126 | static void gzip_mark(void **ptr) |
| 127 | { |
| 128 | *ptr = (void *) free_mem_ptr; |
| 129 | } |
| 130 | |
| 131 | static void gzip_release(void **ptr) |
| 132 | { |
| 133 | free_mem_ptr = (long) *ptr; |
| 134 | } |
| 135 | |
| 136 | void puts(const char *s) |
| 137 | { |
| 138 | } |
| 139 | |
| 140 | void *memset(void *s, int c, size_t n) |
| 141 | { |
| 142 | int i; |
| 143 | char *ss = (char *) s; |
| 144 | |
| 145 | for (i = 0; i < n; i++) |
| 146 | ss[i] = c; |
| 147 | return s; |
| 148 | } |
| 149 | |
| 150 | void *memcpy(void *__dest, __const void *__src, size_t __n) |
| 151 | { |
| 152 | int i; |
| 153 | char *d = (char *) __dest, *s = (char *) __src; |
| 154 | |
| 155 | for (i = 0; i < __n; i++) |
| 156 | d[i] = s[i]; |
| 157 | return __dest; |
| 158 | } |
| 159 | |
| 160 | /* =========================================================================== |
| 161 | * Fill the input buffer. This is called only when the buffer is empty |
| 162 | * and at least one byte is really needed. |
| 163 | */ |
| 164 | static int fill_inbuf(void) |
| 165 | { |
| 166 | if (insize != 0) { |
| 167 | error("ran out of input data\n"); |
| 168 | } |
| 169 | |
| 170 | inbuf = input_data; |
| 171 | insize = input_len; |
| 172 | inptr = 1; |
| 173 | return inbuf[0]; |
| 174 | } |
| 175 | |
| 176 | /* =========================================================================== |
| 177 | * Write the output window window[0..outcnt-1] and update crc and bytes_out. |
| 178 | * (Used for the decompressed data only.) |
| 179 | */ |
| 180 | static void flush_window(void) |
| 181 | { |
| 182 | ulg c = crc; /* temporary variable */ |
| 183 | unsigned n; |
| 184 | uch *in, *out, ch; |
| 185 | |
| 186 | in = window; |
| 187 | out = &output_data[output_ptr]; |
| 188 | for (n = 0; n < outcnt; n++) { |
| 189 | ch = *out++ = *in++; |
| 190 | c = crc_32_tab[((int) c ^ ch) & 0xff] ^ (c >> 8); |
| 191 | } |
| 192 | crc = c; |
| 193 | bytes_out += (ulg) outcnt; |
| 194 | output_ptr += (ulg) outcnt; |
| 195 | outcnt = 0; |
| 196 | puts("."); |
| 197 | } |
| 198 | |
| 199 | static void error(char *x) |
| 200 | { |
| 201 | puts("\n\n"); |
| 202 | puts(x); |
| 203 | puts("\n\n -- System halted"); |
| 204 | |
| 205 | while (1) ; /* Halt */ |
| 206 | } |
| 207 | |
| 208 | #define STACK_SIZE (4096) |
| 209 | long __attribute__ ((aligned(8))) user_stack[STACK_SIZE]; |
| 210 | long *stack_start = &user_stack[STACK_SIZE]; |
| 211 | |
| 212 | void decompress_kernel(void) |
| 213 | { |
| 214 | output_data = (uch *) (CONFIG_MEMORY_START + 0x2000); |
| 215 | free_mem_ptr = (unsigned long) &_end; |
| 216 | free_mem_end_ptr = free_mem_ptr + HEAP_SIZE; |
| 217 | |
| 218 | makecrc(); |
| 219 | puts("Uncompressing Linux... "); |
| 220 | cache_control(CACHE_ENABLE); |
| 221 | gunzip(); |
| 222 | puts("\n"); |
| 223 | |
| 224 | #if 0 |
| 225 | /* When booting from ROM may want to do something like this if the |
| 226 | * boot loader doesn't. |
| 227 | */ |
| 228 | |
| 229 | /* Set up the parameters and command line */ |
| 230 | { |
| 231 | volatile unsigned int *parambase = |
| 232 | (int *) (CONFIG_MEMORY_START + 0x1000); |
| 233 | |
| 234 | parambase[0] = 0x1; /* MOUNT_ROOT_RDONLY */ |
| 235 | parambase[1] = 0x0; /* RAMDISK_FLAGS */ |
| 236 | parambase[2] = 0x0200; /* ORIG_ROOT_DEV */ |
| 237 | parambase[3] = 0x0; /* LOADER_TYPE */ |
| 238 | parambase[4] = 0x0; /* INITRD_START */ |
| 239 | parambase[5] = 0x0; /* INITRD_SIZE */ |
| 240 | parambase[6] = 0; |
| 241 | |
| 242 | strcpy((char *) ((int) parambase + 0x100), |
| 243 | "console=ttySC0,38400"); |
| 244 | } |
| 245 | #endif |
| 246 | |
| 247 | puts("Ok, booting the kernel.\n"); |
| 248 | |
| 249 | cache_control(CACHE_DISABLE); |
| 250 | } |