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