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