]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/h8300/boot/compressed/misc.c
Merge tag 'batman-adv-for-davem' of git://git.open-mesh.org/linux-merge
[karo-tx-linux.git] / arch / h8300 / boot / compressed / misc.c
1 /*
2  * arch/h8300/boot/compressed/misc.c
3  *
4  * This is a collection of several routines from gzip-1.0.3
5  * adapted for Linux.
6  *
7  * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
8  *
9  * Adapted for h8300 by Yoshinori Sato 2006
10  */
11
12 #include <asm/uaccess.h>
13
14 /*
15  * gzip declarations
16  */
17
18 #define OF(args)  args
19 #define STATIC static
20
21 #undef memset
22 #undef memcpy
23 #define memzero(s, n)     memset ((s), 0, (n))
24
25 typedef unsigned char  uch;
26 typedef unsigned short ush;
27 typedef unsigned long  ulg;
28
29 #define WSIZE 0x8000            /* Window size must be at least 32k, */
30                                 /* and a power of two */
31
32 static uch *inbuf;           /* input buffer */
33 static uch window[WSIZE];    /* Sliding window buffer */
34
35 static unsigned insize = 0;  /* valid bytes in inbuf */
36 static unsigned inptr = 0;   /* index of next byte to be processed in inbuf */
37 static unsigned outcnt = 0;  /* bytes in output buffer */
38
39 /* gzip flag byte */
40 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ASCII text */
41 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
42 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
43 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
44 #define COMMENT      0x10 /* bit 4 set: file comment present */
45 #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
46 #define RESERVED     0xC0 /* bit 6,7:   reserved */
47
48 #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
49
50 /* Diagnostic functions */
51 #ifdef DEBUG
52 #  define Assert(cond,msg) {if(!(cond)) error(msg);}
53 #  define Trace(x) fprintf x
54 #  define Tracev(x) {if (verbose) fprintf x ;}
55 #  define Tracevv(x) {if (verbose>1) fprintf x ;}
56 #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
57 #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
58 #else
59 #  define Assert(cond,msg)
60 #  define Trace(x)
61 #  define Tracev(x)
62 #  define Tracevv(x)
63 #  define Tracec(c,x)
64 #  define Tracecv(c,x)
65 #endif
66
67 static int  fill_inbuf(void);
68 static void flush_window(void);
69 static void error(char *m);
70
71 extern char input_data[];
72 extern int input_len;
73
74 static long bytes_out = 0;
75 static uch *output_data;
76 static unsigned long output_ptr = 0;
77
78 static void error(char *m);
79
80 int puts(const char *);
81
82 extern int _end;
83 static unsigned long free_mem_ptr;
84 static unsigned long free_mem_end_ptr;
85
86 #define HEAP_SIZE             0x10000
87
88 #include "../../../../lib/inflate.c"
89
90 #define SCR *((volatile unsigned char *)0xffff8a)
91 #define TDR *((volatile unsigned char *)0xffff8b)
92 #define SSR *((volatile unsigned char *)0xffff8c)
93
94 int puts(const char *s)
95 {
96         return 0;
97 }
98
99 void* memset(void* s, int c, size_t n)
100 {
101         int i;
102         char *ss = (char*)s;
103
104         for (i=0;i<n;i++) ss[i] = c;
105         return s;
106 }
107
108 void* memcpy(void* __dest, __const void* __src,
109                             size_t __n)
110 {
111         int i;
112         char *d = (char *)__dest, *s = (char *)__src;
113
114         for (i=0;i<__n;i++) d[i] = s[i];
115         return __dest;
116 }
117
118 /* ===========================================================================
119  * Fill the input buffer. This is called only when the buffer is empty
120  * and at least one byte is really needed.
121  */
122 static int fill_inbuf(void)
123 {
124         if (insize != 0) {
125                 error("ran out of input data");
126         }
127
128         inbuf = input_data;
129         insize = input_len;
130         inptr = 1;
131         return inbuf[0];
132 }
133
134 /* ===========================================================================
135  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
136  * (Used for the decompressed data only.)
137  */
138 static void flush_window(void)
139 {
140     ulg c = crc;         /* temporary variable */
141     unsigned n;
142     uch *in, *out, ch;
143
144     in = window;
145     out = &output_data[output_ptr];
146     for (n = 0; n < outcnt; n++) {
147             ch = *out++ = *in++;
148             c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
149     }
150     crc = c;
151     bytes_out += (ulg)outcnt;
152     output_ptr += (ulg)outcnt;
153     outcnt = 0;
154 }
155
156 static void error(char *x)
157 {
158         puts("\n\n");
159         puts(x);
160         puts("\n\n -- System halted");
161
162         while(1);       /* Halt */
163 }
164
165 #define STACK_SIZE (4096)
166 long user_stack [STACK_SIZE];
167 long* stack_start = &user_stack[STACK_SIZE];
168
169 void decompress_kernel(void)
170 {
171         output_data = 0;
172         output_ptr = (unsigned long)0x400000;
173         free_mem_ptr = (unsigned long)&_end;
174         free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
175
176         makecrc();
177         puts("Uncompressing Linux... ");
178         gunzip();
179         puts("Ok, booting the kernel.\n");
180 }