xref: /OK3568_Linux_fs/kernel/arch/x86/boot/compressed/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 used to extract the kernel
6*4882a593Smuzhiyun  * which includes KASLR relocation, decompression, ELF parsing, and
7*4882a593Smuzhiyun  * relocation processing. Additionally included are the screen and serial
8*4882a593Smuzhiyun  * output functions and related debugging support functions.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
11*4882a593Smuzhiyun  * puts by Nick Holloway 1993, better puts by Martin Mares 1995
12*4882a593Smuzhiyun  * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include "misc.h"
16*4882a593Smuzhiyun #include "error.h"
17*4882a593Smuzhiyun #include "pgtable.h"
18*4882a593Smuzhiyun #include "../string.h"
19*4882a593Smuzhiyun #include "../voffset.h"
20*4882a593Smuzhiyun #include <asm/bootparam_utils.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun  * WARNING!!
24*4882a593Smuzhiyun  * This code is compiled with -fPIC and it is relocated dynamically at
25*4882a593Smuzhiyun  * run time, but no relocation processing is performed. This means that
26*4882a593Smuzhiyun  * it is not safe to place pointers in static structures.
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /* Macros used by the included decompressor code below. */
30*4882a593Smuzhiyun #define STATIC		static
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun  * Provide definitions of memzero and memmove as some of the decompressors will
34*4882a593Smuzhiyun  * try to define their own functions if these are not defined as macros.
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun #define memzero(s, n)	memset((s), 0, (n))
37*4882a593Smuzhiyun #define memmove		memmove
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /* Functions used by the included decompressor code below. */
40*4882a593Smuzhiyun void *memmove(void *dest, const void *src, size_t n);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun  * This is set up by the setup-routine at boot-time
44*4882a593Smuzhiyun  */
45*4882a593Smuzhiyun struct boot_params *boot_params;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun memptr free_mem_ptr;
48*4882a593Smuzhiyun memptr free_mem_end_ptr;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun static char *vidmem;
51*4882a593Smuzhiyun static int vidport;
52*4882a593Smuzhiyun static int lines, cols;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #ifdef CONFIG_KERNEL_GZIP
55*4882a593Smuzhiyun #include "../../../../lib/decompress_inflate.c"
56*4882a593Smuzhiyun #endif
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #ifdef CONFIG_KERNEL_BZIP2
59*4882a593Smuzhiyun #include "../../../../lib/decompress_bunzip2.c"
60*4882a593Smuzhiyun #endif
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #ifdef CONFIG_KERNEL_LZMA
63*4882a593Smuzhiyun #include "../../../../lib/decompress_unlzma.c"
64*4882a593Smuzhiyun #endif
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun #ifdef CONFIG_KERNEL_XZ
67*4882a593Smuzhiyun #include "../../../../lib/decompress_unxz.c"
68*4882a593Smuzhiyun #endif
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #ifdef CONFIG_KERNEL_LZO
71*4882a593Smuzhiyun #include "../../../../lib/decompress_unlzo.c"
72*4882a593Smuzhiyun #endif
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun #ifdef CONFIG_KERNEL_LZ4
75*4882a593Smuzhiyun #include "../../../../lib/decompress_unlz4.c"
76*4882a593Smuzhiyun #endif
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun #ifdef CONFIG_KERNEL_ZSTD
79*4882a593Smuzhiyun #include "../../../../lib/decompress_unzstd.c"
80*4882a593Smuzhiyun #endif
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun  * NOTE: When adding a new decompressor, please update the analysis in
83*4882a593Smuzhiyun  * ../header.S.
84*4882a593Smuzhiyun  */
85*4882a593Smuzhiyun 
scroll(void)86*4882a593Smuzhiyun static void scroll(void)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	int i;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	memmove(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
91*4882a593Smuzhiyun 	for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
92*4882a593Smuzhiyun 		vidmem[i] = ' ';
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun #define XMTRDY          0x20
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #define TXR             0       /*  Transmit register (WRITE) */
98*4882a593Smuzhiyun #define LSR             5       /*  Line Status               */
serial_putchar(int ch)99*4882a593Smuzhiyun static void serial_putchar(int ch)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	unsigned timeout = 0xffff;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
104*4882a593Smuzhiyun 		cpu_relax();
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	outb(ch, early_serial_base + TXR);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
__putstr(const char * s)109*4882a593Smuzhiyun void __putstr(const char *s)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	int x, y, pos;
112*4882a593Smuzhiyun 	char c;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	if (early_serial_base) {
115*4882a593Smuzhiyun 		const char *str = s;
116*4882a593Smuzhiyun 		while (*str) {
117*4882a593Smuzhiyun 			if (*str == '\n')
118*4882a593Smuzhiyun 				serial_putchar('\r');
119*4882a593Smuzhiyun 			serial_putchar(*str++);
120*4882a593Smuzhiyun 		}
121*4882a593Smuzhiyun 	}
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	if (lines == 0 || cols == 0)
124*4882a593Smuzhiyun 		return;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	x = boot_params->screen_info.orig_x;
127*4882a593Smuzhiyun 	y = boot_params->screen_info.orig_y;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	while ((c = *s++) != '\0') {
130*4882a593Smuzhiyun 		if (c == '\n') {
131*4882a593Smuzhiyun 			x = 0;
132*4882a593Smuzhiyun 			if (++y >= lines) {
133*4882a593Smuzhiyun 				scroll();
134*4882a593Smuzhiyun 				y--;
135*4882a593Smuzhiyun 			}
136*4882a593Smuzhiyun 		} else {
137*4882a593Smuzhiyun 			vidmem[(x + cols * y) * 2] = c;
138*4882a593Smuzhiyun 			if (++x >= cols) {
139*4882a593Smuzhiyun 				x = 0;
140*4882a593Smuzhiyun 				if (++y >= lines) {
141*4882a593Smuzhiyun 					scroll();
142*4882a593Smuzhiyun 					y--;
143*4882a593Smuzhiyun 				}
144*4882a593Smuzhiyun 			}
145*4882a593Smuzhiyun 		}
146*4882a593Smuzhiyun 	}
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	boot_params->screen_info.orig_x = x;
149*4882a593Smuzhiyun 	boot_params->screen_info.orig_y = y;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	pos = (x + cols * y) * 2;	/* Update cursor position */
152*4882a593Smuzhiyun 	outb(14, vidport);
153*4882a593Smuzhiyun 	outb(0xff & (pos >> 9), vidport+1);
154*4882a593Smuzhiyun 	outb(15, vidport);
155*4882a593Smuzhiyun 	outb(0xff & (pos >> 1), vidport+1);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
__puthex(unsigned long value)158*4882a593Smuzhiyun void __puthex(unsigned long value)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	char alpha[2] = "0";
161*4882a593Smuzhiyun 	int bits;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	for (bits = sizeof(value) * 8 - 4; bits >= 0; bits -= 4) {
164*4882a593Smuzhiyun 		unsigned long digit = (value >> bits) & 0xf;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 		if (digit < 0xA)
167*4882a593Smuzhiyun 			alpha[0] = '0' + digit;
168*4882a593Smuzhiyun 		else
169*4882a593Smuzhiyun 			alpha[0] = 'a' + (digit - 0xA);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 		__putstr(alpha);
172*4882a593Smuzhiyun 	}
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun #if CONFIG_X86_NEED_RELOCS
handle_relocations(void * output,unsigned long output_len,unsigned long virt_addr)176*4882a593Smuzhiyun static void handle_relocations(void *output, unsigned long output_len,
177*4882a593Smuzhiyun 			       unsigned long virt_addr)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun 	int *reloc;
180*4882a593Smuzhiyun 	unsigned long delta, map, ptr;
181*4882a593Smuzhiyun 	unsigned long min_addr = (unsigned long)output;
182*4882a593Smuzhiyun 	unsigned long max_addr = min_addr + (VO___bss_start - VO__text);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	/*
185*4882a593Smuzhiyun 	 * Calculate the delta between where vmlinux was linked to load
186*4882a593Smuzhiyun 	 * and where it was actually loaded.
187*4882a593Smuzhiyun 	 */
188*4882a593Smuzhiyun 	delta = min_addr - LOAD_PHYSICAL_ADDR;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	/*
191*4882a593Smuzhiyun 	 * The kernel contains a table of relocation addresses. Those
192*4882a593Smuzhiyun 	 * addresses have the final load address of the kernel in virtual
193*4882a593Smuzhiyun 	 * memory. We are currently working in the self map. So we need to
194*4882a593Smuzhiyun 	 * create an adjustment for kernel memory addresses to the self map.
195*4882a593Smuzhiyun 	 * This will involve subtracting out the base address of the kernel.
196*4882a593Smuzhiyun 	 */
197*4882a593Smuzhiyun 	map = delta - __START_KERNEL_map;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	/*
200*4882a593Smuzhiyun 	 * 32-bit always performs relocations. 64-bit relocations are only
201*4882a593Smuzhiyun 	 * needed if KASLR has chosen a different starting address offset
202*4882a593Smuzhiyun 	 * from __START_KERNEL_map.
203*4882a593Smuzhiyun 	 */
204*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_X86_64))
205*4882a593Smuzhiyun 		delta = virt_addr - LOAD_PHYSICAL_ADDR;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	if (!delta) {
208*4882a593Smuzhiyun 		debug_putstr("No relocation needed... ");
209*4882a593Smuzhiyun 		return;
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun 	debug_putstr("Performing relocations... ");
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	/*
214*4882a593Smuzhiyun 	 * Process relocations: 32 bit relocations first then 64 bit after.
215*4882a593Smuzhiyun 	 * Three sets of binary relocations are added to the end of the kernel
216*4882a593Smuzhiyun 	 * before compression. Each relocation table entry is the kernel
217*4882a593Smuzhiyun 	 * address of the location which needs to be updated stored as a
218*4882a593Smuzhiyun 	 * 32-bit value which is sign extended to 64 bits.
219*4882a593Smuzhiyun 	 *
220*4882a593Smuzhiyun 	 * Format is:
221*4882a593Smuzhiyun 	 *
222*4882a593Smuzhiyun 	 * kernel bits...
223*4882a593Smuzhiyun 	 * 0 - zero terminator for 64 bit relocations
224*4882a593Smuzhiyun 	 * 64 bit relocation repeated
225*4882a593Smuzhiyun 	 * 0 - zero terminator for inverse 32 bit relocations
226*4882a593Smuzhiyun 	 * 32 bit inverse relocation repeated
227*4882a593Smuzhiyun 	 * 0 - zero terminator for 32 bit relocations
228*4882a593Smuzhiyun 	 * 32 bit relocation repeated
229*4882a593Smuzhiyun 	 *
230*4882a593Smuzhiyun 	 * So we work backwards from the end of the decompressed image.
231*4882a593Smuzhiyun 	 */
232*4882a593Smuzhiyun 	for (reloc = output + output_len - sizeof(*reloc); *reloc; reloc--) {
233*4882a593Smuzhiyun 		long extended = *reloc;
234*4882a593Smuzhiyun 		extended += map;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 		ptr = (unsigned long)extended;
237*4882a593Smuzhiyun 		if (ptr < min_addr || ptr > max_addr)
238*4882a593Smuzhiyun 			error("32-bit relocation outside of kernel!\n");
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 		*(uint32_t *)ptr += delta;
241*4882a593Smuzhiyun 	}
242*4882a593Smuzhiyun #ifdef CONFIG_X86_64
243*4882a593Smuzhiyun 	while (*--reloc) {
244*4882a593Smuzhiyun 		long extended = *reloc;
245*4882a593Smuzhiyun 		extended += map;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 		ptr = (unsigned long)extended;
248*4882a593Smuzhiyun 		if (ptr < min_addr || ptr > max_addr)
249*4882a593Smuzhiyun 			error("inverse 32-bit relocation outside of kernel!\n");
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 		*(int32_t *)ptr -= delta;
252*4882a593Smuzhiyun 	}
253*4882a593Smuzhiyun 	for (reloc--; *reloc; reloc--) {
254*4882a593Smuzhiyun 		long extended = *reloc;
255*4882a593Smuzhiyun 		extended += map;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 		ptr = (unsigned long)extended;
258*4882a593Smuzhiyun 		if (ptr < min_addr || ptr > max_addr)
259*4882a593Smuzhiyun 			error("64-bit relocation outside of kernel!\n");
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 		*(uint64_t *)ptr += delta;
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun #endif
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun #else
handle_relocations(void * output,unsigned long output_len,unsigned long virt_addr)266*4882a593Smuzhiyun static inline void handle_relocations(void *output, unsigned long output_len,
267*4882a593Smuzhiyun 				      unsigned long virt_addr)
268*4882a593Smuzhiyun { }
269*4882a593Smuzhiyun #endif
270*4882a593Smuzhiyun 
parse_elf(void * output)271*4882a593Smuzhiyun static void parse_elf(void *output)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun #ifdef CONFIG_X86_64
274*4882a593Smuzhiyun 	Elf64_Ehdr ehdr;
275*4882a593Smuzhiyun 	Elf64_Phdr *phdrs, *phdr;
276*4882a593Smuzhiyun #else
277*4882a593Smuzhiyun 	Elf32_Ehdr ehdr;
278*4882a593Smuzhiyun 	Elf32_Phdr *phdrs, *phdr;
279*4882a593Smuzhiyun #endif
280*4882a593Smuzhiyun 	void *dest;
281*4882a593Smuzhiyun 	int i;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	memcpy(&ehdr, output, sizeof(ehdr));
284*4882a593Smuzhiyun 	if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
285*4882a593Smuzhiyun 	   ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
286*4882a593Smuzhiyun 	   ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
287*4882a593Smuzhiyun 	   ehdr.e_ident[EI_MAG3] != ELFMAG3) {
288*4882a593Smuzhiyun 		error("Kernel is not a valid ELF file");
289*4882a593Smuzhiyun 		return;
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	debug_putstr("Parsing ELF... ");
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
295*4882a593Smuzhiyun 	if (!phdrs)
296*4882a593Smuzhiyun 		error("Failed to allocate space for phdrs");
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	for (i = 0; i < ehdr.e_phnum; i++) {
301*4882a593Smuzhiyun 		phdr = &phdrs[i];
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 		switch (phdr->p_type) {
304*4882a593Smuzhiyun 		case PT_LOAD:
305*4882a593Smuzhiyun #ifdef CONFIG_X86_64
306*4882a593Smuzhiyun 			if ((phdr->p_align % 0x200000) != 0)
307*4882a593Smuzhiyun 				error("Alignment of LOAD segment isn't multiple of 2MB");
308*4882a593Smuzhiyun #endif
309*4882a593Smuzhiyun #ifdef CONFIG_RELOCATABLE
310*4882a593Smuzhiyun 			dest = output;
311*4882a593Smuzhiyun 			dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
312*4882a593Smuzhiyun #else
313*4882a593Smuzhiyun 			dest = (void *)(phdr->p_paddr);
314*4882a593Smuzhiyun #endif
315*4882a593Smuzhiyun 			memmove(dest, output + phdr->p_offset, phdr->p_filesz);
316*4882a593Smuzhiyun 			break;
317*4882a593Smuzhiyun 		default: /* Ignore other PT_* */ break;
318*4882a593Smuzhiyun 		}
319*4882a593Smuzhiyun 	}
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	free(phdrs);
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun /*
325*4882a593Smuzhiyun  * The compressed kernel image (ZO), has been moved so that its position
326*4882a593Smuzhiyun  * is against the end of the buffer used to hold the uncompressed kernel
327*4882a593Smuzhiyun  * image (VO) and the execution environment (.bss, .brk), which makes sure
328*4882a593Smuzhiyun  * there is room to do the in-place decompression. (See header.S for the
329*4882a593Smuzhiyun  * calculations.)
330*4882a593Smuzhiyun  *
331*4882a593Smuzhiyun  *                             |-----compressed kernel image------|
332*4882a593Smuzhiyun  *                             V                                  V
333*4882a593Smuzhiyun  * 0                       extract_offset                      +INIT_SIZE
334*4882a593Smuzhiyun  * |-----------|---------------|-------------------------|--------|
335*4882a593Smuzhiyun  *             |               |                         |        |
336*4882a593Smuzhiyun  *           VO__text      startup_32 of ZO          VO__end    ZO__end
337*4882a593Smuzhiyun  *             ^                                         ^
338*4882a593Smuzhiyun  *             |-------uncompressed kernel image---------|
339*4882a593Smuzhiyun  *
340*4882a593Smuzhiyun  */
extract_kernel(void * rmode,memptr heap,unsigned char * input_data,unsigned long input_len,unsigned char * output,unsigned long output_len)341*4882a593Smuzhiyun asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
342*4882a593Smuzhiyun 				  unsigned char *input_data,
343*4882a593Smuzhiyun 				  unsigned long input_len,
344*4882a593Smuzhiyun 				  unsigned char *output,
345*4882a593Smuzhiyun 				  unsigned long output_len)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	const unsigned long kernel_total_size = VO__end - VO__text;
348*4882a593Smuzhiyun 	unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
349*4882a593Smuzhiyun 	unsigned long needed_size;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	/* Retain x86 boot parameters pointer passed from startup_32/64. */
352*4882a593Smuzhiyun 	boot_params = rmode;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	/* Clear flags intended for solely in-kernel use. */
355*4882a593Smuzhiyun 	boot_params->hdr.loadflags &= ~KASLR_FLAG;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	sanitize_boot_params(boot_params);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	if (boot_params->screen_info.orig_video_mode == 7) {
360*4882a593Smuzhiyun 		vidmem = (char *) 0xb0000;
361*4882a593Smuzhiyun 		vidport = 0x3b4;
362*4882a593Smuzhiyun 	} else {
363*4882a593Smuzhiyun 		vidmem = (char *) 0xb8000;
364*4882a593Smuzhiyun 		vidport = 0x3d4;
365*4882a593Smuzhiyun 	}
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	lines = boot_params->screen_info.orig_video_lines;
368*4882a593Smuzhiyun 	cols = boot_params->screen_info.orig_video_cols;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	console_init();
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	/*
373*4882a593Smuzhiyun 	 * Save RSDP address for later use. Have this after console_init()
374*4882a593Smuzhiyun 	 * so that early debugging output from the RSDP parsing code can be
375*4882a593Smuzhiyun 	 * collected.
376*4882a593Smuzhiyun 	 */
377*4882a593Smuzhiyun 	boot_params->acpi_rsdp_addr = get_rsdp_addr();
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	debug_putstr("early console in extract_kernel\n");
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	free_mem_ptr     = heap;	/* Heap */
382*4882a593Smuzhiyun 	free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	/*
385*4882a593Smuzhiyun 	 * The memory hole needed for the kernel is the larger of either
386*4882a593Smuzhiyun 	 * the entire decompressed kernel plus relocation table, or the
387*4882a593Smuzhiyun 	 * entire decompressed kernel plus .bss and .brk sections.
388*4882a593Smuzhiyun 	 *
389*4882a593Smuzhiyun 	 * On X86_64, the memory is mapped with PMD pages. Round the
390*4882a593Smuzhiyun 	 * size up so that the full extent of PMD pages mapped is
391*4882a593Smuzhiyun 	 * included in the check against the valid memory table
392*4882a593Smuzhiyun 	 * entries. This ensures the full mapped area is usable RAM
393*4882a593Smuzhiyun 	 * and doesn't include any reserved areas.
394*4882a593Smuzhiyun 	 */
395*4882a593Smuzhiyun 	needed_size = max(output_len, kernel_total_size);
396*4882a593Smuzhiyun #ifdef CONFIG_X86_64
397*4882a593Smuzhiyun 	needed_size = ALIGN(needed_size, MIN_KERNEL_ALIGN);
398*4882a593Smuzhiyun #endif
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	/* Report initial kernel position details. */
401*4882a593Smuzhiyun 	debug_putaddr(input_data);
402*4882a593Smuzhiyun 	debug_putaddr(input_len);
403*4882a593Smuzhiyun 	debug_putaddr(output);
404*4882a593Smuzhiyun 	debug_putaddr(output_len);
405*4882a593Smuzhiyun 	debug_putaddr(kernel_total_size);
406*4882a593Smuzhiyun 	debug_putaddr(needed_size);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun #ifdef CONFIG_X86_64
409*4882a593Smuzhiyun 	/* Report address of 32-bit trampoline */
410*4882a593Smuzhiyun 	debug_putaddr(trampoline_32bit);
411*4882a593Smuzhiyun #endif
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	choose_random_location((unsigned long)input_data, input_len,
414*4882a593Smuzhiyun 				(unsigned long *)&output,
415*4882a593Smuzhiyun 				needed_size,
416*4882a593Smuzhiyun 				&virt_addr);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	/* Validate memory location choices. */
419*4882a593Smuzhiyun 	if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
420*4882a593Smuzhiyun 		error("Destination physical address inappropriately aligned");
421*4882a593Smuzhiyun 	if (virt_addr & (MIN_KERNEL_ALIGN - 1))
422*4882a593Smuzhiyun 		error("Destination virtual address inappropriately aligned");
423*4882a593Smuzhiyun #ifdef CONFIG_X86_64
424*4882a593Smuzhiyun 	if (heap > 0x3fffffffffffUL)
425*4882a593Smuzhiyun 		error("Destination address too large");
426*4882a593Smuzhiyun 	if (virt_addr + max(output_len, kernel_total_size) > KERNEL_IMAGE_SIZE)
427*4882a593Smuzhiyun 		error("Destination virtual address is beyond the kernel mapping area");
428*4882a593Smuzhiyun #else
429*4882a593Smuzhiyun 	if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
430*4882a593Smuzhiyun 		error("Destination address too large");
431*4882a593Smuzhiyun #endif
432*4882a593Smuzhiyun #ifndef CONFIG_RELOCATABLE
433*4882a593Smuzhiyun 	if ((unsigned long)output != LOAD_PHYSICAL_ADDR)
434*4882a593Smuzhiyun 		error("Destination address does not match LOAD_PHYSICAL_ADDR");
435*4882a593Smuzhiyun 	if (virt_addr != LOAD_PHYSICAL_ADDR)
436*4882a593Smuzhiyun 		error("Destination virtual address changed when not relocatable");
437*4882a593Smuzhiyun #endif
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	debug_putstr("\nDecompressing Linux... ");
440*4882a593Smuzhiyun 	__decompress(input_data, input_len, NULL, NULL, output, output_len,
441*4882a593Smuzhiyun 			NULL, error);
442*4882a593Smuzhiyun 	parse_elf(output);
443*4882a593Smuzhiyun 	handle_relocations(output, output_len, virt_addr);
444*4882a593Smuzhiyun 	debug_putstr("done.\nBooting the kernel.\n");
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	/*
447*4882a593Smuzhiyun 	 * Flush GHCB from cache and map it encrypted again when running as
448*4882a593Smuzhiyun 	 * SEV-ES guest.
449*4882a593Smuzhiyun 	 */
450*4882a593Smuzhiyun 	sev_es_shutdown_ghcb();
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	return output;
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun 
fortify_panic(const char * name)455*4882a593Smuzhiyun void fortify_panic(const char *name)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	error("detected buffer overflow");
458*4882a593Smuzhiyun }
459