4 * This is a collection of several routines used to extract the kernel
5 * which includes KASLR relocation, decompression, ELF parsing, and
6 * relocation processing. Additionally included are the screen and serial
7 * output functions and related debugging support functions.
9 * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
10 * puts by Nick Holloway 1993, better puts by Martin Mares 1995
11 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
15 #include "../string.h"
19 * This code is compiled with -fPIC and it is relocated dynamically at
20 * run time, but no relocation processing is performed. This means that
21 * it is not safe to place pointers in static structures.
24 /* Macros used by the included decompressor code below. */
28 * Use normal definitions of mem*() from string.c. There are already
29 * included header files which expect a definition of memset() and by
30 * the time we define memset macro, it is too late.
34 #define memzero(s, n) memset((s), 0, (n))
36 /* Functions used by the included decompressor code below. */
37 static void error(char *m);
40 * This is set up by the setup-routine at boot-time
42 struct boot_params *boot_params;
45 memptr free_mem_end_ptr;
49 static int lines, cols;
51 #ifdef CONFIG_KERNEL_GZIP
52 #include "../../../../lib/decompress_inflate.c"
55 #ifdef CONFIG_KERNEL_BZIP2
56 #include "../../../../lib/decompress_bunzip2.c"
59 #ifdef CONFIG_KERNEL_LZMA
60 #include "../../../../lib/decompress_unlzma.c"
63 #ifdef CONFIG_KERNEL_XZ
64 #include "../../../../lib/decompress_unxz.c"
67 #ifdef CONFIG_KERNEL_LZO
68 #include "../../../../lib/decompress_unlzo.c"
71 #ifdef CONFIG_KERNEL_LZ4
72 #include "../../../../lib/decompress_unlz4.c"
75 * NOTE: When adding a new decompressor, please update the analysis in
79 static void scroll(void)
83 memcpy(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
84 for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
90 #define TXR 0 /* Transmit register (WRITE) */
91 #define LSR 5 /* Line Status */
92 static void serial_putchar(int ch)
94 unsigned timeout = 0xffff;
96 while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
99 outb(ch, early_serial_base + TXR);
102 void __putstr(const char *s)
107 if (early_serial_base) {
111 serial_putchar('\r');
112 serial_putchar(*str++);
116 if (boot_params->screen_info.orig_video_mode == 0 &&
117 lines == 0 && cols == 0)
120 x = boot_params->screen_info.orig_x;
121 y = boot_params->screen_info.orig_y;
123 while ((c = *s++) != '\0') {
131 vidmem[(x + cols * y) * 2] = c;
142 boot_params->screen_info.orig_x = x;
143 boot_params->screen_info.orig_y = y;
145 pos = (x + cols * y) * 2; /* Update cursor position */
147 outb(0xff & (pos >> 9), vidport+1);
149 outb(0xff & (pos >> 1), vidport+1);
152 void __puthex(unsigned long value)
157 for (bits = sizeof(value) * 8 - 4; bits >= 0; bits -= 4) {
158 unsigned long digit = (value >> bits) & 0xf;
161 alpha[0] = '0' + digit;
163 alpha[0] = 'a' + (digit - 0xA);
171 error_putstr("\n\n");
173 error_putstr("\n\n");
176 static void error(char *m)
179 error_putstr(" -- System halted");
185 #if CONFIG_X86_NEED_RELOCS
186 static void handle_relocations(void *output, unsigned long output_len)
189 unsigned long delta, map, ptr;
190 unsigned long min_addr = (unsigned long)output;
191 unsigned long max_addr = min_addr + output_len;
194 * Calculate the delta between where vmlinux was linked to load
195 * and where it was actually loaded.
197 delta = min_addr - LOAD_PHYSICAL_ADDR;
199 debug_putstr("No relocation needed... ");
202 debug_putstr("Performing relocations... ");
205 * The kernel contains a table of relocation addresses. Those
206 * addresses have the final load address of the kernel in virtual
207 * memory. We are currently working in the self map. So we need to
208 * create an adjustment for kernel memory addresses to the self map.
209 * This will involve subtracting out the base address of the kernel.
211 map = delta - __START_KERNEL_map;
214 * Process relocations: 32 bit relocations first then 64 bit after.
215 * Three sets of binary relocations are added to the end of the kernel
216 * before compression. Each relocation table entry is the kernel
217 * address of the location which needs to be updated stored as a
218 * 32-bit value which is sign extended to 64 bits.
223 * 0 - zero terminator for 64 bit relocations
224 * 64 bit relocation repeated
225 * 0 - zero terminator for inverse 32 bit relocations
226 * 32 bit inverse relocation repeated
227 * 0 - zero terminator for 32 bit relocations
228 * 32 bit relocation repeated
230 * So we work backwards from the end of the decompressed image.
232 for (reloc = output + output_len - sizeof(*reloc); *reloc; reloc--) {
233 int extended = *reloc;
236 ptr = (unsigned long)extended;
237 if (ptr < min_addr || ptr > max_addr)
238 error("32-bit relocation outside of kernel!\n");
240 *(uint32_t *)ptr += delta;
244 long extended = *reloc;
247 ptr = (unsigned long)extended;
248 if (ptr < min_addr || ptr > max_addr)
249 error("inverse 32-bit relocation outside of kernel!\n");
251 *(int32_t *)ptr -= delta;
253 for (reloc--; *reloc; reloc--) {
254 long extended = *reloc;
257 ptr = (unsigned long)extended;
258 if (ptr < min_addr || ptr > max_addr)
259 error("64-bit relocation outside of kernel!\n");
261 *(uint64_t *)ptr += delta;
266 static inline void handle_relocations(void *output, unsigned long output_len)
270 static void parse_elf(void *output)
274 Elf64_Phdr *phdrs, *phdr;
277 Elf32_Phdr *phdrs, *phdr;
282 memcpy(&ehdr, output, sizeof(ehdr));
283 if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
284 ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
285 ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
286 ehdr.e_ident[EI_MAG3] != ELFMAG3) {
287 error("Kernel is not a valid ELF file");
291 debug_putstr("Parsing ELF... ");
293 phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
295 error("Failed to allocate space for phdrs");
297 memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
299 for (i = 0; i < ehdr.e_phnum; i++) {
302 switch (phdr->p_type) {
304 #ifdef CONFIG_RELOCATABLE
306 dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
308 dest = (void *)(phdr->p_paddr);
310 memcpy(dest, output + phdr->p_offset, phdr->p_filesz);
312 default: /* Ignore other PT_* */ break;
319 asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
320 unsigned char *input_data,
321 unsigned long input_len,
322 unsigned char *output,
323 unsigned long output_len,
324 unsigned long run_size)
326 unsigned char *output_orig = output;
328 /* Retain x86 boot parameters pointer passed from startup_32/64. */
331 /* Clear flags intended for solely in-kernel use. */
332 boot_params->hdr.loadflags &= ~KASLR_FLAG;
334 sanitize_boot_params(boot_params);
336 if (boot_params->screen_info.orig_video_mode == 7) {
337 vidmem = (char *) 0xb0000;
340 vidmem = (char *) 0xb8000;
344 lines = boot_params->screen_info.orig_video_lines;
345 cols = boot_params->screen_info.orig_video_cols;
348 debug_putstr("early console in extract_kernel\n");
350 free_mem_ptr = heap; /* Heap */
351 free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
353 /* Report initial kernel position details. */
354 debug_putaddr(input_data);
355 debug_putaddr(input_len);
356 debug_putaddr(output);
357 debug_putaddr(output_len);
358 debug_putaddr(run_size);
361 * The memory hole needed for the kernel is the larger of either
362 * the entire decompressed kernel plus relocation table, or the
363 * entire decompressed kernel plus .bss and .brk sections.
365 output = choose_random_location(input_data, input_len, output,
366 output_len > run_size ? output_len
369 /* Validate memory location choices. */
370 if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
371 error("Destination address inappropriately aligned");
373 if (heap > 0x3fffffffffffUL)
374 error("Destination address too large");
376 if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
377 error("Destination address too large");
379 #ifndef CONFIG_RELOCATABLE
380 if ((unsigned long)output != LOAD_PHYSICAL_ADDR)
381 error("Wrong destination address");
384 debug_putstr("\nDecompressing Linux... ");
385 __decompress(input_data, input_len, NULL, NULL, output, output_len,
389 * 32-bit always performs relocations. 64-bit relocations are only
390 * needed if kASLR has chosen a different load address.
392 if (!IS_ENABLED(CONFIG_X86_64) || output != output_orig)
393 handle_relocations(output, output_len);
394 debug_putstr("done.\nBooting the kernel.\n");