1 // SPDX-License-Identifier: GPL-2.0
5 * This file implements the EFI boot stub for the arm64 kernel.
10 #include <linux/efi.h>
12 #include <asm/memory.h>
13 #include <asm/sections.h>
14 #include <asm/sysreg.h>
18 efi_status_t check_platform_features(void)
22 /* UEFI mandates support for 4 KB granularity, no need to check */
23 if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
26 tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf;
27 if (tg != ID_AA64MMFR0_TGRAN_SUPPORTED) {
28 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
29 efi_err("This 64 KB granular kernel is not supported by your CPU\n");
31 efi_err("This 16 KB granular kernel is not supported by your CPU\n");
32 return EFI_UNSUPPORTED;
38 * Although relocatable kernels can fix up the misalignment with respect to
39 * MIN_KIMG_ALIGN, the resulting virtual text addresses are subtly out of
40 * sync with those recorded in the vmlinux when kaslr is disabled but the
41 * image required relocation anyway. Therefore retain 2M alignment unless
44 static u64 min_kimg_align(void)
46 return efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
49 efi_status_t handle_kernel_image(unsigned long *image_addr,
50 unsigned long *image_size,
51 unsigned long *reserve_addr,
52 unsigned long *reserve_size,
53 unsigned long dram_base,
54 efi_loaded_image_t *image)
57 unsigned long kernel_size, kernel_memsize = 0;
60 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
62 status = efi_get_random_bytes(sizeof(phys_seed),
64 if (status == EFI_NOT_FOUND) {
65 efi_info("EFI_RNG_PROTOCOL unavailable, no randomness supplied\n");
66 } else if (status != EFI_SUCCESS) {
67 efi_err("efi_get_random_bytes() failed\n");
71 efi_info("KASLR disabled on kernel command line\n");
75 if (image->image_base != _text)
76 efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
78 kernel_size = _edata - _text;
79 kernel_memsize = kernel_size + (_end - _edata);
80 *reserve_size = kernel_memsize + TEXT_OFFSET % min_kimg_align();
82 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
84 * If KASLR is enabled, and we have some randomness available,
85 * locate the kernel at a randomized offset in physical memory.
87 status = efi_random_alloc(*reserve_size, min_kimg_align(),
88 reserve_addr, phys_seed);
90 status = EFI_OUT_OF_RESOURCES;
93 if (status != EFI_SUCCESS) {
94 if (IS_ALIGNED((u64)_text - TEXT_OFFSET, min_kimg_align())) {
96 * Just execute from wherever we were loaded by the
97 * UEFI PE/COFF loader if the alignment is suitable.
99 *image_addr = (u64)_text;
104 status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
105 ULONG_MAX, min_kimg_align());
107 if (status != EFI_SUCCESS) {
108 efi_err("Failed to relocate kernel\n");
114 *image_addr = *reserve_addr + TEXT_OFFSET % min_kimg_align();
115 memcpy((void *)*image_addr, _text, kernel_size);