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