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 <linux/sort.h>
14 #include <asm/efi.h>
15 
16 #include "efistub.h"
17 
18 /*
19  * This is the base address at which to start allocating virtual memory ranges
20  * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use
21  * any allocation we choose, and eliminate the risk of a conflict after kexec.
22  * The value chosen is the largest non-zero power of 2 suitable for this purpose
23  * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
24  * be mapped efficiently.
25  * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
26  * map everything below 1 GB. (512 MB is a reasonable upper bound for the
27  * entire footprint of the UEFI runtime services memory regions)
28  */
29 #define EFI_RT_VIRTUAL_BASE	SZ_512M
30 #define EFI_RT_VIRTUAL_SIZE	SZ_512M
31 
32 #ifdef CONFIG_ARM64
33 # define EFI_RT_VIRTUAL_LIMIT	DEFAULT_MAP_WINDOW_64
34 #else
35 # define EFI_RT_VIRTUAL_LIMIT	TASK_SIZE
36 #endif
37 
38 static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
39 
efi_char16_printk(efi_system_table_t * sys_table_arg,efi_char16_t * str)40 void efi_char16_printk(efi_system_table_t *sys_table_arg,
41 			      efi_char16_t *str)
42 {
43 	struct efi_simple_text_output_protocol *out;
44 
45 	out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
46 	out->output_string(out, str);
47 }
48 
setup_graphics(efi_system_table_t * sys_table_arg)49 static struct screen_info *setup_graphics(efi_system_table_t *sys_table_arg)
50 {
51 	efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
52 	efi_status_t status;
53 	unsigned long size;
54 	void **gop_handle = NULL;
55 	struct screen_info *si = NULL;
56 
57 	size = 0;
58 	status = efi_call_early(locate_handle, EFI_LOCATE_BY_PROTOCOL,
59 				&gop_proto, NULL, &size, gop_handle);
60 	if (status == EFI_BUFFER_TOO_SMALL) {
61 		si = alloc_screen_info(sys_table_arg);
62 		if (!si)
63 			return NULL;
64 		efi_setup_gop(sys_table_arg, si, &gop_proto, size);
65 	}
66 	return si;
67 }
68 
install_memreserve_table(efi_system_table_t * sys_table_arg)69 void install_memreserve_table(efi_system_table_t *sys_table_arg)
70 {
71 	struct linux_efi_memreserve *rsv;
72 	efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
73 	efi_status_t status;
74 
75 	status = efi_call_early(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
76 				(void **)&rsv);
77 	if (status != EFI_SUCCESS) {
78 		pr_efi_err(sys_table_arg, "Failed to allocate memreserve entry!\n");
79 		return;
80 	}
81 
82 	rsv->next = 0;
83 	rsv->size = 0;
84 	atomic_set(&rsv->count, 0);
85 
86 	status = efi_call_early(install_configuration_table,
87 				&memreserve_table_guid,
88 				rsv);
89 	if (status != EFI_SUCCESS)
90 		pr_efi_err(sys_table_arg, "Failed to install memreserve config table!\n");
91 }
92 
93 
94 /*
95  * This function handles the architcture specific differences between arm and
96  * arm64 regarding where the kernel image must be loaded and any memory that
97  * must be reserved. On failure it is required to free all
98  * all allocations it has made.
99  */
100 efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
101 				 unsigned long *image_addr,
102 				 unsigned long *image_size,
103 				 unsigned long *reserve_addr,
104 				 unsigned long *reserve_size,
105 				 unsigned long dram_base,
106 				 efi_loaded_image_t *image);
107 /*
108  * EFI entry point for the arm/arm64 EFI stubs.  This is the entrypoint
109  * that is described in the PE/COFF header.  Most of the code is the same
110  * for both archictectures, with the arch-specific code provided in the
111  * handle_kernel_image() function.
112  */
efi_entry(void * handle,efi_system_table_t * sys_table,unsigned long * image_addr)113 unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
114 			       unsigned long *image_addr)
115 {
116 	efi_loaded_image_t *image;
117 	efi_status_t status;
118 	unsigned long image_size = 0;
119 	unsigned long dram_base;
120 	/* addr/point and size pairs for memory management*/
121 	unsigned long initrd_addr;
122 	u64 initrd_size = 0;
123 	unsigned long fdt_addr = 0;  /* Original DTB */
124 	unsigned long fdt_size = 0;
125 	char *cmdline_ptr = NULL;
126 	int cmdline_size = 0;
127 	unsigned long new_fdt_addr;
128 	efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
129 	unsigned long reserve_addr = 0;
130 	unsigned long reserve_size = 0;
131 	enum efi_secureboot_mode secure_boot;
132 	struct screen_info *si;
133 
134 	/* Check if we were booted by the EFI firmware */
135 	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
136 		goto fail;
137 
138 	status = check_platform_features(sys_table);
139 	if (status != EFI_SUCCESS)
140 		goto fail;
141 
142 	/*
143 	 * Get a handle to the loaded image protocol.  This is used to get
144 	 * information about the running image, such as size and the command
145 	 * line.
146 	 */
147 	status = sys_table->boottime->handle_protocol(handle,
148 					&loaded_image_proto, (void *)&image);
149 	if (status != EFI_SUCCESS) {
150 		pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
151 		goto fail;
152 	}
153 
154 	dram_base = get_dram_base(sys_table);
155 	if (dram_base == EFI_ERROR) {
156 		pr_efi_err(sys_table, "Failed to find DRAM base\n");
157 		goto fail;
158 	}
159 
160 	/*
161 	 * Get the command line from EFI, using the LOADED_IMAGE
162 	 * protocol. We are going to copy the command line into the
163 	 * device tree, so this can be allocated anywhere.
164 	 */
165 	cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
166 	if (!cmdline_ptr) {
167 		pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
168 		goto fail;
169 	}
170 
171 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
172 	    IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
173 	    cmdline_size == 0)
174 		efi_parse_options(CONFIG_CMDLINE);
175 
176 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0)
177 		efi_parse_options(cmdline_ptr);
178 
179 	pr_efi(sys_table, "Booting Linux Kernel...\n");
180 
181 	si = setup_graphics(sys_table);
182 
183 	status = handle_kernel_image(sys_table, image_addr, &image_size,
184 				     &reserve_addr,
185 				     &reserve_size,
186 				     dram_base, image);
187 	if (status != EFI_SUCCESS) {
188 		pr_efi_err(sys_table, "Failed to relocate kernel\n");
189 		goto fail_free_cmdline;
190 	}
191 
192 	/* Ask the firmware to clear memory on unclean shutdown */
193 	efi_enable_reset_attack_mitigation(sys_table);
194 
195 	secure_boot = efi_get_secureboot(sys_table);
196 
197 	/*
198 	 * Unauthenticated device tree data is a security hazard, so ignore
199 	 * 'dtb=' unless UEFI Secure Boot is disabled.  We assume that secure
200 	 * boot is enabled if we can't determine its state.
201 	 */
202 	if (!IS_ENABLED(CONFIG_EFI_ARMSTUB_DTB_LOADER) ||
203 	     secure_boot != efi_secureboot_mode_disabled) {
204 		if (strstr(cmdline_ptr, "dtb="))
205 			pr_efi(sys_table, "Ignoring DTB from command line.\n");
206 	} else {
207 		status = handle_cmdline_files(sys_table, image, cmdline_ptr,
208 					      "dtb=",
209 					      ~0UL, &fdt_addr, &fdt_size);
210 
211 		if (status != EFI_SUCCESS) {
212 			pr_efi_err(sys_table, "Failed to load device tree!\n");
213 			goto fail_free_image;
214 		}
215 	}
216 
217 	if (fdt_addr) {
218 		pr_efi(sys_table, "Using DTB from command line\n");
219 	} else {
220 		/* Look for a device tree configuration table entry. */
221 		fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
222 		if (fdt_addr)
223 			pr_efi(sys_table, "Using DTB from configuration table\n");
224 	}
225 
226 	if (!fdt_addr)
227 		pr_efi(sys_table, "Generating empty DTB\n");
228 
229 	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
230 				      efi_get_max_initrd_addr(dram_base,
231 							      *image_addr),
232 				      (unsigned long *)&initrd_addr,
233 				      (unsigned long *)&initrd_size);
234 	if (status != EFI_SUCCESS)
235 		pr_efi_err(sys_table, "Failed initrd from command line!\n");
236 
237 	efi_random_get_seed(sys_table);
238 
239 	/* hibernation expects the runtime regions to stay in the same place */
240 	if (!IS_ENABLED(CONFIG_HIBERNATION) && !nokaslr()) {
241 		/*
242 		 * Randomize the base of the UEFI runtime services region.
243 		 * Preserve the 2 MB alignment of the region by taking a
244 		 * shift of 21 bit positions into account when scaling
245 		 * the headroom value using a 32-bit random value.
246 		 */
247 		static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
248 					    EFI_RT_VIRTUAL_BASE -
249 					    EFI_RT_VIRTUAL_SIZE;
250 		u32 rnd;
251 
252 		status = efi_get_random_bytes(sys_table, sizeof(rnd),
253 					      (u8 *)&rnd);
254 		if (status == EFI_SUCCESS) {
255 			virtmap_base = EFI_RT_VIRTUAL_BASE +
256 				       (((headroom >> 21) * rnd) >> (32 - 21));
257 		}
258 	}
259 
260 	install_memreserve_table(sys_table);
261 
262 	new_fdt_addr = fdt_addr;
263 	status = allocate_new_fdt_and_exit_boot(sys_table, handle,
264 				&new_fdt_addr, efi_get_max_fdt_addr(dram_base),
265 				initrd_addr, initrd_size, cmdline_ptr,
266 				fdt_addr, fdt_size);
267 
268 	/*
269 	 * If all went well, we need to return the FDT address to the
270 	 * calling function so it can be passed to kernel as part of
271 	 * the kernel boot protocol.
272 	 */
273 	if (status == EFI_SUCCESS)
274 		return new_fdt_addr;
275 
276 	pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
277 
278 	efi_free(sys_table, initrd_size, initrd_addr);
279 	efi_free(sys_table, fdt_size, fdt_addr);
280 
281 fail_free_image:
282 	efi_free(sys_table, image_size, *image_addr);
283 	efi_free(sys_table, reserve_size, reserve_addr);
284 fail_free_cmdline:
285 	free_screen_info(sys_table, si);
286 	efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
287 fail:
288 	return EFI_ERROR;
289 }
290 
cmp_mem_desc(const void * l,const void * r)291 static int cmp_mem_desc(const void *l, const void *r)
292 {
293 	const efi_memory_desc_t *left = l, *right = r;
294 
295 	return (left->phys_addr > right->phys_addr) ? 1 : -1;
296 }
297 
298 /*
299  * Returns whether region @left ends exactly where region @right starts,
300  * or false if either argument is NULL.
301  */
regions_are_adjacent(efi_memory_desc_t * left,efi_memory_desc_t * right)302 static bool regions_are_adjacent(efi_memory_desc_t *left,
303 				 efi_memory_desc_t *right)
304 {
305 	u64 left_end;
306 
307 	if (left == NULL || right == NULL)
308 		return false;
309 
310 	left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
311 
312 	return left_end == right->phys_addr;
313 }
314 
315 /*
316  * Returns whether region @left and region @right have compatible memory type
317  * mapping attributes, and are both EFI_MEMORY_RUNTIME regions.
318  */
regions_have_compatible_memory_type_attrs(efi_memory_desc_t * left,efi_memory_desc_t * right)319 static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
320 						      efi_memory_desc_t *right)
321 {
322 	static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
323 					 EFI_MEMORY_WC | EFI_MEMORY_UC |
324 					 EFI_MEMORY_RUNTIME;
325 
326 	return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
327 }
328 
329 /*
330  * efi_get_virtmap() - create a virtual mapping for the EFI memory map
331  *
332  * This function populates the virt_addr fields of all memory region descriptors
333  * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
334  * are also copied to @runtime_map, and their total count is returned in @count.
335  */
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)336 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
337 		     unsigned long desc_size, efi_memory_desc_t *runtime_map,
338 		     int *count)
339 {
340 	u64 efi_virt_base = virtmap_base;
341 	efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
342 	int l;
343 
344 	/*
345 	 * To work around potential issues with the Properties Table feature
346 	 * introduced in UEFI 2.5, which may split PE/COFF executable images
347 	 * in memory into several RuntimeServicesCode and RuntimeServicesData
348 	 * regions, we need to preserve the relative offsets between adjacent
349 	 * EFI_MEMORY_RUNTIME regions with the same memory type attributes.
350 	 * The easiest way to find adjacent regions is to sort the memory map
351 	 * before traversing it.
352 	 */
353 	if (IS_ENABLED(CONFIG_ARM64))
354 		sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc,
355 		     NULL);
356 
357 	for (l = 0; l < map_size; l += desc_size, prev = in) {
358 		u64 paddr, size;
359 
360 		in = (void *)memory_map + l;
361 		if (!(in->attribute & EFI_MEMORY_RUNTIME))
362 			continue;
363 
364 		paddr = in->phys_addr;
365 		size = in->num_pages * EFI_PAGE_SIZE;
366 
367 		if (novamap()) {
368 			in->virt_addr = in->phys_addr;
369 			continue;
370 		}
371 
372 		/*
373 		 * Make the mapping compatible with 64k pages: this allows
374 		 * a 4k page size kernel to kexec a 64k page size kernel and
375 		 * vice versa.
376 		 */
377 		if ((IS_ENABLED(CONFIG_ARM64) &&
378 		     !regions_are_adjacent(prev, in)) ||
379 		    !regions_have_compatible_memory_type_attrs(prev, in)) {
380 
381 			paddr = round_down(in->phys_addr, SZ_64K);
382 			size += in->phys_addr - paddr;
383 
384 			/*
385 			 * Avoid wasting memory on PTEs by choosing a virtual
386 			 * base that is compatible with section mappings if this
387 			 * region has the appropriate size and physical
388 			 * alignment. (Sections are 2 MB on 4k granule kernels)
389 			 */
390 			if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
391 				efi_virt_base = round_up(efi_virt_base, SZ_2M);
392 			else
393 				efi_virt_base = round_up(efi_virt_base, SZ_64K);
394 		}
395 
396 		in->virt_addr = efi_virt_base + in->phys_addr - paddr;
397 		efi_virt_base += size;
398 
399 		memcpy(out, in, desc_size);
400 		out = (void *)out + desc_size;
401 		++*count;
402 	}
403 }
404