Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / nios2 / boot / compressed / misc.c
blob84377825ef1ac9e522a641f60a3c011d7241f193
1 /*
2 * Copyright (C) 2009 Thomas Chou <thomas@wytron.com.tw>
4 * This is a collection of several routines from gzip-1.0.3
5 * adapted for Linux.
7 * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
9 * Adapted for SH by Stuart Menefy, Aug 1999
11 * Modified to use standard LinuxSH BIOS by Greg Banks 7Jul2000
13 * Based on arch/sh/boot/compressed/misc.c
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <linux/string.h>
33 * gzip declarations
35 #define OF(args) args
36 #define STATIC static
38 #undef memset
39 #undef memcpy
40 #define memzero(s, n) memset((s), 0, (n))
42 typedef unsigned char uch;
43 typedef unsigned short ush;
44 typedef unsigned long ulg;
45 #define WSIZE 0x8000 /* Window size must be at least 32k, */
46 /* and a power of two */
48 static uch *inbuf; /* input buffer */
49 static uch window[WSIZE]; /* Sliding window buffer */
51 static unsigned insize; /* valid bytes in inbuf */
52 static unsigned inptr; /* index of next byte to be processed in inbuf */
53 static unsigned outcnt; /* bytes in output buffer */
55 /* gzip flag byte */
56 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
57 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip
58 file */
59 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
60 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
61 #define COMMENT 0x10 /* bit 4 set: file comment present */
62 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
63 #define RESERVED 0xC0 /* bit 6,7: reserved */
65 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
67 #ifdef DEBUG
68 # define Assert(cond, msg) {if (!(cond)) error(msg); }
69 # define Trace(x) fprintf x
70 # define Tracev(x) {if (verbose) fprintf x ; }
71 # define Tracevv(x) {if (verbose > 1) fprintf x ; }
72 # define Tracec(c, x) {if (verbose && (c)) fprintf x ; }
73 # define Tracecv(c, x) {if (verbose > 1 && (c)) fprintf x ; }
74 #else
75 # define Assert(cond, msg)
76 # define Trace(x)
77 # define Tracev(x)
78 # define Tracevv(x)
79 # define Tracec(c, x)
80 # define Tracecv(c, x)
81 #endif
82 static int fill_inbuf(void);
83 static void flush_window(void);
84 static void error(char *m);
86 extern char input_data[];
87 extern int input_len;
89 static long bytes_out;
90 static uch *output_data;
91 static unsigned long output_ptr;
93 #include "console.c"
95 static void error(char *m);
97 int puts(const char *);
99 extern int _end;
100 static unsigned long free_mem_ptr;
101 static unsigned long free_mem_end_ptr;
103 #define HEAP_SIZE 0x10000
105 #include "../../../../lib/inflate.c"
107 void *memset(void *s, int c, size_t n)
109 int i;
110 char *ss = (char *)s;
112 for (i = 0; i < n; i++)
113 ss[i] = c;
114 return s;
117 void *memcpy(void *__dest, __const void *__src, size_t __n)
119 int i;
120 char *d = (char *)__dest, *s = (char *)__src;
122 for (i = 0; i < __n; i++)
123 d[i] = s[i];
124 return __dest;
128 * Fill the input buffer. This is called only when the buffer is empty
129 * and at least one byte is really needed.
131 static int fill_inbuf(void)
133 if (insize != 0)
134 error("ran out of input data");
136 inbuf = input_data;
137 insize = input_len;
138 inptr = 1;
139 return inbuf[0];
143 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
144 * (Used for the decompressed data only.)
146 static void flush_window(void)
148 ulg c = crc; /* temporary variable */
149 unsigned n;
150 uch *in, *out, ch;
152 in = window;
153 out = &output_data[output_ptr];
154 for (n = 0; n < outcnt; n++) {
155 ch = *out++ = *in++;
156 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
158 crc = c;
159 bytes_out += (ulg)outcnt;
160 output_ptr += (ulg)outcnt;
161 outcnt = 0;
164 static void error(char *x)
166 puts("\nERROR\n");
167 puts(x);
168 puts("\n\n -- System halted");
170 while (1) /* Halt */
174 void decompress_kernel(void)
176 output_data = (void *) (CONFIG_NIOS2_MEM_BASE |
177 CONFIG_NIOS2_KERNEL_REGION_BASE);
178 output_ptr = 0;
179 free_mem_ptr = (unsigned long)&_end;
180 free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
182 console_init();
183 makecrc();
184 puts("Uncompressing Linux... ");
185 gunzip();
186 puts("Ok, booting the kernel.\n");