xref: /OK3568_Linux_fs/kernel/arch/alpha/boot/misc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * misc.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This is a collection of several routines from gzip-1.0.3
6*4882a593Smuzhiyun  * adapted for Linux.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Modified for ARM Linux by Russell King
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Nicolas Pitre <nico@visuaide.com>  1999/04/14 :
13*4882a593Smuzhiyun  *  For this code to run directly from Flash, all constant variables must
14*4882a593Smuzhiyun  *  be marked with 'const' and all other variables initialized at run-time
15*4882a593Smuzhiyun  *  only.  This way all non constant variables will end up in the bss segment,
16*4882a593Smuzhiyun  *  which should point to addresses in RAM and cleared to 0 on start.
17*4882a593Smuzhiyun  *  This allows for a much quicker boot time.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * Modified for Alpha, from the ARM version, by Jay Estabrook 2003.
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <linux/kernel.h>
23*4882a593Smuzhiyun #include <linux/slab.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <linux/uaccess.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define memzero(s,n)	memset ((s),0,(n))
28*4882a593Smuzhiyun #define puts		srm_printk
29*4882a593Smuzhiyun extern long srm_printk(const char *, ...)
30*4882a593Smuzhiyun      __attribute__ ((format (printf, 1, 2)));
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun  * gzip delarations
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun #define OF(args)  args
36*4882a593Smuzhiyun #define STATIC static
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun typedef unsigned char  uch;
39*4882a593Smuzhiyun typedef unsigned short ush;
40*4882a593Smuzhiyun typedef unsigned long  ulg;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define WSIZE 0x8000		/* Window size must be at least 32k, */
43*4882a593Smuzhiyun 				/* and a power of two */
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun static uch *inbuf;		/* input buffer */
46*4882a593Smuzhiyun static uch *window;		/* Sliding window buffer */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun static unsigned insize;		/* valid bytes in inbuf */
49*4882a593Smuzhiyun static unsigned inptr;		/* index of next byte to be processed in inbuf */
50*4882a593Smuzhiyun static unsigned outcnt;		/* bytes in output buffer */
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /* gzip flag byte */
53*4882a593Smuzhiyun #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
54*4882a593Smuzhiyun #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
55*4882a593Smuzhiyun #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
56*4882a593Smuzhiyun #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
57*4882a593Smuzhiyun #define COMMENT      0x10 /* bit 4 set: file comment present */
58*4882a593Smuzhiyun #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
59*4882a593Smuzhiyun #define RESERVED     0xC0 /* bit 6,7:   reserved */
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /* Diagnostic functions */
64*4882a593Smuzhiyun #ifdef DEBUG
65*4882a593Smuzhiyun #  define Assert(cond,msg) {if(!(cond)) error(msg);}
66*4882a593Smuzhiyun #  define Trace(x) fprintf x
67*4882a593Smuzhiyun #  define Tracev(x) {if (verbose) fprintf x ;}
68*4882a593Smuzhiyun #  define Tracevv(x) {if (verbose>1) fprintf x ;}
69*4882a593Smuzhiyun #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
70*4882a593Smuzhiyun #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
71*4882a593Smuzhiyun #else
72*4882a593Smuzhiyun #  define Assert(cond,msg)
73*4882a593Smuzhiyun #  define Trace(x)
74*4882a593Smuzhiyun #  define Tracev(x)
75*4882a593Smuzhiyun #  define Tracevv(x)
76*4882a593Smuzhiyun #  define Tracec(c,x)
77*4882a593Smuzhiyun #  define Tracecv(c,x)
78*4882a593Smuzhiyun #endif
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun static int  fill_inbuf(void);
81*4882a593Smuzhiyun static void flush_window(void);
82*4882a593Smuzhiyun static void error(char *m);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun static char *input_data;
85*4882a593Smuzhiyun static int  input_data_size;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun static uch *output_data;
88*4882a593Smuzhiyun static ulg output_ptr;
89*4882a593Smuzhiyun static ulg bytes_out;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun static void error(char *m);
92*4882a593Smuzhiyun static void gzip_mark(void **);
93*4882a593Smuzhiyun static void gzip_release(void **);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun extern int end;
96*4882a593Smuzhiyun static ulg free_mem_ptr;
97*4882a593Smuzhiyun static ulg free_mem_end_ptr;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun #define HEAP_SIZE 0x3000
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun #include "../../../lib/inflate.c"
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /* ===========================================================================
104*4882a593Smuzhiyun  * Fill the input buffer. This is called only when the buffer is empty
105*4882a593Smuzhiyun  * and at least one byte is really needed.
106*4882a593Smuzhiyun  */
fill_inbuf(void)107*4882a593Smuzhiyun int fill_inbuf(void)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	if (insize != 0)
110*4882a593Smuzhiyun 		error("ran out of input data");
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	inbuf = input_data;
113*4882a593Smuzhiyun 	insize = input_data_size;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	inptr = 1;
116*4882a593Smuzhiyun 	return inbuf[0];
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /* ===========================================================================
120*4882a593Smuzhiyun  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
121*4882a593Smuzhiyun  * (Used for the decompressed data only.)
122*4882a593Smuzhiyun  */
flush_window(void)123*4882a593Smuzhiyun void flush_window(void)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	ulg c = crc;
126*4882a593Smuzhiyun 	unsigned n;
127*4882a593Smuzhiyun 	uch *in, *out, ch;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	in = window;
130*4882a593Smuzhiyun 	out = &output_data[output_ptr];
131*4882a593Smuzhiyun 	for (n = 0; n < outcnt; n++) {
132*4882a593Smuzhiyun 		ch = *out++ = *in++;
133*4882a593Smuzhiyun 		c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
134*4882a593Smuzhiyun 	}
135*4882a593Smuzhiyun 	crc = c;
136*4882a593Smuzhiyun 	bytes_out += (ulg)outcnt;
137*4882a593Smuzhiyun 	output_ptr += (ulg)outcnt;
138*4882a593Smuzhiyun 	outcnt = 0;
139*4882a593Smuzhiyun /*	puts("."); */
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
error(char * x)142*4882a593Smuzhiyun static void error(char *x)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	puts("\n\n");
145*4882a593Smuzhiyun 	puts(x);
146*4882a593Smuzhiyun 	puts("\n\n -- System halted");
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	while(1);	/* Halt */
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun unsigned int
decompress_kernel(void * output_start,void * input_start,size_t ksize,size_t kzsize)152*4882a593Smuzhiyun decompress_kernel(void *output_start,
153*4882a593Smuzhiyun 		  void *input_start,
154*4882a593Smuzhiyun 		  size_t ksize,
155*4882a593Smuzhiyun 		  size_t kzsize)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	output_data		= (uch *)output_start;
158*4882a593Smuzhiyun 	input_data		= (uch *)input_start;
159*4882a593Smuzhiyun 	input_data_size		= kzsize; /* use compressed size */
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	/* FIXME FIXME FIXME */
162*4882a593Smuzhiyun 	free_mem_ptr		= (ulg)output_start + ksize;
163*4882a593Smuzhiyun 	free_mem_end_ptr	= (ulg)output_start + ksize + 0x200000;
164*4882a593Smuzhiyun 	/* FIXME FIXME FIXME */
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	/* put in temp area to reduce initial footprint */
167*4882a593Smuzhiyun 	window = malloc(WSIZE);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	makecrc();
170*4882a593Smuzhiyun /*	puts("Uncompressing Linux..."); */
171*4882a593Smuzhiyun 	gunzip();
172*4882a593Smuzhiyun /*	puts(" done, booting the kernel.\n"); */
173*4882a593Smuzhiyun 	return output_ptr;
174*4882a593Smuzhiyun }
175