1bf457786SArd Biesheuvel /*
2bf457786SArd Biesheuvel  * Copyright (C) 2013, 2014 Linaro Ltd;  <roy.franz@linaro.org>
3bf457786SArd Biesheuvel  *
4bf457786SArd Biesheuvel  * This file implements the EFI boot stub for the arm64 kernel.
5bf457786SArd Biesheuvel  * Adapted from ARM version by Mark Salter <msalter@redhat.com>
6bf457786SArd Biesheuvel  *
7bf457786SArd Biesheuvel  * This program is free software; you can redistribute it and/or modify
8bf457786SArd Biesheuvel  * it under the terms of the GNU General Public License version 2 as
9bf457786SArd Biesheuvel  * published by the Free Software Foundation.
10bf457786SArd Biesheuvel  *
11bf457786SArd Biesheuvel  */
12bf457786SArd Biesheuvel #include <linux/efi.h>
13bf457786SArd Biesheuvel #include <asm/efi.h>
14170976bcSMark Rutland #include <asm/memory.h>
15bf457786SArd Biesheuvel #include <asm/sections.h>
1642b55734SArd Biesheuvel #include <asm/sysreg.h>
17bf457786SArd Biesheuvel 
182b5fe07aSArd Biesheuvel #include "efistub.h"
192b5fe07aSArd Biesheuvel 
2042b55734SArd Biesheuvel efi_status_t check_platform_features(efi_system_table_t *sys_table_arg)
2142b55734SArd Biesheuvel {
2242b55734SArd Biesheuvel 	u64 tg;
2342b55734SArd Biesheuvel 
2442b55734SArd Biesheuvel 	/* UEFI mandates support for 4 KB granularity, no need to check */
2542b55734SArd Biesheuvel 	if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
2642b55734SArd Biesheuvel 		return EFI_SUCCESS;
2742b55734SArd Biesheuvel 
2842b55734SArd Biesheuvel 	tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf;
2942b55734SArd Biesheuvel 	if (tg != ID_AA64MMFR0_TGRAN_SUPPORTED) {
3042b55734SArd Biesheuvel 		if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
3142b55734SArd Biesheuvel 			pr_efi_err(sys_table_arg, "This 64 KB granular kernel is not supported by your CPU\n");
3242b55734SArd Biesheuvel 		else
3342b55734SArd Biesheuvel 			pr_efi_err(sys_table_arg, "This 16 KB granular kernel is not supported by your CPU\n");
3442b55734SArd Biesheuvel 		return EFI_UNSUPPORTED;
3542b55734SArd Biesheuvel 	}
3642b55734SArd Biesheuvel 	return EFI_SUCCESS;
3742b55734SArd Biesheuvel }
38bf457786SArd Biesheuvel 
39dae31fd2SArd Biesheuvel efi_status_t handle_kernel_image(efi_system_table_t *sys_table_arg,
40bf457786SArd Biesheuvel 				 unsigned long *image_addr,
41bf457786SArd Biesheuvel 				 unsigned long *image_size,
42bf457786SArd Biesheuvel 				 unsigned long *reserve_addr,
43bf457786SArd Biesheuvel 				 unsigned long *reserve_size,
44bf457786SArd Biesheuvel 				 unsigned long dram_base,
45bf457786SArd Biesheuvel 				 efi_loaded_image_t *image)
46bf457786SArd Biesheuvel {
47bf457786SArd Biesheuvel 	efi_status_t status;
48bf457786SArd Biesheuvel 	unsigned long kernel_size, kernel_memsize = 0;
49bf457786SArd Biesheuvel 	void *old_image_addr = (void *)*image_addr;
502dc10ad8SLinus Torvalds 	unsigned long preferred_offset;
512b5fe07aSArd Biesheuvel 	u64 phys_seed = 0;
522b5fe07aSArd Biesheuvel 
532b5fe07aSArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
5460f38de7SArd Biesheuvel 		if (!nokaslr()) {
552b5fe07aSArd Biesheuvel 			status = efi_get_random_bytes(sys_table_arg,
562b5fe07aSArd Biesheuvel 						      sizeof(phys_seed),
572b5fe07aSArd Biesheuvel 						      (u8 *)&phys_seed);
582b5fe07aSArd Biesheuvel 			if (status == EFI_NOT_FOUND) {
592b5fe07aSArd Biesheuvel 				pr_efi(sys_table_arg, "EFI_RNG_PROTOCOL unavailable, no randomness supplied\n");
602b5fe07aSArd Biesheuvel 			} else if (status != EFI_SUCCESS) {
612b5fe07aSArd Biesheuvel 				pr_efi_err(sys_table_arg, "efi_get_random_bytes() failed\n");
622b5fe07aSArd Biesheuvel 				return status;
632b5fe07aSArd Biesheuvel 			}
642b5fe07aSArd Biesheuvel 		} else {
652b5fe07aSArd Biesheuvel 			pr_efi(sys_table_arg, "KASLR disabled on kernel command line\n");
662b5fe07aSArd Biesheuvel 		}
672b5fe07aSArd Biesheuvel 	}
682dc10ad8SLinus Torvalds 
692dc10ad8SLinus Torvalds 	/*
702dc10ad8SLinus Torvalds 	 * The preferred offset of the kernel Image is TEXT_OFFSET bytes beyond
712dc10ad8SLinus Torvalds 	 * a 2 MB aligned base, which itself may be lower than dram_base, as
722dc10ad8SLinus Torvalds 	 * long as the resulting offset equals or exceeds it.
732dc10ad8SLinus Torvalds 	 */
742b5fe07aSArd Biesheuvel 	preferred_offset = round_down(dram_base, MIN_KIMG_ALIGN) + TEXT_OFFSET;
752dc10ad8SLinus Torvalds 	if (preferred_offset < dram_base)
762b5fe07aSArd Biesheuvel 		preferred_offset += MIN_KIMG_ALIGN;
77bf457786SArd Biesheuvel 
78bf457786SArd Biesheuvel 	kernel_size = _edata - _text;
79bf457786SArd Biesheuvel 	kernel_memsize = kernel_size + (_end - _edata);
80bf457786SArd Biesheuvel 
812b5fe07aSArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
82bf457786SArd Biesheuvel 		/*
836f26b367SArd Biesheuvel 		 * If CONFIG_DEBUG_ALIGN_RODATA is not set, produce a
846f26b367SArd Biesheuvel 		 * displacement in the interval [0, MIN_KIMG_ALIGN) that
85170976bcSMark Rutland 		 * doesn't violate this kernel's de-facto alignment
86170976bcSMark Rutland 		 * constraints.
876f26b367SArd Biesheuvel 		 */
88170976bcSMark Rutland 		u32 mask = (MIN_KIMG_ALIGN - 1) & ~(EFI_KIMG_ALIGN - 1);
896f26b367SArd Biesheuvel 		u32 offset = !IS_ENABLED(CONFIG_DEBUG_ALIGN_RODATA) ?
906f26b367SArd Biesheuvel 			     (phys_seed >> 32) & mask : TEXT_OFFSET;
916f26b367SArd Biesheuvel 
926f26b367SArd Biesheuvel 		/*
932b5fe07aSArd Biesheuvel 		 * If KASLR is enabled, and we have some randomness available,
942b5fe07aSArd Biesheuvel 		 * locate the kernel at a randomized offset in physical memory.
952b5fe07aSArd Biesheuvel 		 */
966f26b367SArd Biesheuvel 		*reserve_size = kernel_memsize + offset;
972b5fe07aSArd Biesheuvel 		status = efi_random_alloc(sys_table_arg, *reserve_size,
982b5fe07aSArd Biesheuvel 					  MIN_KIMG_ALIGN, reserve_addr,
996f26b367SArd Biesheuvel 					  (u32)phys_seed);
1002b5fe07aSArd Biesheuvel 
1016f26b367SArd Biesheuvel 		*image_addr = *reserve_addr + offset;
1022b5fe07aSArd Biesheuvel 	} else {
1032b5fe07aSArd Biesheuvel 		/*
1042b5fe07aSArd Biesheuvel 		 * Else, try a straight allocation at the preferred offset.
105bf457786SArd Biesheuvel 		 * This will work around the issue where, if dram_base == 0x0,
106bf457786SArd Biesheuvel 		 * efi_low_alloc() refuses to allocate at 0x0 (to prevent the
107bf457786SArd Biesheuvel 		 * address of the allocation to be mistaken for a FAIL return
108bf457786SArd Biesheuvel 		 * value or a NULL pointer). It will also ensure that, on
109bf457786SArd Biesheuvel 		 * platforms where the [dram_base, dram_base + TEXT_OFFSET)
110bf457786SArd Biesheuvel 		 * interval is partially occupied by the firmware (like on APM
111bf457786SArd Biesheuvel 		 * Mustang), we can still place the kernel at the address
112bf457786SArd Biesheuvel 		 * 'dram_base + TEXT_OFFSET'.
113bf457786SArd Biesheuvel 		 */
1142b5fe07aSArd Biesheuvel 		if (*image_addr == preferred_offset)
1152b5fe07aSArd Biesheuvel 			return EFI_SUCCESS;
1162b5fe07aSArd Biesheuvel 
1172dc10ad8SLinus Torvalds 		*image_addr = *reserve_addr = preferred_offset;
1182b5fe07aSArd Biesheuvel 		*reserve_size = round_up(kernel_memsize, EFI_ALLOC_ALIGN);
1192b5fe07aSArd Biesheuvel 
120bf457786SArd Biesheuvel 		status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,
1212b5fe07aSArd Biesheuvel 					EFI_LOADER_DATA,
1222b5fe07aSArd Biesheuvel 					*reserve_size / EFI_PAGE_SIZE,
123bf457786SArd Biesheuvel 					(efi_physical_addr_t *)reserve_addr);
1242b5fe07aSArd Biesheuvel 	}
1252b5fe07aSArd Biesheuvel 
126bf457786SArd Biesheuvel 	if (status != EFI_SUCCESS) {
1272b5fe07aSArd Biesheuvel 		*reserve_size = kernel_memsize + TEXT_OFFSET;
1282b5fe07aSArd Biesheuvel 		status = efi_low_alloc(sys_table_arg, *reserve_size,
1292b5fe07aSArd Biesheuvel 				       MIN_KIMG_ALIGN, reserve_addr);
130bf457786SArd Biesheuvel 
131bf457786SArd Biesheuvel 		if (status != EFI_SUCCESS) {
132bf457786SArd Biesheuvel 			pr_efi_err(sys_table_arg, "Failed to relocate kernel\n");
1332b5fe07aSArd Biesheuvel 			*reserve_size = 0;
134bf457786SArd Biesheuvel 			return status;
135bf457786SArd Biesheuvel 		}
136bf457786SArd Biesheuvel 		*image_addr = *reserve_addr + TEXT_OFFSET;
137bf457786SArd Biesheuvel 	}
138bf457786SArd Biesheuvel 	memcpy((void *)*image_addr, old_image_addr, kernel_size);
139bf457786SArd Biesheuvel 
140bf457786SArd Biesheuvel 	return EFI_SUCCESS;
141bf457786SArd Biesheuvel }
142