1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <asm/uaccess.h>
15#include <asm/addrspace.h>
16#include <asm/page.h>
17#ifdef CONFIG_SH_STANDARD_BIOS
18#include <asm/sh_bios.h>
19#endif
20
21
22
23
24
25#define OF(args) args
26#define STATIC static
27
28#undef memset
29#undef memcpy
30#define memzero(s, n) memset ((s), 0, (n))
31
32typedef unsigned char uch;
33typedef unsigned short ush;
34typedef unsigned long ulg;
35
36#define WSIZE 0x8000
37
38
39static uch *inbuf;
40static uch window[WSIZE];
41
42static unsigned insize = 0;
43static unsigned inptr = 0;
44static unsigned outcnt = 0;
45
46
47#define ASCII_FLAG 0x01
48#define CONTINUATION 0x02
49#define EXTRA_FIELD 0x04
50#define ORIG_NAME 0x08
51#define COMMENT 0x10
52#define ENCRYPTED 0x20
53#define RESERVED 0xC0
54
55#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
56
57
58#ifdef DEBUG
59# define Assert(cond,msg) {if(!(cond)) error(msg);}
60# define Trace(x) fprintf x
61# define Tracev(x) {if (verbose) fprintf x ;}
62# define Tracevv(x) {if (verbose>1) fprintf x ;}
63# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
64# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
65#else
66# define Assert(cond,msg)
67# define Trace(x)
68# define Tracev(x)
69# define Tracevv(x)
70# define Tracec(c,x)
71# define Tracecv(c,x)
72#endif
73
74static int fill_inbuf(void);
75static void flush_window(void);
76static void error(char *m);
77static void gzip_mark(void **);
78static void gzip_release(void **);
79
80extern char input_data[];
81extern int input_len;
82
83static long bytes_out = 0;
84static uch *output_data;
85static unsigned long output_ptr = 0;
86
87static void *malloc(int size);
88static void free(void *where);
89static void error(char *m);
90static void gzip_mark(void **);
91static void gzip_release(void **);
92
93int puts(const char *);
94
95extern int _text;
96extern int _end;
97static unsigned long free_mem_ptr;
98static unsigned long free_mem_end_ptr;
99
100#define HEAP_SIZE 0x10000
101
102#include "../../../../lib/inflate.c"
103
104static void *malloc(int size)
105{
106 void *p;
107
108 if (size <0) error("Malloc error");
109 if (free_mem_ptr == 0) error("Memory error");
110
111 free_mem_ptr = (free_mem_ptr + 3) & ~3;
112
113 p = (void *)free_mem_ptr;
114 free_mem_ptr += size;
115
116 if (free_mem_ptr >= free_mem_end_ptr)
117 error("Out of memory");
118
119 return p;
120}
121
122static void free(void *where)
123{
124}
125
126static void gzip_mark(void **ptr)
127{
128 *ptr = (void *) free_mem_ptr;
129}
130
131static void gzip_release(void **ptr)
132{
133 free_mem_ptr = (long) *ptr;
134}
135
136#ifdef CONFIG_SH_STANDARD_BIOS
137size_t strlen(const char *s)
138{
139 int i = 0;
140
141 while (*s++)
142 i++;
143 return i;
144}
145
146int puts(const char *s)
147{
148 int len = strlen(s);
149 sh_bios_console_write(s, len);
150 return len;
151}
152#else
153int puts(const char *s)
154{
155
156 return 0;
157}
158#endif
159
160void* memset(void* s, int c, size_t n)
161{
162 int i;
163 char *ss = (char*)s;
164
165 for (i=0;i<n;i++) ss[i] = c;
166 return s;
167}
168
169void* memcpy(void* __dest, __const void* __src,
170 size_t __n)
171{
172 int i;
173 char *d = (char *)__dest, *s = (char *)__src;
174
175 for (i=0;i<__n;i++) d[i] = s[i];
176 return __dest;
177}
178
179
180
181
182
183static int fill_inbuf(void)
184{
185 if (insize != 0) {
186 error("ran out of input data");
187 }
188
189 inbuf = input_data;
190 insize = input_len;
191 inptr = 1;
192 return inbuf[0];
193}
194
195
196
197
198
199static void flush_window(void)
200{
201 ulg c = crc;
202 unsigned n;
203 uch *in, *out, ch;
204
205 in = window;
206 out = &output_data[output_ptr];
207 for (n = 0; n < outcnt; n++) {
208 ch = *out++ = *in++;
209 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
210 }
211 crc = c;
212 bytes_out += (ulg)outcnt;
213 output_ptr += (ulg)outcnt;
214 outcnt = 0;
215}
216
217static void error(char *x)
218{
219 puts("\n\n");
220 puts(x);
221 puts("\n\n -- System halted");
222
223 while(1);
224}
225
226#define STACK_SIZE (4096)
227long user_stack [STACK_SIZE];
228long* stack_start = &user_stack[STACK_SIZE];
229
230void decompress_kernel(void)
231{
232 output_data = 0;
233 output_ptr = PHYSADDR((unsigned long)&_text+PAGE_SIZE);
234#ifdef CONFIG_29BIT
235 output_ptr |= P2SEG;
236#endif
237 free_mem_ptr = (unsigned long)&_end;
238 free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
239
240 makecrc();
241 puts("Uncompressing Linux... ");
242 gunzip();
243 puts("Ok, booting the kernel.\n");
244}
245