1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/string.h>
16
17
18
19
20
21#define OF(args) args
22#define STATIC static
23
24#undef memset
25#undef memcpy
26#define memzero(s, n) memset ((s), 0, (n))
27
28typedef unsigned char uch;
29typedef unsigned short ush;
30typedef unsigned long ulg;
31
32#define WSIZE 0x8000
33
34
35static uch *inbuf;
36static uch window[WSIZE];
37
38static unsigned insize = 0;
39static unsigned inptr = 0;
40static unsigned outcnt = 0;
41
42
43#define ASCII_FLAG 0x01
44#define CONTINUATION 0x02
45#define EXTRA_FIELD 0x04
46#define ORIG_NAME 0x08
47#define COMMENT 0x10
48#define ENCRYPTED 0x20
49#define RESERVED 0xC0
50
51#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
52
53
54#ifdef DEBUG
55# define Assert(cond,msg) {if(!(cond)) error(msg);}
56# define Trace(x) fprintf x
57# define Tracev(x) {if (verbose) fprintf x ;}
58# define Tracevv(x) {if (verbose>1) fprintf x ;}
59# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
60# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
61#else
62# define Assert(cond,msg)
63# define Trace(x)
64# define Tracev(x)
65# define Tracevv(x)
66# define Tracec(c,x)
67# define Tracecv(c,x)
68#endif
69
70static int fill_inbuf(void);
71static void flush_window(void);
72static void error(char *m);
73
74static unsigned char *input_data;
75static int input_len;
76
77static long bytes_out = 0;
78static uch *output_data;
79static unsigned long output_ptr = 0;
80
81#include "m32r_sio.c"
82
83static unsigned long free_mem_ptr;
84static unsigned long free_mem_end_ptr;
85
86#define HEAP_SIZE 0x10000
87
88#include "../../../../lib/inflate.c"
89
90void* memset(void* s, int c, size_t n)
91{
92 int i;
93 char *ss = (char*)s;
94
95 for (i=0;i<n;i++) ss[i] = c;
96 return s;
97}
98
99void* memcpy(void* __dest, __const void* __src,
100 size_t __n)
101{
102 int i;
103 char *d = (char *)__dest, *s = (char *)__src;
104
105 for (i=0;i<__n;i++) d[i] = s[i];
106 return __dest;
107}
108
109
110
111
112
113static int fill_inbuf(void)
114{
115 if (insize != 0) {
116 error("ran out of input data");
117 }
118
119 inbuf = input_data;
120 insize = input_len;
121 inptr = 1;
122 return inbuf[0];
123}
124
125
126
127
128
129static void flush_window(void)
130{
131 ulg c = crc;
132 unsigned n;
133 uch *in, *out, ch;
134
135 in = window;
136 out = &output_data[output_ptr];
137 for (n = 0; n < outcnt; n++) {
138 ch = *out++ = *in++;
139 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
140 }
141 crc = c;
142 bytes_out += (ulg)outcnt;
143 output_ptr += (ulg)outcnt;
144 outcnt = 0;
145}
146
147static void error(char *x)
148{
149 puts("\n\n");
150 puts(x);
151 puts("\n\n -- System halted");
152
153 while(1);
154}
155
156
157void
158decompress_kernel(int mmu_on, unsigned char *zimage_data,
159 unsigned int zimage_len, unsigned long heap)
160{
161 output_data = (unsigned char *)CONFIG_MEMORY_START + 0x2000
162 + (mmu_on ? 0x80000000 : 0);
163 free_mem_ptr = heap;
164 free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
165 input_data = zimage_data;
166 input_len = zimage_len;
167
168 makecrc();
169 puts("Uncompressing Linux... ");
170 gunzip();
171 puts("Ok, booting the kernel.\n");
172}
173