Home | History | Annotate | Line # | Download | only in gzboot
gzboot.c revision 1.5
      1 /*	$NetBSD: gzboot.c,v 1.5 2002/02/24 18:36:29 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_err != Z_STREAM_END) {
    222 			printf("FATAL: error %d from zlib\n",
    223 			    s->z_err);
    224 			return (-1);
    225 		}
    226 		if (s->z_err != Z_OK || s->z_eof)
    227 			break;
    228 	}
    229 
    230 	s->crc = crc32(s->crc, start,
    231 	    (unsigned int)(s->stream.next_out - start));
    232 
    233 	return ((ssize_t) (len - s->stream.avail_out));
    234 }
    235 
    236 /* util routines for zlib */
    237 
    238 void
    239 zmemcpy(unsigned char *dst, unsigned char *src, unsigned int len)
    240 {
    241 
    242 	bcopy(src, dst, len);
    243 }
    244 
    245 /* gzip utility routines */
    246 
    247 static uint32_t
    248 get_u8(struct state *s)
    249 {
    250 
    251 	if (s->z_eof)
    252 		return (EOF);
    253 
    254 	if (s->stream.avail_in == 0) {
    255 		ssize_t got;
    256 
    257 		got = readbuf(s, s->inbuf, Z_BUFSIZE);
    258 		if (got <= 0) {
    259 			s->z_eof = 1;
    260 			return (EOF);
    261 		}
    262 		s->stream.avail_in = got;
    263 		s->stream.next_in = s->inbuf;
    264 	}
    265 	s->stream.avail_in--;
    266 	return (*(s->stream.next_in)++);
    267 }
    268 
    269 static uint32_t
    270 get_u32(struct state *s)
    271 {
    272 	uint32_t x, c;
    273 
    274 	x  = get_u8(s);
    275 	x |= get_u8(s) << 8;
    276 	x |= get_u8(s) << 16;
    277 	c = get_u8(s);
    278 	if (c == EOF)
    279 		s->z_err = Z_DATA_ERROR;
    280 	x |= c << 24;
    281 	return (x);
    282 }
    283 
    284 static int
    285 check_header(struct state *s)
    286 {
    287 	int method;	/* method byte */
    288 	int flags;	/* flags byte */
    289 	unsigned int len;
    290 	int c;
    291 
    292 	/* Check the gzip magic header */
    293 	for (len = 0; len < 2; len++) {
    294 		c = get_u8(s);
    295 		if (c == gz_magic[len])
    296 			continue;
    297 		printf("FATAL: not a Gzip'd image\n");
    298 		return (1);
    299 	}
    300 
    301 	method = get_u8(s);
    302 	flags = get_u8(s);
    303 	if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
    304 		printf("FATAL: invalid Gzip header\n");
    305 		return (1);
    306 	}
    307 
    308 	/* Discard time, xflags, and OS code: */
    309 	for (len = 0; len < 6; len++)
    310 		(void) get_u8(s);
    311 
    312 	if (flags & EXTRA_FIELD) {
    313 		/* skip the extra field */
    314 		len  = get_u8(s);
    315 		len |= get_u8(s) << 8;
    316 		/* len is garbage if EOF, but the loop below will quit anyway */
    317 		while (len-- && get_u8(s) != EOF)
    318 			/* loop */;
    319 	}
    320 	if (flags & ORIG_NAME) {
    321 		/* skip the original file name */
    322 		while ((c = get_u8(s)) != 0 && c != EOF)
    323 			/* loop */;
    324 	}
    325 	if (flags & COMMENT) {
    326 		/* skip the file comment */
    327 		while ((c = get_u8(s)) != 0 && c != EOF)
    328 			/* loop */;
    329 	}
    330 	if (flags & HEAD_CRC) {
    331 		/* skip header CRC */
    332 		for (len = 0; len < 2; len++)
    333 			(void) get_u8(s);
    334 	}
    335 
    336 	if (s->z_eof) {
    337 		printf("FATAL: end of image encountered parsing Gzip header\n");
    338 		return (1);
    339 	}
    340 
    341 	/* OK! */
    342 	return (0);
    343 }
    344 
    345 /* the actual gzcopy routine */
    346 
    347 void
    348 gzcopy(void *dst, const void *src, size_t srclen)
    349 {
    350 	struct state state;
    351 	unsigned char *cp = dst;
    352 	ssize_t len;
    353 
    354 	bzero(&state, sizeof(state));
    355 
    356 	state.z_err = Z_OK;
    357 	state.srcbuf = src;
    358 	state.srcsize = srclen;
    359 
    360 	if (inflateInit2(&state.stream, -15) != Z_OK) {
    361 		printf("FATAL: inflateInit2 failed\n");
    362 		_rtt();
    363 	}
    364 
    365 	state.stream.next_in = state.inbuf = alloc(Z_BUFSIZE);
    366 	if (state.inbuf == NULL) {
    367 		inflateEnd(&state.stream);
    368 		printf("FATAL: unable to allocate Z buffer\n");
    369 		_rtt();
    370 	}
    371 
    372 	/* Skip the Gzip header. */
    373 	if (check_header(&state)) {
    374 		inflateEnd(&state.stream);
    375 		free(state.inbuf, Z_BUFSIZE);
    376 		_rtt();
    377 	}
    378 
    379 	/* Uncompress the image! */
    380 	while ((len = readgz(&state, cp, Z_BUFSIZE)) > 0)
    381 		cp += len;
    382 	if (len == -1)
    383 		_rtt();
    384 
    385 	/* All done! */
    386 	inflateEnd(&state.stream);
    387 	free(state.inbuf, Z_BUFSIZE);
    388 }
    389