gzboot.c revision 1.4 1 /* $NetBSD: gzboot.c,v 1.4 2002/02/23 20:48:14 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2002 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * The Gzip header parser and libz interface glue are derived from
40 * sys/lib/libsa/cread.c, which carries the following notice:
41 *
42 * Copyright (c) 1996
43 * Matthias Drochner. All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed for the NetBSD Project
56 * by Matthias Drochner.
57 * 4. The name of the author may not be used to endorse or promote products
58 * derived from this software without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
61 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
62 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
64 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
65 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
69 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70 */
71
72 #include <sys/param.h>
73 #include <lib/libsa/stand.h>
74 #include <lib/libkern/libkern.h>
75 #include <lib/libz/zlib.h>
76
77 #include "board.h"
78 #include "gzboot.h"
79
80 /* zlib glue defns */
81
82 #define EOF (-1) /* needed by compression code */
83
84 #define Z_BUFSIZE 1024
85
86 static const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
87
88 /* gzip flag byte */
89 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
90 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
91 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
92 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
93 #define COMMENT 0x10 /* bit 4 set: file comment present */
94 #define RESERVED 0xe0 /* bits 5..7: reserved */
95
96 struct state {
97 z_stream stream;
98 int z_err; /* error code for last stream operation */
99 int z_eof; /* set of end of input file */
100 const unsigned char *srcbuf;/* source buffer */
101 size_t srcoff; /* source buffer offset */
102 size_t srcsize;/* size of source buffer */
103 unsigned char *inbuf; /* input buffer */
104 uint32_t crc; /* crc32 of uncompressed data */
105 int spinny; /* twiddle every N reads */
106 };
107
108 static uint32_t get_u8(struct state *);
109 static uint32_t get_u32(struct state *);
110 static int check_header(struct state *);
111
112 /* XXX - find a suitable header for these: */
113 void zmemcpy(unsigned char *, unsigned char *, unsigned int);
114
115 /* end zlib glue defns */
116
117 void main(void);
118 void gzcopy(void *, const void *, size_t);
119
120 void
121 main(void)
122 {
123 extern char bootprog_name[], bootprog_rev[],
124 bootprog_maker[], bootprog_date[];
125 void (*loadaddr)(void) = (void *) md_root_loadaddr;
126
127 cons_init();
128
129 printf("\n");
130 printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
131 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
132
133 mem_init();
134
135 printf(">> Load address: 0x%x\n", md_root_loadaddr);
136 printf(">> Image size: %u\n", md_root_size);
137
138 printf("Uncompressing image...");
139 gzcopy((void *) loadaddr, md_root_image, md_root_size);
140 printf("done.\n");
141
142 printf("Jumping to image @ 0x%x...\n", md_root_loadaddr);
143 (*loadaddr)();
144
145 _rtt();
146 }
147
148 void abort(void);
149 void
150 abort(void)
151 {
152
153 for (;;) ;
154 }
155
156 __dead void
157 _rtt(void)
158 {
159
160 for (;;) ;
161 }
162
163 /* internal utility routines */
164
165 static ssize_t
166 readbuf(struct state *s, void *buf, size_t len)
167 {
168
169 if (len > (s->srcsize - s->srcoff))
170 len = s->srcsize - s->srcoff;
171
172 if ((s->spinny++ & 7) == 0)
173 twiddle();
174 bcopy(s->srcbuf + s->srcoff, buf, len);
175 s->srcoff += len;
176
177 return (len);
178 }
179
180 static ssize_t
181 readgz(struct state *s, void *buf, size_t len)
182 {
183 unsigned char *start = buf; /* start for CRC computation */
184
185 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)
186 return (-1);
187 if (s->z_err == Z_STREAM_END)
188 return (0); /* EOF */
189
190 s->stream.next_out = buf;
191 s->stream.avail_out = len;
192
193 while (s->stream.avail_out != 0) {
194 if (s->stream.avail_in == 0 && s->z_eof == 0) {
195 ssize_t got;
196 got = readbuf(s, s->inbuf, Z_BUFSIZE);
197 if (got <= 0)
198 s->z_eof = 1;
199 s->stream.avail_in = got;
200 s->stream.next_in = s->inbuf;
201 }
202
203 s->z_err = inflate(&s->stream, Z_NO_FLUSH);
204
205 if (s->z_err == Z_STREAM_END) {
206 /* Check CRC and original size */
207 s->crc = crc32(s->crc, start, (unsigned int)
208 (s->stream.next_out - start));
209 start = s->stream.next_out;
210
211 if (get_u32(s) != s->crc) {
212 printf("FATAL: CRC checksum mismatch\n");
213 s->z_err = Z_DATA_ERROR;
214 }
215 if (get_u32(s) != s->stream.total_out) {
216 printf("FATAL: total output mismatch\n");
217 s->z_err = Z_DATA_ERROR;
218 }
219 s->z_eof = 1;
220 }
221 if (s->z_err != Z_OK || s->z_eof)
222 break;
223 }
224
225 s->crc = crc32(s->crc, start,
226 (unsigned int)(s->stream.next_out - start));
227
228 return ((ssize_t) (len - s->stream.avail_out));
229 }
230
231 /* util routines for zlib */
232
233 void
234 zmemcpy(unsigned char *dst, unsigned char *src, unsigned int len)
235 {
236
237 bcopy(src, dst, len);
238 }
239
240 /* gzip utility routines */
241
242 static uint32_t
243 get_u8(struct state *s)
244 {
245
246 if (s->z_eof)
247 return (EOF);
248
249 if (s->stream.avail_in == 0) {
250 ssize_t got;
251
252 got = readbuf(s, s->inbuf, Z_BUFSIZE);
253 if (got <= 0) {
254 s->z_eof = 1;
255 return (EOF);
256 }
257 s->stream.avail_in = got;
258 s->stream.next_in = s->inbuf;
259 }
260 s->stream.avail_in--;
261 return (*(s->stream.next_in)++);
262 }
263
264 static uint32_t
265 get_u32(struct state *s)
266 {
267 uint32_t x, c;
268
269 x = get_u8(s);
270 x |= get_u8(s) << 8;
271 x |= get_u8(s) << 16;
272 c = get_u8(s);
273 if (c == EOF)
274 s->z_err = Z_DATA_ERROR;
275 x |= c << 24;
276 return (x);
277 }
278
279 static int
280 check_header(struct state *s)
281 {
282 int method; /* method byte */
283 int flags; /* flags byte */
284 unsigned int len;
285 int c;
286
287 /* Check the gzip magic header */
288 for (len = 0; len < 2; len++) {
289 c = get_u8(s);
290 if (c == gz_magic[len])
291 continue;
292 printf("FATAL: not a Gzip'd image\n");
293 return (1);
294 }
295
296 method = get_u8(s);
297 flags = get_u8(s);
298 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
299 printf("FATAL: invalid Gzip header\n");
300 return (1);
301 }
302
303 /* Discard time, xflags, and OS code: */
304 for (len = 0; len < 6; len++)
305 (void) get_u8(s);
306
307 if (flags & EXTRA_FIELD) {
308 /* skip the extra field */
309 len = get_u8(s);
310 len |= get_u8(s) << 8;
311 /* len is garbage if EOF, but the loop below will quit anyway */
312 while (len-- && get_u8(s) != EOF)
313 /* loop */;
314 }
315 if (flags & ORIG_NAME) {
316 /* skip the original file name */
317 while ((c = get_u8(s)) != 0 && c != EOF)
318 /* loop */;
319 }
320 if (flags & COMMENT) {
321 /* skip the file comment */
322 while ((c = get_u8(s)) != 0 && c != EOF)
323 /* loop */;
324 }
325 if (flags & HEAD_CRC) {
326 /* skip header CRC */
327 for (len = 0; len < 2; len++)
328 (void) get_u8(s);
329 }
330
331 if (s->z_eof) {
332 printf("FATAL: end of image encountered parsing Gzip header\n");
333 return (1);
334 }
335
336 /* OK! */
337 return (0);
338 }
339
340 /* the actual gzcopy routine */
341
342 void
343 gzcopy(void *dst, const void *src, size_t srclen)
344 {
345 struct state state;
346 unsigned char *cp = dst;
347 ssize_t len;
348
349 bzero(&state, sizeof(state));
350
351 state.z_err = Z_OK;
352 state.srcbuf = src;
353 state.srcsize = srclen;
354
355 if (inflateInit2(&state.stream, -15) != Z_OK) {
356 printf("FATAL: inflateInit2 failed\n");
357 _rtt();
358 }
359
360 state.stream.next_in = state.inbuf = alloc(Z_BUFSIZE);
361 if (state.inbuf == NULL) {
362 inflateEnd(&state.stream);
363 printf("FATAL: unable to allocate Z buffer\n");
364 _rtt();
365 }
366
367 /* Skip the Gzip header. */
368 if (check_header(&state)) {
369 inflateEnd(&state.stream);
370 free(state.inbuf, Z_BUFSIZE);
371 _rtt();
372 }
373
374 /* Uncompress the image! */
375 while ((len = readgz(&state, cp, Z_BUFSIZE)) > 0)
376 cp += len;
377 if (len == -1)
378 _rtt();
379
380 /* All done! */
381 inflateEnd(&state.stream);
382 free(state.inbuf, Z_BUFSIZE);
383 }
384