1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2013, 2014 Linaro Ltd;  <roy.franz@linaro.org>
4  *
5  * This file implements the EFI boot stub for the arm64 kernel.
6  * Adapted from ARM version by Mark Salter <msalter@redhat.com>
7  */
8 
9 
10 #include <linux/efi.h>
11 #include <asm/efi.h>
12 #include <asm/memory.h>
13 #include <asm/sections.h>
14 #include <asm/sysreg.h>
15 
16 #include "efistub.h"
17 
18 efi_status_t check_platform_features(void)
19 {
20 	u64 tg;
21 
22 	/* UEFI mandates support for 4 KB granularity, no need to check */
23 	if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
24 		return EFI_SUCCESS;
25 
26 	tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf;
27 	if (tg != ID_AA64MMFR0_TGRAN_SUPPORTED) {
28 		if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
29 			efi_err("This 64 KB granular kernel is not supported by your CPU\n");
30 		else
31 			efi_err("This 16 KB granular kernel is not supported by your CPU\n");
32 		return EFI_UNSUPPORTED;
33 	}
34 	return EFI_SUCCESS;
35 }
36 
37 /*
38  * Although relocatable kernels can fix up the misalignment with respect to
39  * MIN_KIMG_ALIGN, the resulting virtual text addresses are subtly out of
40  * sync with those recorded in the vmlinux when kaslr is disabled but the
41  * image required relocation anyway. Therefore retain 2M alignment unless
42  * KASLR is in use.
43  */
44 static u64 min_kimg_align(void)
45 {
46 	return efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
47 }
48 
49 efi_status_t handle_kernel_image(unsigned long *image_addr,
50 				 unsigned long *image_size,
51 				 unsigned long *reserve_addr,
52 				 unsigned long *reserve_size,
53 				 unsigned long dram_base,
54 				 efi_loaded_image_t *image)
55 {
56 	efi_status_t status;
57 	unsigned long kernel_size, kernel_memsize = 0;
58 	u32 phys_seed = 0;
59 
60 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
61 		if (!efi_nokaslr) {
62 			status = efi_get_random_bytes(sizeof(phys_seed),
63 						      (u8 *)&phys_seed);
64 			if (status == EFI_NOT_FOUND) {
65 				efi_info("EFI_RNG_PROTOCOL unavailable, no randomness supplied\n");
66 			} else if (status != EFI_SUCCESS) {
67 				efi_err("efi_get_random_bytes() failed\n");
68 				return status;
69 			}
70 		} else {
71 			efi_info("KASLR disabled on kernel command line\n");
72 		}
73 	}
74 
75 	if (image->image_base != _text)
76 		efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
77 
78 	kernel_size = _edata - _text;
79 	kernel_memsize = kernel_size + (_end - _edata);
80 	*reserve_size = kernel_memsize + TEXT_OFFSET % min_kimg_align();
81 
82 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
83 		/*
84 		 * If KASLR is enabled, and we have some randomness available,
85 		 * locate the kernel at a randomized offset in physical memory.
86 		 */
87 		status = efi_random_alloc(*reserve_size, min_kimg_align(),
88 					  reserve_addr, phys_seed);
89 	} else {
90 		status = EFI_OUT_OF_RESOURCES;
91 	}
92 
93 	if (status != EFI_SUCCESS) {
94 		if (IS_ALIGNED((u64)_text - TEXT_OFFSET, min_kimg_align())) {
95 			/*
96 			 * Just execute from wherever we were loaded by the
97 			 * UEFI PE/COFF loader if the alignment is suitable.
98 			 */
99 			*image_addr = (u64)_text;
100 			*reserve_size = 0;
101 			return EFI_SUCCESS;
102 		}
103 
104 		status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
105 						    ULONG_MAX, min_kimg_align());
106 
107 		if (status != EFI_SUCCESS) {
108 			efi_err("Failed to relocate kernel\n");
109 			*reserve_size = 0;
110 			return status;
111 		}
112 	}
113 
114 	*image_addr = *reserve_addr + TEXT_OFFSET % min_kimg_align();
115 	memcpy((void *)*image_addr, _text, kernel_size);
116 
117 	return EFI_SUCCESS;
118 }
119