1// SPDX-License-Identifier: GPL-2.0 2 3#include <linux/efi.h> 4#include <linux/pe.h> 5#include <asm/efi.h> 6#include <asm/unaligned.h> 7 8#include "efistub.h" 9 10static unsigned char zboot_heap[SZ_256K] __aligned(64); 11static unsigned long free_mem_ptr, free_mem_end_ptr; 12 13#define STATIC static 14#if defined(CONFIG_KERNEL_GZIP) 15#include "../../../../lib/decompress_inflate.c" 16#elif defined(CONFIG_KERNEL_LZ4) 17#include "../../../../lib/decompress_unlz4.c" 18#elif defined(CONFIG_KERNEL_LZMA) 19#include "../../../../lib/decompress_unlzma.c" 20#elif defined(CONFIG_KERNEL_LZO) 21#include "../../../../lib/decompress_unlzo.c" 22#elif defined(CONFIG_KERNEL_XZ) 23#undef memcpy 24#define memcpy memcpy 25#undef memmove 26#define memmove memmove 27#include "../../../../lib/decompress_unxz.c" 28#elif defined(CONFIG_KERNEL_ZSTD) 29#include "../../../../lib/decompress_unzstd.c" 30#endif 31 32extern char efi_zboot_header[]; 33extern char _gzdata_start[], _gzdata_end[]; 34 35static void error(char *x) 36{ 37 efi_err("EFI decompressor: %s\n", x); 38} 39 40static unsigned long alloc_preferred_address(unsigned long alloc_size) 41{ 42#ifdef EFI_KIMG_PREFERRED_ADDRESS 43 efi_physical_addr_t efi_addr = EFI_KIMG_PREFERRED_ADDRESS; 44 45 if (efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, 46 alloc_size / EFI_PAGE_SIZE, &efi_addr) == EFI_SUCCESS) 47 return efi_addr; 48#endif 49 return ULONG_MAX; 50} 51 52void __weak efi_cache_sync_image(unsigned long image_base, 53 unsigned long alloc_size) 54{ 55 // Provided by the arch to perform the cache maintenance necessary for 56 // executable code loaded into memory to be safe for execution. 57} 58 59struct screen_info *alloc_screen_info(void) 60{ 61 return __alloc_screen_info(); 62} 63 64asmlinkage efi_status_t __efiapi 65efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) 66{ 67 unsigned long compressed_size = _gzdata_end - _gzdata_start; 68 unsigned long image_base, alloc_size; 69 efi_loaded_image_t *image; 70 efi_status_t status; 71 char *cmdline_ptr; 72 int ret; 73 74 WRITE_ONCE(efi_system_table, systab); 75 76 free_mem_ptr = (unsigned long)&zboot_heap; 77 free_mem_end_ptr = free_mem_ptr + sizeof(zboot_heap); 78 79 status = efi_bs_call(handle_protocol, handle, 80 &LOADED_IMAGE_PROTOCOL_GUID, (void **)&image); 81 if (status != EFI_SUCCESS) { 82 error("Failed to locate parent's loaded image protocol"); 83 return status; 84 } 85 86 status = efi_handle_cmdline(image, &cmdline_ptr); 87 if (status != EFI_SUCCESS) 88 return status; 89 90 efi_info("Decompressing Linux Kernel...\n"); 91 92 // SizeOfImage from the compressee's PE/COFF header 93 alloc_size = round_up(get_unaligned_le32(_gzdata_end - 4), 94 EFI_ALLOC_ALIGN); 95 96 // If the architecture has a preferred address for the image, 97 // try that first. 98 image_base = alloc_preferred_address(alloc_size); 99 if (image_base == ULONG_MAX) { 100 unsigned long min_kimg_align = efi_get_kimg_min_align(); 101 u32 seed = U32_MAX; 102 103 if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 104 // Setting the random seed to 0x0 is the same as 105 // allocating as low as possible 106 seed = 0; 107 } else if (efi_nokaslr) { 108 efi_info("KASLR disabled on kernel command line\n"); 109 } else { 110 status = efi_get_random_bytes(sizeof(seed), (u8 *)&seed); 111 if (status == EFI_NOT_FOUND) { 112 efi_info("EFI_RNG_PROTOCOL unavailable\n"); 113 efi_nokaslr = true; 114 } else if (status != EFI_SUCCESS) { 115 efi_err("efi_get_random_bytes() failed (0x%lx)\n", 116 status); 117 efi_nokaslr = true; 118 } 119 } 120 121 status = efi_random_alloc(alloc_size, min_kimg_align, &image_base, 122 seed, EFI_LOADER_CODE, 0, EFI_ALLOC_LIMIT); 123 if (status != EFI_SUCCESS) { 124 efi_err("Failed to allocate memory\n"); 125 goto free_cmdline; 126 } 127 } 128 129 // Decompress the payload into the newly allocated buffer. 130 ret = __decompress(_gzdata_start, compressed_size, NULL, NULL, 131 (void *)image_base, alloc_size, NULL, error); 132 if (ret < 0) { 133 error("Decompression failed"); 134 status = EFI_DEVICE_ERROR; 135 goto free_image; 136 } 137 138 efi_cache_sync_image(image_base, alloc_size); 139 140 status = efi_stub_common(handle, image, image_base, cmdline_ptr); 141 142free_image: 143 efi_free(alloc_size, image_base); 144free_cmdline: 145 efi_bs_call(free_pool, cmdline_ptr); 146 return status; 147} 148