| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * misc.c |
| Ian Campbell | 818a08f | 2008-01-30 13:33:38 +0100 | [diff] [blame] | 3 | * |
| Kees Cook | c040288 | 2016-04-18 09:42:13 -0700 | [diff] [blame] | 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. |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 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 |
| 12 | */ |
| 13 | |
| Yinghai Lu | 8fee13a4 | 2010-08-02 16:21:22 -0700 | [diff] [blame] | 14 | #include "misc.h" |
| Vivek Goyal | 820e8fe | 2014-03-18 15:26:38 -0400 | [diff] [blame] | 15 | #include "../string.h" |
| Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 16 | |
| Baoquan He | 4252db1 | 2016-04-20 13:55:42 -0700 | [diff] [blame^] | 17 | /* |
| 18 | * WARNING!! |
| 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. |
| Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 22 | */ |
| 23 | |
| Ingo Molnar | 1180e01 | 2008-02-21 05:03:48 +0100 | [diff] [blame] | 24 | #define STATIC static |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #undef memcpy |
| Vivek Goyal | 0499955 | 2014-03-18 15:26:40 -0400 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Use a normal definition of memset() from string.c. There are already |
| 30 | * included header files which expect a definition of memset() and by |
| 31 | * the time we define memset macro, it is too late. |
| 32 | */ |
| 33 | #undef memset |
| Ingo Molnar | 1180e01 | 2008-02-21 05:03:48 +0100 | [diff] [blame] | 34 | #define memzero(s, n) memset((s), 0, (n)) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | static void error(char *m); |
| Paolo Ciarrocchi | fd77c7c | 2008-02-21 00:19:10 +0100 | [diff] [blame] | 38 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /* |
| 40 | * This is set up by the setup-routine at boot-time |
| 41 | */ |
| Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 42 | struct boot_params *boot_params; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| Kees Cook | 82fa963 | 2013-10-10 17:18:16 -0700 | [diff] [blame] | 44 | memptr free_mem_ptr; |
| 45 | memptr free_mem_end_ptr; |
| Ian Campbell | 778cb92 | 2008-01-30 13:33:38 +0100 | [diff] [blame] | 46 | |
| Alexander van Heukelum | 03056c8 | 2008-04-06 14:47:00 +0200 | [diff] [blame] | 47 | static char *vidmem; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static int vidport; |
| 49 | static int lines, cols; |
| 50 | |
| Alain Knaff | ae03c49 | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 51 | #ifdef CONFIG_KERNEL_GZIP |
| 52 | #include "../../../../lib/decompress_inflate.c" |
| 53 | #endif |
| 54 | |
| 55 | #ifdef CONFIG_KERNEL_BZIP2 |
| 56 | #include "../../../../lib/decompress_bunzip2.c" |
| 57 | #endif |
| 58 | |
| 59 | #ifdef CONFIG_KERNEL_LZMA |
| 60 | #include "../../../../lib/decompress_unlzma.c" |
| 61 | #endif |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| Lasse Collin | 3031480 | 2011-01-12 17:01:24 -0800 | [diff] [blame] | 63 | #ifdef CONFIG_KERNEL_XZ |
| 64 | #include "../../../../lib/decompress_unxz.c" |
| 65 | #endif |
| 66 | |
| Albin Tonnerre | 1351099 | 2010-01-08 14:42:45 -0800 | [diff] [blame] | 67 | #ifdef CONFIG_KERNEL_LZO |
| 68 | #include "../../../../lib/decompress_unlzo.c" |
| 69 | #endif |
| 70 | |
| Kyungsik Lee | f9b493a | 2013-07-08 16:01:48 -0700 | [diff] [blame] | 71 | #ifdef CONFIG_KERNEL_LZ4 |
| 72 | #include "../../../../lib/decompress_unlz4.c" |
| 73 | #endif |
| Baoquan He | 4252db1 | 2016-04-20 13:55:42 -0700 | [diff] [blame^] | 74 | /* |
| 75 | * NOTE: When adding a new decompressor, please update the analysis in |
| 76 | * ../header.S. |
| 77 | */ |
| Kyungsik Lee | f9b493a | 2013-07-08 16:01:48 -0700 | [diff] [blame] | 78 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | static void scroll(void) |
| 80 | { |
| 81 | int i; |
| 82 | |
| Paolo Ciarrocchi | fd77c7c | 2008-02-21 00:19:10 +0100 | [diff] [blame] | 83 | memcpy(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2); |
| 84 | for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | vidmem[i] = ' '; |
| 86 | } |
| 87 | |
| Yinghai Lu | 8fee13a4 | 2010-08-02 16:21:22 -0700 | [diff] [blame] | 88 | #define XMTRDY 0x20 |
| 89 | |
| 90 | #define TXR 0 /* Transmit register (WRITE) */ |
| 91 | #define LSR 5 /* Line Status */ |
| 92 | static void serial_putchar(int ch) |
| 93 | { |
| 94 | unsigned timeout = 0xffff; |
| 95 | |
| 96 | while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout) |
| 97 | cpu_relax(); |
| 98 | |
| 99 | outb(ch, early_serial_base + TXR); |
| 100 | } |
| 101 | |
| Joe Millenbach | 7aac301 | 2012-07-19 18:04:39 -0700 | [diff] [blame] | 102 | void __putstr(const char *s) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| Paolo Ciarrocchi | fd77c7c | 2008-02-21 00:19:10 +0100 | [diff] [blame] | 104 | int x, y, pos; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | char c; |
| 106 | |
| Yinghai Lu | 8fee13a4 | 2010-08-02 16:21:22 -0700 | [diff] [blame] | 107 | if (early_serial_base) { |
| 108 | const char *str = s; |
| 109 | while (*str) { |
| 110 | if (*str == '\n') |
| 111 | serial_putchar('\r'); |
| 112 | serial_putchar(*str++); |
| 113 | } |
| 114 | } |
| Ben Collins | 6bcb13b | 2008-06-18 14:04:35 -0400 | [diff] [blame] | 115 | |
| Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 116 | if (boot_params->screen_info.orig_video_mode == 0 && |
| Kristian Høgsberg | 23968f7 | 2008-05-29 18:31:14 -0400 | [diff] [blame] | 117 | lines == 0 && cols == 0) |
| Rusty Russell | a24e785 | 2007-10-21 16:41:35 -0700 | [diff] [blame] | 118 | return; |
| 119 | |
| Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 120 | x = boot_params->screen_info.orig_x; |
| 121 | y = boot_params->screen_info.orig_y; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| Paolo Ciarrocchi | fd77c7c | 2008-02-21 00:19:10 +0100 | [diff] [blame] | 123 | while ((c = *s++) != '\0') { |
| 124 | if (c == '\n') { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | x = 0; |
| Paolo Ciarrocchi | fd77c7c | 2008-02-21 00:19:10 +0100 | [diff] [blame] | 126 | if (++y >= lines) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | scroll(); |
| 128 | y--; |
| 129 | } |
| 130 | } else { |
| Paolo Ciarrocchi | 020878a | 2008-08-02 21:23:36 +0200 | [diff] [blame] | 131 | vidmem[(x + cols * y) * 2] = c; |
| Paolo Ciarrocchi | fd77c7c | 2008-02-21 00:19:10 +0100 | [diff] [blame] | 132 | if (++x >= cols) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | x = 0; |
| Paolo Ciarrocchi | fd77c7c | 2008-02-21 00:19:10 +0100 | [diff] [blame] | 134 | if (++y >= lines) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | scroll(); |
| 136 | y--; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 142 | boot_params->screen_info.orig_x = x; |
| 143 | boot_params->screen_info.orig_y = y; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | pos = (x + cols * y) * 2; /* Update cursor position */ |
| Rene Herman | b02aae9 | 2008-01-30 13:30:05 +0100 | [diff] [blame] | 146 | outb(14, vidport); |
| 147 | outb(0xff & (pos >> 9), vidport+1); |
| 148 | outb(15, vidport); |
| 149 | outb(0xff & (pos >> 1), vidport+1); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| Kees Cook | 79063a7 | 2015-07-06 16:06:20 -0700 | [diff] [blame] | 152 | void __puthex(unsigned long value) |
| 153 | { |
| 154 | char alpha[2] = "0"; |
| 155 | int bits; |
| 156 | |
| 157 | for (bits = sizeof(value) * 8 - 4; bits >= 0; bits -= 4) { |
| 158 | unsigned long digit = (value >> bits) & 0xf; |
| 159 | |
| 160 | if (digit < 0xA) |
| 161 | alpha[0] = '0' + digit; |
| 162 | else |
| 163 | alpha[0] = 'a' + (digit - 0xA); |
| 164 | |
| 165 | __putstr(alpha); |
| 166 | } |
| 167 | } |
| 168 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | static void error(char *x) |
| 170 | { |
| Joe Millenbach | cb454fe | 2012-07-19 18:04:38 -0700 | [diff] [blame] | 171 | error_putstr("\n\n"); |
| 172 | error_putstr(x); |
| 173 | error_putstr("\n\n -- System halted"); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| Ingo Molnar | ff3cf85 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 175 | while (1) |
| 176 | asm("hlt"); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| Kees Cook | a021506 | 2013-07-08 09:15:17 -0700 | [diff] [blame] | 179 | #if CONFIG_X86_NEED_RELOCS |
| 180 | static void handle_relocations(void *output, unsigned long output_len) |
| 181 | { |
| 182 | int *reloc; |
| 183 | unsigned long delta, map, ptr; |
| 184 | unsigned long min_addr = (unsigned long)output; |
| 185 | unsigned long max_addr = min_addr + output_len; |
| 186 | |
| 187 | /* |
| 188 | * Calculate the delta between where vmlinux was linked to load |
| 189 | * and where it was actually loaded. |
| 190 | */ |
| 191 | delta = min_addr - LOAD_PHYSICAL_ADDR; |
| 192 | if (!delta) { |
| 193 | debug_putstr("No relocation needed... "); |
| 194 | return; |
| 195 | } |
| 196 | debug_putstr("Performing relocations... "); |
| 197 | |
| 198 | /* |
| 199 | * The kernel contains a table of relocation addresses. Those |
| 200 | * addresses have the final load address of the kernel in virtual |
| 201 | * memory. We are currently working in the self map. So we need to |
| 202 | * create an adjustment for kernel memory addresses to the self map. |
| 203 | * This will involve subtracting out the base address of the kernel. |
| 204 | */ |
| 205 | map = delta - __START_KERNEL_map; |
| 206 | |
| 207 | /* |
| 208 | * Process relocations: 32 bit relocations first then 64 bit after. |
| Jan Beulich | 6d24c5f | 2014-11-04 08:50:18 +0000 | [diff] [blame] | 209 | * Three sets of binary relocations are added to the end of the kernel |
| Kees Cook | a021506 | 2013-07-08 09:15:17 -0700 | [diff] [blame] | 210 | * before compression. Each relocation table entry is the kernel |
| 211 | * address of the location which needs to be updated stored as a |
| 212 | * 32-bit value which is sign extended to 64 bits. |
| 213 | * |
| 214 | * Format is: |
| 215 | * |
| 216 | * kernel bits... |
| 217 | * 0 - zero terminator for 64 bit relocations |
| 218 | * 64 bit relocation repeated |
| Jan Beulich | 6d24c5f | 2014-11-04 08:50:18 +0000 | [diff] [blame] | 219 | * 0 - zero terminator for inverse 32 bit relocations |
| 220 | * 32 bit inverse relocation repeated |
| Kees Cook | a021506 | 2013-07-08 09:15:17 -0700 | [diff] [blame] | 221 | * 0 - zero terminator for 32 bit relocations |
| 222 | * 32 bit relocation repeated |
| 223 | * |
| 224 | * So we work backwards from the end of the decompressed image. |
| 225 | */ |
| 226 | for (reloc = output + output_len - sizeof(*reloc); *reloc; reloc--) { |
| 227 | int extended = *reloc; |
| 228 | extended += map; |
| 229 | |
| 230 | ptr = (unsigned long)extended; |
| 231 | if (ptr < min_addr || ptr > max_addr) |
| 232 | error("32-bit relocation outside of kernel!\n"); |
| 233 | |
| 234 | *(uint32_t *)ptr += delta; |
| 235 | } |
| 236 | #ifdef CONFIG_X86_64 |
| Jan Beulich | 6d24c5f | 2014-11-04 08:50:18 +0000 | [diff] [blame] | 237 | while (*--reloc) { |
| 238 | long extended = *reloc; |
| 239 | extended += map; |
| 240 | |
| 241 | ptr = (unsigned long)extended; |
| 242 | if (ptr < min_addr || ptr > max_addr) |
| 243 | error("inverse 32-bit relocation outside of kernel!\n"); |
| 244 | |
| 245 | *(int32_t *)ptr -= delta; |
| 246 | } |
| Kees Cook | a021506 | 2013-07-08 09:15:17 -0700 | [diff] [blame] | 247 | for (reloc--; *reloc; reloc--) { |
| 248 | long extended = *reloc; |
| 249 | extended += map; |
| 250 | |
| 251 | ptr = (unsigned long)extended; |
| 252 | if (ptr < min_addr || ptr > max_addr) |
| 253 | error("64-bit relocation outside of kernel!\n"); |
| 254 | |
| 255 | *(uint64_t *)ptr += delta; |
| 256 | } |
| 257 | #endif |
| 258 | } |
| 259 | #else |
| 260 | static inline void handle_relocations(void *output, unsigned long output_len) |
| 261 | { } |
| 262 | #endif |
| 263 | |
| Ian Campbell | 099e137 | 2008-02-13 20:54:58 +0000 | [diff] [blame] | 264 | static void parse_elf(void *output) |
| 265 | { |
| 266 | #ifdef CONFIG_X86_64 |
| 267 | Elf64_Ehdr ehdr; |
| 268 | Elf64_Phdr *phdrs, *phdr; |
| 269 | #else |
| 270 | Elf32_Ehdr ehdr; |
| 271 | Elf32_Phdr *phdrs, *phdr; |
| 272 | #endif |
| 273 | void *dest; |
| 274 | int i; |
| 275 | |
| 276 | memcpy(&ehdr, output, sizeof(ehdr)); |
| Paolo Ciarrocchi | fd77c7c | 2008-02-21 00:19:10 +0100 | [diff] [blame] | 277 | if (ehdr.e_ident[EI_MAG0] != ELFMAG0 || |
| Ian Campbell | 099e137 | 2008-02-13 20:54:58 +0000 | [diff] [blame] | 278 | ehdr.e_ident[EI_MAG1] != ELFMAG1 || |
| 279 | ehdr.e_ident[EI_MAG2] != ELFMAG2 || |
| Paolo Ciarrocchi | fd77c7c | 2008-02-21 00:19:10 +0100 | [diff] [blame] | 280 | ehdr.e_ident[EI_MAG3] != ELFMAG3) { |
| Ian Campbell | 099e137 | 2008-02-13 20:54:58 +0000 | [diff] [blame] | 281 | error("Kernel is not a valid ELF file"); |
| 282 | return; |
| 283 | } |
| 284 | |
| Joe Millenbach | e605a42 | 2012-07-19 18:04:37 -0700 | [diff] [blame] | 285 | debug_putstr("Parsing ELF... "); |
| Ian Campbell | 099e137 | 2008-02-13 20:54:58 +0000 | [diff] [blame] | 286 | |
| 287 | phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum); |
| 288 | if (!phdrs) |
| 289 | error("Failed to allocate space for phdrs"); |
| 290 | |
| 291 | memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum); |
| 292 | |
| Paolo Ciarrocchi | fd77c7c | 2008-02-21 00:19:10 +0100 | [diff] [blame] | 293 | for (i = 0; i < ehdr.e_phnum; i++) { |
| Ian Campbell | 099e137 | 2008-02-13 20:54:58 +0000 | [diff] [blame] | 294 | phdr = &phdrs[i]; |
| 295 | |
| 296 | switch (phdr->p_type) { |
| 297 | case PT_LOAD: |
| 298 | #ifdef CONFIG_RELOCATABLE |
| 299 | dest = output; |
| 300 | dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR); |
| 301 | #else |
| Paolo Ciarrocchi | fd77c7c | 2008-02-21 00:19:10 +0100 | [diff] [blame] | 302 | dest = (void *)(phdr->p_paddr); |
| Ian Campbell | 099e137 | 2008-02-13 20:54:58 +0000 | [diff] [blame] | 303 | #endif |
| 304 | memcpy(dest, |
| 305 | output + phdr->p_offset, |
| 306 | phdr->p_filesz); |
| 307 | break; |
| 308 | default: /* Ignore other PT_* */ break; |
| 309 | } |
| 310 | } |
| Jesper Juhl | 5067cf5 | 2012-01-23 23:34:59 +0100 | [diff] [blame] | 311 | |
| 312 | free(phdrs); |
| Ian Campbell | 099e137 | 2008-02-13 20:54:58 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| Kees Cook | c040288 | 2016-04-18 09:42:13 -0700 | [diff] [blame] | 315 | asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, |
| Ingo Molnar | 1180e01 | 2008-02-21 05:03:48 +0100 | [diff] [blame] | 316 | unsigned char *input_data, |
| 317 | unsigned long input_len, |
| Kees Cook | a021506 | 2013-07-08 09:15:17 -0700 | [diff] [blame] | 318 | unsigned char *output, |
| Junjie Mao | e602336 | 2014-10-31 21:40:38 +0800 | [diff] [blame] | 319 | unsigned long output_len, |
| 320 | unsigned long run_size) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
| Kees Cook | f285f4a | 2015-01-15 16:51:46 -0800 | [diff] [blame] | 322 | unsigned char *output_orig = output; |
| 323 | |
| Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 324 | /* Retain x86 boot parameters pointer passed from startup_32/64. */ |
| 325 | boot_params = rmode; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
| Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 327 | /* Clear flags intended for solely in-kernel use. */ |
| 328 | boot_params->hdr.loadflags &= ~KASLR_FLAG; |
| Borislav Petkov | 78cac48 | 2015-04-01 12:49:52 +0200 | [diff] [blame] | 329 | |
| Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 330 | sanitize_boot_params(boot_params); |
| H. Peter Anvin | 5dcd14e | 2013-01-29 01:05:24 -0800 | [diff] [blame] | 331 | |
| Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 332 | if (boot_params->screen_info.orig_video_mode == 7) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | vidmem = (char *) 0xb0000; |
| 334 | vidport = 0x3b4; |
| 335 | } else { |
| 336 | vidmem = (char *) 0xb8000; |
| 337 | vidport = 0x3d4; |
| 338 | } |
| 339 | |
| Kees Cook | 6655e0a | 2016-04-18 09:42:12 -0700 | [diff] [blame] | 340 | lines = boot_params->screen_info.orig_video_lines; |
| 341 | cols = boot_params->screen_info.orig_video_cols; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| Yinghai Lu | 8fee13a4 | 2010-08-02 16:21:22 -0700 | [diff] [blame] | 343 | console_init(); |
| Kees Cook | c040288 | 2016-04-18 09:42:13 -0700 | [diff] [blame] | 344 | debug_putstr("early console in extract_kernel\n"); |
| Yinghai Lu | 8fee13a4 | 2010-08-02 16:21:22 -0700 | [diff] [blame] | 345 | |
| Ian Campbell | 4c83d65 | 2008-01-30 13:33:38 +0100 | [diff] [blame] | 346 | free_mem_ptr = heap; /* Heap */ |
| Alexander van Heukelum | 7c53976 | 2008-04-08 12:54:30 +0200 | [diff] [blame] | 347 | free_mem_end_ptr = heap + BOOT_HEAP_SIZE; |
| Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 348 | |
| Kees Cook | 79063a7 | 2015-07-06 16:06:20 -0700 | [diff] [blame] | 349 | /* Report initial kernel position details. */ |
| 350 | debug_putaddr(input_data); |
| 351 | debug_putaddr(input_len); |
| 352 | debug_putaddr(output); |
| 353 | debug_putaddr(output_len); |
| 354 | debug_putaddr(run_size); |
| 355 | |
| Junjie Mao | e602336 | 2014-10-31 21:40:38 +0800 | [diff] [blame] | 356 | /* |
| 357 | * The memory hole needed for the kernel is the larger of either |
| 358 | * the entire decompressed kernel plus relocation table, or the |
| 359 | * entire decompressed kernel plus .bss and .brk sections. |
| 360 | */ |
| Kees Cook | 7de828d | 2016-04-18 09:42:14 -0700 | [diff] [blame] | 361 | output = choose_random_location(input_data, input_len, output, |
| Junjie Mao | e602336 | 2014-10-31 21:40:38 +0800 | [diff] [blame] | 362 | output_len > run_size ? output_len |
| 363 | : run_size); |
| Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 364 | |
| 365 | /* Validate memory location choices. */ |
| H. Peter Anvin | 7ed42a2 | 2009-05-12 11:33:08 -0700 | [diff] [blame] | 366 | if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1)) |
| 367 | error("Destination address inappropriately aligned"); |
| Ian Campbell | 778cb92 | 2008-01-30 13:33:38 +0100 | [diff] [blame] | 368 | #ifdef CONFIG_X86_64 |
| H. Peter Anvin | 7ed42a2 | 2009-05-12 11:33:08 -0700 | [diff] [blame] | 369 | if (heap > 0x3fffffffffffUL) |
| Ian Campbell | 778cb92 | 2008-01-30 13:33:38 +0100 | [diff] [blame] | 370 | error("Destination address too large"); |
| 371 | #else |
| H. Peter Anvin | 147dd56 | 2010-12-16 19:11:09 -0800 | [diff] [blame] | 372 | if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff)) |
| Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 373 | error("Destination address too large"); |
| Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 374 | #endif |
| H. Peter Anvin | 7ed42a2 | 2009-05-12 11:33:08 -0700 | [diff] [blame] | 375 | #ifndef CONFIG_RELOCATABLE |
| 376 | if ((unsigned long)output != LOAD_PHYSICAL_ADDR) |
| 377 | error("Wrong destination address"); |
| Ian Campbell | 778cb92 | 2008-01-30 13:33:38 +0100 | [diff] [blame] | 378 | #endif |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
| Joe Millenbach | e605a42 | 2012-07-19 18:04:37 -0700 | [diff] [blame] | 380 | debug_putstr("\nDecompressing Linux... "); |
| Yinghai Lu | 2d3862d | 2015-09-09 15:39:12 -0700 | [diff] [blame] | 381 | __decompress(input_data, input_len, NULL, NULL, output, output_len, |
| 382 | NULL, error); |
| Ian Campbell | 099e137 | 2008-02-13 20:54:58 +0000 | [diff] [blame] | 383 | parse_elf(output); |
| Kees Cook | f285f4a | 2015-01-15 16:51:46 -0800 | [diff] [blame] | 384 | /* |
| 385 | * 32-bit always performs relocations. 64-bit relocations are only |
| 386 | * needed if kASLR has chosen a different load address. |
| 387 | */ |
| 388 | if (!IS_ENABLED(CONFIG_X86_64) || output != output_orig) |
| 389 | handle_relocations(output, output_len); |
| Joe Millenbach | e605a42 | 2012-07-19 18:04:37 -0700 | [diff] [blame] | 390 | debug_putstr("done.\nBooting the kernel.\n"); |
| Kees Cook | 8ab3820 | 2013-10-10 17:18:14 -0700 | [diff] [blame] | 391 | return output; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |