gzboot.c revision 1.1 1 /* $NetBSD: gzboot.c,v 1.1 2002/02/23 05:41: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 *zcalloc(void *, unsigned int, unsigned int);
114 void zcfree(void *, void *);
115 void zmemcpy(unsigned char *, unsigned char *, unsigned int);
116
117 /* end zlib glue defns */
118
119 void main(void);
120 void gzcopy(void *, const void *, size_t);
121
122 void
123 main(void)
124 {
125 extern char bootprog_name[], bootprog_rev[],
126 bootprog_maker[], bootprog_date[];
127 void (*loadaddr)(void) = (void *) md_root_loadaddr;
128
129 cons_init();
130
131 printf("\n");
132 printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
133 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
134
135 mem_init();
136
137 printf(">> Load address: 0x%x\n", md_root_loadaddr);
138 printf(">> Image size: %u\n", md_root_size);
139
140 printf("Uncompressing image...");
141 gzcopy((void *) loadaddr, md_root_image, md_root_size);
142 printf("done.\n");
143
144 printf("Jumping to image @ 0x%x...\n", md_root_loadaddr);
145 (*loadaddr)();
146
147 _rtt();
148 }
149
150 void abort(void);
151 void
152 abort(void)
153 {
154
155 for (;;) ;
156 }
157
158 __dead void
159 _rtt(void)
160 {
161
162 for (;;) ;
163 }
164
165 /* internal utility routines */
166
167 static ssize_t
168 readbuf(struct state *s, void *buf, size_t len)
169 {
170
171 if (len > (s->srcsize - s->srcoff))
172 len = s->srcsize - s->srcoff;
173
174 if ((s->spinny & 255) == 0)
175 twiddle();
176 memcpy(buf, s->srcbuf + s->srcoff, len);
177 s->srcoff += len;
178
179 return (len);
180 }
181
182 static ssize_t
183 readgz(struct state *s, void *buf, size_t len)
184 {
185 unsigned char *start = buf; /* start for CRC computation */
186
187 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)
188 return (-1);
189 if (s->z_err == Z_STREAM_END)
190 return (0); /* EOF */
191
192 s->stream.next_out = buf;
193 s->stream.avail_out = len;
194
195 while (s->stream.avail_out != 0) {
196 if (s->stream.avail_in == 0 && s->z_eof == 0) {
197 ssize_t got;
198 got = readbuf(s, s->inbuf, Z_BUFSIZE);
199 if (got <= 0)
200 s->z_eof = 1;
201 s->stream.avail_in = got;
202 s->stream.next_in = s->inbuf;
203 }
204
205 s->z_err = inflate(&s->stream, Z_NO_FLUSH);
206
207 if (s->z_err == Z_STREAM_END) {
208 /* Check CRC and original size */
209 s->crc = crc32(s->crc, start, (unsigned int)
210 (s->stream.next_out - start));
211 start = s->stream.next_out;
212
213 if (get_u32(s) != s->crc) {
214 printf("FATAL: CRC checksum mismatch\n");
215 s->z_err = Z_DATA_ERROR;
216 }
217 if (get_u32(s) != s->stream.total_out) {
218 printf("FATAL: total output mismatch\n");
219 s->z_err = Z_DATA_ERROR;
220 }
221 s->z_eof = 1;
222 }
223 if (s->z_err != Z_OK || s->z_eof)
224 break;
225 }
226
227 s->crc = crc32(s->crc, start,
228 (unsigned int)(s->stream.next_out - start));
229
230 return ((ssize_t) (len - s->stream.avail_out));
231 }
232
233 /* util routines for zlib */
234
235 void *
236 zcalloc(void *arg, unsigned int items, unsigned int size)
237 {
238 void *ptr;
239 size_t sz;
240
241 sz = size * items;
242 ptr = alloc(sz);
243 if (ptr != NULL)
244 memset(ptr, 0, sz);
245 return (ptr);
246 }
247
248 void
249 zcfree(void *arg, void *ptr)
250 {
251
252 free(ptr, 0); /* XXX works only with modified allocator */
253 }
254
255 void
256 zmemcpy(unsigned char *dst, unsigned char *src, unsigned int len)
257 {
258
259 memcpy(dst, src, len);
260 }
261
262 /* gzip utility routines */
263
264 static uint32_t
265 get_u8(struct state *s)
266 {
267
268 if (s->z_eof)
269 return (EOF);
270
271 if (s->stream.avail_in == 0) {
272 ssize_t got;
273
274 got = readbuf(s, s->inbuf, Z_BUFSIZE);
275 if (got <= 0) {
276 s->z_eof = 1;
277 return (EOF);
278 }
279 s->stream.avail_in = got;
280 s->stream.next_in = s->inbuf;
281 }
282 s->stream.avail_in--;
283 return (*(s->stream.next_in)++);
284 }
285
286 static uint32_t
287 get_u32(struct state *s)
288 {
289 uint32_t x, c;
290
291 x = get_u8(s);
292 x |= get_u8(s) << 8;
293 x |= get_u8(s) << 16;
294 c = get_u8(s);
295 if (c == EOF)
296 s->z_err = Z_DATA_ERROR;
297 x |= c << 24;
298 return (x);
299 }
300
301 static int
302 check_header(struct state *s)
303 {
304 int method; /* method byte */
305 int flags; /* flags byte */
306 unsigned int len;
307 int c;
308
309 /* Check the gzip magic header */
310 for (len = 0; len < 2; len++) {
311 c = get_u8(s);
312 if (c == gz_magic[len])
313 continue;
314 printf("FATAL: not a Gzip'd image\n");
315 return (1);
316 }
317
318 method = get_u8(s);
319 flags = get_u8(s);
320 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
321 printf("FATAL: invalid Gzip header\n");
322 return (1);
323 }
324
325 /* Discard time, xflags, and OS code: */
326 for (len = 0; len < 6; len++)
327 (void) get_u8(s);
328
329 if (flags & EXTRA_FIELD) {
330 /* skip the extra field */
331 len = get_u8(s);
332 len |= get_u8(s) << 8;
333 /* len is garbage if EOF, but the loop below will quit anyway */
334 while (len-- && get_u8(s) != EOF)
335 /* loop */;
336 }
337 if (flags & ORIG_NAME) {
338 /* skip the original file name */
339 while ((c = get_u8(s)) != 0 && c != EOF)
340 /* loop */;
341 }
342 if (flags & COMMENT) {
343 /* skip the file comment */
344 while ((c = get_u8(s)) != 0 && c != EOF)
345 /* loop */;
346 }
347 if (flags & HEAD_CRC) {
348 /* skip header CRC */
349 for (len = 0; len < 2; len++)
350 (void) get_u8(s);
351 }
352
353 if (s->z_eof) {
354 printf("FATAL: end of image encountered parsing Gzip header\n");
355 return (1);
356 }
357
358 /* OK! */
359 return (0);
360 }
361
362 /* the actual gzcopy routine */
363
364 void
365 gzcopy(void *dst, const void *src, size_t srclen)
366 {
367 struct state state;
368 unsigned char *cp = dst;
369 ssize_t len;
370
371 memset(&state, 0, sizeof(state));
372
373 state.z_err = Z_OK;
374 state.srcbuf = src;
375 state.srcsize = srclen;
376
377 if (inflateInit2(&state.stream, -15) != Z_OK) {
378 printf("FATAL: inflateInit2 failed\n");
379 _rtt();
380 }
381
382 state.stream.next_in = state.inbuf = alloc(Z_BUFSIZE);
383 if (state.inbuf == NULL) {
384 inflateEnd(&state.stream);
385 printf("FATAL: unable to allocate Z buffer\n");
386 _rtt();
387 }
388
389 /* Skip the Gzip header. */
390 if (check_header(&state)) {
391 inflateEnd(&state.stream);
392 free(state.inbuf, Z_BUFSIZE);
393 _rtt();
394 }
395
396 /* Uncompress the image! */
397 while ((len = readgz(&state, cp, Z_BUFSIZE)) > 0)
398 cp += len;
399 if (len == -1)
400 _rtt();
401
402 /* All done! */
403 inflateEnd(&state.stream);
404 free(state.inbuf, Z_BUFSIZE);
405 }
406