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  */
120426a4e6SArd Biesheuvel 
130426a4e6SArd Biesheuvel /*
140426a4e6SArd Biesheuvel  * To prevent the compiler from emitting GOT-indirected (and thus absolute)
150426a4e6SArd Biesheuvel  * references to the section markers, override their visibility as 'hidden'
160426a4e6SArd Biesheuvel  */
170426a4e6SArd Biesheuvel #pragma GCC visibility push(hidden)
180426a4e6SArd Biesheuvel #include <asm/sections.h>
190426a4e6SArd Biesheuvel #pragma GCC visibility pop
200426a4e6SArd Biesheuvel 
21bf457786SArd Biesheuvel #include <linux/efi.h>
22bf457786SArd Biesheuvel #include <asm/efi.h>
2342b55734SArd Biesheuvel #include <asm/sysreg.h>
24bf457786SArd Biesheuvel 
252b5fe07aSArd Biesheuvel #include "efistub.h"
262b5fe07aSArd Biesheuvel 
2742b55734SArd Biesheuvel efi_status_t check_platform_features(efi_system_table_t *sys_table_arg)
2842b55734SArd Biesheuvel {
2942b55734SArd Biesheuvel 	u64 tg;
3042b55734SArd Biesheuvel 
3142b55734SArd Biesheuvel 	/* UEFI mandates support for 4 KB granularity, no need to check */
3242b55734SArd Biesheuvel 	if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
3342b55734SArd Biesheuvel 		return EFI_SUCCESS;
3442b55734SArd Biesheuvel 
3542b55734SArd Biesheuvel 	tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf;
3642b55734SArd Biesheuvel 	if (tg != ID_AA64MMFR0_TGRAN_SUPPORTED) {
3742b55734SArd Biesheuvel 		if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
3842b55734SArd Biesheuvel 			pr_efi_err(sys_table_arg, "This 64 KB granular kernel is not supported by your CPU\n");
3942b55734SArd Biesheuvel 		else
4042b55734SArd Biesheuvel 			pr_efi_err(sys_table_arg, "This 16 KB granular kernel is not supported by your CPU\n");
4142b55734SArd Biesheuvel 		return EFI_UNSUPPORTED;
4242b55734SArd Biesheuvel 	}
4342b55734SArd Biesheuvel 	return EFI_SUCCESS;
4442b55734SArd Biesheuvel }
45bf457786SArd Biesheuvel 
46dae31fd2SArd Biesheuvel efi_status_t handle_kernel_image(efi_system_table_t *sys_table_arg,
47bf457786SArd Biesheuvel 				 unsigned long *image_addr,
48bf457786SArd Biesheuvel 				 unsigned long *image_size,
49bf457786SArd Biesheuvel 				 unsigned long *reserve_addr,
50bf457786SArd Biesheuvel 				 unsigned long *reserve_size,
51bf457786SArd Biesheuvel 				 unsigned long dram_base,
52bf457786SArd Biesheuvel 				 efi_loaded_image_t *image)
53bf457786SArd Biesheuvel {
54bf457786SArd Biesheuvel 	efi_status_t status;
55bf457786SArd Biesheuvel 	unsigned long kernel_size, kernel_memsize = 0;
56bf457786SArd Biesheuvel 	void *old_image_addr = (void *)*image_addr;
572dc10ad8SLinus Torvalds 	unsigned long preferred_offset;
582b5fe07aSArd Biesheuvel 	u64 phys_seed = 0;
592b5fe07aSArd Biesheuvel 
602b5fe07aSArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
6160f38de7SArd Biesheuvel 		if (!nokaslr()) {
622b5fe07aSArd Biesheuvel 			status = efi_get_random_bytes(sys_table_arg,
632b5fe07aSArd Biesheuvel 						      sizeof(phys_seed),
642b5fe07aSArd Biesheuvel 						      (u8 *)&phys_seed);
652b5fe07aSArd Biesheuvel 			if (status == EFI_NOT_FOUND) {
662b5fe07aSArd Biesheuvel 				pr_efi(sys_table_arg, "EFI_RNG_PROTOCOL unavailable, no randomness supplied\n");
672b5fe07aSArd Biesheuvel 			} else if (status != EFI_SUCCESS) {
682b5fe07aSArd Biesheuvel 				pr_efi_err(sys_table_arg, "efi_get_random_bytes() failed\n");
692b5fe07aSArd Biesheuvel 				return status;
702b5fe07aSArd Biesheuvel 			}
712b5fe07aSArd Biesheuvel 		} else {
722b5fe07aSArd Biesheuvel 			pr_efi(sys_table_arg, "KASLR disabled on kernel command line\n");
732b5fe07aSArd Biesheuvel 		}
742b5fe07aSArd Biesheuvel 	}
752dc10ad8SLinus Torvalds 
762dc10ad8SLinus Torvalds 	/*
772dc10ad8SLinus Torvalds 	 * The preferred offset of the kernel Image is TEXT_OFFSET bytes beyond
782dc10ad8SLinus Torvalds 	 * a 2 MB aligned base, which itself may be lower than dram_base, as
792dc10ad8SLinus Torvalds 	 * long as the resulting offset equals or exceeds it.
802dc10ad8SLinus Torvalds 	 */
812b5fe07aSArd Biesheuvel 	preferred_offset = round_down(dram_base, MIN_KIMG_ALIGN) + TEXT_OFFSET;
822dc10ad8SLinus Torvalds 	if (preferred_offset < dram_base)
832b5fe07aSArd Biesheuvel 		preferred_offset += MIN_KIMG_ALIGN;
84bf457786SArd Biesheuvel 
85bf457786SArd Biesheuvel 	kernel_size = _edata - _text;
86bf457786SArd Biesheuvel 	kernel_memsize = kernel_size + (_end - _edata);
87bf457786SArd Biesheuvel 
882b5fe07aSArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
89bf457786SArd Biesheuvel 		/*
906f26b367SArd Biesheuvel 		 * If CONFIG_DEBUG_ALIGN_RODATA is not set, produce a
916f26b367SArd Biesheuvel 		 * displacement in the interval [0, MIN_KIMG_ALIGN) that
926f26b367SArd Biesheuvel 		 * is a multiple of the minimal segment alignment (SZ_64K)
936f26b367SArd Biesheuvel 		 */
946f26b367SArd Biesheuvel 		u32 mask = (MIN_KIMG_ALIGN - 1) & ~(SZ_64K - 1);
956f26b367SArd Biesheuvel 		u32 offset = !IS_ENABLED(CONFIG_DEBUG_ALIGN_RODATA) ?
966f26b367SArd Biesheuvel 			     (phys_seed >> 32) & mask : TEXT_OFFSET;
976f26b367SArd Biesheuvel 
986f26b367SArd Biesheuvel 		/*
992b5fe07aSArd Biesheuvel 		 * If KASLR is enabled, and we have some randomness available,
1002b5fe07aSArd Biesheuvel 		 * locate the kernel at a randomized offset in physical memory.
1012b5fe07aSArd Biesheuvel 		 */
1026f26b367SArd Biesheuvel 		*reserve_size = kernel_memsize + offset;
1032b5fe07aSArd Biesheuvel 		status = efi_random_alloc(sys_table_arg, *reserve_size,
1042b5fe07aSArd Biesheuvel 					  MIN_KIMG_ALIGN, reserve_addr,
1056f26b367SArd Biesheuvel 					  (u32)phys_seed);
1062b5fe07aSArd Biesheuvel 
1076f26b367SArd Biesheuvel 		*image_addr = *reserve_addr + offset;
1082b5fe07aSArd Biesheuvel 	} else {
1092b5fe07aSArd Biesheuvel 		/*
1102b5fe07aSArd Biesheuvel 		 * Else, try a straight allocation at the preferred offset.
111bf457786SArd Biesheuvel 		 * This will work around the issue where, if dram_base == 0x0,
112bf457786SArd Biesheuvel 		 * efi_low_alloc() refuses to allocate at 0x0 (to prevent the
113bf457786SArd Biesheuvel 		 * address of the allocation to be mistaken for a FAIL return
114bf457786SArd Biesheuvel 		 * value or a NULL pointer). It will also ensure that, on
115bf457786SArd Biesheuvel 		 * platforms where the [dram_base, dram_base + TEXT_OFFSET)
116bf457786SArd Biesheuvel 		 * interval is partially occupied by the firmware (like on APM
117bf457786SArd Biesheuvel 		 * Mustang), we can still place the kernel at the address
118bf457786SArd Biesheuvel 		 * 'dram_base + TEXT_OFFSET'.
119bf457786SArd Biesheuvel 		 */
1202b5fe07aSArd Biesheuvel 		if (*image_addr == preferred_offset)
1212b5fe07aSArd Biesheuvel 			return EFI_SUCCESS;
1222b5fe07aSArd Biesheuvel 
1232dc10ad8SLinus Torvalds 		*image_addr = *reserve_addr = preferred_offset;
1242b5fe07aSArd Biesheuvel 		*reserve_size = round_up(kernel_memsize, EFI_ALLOC_ALIGN);
1252b5fe07aSArd Biesheuvel 
126bf457786SArd Biesheuvel 		status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,
1272b5fe07aSArd Biesheuvel 					EFI_LOADER_DATA,
1282b5fe07aSArd Biesheuvel 					*reserve_size / EFI_PAGE_SIZE,
129bf457786SArd Biesheuvel 					(efi_physical_addr_t *)reserve_addr);
1302b5fe07aSArd Biesheuvel 	}
1312b5fe07aSArd Biesheuvel 
132bf457786SArd Biesheuvel 	if (status != EFI_SUCCESS) {
1332b5fe07aSArd Biesheuvel 		*reserve_size = kernel_memsize + TEXT_OFFSET;
1342b5fe07aSArd Biesheuvel 		status = efi_low_alloc(sys_table_arg, *reserve_size,
1352b5fe07aSArd Biesheuvel 				       MIN_KIMG_ALIGN, reserve_addr);
136bf457786SArd Biesheuvel 
137bf457786SArd Biesheuvel 		if (status != EFI_SUCCESS) {
138bf457786SArd Biesheuvel 			pr_efi_err(sys_table_arg, "Failed to relocate kernel\n");
1392b5fe07aSArd Biesheuvel 			*reserve_size = 0;
140bf457786SArd Biesheuvel 			return status;
141bf457786SArd Biesheuvel 		}
142bf457786SArd Biesheuvel 		*image_addr = *reserve_addr + TEXT_OFFSET;
143bf457786SArd Biesheuvel 	}
144bf457786SArd Biesheuvel 	memcpy((void *)*image_addr, old_image_addr, kernel_size);
145bf457786SArd Biesheuvel 
146bf457786SArd Biesheuvel 	return EFI_SUCCESS;
147bf457786SArd Biesheuvel }
148