1 // SPDX-License-Identifier: GPL-2.0
5 * This is a collection of several routines used to extract the kernel
6 * which includes KASLR relocation, decompression, ELF parsing, and
7 * relocation processing. Additionally included are the screen and serial
8 * output functions and related debugging support functions.
10 * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
11 * puts by Nick Holloway 1993, better puts by Martin Mares 1995
12 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
17 #include "../string.h"
18 #include "../voffset.h"
22 * This code is compiled with -fPIC and it is relocated dynamically at
23 * run time, but no relocation processing is performed. This means that
24 * it is not safe to place pointers in static structures.
27 /* Macros used by the included decompressor code below. */
31 * Use normal definitions of mem*() from string.c. There are already
32 * included header files which expect a definition of memset() and by
33 * the time we define memset macro, it is too late.
37 #define memzero(s, n) memset((s), 0, (n))
38 #define memmove memmove
40 /* Functions used by the included decompressor code below. */
41 void *memmove(void *dest
, const void *src
, size_t n
);
44 * This is set up by the setup-routine at boot-time
46 struct boot_params
*boot_params
;
49 memptr free_mem_end_ptr
;
53 static int lines
, cols
;
55 #ifdef CONFIG_KERNEL_GZIP
56 #include "../../../../lib/decompress_inflate.c"
59 #ifdef CONFIG_KERNEL_BZIP2
60 #include "../../../../lib/decompress_bunzip2.c"
63 #ifdef CONFIG_KERNEL_LZMA
64 #include "../../../../lib/decompress_unlzma.c"
67 #ifdef CONFIG_KERNEL_XZ
68 #include "../../../../lib/decompress_unxz.c"
71 #ifdef CONFIG_KERNEL_LZO
72 #include "../../../../lib/decompress_unlzo.c"
75 #ifdef CONFIG_KERNEL_LZ4
76 #include "../../../../lib/decompress_unlz4.c"
79 * NOTE: When adding a new decompressor, please update the analysis in
83 static void scroll(void)
87 memmove(vidmem
, vidmem
+ cols
* 2, (lines
- 1) * cols
* 2);
88 for (i
= (lines
- 1) * cols
* 2; i
< lines
* cols
* 2; i
+= 2)
94 #define TXR 0 /* Transmit register (WRITE) */
95 #define LSR 5 /* Line Status */
96 static void serial_putchar(int ch
)
98 unsigned timeout
= 0xffff;
100 while ((inb(early_serial_base
+ LSR
) & XMTRDY
) == 0 && --timeout
)
103 outb(ch
, early_serial_base
+ TXR
);
106 void __putstr(const char *s
)
111 if (early_serial_base
) {
115 serial_putchar('\r');
116 serial_putchar(*str
++);
120 if (lines
== 0 || cols
== 0)
123 x
= boot_params
->screen_info
.orig_x
;
124 y
= boot_params
->screen_info
.orig_y
;
126 while ((c
= *s
++) != '\0') {
134 vidmem
[(x
+ cols
* y
) * 2] = c
;
145 boot_params
->screen_info
.orig_x
= x
;
146 boot_params
->screen_info
.orig_y
= y
;
148 pos
= (x
+ cols
* y
) * 2; /* Update cursor position */
150 outb(0xff & (pos
>> 9), vidport
+1);
152 outb(0xff & (pos
>> 1), vidport
+1);
155 void __puthex(unsigned long value
)
160 for (bits
= sizeof(value
) * 8 - 4; bits
>= 0; bits
-= 4) {
161 unsigned long digit
= (value
>> bits
) & 0xf;
164 alpha
[0] = '0' + digit
;
166 alpha
[0] = 'a' + (digit
- 0xA);
172 static bool l5_supported(void)
174 /* Check if leaf 7 is supported. */
175 if (native_cpuid_eax(0) < 7)
178 /* Check if la57 is supported. */
179 return native_cpuid_ecx(7) & (1 << (X86_FEATURE_LA57
& 31));
182 #if CONFIG_X86_NEED_RELOCS
183 static void handle_relocations(void *output
, unsigned long output_len
,
184 unsigned long virt_addr
)
187 unsigned long delta
, map
, ptr
;
188 unsigned long min_addr
= (unsigned long)output
;
189 unsigned long max_addr
= min_addr
+ (VO___bss_start
- VO__text
);
192 * Calculate the delta between where vmlinux was linked to load
193 * and where it was actually loaded.
195 delta
= min_addr
- LOAD_PHYSICAL_ADDR
;
198 * The kernel contains a table of relocation addresses. Those
199 * addresses have the final load address of the kernel in virtual
200 * memory. We are currently working in the self map. So we need to
201 * create an adjustment for kernel memory addresses to the self map.
202 * This will involve subtracting out the base address of the kernel.
204 map
= delta
- __START_KERNEL_map
;
207 * 32-bit always performs relocations. 64-bit relocations are only
208 * needed if KASLR has chosen a different starting address offset
209 * from __START_KERNEL_map.
211 if (IS_ENABLED(CONFIG_X86_64
))
212 delta
= virt_addr
- LOAD_PHYSICAL_ADDR
;
215 debug_putstr("No relocation needed... ");
218 debug_putstr("Performing relocations... ");
221 * Process relocations: 32 bit relocations first then 64 bit after.
222 * Three sets of binary relocations are added to the end of the kernel
223 * before compression. Each relocation table entry is the kernel
224 * address of the location which needs to be updated stored as a
225 * 32-bit value which is sign extended to 64 bits.
230 * 0 - zero terminator for 64 bit relocations
231 * 64 bit relocation repeated
232 * 0 - zero terminator for inverse 32 bit relocations
233 * 32 bit inverse relocation repeated
234 * 0 - zero terminator for 32 bit relocations
235 * 32 bit relocation repeated
237 * So we work backwards from the end of the decompressed image.
239 for (reloc
= output
+ output_len
- sizeof(*reloc
); *reloc
; reloc
--) {
240 long extended
= *reloc
;
243 ptr
= (unsigned long)extended
;
244 if (ptr
< min_addr
|| ptr
> max_addr
)
245 error("32-bit relocation outside of kernel!\n");
247 *(uint32_t *)ptr
+= delta
;
251 long extended
= *reloc
;
254 ptr
= (unsigned long)extended
;
255 if (ptr
< min_addr
|| ptr
> max_addr
)
256 error("inverse 32-bit relocation outside of kernel!\n");
258 *(int32_t *)ptr
-= delta
;
260 for (reloc
--; *reloc
; reloc
--) {
261 long extended
= *reloc
;
264 ptr
= (unsigned long)extended
;
265 if (ptr
< min_addr
|| ptr
> max_addr
)
266 error("64-bit relocation outside of kernel!\n");
268 *(uint64_t *)ptr
+= delta
;
273 static inline void handle_relocations(void *output
, unsigned long output_len
,
274 unsigned long virt_addr
)
278 static void parse_elf(void *output
)
282 Elf64_Phdr
*phdrs
, *phdr
;
285 Elf32_Phdr
*phdrs
, *phdr
;
290 memcpy(&ehdr
, output
, sizeof(ehdr
));
291 if (ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
||
292 ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
||
293 ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
||
294 ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
) {
295 error("Kernel is not a valid ELF file");
299 debug_putstr("Parsing ELF... ");
301 phdrs
= malloc(sizeof(*phdrs
) * ehdr
.e_phnum
);
303 error("Failed to allocate space for phdrs");
305 memcpy(phdrs
, output
+ ehdr
.e_phoff
, sizeof(*phdrs
) * ehdr
.e_phnum
);
307 for (i
= 0; i
< ehdr
.e_phnum
; i
++) {
310 switch (phdr
->p_type
) {
312 #ifdef CONFIG_RELOCATABLE
314 dest
+= (phdr
->p_paddr
- LOAD_PHYSICAL_ADDR
);
316 dest
= (void *)(phdr
->p_paddr
);
318 memmove(dest
, output
+ phdr
->p_offset
, phdr
->p_filesz
);
320 default: /* Ignore other PT_* */ break;
328 * The compressed kernel image (ZO), has been moved so that its position
329 * is against the end of the buffer used to hold the uncompressed kernel
330 * image (VO) and the execution environment (.bss, .brk), which makes sure
331 * there is room to do the in-place decompression. (See header.S for the
334 * |-----compressed kernel image------|
336 * 0 extract_offset +INIT_SIZE
337 * |-----------|---------------|-------------------------|--------|
339 * VO__text startup_32 of ZO VO__end ZO__end
341 * |-------uncompressed kernel image---------|
344 asmlinkage __visible
void *extract_kernel(void *rmode
, memptr heap
,
345 unsigned char *input_data
,
346 unsigned long input_len
,
347 unsigned char *output
,
348 unsigned long output_len
)
350 const unsigned long kernel_total_size
= VO__end
- VO__text
;
351 unsigned long virt_addr
= LOAD_PHYSICAL_ADDR
;
353 /* Retain x86 boot parameters pointer passed from startup_32/64. */
356 /* Clear flags intended for solely in-kernel use. */
357 boot_params
->hdr
.loadflags
&= ~KASLR_FLAG
;
359 sanitize_boot_params(boot_params
);
361 if (boot_params
->screen_info
.orig_video_mode
== 7) {
362 vidmem
= (char *) 0xb0000;
365 vidmem
= (char *) 0xb8000;
369 lines
= boot_params
->screen_info
.orig_video_lines
;
370 cols
= boot_params
->screen_info
.orig_video_cols
;
373 debug_putstr("early console in extract_kernel\n");
375 if (IS_ENABLED(CONFIG_X86_5LEVEL
) && !l5_supported()) {
376 error("This linux kernel as configured requires 5-level paging\n"
377 "This CPU does not support the required 'cr4.la57' feature\n"
378 "Unable to boot - please use a kernel appropriate for your CPU\n");
381 free_mem_ptr
= heap
; /* Heap */
382 free_mem_end_ptr
= heap
+ BOOT_HEAP_SIZE
;
384 /* Report initial kernel position details. */
385 debug_putaddr(input_data
);
386 debug_putaddr(input_len
);
387 debug_putaddr(output
);
388 debug_putaddr(output_len
);
389 debug_putaddr(kernel_total_size
);
392 * The memory hole needed for the kernel is the larger of either
393 * the entire decompressed kernel plus relocation table, or the
394 * entire decompressed kernel plus .bss and .brk sections.
396 choose_random_location((unsigned long)input_data
, input_len
,
397 (unsigned long *)&output
,
398 max(output_len
, kernel_total_size
),
401 /* Validate memory location choices. */
402 if ((unsigned long)output
& (MIN_KERNEL_ALIGN
- 1))
403 error("Destination physical address inappropriately aligned");
404 if (virt_addr
& (MIN_KERNEL_ALIGN
- 1))
405 error("Destination virtual address inappropriately aligned");
407 if (heap
> 0x3fffffffffffUL
)
408 error("Destination address too large");
409 if (virt_addr
+ max(output_len
, kernel_total_size
) > KERNEL_IMAGE_SIZE
)
410 error("Destination virtual address is beyond the kernel mapping area");
412 if (heap
> ((-__PAGE_OFFSET
-(128<<20)-1) & 0x7fffffff))
413 error("Destination address too large");
415 #ifndef CONFIG_RELOCATABLE
416 if ((unsigned long)output
!= LOAD_PHYSICAL_ADDR
)
417 error("Destination address does not match LOAD_PHYSICAL_ADDR");
418 if (virt_addr
!= LOAD_PHYSICAL_ADDR
)
419 error("Destination virtual address changed when not relocatable");
422 debug_putstr("\nDecompressing Linux... ");
423 __decompress(input_data
, input_len
, NULL
, NULL
, output
, output_len
,
426 handle_relocations(output
, output_len
, virt_addr
);
427 debug_putstr("done.\nBooting the kernel.\n");
431 void fortify_panic(const char *name
)
433 error("detected buffer overflow");