#include <sys/param.h>
#include <lib/libsa/stand.h>
#include <lib/libkern/libkern.h>
#include <lib/libz/libz.h>
#include "board.h"
#include "gzboot.h"
#define EOF (-1)
#define Z_BUFSIZE 1024
static const int gz_magic[2] = { 0x1f, 0x8b };
#define ASCII_FLAG 0x01
#define HEAD_CRC 0x02
#define EXTRA_FIELD 0x04
#define ORIG_NAME 0x08
#define COMMENT 0x10
#define RESERVED 0xe0
struct state {
z_stream stream;
int z_err;
int z_eof;
const unsigned char *srcbuf;
size_t srcoff;
size_t srcsize;
unsigned char *inbuf;
uint32_t crc;
int spinny;
};
static uint32_t get_u8(struct state *);
static uint32_t get_u32(struct state *);
static int check_header(struct state *);
void zmemcpy(unsigned char *, unsigned char *, unsigned int);
void main(void);
void gzcopy(void *, const void *, size_t);
#ifdef GZSRCADDR
#define compressed_image (void *)GZSRCADDR
#else
#define compressed_image md_root_image
#endif
void
main(void)
{
extern char bootprog_name[], bootprog_rev[];
void (*loadaddr)(void) = (void *) md_root_loadaddr;
cons_init();
printf("\n");
printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
board_init();
printf(">> Load address: 0x%x\n", md_root_loadaddr);
#ifdef GZSRCADDR
printf(">> Image address: %p\n", compressed_image);
#endif
if (md_root_size != 0)
printf(">> Image size: %u\n", md_root_size);
printf("Uncompressing image...");
gzcopy((void *) loadaddr, compressed_image, md_root_size);
printf("done.\n");
printf("Jumping to image @ 0x%x...\n", md_root_loadaddr);
board_fini();
(*loadaddr)();
_rtt();
}
void abort(void);
void
abort(void)
{
for (;;) ;
}
__dead void
_rtt(void)
{
for (;;) ;
}
static ssize_t
readbuf(struct state *s, void *buf, size_t len)
{
if (s->srcsize != 0 && len > (s->srcsize - s->srcoff))
len = s->srcsize - s->srcoff;
if ((s->spinny++ & 7) == 0)
twiddle();
memcpy(buf, s->srcbuf + s->srcoff, len);
s->srcoff += len;
return (len);
}
static ssize_t
readgz(struct state *s, void *buf, size_t len)
{
unsigned char *start = buf;
if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)
return (-1);
if (s->z_err == Z_STREAM_END)
return (0);
s->stream.next_out = buf;
s->stream.avail_out = len;
while (s->stream.avail_out != 0) {
if (s->stream.avail_in == 0 && s->z_eof == 0) {
ssize_t got;
got = readbuf(s, s->inbuf, Z_BUFSIZE);
if (got <= 0)
s->z_eof = 1;
s->stream.avail_in = got;
s->stream.next_in = s->inbuf;
}
s->z_err = inflate(&s->stream, Z_NO_FLUSH);
if (s->z_err == Z_STREAM_END) {
s->crc = crc32(s->crc, start, (unsigned int)
(s->stream.next_out - start));
start = s->stream.next_out;
if (get_u32(s) != s->crc) {
printf("FATAL: CRC checksum mismatch\n");
s->z_err = Z_DATA_ERROR;
}
if (get_u32(s) != s->stream.total_out) {
printf("FATAL: total output mismatch\n");
s->z_err = Z_DATA_ERROR;
}
s->z_eof = 1;
}
if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) {
printf("FATAL: error %d from zlib\n",
s->z_err);
return (-1);
}
if (s->z_err != Z_OK || s->z_eof)
break;
}
s->crc = crc32(s->crc, start,
(unsigned int)(s->stream.next_out - start));
return ((ssize_t) (len - s->stream.avail_out));
}
void
zmemcpy(unsigned char *dst, unsigned char *src, unsigned int len)
{
memcpy(dst, src, len);
}
static uint32_t
get_u8(struct state *s)
{
if (s->z_eof)
return (EOF);
if (s->stream.avail_in == 0) {
ssize_t got;
got = readbuf(s, s->inbuf, Z_BUFSIZE);
if (got <= 0) {
s->z_eof = 1;
return (EOF);
}
s->stream.avail_in = got;
s->stream.next_in = s->inbuf;
}
s->stream.avail_in--;
return (*(s->stream.next_in)++);
}
static uint32_t
get_u32(struct state *s)
{
uint32_t x, c;
x = get_u8(s);
x |= get_u8(s) << 8;
x |= get_u8(s) << 16;
c = get_u8(s);
if (c == EOF)
s->z_err = Z_DATA_ERROR;
x |= c << 24;
return (x);
}
static int
check_header(struct state *s)
{
int method;
int flags;
unsigned int len;
int c;
for (len = 0; len < 2; len++) {
c = get_u8(s);
if (c == gz_magic[len])
continue;
printf("FATAL: not a Gzip'd image\n");
return (1);
}
method = get_u8(s);
flags = get_u8(s);
if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
printf("FATAL: invalid Gzip header\n");
return (1);
}
for (len = 0; len < 6; len++)
(void) get_u8(s);
if (flags & EXTRA_FIELD) {
len = get_u8(s);
len |= get_u8(s) << 8;
while (len-- && get_u8(s) != EOF)
;
}
if (flags & ORIG_NAME) {
while ((c = get_u8(s)) != 0 && c != EOF)
;
}
if (flags & COMMENT) {
while ((c = get_u8(s)) != 0 && c != EOF)
;
}
if (flags & HEAD_CRC) {
for (len = 0; len < 2; len++)
(void) get_u8(s);
}
if (s->z_eof) {
printf("FATAL: end of image encountered parsing Gzip header\n");
return (1);
}
return (0);
}
void
gzcopy(void *dst, const void *src, size_t srclen)
{
struct state state;
unsigned char *cp = dst;
ssize_t len;
memset(&state, 0, sizeof(state));
state.z_err = Z_OK;
state.srcbuf = src;
state.srcsize = srclen;
if (inflateInit2(&state.stream, -15) != Z_OK) {
printf("FATAL: inflateInit2 failed\n");
_rtt();
}
state.stream.next_in = state.inbuf = alloc(Z_BUFSIZE);
if (state.inbuf == NULL) {
inflateEnd(&state.stream);
printf("FATAL: unable to allocate Z buffer\n");
_rtt();
}
if (check_header(&state)) {
inflateEnd(&state.stream);
dealloc(state.inbuf, Z_BUFSIZE);
_rtt();
}
while ((len = readgz(&state, cp, Z_BUFSIZE)) > 0)
cp += len;
if (len == -1)
_rtt();
inflateEnd(&state.stream);
dealloc(state.inbuf, Z_BUFSIZE);
}