14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
2bf457786SArd Biesheuvel /*
3bf457786SArd Biesheuvel  * Copyright (C) 2013, 2014 Linaro Ltd;  <roy.franz@linaro.org>
4bf457786SArd Biesheuvel  *
5bf457786SArd Biesheuvel  * This file implements the EFI boot stub for the arm64 kernel.
6bf457786SArd Biesheuvel  * Adapted from ARM version by Mark Salter <msalter@redhat.com>
7bf457786SArd Biesheuvel  */
80426a4e6SArd Biesheuvel 
90426a4e6SArd Biesheuvel /*
100426a4e6SArd Biesheuvel  * To prevent the compiler from emitting GOT-indirected (and thus absolute)
110426a4e6SArd Biesheuvel  * references to the section markers, override their visibility as 'hidden'
120426a4e6SArd Biesheuvel  */
130426a4e6SArd Biesheuvel #pragma GCC visibility push(hidden)
140426a4e6SArd Biesheuvel #include <asm/sections.h>
150426a4e6SArd Biesheuvel #pragma GCC visibility pop
160426a4e6SArd Biesheuvel 
17bf457786SArd Biesheuvel #include <linux/efi.h>
18bf457786SArd Biesheuvel #include <asm/efi.h>
19170976bcSMark Rutland #include <asm/memory.h>
2042b55734SArd Biesheuvel #include <asm/sysreg.h>
21bf457786SArd Biesheuvel 
222b5fe07aSArd Biesheuvel #include "efistub.h"
232b5fe07aSArd Biesheuvel 
2442b55734SArd Biesheuvel efi_status_t check_platform_features(efi_system_table_t *sys_table_arg)
2542b55734SArd Biesheuvel {
2642b55734SArd Biesheuvel 	u64 tg;
2742b55734SArd Biesheuvel 
2842b55734SArd Biesheuvel 	/* UEFI mandates support for 4 KB granularity, no need to check */
2942b55734SArd Biesheuvel 	if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
3042b55734SArd Biesheuvel 		return EFI_SUCCESS;
3142b55734SArd Biesheuvel 
3242b55734SArd Biesheuvel 	tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf;
3342b55734SArd Biesheuvel 	if (tg != ID_AA64MMFR0_TGRAN_SUPPORTED) {
3442b55734SArd Biesheuvel 		if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
3542b55734SArd Biesheuvel 			pr_efi_err(sys_table_arg, "This 64 KB granular kernel is not supported by your CPU\n");
3642b55734SArd Biesheuvel 		else
3742b55734SArd Biesheuvel 			pr_efi_err(sys_table_arg, "This 16 KB granular kernel is not supported by your CPU\n");
3842b55734SArd Biesheuvel 		return EFI_UNSUPPORTED;
3942b55734SArd Biesheuvel 	}
4042b55734SArd Biesheuvel 	return EFI_SUCCESS;
4142b55734SArd Biesheuvel }
42bf457786SArd Biesheuvel 
43dae31fd2SArd Biesheuvel efi_status_t handle_kernel_image(efi_system_table_t *sys_table_arg,
44bf457786SArd Biesheuvel 				 unsigned long *image_addr,
45bf457786SArd Biesheuvel 				 unsigned long *image_size,
46bf457786SArd Biesheuvel 				 unsigned long *reserve_addr,
47bf457786SArd Biesheuvel 				 unsigned long *reserve_size,
48bf457786SArd Biesheuvel 				 unsigned long dram_base,
49bf457786SArd Biesheuvel 				 efi_loaded_image_t *image)
50bf457786SArd Biesheuvel {
51bf457786SArd Biesheuvel 	efi_status_t status;
52bf457786SArd Biesheuvel 	unsigned long kernel_size, kernel_memsize = 0;
53bf457786SArd Biesheuvel 	void *old_image_addr = (void *)*image_addr;
542dc10ad8SLinus Torvalds 	unsigned long preferred_offset;
552b5fe07aSArd Biesheuvel 	u64 phys_seed = 0;
562b5fe07aSArd Biesheuvel 
572b5fe07aSArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
5860f38de7SArd Biesheuvel 		if (!nokaslr()) {
592b5fe07aSArd Biesheuvel 			status = efi_get_random_bytes(sys_table_arg,
602b5fe07aSArd Biesheuvel 						      sizeof(phys_seed),
612b5fe07aSArd Biesheuvel 						      (u8 *)&phys_seed);
622b5fe07aSArd Biesheuvel 			if (status == EFI_NOT_FOUND) {
632b5fe07aSArd Biesheuvel 				pr_efi(sys_table_arg, "EFI_RNG_PROTOCOL unavailable, no randomness supplied\n");
642b5fe07aSArd Biesheuvel 			} else if (status != EFI_SUCCESS) {
652b5fe07aSArd Biesheuvel 				pr_efi_err(sys_table_arg, "efi_get_random_bytes() failed\n");
662b5fe07aSArd Biesheuvel 				return status;
672b5fe07aSArd Biesheuvel 			}
682b5fe07aSArd Biesheuvel 		} else {
692b5fe07aSArd Biesheuvel 			pr_efi(sys_table_arg, "KASLR disabled on kernel command line\n");
702b5fe07aSArd Biesheuvel 		}
712b5fe07aSArd Biesheuvel 	}
722dc10ad8SLinus Torvalds 
732dc10ad8SLinus Torvalds 	/*
742dc10ad8SLinus Torvalds 	 * The preferred offset of the kernel Image is TEXT_OFFSET bytes beyond
752dc10ad8SLinus Torvalds 	 * a 2 MB aligned base, which itself may be lower than dram_base, as
762dc10ad8SLinus Torvalds 	 * long as the resulting offset equals or exceeds it.
772dc10ad8SLinus Torvalds 	 */
782b5fe07aSArd Biesheuvel 	preferred_offset = round_down(dram_base, MIN_KIMG_ALIGN) + TEXT_OFFSET;
792dc10ad8SLinus Torvalds 	if (preferred_offset < dram_base)
802b5fe07aSArd Biesheuvel 		preferred_offset += MIN_KIMG_ALIGN;
81bf457786SArd Biesheuvel 
82bf457786SArd Biesheuvel 	kernel_size = _edata - _text;
83bf457786SArd Biesheuvel 	kernel_memsize = kernel_size + (_end - _edata);
84bf457786SArd Biesheuvel 
852b5fe07aSArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
86bf457786SArd Biesheuvel 		/*
876f26b367SArd Biesheuvel 		 * If CONFIG_DEBUG_ALIGN_RODATA is not set, produce a
886f26b367SArd Biesheuvel 		 * displacement in the interval [0, MIN_KIMG_ALIGN) that
89170976bcSMark Rutland 		 * doesn't violate this kernel's de-facto alignment
90170976bcSMark Rutland 		 * constraints.
916f26b367SArd Biesheuvel 		 */
92170976bcSMark Rutland 		u32 mask = (MIN_KIMG_ALIGN - 1) & ~(EFI_KIMG_ALIGN - 1);
936f26b367SArd Biesheuvel 		u32 offset = !IS_ENABLED(CONFIG_DEBUG_ALIGN_RODATA) ?
946f26b367SArd Biesheuvel 			     (phys_seed >> 32) & mask : TEXT_OFFSET;
956f26b367SArd Biesheuvel 
966f26b367SArd Biesheuvel 		/*
974f74d72aSMark Rutland 		 * With CONFIG_RANDOMIZE_TEXT_OFFSET=y, TEXT_OFFSET may not
984f74d72aSMark Rutland 		 * be a multiple of EFI_KIMG_ALIGN, and we must ensure that
994f74d72aSMark Rutland 		 * we preserve the misalignment of 'offset' relative to
1004f74d72aSMark Rutland 		 * EFI_KIMG_ALIGN so that statically allocated objects whose
1014f74d72aSMark Rutland 		 * alignment exceeds PAGE_SIZE appear correctly aligned in
1024f74d72aSMark Rutland 		 * memory.
1034f74d72aSMark Rutland 		 */
1044f74d72aSMark Rutland 		offset |= TEXT_OFFSET % EFI_KIMG_ALIGN;
1054f74d72aSMark Rutland 
1064f74d72aSMark Rutland 		/*
1072b5fe07aSArd Biesheuvel 		 * If KASLR is enabled, and we have some randomness available,
1082b5fe07aSArd Biesheuvel 		 * locate the kernel at a randomized offset in physical memory.
1092b5fe07aSArd Biesheuvel 		 */
1106f26b367SArd Biesheuvel 		*reserve_size = kernel_memsize + offset;
1112b5fe07aSArd Biesheuvel 		status = efi_random_alloc(sys_table_arg, *reserve_size,
1122b5fe07aSArd Biesheuvel 					  MIN_KIMG_ALIGN, reserve_addr,
1136f26b367SArd Biesheuvel 					  (u32)phys_seed);
1142b5fe07aSArd Biesheuvel 
1156f26b367SArd Biesheuvel 		*image_addr = *reserve_addr + offset;
1162b5fe07aSArd Biesheuvel 	} else {
1172b5fe07aSArd Biesheuvel 		/*
1182b5fe07aSArd Biesheuvel 		 * Else, try a straight allocation at the preferred offset.
119bf457786SArd Biesheuvel 		 * This will work around the issue where, if dram_base == 0x0,
120bf457786SArd Biesheuvel 		 * efi_low_alloc() refuses to allocate at 0x0 (to prevent the
121bf457786SArd Biesheuvel 		 * address of the allocation to be mistaken for a FAIL return
122bf457786SArd Biesheuvel 		 * value or a NULL pointer). It will also ensure that, on
123bf457786SArd Biesheuvel 		 * platforms where the [dram_base, dram_base + TEXT_OFFSET)
124bf457786SArd Biesheuvel 		 * interval is partially occupied by the firmware (like on APM
125bf457786SArd Biesheuvel 		 * Mustang), we can still place the kernel at the address
126bf457786SArd Biesheuvel 		 * 'dram_base + TEXT_OFFSET'.
127bf457786SArd Biesheuvel 		 */
1282b5fe07aSArd Biesheuvel 		if (*image_addr == preferred_offset)
1292b5fe07aSArd Biesheuvel 			return EFI_SUCCESS;
1302b5fe07aSArd Biesheuvel 
1312dc10ad8SLinus Torvalds 		*image_addr = *reserve_addr = preferred_offset;
1322b5fe07aSArd Biesheuvel 		*reserve_size = round_up(kernel_memsize, EFI_ALLOC_ALIGN);
1332b5fe07aSArd Biesheuvel 
134bf457786SArd Biesheuvel 		status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,
1352b5fe07aSArd Biesheuvel 					EFI_LOADER_DATA,
1362b5fe07aSArd Biesheuvel 					*reserve_size / EFI_PAGE_SIZE,
137bf457786SArd Biesheuvel 					(efi_physical_addr_t *)reserve_addr);
1382b5fe07aSArd Biesheuvel 	}
1392b5fe07aSArd Biesheuvel 
140bf457786SArd Biesheuvel 	if (status != EFI_SUCCESS) {
1412b5fe07aSArd Biesheuvel 		*reserve_size = kernel_memsize + TEXT_OFFSET;
1422b5fe07aSArd Biesheuvel 		status = efi_low_alloc(sys_table_arg, *reserve_size,
1432b5fe07aSArd Biesheuvel 				       MIN_KIMG_ALIGN, reserve_addr);
144bf457786SArd Biesheuvel 
145bf457786SArd Biesheuvel 		if (status != EFI_SUCCESS) {
146bf457786SArd Biesheuvel 			pr_efi_err(sys_table_arg, "Failed to relocate kernel\n");
1472b5fe07aSArd Biesheuvel 			*reserve_size = 0;
148bf457786SArd Biesheuvel 			return status;
149bf457786SArd Biesheuvel 		}
150bf457786SArd Biesheuvel 		*image_addr = *reserve_addr + TEXT_OFFSET;
151bf457786SArd Biesheuvel 	}
152bf457786SArd Biesheuvel 	memcpy((void *)*image_addr, old_image_addr, kernel_size);
153bf457786SArd Biesheuvel 
154bf457786SArd Biesheuvel 	return EFI_SUCCESS;
155bf457786SArd Biesheuvel }
156