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 
10bf457786SArd Biesheuvel #include <linux/efi.h>
11bf457786SArd Biesheuvel #include <asm/efi.h>
12170976bcSMark Rutland #include <asm/memory.h>
136f05106eSArd Biesheuvel #include <asm/sections.h>
1442b55734SArd Biesheuvel #include <asm/sysreg.h>
15bf457786SArd Biesheuvel 
162b5fe07aSArd Biesheuvel #include "efistub.h"
172b5fe07aSArd Biesheuvel 
18cd33a5c1SArd Biesheuvel efi_status_t check_platform_features(void)
1942b55734SArd Biesheuvel {
2042b55734SArd Biesheuvel 	u64 tg;
2142b55734SArd Biesheuvel 
2242b55734SArd Biesheuvel 	/* UEFI mandates support for 4 KB granularity, no need to check */
2342b55734SArd Biesheuvel 	if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
2442b55734SArd Biesheuvel 		return EFI_SUCCESS;
2542b55734SArd Biesheuvel 
2642b55734SArd Biesheuvel 	tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf;
2726f55386SJames Morse 	if (tg < ID_AA64MMFR0_TGRAN_SUPPORTED_MIN || tg > ID_AA64MMFR0_TGRAN_SUPPORTED_MAX) {
2842b55734SArd Biesheuvel 		if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
29793473c2SArvind Sankar 			efi_err("This 64 KB granular kernel is not supported by your CPU\n");
3042b55734SArd Biesheuvel 		else
31793473c2SArvind Sankar 			efi_err("This 16 KB granular kernel is not supported by your CPU\n");
3242b55734SArd Biesheuvel 		return EFI_UNSUPPORTED;
3342b55734SArd Biesheuvel 	}
3442b55734SArd Biesheuvel 	return EFI_SUCCESS;
3542b55734SArd Biesheuvel }
36bf457786SArd Biesheuvel 
3782046702SArd Biesheuvel /*
385b94046eSArd Biesheuvel  * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
395b94046eSArd Biesheuvel  * to provide space, and fail to zero it). Check for this condition by double
405b94046eSArd Biesheuvel  * checking that the first and the last byte of the image are covered by the
415b94046eSArd Biesheuvel  * same EFI memory map entry.
425b94046eSArd Biesheuvel  */
435b94046eSArd Biesheuvel static bool check_image_region(u64 base, u64 size)
445b94046eSArd Biesheuvel {
455b94046eSArd Biesheuvel 	unsigned long map_size, desc_size, buff_size;
465b94046eSArd Biesheuvel 	efi_memory_desc_t *memory_map;
475b94046eSArd Biesheuvel 	struct efi_boot_memmap map;
485b94046eSArd Biesheuvel 	efi_status_t status;
495b94046eSArd Biesheuvel 	bool ret = false;
505b94046eSArd Biesheuvel 	int map_offset;
515b94046eSArd Biesheuvel 
525b94046eSArd Biesheuvel 	map.map =	&memory_map;
535b94046eSArd Biesheuvel 	map.map_size =	&map_size;
545b94046eSArd Biesheuvel 	map.desc_size =	&desc_size;
555b94046eSArd Biesheuvel 	map.desc_ver =	NULL;
565b94046eSArd Biesheuvel 	map.key_ptr =	NULL;
575b94046eSArd Biesheuvel 	map.buff_size =	&buff_size;
585b94046eSArd Biesheuvel 
595b94046eSArd Biesheuvel 	status = efi_get_memory_map(&map);
605b94046eSArd Biesheuvel 	if (status != EFI_SUCCESS)
615b94046eSArd Biesheuvel 		return false;
625b94046eSArd Biesheuvel 
635b94046eSArd Biesheuvel 	for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
645b94046eSArd Biesheuvel 		efi_memory_desc_t *md = (void *)memory_map + map_offset;
655b94046eSArd Biesheuvel 		u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
665b94046eSArd Biesheuvel 
675b94046eSArd Biesheuvel 		/*
685b94046eSArd Biesheuvel 		 * Find the region that covers base, and return whether
695b94046eSArd Biesheuvel 		 * it covers base+size bytes.
705b94046eSArd Biesheuvel 		 */
715b94046eSArd Biesheuvel 		if (base >= md->phys_addr && base < end) {
725b94046eSArd Biesheuvel 			ret = (base + size) <= end;
735b94046eSArd Biesheuvel 			break;
745b94046eSArd Biesheuvel 		}
755b94046eSArd Biesheuvel 	}
765b94046eSArd Biesheuvel 
775b94046eSArd Biesheuvel 	efi_bs_call(free_pool, memory_map);
785b94046eSArd Biesheuvel 
795b94046eSArd Biesheuvel 	return ret;
805b94046eSArd Biesheuvel }
815b94046eSArd Biesheuvel 
82cd33a5c1SArd Biesheuvel efi_status_t handle_kernel_image(unsigned long *image_addr,
83bf457786SArd Biesheuvel 				 unsigned long *image_size,
84bf457786SArd Biesheuvel 				 unsigned long *reserve_addr,
85bf457786SArd Biesheuvel 				 unsigned long *reserve_size,
86bf457786SArd Biesheuvel 				 efi_loaded_image_t *image)
87bf457786SArd Biesheuvel {
88bf457786SArd Biesheuvel 	efi_status_t status;
89bf457786SArd Biesheuvel 	unsigned long kernel_size, kernel_memsize = 0;
905d12da9dSArd Biesheuvel 	u32 phys_seed = 0;
912b5fe07aSArd Biesheuvel 
923a262423SArd Biesheuvel 	/*
933a262423SArd Biesheuvel 	 * Although relocatable kernels can fix up the misalignment with
943a262423SArd Biesheuvel 	 * respect to MIN_KIMG_ALIGN, the resulting virtual text addresses are
953a262423SArd Biesheuvel 	 * subtly out of sync with those recorded in the vmlinux when kaslr is
963a262423SArd Biesheuvel 	 * disabled but the image required relocation anyway. Therefore retain
973a262423SArd Biesheuvel 	 * 2M alignment if KASLR was explicitly disabled, even if it was not
983a262423SArd Biesheuvel 	 * going to be activated to begin with.
993a262423SArd Biesheuvel 	 */
1003a262423SArd Biesheuvel 	u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
1013a262423SArd Biesheuvel 
1022b5fe07aSArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
103980771f6SArd Biesheuvel 		if (!efi_nokaslr) {
104cd33a5c1SArd Biesheuvel 			status = efi_get_random_bytes(sizeof(phys_seed),
1052b5fe07aSArd Biesheuvel 						      (u8 *)&phys_seed);
1062b5fe07aSArd Biesheuvel 			if (status == EFI_NOT_FOUND) {
1071c761ee9SMark Brown 				efi_info("EFI_RNG_PROTOCOL unavailable\n");
108d32de913SArd Biesheuvel 				efi_nokaslr = true;
1092b5fe07aSArd Biesheuvel 			} else if (status != EFI_SUCCESS) {
1101c761ee9SMark Brown 				efi_err("efi_get_random_bytes() failed (0x%lx)\n",
111d32de913SArd Biesheuvel 					status);
112d32de913SArd Biesheuvel 				efi_nokaslr = true;
1132b5fe07aSArd Biesheuvel 			}
1142b5fe07aSArd Biesheuvel 		} else {
115793473c2SArvind Sankar 			efi_info("KASLR disabled on kernel command line\n");
1162b5fe07aSArd Biesheuvel 		}
1172b5fe07aSArd Biesheuvel 	}
1182dc10ad8SLinus Torvalds 
11982046702SArd Biesheuvel 	if (image->image_base != _text)
120793473c2SArvind Sankar 		efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
121bf457786SArd Biesheuvel 
122*e9b7c3a4SMihai Carabas 	if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
123*e9b7c3a4SMihai Carabas 		efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
124*e9b7c3a4SMihai Carabas 			SEGMENT_ALIGN >> 10);
125c32ac11dSArd Biesheuvel 
126bf457786SArd Biesheuvel 	kernel_size = _edata - _text;
127bf457786SArd Biesheuvel 	kernel_memsize = kernel_size + (_end - _edata);
128120dc60dSArd Biesheuvel 	*reserve_size = kernel_memsize;
129bf457786SArd Biesheuvel 
1302b5fe07aSArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
131bf457786SArd Biesheuvel 		/*
1322b5fe07aSArd Biesheuvel 		 * If KASLR is enabled, and we have some randomness available,
1332b5fe07aSArd Biesheuvel 		 * locate the kernel at a randomized offset in physical memory.
1342b5fe07aSArd Biesheuvel 		 */
1353a262423SArd Biesheuvel 		status = efi_random_alloc(*reserve_size, min_kimg_align,
1365d12da9dSArd Biesheuvel 					  reserve_addr, phys_seed);
137ff80ef5bSArd Biesheuvel 		if (status != EFI_SUCCESS)
138ff80ef5bSArd Biesheuvel 			efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
1392b5fe07aSArd Biesheuvel 	} else {
14082046702SArd Biesheuvel 		status = EFI_OUT_OF_RESOURCES;
1412b5fe07aSArd Biesheuvel 	}
1422b5fe07aSArd Biesheuvel 
143bf457786SArd Biesheuvel 	if (status != EFI_SUCCESS) {
1445b94046eSArd Biesheuvel 		if (!check_image_region((u64)_text, kernel_memsize)) {
1455b94046eSArd Biesheuvel 			efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
1463a262423SArd Biesheuvel 		} else if (IS_ALIGNED((u64)_text, min_kimg_align)) {
14782046702SArd Biesheuvel 			/*
14882046702SArd Biesheuvel 			 * Just execute from wherever we were loaded by the
14982046702SArd Biesheuvel 			 * UEFI PE/COFF loader if the alignment is suitable.
15082046702SArd Biesheuvel 			 */
15182046702SArd Biesheuvel 			*image_addr = (u64)_text;
15282046702SArd Biesheuvel 			*reserve_size = 0;
15382046702SArd Biesheuvel 			return EFI_SUCCESS;
15482046702SArd Biesheuvel 		}
15582046702SArd Biesheuvel 
156e71356feSArd Biesheuvel 		status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
1573a262423SArd Biesheuvel 						    ULONG_MAX, min_kimg_align);
158bf457786SArd Biesheuvel 
159bf457786SArd Biesheuvel 		if (status != EFI_SUCCESS) {
160793473c2SArvind Sankar 			efi_err("Failed to relocate kernel\n");
1612b5fe07aSArd Biesheuvel 			*reserve_size = 0;
162bf457786SArd Biesheuvel 			return status;
163bf457786SArd Biesheuvel 		}
164bf457786SArd Biesheuvel 	}
165c2136dceSArd Biesheuvel 
166120dc60dSArd Biesheuvel 	*image_addr = *reserve_addr;
167c2136dceSArd Biesheuvel 	memcpy((void *)*image_addr, _text, kernel_size);
168bf457786SArd Biesheuvel 
169bf457786SArd Biesheuvel 	return EFI_SUCCESS;
170bf457786SArd Biesheuvel }
171