linux/arch/x86/boot/compressed/misc.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * misc.c
   4 *
   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.
   9 *
  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
  13 */
  14
  15#include "misc.h"
  16#include "error.h"
  17#include "../string.h"
  18#include "../voffset.h"
  19
  20/*
  21 * WARNING!!
  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.
  25 */
  26
  27/* Macros used by the included decompressor code below. */
  28#define STATIC          static
  29
  30/*
  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.
  34 */
  35#undef memcpy
  36#undef memset
  37#define memzero(s, n)   memset((s), 0, (n))
  38#define memmove         memmove
  39
  40/* Functions used by the included decompressor code below. */
  41void *memmove(void *dest, const void *src, size_t n);
  42
  43/*
  44 * This is set up by the setup-routine at boot-time
  45 */
  46struct boot_params *boot_params;
  47
  48memptr free_mem_ptr;
  49memptr free_mem_end_ptr;
  50
  51static char *vidmem;
  52static int vidport;
  53static int lines, cols;
  54
  55#ifdef CONFIG_KERNEL_GZIP
  56#include "../../../../lib/decompress_inflate.c"
  57#endif
  58
  59#ifdef CONFIG_KERNEL_BZIP2
  60#include "../../../../lib/decompress_bunzip2.c"
  61#endif
  62
  63#ifdef CONFIG_KERNEL_LZMA
  64#include "../../../../lib/decompress_unlzma.c"
  65#endif
  66
  67#ifdef CONFIG_KERNEL_XZ
  68#include "../../../../lib/decompress_unxz.c"
  69#endif
  70
  71#ifdef CONFIG_KERNEL_LZO
  72#include "../../../../lib/decompress_unlzo.c"
  73#endif
  74
  75#ifdef CONFIG_KERNEL_LZ4
  76#include "../../../../lib/decompress_unlz4.c"
  77#endif
  78/*
  79 * NOTE: When adding a new decompressor, please update the analysis in
  80 * ../header.S.
  81 */
  82
  83static void scroll(void)
  84{
  85        int i;
  86
  87        memmove(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
  88        for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
  89                vidmem[i] = ' ';
  90}
  91
  92#define XMTRDY          0x20
  93
  94#define TXR             0       /*  Transmit register (WRITE) */
  95#define LSR             5       /*  Line Status               */
  96static void serial_putchar(int ch)
  97{
  98        unsigned timeout = 0xffff;
  99
 100        while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
 101                cpu_relax();
 102
 103        outb(ch, early_serial_base + TXR);
 104}
 105
 106void __putstr(const char *s)
 107{
 108        int x, y, pos;
 109        char c;
 110
 111        if (early_serial_base) {
 112                const char *str = s;
 113                while (*str) {
 114                        if (*str == '\n')
 115                                serial_putchar('\r');
 116                        serial_putchar(*str++);
 117                }
 118        }
 119
 120        if (lines == 0 || cols == 0)
 121                return;
 122
 123        x = boot_params->screen_info.orig_x;
 124        y = boot_params->screen_info.orig_y;
 125
 126        while ((c = *s++) != '\0') {
 127                if (c == '\n') {
 128                        x = 0;
 129                        if (++y >= lines) {
 130                                scroll();
 131                                y--;
 132                        }
 133                } else {
 134                        vidmem[(x + cols * y) * 2] = c;
 135                        if (++x >= cols) {
 136                                x = 0;
 137                                if (++y >= lines) {
 138                                        scroll();
 139                                        y--;
 140                                }
 141                        }
 142                }
 143        }
 144
 145        boot_params->screen_info.orig_x = x;
 146        boot_params->screen_info.orig_y = y;
 147
 148        pos = (x + cols * y) * 2;       /* Update cursor position */
 149        outb(14, vidport);
 150        outb(0xff & (pos >> 9), vidport+1);
 151        outb(15, vidport);
 152        outb(0xff & (pos >> 1), vidport+1);
 153}
 154
 155void __puthex(unsigned long value)
 156{
 157        char alpha[2] = "0";
 158        int bits;
 159
 160        for (bits = sizeof(value) * 8 - 4; bits >= 0; bits -= 4) {
 161                unsigned long digit = (value >> bits) & 0xf;
 162
 163                if (digit < 0xA)
 164                        alpha[0] = '0' + digit;
 165                else
 166                        alpha[0] = 'a' + (digit - 0xA);
 167
 168                __putstr(alpha);
 169        }
 170}
 171
 172static bool l5_supported(void)
 173{
 174        /* Check if leaf 7 is supported. */
 175        if (native_cpuid_eax(0) < 7)
 176                return 0;
 177
 178        /* Check if la57 is supported. */
 179        return native_cpuid_ecx(7) & (1 << (X86_FEATURE_LA57 & 31));
 180}
 181
 182#if CONFIG_X86_NEED_RELOCS
 183static void handle_relocations(void *output, unsigned long output_len,
 184                               unsigned long virt_addr)
 185{
 186        int *reloc;
 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);
 190
 191        /*
 192         * Calculate the delta between where vmlinux was linked to load
 193         * and where it was actually loaded.
 194         */
 195        delta = min_addr - LOAD_PHYSICAL_ADDR;
 196
 197        /*
 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.
 203         */
 204        map = delta - __START_KERNEL_map;
 205
 206        /*
 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.
 210         */
 211        if (IS_ENABLED(CONFIG_X86_64))
 212                delta = virt_addr - LOAD_PHYSICAL_ADDR;
 213
 214        if (!delta) {
 215                debug_putstr("No relocation needed... ");
 216                return;
 217        }
 218        debug_putstr("Performing relocations... ");
 219
 220        /*
 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.
 226         *
 227         * Format is:
 228         *
 229         * kernel 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
 236         *
 237         * So we work backwards from the end of the decompressed image.
 238         */
 239        for (reloc = output + output_len - sizeof(*reloc); *reloc; reloc--) {
 240                long extended = *reloc;
 241                extended += map;
 242
 243                ptr = (unsigned long)extended;
 244                if (ptr < min_addr || ptr > max_addr)
 245                        error("32-bit relocation outside of kernel!\n");
 246
 247                *(uint32_t *)ptr += delta;
 248        }
 249#ifdef CONFIG_X86_64
 250        while (*--reloc) {
 251                long extended = *reloc;
 252                extended += map;
 253
 254                ptr = (unsigned long)extended;
 255                if (ptr < min_addr || ptr > max_addr)
 256                        error("inverse 32-bit relocation outside of kernel!\n");
 257
 258                *(int32_t *)ptr -= delta;
 259        }
 260        for (reloc--; *reloc; reloc--) {
 261                long extended = *reloc;
 262                extended += map;
 263
 264                ptr = (unsigned long)extended;
 265                if (ptr < min_addr || ptr > max_addr)
 266                        error("64-bit relocation outside of kernel!\n");
 267
 268                *(uint64_t *)ptr += delta;
 269        }
 270#endif
 271}
 272#else
 273static inline void handle_relocations(void *output, unsigned long output_len,
 274                                      unsigned long virt_addr)
 275{ }
 276#endif
 277
 278static void parse_elf(void *output)
 279{
 280#ifdef CONFIG_X86_64
 281        Elf64_Ehdr ehdr;
 282        Elf64_Phdr *phdrs, *phdr;
 283#else
 284        Elf32_Ehdr ehdr;
 285        Elf32_Phdr *phdrs, *phdr;
 286#endif
 287        void *dest;
 288        int i;
 289
 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");
 296                return;
 297        }
 298
 299        debug_putstr("Parsing ELF... ");
 300
 301        phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
 302        if (!phdrs)
 303                error("Failed to allocate space for phdrs");
 304
 305        memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
 306
 307        for (i = 0; i < ehdr.e_phnum; i++) {
 308                phdr = &phdrs[i];
 309
 310                switch (phdr->p_type) {
 311                case PT_LOAD:
 312#ifdef CONFIG_X86_64
 313                        if ((phdr->p_align % 0x200000) != 0)
 314                                error("Alignment of LOAD segment isn't multiple of 2MB");
 315#endif
 316#ifdef CONFIG_RELOCATABLE
 317                        dest = output;
 318                        dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
 319#else
 320                        dest = (void *)(phdr->p_paddr);
 321#endif
 322                        memmove(dest, output + phdr->p_offset, phdr->p_filesz);
 323                        break;
 324                default: /* Ignore other PT_* */ break;
 325                }
 326        }
 327
 328        free(phdrs);
 329}
 330
 331/*
 332 * The compressed kernel image (ZO), has been moved so that its position
 333 * is against the end of the buffer used to hold the uncompressed kernel
 334 * image (VO) and the execution environment (.bss, .brk), which makes sure
 335 * there is room to do the in-place decompression. (See header.S for the
 336 * calculations.)
 337 *
 338 *                             |-----compressed kernel image------|
 339 *                             V                                  V
 340 * 0                       extract_offset                      +INIT_SIZE
 341 * |-----------|---------------|-------------------------|--------|
 342 *             |               |                         |        |
 343 *           VO__text      startup_32 of ZO          VO__end    ZO__end
 344 *             ^                                         ^
 345 *             |-------uncompressed kernel image---------|
 346 *
 347 */
 348asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
 349                                  unsigned char *input_data,
 350                                  unsigned long input_len,
 351                                  unsigned char *output,
 352                                  unsigned long output_len)
 353{
 354        const unsigned long kernel_total_size = VO__end - VO__text;
 355        unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
 356
 357        /* Retain x86 boot parameters pointer passed from startup_32/64. */
 358        boot_params = rmode;
 359
 360        /* Clear flags intended for solely in-kernel use. */
 361        boot_params->hdr.loadflags &= ~KASLR_FLAG;
 362
 363        sanitize_boot_params(boot_params);
 364
 365        if (boot_params->screen_info.orig_video_mode == 7) {
 366                vidmem = (char *) 0xb0000;
 367                vidport = 0x3b4;
 368        } else {
 369                vidmem = (char *) 0xb8000;
 370                vidport = 0x3d4;
 371        }
 372
 373        lines = boot_params->screen_info.orig_video_lines;
 374        cols = boot_params->screen_info.orig_video_cols;
 375
 376        console_init();
 377        debug_putstr("early console in extract_kernel\n");
 378
 379        if (IS_ENABLED(CONFIG_X86_5LEVEL) && !l5_supported()) {
 380                error("This linux kernel as configured requires 5-level paging\n"
 381                        "This CPU does not support the required 'cr4.la57' feature\n"
 382                        "Unable to boot - please use a kernel appropriate for your CPU\n");
 383        }
 384
 385        free_mem_ptr     = heap;        /* Heap */
 386        free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
 387
 388        /* Report initial kernel position details. */
 389        debug_putaddr(input_data);
 390        debug_putaddr(input_len);
 391        debug_putaddr(output);
 392        debug_putaddr(output_len);
 393        debug_putaddr(kernel_total_size);
 394
 395        /*
 396         * The memory hole needed for the kernel is the larger of either
 397         * the entire decompressed kernel plus relocation table, or the
 398         * entire decompressed kernel plus .bss and .brk sections.
 399         */
 400        choose_random_location((unsigned long)input_data, input_len,
 401                                (unsigned long *)&output,
 402                                max(output_len, kernel_total_size),
 403                                &virt_addr);
 404
 405        /* Validate memory location choices. */
 406        if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
 407                error("Destination physical address inappropriately aligned");
 408        if (virt_addr & (MIN_KERNEL_ALIGN - 1))
 409                error("Destination virtual address inappropriately aligned");
 410#ifdef CONFIG_X86_64
 411        if (heap > 0x3fffffffffffUL)
 412                error("Destination address too large");
 413        if (virt_addr + max(output_len, kernel_total_size) > KERNEL_IMAGE_SIZE)
 414                error("Destination virtual address is beyond the kernel mapping area");
 415#else
 416        if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
 417                error("Destination address too large");
 418#endif
 419#ifndef CONFIG_RELOCATABLE
 420        if ((unsigned long)output != LOAD_PHYSICAL_ADDR)
 421                error("Destination address does not match LOAD_PHYSICAL_ADDR");
 422        if (virt_addr != LOAD_PHYSICAL_ADDR)
 423                error("Destination virtual address changed when not relocatable");
 424#endif
 425
 426        debug_putstr("\nDecompressing Linux... ");
 427        __decompress(input_data, input_len, NULL, NULL, output, output_len,
 428                        NULL, error);
 429        parse_elf(output);
 430        handle_relocations(output, output_len, virt_addr);
 431        debug_putstr("done.\nBooting the kernel.\n");
 432        return output;
 433}
 434
 435void fortify_panic(const char *name)
 436{
 437        error("detected buffer overflow");
 438}
 439