Path: blob/master/arch/h8300/boot/compressed/misc.c
10818 views
/*1* arch/h8300/boot/compressed/misc.c2*3* This is a collection of several routines from gzip-1.0.34* adapted for Linux.5*6* malloc by Hannu Savolainen 1993 and Matthias Urlichs 19947*8* Adapted for h8300 by Yoshinori Sato 20069*/1011#include <asm/uaccess.h>1213/*14* gzip declarations15*/1617#define OF(args) args18#define STATIC static1920#undef memset21#undef memcpy22#define memzero(s, n) memset ((s), 0, (n))2324typedef unsigned char uch;25typedef unsigned short ush;26typedef unsigned long ulg;2728#define WSIZE 0x8000 /* Window size must be at least 32k, */29/* and a power of two */3031static uch *inbuf; /* input buffer */32static uch window[WSIZE]; /* Sliding window buffer */3334static unsigned insize = 0; /* valid bytes in inbuf */35static unsigned inptr = 0; /* index of next byte to be processed in inbuf */36static unsigned outcnt = 0; /* bytes in output buffer */3738/* gzip flag byte */39#define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */40#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */41#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */42#define ORIG_NAME 0x08 /* bit 3 set: original file name present */43#define COMMENT 0x10 /* bit 4 set: file comment present */44#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */45#define RESERVED 0xC0 /* bit 6,7: reserved */4647#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())4849/* Diagnostic functions */50#ifdef DEBUG51# define Assert(cond,msg) {if(!(cond)) error(msg);}52# define Trace(x) fprintf x53# define Tracev(x) {if (verbose) fprintf x ;}54# define Tracevv(x) {if (verbose>1) fprintf x ;}55# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}56# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}57#else58# define Assert(cond,msg)59# define Trace(x)60# define Tracev(x)61# define Tracevv(x)62# define Tracec(c,x)63# define Tracecv(c,x)64#endif6566static int fill_inbuf(void);67static void flush_window(void);68static void error(char *m);6970extern char input_data[];71extern int input_len;7273static long bytes_out = 0;74static uch *output_data;75static unsigned long output_ptr = 0;7677static void error(char *m);7879int puts(const char *);8081extern int _text; /* Defined in vmlinux.lds.S */82extern int _end;83static unsigned long free_mem_ptr;84static unsigned long free_mem_end_ptr;8586#define HEAP_SIZE 0x100008788#include "../../../../lib/inflate.c"8990#define SCR *((volatile unsigned char *)0xffff8a)91#define TDR *((volatile unsigned char *)0xffff8b)92#define SSR *((volatile unsigned char *)0xffff8c)9394int puts(const char *s)95{96return 0;97}9899void* memset(void* s, int c, size_t n)100{101int i;102char *ss = (char*)s;103104for (i=0;i<n;i++) ss[i] = c;105return s;106}107108void* memcpy(void* __dest, __const void* __src,109size_t __n)110{111int i;112char *d = (char *)__dest, *s = (char *)__src;113114for (i=0;i<__n;i++) d[i] = s[i];115return __dest;116}117118/* ===========================================================================119* Fill the input buffer. This is called only when the buffer is empty120* and at least one byte is really needed.121*/122static int fill_inbuf(void)123{124if (insize != 0) {125error("ran out of input data");126}127128inbuf = input_data;129insize = input_len;130inptr = 1;131return inbuf[0];132}133134/* ===========================================================================135* Write the output window window[0..outcnt-1] and update crc and bytes_out.136* (Used for the decompressed data only.)137*/138static void flush_window(void)139{140ulg c = crc; /* temporary variable */141unsigned n;142uch *in, *out, ch;143144in = window;145out = &output_data[output_ptr];146for (n = 0; n < outcnt; n++) {147ch = *out++ = *in++;148c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);149}150crc = c;151bytes_out += (ulg)outcnt;152output_ptr += (ulg)outcnt;153outcnt = 0;154}155156static void error(char *x)157{158puts("\n\n");159puts(x);160puts("\n\n -- System halted");161162while(1); /* Halt */163}164165#define STACK_SIZE (4096)166long user_stack [STACK_SIZE];167long* stack_start = &user_stack[STACK_SIZE];168169void decompress_kernel(void)170{171output_data = 0;172output_ptr = (unsigned long)0x400000;173free_mem_ptr = (unsigned long)&_end;174free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;175176makecrc();177puts("Uncompressing Linux... ");178gunzip();179puts("Ok, booting the kernel.\n");180}181182183