xref: /linux/arch/x86/boot/compressed/misc.c (revision 23968f71b26ece45ed52895d41b0208b90a516e7)
1778cb929SIan Campbell /*
2778cb929SIan Campbell  * misc.c
3778cb929SIan Campbell  *
4778cb929SIan Campbell  * This is a collection of several routines from gzip-1.0.3
5778cb929SIan Campbell  * adapted for Linux.
6778cb929SIan Campbell  *
7778cb929SIan Campbell  * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
8778cb929SIan Campbell  * puts by Nick Holloway 1993, better puts by Martin Mares 1995
9778cb929SIan Campbell  * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
10778cb929SIan Campbell  */
11778cb929SIan Campbell 
12778cb929SIan Campbell /*
13778cb929SIan Campbell  * we have to be careful, because no indirections are allowed here, and
14778cb929SIan Campbell  * paravirt_ops is a kind of one. As it will only run in baremetal anyway,
15778cb929SIan Campbell  * we just keep it from happening
16778cb929SIan Campbell  */
17778cb929SIan Campbell #undef CONFIG_PARAVIRT
18099e1377SIan Campbell #ifdef CONFIG_X86_32
19099e1377SIan Campbell #define _ASM_DESC_H_ 1
20099e1377SIan Campbell #endif
21099e1377SIan Campbell 
22778cb929SIan Campbell #ifdef CONFIG_X86_64
23778cb929SIan Campbell #define _LINUX_STRING_H_ 1
24778cb929SIan Campbell #define __LINUX_BITMAP_H 1
25778cb929SIan Campbell #endif
26778cb929SIan Campbell 
27778cb929SIan Campbell #include <linux/linkage.h>
28778cb929SIan Campbell #include <linux/screen_info.h>
29099e1377SIan Campbell #include <linux/elf.h>
30778cb929SIan Campbell #include <asm/io.h>
31778cb929SIan Campbell #include <asm/page.h>
32778cb929SIan Campbell #include <asm/boot.h>
33*23968f71SKristian Høgsberg #include <asm/bootparam.h>
34778cb929SIan Campbell 
35778cb929SIan Campbell /* WARNING!!
36778cb929SIan Campbell  * This code is compiled with -fPIC and it is relocated dynamically
37778cb929SIan Campbell  * at run time, but no relocation processing is performed.
38778cb929SIan Campbell  * This means that it is not safe to place pointers in static structures.
39778cb929SIan Campbell  */
40778cb929SIan Campbell 
41778cb929SIan Campbell /*
42778cb929SIan Campbell  * Getting to provable safe in place decompression is hard.
43778cb929SIan Campbell  * Worst case behaviours need to be analyzed.
44778cb929SIan Campbell  * Background information:
45778cb929SIan Campbell  *
46778cb929SIan Campbell  * The file layout is:
47778cb929SIan Campbell  *    magic[2]
48778cb929SIan Campbell  *    method[1]
49778cb929SIan Campbell  *    flags[1]
50778cb929SIan Campbell  *    timestamp[4]
51778cb929SIan Campbell  *    extraflags[1]
52778cb929SIan Campbell  *    os[1]
53778cb929SIan Campbell  *    compressed data blocks[N]
54778cb929SIan Campbell  *    crc[4] orig_len[4]
55778cb929SIan Campbell  *
56778cb929SIan Campbell  * resulting in 18 bytes of non compressed data overhead.
57778cb929SIan Campbell  *
58778cb929SIan Campbell  * Files divided into blocks
59778cb929SIan Campbell  * 1 bit (last block flag)
60778cb929SIan Campbell  * 2 bits (block type)
61778cb929SIan Campbell  *
621180e01dSIngo Molnar  * 1 block occurs every 32K -1 bytes or when there 50% compression
631180e01dSIngo Molnar  * has been achieved. The smallest block type encoding is always used.
64778cb929SIan Campbell  *
65778cb929SIan Campbell  * stored:
66778cb929SIan Campbell  *    32 bits length in bytes.
67778cb929SIan Campbell  *
68778cb929SIan Campbell  * fixed:
69778cb929SIan Campbell  *    magic fixed tree.
70778cb929SIan Campbell  *    symbols.
71778cb929SIan Campbell  *
72778cb929SIan Campbell  * dynamic:
73778cb929SIan Campbell  *    dynamic tree encoding.
74778cb929SIan Campbell  *    symbols.
75778cb929SIan Campbell  *
76778cb929SIan Campbell  *
77778cb929SIan Campbell  * The buffer for decompression in place is the length of the
78778cb929SIan Campbell  * uncompressed data, plus a small amount extra to keep the algorithm safe.
79778cb929SIan Campbell  * The compressed data is placed at the end of the buffer.  The output
80778cb929SIan Campbell  * pointer is placed at the start of the buffer and the input pointer
81778cb929SIan Campbell  * is placed where the compressed data starts.  Problems will occur
82778cb929SIan Campbell  * when the output pointer overruns the input pointer.
83778cb929SIan Campbell  *
84778cb929SIan Campbell  * The output pointer can only overrun the input pointer if the input
85778cb929SIan Campbell  * pointer is moving faster than the output pointer.  A condition only
86778cb929SIan Campbell  * triggered by data whose compressed form is larger than the uncompressed
87778cb929SIan Campbell  * form.
88778cb929SIan Campbell  *
89778cb929SIan Campbell  * The worst case at the block level is a growth of the compressed data
90778cb929SIan Campbell  * of 5 bytes per 32767 bytes.
91778cb929SIan Campbell  *
92778cb929SIan Campbell  * The worst case internal to a compressed block is very hard to figure.
93778cb929SIan Campbell  * The worst case can at least be boundined by having one bit that represents
94778cb929SIan Campbell  * 32764 bytes and then all of the rest of the bytes representing the very
95778cb929SIan Campbell  * very last byte.
96778cb929SIan Campbell  *
97778cb929SIan Campbell  * All of which is enough to compute an amount of extra data that is required
98778cb929SIan Campbell  * to be safe.  To avoid problems at the block level allocating 5 extra bytes
991180e01dSIngo Molnar  * per 32767 bytes of data is sufficient.  To avoind problems internal to a
1001180e01dSIngo Molnar  * block adding an extra 32767 bytes (the worst case uncompressed block size)
1011180e01dSIngo Molnar  * is sufficient, to ensure that in the worst case the decompressed data for
102778cb929SIan Campbell  * block will stop the byte before the compressed data for a block begins.
103778cb929SIan Campbell  * To avoid problems with the compressed data's meta information an extra 18
104778cb929SIan Campbell  * bytes are needed.  Leading to the formula:
105778cb929SIan Campbell  *
106778cb929SIan Campbell  * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
107778cb929SIan Campbell  *
108778cb929SIan Campbell  * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
109778cb929SIan Campbell  * Adding 32768 instead of 32767 just makes for round numbers.
110778cb929SIan Campbell  * Adding the decompressor_size is necessary as it musht live after all
111778cb929SIan Campbell  * of the data as well.  Last I measured the decompressor is about 14K.
112778cb929SIan Campbell  * 10K of actual data and 4K of bss.
113778cb929SIan Campbell  *
114778cb929SIan Campbell  */
115778cb929SIan Campbell 
116778cb929SIan Campbell /*
117778cb929SIan Campbell  * gzip declarations
118778cb929SIan Campbell  */
119778cb929SIan Campbell 
120778cb929SIan Campbell #define OF(args)	args
121778cb929SIan Campbell #define STATIC		static
122778cb929SIan Campbell 
123778cb929SIan Campbell #undef memset
124778cb929SIan Campbell #undef memcpy
125778cb929SIan Campbell #define memzero(s, n)	memset((s), 0, (n))
126778cb929SIan Campbell 
127778cb929SIan Campbell typedef unsigned char	uch;
128778cb929SIan Campbell typedef unsigned short	ush;
129778cb929SIan Campbell typedef unsigned long	ulg;
130778cb929SIan Campbell 
1311180e01dSIngo Molnar /*
1321180e01dSIngo Molnar  * Window size must be at least 32k, and a power of two.
1331180e01dSIngo Molnar  * We don't actually have a window just a huge output buffer,
1341180e01dSIngo Molnar  * so we report a 2G window size, as that should always be
1351180e01dSIngo Molnar  * larger than our output buffer:
136778cb929SIan Campbell  */
1371180e01dSIngo Molnar #define WSIZE		0x80000000
138778cb929SIan Campbell 
1391180e01dSIngo Molnar /* Input buffer: */
1401180e01dSIngo Molnar static unsigned char	*inbuf;
141778cb929SIan Campbell 
1421180e01dSIngo Molnar /* Sliding window buffer (and final output buffer): */
1431180e01dSIngo Molnar static unsigned char	*window;
1441180e01dSIngo Molnar 
1451180e01dSIngo Molnar /* Valid bytes in inbuf: */
1461180e01dSIngo Molnar static unsigned		insize;
1471180e01dSIngo Molnar 
1481180e01dSIngo Molnar /* Index of next byte to be processed in inbuf: */
1491180e01dSIngo Molnar static unsigned		inptr;
1501180e01dSIngo Molnar 
1511180e01dSIngo Molnar /* Bytes in output buffer: */
1521180e01dSIngo Molnar static unsigned		outcnt;
153778cb929SIan Campbell 
154778cb929SIan Campbell /* gzip flag byte */
155778cb929SIan Campbell #define ASCII_FLAG	0x01 /* bit 0 set: file probably ASCII text */
1561180e01dSIngo Molnar #define CONTINUATION	0x02 /* bit 1 set: continuation of multi-part gz file */
157778cb929SIan Campbell #define EXTRA_FIELD	0x04 /* bit 2 set: extra field present */
1581180e01dSIngo Molnar #define ORIG_NAM	0x08 /* bit 3 set: original file name present */
159778cb929SIan Campbell #define COMMENT		0x10 /* bit 4 set: file comment present */
160778cb929SIan Campbell #define ENCRYPTED	0x20 /* bit 5 set: file is encrypted */
161778cb929SIan Campbell #define RESERVED	0xC0 /* bit 6, 7:  reserved */
162778cb929SIan Campbell 
163778cb929SIan Campbell #define get_byte()	(inptr < insize ? inbuf[inptr++] : fill_inbuf())
164778cb929SIan Campbell 
165778cb929SIan Campbell /* Diagnostic functions */
166778cb929SIan Campbell #ifdef DEBUG
1671180e01dSIngo Molnar #  define Assert(cond, msg) do { if (!(cond)) error(msg); } while (0)
1681180e01dSIngo Molnar #  define Trace(x)	do { fprintf x; } while (0)
1691180e01dSIngo Molnar #  define Tracev(x)	do { if (verbose) fprintf x ; } while (0)
1701180e01dSIngo Molnar #  define Tracevv(x)	do { if (verbose > 1) fprintf x ; } while (0)
1711180e01dSIngo Molnar #  define Tracec(c, x)	do { if (verbose && (c)) fprintf x ; } while (0)
1721180e01dSIngo Molnar #  define Tracecv(c, x)	do { if (verbose > 1 && (c)) fprintf x ; } while (0)
173778cb929SIan Campbell #else
174778cb929SIan Campbell #  define Assert(cond, msg)
175778cb929SIan Campbell #  define Trace(x)
176778cb929SIan Campbell #  define Tracev(x)
177778cb929SIan Campbell #  define Tracevv(x)
178778cb929SIan Campbell #  define Tracec(c, x)
179778cb929SIan Campbell #  define Tracecv(c, x)
180778cb929SIan Campbell #endif
181778cb929SIan Campbell 
182778cb929SIan Campbell static int  fill_inbuf(void);
183778cb929SIan Campbell static void flush_window(void);
184778cb929SIan Campbell static void error(char *m);
185778cb929SIan Campbell static void gzip_mark(void **);
186778cb929SIan Campbell static void gzip_release(void **);
187778cb929SIan Campbell 
188778cb929SIan Campbell /*
189778cb929SIan Campbell  * This is set up by the setup-routine at boot-time
190778cb929SIan Campbell  */
191*23968f71SKristian Høgsberg static struct boot_params *real_mode;		/* Pointer to real-mode data */
192778cb929SIan Campbell 
193778cb929SIan Campbell extern unsigned char input_data[];
194778cb929SIan Campbell extern int input_len;
195778cb929SIan Campbell 
196fd77c7caSPaolo Ciarrocchi static long bytes_out;
197778cb929SIan Campbell 
198778cb929SIan Campbell static void *malloc(int size);
199778cb929SIan Campbell static void free(void *where);
200778cb929SIan Campbell 
201778cb929SIan Campbell static void *memset(void *s, int c, unsigned n);
202778cb929SIan Campbell static void *memcpy(void *dest, const void *src, unsigned n);
203778cb929SIan Campbell 
204778cb929SIan Campbell static void putstr(const char *);
205778cb929SIan Campbell 
206778cb929SIan Campbell #ifdef CONFIG_X86_64
207778cb929SIan Campbell #define memptr long
208778cb929SIan Campbell #else
209778cb929SIan Campbell #define memptr unsigned
210778cb929SIan Campbell #endif
211778cb929SIan Campbell 
212778cb929SIan Campbell static memptr free_mem_ptr;
213778cb929SIan Campbell static memptr free_mem_end_ptr;
214778cb929SIan Campbell 
21503056c88SAlexander van Heukelum static char *vidmem;
216778cb929SIan Campbell static int vidport;
217778cb929SIan Campbell static int lines, cols;
218778cb929SIan Campbell 
219778cb929SIan Campbell #ifdef CONFIG_X86_NUMAQ
220778cb929SIan Campbell void *xquad_portio;
221778cb929SIan Campbell #endif
222778cb929SIan Campbell 
223778cb929SIan Campbell #include "../../../../lib/inflate.c"
224778cb929SIan Campbell 
225778cb929SIan Campbell static void *malloc(int size)
226778cb929SIan Campbell {
227778cb929SIan Campbell 	void *p;
228778cb929SIan Campbell 
229fd77c7caSPaolo Ciarrocchi 	if (size < 0)
230fd77c7caSPaolo Ciarrocchi 		error("Malloc error");
231fd77c7caSPaolo Ciarrocchi 	if (free_mem_ptr <= 0)
232fd77c7caSPaolo Ciarrocchi 		error("Memory error");
233778cb929SIan Campbell 
234778cb929SIan Campbell 	free_mem_ptr = (free_mem_ptr + 3) & ~3;	/* Align */
235778cb929SIan Campbell 
236778cb929SIan Campbell 	p = (void *)free_mem_ptr;
237778cb929SIan Campbell 	free_mem_ptr += size;
238778cb929SIan Campbell 
239778cb929SIan Campbell 	if (free_mem_ptr >= free_mem_end_ptr)
240778cb929SIan Campbell 		error("Out of memory");
241778cb929SIan Campbell 
242778cb929SIan Campbell 	return p;
243778cb929SIan Campbell }
244778cb929SIan Campbell 
245778cb929SIan Campbell static void free(void *where)
246778cb929SIan Campbell {	/* Don't care */
247778cb929SIan Campbell }
248778cb929SIan Campbell 
249778cb929SIan Campbell static void gzip_mark(void **ptr)
250778cb929SIan Campbell {
251778cb929SIan Campbell 	*ptr = (void *) free_mem_ptr;
252778cb929SIan Campbell }
253778cb929SIan Campbell 
254778cb929SIan Campbell static void gzip_release(void **ptr)
255778cb929SIan Campbell {
256778cb929SIan Campbell 	free_mem_ptr = (memptr) *ptr;
257778cb929SIan Campbell }
258778cb929SIan Campbell 
259778cb929SIan Campbell static void scroll(void)
260778cb929SIan Campbell {
261778cb929SIan Campbell 	int i;
262778cb929SIan Campbell 
263778cb929SIan Campbell 	memcpy(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
264778cb929SIan Campbell 	for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
265778cb929SIan Campbell 		vidmem[i] = ' ';
266778cb929SIan Campbell }
267778cb929SIan Campbell 
268778cb929SIan Campbell static void putstr(const char *s)
269778cb929SIan Campbell {
270778cb929SIan Campbell 	int x, y, pos;
271778cb929SIan Campbell 	char c;
272778cb929SIan Campbell 
273778cb929SIan Campbell #ifdef CONFIG_X86_32
274*23968f71SKristian Høgsberg 	if (real_mode->screen_info.orig_video_mode == 0 &&
275*23968f71SKristian Høgsberg 	    lines == 0 && cols == 0)
276778cb929SIan Campbell 		return;
277778cb929SIan Campbell #endif
278778cb929SIan Campbell 
279*23968f71SKristian Høgsberg 	x = real_mode->screen_info.orig_x;
280*23968f71SKristian Høgsberg 	y = real_mode->screen_info.orig_y;
281778cb929SIan Campbell 
282778cb929SIan Campbell 	while ((c = *s++) != '\0') {
283778cb929SIan Campbell 		if (c == '\n') {
284778cb929SIan Campbell 			x = 0;
285778cb929SIan Campbell 			if (++y >= lines) {
286778cb929SIan Campbell 				scroll();
287778cb929SIan Campbell 				y--;
288778cb929SIan Campbell 			}
289778cb929SIan Campbell 		} else {
290778cb929SIan Campbell 			vidmem [(x + cols * y) * 2] = c;
291778cb929SIan Campbell 			if (++x >= cols) {
292778cb929SIan Campbell 				x = 0;
293778cb929SIan Campbell 				if (++y >= lines) {
294778cb929SIan Campbell 					scroll();
295778cb929SIan Campbell 					y--;
296778cb929SIan Campbell 				}
297778cb929SIan Campbell 			}
298778cb929SIan Campbell 		}
299778cb929SIan Campbell 	}
300778cb929SIan Campbell 
301*23968f71SKristian Høgsberg 	real_mode->screen_info.orig_x = x;
302*23968f71SKristian Høgsberg 	real_mode->screen_info.orig_y = y;
303778cb929SIan Campbell 
304778cb929SIan Campbell 	pos = (x + cols * y) * 2;	/* Update cursor position */
305778cb929SIan Campbell 	outb(14, vidport);
306778cb929SIan Campbell 	outb(0xff & (pos >> 9), vidport+1);
307778cb929SIan Campbell 	outb(15, vidport);
308778cb929SIan Campbell 	outb(0xff & (pos >> 1), vidport+1);
309778cb929SIan Campbell }
310778cb929SIan Campbell 
311778cb929SIan Campbell static void *memset(void *s, int c, unsigned n)
312778cb929SIan Campbell {
313778cb929SIan Campbell 	int i;
314778cb929SIan Campbell 	char *ss = s;
315778cb929SIan Campbell 
316778cb929SIan Campbell 	for (i = 0; i < n; i++) ss[i] = c;
317778cb929SIan Campbell 	return s;
318778cb929SIan Campbell }
319778cb929SIan Campbell 
320778cb929SIan Campbell static void *memcpy(void *dest, const void *src, unsigned n)
321778cb929SIan Campbell {
322778cb929SIan Campbell 	int i;
323778cb929SIan Campbell 	const char *s = src;
324778cb929SIan Campbell 	char *d = dest;
325778cb929SIan Campbell 
326778cb929SIan Campbell 	for (i = 0; i < n; i++) d[i] = s[i];
327778cb929SIan Campbell 	return dest;
328778cb929SIan Campbell }
329778cb929SIan Campbell 
330778cb929SIan Campbell /* ===========================================================================
331778cb929SIan Campbell  * Fill the input buffer. This is called only when the buffer is empty
332778cb929SIan Campbell  * and at least one byte is really needed.
333778cb929SIan Campbell  */
334778cb929SIan Campbell static int fill_inbuf(void)
335778cb929SIan Campbell {
336778cb929SIan Campbell 	error("ran out of input data");
337778cb929SIan Campbell 	return 0;
338778cb929SIan Campbell }
339778cb929SIan Campbell 
340778cb929SIan Campbell /* ===========================================================================
341778cb929SIan Campbell  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
342778cb929SIan Campbell  * (Used for the decompressed data only.)
343778cb929SIan Campbell  */
344778cb929SIan Campbell static void flush_window(void)
345778cb929SIan Campbell {
346778cb929SIan Campbell 	/* With my window equal to my output buffer
347778cb929SIan Campbell 	 * I only need to compute the crc here.
348778cb929SIan Campbell 	 */
3491180e01dSIngo Molnar 	unsigned long c = crc;         /* temporary variable */
350778cb929SIan Campbell 	unsigned n;
3511180e01dSIngo Molnar 	unsigned char *in, ch;
352778cb929SIan Campbell 
353778cb929SIan Campbell 	in = window;
354778cb929SIan Campbell 	for (n = 0; n < outcnt; n++) {
355778cb929SIan Campbell 		ch = *in++;
356778cb929SIan Campbell 		c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
357778cb929SIan Campbell 	}
358778cb929SIan Campbell 	crc = c;
3591180e01dSIngo Molnar 	bytes_out += (unsigned long)outcnt;
360778cb929SIan Campbell 	outcnt = 0;
361778cb929SIan Campbell }
362778cb929SIan Campbell 
363778cb929SIan Campbell static void error(char *x)
364778cb929SIan Campbell {
365778cb929SIan Campbell 	putstr("\n\n");
366778cb929SIan Campbell 	putstr(x);
367778cb929SIan Campbell 	putstr("\n\n -- System halted");
368778cb929SIan Campbell 
369778cb929SIan Campbell 	while (1)
370778cb929SIan Campbell 		asm("hlt");
371778cb929SIan Campbell }
372778cb929SIan Campbell 
373099e1377SIan Campbell static void parse_elf(void *output)
374099e1377SIan Campbell {
375099e1377SIan Campbell #ifdef CONFIG_X86_64
376099e1377SIan Campbell 	Elf64_Ehdr ehdr;
377099e1377SIan Campbell 	Elf64_Phdr *phdrs, *phdr;
378099e1377SIan Campbell #else
379099e1377SIan Campbell 	Elf32_Ehdr ehdr;
380099e1377SIan Campbell 	Elf32_Phdr *phdrs, *phdr;
381099e1377SIan Campbell #endif
382099e1377SIan Campbell 	void *dest;
383099e1377SIan Campbell 	int i;
384099e1377SIan Campbell 
385099e1377SIan Campbell 	memcpy(&ehdr, output, sizeof(ehdr));
386099e1377SIan Campbell 	if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
387099e1377SIan Campbell 	   ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
388099e1377SIan Campbell 	   ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
389fd77c7caSPaolo Ciarrocchi 	   ehdr.e_ident[EI_MAG3] != ELFMAG3) {
390099e1377SIan Campbell 		error("Kernel is not a valid ELF file");
391099e1377SIan Campbell 		return;
392099e1377SIan Campbell 	}
393099e1377SIan Campbell 
394099e1377SIan Campbell 	putstr("Parsing ELF... ");
395099e1377SIan Campbell 
396099e1377SIan Campbell 	phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
397099e1377SIan Campbell 	if (!phdrs)
398099e1377SIan Campbell 		error("Failed to allocate space for phdrs");
399099e1377SIan Campbell 
400099e1377SIan Campbell 	memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
401099e1377SIan Campbell 
402099e1377SIan Campbell 	for (i = 0; i < ehdr.e_phnum; i++) {
403099e1377SIan Campbell 		phdr = &phdrs[i];
404099e1377SIan Campbell 
405099e1377SIan Campbell 		switch (phdr->p_type) {
406099e1377SIan Campbell 		case PT_LOAD:
407099e1377SIan Campbell #ifdef CONFIG_RELOCATABLE
408099e1377SIan Campbell 			dest = output;
409099e1377SIan Campbell 			dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
410099e1377SIan Campbell #else
411099e1377SIan Campbell 			dest = (void *)(phdr->p_paddr);
412099e1377SIan Campbell #endif
413099e1377SIan Campbell 			memcpy(dest,
414099e1377SIan Campbell 			       output + phdr->p_offset,
415099e1377SIan Campbell 			       phdr->p_filesz);
416099e1377SIan Campbell 			break;
417099e1377SIan Campbell 		default: /* Ignore other PT_* */ break;
418099e1377SIan Campbell 		}
419099e1377SIan Campbell 	}
420099e1377SIan Campbell }
421099e1377SIan Campbell 
422778cb929SIan Campbell asmlinkage void decompress_kernel(void *rmode, memptr heap,
4231180e01dSIngo Molnar 				  unsigned char *input_data,
4241180e01dSIngo Molnar 				  unsigned long input_len,
4251180e01dSIngo Molnar 				  unsigned char *output)
426778cb929SIan Campbell {
427778cb929SIan Campbell 	real_mode = rmode;
428778cb929SIan Campbell 
429*23968f71SKristian Høgsberg 	if (real_mode->screen_info.orig_video_mode == 7) {
430778cb929SIan Campbell 		vidmem = (char *) 0xb0000;
431778cb929SIan Campbell 		vidport = 0x3b4;
432778cb929SIan Campbell 	} else {
433778cb929SIan Campbell 		vidmem = (char *) 0xb8000;
434778cb929SIan Campbell 		vidport = 0x3d4;
435778cb929SIan Campbell 	}
436778cb929SIan Campbell 
437*23968f71SKristian Høgsberg 	lines = real_mode->screen_info.orig_video_lines;
438*23968f71SKristian Høgsberg 	cols = real_mode->screen_info.orig_video_cols;
439778cb929SIan Campbell 
440778cb929SIan Campbell 	window = output;		/* Output buffer (Normally at 1M) */
441778cb929SIan Campbell 	free_mem_ptr     = heap;	/* Heap */
4427c539764SAlexander van Heukelum 	free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
443778cb929SIan Campbell 	inbuf  = input_data;		/* Input buffer */
444778cb929SIan Campbell 	insize = input_len;
445778cb929SIan Campbell 	inptr  = 0;
446778cb929SIan Campbell 
447778cb929SIan Campbell #ifdef CONFIG_X86_64
4481180e01dSIngo Molnar 	if ((unsigned long)output & (__KERNEL_ALIGN - 1))
449778cb929SIan Campbell 		error("Destination address not 2M aligned");
4501180e01dSIngo Molnar 	if ((unsigned long)output >= 0xffffffffffUL)
451778cb929SIan Campbell 		error("Destination address too large");
452778cb929SIan Campbell #else
453778cb929SIan Campbell 	if ((u32)output & (CONFIG_PHYSICAL_ALIGN - 1))
454778cb929SIan Campbell 		error("Destination address not CONFIG_PHYSICAL_ALIGN aligned");
455778cb929SIan Campbell 	if (heap > ((-__PAGE_OFFSET-(512<<20)-1) & 0x7fffffff))
456778cb929SIan Campbell 		error("Destination address too large");
457778cb929SIan Campbell #ifndef CONFIG_RELOCATABLE
458778cb929SIan Campbell 	if ((u32)output != LOAD_PHYSICAL_ADDR)
459778cb929SIan Campbell 		error("Wrong destination address");
460778cb929SIan Campbell #endif
461778cb929SIan Campbell #endif
462778cb929SIan Campbell 
463778cb929SIan Campbell 	makecrc();
464778cb929SIan Campbell 	putstr("\nDecompressing Linux... ");
465778cb929SIan Campbell 	gunzip();
466099e1377SIan Campbell 	parse_elf(output);
467778cb929SIan Campbell 	putstr("done.\nBooting the kernel.\n");
468778cb929SIan Campbell 	return;
469778cb929SIan Campbell }
470