MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / nios2nommu / boot / compressed / misc.c
blobc513e6eaa8eafed58b11fe34e80b5bb88bb4cf57
1 /*
2 * arch/nios2nommu/boot/compressed/misc.c
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
14 #include <linux/string.h>
17 * gzip declarations
20 #define OF(args) args
21 #define STATIC static
23 #undef memset
24 #undef memcpy
25 #define memzero(s, n) memset ((s), 0, (n))
27 typedef unsigned char uch;
28 typedef unsigned short ush;
29 typedef unsigned long ulg;
31 #define WSIZE 0x8000 /* Window size must be at least 32k, */
32 /* and a power of two */
34 static uch *inbuf; /* input buffer */
35 static uch window[WSIZE]; /* Sliding window buffer */
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 */
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 */
50 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
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
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 **);
75 extern char input_data[];
76 extern int input_len;
78 static long bytes_out = 0;
79 static uch *output_data;
80 static unsigned long output_ptr = 0;
82 #include "nios2_sio.c"
84 static void *malloc(int size);
85 static void free(void *where);
86 static void error(char *m);
87 static void gzip_mark(void **);
88 static void gzip_release(void **);
90 int puts(const char *);
92 extern int _end;
93 static unsigned long free_mem_ptr;
94 static unsigned long free_mem_end_ptr;
96 #define HEAP_SIZE 0x10000
98 #include "../../../../lib/inflate.c"
100 static void *malloc(int size)
102 void *p;
104 if (size <0) error("Malloc error");
105 if (free_mem_ptr == 0) error("Memory error");
107 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
109 p = (void *)free_mem_ptr;
110 free_mem_ptr += size;
112 if (free_mem_ptr >= free_mem_end_ptr)
113 error("Out of memory");
115 return p;
118 static void free(void *where)
119 { /* Don't care */
122 static void gzip_mark(void **ptr)
124 *ptr = (void *) free_mem_ptr;
127 static void gzip_release(void **ptr)
129 free_mem_ptr = (long) *ptr;
132 void* memset(void* s, int c, size_t n)
134 int i;
135 char *ss = (char*)s;
137 for (i=0;i<n;i++) ss[i] = c;
138 return s;
141 void* memcpy(void* __dest, __const void* __src,
142 size_t __n)
144 int i;
145 char *d = (char *)__dest, *s = (char *)__src;
147 for (i=0;i<__n;i++) d[i] = s[i];
148 return __dest;
151 /* ===========================================================================
152 * Fill the input buffer. This is called only when the buffer is empty
153 * and at least one byte is really needed.
155 static int fill_inbuf(void)
157 if (insize != 0) {
158 error("ran out of input data");
161 inbuf = input_data;
162 insize = input_len;
163 inptr = 1;
164 return inbuf[0];
167 /* ===========================================================================
168 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
169 * (Used for the decompressed data only.)
171 static void flush_window(void)
173 ulg c = crc; /* temporary variable */
174 unsigned n;
175 uch *in, *out, ch;
176 in = window;
177 out = &output_data[output_ptr];
178 for (n = 0; n < outcnt; n++) {
179 ch = *out++ = *in++;
180 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
182 crc = c;
183 bytes_out += (ulg)outcnt;
184 output_ptr += (ulg)outcnt;
185 outcnt = 0;
188 static void error(char *x)
190 puts("\nERROR\n");
191 puts(x);
192 puts("\n\n -- System halted");
194 while(1); /* Halt */
197 void decompress_kernel(void)
199 output_data = (void *)nasys_program_mem;
200 output_ptr = 0;
201 free_mem_ptr = (unsigned long)&_end;
202 free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
204 makecrc();
205 puts("Uncompressing Linux... ");
206 gunzip();
207 puts("Ok, booting the kernel.\n");