xref: /openbmc/linux/arch/sh/boot/compressed/misc.c (revision 05668381140309088443bf5dc53add4104610fbb)
1 /*
2  * arch/sh/boot/compressed/misc.c
3  *
4  * This is a collection of several routines from gzip-1.0.3
5  * adapted for Linux.
6  *
7  * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
8  *
9  * Adapted for SH by Stuart Menefy, Aug 1999
10  *
11  * Modified to use standard LinuxSH BIOS by Greg Banks 7Jul2000
12  */
13 
14 #include <asm/uaccess.h>
15 #ifdef CONFIG_SH_STANDARD_BIOS
16 #include <asm/sh_bios.h>
17 #endif
18 
19 /*
20  * gzip declarations
21  */
22 
23 #define OF(args)  args
24 #define STATIC static
25 
26 #undef memset
27 #undef memcpy
28 #define memzero(s, n)     memset ((s), 0, (n))
29 
30 typedef unsigned char  uch;
31 typedef unsigned short ush;
32 typedef unsigned long  ulg;
33 
34 #define WSIZE 0x8000		/* Window size must be at least 32k, */
35 				/* and a power of two */
36 
37 static uch *inbuf;	     /* input buffer */
38 static uch window[WSIZE];    /* Sliding window buffer */
39 
40 static unsigned insize = 0;  /* valid bytes in inbuf */
41 static unsigned inptr = 0;   /* index of next byte to be processed in inbuf */
42 static unsigned outcnt = 0;  /* bytes in output buffer */
43 
44 /* gzip flag byte */
45 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ASCII text */
46 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
47 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
48 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
49 #define COMMENT      0x10 /* bit 4 set: file comment present */
50 #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
51 #define RESERVED     0xC0 /* bit 6,7:   reserved */
52 
53 #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
54 
55 /* Diagnostic functions */
56 #ifdef DEBUG
57 #  define Assert(cond,msg) {if(!(cond)) error(msg);}
58 #  define Trace(x) fprintf x
59 #  define Tracev(x) {if (verbose) fprintf x ;}
60 #  define Tracevv(x) {if (verbose>1) fprintf x ;}
61 #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
62 #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
63 #else
64 #  define Assert(cond,msg)
65 #  define Trace(x)
66 #  define Tracev(x)
67 #  define Tracevv(x)
68 #  define Tracec(c,x)
69 #  define Tracecv(c,x)
70 #endif
71 
72 static int  fill_inbuf(void);
73 static void flush_window(void);
74 static void error(char *m);
75 static void gzip_mark(void **);
76 static void gzip_release(void **);
77 
78 extern char input_data[];
79 extern int input_len;
80 
81 static long bytes_out = 0;
82 static uch *output_data;
83 static unsigned long output_ptr = 0;
84 
85 static void *malloc(int size);
86 static void free(void *where);
87 static void error(char *m);
88 static void gzip_mark(void **);
89 static void gzip_release(void **);
90 
91 int puts(const char *);
92 
93 extern int _text;		/* Defined in vmlinux.lds.S */
94 extern int _end;
95 static unsigned long free_mem_ptr;
96 static unsigned long free_mem_end_ptr;
97 
98 #define HEAP_SIZE             0x10000
99 
100 #include "../../../../lib/inflate.c"
101 
102 static void *malloc(int size)
103 {
104 	void *p;
105 
106 	if (size <0) error("Malloc error");
107 	if (free_mem_ptr == 0) error("Memory error");
108 
109 	free_mem_ptr = (free_mem_ptr + 3) & ~3;	/* Align */
110 
111 	p = (void *)free_mem_ptr;
112 	free_mem_ptr += size;
113 
114 	if (free_mem_ptr >= free_mem_end_ptr)
115 		error("Out of memory");
116 
117 	return p;
118 }
119 
120 static void free(void *where)
121 {	/* Don't care */
122 }
123 
124 static void gzip_mark(void **ptr)
125 {
126 	*ptr = (void *) free_mem_ptr;
127 }
128 
129 static void gzip_release(void **ptr)
130 {
131 	free_mem_ptr = (long) *ptr;
132 }
133 
134 #ifdef CONFIG_SH_STANDARD_BIOS
135 size_t strlen(const char *s)
136 {
137 	int i = 0;
138 
139 	while (*s++)
140 		i++;
141 	return i;
142 }
143 
144 int puts(const char *s)
145 {
146 	int len = strlen(s);
147 	sh_bios_console_write(s, len);
148 	return len;
149 }
150 #else
151 int puts(const char *s)
152 {
153 	/* This should be updated to use the sh-sci routines */
154 	return 0;
155 }
156 #endif
157 
158 void* memset(void* s, int c, size_t n)
159 {
160 	int i;
161 	char *ss = (char*)s;
162 
163 	for (i=0;i<n;i++) ss[i] = c;
164 	return s;
165 }
166 
167 void* memcpy(void* __dest, __const void* __src,
168 			    size_t __n)
169 {
170 	int i;
171 	char *d = (char *)__dest, *s = (char *)__src;
172 
173 	for (i=0;i<__n;i++) d[i] = s[i];
174 	return __dest;
175 }
176 
177 /* ===========================================================================
178  * Fill the input buffer. This is called only when the buffer is empty
179  * and at least one byte is really needed.
180  */
181 static int fill_inbuf(void)
182 {
183 	if (insize != 0) {
184 		error("ran out of input data");
185 	}
186 
187 	inbuf = input_data;
188 	insize = input_len;
189 	inptr = 1;
190 	return inbuf[0];
191 }
192 
193 /* ===========================================================================
194  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
195  * (Used for the decompressed data only.)
196  */
197 static void flush_window(void)
198 {
199     ulg c = crc;         /* temporary variable */
200     unsigned n;
201     uch *in, *out, ch;
202 
203     in = window;
204     out = &output_data[output_ptr];
205     for (n = 0; n < outcnt; n++) {
206 	    ch = *out++ = *in++;
207 	    c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
208     }
209     crc = c;
210     bytes_out += (ulg)outcnt;
211     output_ptr += (ulg)outcnt;
212     outcnt = 0;
213 }
214 
215 static void error(char *x)
216 {
217 	puts("\n\n");
218 	puts(x);
219 	puts("\n\n -- System halted");
220 
221 	while(1);	/* Halt */
222 }
223 
224 #define STACK_SIZE (4096)
225 long user_stack [STACK_SIZE];
226 long* stack_start = &user_stack[STACK_SIZE];
227 
228 void decompress_kernel(void)
229 {
230 	output_data = 0;
231 	output_ptr = (unsigned long)&_text+0x20001000;
232 	free_mem_ptr = (unsigned long)&_end;
233 	free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
234 
235 	makecrc();
236 	puts("Uncompressing Linux... ");
237 	gunzip();
238 	puts("Ok, booting the kernel.\n");
239 }
240