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 	/*
23 	 * If we have 48 bits of VA space for TTBR0 mappings, we can map the
24 	 * UEFI runtime regions 1:1 and so calling SetVirtualAddressMap() is
25 	 * unnecessary.
26 	 */
27 	if (VA_BITS_MIN >= 48)
28 		efi_novamap = true;
29 
30 	/* UEFI mandates support for 4 KB granularity, no need to check */
31 	if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
32 		return EFI_SUCCESS;
33 
34 	tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_EL1_TGRAN_SHIFT) & 0xf;
35 	if (tg < ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MIN || tg > ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MAX) {
36 		if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
37 			efi_err("This 64 KB granular kernel is not supported by your CPU\n");
38 		else
39 			efi_err("This 16 KB granular kernel is not supported by your CPU\n");
40 		return EFI_UNSUPPORTED;
41 	}
42 	return EFI_SUCCESS;
43 }
44 
45 /*
46  * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
47  * to provide space, and fail to zero it). Check for this condition by double
48  * checking that the first and the last byte of the image are covered by the
49  * same EFI memory map entry.
50  */
51 static bool check_image_region(u64 base, u64 size)
52 {
53 	struct efi_boot_memmap *map;
54 	efi_status_t status;
55 	bool ret = false;
56 	int map_offset;
57 
58 	status = efi_get_memory_map(&map, false);
59 	if (status != EFI_SUCCESS)
60 		return false;
61 
62 	for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
63 		efi_memory_desc_t *md = (void *)map->map + map_offset;
64 		u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
65 
66 		/*
67 		 * Find the region that covers base, and return whether
68 		 * it covers base+size bytes.
69 		 */
70 		if (base >= md->phys_addr && base < end) {
71 			ret = (base + size) <= end;
72 			break;
73 		}
74 	}
75 
76 	efi_bs_call(free_pool, map);
77 
78 	return ret;
79 }
80 
81 efi_status_t handle_kernel_image(unsigned long *image_addr,
82 				 unsigned long *image_size,
83 				 unsigned long *reserve_addr,
84 				 unsigned long *reserve_size,
85 				 efi_loaded_image_t *image,
86 				 efi_handle_t image_handle)
87 {
88 	efi_status_t status;
89 	unsigned long kernel_size, kernel_memsize = 0;
90 	u32 phys_seed = 0;
91 
92 	/*
93 	 * Although relocatable kernels can fix up the misalignment with
94 	 * respect to MIN_KIMG_ALIGN, the resulting virtual text addresses are
95 	 * subtly out of sync with those recorded in the vmlinux when kaslr is
96 	 * disabled but the image required relocation anyway. Therefore retain
97 	 * 2M alignment if KASLR was explicitly disabled, even if it was not
98 	 * going to be activated to begin with.
99 	 */
100 	u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
101 
102 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
103 		efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID;
104 		void *p;
105 
106 		if (efi_nokaslr) {
107 			efi_info("KASLR disabled on kernel command line\n");
108 		} else if (efi_bs_call(handle_protocol, image_handle,
109 				       &li_fixed_proto, &p) == EFI_SUCCESS) {
110 			efi_info("Image placement fixed by loader\n");
111 		} else {
112 			status = efi_get_random_bytes(sizeof(phys_seed),
113 						      (u8 *)&phys_seed);
114 			if (status == EFI_NOT_FOUND) {
115 				efi_info("EFI_RNG_PROTOCOL unavailable\n");
116 				efi_nokaslr = true;
117 			} else if (status != EFI_SUCCESS) {
118 				efi_err("efi_get_random_bytes() failed (0x%lx)\n",
119 					status);
120 				efi_nokaslr = true;
121 			}
122 		}
123 	}
124 
125 	if (image->image_base != _text)
126 		efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
127 
128 	if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
129 		efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
130 			SEGMENT_ALIGN >> 10);
131 
132 	kernel_size = _edata - _text;
133 	kernel_memsize = kernel_size + (_end - _edata);
134 	*reserve_size = kernel_memsize;
135 
136 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
137 		/*
138 		 * If KASLR is enabled, and we have some randomness available,
139 		 * locate the kernel at a randomized offset in physical memory.
140 		 */
141 		status = efi_random_alloc(*reserve_size, min_kimg_align,
142 					  reserve_addr, phys_seed);
143 		if (status != EFI_SUCCESS)
144 			efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
145 	} else {
146 		status = EFI_OUT_OF_RESOURCES;
147 	}
148 
149 	if (status != EFI_SUCCESS) {
150 		if (!check_image_region((u64)_text, kernel_memsize)) {
151 			efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
152 		} else if (IS_ALIGNED((u64)_text, min_kimg_align)) {
153 			/*
154 			 * Just execute from wherever we were loaded by the
155 			 * UEFI PE/COFF loader if the alignment is suitable.
156 			 */
157 			*image_addr = (u64)_text;
158 			*reserve_size = 0;
159 			return EFI_SUCCESS;
160 		}
161 
162 		status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
163 						    ULONG_MAX, min_kimg_align);
164 
165 		if (status != EFI_SUCCESS) {
166 			efi_err("Failed to relocate kernel\n");
167 			*reserve_size = 0;
168 			return status;
169 		}
170 	}
171 
172 	*image_addr = *reserve_addr;
173 	memcpy((void *)*image_addr, _text, kernel_size);
174 
175 	return EFI_SUCCESS;
176 }
177