File Index
Files and directories
Sign in
FILE

zboot.c

File preview with syntax highlighting.

linux-6.19.14/drivers/firmware/efi/libstub/zboot.c C
  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 <linux/unaligned.h>
  7
  8#include "efistub.h"
  9
 10static unsigned long alloc_preferred_address(unsigned long alloc_size)
 11{
 12#ifdef EFI_KIMG_PREFERRED_ADDRESS
 13	efi_physical_addr_t efi_addr = EFI_KIMG_PREFERRED_ADDRESS;
 14
 15	if (efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
 16			alloc_size / EFI_PAGE_SIZE, &efi_addr) == EFI_SUCCESS)
 17		return efi_addr;
 18#endif
 19	return ULONG_MAX;
 20}
 21
 22void __weak efi_cache_sync_image(unsigned long image_base,
 23				 unsigned long alloc_size)
 24{
 25	// Provided by the arch to perform the cache maintenance necessary for
 26	// executable code loaded into memory to be safe for execution.
 27}
 28
 29struct screen_info *alloc_screen_info(void)
 30{
 31	return __alloc_screen_info();
 32}
 33
 34asmlinkage efi_status_t __efiapi
 35efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 36{
 37	char *cmdline_ptr __free(efi_pool) = NULL;
 38	unsigned long image_base, alloc_size;
 39	efi_loaded_image_t *image;
 40	efi_status_t status;
 41
 42	WRITE_ONCE(efi_system_table, systab);
 43
 44	status = efi_bs_call(handle_protocol, handle,
 45			     &LOADED_IMAGE_PROTOCOL_GUID, (void **)&image);
 46	if (status != EFI_SUCCESS) {
 47		efi_err("Failed to locate parent's loaded image protocol\n");
 48		return status;
 49	}
 50
 51	status = efi_handle_cmdline(image, &cmdline_ptr);
 52	if (status != EFI_SUCCESS)
 53		return status;
 54
 55	efi_info("Decompressing Linux Kernel...\n");
 56
 57	status = efi_zboot_decompress_init(&alloc_size);
 58	if (status != EFI_SUCCESS)
 59		return status;
 60
 61	 // If the architecture has a preferred address for the image,
 62	 // try that first.
 63	image_base = alloc_preferred_address(alloc_size);
 64	if (image_base == ULONG_MAX) {
 65		unsigned long min_kimg_align = efi_get_kimg_min_align();
 66		u32 seed = U32_MAX;
 67
 68		if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
 69			// Setting the random seed to 0x0 is the same as
 70			// allocating as low as possible
 71			seed = 0;
 72		} else if (efi_nokaslr) {
 73			efi_info("KASLR disabled on kernel command line\n");
 74		} else {
 75			status = efi_get_random_bytes(sizeof(seed), (u8 *)&seed);
 76			if (status == EFI_NOT_FOUND) {
 77				efi_info("EFI_RNG_PROTOCOL unavailable\n");
 78				efi_nokaslr = true;
 79			} else if (status != EFI_SUCCESS) {
 80				efi_err("efi_get_random_bytes() failed (0x%lx)\n",
 81					status);
 82				efi_nokaslr = true;
 83			}
 84		}
 85
 86		status = efi_random_alloc(alloc_size, min_kimg_align, &image_base,
 87					  seed, EFI_LOADER_CODE, 0, EFI_ALLOC_LIMIT);
 88		if (status != EFI_SUCCESS) {
 89			efi_err("Failed to allocate memory\n");
 90			return status;
 91		}
 92	}
 93
 94	// Decompress the payload into the newly allocated buffer
 95	status = efi_zboot_decompress((void *)image_base, alloc_size) ?:
 96	         efi_stub_common(handle, image, image_base, cmdline_ptr);
 97
 98	efi_free(alloc_size, image_base);
 99	return status;
100}
~/ Read-only file preview.