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