xref: /linux/arch/x86/boot/compressed/misc.c (revision 1180e01de50c0c7683c6648251f32957bc2d7850)
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>
33778cb929SIan Campbell 
34778cb929SIan Campbell /* WARNING!!
35778cb929SIan Campbell  * This code is compiled with -fPIC and it is relocated dynamically
36778cb929SIan Campbell  * at run time, but no relocation processing is performed.
37778cb929SIan Campbell  * This means that it is not safe to place pointers in static structures.
38778cb929SIan Campbell  */
39778cb929SIan Campbell 
40778cb929SIan Campbell /*
41778cb929SIan Campbell  * Getting to provable safe in place decompression is hard.
42778cb929SIan Campbell  * Worst case behaviours need to be analyzed.
43778cb929SIan Campbell  * Background information:
44778cb929SIan Campbell  *
45778cb929SIan Campbell  * The file layout is:
46778cb929SIan Campbell  *    magic[2]
47778cb929SIan Campbell  *    method[1]
48778cb929SIan Campbell  *    flags[1]
49778cb929SIan Campbell  *    timestamp[4]
50778cb929SIan Campbell  *    extraflags[1]
51778cb929SIan Campbell  *    os[1]
52778cb929SIan Campbell  *    compressed data blocks[N]
53778cb929SIan Campbell  *    crc[4] orig_len[4]
54778cb929SIan Campbell  *
55778cb929SIan Campbell  * resulting in 18 bytes of non compressed data overhead.
56778cb929SIan Campbell  *
57778cb929SIan Campbell  * Files divided into blocks
58778cb929SIan Campbell  * 1 bit (last block flag)
59778cb929SIan Campbell  * 2 bits (block type)
60778cb929SIan Campbell  *
61*1180e01dSIngo Molnar  * 1 block occurs every 32K -1 bytes or when there 50% compression
62*1180e01dSIngo Molnar  * has been achieved. The smallest block type encoding is always used.
63778cb929SIan Campbell  *
64778cb929SIan Campbell  * stored:
65778cb929SIan Campbell  *    32 bits length in bytes.
66778cb929SIan Campbell  *
67778cb929SIan Campbell  * fixed:
68778cb929SIan Campbell  *    magic fixed tree.
69778cb929SIan Campbell  *    symbols.
70778cb929SIan Campbell  *
71778cb929SIan Campbell  * dynamic:
72778cb929SIan Campbell  *    dynamic tree encoding.
73778cb929SIan Campbell  *    symbols.
74778cb929SIan Campbell  *
75778cb929SIan Campbell  *
76778cb929SIan Campbell  * The buffer for decompression in place is the length of the
77778cb929SIan Campbell  * uncompressed data, plus a small amount extra to keep the algorithm safe.
78778cb929SIan Campbell  * The compressed data is placed at the end of the buffer.  The output
79778cb929SIan Campbell  * pointer is placed at the start of the buffer and the input pointer
80778cb929SIan Campbell  * is placed where the compressed data starts.  Problems will occur
81778cb929SIan Campbell  * when the output pointer overruns the input pointer.
82778cb929SIan Campbell  *
83778cb929SIan Campbell  * The output pointer can only overrun the input pointer if the input
84778cb929SIan Campbell  * pointer is moving faster than the output pointer.  A condition only
85778cb929SIan Campbell  * triggered by data whose compressed form is larger than the uncompressed
86778cb929SIan Campbell  * form.
87778cb929SIan Campbell  *
88778cb929SIan Campbell  * The worst case at the block level is a growth of the compressed data
89778cb929SIan Campbell  * of 5 bytes per 32767 bytes.
90778cb929SIan Campbell  *
91778cb929SIan Campbell  * The worst case internal to a compressed block is very hard to figure.
92778cb929SIan Campbell  * The worst case can at least be boundined by having one bit that represents
93778cb929SIan Campbell  * 32764 bytes and then all of the rest of the bytes representing the very
94778cb929SIan Campbell  * very last byte.
95778cb929SIan Campbell  *
96778cb929SIan Campbell  * All of which is enough to compute an amount of extra data that is required
97778cb929SIan Campbell  * to be safe.  To avoid problems at the block level allocating 5 extra bytes
98*1180e01dSIngo Molnar  * per 32767 bytes of data is sufficient.  To avoind problems internal to a
99*1180e01dSIngo Molnar  * block adding an extra 32767 bytes (the worst case uncompressed block size)
100*1180e01dSIngo Molnar  * is sufficient, to ensure that in the worst case the decompressed data for
101778cb929SIan Campbell  * block will stop the byte before the compressed data for a block begins.
102778cb929SIan Campbell  * To avoid problems with the compressed data's meta information an extra 18
103778cb929SIan Campbell  * bytes are needed.  Leading to the formula:
104778cb929SIan Campbell  *
105778cb929SIan Campbell  * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
106778cb929SIan Campbell  *
107778cb929SIan Campbell  * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
108778cb929SIan Campbell  * Adding 32768 instead of 32767 just makes for round numbers.
109778cb929SIan Campbell  * Adding the decompressor_size is necessary as it musht live after all
110778cb929SIan Campbell  * of the data as well.  Last I measured the decompressor is about 14K.
111778cb929SIan Campbell  * 10K of actual data and 4K of bss.
112778cb929SIan Campbell  *
113778cb929SIan Campbell  */
114778cb929SIan Campbell 
115778cb929SIan Campbell /*
116778cb929SIan Campbell  * gzip declarations
117778cb929SIan Campbell  */
118778cb929SIan Campbell 
119778cb929SIan Campbell #define OF(args)	args
120778cb929SIan Campbell #define STATIC		static
121778cb929SIan Campbell 
122778cb929SIan Campbell #undef memset
123778cb929SIan Campbell #undef memcpy
124778cb929SIan Campbell #define memzero(s, n)	memset((s), 0, (n))
125778cb929SIan Campbell 
126778cb929SIan Campbell typedef unsigned char	uch;
127778cb929SIan Campbell typedef unsigned short	ush;
128778cb929SIan Campbell typedef unsigned long	ulg;
129778cb929SIan Campbell 
130*1180e01dSIngo Molnar /*
131*1180e01dSIngo Molnar  * Window size must be at least 32k, and a power of two.
132*1180e01dSIngo Molnar  * We don't actually have a window just a huge output buffer,
133*1180e01dSIngo Molnar  * so we report a 2G window size, as that should always be
134*1180e01dSIngo Molnar  * larger than our output buffer:
135778cb929SIan Campbell  */
136*1180e01dSIngo Molnar #define WSIZE		0x80000000
137778cb929SIan Campbell 
138*1180e01dSIngo Molnar /* Input buffer: */
139*1180e01dSIngo Molnar static unsigned char	*inbuf;
140778cb929SIan Campbell 
141*1180e01dSIngo Molnar /* Sliding window buffer (and final output buffer): */
142*1180e01dSIngo Molnar static unsigned char	*window;
143*1180e01dSIngo Molnar 
144*1180e01dSIngo Molnar /* Valid bytes in inbuf: */
145*1180e01dSIngo Molnar static unsigned		insize;
146*1180e01dSIngo Molnar 
147*1180e01dSIngo Molnar /* Index of next byte to be processed in inbuf: */
148*1180e01dSIngo Molnar static unsigned		inptr;
149*1180e01dSIngo Molnar 
150*1180e01dSIngo Molnar /* Bytes in output buffer: */
151*1180e01dSIngo Molnar static unsigned		outcnt;
152778cb929SIan Campbell 
153778cb929SIan Campbell /* gzip flag byte */
154778cb929SIan Campbell #define ASCII_FLAG	0x01 /* bit 0 set: file probably ASCII text */
155*1180e01dSIngo Molnar #define CONTINUATION	0x02 /* bit 1 set: continuation of multi-part gz file */
156778cb929SIan Campbell #define EXTRA_FIELD	0x04 /* bit 2 set: extra field present */
157*1180e01dSIngo Molnar #define ORIG_NAM	0x08 /* bit 3 set: original file name present */
158778cb929SIan Campbell #define COMMENT		0x10 /* bit 4 set: file comment present */
159778cb929SIan Campbell #define ENCRYPTED	0x20 /* bit 5 set: file is encrypted */
160778cb929SIan Campbell #define RESERVED	0xC0 /* bit 6, 7:  reserved */
161778cb929SIan Campbell 
162778cb929SIan Campbell #define get_byte()	(inptr < insize ? inbuf[inptr++] : fill_inbuf())
163778cb929SIan Campbell 
164778cb929SIan Campbell /* Diagnostic functions */
165778cb929SIan Campbell #ifdef DEBUG
166*1180e01dSIngo Molnar #  define Assert(cond, msg) do { if (!(cond)) error(msg); } while (0)
167*1180e01dSIngo Molnar #  define Trace(x)	do { fprintf x; } while (0)
168*1180e01dSIngo Molnar #  define Tracev(x)	do { if (verbose) fprintf x ; } while (0)
169*1180e01dSIngo Molnar #  define Tracevv(x)	do { if (verbose > 1) fprintf x ; } while (0)
170*1180e01dSIngo Molnar #  define Tracec(c, x)	do { if (verbose && (c)) fprintf x ; } while (0)
171*1180e01dSIngo Molnar #  define Tracecv(c, x)	do { if (verbose > 1 && (c)) fprintf x ; } while (0)
172778cb929SIan Campbell #else
173778cb929SIan Campbell #  define Assert(cond, msg)
174778cb929SIan Campbell #  define Trace(x)
175778cb929SIan Campbell #  define Tracev(x)
176778cb929SIan Campbell #  define Tracevv(x)
177778cb929SIan Campbell #  define Tracec(c, x)
178778cb929SIan Campbell #  define Tracecv(c, x)
179778cb929SIan Campbell #endif
180778cb929SIan Campbell 
181778cb929SIan Campbell static int  fill_inbuf(void);
182778cb929SIan Campbell static void flush_window(void);
183778cb929SIan Campbell static void error(char *m);
184778cb929SIan Campbell static void gzip_mark(void **);
185778cb929SIan Campbell static void gzip_release(void **);
186778cb929SIan Campbell 
187778cb929SIan Campbell /*
188778cb929SIan Campbell  * This is set up by the setup-routine at boot-time
189778cb929SIan Campbell  */
190778cb929SIan Campbell static unsigned char *real_mode; /* Pointer to real-mode data */
191778cb929SIan Campbell 
192778cb929SIan Campbell #define RM_EXT_MEM_K   (*(unsigned short *)(real_mode + 0x2))
193778cb929SIan Campbell #ifndef STANDARD_MEMORY_BIOS_CALL
194778cb929SIan Campbell #define RM_ALT_MEM_K   (*(unsigned long *)(real_mode + 0x1e0))
195778cb929SIan Campbell #endif
196778cb929SIan Campbell #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
197778cb929SIan Campbell 
198778cb929SIan Campbell extern unsigned char input_data[];
199778cb929SIan Campbell extern int input_len;
200778cb929SIan Campbell 
201fd77c7caSPaolo Ciarrocchi static long bytes_out;
202778cb929SIan Campbell 
203778cb929SIan Campbell static void *malloc(int size);
204778cb929SIan Campbell static void free(void *where);
205778cb929SIan Campbell 
206778cb929SIan Campbell static void *memset(void *s, int c, unsigned n);
207778cb929SIan Campbell static void *memcpy(void *dest, const void *src, unsigned n);
208778cb929SIan Campbell 
209778cb929SIan Campbell static void putstr(const char *);
210778cb929SIan Campbell 
211778cb929SIan Campbell #ifdef CONFIG_X86_64
212778cb929SIan Campbell #define memptr long
213778cb929SIan Campbell #else
214778cb929SIan Campbell #define memptr unsigned
215778cb929SIan Campbell #endif
216778cb929SIan Campbell 
217778cb929SIan Campbell static memptr free_mem_ptr;
218778cb929SIan Campbell static memptr free_mem_end_ptr;
219778cb929SIan Campbell 
220778cb929SIan Campbell #ifdef CONFIG_X86_64
221778cb929SIan Campbell #define HEAP_SIZE             0x7000
222778cb929SIan Campbell #else
223778cb929SIan Campbell #define HEAP_SIZE             0x4000
224778cb929SIan Campbell #endif
225778cb929SIan Campbell 
226778cb929SIan Campbell static char *vidmem = (char *)0xb8000;
227778cb929SIan Campbell static int vidport;
228778cb929SIan Campbell static int lines, cols;
229778cb929SIan Campbell 
230778cb929SIan Campbell #ifdef CONFIG_X86_NUMAQ
231778cb929SIan Campbell void *xquad_portio;
232778cb929SIan Campbell #endif
233778cb929SIan Campbell 
234778cb929SIan Campbell #include "../../../../lib/inflate.c"
235778cb929SIan Campbell 
236778cb929SIan Campbell static void *malloc(int size)
237778cb929SIan Campbell {
238778cb929SIan Campbell 	void *p;
239778cb929SIan Campbell 
240fd77c7caSPaolo Ciarrocchi 	if (size < 0)
241fd77c7caSPaolo Ciarrocchi 		error("Malloc error");
242fd77c7caSPaolo Ciarrocchi 	if (free_mem_ptr <= 0)
243fd77c7caSPaolo Ciarrocchi 		error("Memory error");
244778cb929SIan Campbell 
245778cb929SIan Campbell 	free_mem_ptr = (free_mem_ptr + 3) & ~3;	/* Align */
246778cb929SIan Campbell 
247778cb929SIan Campbell 	p = (void *)free_mem_ptr;
248778cb929SIan Campbell 	free_mem_ptr += size;
249778cb929SIan Campbell 
250778cb929SIan Campbell 	if (free_mem_ptr >= free_mem_end_ptr)
251778cb929SIan Campbell 		error("Out of memory");
252778cb929SIan Campbell 
253778cb929SIan Campbell 	return p;
254778cb929SIan Campbell }
255778cb929SIan Campbell 
256778cb929SIan Campbell static void free(void *where)
257778cb929SIan Campbell {	/* Don't care */
258778cb929SIan Campbell }
259778cb929SIan Campbell 
260778cb929SIan Campbell static void gzip_mark(void **ptr)
261778cb929SIan Campbell {
262778cb929SIan Campbell 	*ptr = (void *) free_mem_ptr;
263778cb929SIan Campbell }
264778cb929SIan Campbell 
265778cb929SIan Campbell static void gzip_release(void **ptr)
266778cb929SIan Campbell {
267778cb929SIan Campbell 	free_mem_ptr = (memptr) *ptr;
268778cb929SIan Campbell }
269778cb929SIan Campbell 
270778cb929SIan Campbell static void scroll(void)
271778cb929SIan Campbell {
272778cb929SIan Campbell 	int i;
273778cb929SIan Campbell 
274778cb929SIan Campbell 	memcpy(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
275778cb929SIan Campbell 	for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
276778cb929SIan Campbell 		vidmem[i] = ' ';
277778cb929SIan Campbell }
278778cb929SIan Campbell 
279778cb929SIan Campbell static void putstr(const char *s)
280778cb929SIan Campbell {
281778cb929SIan Campbell 	int x, y, pos;
282778cb929SIan Campbell 	char c;
283778cb929SIan Campbell 
284778cb929SIan Campbell #ifdef CONFIG_X86_32
285778cb929SIan Campbell 	if (RM_SCREEN_INFO.orig_video_mode == 0 && lines == 0 && cols == 0)
286778cb929SIan Campbell 		return;
287778cb929SIan Campbell #endif
288778cb929SIan Campbell 
289778cb929SIan Campbell 	x = RM_SCREEN_INFO.orig_x;
290778cb929SIan Campbell 	y = RM_SCREEN_INFO.orig_y;
291778cb929SIan Campbell 
292778cb929SIan Campbell 	while ((c = *s++) != '\0') {
293778cb929SIan Campbell 		if (c == '\n') {
294778cb929SIan Campbell 			x = 0;
295778cb929SIan Campbell 			if (++y >= lines) {
296778cb929SIan Campbell 				scroll();
297778cb929SIan Campbell 				y--;
298778cb929SIan Campbell 			}
299778cb929SIan Campbell 		} else {
300778cb929SIan Campbell 			vidmem [(x + cols * y) * 2] = c;
301778cb929SIan Campbell 			if (++x >= cols) {
302778cb929SIan Campbell 				x = 0;
303778cb929SIan Campbell 				if (++y >= lines) {
304778cb929SIan Campbell 					scroll();
305778cb929SIan Campbell 					y--;
306778cb929SIan Campbell 				}
307778cb929SIan Campbell 			}
308778cb929SIan Campbell 		}
309778cb929SIan Campbell 	}
310778cb929SIan Campbell 
311778cb929SIan Campbell 	RM_SCREEN_INFO.orig_x = x;
312778cb929SIan Campbell 	RM_SCREEN_INFO.orig_y = y;
313778cb929SIan Campbell 
314778cb929SIan Campbell 	pos = (x + cols * y) * 2;	/* Update cursor position */
315778cb929SIan Campbell 	outb(14, vidport);
316778cb929SIan Campbell 	outb(0xff & (pos >> 9), vidport+1);
317778cb929SIan Campbell 	outb(15, vidport);
318778cb929SIan Campbell 	outb(0xff & (pos >> 1), vidport+1);
319778cb929SIan Campbell }
320778cb929SIan Campbell 
321778cb929SIan Campbell static void *memset(void *s, int c, unsigned n)
322778cb929SIan Campbell {
323778cb929SIan Campbell 	int i;
324778cb929SIan Campbell 	char *ss = s;
325778cb929SIan Campbell 
326778cb929SIan Campbell 	for (i = 0; i < n; i++) ss[i] = c;
327778cb929SIan Campbell 	return s;
328778cb929SIan Campbell }
329778cb929SIan Campbell 
330778cb929SIan Campbell static void *memcpy(void *dest, const void *src, unsigned n)
331778cb929SIan Campbell {
332778cb929SIan Campbell 	int i;
333778cb929SIan Campbell 	const char *s = src;
334778cb929SIan Campbell 	char *d = dest;
335778cb929SIan Campbell 
336778cb929SIan Campbell 	for (i = 0; i < n; i++) d[i] = s[i];
337778cb929SIan Campbell 	return dest;
338778cb929SIan Campbell }
339778cb929SIan Campbell 
340778cb929SIan Campbell /* ===========================================================================
341778cb929SIan Campbell  * Fill the input buffer. This is called only when the buffer is empty
342778cb929SIan Campbell  * and at least one byte is really needed.
343778cb929SIan Campbell  */
344778cb929SIan Campbell static int fill_inbuf(void)
345778cb929SIan Campbell {
346778cb929SIan Campbell 	error("ran out of input data");
347778cb929SIan Campbell 	return 0;
348778cb929SIan Campbell }
349778cb929SIan Campbell 
350778cb929SIan Campbell /* ===========================================================================
351778cb929SIan Campbell  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
352778cb929SIan Campbell  * (Used for the decompressed data only.)
353778cb929SIan Campbell  */
354778cb929SIan Campbell static void flush_window(void)
355778cb929SIan Campbell {
356778cb929SIan Campbell 	/* With my window equal to my output buffer
357778cb929SIan Campbell 	 * I only need to compute the crc here.
358778cb929SIan Campbell 	 */
359*1180e01dSIngo Molnar 	unsigned long c = crc;         /* temporary variable */
360778cb929SIan Campbell 	unsigned n;
361*1180e01dSIngo Molnar 	unsigned char *in, ch;
362778cb929SIan Campbell 
363778cb929SIan Campbell 	in = window;
364778cb929SIan Campbell 	for (n = 0; n < outcnt; n++) {
365778cb929SIan Campbell 		ch = *in++;
366778cb929SIan Campbell 		c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
367778cb929SIan Campbell 	}
368778cb929SIan Campbell 	crc = c;
369*1180e01dSIngo Molnar 	bytes_out += (unsigned long)outcnt;
370778cb929SIan Campbell 	outcnt = 0;
371778cb929SIan Campbell }
372778cb929SIan Campbell 
373778cb929SIan Campbell static void error(char *x)
374778cb929SIan Campbell {
375778cb929SIan Campbell 	putstr("\n\n");
376778cb929SIan Campbell 	putstr(x);
377778cb929SIan Campbell 	putstr("\n\n -- System halted");
378778cb929SIan Campbell 
379778cb929SIan Campbell 	while (1)
380778cb929SIan Campbell 		asm("hlt");
381778cb929SIan Campbell }
382778cb929SIan Campbell 
383099e1377SIan Campbell static void parse_elf(void *output)
384099e1377SIan Campbell {
385099e1377SIan Campbell #ifdef CONFIG_X86_64
386099e1377SIan Campbell 	Elf64_Ehdr ehdr;
387099e1377SIan Campbell 	Elf64_Phdr *phdrs, *phdr;
388099e1377SIan Campbell #else
389099e1377SIan Campbell 	Elf32_Ehdr ehdr;
390099e1377SIan Campbell 	Elf32_Phdr *phdrs, *phdr;
391099e1377SIan Campbell #endif
392099e1377SIan Campbell 	void *dest;
393099e1377SIan Campbell 	int i;
394099e1377SIan Campbell 
395099e1377SIan Campbell 	memcpy(&ehdr, output, sizeof(ehdr));
396099e1377SIan Campbell 	if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
397099e1377SIan Campbell 	   ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
398099e1377SIan Campbell 	   ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
399fd77c7caSPaolo Ciarrocchi 	   ehdr.e_ident[EI_MAG3] != ELFMAG3) {
400099e1377SIan Campbell 		error("Kernel is not a valid ELF file");
401099e1377SIan Campbell 		return;
402099e1377SIan Campbell 	}
403099e1377SIan Campbell 
404099e1377SIan Campbell 	putstr("Parsing ELF... ");
405099e1377SIan Campbell 
406099e1377SIan Campbell 	phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
407099e1377SIan Campbell 	if (!phdrs)
408099e1377SIan Campbell 		error("Failed to allocate space for phdrs");
409099e1377SIan Campbell 
410099e1377SIan Campbell 	memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
411099e1377SIan Campbell 
412099e1377SIan Campbell 	for (i = 0; i < ehdr.e_phnum; i++) {
413099e1377SIan Campbell 		phdr = &phdrs[i];
414099e1377SIan Campbell 
415099e1377SIan Campbell 		switch (phdr->p_type) {
416099e1377SIan Campbell 		case PT_LOAD:
417099e1377SIan Campbell #ifdef CONFIG_RELOCATABLE
418099e1377SIan Campbell 			dest = output;
419099e1377SIan Campbell 			dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
420099e1377SIan Campbell #else
421099e1377SIan Campbell 			dest = (void *)(phdr->p_paddr);
422099e1377SIan Campbell #endif
423099e1377SIan Campbell 			memcpy(dest,
424099e1377SIan Campbell 			       output + phdr->p_offset,
425099e1377SIan Campbell 			       phdr->p_filesz);
426099e1377SIan Campbell 			break;
427099e1377SIan Campbell 		default: /* Ignore other PT_* */ break;
428099e1377SIan Campbell 		}
429099e1377SIan Campbell 	}
430099e1377SIan Campbell }
431099e1377SIan Campbell 
432778cb929SIan Campbell asmlinkage void decompress_kernel(void *rmode, memptr heap,
433*1180e01dSIngo Molnar 				  unsigned char *input_data,
434*1180e01dSIngo Molnar 				  unsigned long input_len,
435*1180e01dSIngo Molnar 				  unsigned char *output)
436778cb929SIan Campbell {
437778cb929SIan Campbell 	real_mode = rmode;
438778cb929SIan Campbell 
439778cb929SIan Campbell 	if (RM_SCREEN_INFO.orig_video_mode == 7) {
440778cb929SIan Campbell 		vidmem = (char *) 0xb0000;
441778cb929SIan Campbell 		vidport = 0x3b4;
442778cb929SIan Campbell 	} else {
443778cb929SIan Campbell 		vidmem = (char *) 0xb8000;
444778cb929SIan Campbell 		vidport = 0x3d4;
445778cb929SIan Campbell 	}
446778cb929SIan Campbell 
447778cb929SIan Campbell 	lines = RM_SCREEN_INFO.orig_video_lines;
448778cb929SIan Campbell 	cols = RM_SCREEN_INFO.orig_video_cols;
449778cb929SIan Campbell 
450778cb929SIan Campbell 	window = output;		/* Output buffer (Normally at 1M) */
451778cb929SIan Campbell 	free_mem_ptr     = heap;	/* Heap */
452778cb929SIan Campbell 	free_mem_end_ptr = heap + HEAP_SIZE;
453778cb929SIan Campbell 	inbuf  = input_data;		/* Input buffer */
454778cb929SIan Campbell 	insize = input_len;
455778cb929SIan Campbell 	inptr  = 0;
456778cb929SIan Campbell 
457778cb929SIan Campbell #ifdef CONFIG_X86_64
458*1180e01dSIngo Molnar 	if ((unsigned long)output & (__KERNEL_ALIGN - 1))
459778cb929SIan Campbell 		error("Destination address not 2M aligned");
460*1180e01dSIngo Molnar 	if ((unsigned long)output >= 0xffffffffffUL)
461778cb929SIan Campbell 		error("Destination address too large");
462778cb929SIan Campbell #else
463778cb929SIan Campbell 	if ((u32)output & (CONFIG_PHYSICAL_ALIGN - 1))
464778cb929SIan Campbell 		error("Destination address not CONFIG_PHYSICAL_ALIGN aligned");
465778cb929SIan Campbell 	if (heap > ((-__PAGE_OFFSET-(512<<20)-1) & 0x7fffffff))
466778cb929SIan Campbell 		error("Destination address too large");
467778cb929SIan Campbell #ifndef CONFIG_RELOCATABLE
468778cb929SIan Campbell 	if ((u32)output != LOAD_PHYSICAL_ADDR)
469778cb929SIan Campbell 		error("Wrong destination address");
470778cb929SIan Campbell #endif
471778cb929SIan Campbell #endif
472778cb929SIan Campbell 
473778cb929SIan Campbell 	makecrc();
474778cb929SIan Campbell 	putstr("\nDecompressing Linux... ");
475778cb929SIan Campbell 	gunzip();
476099e1377SIan Campbell 	parse_elf(output);
477778cb929SIan Campbell 	putstr("done.\nBooting the kernel.\n");
478778cb929SIan Campbell 	return;
479778cb929SIan Campbell }
480