Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.9.4.
  1/*
  2 * EFI stub implementation that is shared by arm and arm64 architectures.
  3 * This should be #included by the EFI stub implementation files.
  4 *
  5 * Copyright (C) 2013,2014 Linaro Limited
  6 *     Roy Franz <roy.franz@linaro.org
  7 * Copyright (C) 2013 Red Hat, Inc.
  8 *     Mark Salter <msalter@redhat.com>
  9 *
 10 * This file is part of the Linux kernel, and is made available under the
 11 * terms of the GNU General Public License version 2.
 12 *
 13 */
 14
 15#include <linux/efi.h>
 16#include <linux/sort.h>
 17#include <asm/efi.h>
 18
 19#include "efistub.h"
 20
 21bool __nokaslr;
 22
 23static int efi_secureboot_enabled(efi_system_table_t *sys_table_arg)
 24{
 25	static efi_guid_t const var_guid = EFI_GLOBAL_VARIABLE_GUID;
 26	static efi_char16_t const var_name[] = {
 27		'S', 'e', 'c', 'u', 'r', 'e', 'B', 'o', 'o', 't', 0 };
 28
 29	efi_get_variable_t *f_getvar = sys_table_arg->runtime->get_variable;
 30	unsigned long size = sizeof(u8);
 31	efi_status_t status;
 32	u8 val;
 33
 34	status = f_getvar((efi_char16_t *)var_name, (efi_guid_t *)&var_guid,
 35			  NULL, &size, &val);
 36
 37	switch (status) {
 38	case EFI_SUCCESS:
 39		return val;
 40	case EFI_NOT_FOUND:
 41		return 0;
 42	default:
 43		return 1;
 44	}
 45}
 46
 47efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
 48			     void *__image, void **__fh)
 49{
 50	efi_file_io_interface_t *io;
 51	efi_loaded_image_t *image = __image;
 52	efi_file_handle_t *fh;
 53	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
 54	efi_status_t status;
 55	void *handle = (void *)(unsigned long)image->device_handle;
 56
 57	status = sys_table_arg->boottime->handle_protocol(handle,
 58				 &fs_proto, (void **)&io);
 59	if (status != EFI_SUCCESS) {
 60		efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
 61		return status;
 62	}
 63
 64	status = io->open_volume(io, &fh);
 65	if (status != EFI_SUCCESS)
 66		efi_printk(sys_table_arg, "Failed to open volume\n");
 67
 68	*__fh = fh;
 69	return status;
 70}
 71
 72efi_status_t efi_file_close(void *handle)
 73{
 74	efi_file_handle_t *fh = handle;
 75
 76	return fh->close(handle);
 77}
 78
 79efi_status_t
 80efi_file_read(void *handle, unsigned long *size, void *addr)
 81{
 82	efi_file_handle_t *fh = handle;
 83
 84	return fh->read(handle, size, addr);
 85}
 86
 87
 88efi_status_t
 89efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
 90	      efi_char16_t *filename_16, void **handle, u64 *file_sz)
 91{
 92	efi_file_handle_t *h, *fh = __fh;
 93	efi_file_info_t *info;
 94	efi_status_t status;
 95	efi_guid_t info_guid = EFI_FILE_INFO_ID;
 96	unsigned long info_sz;
 97
 98	status = fh->open(fh, &h, filename_16, EFI_FILE_MODE_READ, (u64)0);
 99	if (status != EFI_SUCCESS) {
100		efi_printk(sys_table_arg, "Failed to open file: ");
101		efi_char16_printk(sys_table_arg, filename_16);
102		efi_printk(sys_table_arg, "\n");
103		return status;
104	}
105
106	*handle = h;
107
108	info_sz = 0;
109	status = h->get_info(h, &info_guid, &info_sz, NULL);
110	if (status != EFI_BUFFER_TOO_SMALL) {
111		efi_printk(sys_table_arg, "Failed to get file info size\n");
112		return status;
113	}
114
115grow:
116	status = sys_table_arg->boottime->allocate_pool(EFI_LOADER_DATA,
117				 info_sz, (void **)&info);
118	if (status != EFI_SUCCESS) {
119		efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
120		return status;
121	}
122
123	status = h->get_info(h, &info_guid, &info_sz,
124						   info);
125	if (status == EFI_BUFFER_TOO_SMALL) {
126		sys_table_arg->boottime->free_pool(info);
127		goto grow;
128	}
129
130	*file_sz = info->file_size;
131	sys_table_arg->boottime->free_pool(info);
132
133	if (status != EFI_SUCCESS)
134		efi_printk(sys_table_arg, "Failed to get initrd info\n");
135
136	return status;
137}
138
139
140
141void efi_char16_printk(efi_system_table_t *sys_table_arg,
142			      efi_char16_t *str)
143{
144	struct efi_simple_text_output_protocol *out;
145
146	out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
147	out->output_string(out, str);
148}
149
150
151/*
152 * This function handles the architcture specific differences between arm and
153 * arm64 regarding where the kernel image must be loaded and any memory that
154 * must be reserved. On failure it is required to free all
155 * all allocations it has made.
156 */
157efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
158				 unsigned long *image_addr,
159				 unsigned long *image_size,
160				 unsigned long *reserve_addr,
161				 unsigned long *reserve_size,
162				 unsigned long dram_base,
163				 efi_loaded_image_t *image);
164/*
165 * EFI entry point for the arm/arm64 EFI stubs.  This is the entrypoint
166 * that is described in the PE/COFF header.  Most of the code is the same
167 * for both archictectures, with the arch-specific code provided in the
168 * handle_kernel_image() function.
169 */
170unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
171			       unsigned long *image_addr)
172{
173	efi_loaded_image_t *image;
174	efi_status_t status;
175	unsigned long image_size = 0;
176	unsigned long dram_base;
177	/* addr/point and size pairs for memory management*/
178	unsigned long initrd_addr;
179	u64 initrd_size = 0;
180	unsigned long fdt_addr = 0;  /* Original DTB */
181	unsigned long fdt_size = 0;
182	char *cmdline_ptr = NULL;
183	int cmdline_size = 0;
184	unsigned long new_fdt_addr;
185	efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
186	unsigned long reserve_addr = 0;
187	unsigned long reserve_size = 0;
188
189	/* Check if we were booted by the EFI firmware */
190	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
191		goto fail;
192
193	pr_efi(sys_table, "Booting Linux Kernel...\n");
194
195	status = check_platform_features(sys_table);
196	if (status != EFI_SUCCESS)
197		goto fail;
198
199	/*
200	 * Get a handle to the loaded image protocol.  This is used to get
201	 * information about the running image, such as size and the command
202	 * line.
203	 */
204	status = sys_table->boottime->handle_protocol(handle,
205					&loaded_image_proto, (void *)&image);
206	if (status != EFI_SUCCESS) {
207		pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
208		goto fail;
209	}
210
211	dram_base = get_dram_base(sys_table);
212	if (dram_base == EFI_ERROR) {
213		pr_efi_err(sys_table, "Failed to find DRAM base\n");
214		goto fail;
215	}
216
217	/*
218	 * Get the command line from EFI, using the LOADED_IMAGE
219	 * protocol. We are going to copy the command line into the
220	 * device tree, so this can be allocated anywhere.
221	 */
222	cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
223	if (!cmdline_ptr) {
224		pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
225		goto fail;
226	}
227
228	/* check whether 'nokaslr' was passed on the command line */
229	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
230		static const u8 default_cmdline[] = CONFIG_CMDLINE;
231		const u8 *str, *cmdline = cmdline_ptr;
232
233		if (IS_ENABLED(CONFIG_CMDLINE_FORCE))
234			cmdline = default_cmdline;
235		str = strstr(cmdline, "nokaslr");
236		if (str == cmdline || (str > cmdline && *(str - 1) == ' '))
237			__nokaslr = true;
238	}
239
240	status = handle_kernel_image(sys_table, image_addr, &image_size,
241				     &reserve_addr,
242				     &reserve_size,
243				     dram_base, image);
244	if (status != EFI_SUCCESS) {
245		pr_efi_err(sys_table, "Failed to relocate kernel\n");
246		goto fail_free_cmdline;
247	}
248
249	status = efi_parse_options(cmdline_ptr);
250	if (status != EFI_SUCCESS)
251		pr_efi_err(sys_table, "Failed to parse EFI cmdline options\n");
252
253	/*
254	 * Unauthenticated device tree data is a security hazard, so
255	 * ignore 'dtb=' unless UEFI Secure Boot is disabled.
256	 */
257	if (efi_secureboot_enabled(sys_table)) {
258		pr_efi(sys_table, "UEFI Secure Boot is enabled.\n");
259	} else {
260		status = handle_cmdline_files(sys_table, image, cmdline_ptr,
261					      "dtb=",
262					      ~0UL, &fdt_addr, &fdt_size);
263
264		if (status != EFI_SUCCESS) {
265			pr_efi_err(sys_table, "Failed to load device tree!\n");
266			goto fail_free_image;
267		}
268	}
269
270	if (fdt_addr) {
271		pr_efi(sys_table, "Using DTB from command line\n");
272	} else {
273		/* Look for a device tree configuration table entry. */
274		fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
275		if (fdt_addr)
276			pr_efi(sys_table, "Using DTB from configuration table\n");
277	}
278
279	if (!fdt_addr)
280		pr_efi(sys_table, "Generating empty DTB\n");
281
282	status = handle_cmdline_files(sys_table, image, cmdline_ptr,
283				      "initrd=", dram_base + SZ_512M,
284				      (unsigned long *)&initrd_addr,
285				      (unsigned long *)&initrd_size);
286	if (status != EFI_SUCCESS)
287		pr_efi_err(sys_table, "Failed initrd from command line!\n");
288
289	new_fdt_addr = fdt_addr;
290	status = allocate_new_fdt_and_exit_boot(sys_table, handle,
291				&new_fdt_addr, dram_base + MAX_FDT_OFFSET,
292				initrd_addr, initrd_size, cmdline_ptr,
293				fdt_addr, fdt_size);
294
295	/*
296	 * If all went well, we need to return the FDT address to the
297	 * calling function so it can be passed to kernel as part of
298	 * the kernel boot protocol.
299	 */
300	if (status == EFI_SUCCESS)
301		return new_fdt_addr;
302
303	pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
304
305	efi_free(sys_table, initrd_size, initrd_addr);
306	efi_free(sys_table, fdt_size, fdt_addr);
307
308fail_free_image:
309	efi_free(sys_table, image_size, *image_addr);
310	efi_free(sys_table, reserve_size, reserve_addr);
311fail_free_cmdline:
312	efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
313fail:
314	return EFI_ERROR;
315}
316
317/*
318 * This is the base address at which to start allocating virtual memory ranges
319 * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use
320 * any allocation we choose, and eliminate the risk of a conflict after kexec.
321 * The value chosen is the largest non-zero power of 2 suitable for this purpose
322 * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
323 * be mapped efficiently.
324 * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
325 * map everything below 1 GB.
326 */
327#define EFI_RT_VIRTUAL_BASE	SZ_512M
328
329static int cmp_mem_desc(const void *l, const void *r)
330{
331	const efi_memory_desc_t *left = l, *right = r;
332
333	return (left->phys_addr > right->phys_addr) ? 1 : -1;
334}
335
336/*
337 * Returns whether region @left ends exactly where region @right starts,
338 * or false if either argument is NULL.
339 */
340static bool regions_are_adjacent(efi_memory_desc_t *left,
341				 efi_memory_desc_t *right)
342{
343	u64 left_end;
344
345	if (left == NULL || right == NULL)
346		return false;
347
348	left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
349
350	return left_end == right->phys_addr;
351}
352
353/*
354 * Returns whether region @left and region @right have compatible memory type
355 * mapping attributes, and are both EFI_MEMORY_RUNTIME regions.
356 */
357static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
358						      efi_memory_desc_t *right)
359{
360	static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
361					 EFI_MEMORY_WC | EFI_MEMORY_UC |
362					 EFI_MEMORY_RUNTIME;
363
364	return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
365}
366
367/*
368 * efi_get_virtmap() - create a virtual mapping for the EFI memory map
369 *
370 * This function populates the virt_addr fields of all memory region descriptors
371 * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
372 * are also copied to @runtime_map, and their total count is returned in @count.
373 */
374void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
375		     unsigned long desc_size, efi_memory_desc_t *runtime_map,
376		     int *count)
377{
378	u64 efi_virt_base = EFI_RT_VIRTUAL_BASE;
379	efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
380	int l;
381
382	/*
383	 * To work around potential issues with the Properties Table feature
384	 * introduced in UEFI 2.5, which may split PE/COFF executable images
385	 * in memory into several RuntimeServicesCode and RuntimeServicesData
386	 * regions, we need to preserve the relative offsets between adjacent
387	 * EFI_MEMORY_RUNTIME regions with the same memory type attributes.
388	 * The easiest way to find adjacent regions is to sort the memory map
389	 * before traversing it.
390	 */
391	sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, NULL);
392
393	for (l = 0; l < map_size; l += desc_size, prev = in) {
394		u64 paddr, size;
395
396		in = (void *)memory_map + l;
397		if (!(in->attribute & EFI_MEMORY_RUNTIME))
398			continue;
399
400		paddr = in->phys_addr;
401		size = in->num_pages * EFI_PAGE_SIZE;
402
403		/*
404		 * Make the mapping compatible with 64k pages: this allows
405		 * a 4k page size kernel to kexec a 64k page size kernel and
406		 * vice versa.
407		 */
408		if (!regions_are_adjacent(prev, in) ||
409		    !regions_have_compatible_memory_type_attrs(prev, in)) {
410
411			paddr = round_down(in->phys_addr, SZ_64K);
412			size += in->phys_addr - paddr;
413
414			/*
415			 * Avoid wasting memory on PTEs by choosing a virtual
416			 * base that is compatible with section mappings if this
417			 * region has the appropriate size and physical
418			 * alignment. (Sections are 2 MB on 4k granule kernels)
419			 */
420			if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
421				efi_virt_base = round_up(efi_virt_base, SZ_2M);
422			else
423				efi_virt_base = round_up(efi_virt_base, SZ_64K);
424		}
425
426		in->virt_addr = efi_virt_base + in->phys_addr - paddr;
427		efi_virt_base += size;
428
429		memcpy(out, in, desc_size);
430		out = (void *)out + desc_size;
431		++*count;
432	}
433}