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 10 static unsigned char zboot_heap[SZ_256K] __aligned(64); 11 static 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 32 extern char efi_zboot_header[]; 33 extern char _gzdata_start[], _gzdata_end[]; 34 35 static void error(char *x) 36 { 37 efi_err("EFI decompressor: %s\n", x); 38 } 39 40 static 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 52 void __weak efi_cache_sync_image(unsigned long image_base, 53 unsigned long alloc_size, 54 unsigned long code_size) 55 { 56 // Provided by the arch to perform the cache maintenance necessary for 57 // executable code loaded into memory to be safe for execution. 58 } 59 60 struct screen_info *alloc_screen_info(void) 61 { 62 return __alloc_screen_info(); 63 } 64 65 asmlinkage efi_status_t __efiapi 66 efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) 67 { 68 unsigned long compressed_size = _gzdata_end - _gzdata_start; 69 unsigned long image_base, alloc_size, code_size; 70 efi_loaded_image_t *image; 71 efi_status_t status; 72 char *cmdline_ptr; 73 int ret; 74 75 WRITE_ONCE(efi_system_table, systab); 76 77 free_mem_ptr = (unsigned long)&zboot_heap; 78 free_mem_end_ptr = free_mem_ptr + sizeof(zboot_heap); 79 80 status = efi_bs_call(handle_protocol, handle, 81 &LOADED_IMAGE_PROTOCOL_GUID, (void **)&image); 82 if (status != EFI_SUCCESS) { 83 error("Failed to locate parent's loaded image protocol"); 84 return status; 85 } 86 87 status = efi_handle_cmdline(image, &cmdline_ptr); 88 if (status != EFI_SUCCESS) 89 return status; 90 91 efi_info("Decompressing Linux Kernel...\n"); 92 93 // SizeOfImage from the compressee's PE/COFF header 94 alloc_size = round_up(get_unaligned_le32(_gzdata_end - 4), 95 EFI_ALLOC_ALIGN); 96 97 // SizeOfHeaders and SizeOfCode from the compressee's PE/COFF header 98 code_size = get_unaligned_le32(_gzdata_end - 8) + 99 get_unaligned_le32(_gzdata_end - 12); 100 101 // If the architecture has a preferred address for the image, 102 // try that first. 103 image_base = alloc_preferred_address(alloc_size); 104 if (image_base == ULONG_MAX) { 105 unsigned long min_kimg_align = efi_get_kimg_min_align(); 106 u32 seed = U32_MAX; 107 108 if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 109 // Setting the random seed to 0x0 is the same as 110 // allocating as low as possible 111 seed = 0; 112 } else if (efi_nokaslr) { 113 efi_info("KASLR disabled on kernel command line\n"); 114 } else { 115 status = efi_get_random_bytes(sizeof(seed), (u8 *)&seed); 116 if (status == EFI_NOT_FOUND) { 117 efi_info("EFI_RNG_PROTOCOL unavailable\n"); 118 efi_nokaslr = true; 119 } else if (status != EFI_SUCCESS) { 120 efi_err("efi_get_random_bytes() failed (0x%lx)\n", 121 status); 122 efi_nokaslr = true; 123 } 124 } 125 126 status = efi_random_alloc(alloc_size, min_kimg_align, &image_base, 127 seed, EFI_LOADER_CODE); 128 if (status != EFI_SUCCESS) { 129 efi_err("Failed to allocate memory\n"); 130 goto free_cmdline; 131 } 132 } 133 134 // Decompress the payload into the newly allocated buffer. 135 ret = __decompress(_gzdata_start, compressed_size, NULL, NULL, 136 (void *)image_base, alloc_size, NULL, error); 137 if (ret < 0) { 138 error("Decompression failed"); 139 status = EFI_DEVICE_ERROR; 140 goto free_image; 141 } 142 143 efi_cache_sync_image(image_base, alloc_size, code_size); 144 145 efi_remap_image(image_base, alloc_size, code_size); 146 147 status = efi_stub_common(handle, image, image_base, cmdline_ptr); 148 149 free_image: 150 efi_free(alloc_size, image_base); 151 free_cmdline: 152 efi_bs_call(free_pool, cmdline_ptr); 153 return status; 154 } 155