1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * EFI stub implementation that is shared by arm and arm64 architectures.
4 * This should be #included by the EFI stub implementation files.
5 *
6 * Copyright (C) 2013,2014 Linaro Limited
7 * Roy Franz <roy.franz@linaro.org
8 * Copyright (C) 2013 Red Hat, Inc.
9 * Mark Salter <msalter@redhat.com>
10 */
11
12 #include <linux/efi.h>
13 #include <asm/efi.h>
14
15 #include "efistub.h"
16
17 /*
18 * This is the base address at which to start allocating virtual memory ranges
19 * for UEFI Runtime Services.
20 *
21 * For ARM/ARM64:
22 * This is in the low TTBR0 range so that we can use
23 * any allocation we choose, and eliminate the risk of a conflict after kexec.
24 * The value chosen is the largest non-zero power of 2 suitable for this purpose
25 * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
26 * be mapped efficiently.
27 * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
28 * map everything below 1 GB. (512 MB is a reasonable upper bound for the
29 * entire footprint of the UEFI runtime services memory regions)
30 *
31 * For RISC-V:
32 * There is no specific reason for which, this address (512MB) can't be used
33 * EFI runtime virtual address for RISC-V. It also helps to use EFI runtime
34 * services on both RV32/RV64. Keep the same runtime virtual address for RISC-V
35 * as well to minimize the code churn.
36 */
37 #define EFI_RT_VIRTUAL_BASE SZ_512M
38
39 /*
40 * Some architectures map the EFI regions into the kernel's linear map using a
41 * fixed offset.
42 */
43 #ifndef EFI_RT_VIRTUAL_OFFSET
44 #define EFI_RT_VIRTUAL_OFFSET 0
45 #endif
46
47 static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
48 static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET != 0);
49
alloc_screen_info(void)50 struct screen_info * __weak alloc_screen_info(void)
51 {
52 return &screen_info;
53 }
54
free_screen_info(struct screen_info * si)55 void __weak free_screen_info(struct screen_info *si)
56 {
57 }
58
setup_graphics(void)59 static struct screen_info *setup_graphics(void)
60 {
61 efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
62 efi_status_t status;
63 unsigned long size;
64 void **gop_handle = NULL;
65 struct screen_info *si = NULL;
66
67 size = 0;
68 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
69 &gop_proto, NULL, &size, gop_handle);
70 if (status == EFI_BUFFER_TOO_SMALL) {
71 si = alloc_screen_info();
72 if (!si)
73 return NULL;
74 status = efi_setup_gop(si, &gop_proto, size);
75 if (status != EFI_SUCCESS) {
76 free_screen_info(si);
77 return NULL;
78 }
79 }
80 return si;
81 }
82
install_memreserve_table(void)83 static void install_memreserve_table(void)
84 {
85 struct linux_efi_memreserve *rsv;
86 efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
87 efi_status_t status;
88
89 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
90 (void **)&rsv);
91 if (status != EFI_SUCCESS) {
92 efi_err("Failed to allocate memreserve entry!\n");
93 return;
94 }
95
96 rsv->next = 0;
97 rsv->size = 0;
98 atomic_set(&rsv->count, 0);
99
100 status = efi_bs_call(install_configuration_table,
101 &memreserve_table_guid, rsv);
102 if (status != EFI_SUCCESS)
103 efi_err("Failed to install memreserve config table!\n");
104 }
105
get_supported_rt_services(void)106 static u32 get_supported_rt_services(void)
107 {
108 const efi_rt_properties_table_t *rt_prop_table;
109 u32 supported = EFI_RT_SUPPORTED_ALL;
110
111 rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID);
112 if (rt_prop_table)
113 supported &= rt_prop_table->runtime_services_supported;
114
115 return supported;
116 }
117
efi_handle_cmdline(efi_loaded_image_t * image,char ** cmdline_ptr)118 efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr)
119 {
120 int cmdline_size = 0;
121 efi_status_t status;
122 char *cmdline;
123
124 /*
125 * Get the command line from EFI, using the LOADED_IMAGE
126 * protocol. We are going to copy the command line into the
127 * device tree, so this can be allocated anywhere.
128 */
129 cmdline = efi_convert_cmdline(image, &cmdline_size);
130 if (!cmdline) {
131 efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n");
132 return EFI_OUT_OF_RESOURCES;
133 }
134
135 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
136 IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
137 cmdline_size == 0) {
138 status = efi_parse_options(CONFIG_CMDLINE);
139 if (status != EFI_SUCCESS) {
140 efi_err("Failed to parse options\n");
141 goto fail_free_cmdline;
142 }
143 }
144
145 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) {
146 status = efi_parse_options(cmdline);
147 if (status != EFI_SUCCESS) {
148 efi_err("Failed to parse options\n");
149 goto fail_free_cmdline;
150 }
151 }
152
153 *cmdline_ptr = cmdline;
154 return EFI_SUCCESS;
155
156 fail_free_cmdline:
157 efi_bs_call(free_pool, cmdline_ptr);
158 return status;
159 }
160
efi_stub_common(efi_handle_t handle,efi_loaded_image_t * image,unsigned long image_addr,char * cmdline_ptr)161 efi_status_t efi_stub_common(efi_handle_t handle,
162 efi_loaded_image_t *image,
163 unsigned long image_addr,
164 char *cmdline_ptr)
165 {
166 struct screen_info *si;
167 efi_status_t status;
168
169 status = check_platform_features();
170 if (status != EFI_SUCCESS)
171 return status;
172
173 si = setup_graphics();
174
175 efi_retrieve_tpm2_eventlog();
176
177 /* Ask the firmware to clear memory on unclean shutdown */
178 efi_enable_reset_attack_mitigation();
179
180 efi_load_initrd(image, ULONG_MAX, efi_get_max_initrd_addr(image_addr),
181 NULL);
182
183 efi_random_get_seed();
184
185 /* force efi_novamap if SetVirtualAddressMap() is unsupported */
186 efi_novamap |= !(get_supported_rt_services() &
187 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP);
188
189 install_memreserve_table();
190
191 status = efi_boot_kernel(handle, image, image_addr, cmdline_ptr);
192
193 free_screen_info(si);
194 return status;
195 }
196
197 /*
198 * efi_allocate_virtmap() - create a pool allocation for the virtmap
199 *
200 * Create an allocation that is of sufficient size to hold all the memory
201 * descriptors that will be passed to SetVirtualAddressMap() to inform the
202 * firmware about the virtual mapping that will be used under the OS to call
203 * into the firmware.
204 */
efi_alloc_virtmap(efi_memory_desc_t ** virtmap,unsigned long * desc_size,u32 * desc_ver)205 efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,
206 unsigned long *desc_size, u32 *desc_ver)
207 {
208 unsigned long size, mmap_key;
209 efi_status_t status;
210
211 /*
212 * Use the size of the current memory map as an upper bound for the
213 * size of the buffer we need to pass to SetVirtualAddressMap() to
214 * cover all EFI_MEMORY_RUNTIME regions.
215 */
216 size = 0;
217 status = efi_bs_call(get_memory_map, &size, NULL, &mmap_key, desc_size,
218 desc_ver);
219 if (status != EFI_BUFFER_TOO_SMALL)
220 return EFI_LOAD_ERROR;
221
222 return efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
223 (void **)virtmap);
224 }
225
226 /*
227 * efi_get_virtmap() - create a virtual mapping for the EFI memory map
228 *
229 * This function populates the virt_addr fields of all memory region descriptors
230 * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
231 * are also copied to @runtime_map, and their total count is returned in @count.
232 */
efi_get_virtmap(efi_memory_desc_t * memory_map,unsigned long map_size,unsigned long desc_size,efi_memory_desc_t * runtime_map,int * count)233 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
234 unsigned long desc_size, efi_memory_desc_t *runtime_map,
235 int *count)
236 {
237 u64 efi_virt_base = virtmap_base;
238 efi_memory_desc_t *in, *out = runtime_map;
239 int l;
240
241 *count = 0;
242
243 for (l = 0; l < map_size; l += desc_size) {
244 u64 paddr, size;
245
246 in = (void *)memory_map + l;
247 if (!(in->attribute & EFI_MEMORY_RUNTIME))
248 continue;
249
250 paddr = in->phys_addr;
251 size = in->num_pages * EFI_PAGE_SIZE;
252
253 in->virt_addr = in->phys_addr + EFI_RT_VIRTUAL_OFFSET;
254 if (efi_novamap) {
255 continue;
256 }
257
258 /*
259 * Make the mapping compatible with 64k pages: this allows
260 * a 4k page size kernel to kexec a 64k page size kernel and
261 * vice versa.
262 */
263 if (!flat_va_mapping) {
264
265 paddr = round_down(in->phys_addr, SZ_64K);
266 size += in->phys_addr - paddr;
267
268 /*
269 * Avoid wasting memory on PTEs by choosing a virtual
270 * base that is compatible with section mappings if this
271 * region has the appropriate size and physical
272 * alignment. (Sections are 2 MB on 4k granule kernels)
273 */
274 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
275 efi_virt_base = round_up(efi_virt_base, SZ_2M);
276 else
277 efi_virt_base = round_up(efi_virt_base, SZ_64K);
278
279 in->virt_addr += efi_virt_base - paddr;
280 efi_virt_base += size;
281 }
282
283 memcpy(out, in, desc_size);
284 out = (void *)out + desc_size;
285 ++*count;
286 }
287 }
288