14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
2f4f75ad5SArd Biesheuvel /*
3f4f75ad5SArd Biesheuvel  * Helper functions used by the EFI stub on multiple
4f4f75ad5SArd Biesheuvel  * architectures. This should be #included by the EFI stub
5f4f75ad5SArd Biesheuvel  * implementation files.
6f4f75ad5SArd Biesheuvel  *
7f4f75ad5SArd Biesheuvel  * Copyright 2011 Intel Corporation; author Matt Fleming
8f4f75ad5SArd Biesheuvel  */
9f4f75ad5SArd Biesheuvel 
10c0891ac1SAlexey Dobriyan #include <linux/stdarg.h>
112c7d1e30SArvind Sankar 
12f4f75ad5SArd Biesheuvel #include <linux/efi.h>
13fd0528a2SArvind Sankar #include <linux/kernel.h>
14f4f75ad5SArd Biesheuvel #include <asm/efi.h>
1580b1bfe1SArvind Sankar #include <asm/setup.h>
16f4f75ad5SArd Biesheuvel 
17f4f75ad5SArd Biesheuvel #include "efistub.h"
18f4f75ad5SArd Biesheuvel 
19980771f6SArd Biesheuvel bool efi_nochunk;
207c116db2SWill Deacon bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE);
21980771f6SArd Biesheuvel bool efi_novamap;
22980771f6SArd Biesheuvel 
2320287d56SArd Biesheuvel static bool efi_noinitrd;
2454439370SArvind Sankar static bool efi_nosoftreserve;
2554439370SArvind Sankar static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
2660f38de7SArd Biesheuvel 
27*dc4cbf9eSArd Biesheuvel int efi_mem_encrypt;
28*dc4cbf9eSArd Biesheuvel 
__efi_soft_reserve_enabled(void)29b617c526SDan Williams bool __pure __efi_soft_reserve_enabled(void)
30b617c526SDan Williams {
31b617c526SDan Williams 	return !efi_nosoftreserve;
32b617c526SDan Williams }
3360f38de7SArd Biesheuvel 
348c0a839cSHeinrich Schuchardt /**
358c0a839cSHeinrich Schuchardt  * efi_parse_options() - Parse EFI command line options
368c0a839cSHeinrich Schuchardt  * @cmdline:	kernel command line
378c0a839cSHeinrich Schuchardt  *
388c0a839cSHeinrich Schuchardt  * Parse the ASCII string @cmdline for EFI options, denoted by the efi=
395a17dae4SMatt Fleming  * option, e.g. efi=nochunk.
405a17dae4SMatt Fleming  *
415a17dae4SMatt Fleming  * It should be noted that efi= is parsed in two very different
425a17dae4SMatt Fleming  * environments, first in the early boot environment of the EFI boot
435a17dae4SMatt Fleming  * stub, and subsequently during the kernel boot.
448c0a839cSHeinrich Schuchardt  *
458c0a839cSHeinrich Schuchardt  * Return:	status code
465a17dae4SMatt Fleming  */
efi_parse_options(char const * cmdline)4760f38de7SArd Biesheuvel efi_status_t efi_parse_options(char const *cmdline)
485a17dae4SMatt Fleming {
49a37ca6a2SArvind Sankar 	size_t len;
5091d150c0SArd Biesheuvel 	efi_status_t status;
5191d150c0SArd Biesheuvel 	char *str, *buf;
525a17dae4SMatt Fleming 
53a37ca6a2SArvind Sankar 	if (!cmdline)
54a37ca6a2SArvind Sankar 		return EFI_SUCCESS;
55a37ca6a2SArvind Sankar 
568a8a3237SArvind Sankar 	len = strnlen(cmdline, COMMAND_LINE_SIZE - 1) + 1;
5791d150c0SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
5891d150c0SArd Biesheuvel 	if (status != EFI_SUCCESS)
5991d150c0SArd Biesheuvel 		return status;
6091d150c0SArd Biesheuvel 
618a8a3237SArvind Sankar 	memcpy(buf, cmdline, len - 1);
628a8a3237SArvind Sankar 	buf[len - 1] = '\0';
638a8a3237SArvind Sankar 	str = skip_spaces(buf);
6491d150c0SArd Biesheuvel 
6591d150c0SArd Biesheuvel 	while (*str) {
6691d150c0SArd Biesheuvel 		char *param, *val;
6791d150c0SArd Biesheuvel 
6891d150c0SArd Biesheuvel 		str = next_arg(str, &param, &val);
691fd9717dSArvind Sankar 		if (!val && !strcmp(param, "--"))
701fd9717dSArvind Sankar 			break;
7191d150c0SArd Biesheuvel 
7291d150c0SArd Biesheuvel 		if (!strcmp(param, "nokaslr")) {
737d4e323dSArd Biesheuvel 			efi_nokaslr = true;
7491d150c0SArd Biesheuvel 		} else if (!strcmp(param, "quiet")) {
7523d5b73fSArvind Sankar 			efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
7679d3219dSArd Biesheuvel 		} else if (!strcmp(param, "noinitrd")) {
7779d3219dSArd Biesheuvel 			efi_noinitrd = true;
78cb1c9e02SArd Biesheuvel 		} else if (IS_ENABLED(CONFIG_X86_64) && !strcmp(param, "no5lvl")) {
79cb1c9e02SArd Biesheuvel 			efi_no5lvl = true;
80*dc4cbf9eSArd Biesheuvel 		} else if (IS_ENABLED(CONFIG_ARCH_HAS_MEM_ENCRYPT) &&
81*dc4cbf9eSArd Biesheuvel 			   !strcmp(param, "mem_encrypt") && val) {
82*dc4cbf9eSArd Biesheuvel 			if (parse_option_str(val, "on"))
83*dc4cbf9eSArd Biesheuvel 				efi_mem_encrypt = 1;
84*dc4cbf9eSArd Biesheuvel 			else if (parse_option_str(val, "off"))
85*dc4cbf9eSArd Biesheuvel 				efi_mem_encrypt = -1;
8691d150c0SArd Biesheuvel 		} else if (!strcmp(param, "efi") && val) {
8791d150c0SArd Biesheuvel 			efi_nochunk = parse_option_str(val, "nochunk");
88d3549a93SArd Biesheuvel 			efi_novamap |= parse_option_str(val, "novamap");
89eeff7d63SArd Biesheuvel 
9091d150c0SArd Biesheuvel 			efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
9191d150c0SArd Biesheuvel 					    parse_option_str(val, "nosoftreserve");
925a17dae4SMatt Fleming 
9391d150c0SArd Biesheuvel 			if (parse_option_str(val, "disable_early_pci_dma"))
944444f854SMatthew Garrett 				efi_disable_pci_dma = true;
9591d150c0SArd Biesheuvel 			if (parse_option_str(val, "no_disable_early_pci_dma"))
964444f854SMatthew Garrett 				efi_disable_pci_dma = false;
9723d5b73fSArvind Sankar 			if (parse_option_str(val, "debug"))
9823d5b73fSArvind Sankar 				efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
99fffb6804SArvind Sankar 		} else if (!strcmp(param, "video") &&
100fffb6804SArvind Sankar 			   val && strstarts(val, "efifb:")) {
101fffb6804SArvind Sankar 			efi_parse_option_graphics(val + strlen("efifb:"));
1024444f854SMatthew Garrett 		}
1035a17dae4SMatt Fleming 	}
10491d150c0SArd Biesheuvel 	efi_bs_call(free_pool, buf);
1055a17dae4SMatt Fleming 	return EFI_SUCCESS;
1065a17dae4SMatt Fleming }
107f4f75ad5SArd Biesheuvel 
108f4f75ad5SArd Biesheuvel /*
1094a568ce2SArvind Sankar  * The EFI_LOAD_OPTION descriptor has the following layout:
1104a568ce2SArvind Sankar  *	u32 Attributes;
1114a568ce2SArvind Sankar  *	u16 FilePathListLength;
1124a568ce2SArvind Sankar  *	u16 Description[];
1134a568ce2SArvind Sankar  *	efi_device_path_protocol_t FilePathList[];
1144a568ce2SArvind Sankar  *	u8 OptionalData[];
1154a568ce2SArvind Sankar  *
1164a568ce2SArvind Sankar  * This function validates and unpacks the variable-size data fields.
1174a568ce2SArvind Sankar  */
1184a568ce2SArvind Sankar static
efi_load_option_unpack(efi_load_option_unpacked_t * dest,const efi_load_option_t * src,size_t size)1194a568ce2SArvind Sankar bool efi_load_option_unpack(efi_load_option_unpacked_t *dest,
1204a568ce2SArvind Sankar 			    const efi_load_option_t *src, size_t size)
1214a568ce2SArvind Sankar {
1224a568ce2SArvind Sankar 	const void *pos;
1234a568ce2SArvind Sankar 	u16 c;
1244a568ce2SArvind Sankar 	efi_device_path_protocol_t header;
1254a568ce2SArvind Sankar 	const efi_char16_t *description;
1264a568ce2SArvind Sankar 	const efi_device_path_protocol_t *file_path_list;
1274a568ce2SArvind Sankar 
1284a568ce2SArvind Sankar 	if (size < offsetof(efi_load_option_t, variable_data))
1294a568ce2SArvind Sankar 		return false;
1304a568ce2SArvind Sankar 	pos = src->variable_data;
1314a568ce2SArvind Sankar 	size -= offsetof(efi_load_option_t, variable_data);
1324a568ce2SArvind Sankar 
1334a568ce2SArvind Sankar 	if ((src->attributes & ~EFI_LOAD_OPTION_MASK) != 0)
1344a568ce2SArvind Sankar 		return false;
1354a568ce2SArvind Sankar 
1364a568ce2SArvind Sankar 	/* Scan description. */
1374a568ce2SArvind Sankar 	description = pos;
1384a568ce2SArvind Sankar 	do {
1394a568ce2SArvind Sankar 		if (size < sizeof(c))
1404a568ce2SArvind Sankar 			return false;
1414a568ce2SArvind Sankar 		c = *(const u16 *)pos;
1424a568ce2SArvind Sankar 		pos += sizeof(c);
1434a568ce2SArvind Sankar 		size -= sizeof(c);
1444a568ce2SArvind Sankar 	} while (c != L'\0');
1454a568ce2SArvind Sankar 
1464a568ce2SArvind Sankar 	/* Scan file_path_list. */
1474a568ce2SArvind Sankar 	file_path_list = pos;
1484a568ce2SArvind Sankar 	do {
1494a568ce2SArvind Sankar 		if (size < sizeof(header))
1504a568ce2SArvind Sankar 			return false;
1514a568ce2SArvind Sankar 		header = *(const efi_device_path_protocol_t *)pos;
1524a568ce2SArvind Sankar 		if (header.length < sizeof(header))
1534a568ce2SArvind Sankar 			return false;
1544a568ce2SArvind Sankar 		if (size < header.length)
1554a568ce2SArvind Sankar 			return false;
1564a568ce2SArvind Sankar 		pos += header.length;
1574a568ce2SArvind Sankar 		size -= header.length;
1584a568ce2SArvind Sankar 	} while ((header.type != EFI_DEV_END_PATH && header.type != EFI_DEV_END_PATH2) ||
1594a568ce2SArvind Sankar 		 (header.sub_type != EFI_DEV_END_ENTIRE));
1604a568ce2SArvind Sankar 	if (pos != (const void *)file_path_list + src->file_path_list_length)
1614a568ce2SArvind Sankar 		return false;
1624a568ce2SArvind Sankar 
1634a568ce2SArvind Sankar 	dest->attributes = src->attributes;
1644a568ce2SArvind Sankar 	dest->file_path_list_length = src->file_path_list_length;
1654a568ce2SArvind Sankar 	dest->description = description;
1664a568ce2SArvind Sankar 	dest->file_path_list = file_path_list;
1674a568ce2SArvind Sankar 	dest->optional_data_size = size;
1684a568ce2SArvind Sankar 	dest->optional_data = size ? pos : NULL;
1694a568ce2SArvind Sankar 
1704a568ce2SArvind Sankar 	return true;
1714a568ce2SArvind Sankar }
1724a568ce2SArvind Sankar 
1734a568ce2SArvind Sankar /*
1744a568ce2SArvind Sankar  * At least some versions of Dell firmware pass the entire contents of the
1754a568ce2SArvind Sankar  * Boot#### variable, i.e. the EFI_LOAD_OPTION descriptor, rather than just the
1764a568ce2SArvind Sankar  * OptionalData field.
1774a568ce2SArvind Sankar  *
1784a568ce2SArvind Sankar  * Detect this case and extract OptionalData.
1794a568ce2SArvind Sankar  */
efi_apply_loadoptions_quirk(const void ** load_options,u32 * load_options_size)180a241d94bSArd Biesheuvel void efi_apply_loadoptions_quirk(const void **load_options, u32 *load_options_size)
1814a568ce2SArvind Sankar {
1824a568ce2SArvind Sankar 	const efi_load_option_t *load_option = *load_options;
1834a568ce2SArvind Sankar 	efi_load_option_unpacked_t load_option_unpacked;
1844a568ce2SArvind Sankar 
1854a568ce2SArvind Sankar 	if (!IS_ENABLED(CONFIG_X86))
1864a568ce2SArvind Sankar 		return;
1874a568ce2SArvind Sankar 	if (!load_option)
1884a568ce2SArvind Sankar 		return;
1894a568ce2SArvind Sankar 	if (*load_options_size < sizeof(*load_option))
1904a568ce2SArvind Sankar 		return;
1914a568ce2SArvind Sankar 	if ((load_option->attributes & ~EFI_LOAD_OPTION_BOOT_MASK) != 0)
1924a568ce2SArvind Sankar 		return;
1934a568ce2SArvind Sankar 
1944a568ce2SArvind Sankar 	if (!efi_load_option_unpack(&load_option_unpacked, load_option, *load_options_size))
1954a568ce2SArvind Sankar 		return;
1964a568ce2SArvind Sankar 
1974a568ce2SArvind Sankar 	efi_warn_once(FW_BUG "LoadOptions is an EFI_LOAD_OPTION descriptor\n");
1984a568ce2SArvind Sankar 	efi_warn_once(FW_BUG "Using OptionalData as a workaround\n");
1994a568ce2SArvind Sankar 
2004a568ce2SArvind Sankar 	*load_options = load_option_unpacked.optional_data;
2014a568ce2SArvind Sankar 	*load_options_size = load_option_unpacked.optional_data_size;
2024a568ce2SArvind Sankar }
2034a568ce2SArvind Sankar 
20456633169SIlias Apalodimas enum efistub_event {
20556633169SIlias Apalodimas 	EFISTUB_EVT_INITRD,
20671c7adc9SIlias Apalodimas 	EFISTUB_EVT_LOAD_OPTIONS,
20756633169SIlias Apalodimas 	EFISTUB_EVT_COUNT,
20856633169SIlias Apalodimas };
20956633169SIlias Apalodimas 
21056633169SIlias Apalodimas #define STR_WITH_SIZE(s)	sizeof(s), s
21156633169SIlias Apalodimas 
21256633169SIlias Apalodimas static const struct {
21356633169SIlias Apalodimas 	u32		pcr_index;
21456633169SIlias Apalodimas 	u32		event_id;
21556633169SIlias Apalodimas 	u32		event_data_len;
21656633169SIlias Apalodimas 	u8		event_data[52];
21756633169SIlias Apalodimas } events[] = {
21856633169SIlias Apalodimas 	[EFISTUB_EVT_INITRD] = {
21956633169SIlias Apalodimas 		9,
22056633169SIlias Apalodimas 		INITRD_EVENT_TAG_ID,
22156633169SIlias Apalodimas 		STR_WITH_SIZE("Linux initrd")
22256633169SIlias Apalodimas 	},
22371c7adc9SIlias Apalodimas 	[EFISTUB_EVT_LOAD_OPTIONS] = {
22471c7adc9SIlias Apalodimas 		9,
22571c7adc9SIlias Apalodimas 		LOAD_OPTIONS_EVENT_TAG_ID,
22671c7adc9SIlias Apalodimas 		STR_WITH_SIZE("LOADED_IMAGE::LoadOptions")
22771c7adc9SIlias Apalodimas 	},
22856633169SIlias Apalodimas };
22956633169SIlias Apalodimas 
efi_measure_tagged_event(unsigned long load_addr,unsigned long load_size,enum efistub_event event)23056633169SIlias Apalodimas static efi_status_t efi_measure_tagged_event(unsigned long load_addr,
23156633169SIlias Apalodimas 					     unsigned long load_size,
23256633169SIlias Apalodimas 					     enum efistub_event event)
23356633169SIlias Apalodimas {
23456633169SIlias Apalodimas 	efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
23556633169SIlias Apalodimas 	efi_tcg2_protocol_t *tcg2 = NULL;
23656633169SIlias Apalodimas 	efi_status_t status;
23756633169SIlias Apalodimas 
23856633169SIlias Apalodimas 	efi_bs_call(locate_protocol, &tcg2_guid, NULL, (void **)&tcg2);
23956633169SIlias Apalodimas 	if (tcg2) {
24056633169SIlias Apalodimas 		struct efi_measured_event {
24156633169SIlias Apalodimas 			efi_tcg2_event_t	event_data;
24256633169SIlias Apalodimas 			efi_tcg2_tagged_event_t tagged_event;
24356633169SIlias Apalodimas 			u8			tagged_event_data[];
24456633169SIlias Apalodimas 		} *evt;
24556633169SIlias Apalodimas 		int size = sizeof(*evt) + events[event].event_data_len;
24656633169SIlias Apalodimas 
24756633169SIlias Apalodimas 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
24856633169SIlias Apalodimas 				     (void **)&evt);
24956633169SIlias Apalodimas 		if (status != EFI_SUCCESS)
25056633169SIlias Apalodimas 			goto fail;
25156633169SIlias Apalodimas 
25256633169SIlias Apalodimas 		evt->event_data = (struct efi_tcg2_event){
25356633169SIlias Apalodimas 			.event_size			= size,
25456633169SIlias Apalodimas 			.event_header.header_size	= sizeof(evt->event_data.event_header),
25556633169SIlias Apalodimas 			.event_header.header_version	= EFI_TCG2_EVENT_HEADER_VERSION,
25656633169SIlias Apalodimas 			.event_header.pcr_index		= events[event].pcr_index,
25756633169SIlias Apalodimas 			.event_header.event_type	= EV_EVENT_TAG,
25856633169SIlias Apalodimas 		};
25956633169SIlias Apalodimas 
26056633169SIlias Apalodimas 		evt->tagged_event = (struct efi_tcg2_tagged_event){
26156633169SIlias Apalodimas 			.tagged_event_id		= events[event].event_id,
26256633169SIlias Apalodimas 			.tagged_event_data_size		= events[event].event_data_len,
26356633169SIlias Apalodimas 		};
26456633169SIlias Apalodimas 
26556633169SIlias Apalodimas 		memcpy(evt->tagged_event_data, events[event].event_data,
26656633169SIlias Apalodimas 		       events[event].event_data_len);
26756633169SIlias Apalodimas 
26856633169SIlias Apalodimas 		status = efi_call_proto(tcg2, hash_log_extend_event, 0,
26956633169SIlias Apalodimas 					load_addr, load_size, &evt->event_data);
27056633169SIlias Apalodimas 		efi_bs_call(free_pool, evt);
27156633169SIlias Apalodimas 
27256633169SIlias Apalodimas 		if (status != EFI_SUCCESS)
27356633169SIlias Apalodimas 			goto fail;
27456633169SIlias Apalodimas 		return EFI_SUCCESS;
27556633169SIlias Apalodimas 	}
27656633169SIlias Apalodimas 
27756633169SIlias Apalodimas 	return EFI_UNSUPPORTED;
27856633169SIlias Apalodimas fail:
27956633169SIlias Apalodimas 	efi_warn("Failed to measure data for event %d: 0x%lx\n", event, status);
28056633169SIlias Apalodimas 	return status;
28156633169SIlias Apalodimas }
28256633169SIlias Apalodimas 
2834a568ce2SArvind Sankar /*
284f4f75ad5SArd Biesheuvel  * Convert the unicode UEFI command line to ASCII to pass to kernel.
285f4f75ad5SArd Biesheuvel  * Size of memory allocated return in *cmd_line_len.
286f4f75ad5SArd Biesheuvel  * Returns NULL on error.
287f4f75ad5SArd Biesheuvel  */
efi_convert_cmdline(efi_loaded_image_t * image,int * cmd_line_len)28827cd5511SArd Biesheuvel char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len)
289f4f75ad5SArd Biesheuvel {
290a241d94bSArd Biesheuvel 	const efi_char16_t *options = efi_table_attr(image, load_options);
291a241d94bSArd Biesheuvel 	u32 options_size = efi_table_attr(image, load_options_size);
29280b1bfe1SArvind Sankar 	int options_bytes = 0, safe_options_bytes = 0;  /* UTF-8 bytes */
293a241d94bSArd Biesheuvel 	unsigned long cmdline_addr = 0;
294a241d94bSArd Biesheuvel 	const efi_char16_t *s2;
29580b1bfe1SArvind Sankar 	bool in_quote = false;
296f4f75ad5SArd Biesheuvel 	efi_status_t status;
297a241d94bSArd Biesheuvel 	u32 options_chars;
298f4f75ad5SArd Biesheuvel 
29971c7adc9SIlias Apalodimas 	if (options_size > 0)
30071c7adc9SIlias Apalodimas 		efi_measure_tagged_event((unsigned long)options, options_size,
30171c7adc9SIlias Apalodimas 					 EFISTUB_EVT_LOAD_OPTIONS);
30271c7adc9SIlias Apalodimas 
303a241d94bSArd Biesheuvel 	efi_apply_loadoptions_quirk((const void **)&options, &options_size);
304a241d94bSArd Biesheuvel 	options_chars = options_size / sizeof(efi_char16_t);
3054a568ce2SArvind Sankar 
306f4f75ad5SArd Biesheuvel 	if (options) {
307f4f75ad5SArd Biesheuvel 		s2 = options;
30880b1bfe1SArvind Sankar 		while (options_bytes < COMMAND_LINE_SIZE && options_chars--) {
309a241d94bSArd Biesheuvel 			efi_char16_t c = *s2++;
31015c316bcSArvind Sankar 
31180b1bfe1SArvind Sankar 			if (c < 0x80) {
31215c316bcSArvind Sankar 				if (c == L'\0' || c == L'\n')
31315c316bcSArvind Sankar 					break;
31480b1bfe1SArvind Sankar 				if (c == L'"')
31580b1bfe1SArvind Sankar 					in_quote = !in_quote;
31680b1bfe1SArvind Sankar 				else if (!in_quote && isspace((char)c))
31780b1bfe1SArvind Sankar 					safe_options_bytes = options_bytes;
31880b1bfe1SArvind Sankar 
31980b1bfe1SArvind Sankar 				options_bytes++;
32080b1bfe1SArvind Sankar 				continue;
32180b1bfe1SArvind Sankar 			}
32280b1bfe1SArvind Sankar 
32315c316bcSArvind Sankar 			/*
32415c316bcSArvind Sankar 			 * Get the number of UTF-8 bytes corresponding to a
32515c316bcSArvind Sankar 			 * UTF-16 character.
32615c316bcSArvind Sankar 			 * The first part handles everything in the BMP.
32715c316bcSArvind Sankar 			 */
32880b1bfe1SArvind Sankar 			options_bytes += 2 + (c >= 0x800);
32915c316bcSArvind Sankar 			/*
33015c316bcSArvind Sankar 			 * Add one more byte for valid surrogate pairs. Invalid
33115c316bcSArvind Sankar 			 * surrogates will be replaced with 0xfffd and take up
33215c316bcSArvind Sankar 			 * only 3 bytes.
33315c316bcSArvind Sankar 			 */
33415c316bcSArvind Sankar 			if ((c & 0xfc00) == 0xd800) {
33515c316bcSArvind Sankar 				/*
33615c316bcSArvind Sankar 				 * If the very last word is a high surrogate,
33715c316bcSArvind Sankar 				 * we must ignore it since we can't access the
33815c316bcSArvind Sankar 				 * low surrogate.
33915c316bcSArvind Sankar 				 */
34004b24409SArvind Sankar 				if (!options_chars) {
34115c316bcSArvind Sankar 					options_bytes -= 3;
34215c316bcSArvind Sankar 				} else if ((*s2 & 0xfc00) == 0xdc00) {
34315c316bcSArvind Sankar 					options_bytes++;
34404b24409SArvind Sankar 					options_chars--;
34515c316bcSArvind Sankar 					s2++;
34615c316bcSArvind Sankar 				}
34715c316bcSArvind Sankar 			}
348f4f75ad5SArd Biesheuvel 		}
34980b1bfe1SArvind Sankar 		if (options_bytes >= COMMAND_LINE_SIZE) {
35080b1bfe1SArvind Sankar 			options_bytes = safe_options_bytes;
35180b1bfe1SArvind Sankar 			efi_err("Command line is too long: truncated to %d bytes\n",
35280b1bfe1SArvind Sankar 				options_bytes);
35380b1bfe1SArvind Sankar 		}
354f4f75ad5SArd Biesheuvel 	}
355f4f75ad5SArd Biesheuvel 
356f4f75ad5SArd Biesheuvel 	options_bytes++;	/* NUL termination */
357f4f75ad5SArd Biesheuvel 
35827cd5511SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, options_bytes,
35927cd5511SArd Biesheuvel 			     (void **)&cmdline_addr);
360f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
361f4f75ad5SArd Biesheuvel 		return NULL;
362f4f75ad5SArd Biesheuvel 
36304b24409SArvind Sankar 	snprintf((char *)cmdline_addr, options_bytes, "%.*ls",
36404b24409SArvind Sankar 		 options_bytes - 1, options);
365f4f75ad5SArd Biesheuvel 
366f4f75ad5SArd Biesheuvel 	*cmd_line_len = options_bytes;
367f4f75ad5SArd Biesheuvel 	return (char *)cmdline_addr;
368f4f75ad5SArd Biesheuvel }
369fc07716bSJeffrey Hugo 
3708c0a839cSHeinrich Schuchardt /**
3718c0a839cSHeinrich Schuchardt  * efi_exit_boot_services() - Exit boot services
3728c0a839cSHeinrich Schuchardt  * @handle:	handle of the exiting image
3738c0a839cSHeinrich Schuchardt  * @priv:	argument to be passed to @priv_func
3748c0a839cSHeinrich Schuchardt  * @priv_func:	function to process the memory map before exiting boot services
3758c0a839cSHeinrich Schuchardt  *
376fc07716bSJeffrey Hugo  * Handle calling ExitBootServices according to the requirements set out by the
377fc07716bSJeffrey Hugo  * spec.  Obtains the current memory map, and returns that info after calling
378fc07716bSJeffrey Hugo  * ExitBootServices.  The client must specify a function to perform any
379fc07716bSJeffrey Hugo  * processing of the memory map data prior to ExitBootServices.  A client
380fc07716bSJeffrey Hugo  * specific structure may be passed to the function via priv.  The client
381fc07716bSJeffrey Hugo  * function may be called multiple times.
3828c0a839cSHeinrich Schuchardt  *
3838c0a839cSHeinrich Schuchardt  * Return:	status code
384fc07716bSJeffrey Hugo  */
efi_exit_boot_services(void * handle,void * priv,efi_exit_boot_map_processing priv_func)385eab31265SArd Biesheuvel efi_status_t efi_exit_boot_services(void *handle, void *priv,
386fc07716bSJeffrey Hugo 				    efi_exit_boot_map_processing priv_func)
387fc07716bSJeffrey Hugo {
388eab31265SArd Biesheuvel 	struct efi_boot_memmap *map;
389fc07716bSJeffrey Hugo 	efi_status_t status;
390fc07716bSJeffrey Hugo 
3912e28a798SArd Biesheuvel 	if (efi_disable_pci_dma)
3922e28a798SArd Biesheuvel 		efi_pci_disable_bridge_busmaster();
3932e28a798SArd Biesheuvel 
394171539f5SArd Biesheuvel 	status = efi_get_memory_map(&map, true);
395fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
396a12b78b5SArd Biesheuvel 		return status;
397fc07716bSJeffrey Hugo 
398cd33a5c1SArd Biesheuvel 	status = priv_func(map, priv);
399a12b78b5SArd Biesheuvel 	if (status != EFI_SUCCESS) {
400a12b78b5SArd Biesheuvel 		efi_bs_call(free_pool, map);
401a12b78b5SArd Biesheuvel 		return status;
402a12b78b5SArd Biesheuvel 	}
403fc07716bSJeffrey Hugo 
404eab31265SArd Biesheuvel 	status = efi_bs_call(exit_boot_services, handle, map->map_key);
405fc07716bSJeffrey Hugo 
406fc07716bSJeffrey Hugo 	if (status == EFI_INVALID_PARAMETER) {
407fc07716bSJeffrey Hugo 		/*
408fc07716bSJeffrey Hugo 		 * The memory map changed between efi_get_memory_map() and
409fc07716bSJeffrey Hugo 		 * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
410fc07716bSJeffrey Hugo 		 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
411fc07716bSJeffrey Hugo 		 * updated map, and try again.  The spec implies one retry
412fc07716bSJeffrey Hugo 		 * should be sufficent, which is confirmed against the EDK2
413fc07716bSJeffrey Hugo 		 * implementation.  Per the spec, we can only invoke
414fc07716bSJeffrey Hugo 		 * get_memory_map() and exit_boot_services() - we cannot alloc
415fc07716bSJeffrey Hugo 		 * so efi_get_memory_map() cannot be used, and we must reuse
416fc07716bSJeffrey Hugo 		 * the buffer.  For all practical purposes, the headroom in the
417fc07716bSJeffrey Hugo 		 * buffer should account for any changes in the map so the call
418fc07716bSJeffrey Hugo 		 * to get_memory_map() is expected to succeed here.
419fc07716bSJeffrey Hugo 		 */
420eab31265SArd Biesheuvel 		map->map_size = map->buff_size;
421966291f6SArd Biesheuvel 		status = efi_bs_call(get_memory_map,
422eab31265SArd Biesheuvel 				     &map->map_size,
423eab31265SArd Biesheuvel 				     &map->map,
424eab31265SArd Biesheuvel 				     &map->map_key,
425eab31265SArd Biesheuvel 				     &map->desc_size,
426eab31265SArd Biesheuvel 				     &map->desc_ver);
427fc07716bSJeffrey Hugo 
428fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
429fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
430a12b78b5SArd Biesheuvel 			return status;
431fc07716bSJeffrey Hugo 
432cd33a5c1SArd Biesheuvel 		status = priv_func(map, priv);
433fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
434fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
435a12b78b5SArd Biesheuvel 			return status;
436fc07716bSJeffrey Hugo 
437eab31265SArd Biesheuvel 		status = efi_bs_call(exit_boot_services, handle, map->map_key);
438fc07716bSJeffrey Hugo 	}
439fc07716bSJeffrey Hugo 
440fc07716bSJeffrey Hugo 	return status;
441fc07716bSJeffrey Hugo }
44282d736acSMatthew Garrett 
4438c0a839cSHeinrich Schuchardt /**
4448c0a839cSHeinrich Schuchardt  * get_efi_config_table() - retrieve UEFI configuration table
4458c0a839cSHeinrich Schuchardt  * @guid:	GUID of the configuration table to be retrieved
4468c0a839cSHeinrich Schuchardt  * Return:	pointer to the configuration table or NULL
4478c0a839cSHeinrich Schuchardt  */
get_efi_config_table(efi_guid_t guid)448cd33a5c1SArd Biesheuvel void *get_efi_config_table(efi_guid_t guid)
44982d736acSMatthew Garrett {
450ccc27ae7SArd Biesheuvel 	unsigned long tables = efi_table_attr(efi_system_table, tables);
451ccc27ae7SArd Biesheuvel 	int nr_tables = efi_table_attr(efi_system_table, nr_tables);
452f958efe9SArd Biesheuvel 	int i;
453f958efe9SArd Biesheuvel 
454f958efe9SArd Biesheuvel 	for (i = 0; i < nr_tables; i++) {
455f958efe9SArd Biesheuvel 		efi_config_table_t *t = (void *)tables;
456f958efe9SArd Biesheuvel 
457f958efe9SArd Biesheuvel 		if (efi_guidcmp(t->guid, guid) == 0)
45899ea8b1dSArd Biesheuvel 			return efi_table_attr(t, table);
459f958efe9SArd Biesheuvel 
460f958efe9SArd Biesheuvel 		tables += efi_is_native() ? sizeof(efi_config_table_t)
461f958efe9SArd Biesheuvel 					  : sizeof(efi_config_table_32_t);
462f958efe9SArd Biesheuvel 	}
463f958efe9SArd Biesheuvel 	return NULL;
46482d736acSMatthew Garrett }
465dc29da14SArd Biesheuvel 
466ec93fc37SArd Biesheuvel /*
467ec93fc37SArd Biesheuvel  * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
468ec93fc37SArd Biesheuvel  * for the firmware or bootloader to expose the initrd data directly to the stub
469ec93fc37SArd Biesheuvel  * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
470ec93fc37SArd Biesheuvel  * very easy to implement. It is a simple Linux initrd specific conduit between
471ec93fc37SArd Biesheuvel  * kernel and firmware, allowing us to put the EFI stub (being part of the
472ec93fc37SArd Biesheuvel  * kernel) in charge of where and when to load the initrd, while leaving it up
473ec93fc37SArd Biesheuvel  * to the firmware to decide whether it needs to expose its filesystem hierarchy
474ec93fc37SArd Biesheuvel  * via EFI protocols.
475ec93fc37SArd Biesheuvel  */
476ec93fc37SArd Biesheuvel static const struct {
477ec93fc37SArd Biesheuvel 	struct efi_vendor_dev_path	vendor;
478ec93fc37SArd Biesheuvel 	struct efi_generic_dev_path	end;
479ec93fc37SArd Biesheuvel } __packed initrd_dev_path = {
480ec93fc37SArd Biesheuvel 	{
481ec93fc37SArd Biesheuvel 		{
482ec93fc37SArd Biesheuvel 			EFI_DEV_MEDIA,
483ec93fc37SArd Biesheuvel 			EFI_DEV_MEDIA_VENDOR,
484ec93fc37SArd Biesheuvel 			sizeof(struct efi_vendor_dev_path),
485ec93fc37SArd Biesheuvel 		},
486ec93fc37SArd Biesheuvel 		LINUX_EFI_INITRD_MEDIA_GUID
487ec93fc37SArd Biesheuvel 	}, {
488ec93fc37SArd Biesheuvel 		EFI_DEV_END_PATH,
489ec93fc37SArd Biesheuvel 		EFI_DEV_END_ENTIRE,
490ec93fc37SArd Biesheuvel 		sizeof(struct efi_generic_dev_path)
491ec93fc37SArd Biesheuvel 	}
492ec93fc37SArd Biesheuvel };
493ec93fc37SArd Biesheuvel 
494ec93fc37SArd Biesheuvel /**
4958c0a839cSHeinrich Schuchardt  * efi_load_initrd_dev_path() - load the initrd from the Linux initrd device path
496d981a88cSJialin Zhang  * @initrd:	pointer of struct to store the address where the initrd was loaded
497d981a88cSJialin Zhang  *		and the size of the loaded initrd
498ec93fc37SArd Biesheuvel  * @max:	upper limit for the initrd memory allocation
4998c0a839cSHeinrich Schuchardt  *
5008c0a839cSHeinrich Schuchardt  * Return:
5018c0a839cSHeinrich Schuchardt  * * %EFI_SUCCESS if the initrd was loaded successfully, in which
502ec93fc37SArd Biesheuvel  *   case @load_addr and @load_size are assigned accordingly
5038c0a839cSHeinrich Schuchardt  * * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd device path
5048c0a839cSHeinrich Schuchardt  * * %EFI_OUT_OF_RESOURCES if memory allocation failed
5058c0a839cSHeinrich Schuchardt  * * %EFI_LOAD_ERROR in all other cases
506ec93fc37SArd Biesheuvel  */
507f61900fdSArvind Sankar static
efi_load_initrd_dev_path(struct linux_efi_initrd * initrd,unsigned long max)508f4dc7fffSArd Biesheuvel efi_status_t efi_load_initrd_dev_path(struct linux_efi_initrd *initrd,
509ec93fc37SArd Biesheuvel 				      unsigned long max)
510ec93fc37SArd Biesheuvel {
511ec93fc37SArd Biesheuvel 	efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
512ec93fc37SArd Biesheuvel 	efi_device_path_protocol_t *dp;
513ec93fc37SArd Biesheuvel 	efi_load_file2_protocol_t *lf2;
514ec93fc37SArd Biesheuvel 	efi_handle_t handle;
515ec93fc37SArd Biesheuvel 	efi_status_t status;
516ec93fc37SArd Biesheuvel 
517ec93fc37SArd Biesheuvel 	dp = (efi_device_path_protocol_t *)&initrd_dev_path;
518ec93fc37SArd Biesheuvel 	status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
519ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
520ec93fc37SArd Biesheuvel 		return status;
521ec93fc37SArd Biesheuvel 
522ec93fc37SArd Biesheuvel 	status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
523ec93fc37SArd Biesheuvel 			     (void **)&lf2);
524ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
525ec93fc37SArd Biesheuvel 		return status;
526ec93fc37SArd Biesheuvel 
527f4dc7fffSArd Biesheuvel 	initrd->size = 0;
528f4dc7fffSArd Biesheuvel 	status = efi_call_proto(lf2, load_file, dp, false, &initrd->size, NULL);
529ec93fc37SArd Biesheuvel 	if (status != EFI_BUFFER_TOO_SMALL)
530ec93fc37SArd Biesheuvel 		return EFI_LOAD_ERROR;
531ec93fc37SArd Biesheuvel 
532f4dc7fffSArd Biesheuvel 	status = efi_allocate_pages(initrd->size, &initrd->base, max);
533ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
534ec93fc37SArd Biesheuvel 		return status;
535ec93fc37SArd Biesheuvel 
536f4dc7fffSArd Biesheuvel 	status = efi_call_proto(lf2, load_file, dp, false, &initrd->size,
537f4dc7fffSArd Biesheuvel 				(void *)initrd->base);
538ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS) {
539f4dc7fffSArd Biesheuvel 		efi_free(initrd->size, initrd->base);
540ec93fc37SArd Biesheuvel 		return EFI_LOAD_ERROR;
541ec93fc37SArd Biesheuvel 	}
542ec93fc37SArd Biesheuvel 	return EFI_SUCCESS;
543ec93fc37SArd Biesheuvel }
544f61900fdSArvind Sankar 
545f61900fdSArvind Sankar static
efi_load_initrd_cmdline(efi_loaded_image_t * image,struct linux_efi_initrd * initrd,unsigned long soft_limit,unsigned long hard_limit)546f61900fdSArvind Sankar efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
547f4dc7fffSArd Biesheuvel 				     struct linux_efi_initrd *initrd,
548f61900fdSArvind Sankar 				     unsigned long soft_limit,
549f61900fdSArvind Sankar 				     unsigned long hard_limit)
550f61900fdSArvind Sankar {
551e346bebbSArd Biesheuvel 	if (image == NULL)
552f4dc7fffSArd Biesheuvel 		return EFI_UNSUPPORTED;
553f61900fdSArvind Sankar 
554f61900fdSArvind Sankar 	return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
555f61900fdSArvind Sankar 				    soft_limit, hard_limit,
556f4dc7fffSArd Biesheuvel 				    &initrd->base, &initrd->size);
557f61900fdSArvind Sankar }
558f61900fdSArvind Sankar 
5598c0a839cSHeinrich Schuchardt /**
5608c0a839cSHeinrich Schuchardt  * efi_load_initrd() - Load initial RAM disk
5618c0a839cSHeinrich Schuchardt  * @image:	EFI loaded image protocol
562947228cbSAtish Patra  * @soft_limit:	preferred address for loading the initrd
563947228cbSAtish Patra  * @hard_limit:	upper limit address for loading the initrd
5648c0a839cSHeinrich Schuchardt  *
5658c0a839cSHeinrich Schuchardt  * Return:	status code
5668c0a839cSHeinrich Schuchardt  */
efi_load_initrd(efi_loaded_image_t * image,unsigned long soft_limit,unsigned long hard_limit,const struct linux_efi_initrd ** out)567f61900fdSArvind Sankar efi_status_t efi_load_initrd(efi_loaded_image_t *image,
568f61900fdSArvind Sankar 			     unsigned long soft_limit,
569f4dc7fffSArd Biesheuvel 			     unsigned long hard_limit,
570f4dc7fffSArd Biesheuvel 			     const struct linux_efi_initrd **out)
571f61900fdSArvind Sankar {
572f4dc7fffSArd Biesheuvel 	efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID;
573f4dc7fffSArd Biesheuvel 	efi_status_t status = EFI_SUCCESS;
574f4dc7fffSArd Biesheuvel 	struct linux_efi_initrd initrd, *tbl;
575f61900fdSArvind Sankar 
576f4dc7fffSArd Biesheuvel 	if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD) || efi_noinitrd)
577f4dc7fffSArd Biesheuvel 		return EFI_SUCCESS;
578f4dc7fffSArd Biesheuvel 
579f4dc7fffSArd Biesheuvel 	status = efi_load_initrd_dev_path(&initrd, hard_limit);
580f61900fdSArvind Sankar 	if (status == EFI_SUCCESS) {
581f61900fdSArvind Sankar 		efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
58256633169SIlias Apalodimas 		if (initrd.size > 0 &&
58356633169SIlias Apalodimas 		    efi_measure_tagged_event(initrd.base, initrd.size,
58456633169SIlias Apalodimas 					     EFISTUB_EVT_INITRD) == EFI_SUCCESS)
58556633169SIlias Apalodimas 			efi_info("Measured initrd data into PCR 9\n");
586f61900fdSArvind Sankar 	} else if (status == EFI_NOT_FOUND) {
587f4dc7fffSArd Biesheuvel 		status = efi_load_initrd_cmdline(image, &initrd, soft_limit,
588f4dc7fffSArd Biesheuvel 						 hard_limit);
589f4dc7fffSArd Biesheuvel 		/* command line loader disabled or no initrd= passed? */
590f4dc7fffSArd Biesheuvel 		if (status == EFI_UNSUPPORTED || status == EFI_NOT_READY)
591f4dc7fffSArd Biesheuvel 			return EFI_SUCCESS;
592f4dc7fffSArd Biesheuvel 		if (status == EFI_SUCCESS)
593f61900fdSArvind Sankar 			efi_info("Loaded initrd from command line option\n");
594f61900fdSArvind Sankar 	}
595f4dc7fffSArd Biesheuvel 	if (status != EFI_SUCCESS)
596f4dc7fffSArd Biesheuvel 		goto failed;
597f046fff8SIlias Apalodimas 
598f4dc7fffSArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(initrd),
599f4dc7fffSArd Biesheuvel 			     (void **)&tbl);
600f4dc7fffSArd Biesheuvel 	if (status != EFI_SUCCESS)
601f4dc7fffSArd Biesheuvel 		goto free_initrd;
602f4dc7fffSArd Biesheuvel 
603f4dc7fffSArd Biesheuvel 	*tbl = initrd;
604f4dc7fffSArd Biesheuvel 	status = efi_bs_call(install_configuration_table, &tbl_guid, tbl);
605f4dc7fffSArd Biesheuvel 	if (status != EFI_SUCCESS)
606f4dc7fffSArd Biesheuvel 		goto free_tbl;
607f4dc7fffSArd Biesheuvel 
608f4dc7fffSArd Biesheuvel 	if (out)
609f4dc7fffSArd Biesheuvel 		*out = tbl;
610f4dc7fffSArd Biesheuvel 	return EFI_SUCCESS;
611f4dc7fffSArd Biesheuvel 
612f4dc7fffSArd Biesheuvel free_tbl:
613f4dc7fffSArd Biesheuvel 	efi_bs_call(free_pool, tbl);
614f4dc7fffSArd Biesheuvel free_initrd:
615f4dc7fffSArd Biesheuvel 	efi_free(initrd.size, initrd.base);
616f4dc7fffSArd Biesheuvel failed:
617f4dc7fffSArd Biesheuvel 	efi_err("Failed to load initrd: 0x%lx\n", status);
618f61900fdSArvind Sankar 	return status;
619f61900fdSArvind Sankar }
62014c574f3SArvind Sankar 
6218c0a839cSHeinrich Schuchardt /**
6228c0a839cSHeinrich Schuchardt  * efi_wait_for_key() - Wait for key stroke
6238c0a839cSHeinrich Schuchardt  * @usec:	number of microseconds to wait for key stroke
6248c0a839cSHeinrich Schuchardt  * @key:	key entered
6258c0a839cSHeinrich Schuchardt  *
6268c0a839cSHeinrich Schuchardt  * Wait for up to @usec microseconds for a key stroke.
6278c0a839cSHeinrich Schuchardt  *
6288c0a839cSHeinrich Schuchardt  * Return:	status code, EFI_SUCCESS if key received
6298c0a839cSHeinrich Schuchardt  */
efi_wait_for_key(unsigned long usec,efi_input_key_t * key)63014c574f3SArvind Sankar efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
63114c574f3SArvind Sankar {
63214c574f3SArvind Sankar 	efi_event_t events[2], timer;
63314c574f3SArvind Sankar 	unsigned long index;
63414c574f3SArvind Sankar 	efi_simple_text_input_protocol_t *con_in;
63514c574f3SArvind Sankar 	efi_status_t status;
63614c574f3SArvind Sankar 
63714c574f3SArvind Sankar 	con_in = efi_table_attr(efi_system_table, con_in);
63814c574f3SArvind Sankar 	if (!con_in)
63914c574f3SArvind Sankar 		return EFI_UNSUPPORTED;
64014c574f3SArvind Sankar 	efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
64114c574f3SArvind Sankar 
64214c574f3SArvind Sankar 	status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
64314c574f3SArvind Sankar 	if (status != EFI_SUCCESS)
64414c574f3SArvind Sankar 		return status;
64514c574f3SArvind Sankar 
64614c574f3SArvind Sankar 	status = efi_bs_call(set_timer, timer, EfiTimerRelative,
64714c574f3SArvind Sankar 			     EFI_100NSEC_PER_USEC * usec);
64814c574f3SArvind Sankar 	if (status != EFI_SUCCESS)
64914c574f3SArvind Sankar 		return status;
65014c574f3SArvind Sankar 	efi_set_event_at(events, 1, timer);
65114c574f3SArvind Sankar 
65214c574f3SArvind Sankar 	status = efi_bs_call(wait_for_event, 2, events, &index);
65314c574f3SArvind Sankar 	if (status == EFI_SUCCESS) {
65414c574f3SArvind Sankar 		if (index == 0)
65514c574f3SArvind Sankar 			status = efi_call_proto(con_in, read_keystroke, key);
65614c574f3SArvind Sankar 		else
65714c574f3SArvind Sankar 			status = EFI_TIMEOUT;
65814c574f3SArvind Sankar 	}
65914c574f3SArvind Sankar 
66014c574f3SArvind Sankar 	efi_bs_call(close_event, timer);
66114c574f3SArvind Sankar 
66214c574f3SArvind Sankar 	return status;
66314c574f3SArvind Sankar }
664ace013a5SArd Biesheuvel 
665ace013a5SArd Biesheuvel /**
666ace013a5SArd Biesheuvel  * efi_remap_image - Remap a loaded image with the appropriate permissions
667ace013a5SArd Biesheuvel  *                   for code and data
668ace013a5SArd Biesheuvel  *
669ace013a5SArd Biesheuvel  * @image_base:	the base of the image in memory
670ace013a5SArd Biesheuvel  * @alloc_size:	the size of the area in memory occupied by the image
671ace013a5SArd Biesheuvel  * @code_size:	the size of the leading part of the image containing code
672ace013a5SArd Biesheuvel  * 		and read-only data
673ace013a5SArd Biesheuvel  *
674ace013a5SArd Biesheuvel  * efi_remap_image() uses the EFI memory attribute protocol to remap the code
675ace013a5SArd Biesheuvel  * region of the loaded image read-only/executable, and the remainder
676ace013a5SArd Biesheuvel  * read-write/non-executable. The code region is assumed to start at the base
677ace013a5SArd Biesheuvel  * of the image, and will therefore cover the PE/COFF header as well.
678ace013a5SArd Biesheuvel  */
efi_remap_image(unsigned long image_base,unsigned alloc_size,unsigned long code_size)679ace013a5SArd Biesheuvel void efi_remap_image(unsigned long image_base, unsigned alloc_size,
680ace013a5SArd Biesheuvel 		     unsigned long code_size)
681ace013a5SArd Biesheuvel {
682ace013a5SArd Biesheuvel 	efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
683ace013a5SArd Biesheuvel 	efi_memory_attribute_protocol_t *memattr;
684ace013a5SArd Biesheuvel 	efi_status_t status;
685ace013a5SArd Biesheuvel 	u64 attr;
686ace013a5SArd Biesheuvel 
687ace013a5SArd Biesheuvel 	/*
688ace013a5SArd Biesheuvel 	 * If the firmware implements the EFI_MEMORY_ATTRIBUTE_PROTOCOL, let's
689ace013a5SArd Biesheuvel 	 * invoke it to remap the text/rodata region of the decompressed image
690ace013a5SArd Biesheuvel 	 * as read-only and the data/bss region as non-executable.
691ace013a5SArd Biesheuvel 	 */
692ace013a5SArd Biesheuvel 	status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
693ace013a5SArd Biesheuvel 	if (status != EFI_SUCCESS)
694ace013a5SArd Biesheuvel 		return;
695ace013a5SArd Biesheuvel 
696ace013a5SArd Biesheuvel 	// Get the current attributes for the entire region
697ace013a5SArd Biesheuvel 	status = memattr->get_memory_attributes(memattr, image_base,
698ace013a5SArd Biesheuvel 						alloc_size, &attr);
699ace013a5SArd Biesheuvel 	if (status != EFI_SUCCESS) {
700ace013a5SArd Biesheuvel 		efi_warn("Failed to retrieve memory attributes for image region: 0x%lx\n",
701ace013a5SArd Biesheuvel 			 status);
702ace013a5SArd Biesheuvel 		return;
703ace013a5SArd Biesheuvel 	}
704ace013a5SArd Biesheuvel 
705ace013a5SArd Biesheuvel 	// Mark the code region as read-only
706ace013a5SArd Biesheuvel 	status = memattr->set_memory_attributes(memattr, image_base, code_size,
707ace013a5SArd Biesheuvel 						EFI_MEMORY_RO);
708ace013a5SArd Biesheuvel 	if (status != EFI_SUCCESS) {
709ace013a5SArd Biesheuvel 		efi_warn("Failed to remap code region read-only\n");
710ace013a5SArd Biesheuvel 		return;
711ace013a5SArd Biesheuvel 	}
712ace013a5SArd Biesheuvel 
713ace013a5SArd Biesheuvel 	// If the entire region was already mapped as non-exec, clear the
714ace013a5SArd Biesheuvel 	// attribute from the code region. Otherwise, set it on the data
715ace013a5SArd Biesheuvel 	// region.
716ace013a5SArd Biesheuvel 	if (attr & EFI_MEMORY_XP) {
717ace013a5SArd Biesheuvel 		status = memattr->clear_memory_attributes(memattr, image_base,
718ace013a5SArd Biesheuvel 							  code_size,
719ace013a5SArd Biesheuvel 							  EFI_MEMORY_XP);
720ace013a5SArd Biesheuvel 		if (status != EFI_SUCCESS)
721ace013a5SArd Biesheuvel 			efi_warn("Failed to remap code region executable\n");
722ace013a5SArd Biesheuvel 	} else {
723ace013a5SArd Biesheuvel 		status = memattr->set_memory_attributes(memattr,
724ace013a5SArd Biesheuvel 							image_base + code_size,
725ace013a5SArd Biesheuvel 							alloc_size - code_size,
726ace013a5SArd Biesheuvel 							EFI_MEMORY_XP);
727ace013a5SArd Biesheuvel 		if (status != EFI_SUCCESS)
728ace013a5SArd Biesheuvel 			efi_warn("Failed to remap data region non-executable\n");
729ace013a5SArd Biesheuvel 	}
730ace013a5SArd Biesheuvel }
731