Linux Audio

Check our new training course

Loading...
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Helper functions used by the EFI stub on multiple
  4 * architectures. This should be #included by the EFI stub
  5 * implementation files.
  6 *
  7 * Copyright 2011 Intel Corporation; author Matt Fleming
  8 */
  9
 10#include <linux/stdarg.h>
 11
 12#include <linux/efi.h>
 13#include <linux/kernel.h>
 14#include <linux/overflow.h>
 15#include <asm/efi.h>
 16#include <asm/setup.h>
 17
 18#include "efistub.h"
 19
 20bool efi_nochunk;
 21bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE);
 22bool efi_novamap;
 
 
 
 
 
 
 
 
 
 
 
 23
 24static bool efi_noinitrd;
 25static bool efi_nosoftreserve;
 26static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
 27
 28int efi_mem_encrypt;
 
 
 29
 30bool __pure __efi_soft_reserve_enabled(void)
 
 
 
 
 
 
 
 
 31{
 32	return !efi_nosoftreserve;
 33}
 34
 35/**
 36 * efi_parse_options() - Parse EFI command line options
 37 * @cmdline:	kernel command line
 38 *
 39 * Parse the ASCII string @cmdline for EFI options, denoted by the efi=
 40 * option, e.g. efi=nochunk.
 41 *
 42 * It should be noted that efi= is parsed in two very different
 43 * environments, first in the early boot environment of the EFI boot
 44 * stub, and subsequently during the kernel boot.
 45 *
 46 * Return:	status code
 47 */
 48efi_status_t efi_parse_options(char const *cmdline)
 49{
 50	size_t len;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 51	efi_status_t status;
 52	char *str, *buf;
 
 53
 54	if (!cmdline)
 55		return EFI_SUCCESS;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 56
 57	len = strnlen(cmdline, COMMAND_LINE_SIZE - 1) + 1;
 58	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
 59	if (status != EFI_SUCCESS)
 60		return status;
 61
 62	memcpy(buf, cmdline, len - 1);
 63	buf[len - 1] = '\0';
 64	str = skip_spaces(buf);
 
 65
 66	while (*str) {
 67		char *param, *val;
 
 
 68
 69		str = next_arg(str, &param, &val);
 70		if (!val && !strcmp(param, "--"))
 71			break;
 72
 73		if (!strcmp(param, "nokaslr")) {
 74			efi_nokaslr = true;
 75		} else if (!strcmp(param, "quiet")) {
 76			efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
 77		} else if (!strcmp(param, "noinitrd")) {
 78			efi_noinitrd = true;
 79		} else if (IS_ENABLED(CONFIG_X86_64) && !strcmp(param, "no5lvl")) {
 80			efi_no5lvl = true;
 81		} else if (IS_ENABLED(CONFIG_ARCH_HAS_MEM_ENCRYPT) &&
 82			   !strcmp(param, "mem_encrypt") && val) {
 83			if (parse_option_str(val, "on"))
 84				efi_mem_encrypt = 1;
 85			else if (parse_option_str(val, "off"))
 86				efi_mem_encrypt = -1;
 87		} else if (!strcmp(param, "efi") && val) {
 88			efi_nochunk = parse_option_str(val, "nochunk");
 89			efi_novamap |= parse_option_str(val, "novamap");
 90
 91			efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
 92					    parse_option_str(val, "nosoftreserve");
 93
 94			if (parse_option_str(val, "disable_early_pci_dma"))
 95				efi_disable_pci_dma = true;
 96			if (parse_option_str(val, "no_disable_early_pci_dma"))
 97				efi_disable_pci_dma = false;
 98			if (parse_option_str(val, "debug"))
 99				efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
100		} else if (!strcmp(param, "video") &&
101			   val && strstarts(val, "efifb:")) {
102			efi_parse_option_graphics(val + strlen("efifb:"));
103		}
104	}
105	efi_bs_call(free_pool, buf);
106	return EFI_SUCCESS;
107}
108
109/*
110 * The EFI_LOAD_OPTION descriptor has the following layout:
111 *	u32 Attributes;
112 *	u16 FilePathListLength;
113 *	u16 Description[];
114 *	efi_device_path_protocol_t FilePathList[];
115 *	u8 OptionalData[];
116 *
117 * This function validates and unpacks the variable-size data fields.
118 */
119static
120bool efi_load_option_unpack(efi_load_option_unpacked_t *dest,
121			    const efi_load_option_t *src, size_t size)
122{
123	const void *pos;
124	u16 c;
125	efi_device_path_protocol_t header;
126	const efi_char16_t *description;
127	const efi_device_path_protocol_t *file_path_list;
128
129	if (size < offsetof(efi_load_option_t, variable_data))
130		return false;
131	pos = src->variable_data;
132	size -= offsetof(efi_load_option_t, variable_data);
133
134	if ((src->attributes & ~EFI_LOAD_OPTION_MASK) != 0)
135		return false;
136
137	/* Scan description. */
138	description = pos;
139	do {
140		if (size < sizeof(c))
141			return false;
142		c = *(const u16 *)pos;
143		pos += sizeof(c);
144		size -= sizeof(c);
145	} while (c != L'\0');
146
147	/* Scan file_path_list. */
148	file_path_list = pos;
149	do {
150		if (size < sizeof(header))
151			return false;
152		header = *(const efi_device_path_protocol_t *)pos;
153		if (header.length < sizeof(header))
154			return false;
155		if (size < header.length)
156			return false;
157		pos += header.length;
158		size -= header.length;
159	} while ((header.type != EFI_DEV_END_PATH && header.type != EFI_DEV_END_PATH2) ||
160		 (header.sub_type != EFI_DEV_END_ENTIRE));
161	if (pos != (const void *)file_path_list + src->file_path_list_length)
162		return false;
163
164	dest->attributes = src->attributes;
165	dest->file_path_list_length = src->file_path_list_length;
166	dest->description = description;
167	dest->file_path_list = file_path_list;
168	dest->optional_data_size = size;
169	dest->optional_data = size ? pos : NULL;
170
171	return true;
172}
173
174/*
175 * At least some versions of Dell firmware pass the entire contents of the
176 * Boot#### variable, i.e. the EFI_LOAD_OPTION descriptor, rather than just the
177 * OptionalData field.
178 *
179 * Detect this case and extract OptionalData.
180 */
181void efi_apply_loadoptions_quirk(const void **load_options, u32 *load_options_size)
 
 
182{
183	const efi_load_option_t *load_option = *load_options;
184	efi_load_option_unpacked_t load_option_unpacked;
 
 
 
 
 
185
186	if (!IS_ENABLED(CONFIG_X86))
187		return;
188	if (!load_option)
189		return;
190	if (*load_options_size < sizeof(*load_option))
191		return;
192	if ((load_option->attributes & ~EFI_LOAD_OPTION_BOOT_MASK) != 0)
193		return;
194
195	if (!efi_load_option_unpack(&load_option_unpacked, load_option, *load_options_size))
196		return;
 
197
198	efi_warn_once(FW_BUG "LoadOptions is an EFI_LOAD_OPTION descriptor\n");
199	efi_warn_once(FW_BUG "Using OptionalData as a workaround\n");
 
 
 
 
 
 
200
201	*load_options = load_option_unpacked.optional_data;
202	*load_options_size = load_option_unpacked.optional_data_size;
203}
 
 
 
 
204
205enum efistub_event_type {
206	EFISTUB_EVT_INITRD,
207	EFISTUB_EVT_LOAD_OPTIONS,
208	EFISTUB_EVT_COUNT,
209};
210
211#define STR_WITH_SIZE(s)	sizeof(s), s
 
212
213static const struct {
214	u32		pcr_index;
215	u32		event_id;
216	u32		event_data_len;
217	u8		event_data[52];
218} events[] = {
219	[EFISTUB_EVT_INITRD] = {
220		9,
221		INITRD_EVENT_TAG_ID,
222		STR_WITH_SIZE("Linux initrd")
223	},
224	[EFISTUB_EVT_LOAD_OPTIONS] = {
225		9,
226		LOAD_OPTIONS_EVENT_TAG_ID,
227		STR_WITH_SIZE("LOADED_IMAGE::LoadOptions")
228	},
229};
230
231static_assert(sizeof(efi_tcg2_event_t) == sizeof(efi_cc_event_t));
 
232
233union efistub_event {
234	efi_tcg2_event_t	tcg2_data;
235	efi_cc_event_t		cc_data;
236};
237
238struct efistub_measured_event {
239	union efistub_event	event_data;
240	TCG_PCClientTaggedEvent tagged_event __packed;
241};
242
243static efi_status_t efi_measure_tagged_event(unsigned long load_addr,
244					     unsigned long load_size,
245					     enum efistub_event_type event)
246{
247	union {
248		efi_status_t
249		(__efiapi *hash_log_extend_event)(void *, u64, efi_physical_addr_t,
250						  u64, const union efistub_event *);
251		struct { u32 hash_log_extend_event; } mixed_mode;
252	} method;
253	struct efistub_measured_event *evt;
254	int size = struct_size(evt, tagged_event.tagged_event_data,
255			       events[event].event_data_len);
256	efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
257	efi_tcg2_protocol_t *tcg2 = NULL;
258	union efistub_event ev;
259	efi_status_t status;
260	void *protocol;
261
262	efi_bs_call(locate_protocol, &tcg2_guid, NULL, (void **)&tcg2);
263	if (tcg2) {
264		ev.tcg2_data = (struct efi_tcg2_event){
265			.event_size			= size,
266			.event_header.header_size	= sizeof(ev.tcg2_data.event_header),
267			.event_header.header_version	= EFI_TCG2_EVENT_HEADER_VERSION,
268			.event_header.pcr_index		= events[event].pcr_index,
269			.event_header.event_type	= EV_EVENT_TAG,
270		};
271		protocol = tcg2;
272		method.hash_log_extend_event =
273			(void *)efi_table_attr(tcg2, hash_log_extend_event);
274	} else {
275		efi_guid_t cc_guid = EFI_CC_MEASUREMENT_PROTOCOL_GUID;
276		efi_cc_protocol_t *cc = NULL;
277
278		efi_bs_call(locate_protocol, &cc_guid, NULL, (void **)&cc);
279		if (!cc)
280			return EFI_UNSUPPORTED;
281
282		ev.cc_data = (struct efi_cc_event){
283			.event_size			= size,
284			.event_header.header_size	= sizeof(ev.cc_data.event_header),
285			.event_header.header_version	= EFI_CC_EVENT_HEADER_VERSION,
286			.event_header.event_type	= EV_EVENT_TAG,
287		};
288
289		status = efi_call_proto(cc, map_pcr_to_mr_index,
290					events[event].pcr_index,
291					&ev.cc_data.event_header.mr_index);
292		if (status != EFI_SUCCESS)
293			goto fail;
294
295		protocol = cc;
296		method.hash_log_extend_event =
297			(void *)efi_table_attr(cc, hash_log_extend_event);
298	}
299
300	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, (void **)&evt);
301	if (status != EFI_SUCCESS)
302		goto fail;
303
304	*evt = (struct efistub_measured_event) {
305		.event_data			     = ev,
306		.tagged_event.tagged_event_id	     = events[event].event_id,
307		.tagged_event.tagged_event_data_size = events[event].event_data_len,
308	};
309
310	memcpy(evt->tagged_event.tagged_event_data, events[event].event_data,
311	       events[event].event_data_len);
312
313	status = efi_fn_call(&method, hash_log_extend_event, protocol, 0,
314			     load_addr, load_size, &evt->event_data);
315	efi_bs_call(free_pool, evt);
316
317	if (status == EFI_SUCCESS)
318		return EFI_SUCCESS;
319
 
320fail:
321	efi_warn("Failed to measure data for event %d: 0x%lx\n", event, status);
322	return status;
323}
324
325/*
326 * Convert the unicode UEFI command line to ASCII to pass to kernel.
327 * Size of memory allocated return in *cmd_line_len.
328 * Returns NULL on error.
329 */
330char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len)
 
 
331{
332	const efi_char16_t *options = efi_table_attr(image, load_options);
333	u32 options_size = efi_table_attr(image, load_options_size);
334	int options_bytes = 0, safe_options_bytes = 0;  /* UTF-8 bytes */
335	unsigned long cmdline_addr = 0;
336	const efi_char16_t *s2;
337	bool in_quote = false;
338	efi_status_t status;
339	u32 options_chars;
340
341	if (options_size > 0)
342		efi_measure_tagged_event((unsigned long)options, options_size,
343					 EFISTUB_EVT_LOAD_OPTIONS);
344
345	efi_apply_loadoptions_quirk((const void **)&options, &options_size);
346	options_chars = options_size / sizeof(efi_char16_t);
347
348	if (options) {
349		s2 = options;
350		while (options_bytes < COMMAND_LINE_SIZE && options_chars--) {
351			efi_char16_t c = *s2++;
 
 
352
353			if (c < 0x80) {
354				if (c == L'\0' || c == L'\n')
355					break;
356				if (c == L'"')
357					in_quote = !in_quote;
358				else if (!in_quote && isspace((char)c))
359					safe_options_bytes = options_bytes;
360
361				options_bytes++;
362				continue;
363			}
 
 
 
 
 
364
365			/*
366			 * Get the number of UTF-8 bytes corresponding to a
367			 * UTF-16 character.
368			 * The first part handles everything in the BMP.
369			 */
370			options_bytes += 2 + (c >= 0x800);
371			/*
372			 * Add one more byte for valid surrogate pairs. Invalid
373			 * surrogates will be replaced with 0xfffd and take up
374			 * only 3 bytes.
375			 */
376			if ((c & 0xfc00) == 0xd800) {
377				/*
378				 * If the very last word is a high surrogate,
379				 * we must ignore it since we can't access the
380				 * low surrogate.
381				 */
382				if (!options_chars) {
383					options_bytes -= 3;
384				} else if ((*s2 & 0xfc00) == 0xdc00) {
385					options_bytes++;
386					options_chars--;
387					s2++;
388				}
389			}
390		}
391		if (options_bytes >= COMMAND_LINE_SIZE) {
392			options_bytes = safe_options_bytes;
393			efi_err("Command line is too long: truncated to %d bytes\n",
394				options_bytes);
 
395		}
396	}
397
398	options_bytes++;	/* NUL termination */
 
399
400	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, options_bytes,
401			     (void **)&cmdline_addr);
402	if (status != EFI_SUCCESS)
403		return NULL;
404
405	snprintf((char *)cmdline_addr, options_bytes, "%.*ls",
406		 options_bytes - 1, options);
 
 
407
408	*cmd_line_len = options_bytes;
409	return (char *)cmdline_addr;
 
 
 
410}
411
412/**
413 * efi_exit_boot_services() - Exit boot services
414 * @handle:	handle of the exiting image
415 * @priv:	argument to be passed to @priv_func
416 * @priv_func:	function to process the memory map before exiting boot services
417 *
418 * Handle calling ExitBootServices according to the requirements set out by the
419 * spec.  Obtains the current memory map, and returns that info after calling
420 * ExitBootServices.  The client must specify a function to perform any
421 * processing of the memory map data prior to ExitBootServices.  A client
422 * specific structure may be passed to the function via priv.  The client
423 * function may be called multiple times.
424 *
425 * Return:	status code
426 */
427efi_status_t efi_exit_boot_services(void *handle, void *priv,
428				    efi_exit_boot_map_processing priv_func)
429{
430	struct efi_boot_memmap *map;
 
431	efi_status_t status;
 
 
 
 
 
 
 
 
 
 
 
432
433	if (efi_disable_pci_dma)
434		efi_pci_disable_bridge_busmaster();
435
436	status = efi_get_memory_map(&map, true);
437	if (status != EFI_SUCCESS)
 
 
 
438		return status;
 
439
440	status = priv_func(map, priv);
 
 
441	if (status != EFI_SUCCESS) {
442		efi_bs_call(free_pool, map);
443		return status;
444	}
445
446	status = efi_bs_call(exit_boot_services, handle, map->map_key);
 
 
 
 
 
447
448	if (status == EFI_INVALID_PARAMETER) {
449		/*
450		 * The memory map changed between efi_get_memory_map() and
451		 * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
452		 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
453		 * updated map, and try again.  The spec implies one retry
454		 * should be sufficent, which is confirmed against the EDK2
455		 * implementation.  Per the spec, we can only invoke
456		 * get_memory_map() and exit_boot_services() - we cannot alloc
457		 * so efi_get_memory_map() cannot be used, and we must reuse
458		 * the buffer.  For all practical purposes, the headroom in the
459		 * buffer should account for any changes in the map so the call
460		 * to get_memory_map() is expected to succeed here.
461		 */
462		map->map_size = map->buff_size;
463		status = efi_bs_call(get_memory_map,
464				     &map->map_size,
465				     &map->map,
466				     &map->map_key,
467				     &map->desc_size,
468				     &map->desc_ver);
469
470		/* exit_boot_services() was called, thus cannot free */
471		if (status != EFI_SUCCESS)
472			return status;
473
474		status = priv_func(map, priv);
475		/* exit_boot_services() was called, thus cannot free */
476		if (status != EFI_SUCCESS)
477			return status;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478
479		status = efi_bs_call(exit_boot_services, handle, map->map_key);
 
 
 
 
480	}
481
 
 
 
 
 
 
482	return status;
483}
484
485/**
486 * get_efi_config_table() - retrieve UEFI configuration table
487 * @guid:	GUID of the configuration table to be retrieved
488 * Return:	pointer to the configuration table or NULL
 
 
 
489 */
490void *get_efi_config_table(efi_guid_t guid)
491{
492	unsigned long tables = efi_table_attr(efi_system_table, tables);
493	int nr_tables = efi_table_attr(efi_system_table, nr_tables);
494	int i;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
495
496	for (i = 0; i < nr_tables; i++) {
497		efi_config_table_t *t = (void *)tables;
 
 
498
499		if (efi_guidcmp(t->guid, guid) == 0)
500			return efi_table_attr(t, table);
 
501
502		tables += efi_is_native() ? sizeof(efi_config_table_t)
503					  : sizeof(efi_config_table_32_t);
504	}
505	return NULL;
 
506}
507
508/*
509 * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
510 * for the firmware or bootloader to expose the initrd data directly to the stub
511 * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
512 * very easy to implement. It is a simple Linux initrd specific conduit between
513 * kernel and firmware, allowing us to put the EFI stub (being part of the
514 * kernel) in charge of where and when to load the initrd, while leaving it up
515 * to the firmware to decide whether it needs to expose its filesystem hierarchy
516 * via EFI protocols.
517 */
518static const struct {
519	struct efi_vendor_dev_path	vendor;
520	struct efi_generic_dev_path	end;
521} __packed initrd_dev_path = {
522	{
523		{
524			EFI_DEV_MEDIA,
525			EFI_DEV_MEDIA_VENDOR,
526			sizeof(struct efi_vendor_dev_path),
527		},
528		LINUX_EFI_INITRD_MEDIA_GUID
529	}, {
530		EFI_DEV_END_PATH,
531		EFI_DEV_END_ENTIRE,
532		sizeof(struct efi_generic_dev_path)
533	}
534};
535
536/**
537 * efi_load_initrd_dev_path() - load the initrd from the Linux initrd device path
538 * @initrd:	pointer of struct to store the address where the initrd was loaded
539 *		and the size of the loaded initrd
540 * @max:	upper limit for the initrd memory allocation
541 *
542 * Return:
543 * * %EFI_SUCCESS if the initrd was loaded successfully, in which
544 *   case @load_addr and @load_size are assigned accordingly
545 * * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd device path
546 * * %EFI_OUT_OF_RESOURCES if memory allocation failed
547 * * %EFI_LOAD_ERROR in all other cases
548 */
549static
550efi_status_t efi_load_initrd_dev_path(struct linux_efi_initrd *initrd,
551				      unsigned long max)
552{
553	efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
554	efi_device_path_protocol_t *dp;
555	efi_load_file2_protocol_t *lf2;
556	efi_handle_t handle;
 
 
 
557	efi_status_t status;
 
 
 
558
559	dp = (efi_device_path_protocol_t *)&initrd_dev_path;
560	status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
561	if (status != EFI_SUCCESS)
562		return status;
563
564	status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
565			     (void **)&lf2);
566	if (status != EFI_SUCCESS)
567		return status;
568
569	initrd->size = 0;
570	status = efi_call_proto(lf2, load_file, dp, false, &initrd->size, NULL);
571	if (status != EFI_BUFFER_TOO_SMALL)
572		return EFI_LOAD_ERROR;
573
574	status = efi_allocate_pages(initrd->size, &initrd->base, max);
575	if (status != EFI_SUCCESS)
576		return status;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
577
578	status = efi_call_proto(lf2, load_file, dp, false, &initrd->size,
579				(void *)initrd->base);
 
 
 
580	if (status != EFI_SUCCESS) {
581		efi_free(initrd->size, initrd->base);
582		return EFI_LOAD_ERROR;
583	}
584	return EFI_SUCCESS;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
585}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
586
587static
588efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
589				     struct linux_efi_initrd *initrd,
590				     unsigned long soft_limit,
591				     unsigned long hard_limit)
592{
593	if (image == NULL)
594		return EFI_UNSUPPORTED;
595
596	return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
597				    soft_limit, hard_limit,
598				    &initrd->base, &initrd->size);
599}
600
601/**
602 * efi_load_initrd() - Load initial RAM disk
603 * @image:	EFI loaded image protocol
604 * @soft_limit:	preferred address for loading the initrd
605 * @hard_limit:	upper limit address for loading the initrd
606 *
607 * Return:	status code
608 */
609efi_status_t efi_load_initrd(efi_loaded_image_t *image,
610			     unsigned long soft_limit,
611			     unsigned long hard_limit,
612			     const struct linux_efi_initrd **out)
613{
614	efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID;
615	efi_status_t status = EFI_SUCCESS;
616	struct linux_efi_initrd initrd, *tbl;
617
618	if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD) || efi_noinitrd)
619		return EFI_SUCCESS;
620
621	status = efi_load_initrd_dev_path(&initrd, hard_limit);
622	if (status == EFI_SUCCESS) {
623		efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
624		if (initrd.size > 0 &&
625		    efi_measure_tagged_event(initrd.base, initrd.size,
626					     EFISTUB_EVT_INITRD) == EFI_SUCCESS)
627			efi_info("Measured initrd data into PCR 9\n");
628	} else if (status == EFI_NOT_FOUND) {
629		status = efi_load_initrd_cmdline(image, &initrd, soft_limit,
630						 hard_limit);
631		/* command line loader disabled or no initrd= passed? */
632		if (status == EFI_UNSUPPORTED || status == EFI_NOT_READY)
633			return EFI_SUCCESS;
634		if (status == EFI_SUCCESS)
635			efi_info("Loaded initrd from command line option\n");
636	}
637	if (status != EFI_SUCCESS)
638		goto failed;
639
640	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(initrd),
641			     (void **)&tbl);
642	if (status != EFI_SUCCESS)
643		goto free_initrd;
 
 
644
645	*tbl = initrd;
646	status = efi_bs_call(install_configuration_table, &tbl_guid, tbl);
647	if (status != EFI_SUCCESS)
648		goto free_tbl;
 
649
650	if (out)
651		*out = tbl;
652	return EFI_SUCCESS;
653
654free_tbl:
655	efi_bs_call(free_pool, tbl);
656free_initrd:
657	efi_free(initrd.size, initrd.base);
658failed:
659	efi_err("Failed to load initrd: 0x%lx\n", status);
660	return status;
661}
662
663/**
664 * efi_wait_for_key() - Wait for key stroke
665 * @usec:	number of microseconds to wait for key stroke
666 * @key:	key entered
667 *
668 * Wait for up to @usec microseconds for a key stroke.
669 *
670 * Return:	status code, EFI_SUCCESS if key received
671 */
672efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
673{
674	efi_event_t events[2], timer;
675	unsigned long index;
676	efi_simple_text_input_protocol_t *con_in;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
677	efi_status_t status;
 
 
 
 
 
 
 
 
 
 
678
679	con_in = efi_table_attr(efi_system_table, con_in);
680	if (!con_in)
681		return EFI_UNSUPPORTED;
682	efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
683
684	status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
685	if (status != EFI_SUCCESS)
686		return status;
687
688	status = efi_bs_call(set_timer, timer, EfiTimerRelative,
689			     EFI_100NSEC_PER_USEC * usec);
690	if (status != EFI_SUCCESS)
691		return status;
692	efi_set_event_at(events, 1, timer);
693
694	status = efi_bs_call(wait_for_event, 2, events, &index);
695	if (status == EFI_SUCCESS) {
696		if (index == 0)
697			status = efi_call_proto(con_in, read_keystroke, key);
698		else
699			status = EFI_TIMEOUT;
700	}
701
702	efi_bs_call(close_event, timer);
 
703
704	return status;
 
705}
706
707/**
708 * efi_remap_image - Remap a loaded image with the appropriate permissions
709 *                   for code and data
710 *
711 * @image_base:	the base of the image in memory
712 * @alloc_size:	the size of the area in memory occupied by the image
713 * @code_size:	the size of the leading part of the image containing code
714 * 		and read-only data
715 *
716 * efi_remap_image() uses the EFI memory attribute protocol to remap the code
717 * region of the loaded image read-only/executable, and the remainder
718 * read-write/non-executable. The code region is assumed to start at the base
719 * of the image, and will therefore cover the PE/COFF header as well.
720 */
721void efi_remap_image(unsigned long image_base, unsigned alloc_size,
722		     unsigned long code_size)
 
 
 
723{
724	efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
725	efi_memory_attribute_protocol_t *memattr;
726	efi_status_t status;
727	u64 attr;
728
729	/*
730	 * If the firmware implements the EFI_MEMORY_ATTRIBUTE_PROTOCOL, let's
731	 * invoke it to remap the text/rodata region of the decompressed image
732	 * as read-only and the data/bss region as non-executable.
733	 */
734	status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
735	if (status != EFI_SUCCESS)
736		return;
737
738	// Get the current attributes for the entire region
739	status = memattr->get_memory_attributes(memattr, image_base,
740						alloc_size, &attr);
741	if (status != EFI_SUCCESS) {
742		efi_warn("Failed to retrieve memory attributes for image region: 0x%lx\n",
743			 status);
744		return;
745	}
746
747	// Mark the code region as read-only
748	status = memattr->set_memory_attributes(memattr, image_base, code_size,
749						EFI_MEMORY_RO);
750	if (status != EFI_SUCCESS) {
751		efi_warn("Failed to remap code region read-only\n");
752		return;
753	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
754
755	// If the entire region was already mapped as non-exec, clear the
756	// attribute from the code region. Otherwise, set it on the data
757	// region.
758	if (attr & EFI_MEMORY_XP) {
759		status = memattr->clear_memory_attributes(memattr, image_base,
760							  code_size,
761							  EFI_MEMORY_XP);
762		if (status != EFI_SUCCESS)
763			efi_warn("Failed to remap code region executable\n");
764	} else {
765		status = memattr->set_memory_attributes(memattr,
766							image_base + code_size,
767							alloc_size - code_size,
768							EFI_MEMORY_XP);
769		if (status != EFI_SUCCESS)
770			efi_warn("Failed to remap data region non-executable\n");
 
 
771	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
772}
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Helper functions used by the EFI stub on multiple
  4 * architectures. This should be #included by the EFI stub
  5 * implementation files.
  6 *
  7 * Copyright 2011 Intel Corporation; author Matt Fleming
  8 */
  9
 
 
 10#include <linux/efi.h>
 
 
 11#include <asm/efi.h>
 
 12
 13#include "efistub.h"
 14
 15/*
 16 * Some firmware implementations have problems reading files in one go.
 17 * A read chunk size of 1MB seems to work for most platforms.
 18 *
 19 * Unfortunately, reading files in chunks triggers *other* bugs on some
 20 * platforms, so we provide a way to disable this workaround, which can
 21 * be done by passing "efi=nochunk" on the EFI boot stub command line.
 22 *
 23 * If you experience issues with initrd images being corrupt it's worth
 24 * trying efi=nochunk, but chunking is enabled by default because there
 25 * are far more machines that require the workaround than those that
 26 * break with it enabled.
 27 */
 28#define EFI_READ_CHUNK_SIZE	(1024 * 1024)
 29
 30static unsigned long __chunk_size = EFI_READ_CHUNK_SIZE;
 
 
 31
 32static int __section(.data) __nokaslr;
 33static int __section(.data) __quiet;
 34static int __section(.data) __novamap;
 35
 36int __pure nokaslr(void)
 37{
 38	return __nokaslr;
 39}
 40int __pure is_quiet(void)
 41{
 42	return __quiet;
 43}
 44int __pure novamap(void)
 45{
 46	return __novamap;
 47}
 48
 49#define EFI_MMAP_NR_SLACK_SLOTS	8
 50
 51struct file_info {
 52	efi_file_handle_t *handle;
 53	u64 size;
 54};
 55
 56void efi_printk(efi_system_table_t *sys_table_arg, char *str)
 
 
 
 
 
 
 57{
 58	char *s8;
 59
 60	for (s8 = str; *s8; s8++) {
 61		efi_char16_t ch[2] = { 0 };
 62
 63		ch[0] = *s8;
 64		if (*s8 == '\n') {
 65			efi_char16_t nl[2] = { '\r', 0 };
 66			efi_char16_printk(sys_table_arg, nl);
 67		}
 68
 69		efi_char16_printk(sys_table_arg, ch);
 70	}
 71}
 72
 73static inline bool mmap_has_headroom(unsigned long buff_size,
 74				     unsigned long map_size,
 75				     unsigned long desc_size)
 76{
 77	unsigned long slack = buff_size - map_size;
 78
 79	return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
 80}
 81
 82efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
 83				struct efi_boot_memmap *map)
 84{
 85	efi_memory_desc_t *m = NULL;
 86	efi_status_t status;
 87	unsigned long key;
 88	u32 desc_version;
 89
 90	*map->desc_size =	sizeof(*m);
 91	*map->map_size =	*map->desc_size * 32;
 92	*map->buff_size =	*map->map_size;
 93again:
 94	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
 95				*map->map_size, (void **)&m);
 96	if (status != EFI_SUCCESS)
 97		goto fail;
 98
 99	*map->desc_size = 0;
100	key = 0;
101	status = efi_call_early(get_memory_map, map->map_size, m,
102				&key, map->desc_size, &desc_version);
103	if (status == EFI_BUFFER_TOO_SMALL ||
104	    !mmap_has_headroom(*map->buff_size, *map->map_size,
105			       *map->desc_size)) {
106		efi_call_early(free_pool, m);
107		/*
108		 * Make sure there is some entries of headroom so that the
109		 * buffer can be reused for a new map after allocations are
110		 * no longer permitted.  Its unlikely that the map will grow to
111		 * exceed this headroom once we are ready to trigger
112		 * ExitBootServices()
113		 */
114		*map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
115		*map->buff_size = *map->map_size;
116		goto again;
117	}
118
 
 
119	if (status != EFI_SUCCESS)
120		efi_call_early(free_pool, m);
121
122	if (map->key_ptr && status == EFI_SUCCESS)
123		*map->key_ptr = key;
124	if (map->desc_ver && status == EFI_SUCCESS)
125		*map->desc_ver = desc_version;
126
127fail:
128	*map->map = m;
129	return status;
130}
131
 
 
 
132
133unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
134{
135	efi_status_t status;
136	unsigned long map_size, buff_size;
137	unsigned long membase  = EFI_ERROR;
138	struct efi_memory_map map;
139	efi_memory_desc_t *md;
140	struct efi_boot_memmap boot_map;
141
142	boot_map.map =		(efi_memory_desc_t **)&map.map;
143	boot_map.map_size =	&map_size;
144	boot_map.desc_size =	&map.desc_size;
145	boot_map.desc_ver =	NULL;
146	boot_map.key_ptr =	NULL;
147	boot_map.buff_size =	&buff_size;
148
149	status = efi_get_memory_map(sys_table_arg, &boot_map);
150	if (status != EFI_SUCCESS)
151		return membase;
152
153	map.map_end = map.map + map_size;
154
155	for_each_efi_memory_desc_in_map(&map, md) {
156		if (md->attribute & EFI_MEMORY_WB) {
157			if (membase > md->phys_addr)
158				membase = md->phys_addr;
 
 
 
 
159		}
160	}
 
 
 
161
162	efi_call_early(free_pool, map.map);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
164	return membase;
165}
166
167/*
168 * Allocate at the highest possible address that is not above 'max'.
 
 
 
 
169 */
170efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
171			    unsigned long size, unsigned long align,
172			    unsigned long *addr, unsigned long max)
173{
174	unsigned long map_size, desc_size, buff_size;
175	efi_memory_desc_t *map;
176	efi_status_t status;
177	unsigned long nr_pages;
178	u64 max_addr = 0;
179	int i;
180	struct efi_boot_memmap boot_map;
181
182	boot_map.map =		&map;
183	boot_map.map_size =	&map_size;
184	boot_map.desc_size =	&desc_size;
185	boot_map.desc_ver =	NULL;
186	boot_map.key_ptr =	NULL;
187	boot_map.buff_size =	&buff_size;
 
 
188
189	status = efi_get_memory_map(sys_table_arg, &boot_map);
190	if (status != EFI_SUCCESS)
191		goto fail;
192
193	/*
194	 * Enforce minimum alignment that EFI or Linux requires when
195	 * requesting a specific address.  We are doing page-based (or
196	 * larger) allocations, and both the address and size must meet
197	 * alignment constraints.
198	 */
199	if (align < EFI_ALLOC_ALIGN)
200		align = EFI_ALLOC_ALIGN;
201
202	size = round_up(size, EFI_ALLOC_ALIGN);
203	nr_pages = size / EFI_PAGE_SIZE;
204again:
205	for (i = 0; i < map_size / desc_size; i++) {
206		efi_memory_desc_t *desc;
207		unsigned long m = (unsigned long)map;
208		u64 start, end;
209
210		desc = efi_early_memdesc_ptr(m, desc_size, i);
211		if (desc->type != EFI_CONVENTIONAL_MEMORY)
212			continue;
 
 
213
214		if (desc->num_pages < nr_pages)
215			continue;
216
217		start = desc->phys_addr;
218		end = start + desc->num_pages * EFI_PAGE_SIZE;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
220		if (end > max)
221			end = max;
222
223		if ((start + size) > end)
224			continue;
 
 
225
226		if (round_down(end - size, align) < start)
227			continue;
 
 
228
229		start = round_down(end - size, align);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
231		/*
232		 * Don't allocate at 0x0. It will confuse code that
233		 * checks pointers against NULL.
234		 */
235		if (start == 0x0)
236			continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
238		if (start > max_addr)
239			max_addr = start;
 
240	}
241
242	if (!max_addr)
243		status = EFI_NOT_FOUND;
244	else {
245		status = efi_call_early(allocate_pages,
246					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
247					nr_pages, &max_addr);
248		if (status != EFI_SUCCESS) {
249			max = max_addr;
250			max_addr = 0;
251			goto again;
252		}
 
 
 
 
 
253
254		*addr = max_addr;
255	}
256
257	efi_call_early(free_pool, map);
258fail:
 
259	return status;
260}
261
262/*
263 * Allocate at the lowest possible address that is not below 'min'.
 
 
264 */
265efi_status_t efi_low_alloc_above(efi_system_table_t *sys_table_arg,
266				 unsigned long size, unsigned long align,
267				 unsigned long *addr, unsigned long min)
268{
269	unsigned long map_size, desc_size, buff_size;
270	efi_memory_desc_t *map;
 
 
 
 
271	efi_status_t status;
272	unsigned long nr_pages;
273	int i;
274	struct efi_boot_memmap boot_map;
 
 
 
 
 
275
276	boot_map.map =		&map;
277	boot_map.map_size =	&map_size;
278	boot_map.desc_size =	&desc_size;
279	boot_map.desc_ver =	NULL;
280	boot_map.key_ptr =	NULL;
281	boot_map.buff_size =	&buff_size;
282
283	status = efi_get_memory_map(sys_table_arg, &boot_map);
284	if (status != EFI_SUCCESS)
285		goto fail;
 
 
 
 
286
287	/*
288	 * Enforce minimum alignment that EFI or Linux requires when
289	 * requesting a specific address.  We are doing page-based (or
290	 * larger) allocations, and both the address and size must meet
291	 * alignment constraints.
292	 */
293	if (align < EFI_ALLOC_ALIGN)
294		align = EFI_ALLOC_ALIGN;
295
296	size = round_up(size, EFI_ALLOC_ALIGN);
297	nr_pages = size / EFI_PAGE_SIZE;
298	for (i = 0; i < map_size / desc_size; i++) {
299		efi_memory_desc_t *desc;
300		unsigned long m = (unsigned long)map;
301		u64 start, end;
302
303		desc = efi_early_memdesc_ptr(m, desc_size, i);
304
305		if (desc->type != EFI_CONVENTIONAL_MEMORY)
306			continue;
307
308		if (desc->num_pages < nr_pages)
309			continue;
310
311		start = desc->phys_addr;
312		end = start + desc->num_pages * EFI_PAGE_SIZE;
313
314		if (start < min)
315			start = min;
316
317		start = round_up(start, align);
318		if ((start + size) > end)
319			continue;
320
321		status = efi_call_early(allocate_pages,
322					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
323					nr_pages, &start);
324		if (status == EFI_SUCCESS) {
325			*addr = start;
326			break;
327		}
328	}
329
330	if (i == map_size / desc_size)
331		status = EFI_NOT_FOUND;
332
333	efi_call_early(free_pool, map);
334fail:
335	return status;
336}
337
338void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
339	      unsigned long addr)
340{
341	unsigned long nr_pages;
342
343	if (!size)
344		return;
345
346	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
347	efi_call_early(free_pages, addr, nr_pages);
348}
349
350static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
351				  efi_char16_t *filename_16, void **handle,
352				  u64 *file_sz)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353{
354	efi_file_handle_t *h, *fh = __fh;
355	efi_file_info_t *info;
356	efi_status_t status;
357	efi_guid_t info_guid = EFI_FILE_INFO_ID;
358	unsigned long info_sz;
359
360	status = efi_call_proto(efi_file_handle, open, fh, &h, filename_16,
361				EFI_FILE_MODE_READ, (u64)0);
362	if (status != EFI_SUCCESS) {
363		efi_printk(sys_table_arg, "Failed to open file: ");
364		efi_char16_printk(sys_table_arg, filename_16);
365		efi_printk(sys_table_arg, "\n");
366		return status;
367	}
368
369	*handle = h;
 
370
371	info_sz = 0;
372	status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
373				&info_sz, NULL);
374	if (status != EFI_BUFFER_TOO_SMALL) {
375		efi_printk(sys_table_arg, "Failed to get file info size\n");
376		return status;
377	}
378
379grow:
380	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
381				info_sz, (void **)&info);
382	if (status != EFI_SUCCESS) {
383		efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
384		return status;
385	}
386
387	status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
388				&info_sz, info);
389	if (status == EFI_BUFFER_TOO_SMALL) {
390		efi_call_early(free_pool, info);
391		goto grow;
392	}
393
394	*file_sz = info->file_size;
395	efi_call_early(free_pool, info);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396
397	if (status != EFI_SUCCESS)
398		efi_printk(sys_table_arg, "Failed to get initrd info\n");
 
399
400	return status;
401}
402
403static efi_status_t efi_file_read(void *handle, unsigned long *size, void *addr)
404{
405	return efi_call_proto(efi_file_handle, read, handle, size, addr);
406}
407
408static efi_status_t efi_file_close(void *handle)
409{
410	return efi_call_proto(efi_file_handle, close, handle);
411}
412
413static efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
414				    efi_loaded_image_t *image,
415				    efi_file_handle_t **__fh)
416{
417	efi_file_io_interface_t *io;
418	efi_file_handle_t *fh;
419	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
420	efi_status_t status;
421	void *handle = (void *)(unsigned long)efi_table_attr(efi_loaded_image,
422							     device_handle,
423							     image);
424
425	status = efi_call_early(handle_protocol, handle,
426				&fs_proto, (void **)&io);
427	if (status != EFI_SUCCESS) {
428		efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
429		return status;
430	}
431
432	status = efi_call_proto(efi_file_io_interface, open_volume, io, &fh);
433	if (status != EFI_SUCCESS)
434		efi_printk(sys_table_arg, "Failed to open volume\n");
435	else
436		*__fh = fh;
437
438	return status;
439}
440
441/*
442 * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
443 * option, e.g. efi=nochunk.
444 *
445 * It should be noted that efi= is parsed in two very different
446 * environments, first in the early boot environment of the EFI boot
447 * stub, and subsequently during the kernel boot.
448 */
449efi_status_t efi_parse_options(char const *cmdline)
450{
451	char *str;
452
453	str = strstr(cmdline, "nokaslr");
454	if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
455		__nokaslr = 1;
456
457	str = strstr(cmdline, "quiet");
458	if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
459		__quiet = 1;
460
461	/*
462	 * If no EFI parameters were specified on the cmdline we've got
463	 * nothing to do.
464	 */
465	str = strstr(cmdline, "efi=");
466	if (!str)
467		return EFI_SUCCESS;
468
469	/* Skip ahead to first argument */
470	str += strlen("efi=");
471
472	/*
473	 * Remember, because efi= is also used by the kernel we need to
474	 * skip over arguments we don't understand.
475	 */
476	while (*str && *str != ' ') {
477		if (!strncmp(str, "nochunk", 7)) {
478			str += strlen("nochunk");
479			__chunk_size = -1UL;
480		}
481
482		if (!strncmp(str, "novamap", 7)) {
483			str += strlen("novamap");
484			__novamap = 1;
485		}
486
487		/* Group words together, delimited by "," */
488		while (*str && *str != ' ' && *str != ',')
489			str++;
490
491		if (*str == ',')
492			str++;
493	}
494
495	return EFI_SUCCESS;
496}
497
498/*
499 * Check the cmdline for a LILO-style file= arguments.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
500 *
501 * We only support loading a file from the same filesystem as
502 * the kernel image.
 
 
 
 
503 */
504efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
505				  efi_loaded_image_t *image,
506				  char *cmd_line, char *option_string,
507				  unsigned long max_addr,
508				  unsigned long *load_addr,
509				  unsigned long *load_size)
510{
511	struct file_info *files;
512	unsigned long file_addr;
513	u64 file_size_total;
514	efi_file_handle_t *fh = NULL;
515	efi_status_t status;
516	int nr_files;
517	char *str;
518	int i, j, k;
519
520	file_addr = 0;
521	file_size_total = 0;
 
 
522
523	str = cmd_line;
 
 
 
524
525	j = 0;			/* See close_handles */
 
 
 
526
527	if (!load_addr || !load_size)
528		return EFI_INVALID_PARAMETER;
529
530	*load_addr = 0;
531	*load_size = 0;
532
533	if (!str || !*str)
534		return EFI_SUCCESS;
535
536	for (nr_files = 0; *str; nr_files++) {
537		str = strstr(str, option_string);
538		if (!str)
539			break;
540
541		str += strlen(option_string);
542
543		/* Skip any leading slashes */
544		while (*str == '/' || *str == '\\')
545			str++;
546
547		while (*str && *str != ' ' && *str != '\n')
548			str++;
549	}
550
551	if (!nr_files)
552		return EFI_SUCCESS;
553
554	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
555				nr_files * sizeof(*files), (void **)&files);
556	if (status != EFI_SUCCESS) {
557		pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
558		goto fail;
559	}
560
561	str = cmd_line;
562	for (i = 0; i < nr_files; i++) {
563		struct file_info *file;
564		efi_char16_t filename_16[256];
565		efi_char16_t *p;
566
567		str = strstr(str, option_string);
568		if (!str)
569			break;
570
571		str += strlen(option_string);
572
573		file = &files[i];
574		p = filename_16;
575
576		/* Skip any leading slashes */
577		while (*str == '/' || *str == '\\')
578			str++;
579
580		while (*str && *str != ' ' && *str != '\n') {
581			if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
582				break;
583
584			if (*str == '/') {
585				*p++ = '\\';
586				str++;
587			} else {
588				*p++ = *str++;
589			}
590		}
591
592		*p = '\0';
593
594		/* Only open the volume once. */
595		if (!i) {
596			status = efi_open_volume(sys_table_arg, image, &fh);
597			if (status != EFI_SUCCESS)
598				goto free_files;
599		}
600
601		status = efi_file_size(sys_table_arg, fh, filename_16,
602				       (void **)&file->handle, &file->size);
603		if (status != EFI_SUCCESS)
604			goto close_handles;
605
606		file_size_total += file->size;
607	}
608
609	if (file_size_total) {
610		unsigned long addr;
611
612		/*
613		 * Multiple files need to be at consecutive addresses in memory,
614		 * so allocate enough memory for all the files.  This is used
615		 * for loading multiple files.
616		 */
617		status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
618				    &file_addr, max_addr);
619		if (status != EFI_SUCCESS) {
620			pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
621			goto close_handles;
622		}
623
624		/* We've run out of free low memory. */
625		if (file_addr > max_addr) {
626			pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
627			status = EFI_INVALID_PARAMETER;
628			goto free_file_total;
629		}
630
631		addr = file_addr;
632		for (j = 0; j < nr_files; j++) {
633			unsigned long size;
634
635			size = files[j].size;
636			while (size) {
637				unsigned long chunksize;
638
639				if (IS_ENABLED(CONFIG_X86) && size > __chunk_size)
640					chunksize = __chunk_size;
641				else
642					chunksize = size;
643
644				status = efi_file_read(files[j].handle,
645						       &chunksize,
646						       (void *)addr);
647				if (status != EFI_SUCCESS) {
648					pr_efi_err(sys_table_arg, "Failed to read file\n");
649					goto free_file_total;
650				}
651				addr += chunksize;
652				size -= chunksize;
653			}
654
655			efi_file_close(files[j].handle);
656		}
657
658	}
659
660	efi_call_early(free_pool, files);
661
662	*load_addr = file_addr;
663	*load_size = file_size_total;
664
665	return status;
666
667free_file_total:
668	efi_free(sys_table_arg, file_size_total, file_addr);
669
670close_handles:
671	for (k = j; k < i; k++)
672		efi_file_close(files[k].handle);
673free_files:
674	efi_call_early(free_pool, files);
675fail:
676	*load_addr = 0;
677	*load_size = 0;
678
679	return status;
680}
681/*
682 * Relocate a kernel image, either compressed or uncompressed.
683 * In the ARM64 case, all kernel images are currently
684 * uncompressed, and as such when we relocate it we need to
685 * allocate additional space for the BSS segment. Any low
686 * memory that this function should avoid needs to be
687 * unavailable in the EFI memory map, as if the preferred
688 * address is not available the lowest available address will
689 * be used.
690 */
691efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
692				 unsigned long *image_addr,
693				 unsigned long image_size,
694				 unsigned long alloc_size,
695				 unsigned long preferred_addr,
696				 unsigned long alignment,
697				 unsigned long min_addr)
698{
699	unsigned long cur_image_addr;
700	unsigned long new_addr = 0;
701	efi_status_t status;
702	unsigned long nr_pages;
703	efi_physical_addr_t efi_addr = preferred_addr;
704
705	if (!image_addr || !image_size || !alloc_size)
706		return EFI_INVALID_PARAMETER;
707	if (alloc_size < image_size)
708		return EFI_INVALID_PARAMETER;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
709
710	cur_image_addr = *image_addr;
 
711
712	/*
713	 * The EFI firmware loader could have placed the kernel image
714	 * anywhere in memory, but the kernel has restrictions on the
715	 * max physical address it can run at.  Some architectures
716	 * also have a prefered address, so first try to relocate
717	 * to the preferred address.  If that fails, allocate as low
718	 * as possible while respecting the required alignment.
719	 */
720	nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
721	status = efi_call_early(allocate_pages,
722				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
723				nr_pages, &efi_addr);
724	new_addr = efi_addr;
725	/*
726	 * If preferred address allocation failed allocate as low as
727	 * possible.
728	 */
729	if (status != EFI_SUCCESS) {
730		status = efi_low_alloc_above(sys_table_arg, alloc_size,
731					     alignment, &new_addr, min_addr);
732	}
733	if (status != EFI_SUCCESS) {
734		pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
735		return status;
736	}
737
738	/*
739	 * We know source/dest won't overlap since both memory ranges
740	 * have been allocated by UEFI, so we can safely use memcpy.
741	 */
742	memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
743
744	/* Return the new address of the relocated image. */
745	*image_addr = new_addr;
 
746
 
 
 
 
 
 
747	return status;
748}
749
750/*
751 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
752 * This overestimates for surrogates, but that is okay.
 
 
 
 
 
753 */
754static int efi_utf8_bytes(u16 c)
755{
756	return 1 + (c >= 0x80) + (c >= 0x800);
757}
758
759/*
760 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
761 */
762static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
763{
764	unsigned int c;
765
766	while (n--) {
767		c = *src++;
768		if (n && c >= 0xd800 && c <= 0xdbff &&
769		    *src >= 0xdc00 && *src <= 0xdfff) {
770			c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
771			src++;
772			n--;
773		}
774		if (c >= 0xd800 && c <= 0xdfff)
775			c = 0xfffd; /* Unmatched surrogate */
776		if (c < 0x80) {
777			*dst++ = c;
778			continue;
779		}
780		if (c < 0x800) {
781			*dst++ = 0xc0 + (c >> 6);
782			goto t1;
783		}
784		if (c < 0x10000) {
785			*dst++ = 0xe0 + (c >> 12);
786			goto t2;
787		}
788		*dst++ = 0xf0 + (c >> 18);
789		*dst++ = 0x80 + ((c >> 12) & 0x3f);
790	t2:
791		*dst++ = 0x80 + ((c >> 6) & 0x3f);
792	t1:
793		*dst++ = 0x80 + (c & 0x3f);
794	}
795
796	return dst;
797}
798
799#ifndef MAX_CMDLINE_ADDRESS
800#define MAX_CMDLINE_ADDRESS	ULONG_MAX
801#endif
802
803/*
804 * Convert the unicode UEFI command line to ASCII to pass to kernel.
805 * Size of memory allocated return in *cmd_line_len.
806 * Returns NULL on error.
807 */
808char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
809			  efi_loaded_image_t *image,
810			  int *cmd_line_len)
811{
812	const u16 *s2;
813	u8 *s1 = NULL;
814	unsigned long cmdline_addr = 0;
815	int load_options_chars = image->load_options_size / 2; /* UTF-16 */
816	const u16 *options = image->load_options;
817	int options_bytes = 0;  /* UTF-8 bytes */
818	int options_chars = 0;  /* UTF-16 chars */
819	efi_status_t status;
820	u16 zero = 0;
821
822	if (options) {
823		s2 = options;
824		while (*s2 && *s2 != '\n'
825		       && options_chars < load_options_chars) {
826			options_bytes += efi_utf8_bytes(*s2++);
827			options_chars++;
828		}
829	}
830
831	if (!options_chars) {
832		/* No command line options, so return empty string*/
833		options = &zero;
834	}
835
836	options_bytes++;	/* NUL termination */
 
 
837
838	status = efi_high_alloc(sys_table_arg, options_bytes, 0,
839				&cmdline_addr, MAX_CMDLINE_ADDRESS);
840	if (status != EFI_SUCCESS)
841		return NULL;
 
842
843	s1 = (u8 *)cmdline_addr;
844	s2 = (const u16 *)options;
 
 
 
 
 
845
846	s1 = efi_utf16_to_utf8(s1, s2, options_chars);
847	*s1 = '\0';
848
849	*cmd_line_len = options_bytes;
850	return (char *)cmdline_addr;
851}
852
853/*
854 * Handle calling ExitBootServices according to the requirements set out by the
855 * spec.  Obtains the current memory map, and returns that info after calling
856 * ExitBootServices.  The client must specify a function to perform any
857 * processing of the memory map data prior to ExitBootServices.  A client
858 * specific structure may be passed to the function via priv.  The client
859 * function may be called multiple times.
 
 
 
 
 
 
860 */
861efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
862				    void *handle,
863				    struct efi_boot_memmap *map,
864				    void *priv,
865				    efi_exit_boot_map_processing priv_func)
866{
 
 
867	efi_status_t status;
 
868
869	status = efi_get_memory_map(sys_table_arg, map);
870
 
 
 
 
871	if (status != EFI_SUCCESS)
872		goto fail;
873
874	status = priv_func(sys_table_arg, map, priv);
875	if (status != EFI_SUCCESS)
876		goto free_map;
 
 
 
 
 
877
878	status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
879
880	if (status == EFI_INVALID_PARAMETER) {
881		/*
882		 * The memory map changed between efi_get_memory_map() and
883		 * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
884		 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
885		 * updated map, and try again.  The spec implies one retry
886		 * should be sufficent, which is confirmed against the EDK2
887		 * implementation.  Per the spec, we can only invoke
888		 * get_memory_map() and exit_boot_services() - we cannot alloc
889		 * so efi_get_memory_map() cannot be used, and we must reuse
890		 * the buffer.  For all practical purposes, the headroom in the
891		 * buffer should account for any changes in the map so the call
892		 * to get_memory_map() is expected to succeed here.
893		 */
894		*map->map_size = *map->buff_size;
895		status = efi_call_early(get_memory_map,
896					map->map_size,
897					*map->map,
898					map->key_ptr,
899					map->desc_size,
900					map->desc_ver);
901
902		/* exit_boot_services() was called, thus cannot free */
 
 
 
 
 
 
903		if (status != EFI_SUCCESS)
904			goto fail;
905
906		status = priv_func(sys_table_arg, map, priv);
907		/* exit_boot_services() was called, thus cannot free */
 
 
908		if (status != EFI_SUCCESS)
909			goto fail;
910
911		status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
912	}
913
914	/* exit_boot_services() was called, thus cannot free */
915	if (status != EFI_SUCCESS)
916		goto fail;
917
918	return EFI_SUCCESS;
919
920free_map:
921	efi_call_early(free_pool, *map->map);
922fail:
923	return status;
924}
925
926#define GET_EFI_CONFIG_TABLE(bits)					\
927static void *get_efi_config_table##bits(efi_system_table_t *_sys_table,	\
928					efi_guid_t guid)		\
929{									\
930	efi_system_table_##bits##_t *sys_table;				\
931	efi_config_table_##bits##_t *tables;				\
932	int i;								\
933									\
934	sys_table = (typeof(sys_table))_sys_table;			\
935	tables = (typeof(tables))(unsigned long)sys_table->tables;	\
936									\
937	for (i = 0; i < sys_table->nr_tables; i++) {			\
938		if (efi_guidcmp(tables[i].guid, guid) != 0)		\
939			continue;					\
940									\
941		return (void *)(unsigned long)tables[i].table;		\
942	}								\
943									\
944	return NULL;							\
945}
946GET_EFI_CONFIG_TABLE(32)
947GET_EFI_CONFIG_TABLE(64)
948
949void *get_efi_config_table(efi_system_table_t *sys_table, efi_guid_t guid)
950{
951	if (efi_is_64bit())
952		return get_efi_config_table64(sys_table, guid);
953	else
954		return get_efi_config_table32(sys_table, guid);
955}