xref: /netbsd-src/sys/arch/evbarm/stand/gzboot/gzboot.c (revision b2459078df579113cae0a460a1c00bfffcd7506f)
1 /*	$NetBSD: gzboot.c,v 1.16 2020/02/12 19:55:32 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  *
54  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 #include <sys/param.h>
67 #include <lib/libsa/stand.h>
68 #include <lib/libkern/libkern.h>
69 #include <lib/libz/libz.h>
70 
71 #include "board.h"
72 #include "gzboot.h"
73 
74 /* zlib glue defns */
75 
76 #define	EOF	(-1)	/* needed by compression code */
77 
78 #define	Z_BUFSIZE	1024
79 
80 static const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
81 
82 /* gzip flag byte */
83 #define	ASCII_FLAG	0x01	/* bit 0 set: file probably ascii text */
84 #define	HEAD_CRC	0x02	/* bit 1 set: header CRC present */
85 #define	EXTRA_FIELD	0x04	/* bit 2 set: extra field present */
86 #define	ORIG_NAME	0x08	/* bit 3 set: original file name present */
87 #define	COMMENT		0x10	/* bit 4 set: file comment present */
88 #define	RESERVED	0xe0	/* bits 5..7: reserved */
89 
90 struct state {
91 	z_stream	stream;
92 	int		z_err;	/* error code for last stream operation */
93 	int		z_eof;	/* set of end of input file */
94 	const unsigned char *srcbuf;/* source buffer */
95 	size_t		srcoff;	/* source buffer offset */
96 	size_t		srcsize;/* size of source buffer */
97 	unsigned char	*inbuf;	/* input buffer */
98 	uint32_t	crc;	/* crc32 of uncompressed data */
99 	int		spinny;	/* twiddle every N reads */
100 };
101 
102 static uint32_t	get_u8(struct state *);
103 static uint32_t	get_u32(struct state *);
104 static int	check_header(struct state *);
105 
106 /* XXX - find a suitable header for these: */
107 void	zmemcpy(unsigned char *, unsigned char *, unsigned int);
108 
109 /* end zlib glue defns */
110 
111 void	main(void);
112 void	gzcopy(void *, const void *, size_t);
113 
114 #ifdef GZSRCADDR
115 #define	compressed_image	(void *)GZSRCADDR
116 #else
117 #define	compressed_image	md_root_image
118 #endif
119 
120 void
main(void)121 main(void)
122 {
123 	extern char bootprog_name[], bootprog_rev[];
124 	void (*loadaddr)(void) = (void *) md_root_loadaddr;
125 
126 	cons_init();
127 
128 	printf("\n");
129 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
130 
131 	board_init();
132 
133 	printf(">> Load address: 0x%x\n", md_root_loadaddr);
134 
135 	/*
136 	 * If md_root_size is 0, then it means that we are simply
137 	 * decompressing from an image which was concatenated onto
138 	 * the end of the gzboot binary.
139 	 */
140 #ifdef GZSRCADDR
141 	printf(">> Image address: %p\n", compressed_image);
142 #endif
143 	if (md_root_size != 0)
144 		printf(">> Image size: %u\n", md_root_size);
145 
146 	printf("Uncompressing image...");
147 	gzcopy((void *) loadaddr, compressed_image, md_root_size);
148 	printf("done.\n");
149 
150 	printf("Jumping to image @ 0x%x...\n", md_root_loadaddr);
151 
152 	board_fini();
153 
154 	(*loadaddr)();
155 
156 	_rtt();
157 }
158 
159 void abort(void);
160 void
abort(void)161 abort(void)
162 {
163 
164 	for (;;) ;
165 }
166 
167 __dead void
_rtt(void)168 _rtt(void)
169 {
170 
171 	for (;;) ;
172 }
173 
174 /* internal utility routines */
175 
176 static ssize_t
readbuf(struct state * s,void * buf,size_t len)177 readbuf(struct state *s, void *buf, size_t len)
178 {
179 
180 	if (s->srcsize != 0 && len > (s->srcsize - s->srcoff))
181 		len = s->srcsize - s->srcoff;
182 
183 	if ((s->spinny++ & 7) == 0)
184 		twiddle();
185 	memcpy(buf, s->srcbuf + s->srcoff, len);
186 	s->srcoff += len;
187 
188 	return (len);
189 }
190 
191 static ssize_t
readgz(struct state * s,void * buf,size_t len)192 readgz(struct state *s, void *buf, size_t len)
193 {
194 	unsigned char *start = buf;	/* start for CRC computation */
195 
196 	if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)
197 		return (-1);
198 	if (s->z_err == Z_STREAM_END)
199 		return (0);	/* EOF */
200 
201 	s->stream.next_out = buf;
202 	s->stream.avail_out = len;
203 
204 	while (s->stream.avail_out != 0) {
205 		if (s->stream.avail_in == 0 && s->z_eof == 0) {
206 			ssize_t got;
207 			got = readbuf(s, s->inbuf, Z_BUFSIZE);
208 			if (got <= 0)
209 				s->z_eof = 1;
210 			s->stream.avail_in = got;
211 			s->stream.next_in = s->inbuf;
212 		}
213 
214 		s->z_err = inflate(&s->stream, Z_NO_FLUSH);
215 
216 		if (s->z_err == Z_STREAM_END) {
217 			/* Check CRC and original size */
218 			s->crc = crc32(s->crc, start, (unsigned int)
219 			    (s->stream.next_out - start));
220 			start = s->stream.next_out;
221 
222 			if (get_u32(s) != s->crc) {
223 				printf("FATAL: CRC checksum mismatch\n");
224 				s->z_err = Z_DATA_ERROR;
225 			}
226 			if (get_u32(s) != s->stream.total_out) {
227 				printf("FATAL: total output mismatch\n");
228 				s->z_err = Z_DATA_ERROR;
229 			}
230 			s->z_eof = 1;
231 		}
232 		if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) {
233 			printf("FATAL: error %d from zlib\n",
234 			    s->z_err);
235 			return (-1);
236 		}
237 		if (s->z_err != Z_OK || s->z_eof)
238 			break;
239 	}
240 
241 	s->crc = crc32(s->crc, start,
242 	    (unsigned int)(s->stream.next_out - start));
243 
244 	return ((ssize_t) (len - s->stream.avail_out));
245 }
246 
247 /* util routines for zlib */
248 
249 void
zmemcpy(unsigned char * dst,unsigned char * src,unsigned int len)250 zmemcpy(unsigned char *dst, unsigned char *src, unsigned int len)
251 {
252 
253 	memcpy(dst, src, len);
254 }
255 
256 /* gzip utility routines */
257 
258 static uint32_t
get_u8(struct state * s)259 get_u8(struct state *s)
260 {
261 
262 	if (s->z_eof)
263 		return (EOF);
264 
265 	if (s->stream.avail_in == 0) {
266 		ssize_t got;
267 
268 		got = readbuf(s, s->inbuf, Z_BUFSIZE);
269 		if (got <= 0) {
270 			s->z_eof = 1;
271 			return (EOF);
272 		}
273 		s->stream.avail_in = got;
274 		s->stream.next_in = s->inbuf;
275 	}
276 	s->stream.avail_in--;
277 	return (*(s->stream.next_in)++);
278 }
279 
280 static uint32_t
get_u32(struct state * s)281 get_u32(struct state *s)
282 {
283 	uint32_t x, c;
284 
285 	x  = get_u8(s);
286 	x |= get_u8(s) << 8;
287 	x |= get_u8(s) << 16;
288 	c = get_u8(s);
289 	if (c == EOF)
290 		s->z_err = Z_DATA_ERROR;
291 	x |= c << 24;
292 	return (x);
293 }
294 
295 static int
check_header(struct state * s)296 check_header(struct state *s)
297 {
298 	int method;	/* method byte */
299 	int flags;	/* flags byte */
300 	unsigned int len;
301 	int c;
302 
303 	/* Check the gzip magic header */
304 	for (len = 0; len < 2; len++) {
305 		c = get_u8(s);
306 		if (c == gz_magic[len])
307 			continue;
308 		printf("FATAL: not a Gzip'd image\n");
309 		return (1);
310 	}
311 
312 	method = get_u8(s);
313 	flags = get_u8(s);
314 	if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
315 		printf("FATAL: invalid Gzip header\n");
316 		return (1);
317 	}
318 
319 	/* Discard time, xflags, and OS code: */
320 	for (len = 0; len < 6; len++)
321 		(void) get_u8(s);
322 
323 	if (flags & EXTRA_FIELD) {
324 		/* skip the extra field */
325 		len  = get_u8(s);
326 		len |= get_u8(s) << 8;
327 		/* len is garbage if EOF, but the loop below will quit anyway */
328 		while (len-- && get_u8(s) != EOF)
329 			/* loop */;
330 	}
331 	if (flags & ORIG_NAME) {
332 		/* skip the original file name */
333 		while ((c = get_u8(s)) != 0 && c != EOF)
334 			/* loop */;
335 	}
336 	if (flags & COMMENT) {
337 		/* skip the file comment */
338 		while ((c = get_u8(s)) != 0 && c != EOF)
339 			/* loop */;
340 	}
341 	if (flags & HEAD_CRC) {
342 		/* skip header CRC */
343 		for (len = 0; len < 2; len++)
344 			(void) get_u8(s);
345 	}
346 
347 	if (s->z_eof) {
348 		printf("FATAL: end of image encountered parsing Gzip header\n");
349 		return (1);
350 	}
351 
352 	/* OK! */
353 	return (0);
354 }
355 
356 /* the actual gzcopy routine */
357 
358 void
gzcopy(void * dst,const void * src,size_t srclen)359 gzcopy(void *dst, const void *src, size_t srclen)
360 {
361 	struct state state;
362 	unsigned char *cp = dst;
363 	ssize_t len;
364 
365 	memset(&state, 0, sizeof(state));
366 
367 	state.z_err = Z_OK;
368 	state.srcbuf = src;
369 	state.srcsize = srclen;
370 
371 	if (inflateInit2(&state.stream, -15) != Z_OK) {
372 		printf("FATAL: inflateInit2 failed\n");
373 		_rtt();
374 	}
375 
376 	state.stream.next_in = state.inbuf = alloc(Z_BUFSIZE);
377 	if (state.inbuf == NULL) {
378 		inflateEnd(&state.stream);
379 		printf("FATAL: unable to allocate Z buffer\n");
380 		_rtt();
381 	}
382 
383 	/* Skip the Gzip header. */
384 	if (check_header(&state)) {
385 		inflateEnd(&state.stream);
386 		dealloc(state.inbuf, Z_BUFSIZE);
387 		_rtt();
388 	}
389 
390 	/* Uncompress the image! */
391 	while ((len = readgz(&state, cp, Z_BUFSIZE)) > 0)
392 		cp += len;
393 	if (len == -1)
394 		_rtt();
395 
396 	/* All done! */
397 	inflateEnd(&state.stream);
398 	dealloc(state.inbuf, Z_BUFSIZE);
399 }
400