18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2013, 2014 Linaro Ltd;  <roy.franz@linaro.org>
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This file implements the EFI boot stub for the arm64 kernel.
68c2ecf20Sopenharmony_ci * Adapted from ARM version by Mark Salter <msalter@redhat.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/efi.h>
118c2ecf20Sopenharmony_ci#include <asm/efi.h>
128c2ecf20Sopenharmony_ci#include <asm/memory.h>
138c2ecf20Sopenharmony_ci#include <asm/sections.h>
148c2ecf20Sopenharmony_ci#include <asm/sysreg.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "efistub.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciefi_status_t check_platform_features(void)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	u64 tg;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	/* UEFI mandates support for 4 KB granularity, no need to check */
238c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
248c2ecf20Sopenharmony_ci		return EFI_SUCCESS;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf;
278c2ecf20Sopenharmony_ci	if (tg < ID_AA64MMFR0_TGRAN_SUPPORTED_MIN || tg > ID_AA64MMFR0_TGRAN_SUPPORTED_MAX) {
288c2ecf20Sopenharmony_ci		if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
298c2ecf20Sopenharmony_ci			efi_err("This 64 KB granular kernel is not supported by your CPU\n");
308c2ecf20Sopenharmony_ci		else
318c2ecf20Sopenharmony_ci			efi_err("This 16 KB granular kernel is not supported by your CPU\n");
328c2ecf20Sopenharmony_ci		return EFI_UNSUPPORTED;
338c2ecf20Sopenharmony_ci	}
348c2ecf20Sopenharmony_ci	return EFI_SUCCESS;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
398c2ecf20Sopenharmony_ci * to provide space, and fail to zero it). Check for this condition by double
408c2ecf20Sopenharmony_ci * checking that the first and the last byte of the image are covered by the
418c2ecf20Sopenharmony_ci * same EFI memory map entry.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_cistatic bool check_image_region(u64 base, u64 size)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct efi_boot_memmap *map;
468c2ecf20Sopenharmony_ci	efi_status_t status;
478c2ecf20Sopenharmony_ci	bool ret = false;
488c2ecf20Sopenharmony_ci	int map_offset;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	status = efi_get_memory_map(&map, false);
518c2ecf20Sopenharmony_ci	if (status != EFI_SUCCESS)
528c2ecf20Sopenharmony_ci		return false;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
558c2ecf20Sopenharmony_ci		efi_memory_desc_t *md = (void *)map->map + map_offset;
568c2ecf20Sopenharmony_ci		u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci		/*
598c2ecf20Sopenharmony_ci		 * Find the region that covers base, and return whether
608c2ecf20Sopenharmony_ci		 * it covers base+size bytes.
618c2ecf20Sopenharmony_ci		 */
628c2ecf20Sopenharmony_ci		if (base >= md->phys_addr && base < end) {
638c2ecf20Sopenharmony_ci			ret = (base + size) <= end;
648c2ecf20Sopenharmony_ci			break;
658c2ecf20Sopenharmony_ci		}
668c2ecf20Sopenharmony_ci	}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	efi_bs_call(free_pool, map);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return ret;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciefi_status_t handle_kernel_image(unsigned long *image_addr,
748c2ecf20Sopenharmony_ci				 unsigned long *image_size,
758c2ecf20Sopenharmony_ci				 unsigned long *reserve_addr,
768c2ecf20Sopenharmony_ci				 unsigned long *reserve_size,
778c2ecf20Sopenharmony_ci				 efi_loaded_image_t *image)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	efi_status_t status;
808c2ecf20Sopenharmony_ci	unsigned long kernel_size, kernel_memsize = 0;
818c2ecf20Sopenharmony_ci	u32 phys_seed = 0;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/*
848c2ecf20Sopenharmony_ci	 * Although relocatable kernels can fix up the misalignment with
858c2ecf20Sopenharmony_ci	 * respect to MIN_KIMG_ALIGN, the resulting virtual text addresses are
868c2ecf20Sopenharmony_ci	 * subtly out of sync with those recorded in the vmlinux when kaslr is
878c2ecf20Sopenharmony_ci	 * disabled but the image required relocation anyway. Therefore retain
888c2ecf20Sopenharmony_ci	 * 2M alignment if KASLR was explicitly disabled, even if it was not
898c2ecf20Sopenharmony_ci	 * going to be activated to begin with.
908c2ecf20Sopenharmony_ci	 */
918c2ecf20Sopenharmony_ci	u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
948c2ecf20Sopenharmony_ci		if (!efi_nokaslr) {
958c2ecf20Sopenharmony_ci			status = efi_get_random_bytes(sizeof(phys_seed),
968c2ecf20Sopenharmony_ci						      (u8 *)&phys_seed);
978c2ecf20Sopenharmony_ci			if (status == EFI_NOT_FOUND) {
988c2ecf20Sopenharmony_ci				efi_info("EFI_RNG_PROTOCOL unavailable, KASLR will be disabled\n");
998c2ecf20Sopenharmony_ci				efi_nokaslr = true;
1008c2ecf20Sopenharmony_ci			} else if (status != EFI_SUCCESS) {
1018c2ecf20Sopenharmony_ci				efi_err("efi_get_random_bytes() failed (0x%lx), KASLR will be disabled\n",
1028c2ecf20Sopenharmony_ci					status);
1038c2ecf20Sopenharmony_ci				efi_nokaslr = true;
1048c2ecf20Sopenharmony_ci			}
1058c2ecf20Sopenharmony_ci		} else {
1068c2ecf20Sopenharmony_ci			efi_info("KASLR disabled on kernel command line\n");
1078c2ecf20Sopenharmony_ci		}
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (image->image_base != _text)
1118c2ecf20Sopenharmony_ci		efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
1148c2ecf20Sopenharmony_ci		efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
1158c2ecf20Sopenharmony_ci			SEGMENT_ALIGN >> 10);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	kernel_size = _edata - _text;
1188c2ecf20Sopenharmony_ci	kernel_memsize = kernel_size + (_end - _edata);
1198c2ecf20Sopenharmony_ci	*reserve_size = kernel_memsize;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
1228c2ecf20Sopenharmony_ci		/*
1238c2ecf20Sopenharmony_ci		 * If KASLR is enabled, and we have some randomness available,
1248c2ecf20Sopenharmony_ci		 * locate the kernel at a randomized offset in physical memory.
1258c2ecf20Sopenharmony_ci		 */
1268c2ecf20Sopenharmony_ci		status = efi_random_alloc(*reserve_size, min_kimg_align,
1278c2ecf20Sopenharmony_ci					  reserve_addr, phys_seed);
1288c2ecf20Sopenharmony_ci	} else {
1298c2ecf20Sopenharmony_ci		status = EFI_OUT_OF_RESOURCES;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	if (status != EFI_SUCCESS) {
1338c2ecf20Sopenharmony_ci		if (!check_image_region((u64)_text, kernel_memsize)) {
1348c2ecf20Sopenharmony_ci			efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
1358c2ecf20Sopenharmony_ci		} else if (IS_ALIGNED((u64)_text, min_kimg_align)) {
1368c2ecf20Sopenharmony_ci			/*
1378c2ecf20Sopenharmony_ci			 * Just execute from wherever we were loaded by the
1388c2ecf20Sopenharmony_ci			 * UEFI PE/COFF loader if the alignment is suitable.
1398c2ecf20Sopenharmony_ci			 */
1408c2ecf20Sopenharmony_ci			*image_addr = (u64)_text;
1418c2ecf20Sopenharmony_ci			*reserve_size = 0;
1428c2ecf20Sopenharmony_ci			return EFI_SUCCESS;
1438c2ecf20Sopenharmony_ci		}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
1468c2ecf20Sopenharmony_ci						    ULONG_MAX, min_kimg_align);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci		if (status != EFI_SUCCESS) {
1498c2ecf20Sopenharmony_ci			efi_err("Failed to relocate kernel\n");
1508c2ecf20Sopenharmony_ci			*reserve_size = 0;
1518c2ecf20Sopenharmony_ci			return status;
1528c2ecf20Sopenharmony_ci		}
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	*image_addr = *reserve_addr;
1568c2ecf20Sopenharmony_ci	memcpy((void *)*image_addr, _text, kernel_size);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	return EFI_SUCCESS;
1598c2ecf20Sopenharmony_ci}
160