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