2 * Copyright (C) 2009 Thomas Chou <thomas@wytron.com.tw>
4 * This is a collection of several routines from gzip-1.0.3
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>
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 */
56 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
57 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip
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())
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 ; }
75 # define Assert(cond, msg)
80 # define Tracecv(c, x)
82 static int fill_inbuf(void);
83 static void flush_window(void);
84 static void error(char *m
);
86 extern char input_data
[];
89 static long bytes_out
;
90 static uch
*output_data
;
91 static unsigned long output_ptr
;
95 static void error(char *m
);
97 int puts(const char *);
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
)
110 char *ss
= (char *)s
;
112 for (i
= 0; i
< n
; i
++)
117 void *memcpy(void *__dest
, __const
void *__src
, size_t __n
)
120 char *d
= (char *)__dest
, *s
= (char *)__src
;
122 for (i
= 0; i
< __n
; i
++)
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)
134 error("ran out of input data");
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 */
153 out
= &output_data
[output_ptr
];
154 for (n
= 0; n
< outcnt
; n
++) {
156 c
= crc_32_tab
[((int)c
^ ch
) & 0xff] ^ (c
>> 8);
159 bytes_out
+= (ulg
)outcnt
;
160 output_ptr
+= (ulg
)outcnt
;
164 static void error(char *x
)
168 puts("\n\n -- System halted");
174 void decompress_kernel(void)
176 output_data
= (void *) (CONFIG_NIOS2_MEM_BASE
|
177 CONFIG_NIOS2_KERNEL_REGION_BASE
);
179 free_mem_ptr
= (unsigned long)&_end
;
180 free_mem_end_ptr
= free_mem_ptr
+ HEAP_SIZE
;
184 puts("Uncompressing Linux... ");
186 puts("Ok, booting the kernel.\n");