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 
102c7d1e30SArvind Sankar #include <stdarg.h>
112c7d1e30SArvind Sankar 
1280b1bfe1SArvind Sankar #include <linux/ctype.h>
13f4f75ad5SArd Biesheuvel #include <linux/efi.h>
14fd0528a2SArvind Sankar #include <linux/kernel.h>
1523d5b73fSArvind Sankar #include <linux/printk.h> /* For CONSOLE_LOGLEVEL_* */
16f4f75ad5SArd Biesheuvel #include <asm/efi.h>
1780b1bfe1SArvind Sankar #include <asm/setup.h>
18f4f75ad5SArd Biesheuvel 
19f4f75ad5SArd Biesheuvel #include "efistub.h"
20f4f75ad5SArd Biesheuvel 
21980771f6SArd Biesheuvel bool efi_nochunk;
227c116db2SWill Deacon bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE);
23980771f6SArd Biesheuvel bool efi_noinitrd;
2423d5b73fSArvind Sankar int efi_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
25980771f6SArd Biesheuvel bool efi_novamap;
26980771f6SArd Biesheuvel 
2754439370SArvind Sankar static bool efi_nosoftreserve;
2854439370SArvind Sankar static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
2960f38de7SArd Biesheuvel 
30b617c526SDan Williams bool __pure __efi_soft_reserve_enabled(void)
31b617c526SDan Williams {
32b617c526SDan Williams 	return !efi_nosoftreserve;
33b617c526SDan Williams }
3460f38de7SArd Biesheuvel 
358c0a839cSHeinrich Schuchardt /**
368c0a839cSHeinrich Schuchardt  * efi_char16_puts() - Write a UCS-2 encoded string to the console
378c0a839cSHeinrich Schuchardt  * @str:	UCS-2 encoded string
388c0a839cSHeinrich Schuchardt  */
39cb8c90a0SArvind Sankar void efi_char16_puts(efi_char16_t *str)
40cb8c90a0SArvind Sankar {
41cb8c90a0SArvind Sankar 	efi_call_proto(efi_table_attr(efi_system_table, con_out),
42cb8c90a0SArvind Sankar 		       output_string, str);
43cb8c90a0SArvind Sankar }
44cb8c90a0SArvind Sankar 
454b75bd36SArvind Sankar static
464b75bd36SArvind Sankar u32 utf8_to_utf32(const u8 **s8)
474b75bd36SArvind Sankar {
484b75bd36SArvind Sankar 	u32 c32;
494b75bd36SArvind Sankar 	u8 c0, cx;
504b75bd36SArvind Sankar 	size_t clen, i;
514b75bd36SArvind Sankar 
524b75bd36SArvind Sankar 	c0 = cx = *(*s8)++;
534b75bd36SArvind Sankar 	/*
544b75bd36SArvind Sankar 	 * The position of the most-significant 0 bit gives us the length of
554b75bd36SArvind Sankar 	 * a multi-octet encoding.
564b75bd36SArvind Sankar 	 */
574b75bd36SArvind Sankar 	for (clen = 0; cx & 0x80; ++clen)
584b75bd36SArvind Sankar 		cx <<= 1;
594b75bd36SArvind Sankar 	/*
604b75bd36SArvind Sankar 	 * If the 0 bit is in position 8, this is a valid single-octet
614b75bd36SArvind Sankar 	 * encoding. If the 0 bit is in position 7 or positions 1-3, the
624b75bd36SArvind Sankar 	 * encoding is invalid.
634b75bd36SArvind Sankar 	 * In either case, we just return the first octet.
644b75bd36SArvind Sankar 	 */
654b75bd36SArvind Sankar 	if (clen < 2 || clen > 4)
664b75bd36SArvind Sankar 		return c0;
674b75bd36SArvind Sankar 	/* Get the bits from the first octet. */
684b75bd36SArvind Sankar 	c32 = cx >> clen--;
694b75bd36SArvind Sankar 	for (i = 0; i < clen; ++i) {
704b75bd36SArvind Sankar 		/* Trailing octets must have 10 in most significant bits. */
714b75bd36SArvind Sankar 		cx = (*s8)[i] ^ 0x80;
724b75bd36SArvind Sankar 		if (cx & 0xc0)
734b75bd36SArvind Sankar 			return c0;
744b75bd36SArvind Sankar 		c32 = (c32 << 6) | cx;
754b75bd36SArvind Sankar 	}
764b75bd36SArvind Sankar 	/*
774b75bd36SArvind Sankar 	 * Check for validity:
784b75bd36SArvind Sankar 	 * - The character must be in the Unicode range.
794b75bd36SArvind Sankar 	 * - It must not be a surrogate.
804b75bd36SArvind Sankar 	 * - It must be encoded using the correct number of octets.
814b75bd36SArvind Sankar 	 */
824b75bd36SArvind Sankar 	if (c32 > 0x10ffff ||
834b75bd36SArvind Sankar 	    (c32 & 0xf800) == 0xd800 ||
844b75bd36SArvind Sankar 	    clen != (c32 >= 0x80) + (c32 >= 0x800) + (c32 >= 0x10000))
854b75bd36SArvind Sankar 		return c0;
864b75bd36SArvind Sankar 	*s8 += clen;
874b75bd36SArvind Sankar 	return c32;
884b75bd36SArvind Sankar }
894b75bd36SArvind Sankar 
908c0a839cSHeinrich Schuchardt /**
918c0a839cSHeinrich Schuchardt  * efi_puts() - Write a UTF-8 encoded string to the console
928c0a839cSHeinrich Schuchardt  * @str:	UTF-8 encoded string
938c0a839cSHeinrich Schuchardt  */
94cb8c90a0SArvind Sankar void efi_puts(const char *str)
95f4f75ad5SArd Biesheuvel {
96fd0528a2SArvind Sankar 	efi_char16_t buf[128];
97fd0528a2SArvind Sankar 	size_t pos = 0, lim = ARRAY_SIZE(buf);
984b75bd36SArvind Sankar 	const u8 *s8 = (const u8 *)str;
994b75bd36SArvind Sankar 	u32 c32;
100f4f75ad5SArd Biesheuvel 
1014b75bd36SArvind Sankar 	while (*s8) {
1024b75bd36SArvind Sankar 		if (*s8 == '\n')
103fd0528a2SArvind Sankar 			buf[pos++] = L'\r';
1044b75bd36SArvind Sankar 		c32 = utf8_to_utf32(&s8);
1054b75bd36SArvind Sankar 		if (c32 < 0x10000) {
1064b75bd36SArvind Sankar 			/* Characters in plane 0 use a single word. */
1074b75bd36SArvind Sankar 			buf[pos++] = c32;
1084b75bd36SArvind Sankar 		} else {
1094b75bd36SArvind Sankar 			/*
1104b75bd36SArvind Sankar 			 * Characters in other planes encode into a surrogate
1114b75bd36SArvind Sankar 			 * pair.
1124b75bd36SArvind Sankar 			 */
1134b75bd36SArvind Sankar 			buf[pos++] = (0xd800 - (0x10000 >> 10)) + (c32 >> 10);
1144b75bd36SArvind Sankar 			buf[pos++] = 0xdc00 + (c32 & 0x3ff);
1154b75bd36SArvind Sankar 		}
1164b75bd36SArvind Sankar 		if (*s8 == '\0' || pos >= lim - 2) {
117fd0528a2SArvind Sankar 			buf[pos] = L'\0';
118fd0528a2SArvind Sankar 			efi_char16_puts(buf);
119fd0528a2SArvind Sankar 			pos = 0;
120fd0528a2SArvind Sankar 		}
121f4f75ad5SArd Biesheuvel 	}
122f4f75ad5SArd Biesheuvel }
123f4f75ad5SArd Biesheuvel 
1248c0a839cSHeinrich Schuchardt /**
1258c0a839cSHeinrich Schuchardt  * efi_printk() - Print a kernel message
1268c0a839cSHeinrich Schuchardt  * @fmt:	format string
1278c0a839cSHeinrich Schuchardt  *
1288c0a839cSHeinrich Schuchardt  * The first letter of the format string is used to determine the logging level
1298c0a839cSHeinrich Schuchardt  * of the message. If the level is less then the current EFI logging level, the
1308c0a839cSHeinrich Schuchardt  * message is suppressed. The message will be truncated to 255 bytes.
1318c0a839cSHeinrich Schuchardt  *
1328c0a839cSHeinrich Schuchardt  * Return:	number of printed characters
1338c0a839cSHeinrich Schuchardt  */
1342c7d1e30SArvind Sankar int efi_printk(const char *fmt, ...)
1352c7d1e30SArvind Sankar {
1362c7d1e30SArvind Sankar 	char printf_buf[256];
1372c7d1e30SArvind Sankar 	va_list args;
1382c7d1e30SArvind Sankar 	int printed;
13923d5b73fSArvind Sankar 	int loglevel = printk_get_level(fmt);
14023d5b73fSArvind Sankar 
14123d5b73fSArvind Sankar 	switch (loglevel) {
14223d5b73fSArvind Sankar 	case '0' ... '9':
14323d5b73fSArvind Sankar 		loglevel -= '0';
14423d5b73fSArvind Sankar 		break;
14523d5b73fSArvind Sankar 	default:
14623d5b73fSArvind Sankar 		/*
14723d5b73fSArvind Sankar 		 * Use loglevel -1 for cases where we just want to print to
14823d5b73fSArvind Sankar 		 * the screen.
14923d5b73fSArvind Sankar 		 */
15023d5b73fSArvind Sankar 		loglevel = -1;
15123d5b73fSArvind Sankar 		break;
15223d5b73fSArvind Sankar 	}
15323d5b73fSArvind Sankar 
15423d5b73fSArvind Sankar 	if (loglevel >= efi_loglevel)
15523d5b73fSArvind Sankar 		return 0;
15623d5b73fSArvind Sankar 
15723d5b73fSArvind Sankar 	if (loglevel >= 0)
15823d5b73fSArvind Sankar 		efi_puts("EFI stub: ");
15923d5b73fSArvind Sankar 
16023d5b73fSArvind Sankar 	fmt = printk_skip_level(fmt);
1612c7d1e30SArvind Sankar 
1622c7d1e30SArvind Sankar 	va_start(args, fmt);
1638fb331e1SArvind Sankar 	printed = vsnprintf(printf_buf, sizeof(printf_buf), fmt, args);
1642c7d1e30SArvind Sankar 	va_end(args);
1652c7d1e30SArvind Sankar 
1662c7d1e30SArvind Sankar 	efi_puts(printf_buf);
1678fb331e1SArvind Sankar 	if (printed >= sizeof(printf_buf)) {
1688fb331e1SArvind Sankar 		efi_puts("[Message truncated]\n");
1698fb331e1SArvind Sankar 		return -1;
1708fb331e1SArvind Sankar 	}
1712c7d1e30SArvind Sankar 
1722c7d1e30SArvind Sankar 	return printed;
1732c7d1e30SArvind Sankar }
1742c7d1e30SArvind Sankar 
1758c0a839cSHeinrich Schuchardt /**
1768c0a839cSHeinrich Schuchardt  * efi_parse_options() - Parse EFI command line options
1778c0a839cSHeinrich Schuchardt  * @cmdline:	kernel command line
1788c0a839cSHeinrich Schuchardt  *
1798c0a839cSHeinrich Schuchardt  * Parse the ASCII string @cmdline for EFI options, denoted by the efi=
1805a17dae4SMatt Fleming  * option, e.g. efi=nochunk.
1815a17dae4SMatt Fleming  *
1825a17dae4SMatt Fleming  * It should be noted that efi= is parsed in two very different
1835a17dae4SMatt Fleming  * environments, first in the early boot environment of the EFI boot
1845a17dae4SMatt Fleming  * stub, and subsequently during the kernel boot.
1858c0a839cSHeinrich Schuchardt  *
1868c0a839cSHeinrich Schuchardt  * Return:	status code
1875a17dae4SMatt Fleming  */
18860f38de7SArd Biesheuvel efi_status_t efi_parse_options(char const *cmdline)
1895a17dae4SMatt Fleming {
19091d150c0SArd Biesheuvel 	size_t len = strlen(cmdline) + 1;
19191d150c0SArd Biesheuvel 	efi_status_t status;
19291d150c0SArd Biesheuvel 	char *str, *buf;
1935a17dae4SMatt Fleming 
19491d150c0SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
19591d150c0SArd Biesheuvel 	if (status != EFI_SUCCESS)
19691d150c0SArd Biesheuvel 		return status;
19791d150c0SArd Biesheuvel 
19891d150c0SArd Biesheuvel 	str = skip_spaces(memcpy(buf, cmdline, len));
19991d150c0SArd Biesheuvel 
20091d150c0SArd Biesheuvel 	while (*str) {
20191d150c0SArd Biesheuvel 		char *param, *val;
20291d150c0SArd Biesheuvel 
20391d150c0SArd Biesheuvel 		str = next_arg(str, &param, &val);
2041fd9717dSArvind Sankar 		if (!val && !strcmp(param, "--"))
2051fd9717dSArvind Sankar 			break;
20691d150c0SArd Biesheuvel 
20791d150c0SArd Biesheuvel 		if (!strcmp(param, "nokaslr")) {
2087d4e323dSArd Biesheuvel 			efi_nokaslr = true;
20991d150c0SArd Biesheuvel 		} else if (!strcmp(param, "quiet")) {
21023d5b73fSArvind Sankar 			efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
21179d3219dSArd Biesheuvel 		} else if (!strcmp(param, "noinitrd")) {
21279d3219dSArd Biesheuvel 			efi_noinitrd = true;
21391d150c0SArd Biesheuvel 		} else if (!strcmp(param, "efi") && val) {
21491d150c0SArd Biesheuvel 			efi_nochunk = parse_option_str(val, "nochunk");
21591d150c0SArd Biesheuvel 			efi_novamap = parse_option_str(val, "novamap");
216eeff7d63SArd Biesheuvel 
21791d150c0SArd Biesheuvel 			efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
21891d150c0SArd Biesheuvel 					    parse_option_str(val, "nosoftreserve");
2195a17dae4SMatt Fleming 
22091d150c0SArd Biesheuvel 			if (parse_option_str(val, "disable_early_pci_dma"))
2214444f854SMatthew Garrett 				efi_disable_pci_dma = true;
22291d150c0SArd Biesheuvel 			if (parse_option_str(val, "no_disable_early_pci_dma"))
2234444f854SMatthew Garrett 				efi_disable_pci_dma = false;
22423d5b73fSArvind Sankar 			if (parse_option_str(val, "debug"))
22523d5b73fSArvind Sankar 				efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
226fffb6804SArvind Sankar 		} else if (!strcmp(param, "video") &&
227fffb6804SArvind Sankar 			   val && strstarts(val, "efifb:")) {
228fffb6804SArvind Sankar 			efi_parse_option_graphics(val + strlen("efifb:"));
2294444f854SMatthew Garrett 		}
2305a17dae4SMatt Fleming 	}
23191d150c0SArd Biesheuvel 	efi_bs_call(free_pool, buf);
2325a17dae4SMatt Fleming 	return EFI_SUCCESS;
2335a17dae4SMatt Fleming }
234f4f75ad5SArd Biesheuvel 
235f4f75ad5SArd Biesheuvel /*
236f4f75ad5SArd Biesheuvel  * Convert the unicode UEFI command line to ASCII to pass to kernel.
237f4f75ad5SArd Biesheuvel  * Size of memory allocated return in *cmd_line_len.
238f4f75ad5SArd Biesheuvel  * Returns NULL on error.
239f4f75ad5SArd Biesheuvel  */
24027cd5511SArd Biesheuvel char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len)
241f4f75ad5SArd Biesheuvel {
242f4f75ad5SArd Biesheuvel 	const u16 *s2;
243f4f75ad5SArd Biesheuvel 	unsigned long cmdline_addr = 0;
24404b24409SArvind Sankar 	int options_chars = efi_table_attr(image, load_options_size) / 2;
245f7b85b33SArd Biesheuvel 	const u16 *options = efi_table_attr(image, load_options);
24680b1bfe1SArvind Sankar 	int options_bytes = 0, safe_options_bytes = 0;  /* UTF-8 bytes */
24780b1bfe1SArvind Sankar 	bool in_quote = false;
248f4f75ad5SArd Biesheuvel 	efi_status_t status;
249f4f75ad5SArd Biesheuvel 
250f4f75ad5SArd Biesheuvel 	if (options) {
251f4f75ad5SArd Biesheuvel 		s2 = options;
25280b1bfe1SArvind Sankar 		while (options_bytes < COMMAND_LINE_SIZE && options_chars--) {
25315c316bcSArvind Sankar 			u16 c = *s2++;
25415c316bcSArvind Sankar 
25580b1bfe1SArvind Sankar 			if (c < 0x80) {
25615c316bcSArvind Sankar 				if (c == L'\0' || c == L'\n')
25715c316bcSArvind Sankar 					break;
25880b1bfe1SArvind Sankar 				if (c == L'"')
25980b1bfe1SArvind Sankar 					in_quote = !in_quote;
26080b1bfe1SArvind Sankar 				else if (!in_quote && isspace((char)c))
26180b1bfe1SArvind Sankar 					safe_options_bytes = options_bytes;
26280b1bfe1SArvind Sankar 
26380b1bfe1SArvind Sankar 				options_bytes++;
26480b1bfe1SArvind Sankar 				continue;
26580b1bfe1SArvind Sankar 			}
26680b1bfe1SArvind Sankar 
26715c316bcSArvind Sankar 			/*
26815c316bcSArvind Sankar 			 * Get the number of UTF-8 bytes corresponding to a
26915c316bcSArvind Sankar 			 * UTF-16 character.
27015c316bcSArvind Sankar 			 * The first part handles everything in the BMP.
27115c316bcSArvind Sankar 			 */
27280b1bfe1SArvind Sankar 			options_bytes += 2 + (c >= 0x800);
27315c316bcSArvind Sankar 			/*
27415c316bcSArvind Sankar 			 * Add one more byte for valid surrogate pairs. Invalid
27515c316bcSArvind Sankar 			 * surrogates will be replaced with 0xfffd and take up
27615c316bcSArvind Sankar 			 * only 3 bytes.
27715c316bcSArvind Sankar 			 */
27815c316bcSArvind Sankar 			if ((c & 0xfc00) == 0xd800) {
27915c316bcSArvind Sankar 				/*
28015c316bcSArvind Sankar 				 * If the very last word is a high surrogate,
28115c316bcSArvind Sankar 				 * we must ignore it since we can't access the
28215c316bcSArvind Sankar 				 * low surrogate.
28315c316bcSArvind Sankar 				 */
28404b24409SArvind Sankar 				if (!options_chars) {
28515c316bcSArvind Sankar 					options_bytes -= 3;
28615c316bcSArvind Sankar 				} else if ((*s2 & 0xfc00) == 0xdc00) {
28715c316bcSArvind Sankar 					options_bytes++;
28804b24409SArvind Sankar 					options_chars--;
28915c316bcSArvind Sankar 					s2++;
29015c316bcSArvind Sankar 				}
29115c316bcSArvind Sankar 			}
292f4f75ad5SArd Biesheuvel 		}
29380b1bfe1SArvind Sankar 		if (options_bytes >= COMMAND_LINE_SIZE) {
29480b1bfe1SArvind Sankar 			options_bytes = safe_options_bytes;
29580b1bfe1SArvind Sankar 			efi_err("Command line is too long: truncated to %d bytes\n",
29680b1bfe1SArvind Sankar 				options_bytes);
29780b1bfe1SArvind Sankar 		}
298f4f75ad5SArd Biesheuvel 	}
299f4f75ad5SArd Biesheuvel 
300f4f75ad5SArd Biesheuvel 	options_bytes++;	/* NUL termination */
301f4f75ad5SArd Biesheuvel 
30227cd5511SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, options_bytes,
30327cd5511SArd Biesheuvel 			     (void **)&cmdline_addr);
304f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
305f4f75ad5SArd Biesheuvel 		return NULL;
306f4f75ad5SArd Biesheuvel 
30704b24409SArvind Sankar 	snprintf((char *)cmdline_addr, options_bytes, "%.*ls",
30804b24409SArvind Sankar 		 options_bytes - 1, options);
309f4f75ad5SArd Biesheuvel 
310f4f75ad5SArd Biesheuvel 	*cmd_line_len = options_bytes;
311f4f75ad5SArd Biesheuvel 	return (char *)cmdline_addr;
312f4f75ad5SArd Biesheuvel }
313fc07716bSJeffrey Hugo 
3148c0a839cSHeinrich Schuchardt /**
3158c0a839cSHeinrich Schuchardt  * efi_exit_boot_services() - Exit boot services
3168c0a839cSHeinrich Schuchardt  * @handle:	handle of the exiting image
3178c0a839cSHeinrich Schuchardt  * @map:	pointer to receive the memory map
3188c0a839cSHeinrich Schuchardt  * @priv:	argument to be passed to @priv_func
3198c0a839cSHeinrich Schuchardt  * @priv_func:	function to process the memory map before exiting boot services
3208c0a839cSHeinrich Schuchardt  *
321fc07716bSJeffrey Hugo  * Handle calling ExitBootServices according to the requirements set out by the
322fc07716bSJeffrey Hugo  * spec.  Obtains the current memory map, and returns that info after calling
323fc07716bSJeffrey Hugo  * ExitBootServices.  The client must specify a function to perform any
324fc07716bSJeffrey Hugo  * processing of the memory map data prior to ExitBootServices.  A client
325fc07716bSJeffrey Hugo  * specific structure may be passed to the function via priv.  The client
326fc07716bSJeffrey Hugo  * function may be called multiple times.
3278c0a839cSHeinrich Schuchardt  *
3288c0a839cSHeinrich Schuchardt  * Return:	status code
329fc07716bSJeffrey Hugo  */
330cd33a5c1SArd Biesheuvel efi_status_t efi_exit_boot_services(void *handle,
331fc07716bSJeffrey Hugo 				    struct efi_boot_memmap *map,
332fc07716bSJeffrey Hugo 				    void *priv,
333fc07716bSJeffrey Hugo 				    efi_exit_boot_map_processing priv_func)
334fc07716bSJeffrey Hugo {
335fc07716bSJeffrey Hugo 	efi_status_t status;
336fc07716bSJeffrey Hugo 
337cd33a5c1SArd Biesheuvel 	status = efi_get_memory_map(map);
338fc07716bSJeffrey Hugo 
339fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
340fc07716bSJeffrey Hugo 		goto fail;
341fc07716bSJeffrey Hugo 
342cd33a5c1SArd Biesheuvel 	status = priv_func(map, priv);
343fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
344fc07716bSJeffrey Hugo 		goto free_map;
345fc07716bSJeffrey Hugo 
3464444f854SMatthew Garrett 	if (efi_disable_pci_dma)
3474444f854SMatthew Garrett 		efi_pci_disable_bridge_busmaster();
3484444f854SMatthew Garrett 
349966291f6SArd Biesheuvel 	status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
350fc07716bSJeffrey Hugo 
351fc07716bSJeffrey Hugo 	if (status == EFI_INVALID_PARAMETER) {
352fc07716bSJeffrey Hugo 		/*
353fc07716bSJeffrey Hugo 		 * The memory map changed between efi_get_memory_map() and
354fc07716bSJeffrey Hugo 		 * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
355fc07716bSJeffrey Hugo 		 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
356fc07716bSJeffrey Hugo 		 * updated map, and try again.  The spec implies one retry
357fc07716bSJeffrey Hugo 		 * should be sufficent, which is confirmed against the EDK2
358fc07716bSJeffrey Hugo 		 * implementation.  Per the spec, we can only invoke
359fc07716bSJeffrey Hugo 		 * get_memory_map() and exit_boot_services() - we cannot alloc
360fc07716bSJeffrey Hugo 		 * so efi_get_memory_map() cannot be used, and we must reuse
361fc07716bSJeffrey Hugo 		 * the buffer.  For all practical purposes, the headroom in the
362fc07716bSJeffrey Hugo 		 * buffer should account for any changes in the map so the call
363fc07716bSJeffrey Hugo 		 * to get_memory_map() is expected to succeed here.
364fc07716bSJeffrey Hugo 		 */
365fc07716bSJeffrey Hugo 		*map->map_size = *map->buff_size;
366966291f6SArd Biesheuvel 		status = efi_bs_call(get_memory_map,
367fc07716bSJeffrey Hugo 				     map->map_size,
368fc07716bSJeffrey Hugo 				     *map->map,
369fc07716bSJeffrey Hugo 				     map->key_ptr,
370fc07716bSJeffrey Hugo 				     map->desc_size,
371fc07716bSJeffrey Hugo 				     map->desc_ver);
372fc07716bSJeffrey Hugo 
373fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
374fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
375fc07716bSJeffrey Hugo 			goto fail;
376fc07716bSJeffrey Hugo 
377cd33a5c1SArd Biesheuvel 		status = priv_func(map, priv);
378fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
379fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
380fc07716bSJeffrey Hugo 			goto fail;
381fc07716bSJeffrey Hugo 
382966291f6SArd Biesheuvel 		status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
383fc07716bSJeffrey Hugo 	}
384fc07716bSJeffrey Hugo 
385fc07716bSJeffrey Hugo 	/* exit_boot_services() was called, thus cannot free */
386fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
387fc07716bSJeffrey Hugo 		goto fail;
388fc07716bSJeffrey Hugo 
389fc07716bSJeffrey Hugo 	return EFI_SUCCESS;
390fc07716bSJeffrey Hugo 
391fc07716bSJeffrey Hugo free_map:
392966291f6SArd Biesheuvel 	efi_bs_call(free_pool, *map->map);
393fc07716bSJeffrey Hugo fail:
394fc07716bSJeffrey Hugo 	return status;
395fc07716bSJeffrey Hugo }
39682d736acSMatthew Garrett 
3978c0a839cSHeinrich Schuchardt /**
3988c0a839cSHeinrich Schuchardt  * get_efi_config_table() - retrieve UEFI configuration table
3998c0a839cSHeinrich Schuchardt  * @guid:	GUID of the configuration table to be retrieved
4008c0a839cSHeinrich Schuchardt  * Return:	pointer to the configuration table or NULL
4018c0a839cSHeinrich Schuchardt  */
402cd33a5c1SArd Biesheuvel void *get_efi_config_table(efi_guid_t guid)
40382d736acSMatthew Garrett {
404ccc27ae7SArd Biesheuvel 	unsigned long tables = efi_table_attr(efi_system_table, tables);
405ccc27ae7SArd Biesheuvel 	int nr_tables = efi_table_attr(efi_system_table, nr_tables);
406f958efe9SArd Biesheuvel 	int i;
407f958efe9SArd Biesheuvel 
408f958efe9SArd Biesheuvel 	for (i = 0; i < nr_tables; i++) {
409f958efe9SArd Biesheuvel 		efi_config_table_t *t = (void *)tables;
410f958efe9SArd Biesheuvel 
411f958efe9SArd Biesheuvel 		if (efi_guidcmp(t->guid, guid) == 0)
41299ea8b1dSArd Biesheuvel 			return efi_table_attr(t, table);
413f958efe9SArd Biesheuvel 
414f958efe9SArd Biesheuvel 		tables += efi_is_native() ? sizeof(efi_config_table_t)
415f958efe9SArd Biesheuvel 					  : sizeof(efi_config_table_32_t);
416f958efe9SArd Biesheuvel 	}
417f958efe9SArd Biesheuvel 	return NULL;
41882d736acSMatthew Garrett }
419dc29da14SArd Biesheuvel 
420ec93fc37SArd Biesheuvel /*
421ec93fc37SArd Biesheuvel  * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
422ec93fc37SArd Biesheuvel  * for the firmware or bootloader to expose the initrd data directly to the stub
423ec93fc37SArd Biesheuvel  * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
424ec93fc37SArd Biesheuvel  * very easy to implement. It is a simple Linux initrd specific conduit between
425ec93fc37SArd Biesheuvel  * kernel and firmware, allowing us to put the EFI stub (being part of the
426ec93fc37SArd Biesheuvel  * kernel) in charge of where and when to load the initrd, while leaving it up
427ec93fc37SArd Biesheuvel  * to the firmware to decide whether it needs to expose its filesystem hierarchy
428ec93fc37SArd Biesheuvel  * via EFI protocols.
429ec93fc37SArd Biesheuvel  */
430ec93fc37SArd Biesheuvel static const struct {
431ec93fc37SArd Biesheuvel 	struct efi_vendor_dev_path	vendor;
432ec93fc37SArd Biesheuvel 	struct efi_generic_dev_path	end;
433ec93fc37SArd Biesheuvel } __packed initrd_dev_path = {
434ec93fc37SArd Biesheuvel 	{
435ec93fc37SArd Biesheuvel 		{
436ec93fc37SArd Biesheuvel 			EFI_DEV_MEDIA,
437ec93fc37SArd Biesheuvel 			EFI_DEV_MEDIA_VENDOR,
438ec93fc37SArd Biesheuvel 			sizeof(struct efi_vendor_dev_path),
439ec93fc37SArd Biesheuvel 		},
440ec93fc37SArd Biesheuvel 		LINUX_EFI_INITRD_MEDIA_GUID
441ec93fc37SArd Biesheuvel 	}, {
442ec93fc37SArd Biesheuvel 		EFI_DEV_END_PATH,
443ec93fc37SArd Biesheuvel 		EFI_DEV_END_ENTIRE,
444ec93fc37SArd Biesheuvel 		sizeof(struct efi_generic_dev_path)
445ec93fc37SArd Biesheuvel 	}
446ec93fc37SArd Biesheuvel };
447ec93fc37SArd Biesheuvel 
448ec93fc37SArd Biesheuvel /**
4498c0a839cSHeinrich Schuchardt  * efi_load_initrd_dev_path() - load the initrd from the Linux initrd device path
450ec93fc37SArd Biesheuvel  * @load_addr:	pointer to store the address where the initrd was loaded
451ec93fc37SArd Biesheuvel  * @load_size:	pointer to store the size of the loaded initrd
452ec93fc37SArd Biesheuvel  * @max:	upper limit for the initrd memory allocation
4538c0a839cSHeinrich Schuchardt  *
4548c0a839cSHeinrich Schuchardt  * Return:
4558c0a839cSHeinrich Schuchardt  * * %EFI_SUCCESS if the initrd was loaded successfully, in which
456ec93fc37SArd Biesheuvel  *   case @load_addr and @load_size are assigned accordingly
4578c0a839cSHeinrich Schuchardt  * * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd device path
4588c0a839cSHeinrich Schuchardt  * * %EFI_INVALID_PARAMETER if load_addr == NULL or load_size == NULL
4598c0a839cSHeinrich Schuchardt  * * %EFI_OUT_OF_RESOURCES if memory allocation failed
4608c0a839cSHeinrich Schuchardt  * * %EFI_LOAD_ERROR in all other cases
461ec93fc37SArd Biesheuvel  */
462f61900fdSArvind Sankar static
463ec93fc37SArd Biesheuvel efi_status_t efi_load_initrd_dev_path(unsigned long *load_addr,
464ec93fc37SArd Biesheuvel 				      unsigned long *load_size,
465ec93fc37SArd Biesheuvel 				      unsigned long max)
466ec93fc37SArd Biesheuvel {
467ec93fc37SArd Biesheuvel 	efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
468ec93fc37SArd Biesheuvel 	efi_device_path_protocol_t *dp;
469ec93fc37SArd Biesheuvel 	efi_load_file2_protocol_t *lf2;
470ec93fc37SArd Biesheuvel 	unsigned long initrd_addr;
471ec93fc37SArd Biesheuvel 	unsigned long initrd_size;
472ec93fc37SArd Biesheuvel 	efi_handle_t handle;
473ec93fc37SArd Biesheuvel 	efi_status_t status;
474ec93fc37SArd Biesheuvel 
475ec93fc37SArd Biesheuvel 	dp = (efi_device_path_protocol_t *)&initrd_dev_path;
476ec93fc37SArd Biesheuvel 	status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
477ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
478ec93fc37SArd Biesheuvel 		return status;
479ec93fc37SArd Biesheuvel 
480ec93fc37SArd Biesheuvel 	status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
481ec93fc37SArd Biesheuvel 			     (void **)&lf2);
482ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
483ec93fc37SArd Biesheuvel 		return status;
484ec93fc37SArd Biesheuvel 
485ec93fc37SArd Biesheuvel 	status = efi_call_proto(lf2, load_file, dp, false, &initrd_size, NULL);
486ec93fc37SArd Biesheuvel 	if (status != EFI_BUFFER_TOO_SMALL)
487ec93fc37SArd Biesheuvel 		return EFI_LOAD_ERROR;
488ec93fc37SArd Biesheuvel 
489ec93fc37SArd Biesheuvel 	status = efi_allocate_pages(initrd_size, &initrd_addr, max);
490ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
491ec93fc37SArd Biesheuvel 		return status;
492ec93fc37SArd Biesheuvel 
493ec93fc37SArd Biesheuvel 	status = efi_call_proto(lf2, load_file, dp, false, &initrd_size,
494ec93fc37SArd Biesheuvel 				(void *)initrd_addr);
495ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS) {
496ec93fc37SArd Biesheuvel 		efi_free(initrd_size, initrd_addr);
497ec93fc37SArd Biesheuvel 		return EFI_LOAD_ERROR;
498ec93fc37SArd Biesheuvel 	}
499ec93fc37SArd Biesheuvel 
500ec93fc37SArd Biesheuvel 	*load_addr = initrd_addr;
501ec93fc37SArd Biesheuvel 	*load_size = initrd_size;
502ec93fc37SArd Biesheuvel 	return EFI_SUCCESS;
503ec93fc37SArd Biesheuvel }
504f61900fdSArvind Sankar 
505f61900fdSArvind Sankar static
506f61900fdSArvind Sankar efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
507f61900fdSArvind Sankar 				     unsigned long *load_addr,
508f61900fdSArvind Sankar 				     unsigned long *load_size,
509f61900fdSArvind Sankar 				     unsigned long soft_limit,
510f61900fdSArvind Sankar 				     unsigned long hard_limit)
511f61900fdSArvind Sankar {
512f61900fdSArvind Sankar 	if (!IS_ENABLED(CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER) ||
513f61900fdSArvind Sankar 	    (IS_ENABLED(CONFIG_X86) && (!efi_is_native() || image == NULL))) {
514f61900fdSArvind Sankar 		*load_addr = *load_size = 0;
515f61900fdSArvind Sankar 		return EFI_SUCCESS;
516f61900fdSArvind Sankar 	}
517f61900fdSArvind Sankar 
518f61900fdSArvind Sankar 	return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
519f61900fdSArvind Sankar 				    soft_limit, hard_limit,
520f61900fdSArvind Sankar 				    load_addr, load_size);
521f61900fdSArvind Sankar }
522f61900fdSArvind Sankar 
5238c0a839cSHeinrich Schuchardt /**
5248c0a839cSHeinrich Schuchardt  * efi_load_initrd() - Load initial RAM disk
5258c0a839cSHeinrich Schuchardt  * @image:	EFI loaded image protocol
5268c0a839cSHeinrich Schuchardt  * @load_addr:	pointer to loaded initrd
5278c0a839cSHeinrich Schuchardt  * @load_size:	size of loaded initrd
5288c0a839cSHeinrich Schuchardt  * @soft_limit:	preferred size of allocated memory for loading the initrd
5298c0a839cSHeinrich Schuchardt  * @hard_limit:	minimum size of allocated memory
5308c0a839cSHeinrich Schuchardt  *
5318c0a839cSHeinrich Schuchardt  * Return:	status code
5328c0a839cSHeinrich Schuchardt  */
533f61900fdSArvind Sankar efi_status_t efi_load_initrd(efi_loaded_image_t *image,
534f61900fdSArvind Sankar 			     unsigned long *load_addr,
535f61900fdSArvind Sankar 			     unsigned long *load_size,
536f61900fdSArvind Sankar 			     unsigned long soft_limit,
537f61900fdSArvind Sankar 			     unsigned long hard_limit)
538f61900fdSArvind Sankar {
539f61900fdSArvind Sankar 	efi_status_t status;
540f61900fdSArvind Sankar 
541f61900fdSArvind Sankar 	if (!load_addr || !load_size)
542f61900fdSArvind Sankar 		return EFI_INVALID_PARAMETER;
543f61900fdSArvind Sankar 
544f61900fdSArvind Sankar 	status = efi_load_initrd_dev_path(load_addr, load_size, hard_limit);
545f61900fdSArvind Sankar 	if (status == EFI_SUCCESS) {
546f61900fdSArvind Sankar 		efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
547f61900fdSArvind Sankar 	} else if (status == EFI_NOT_FOUND) {
548f61900fdSArvind Sankar 		status = efi_load_initrd_cmdline(image, load_addr, load_size,
549f61900fdSArvind Sankar 						 soft_limit, hard_limit);
550f61900fdSArvind Sankar 		if (status == EFI_SUCCESS && *load_size > 0)
551f61900fdSArvind Sankar 			efi_info("Loaded initrd from command line option\n");
552f61900fdSArvind Sankar 	}
553f61900fdSArvind Sankar 
554f61900fdSArvind Sankar 	return status;
555f61900fdSArvind Sankar }
55614c574f3SArvind Sankar 
5578c0a839cSHeinrich Schuchardt /**
5588c0a839cSHeinrich Schuchardt  * efi_wait_for_key() - Wait for key stroke
5598c0a839cSHeinrich Schuchardt  * @usec:	number of microseconds to wait for key stroke
5608c0a839cSHeinrich Schuchardt  * @key:	key entered
5618c0a839cSHeinrich Schuchardt  *
5628c0a839cSHeinrich Schuchardt  * Wait for up to @usec microseconds for a key stroke.
5638c0a839cSHeinrich Schuchardt  *
5648c0a839cSHeinrich Schuchardt  * Return:	status code, EFI_SUCCESS if key received
5658c0a839cSHeinrich Schuchardt  */
56614c574f3SArvind Sankar efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
56714c574f3SArvind Sankar {
56814c574f3SArvind Sankar 	efi_event_t events[2], timer;
56914c574f3SArvind Sankar 	unsigned long index;
57014c574f3SArvind Sankar 	efi_simple_text_input_protocol_t *con_in;
57114c574f3SArvind Sankar 	efi_status_t status;
57214c574f3SArvind Sankar 
57314c574f3SArvind Sankar 	con_in = efi_table_attr(efi_system_table, con_in);
57414c574f3SArvind Sankar 	if (!con_in)
57514c574f3SArvind Sankar 		return EFI_UNSUPPORTED;
57614c574f3SArvind Sankar 	efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
57714c574f3SArvind Sankar 
57814c574f3SArvind Sankar 	status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
57914c574f3SArvind Sankar 	if (status != EFI_SUCCESS)
58014c574f3SArvind Sankar 		return status;
58114c574f3SArvind Sankar 
58214c574f3SArvind Sankar 	status = efi_bs_call(set_timer, timer, EfiTimerRelative,
58314c574f3SArvind Sankar 			     EFI_100NSEC_PER_USEC * usec);
58414c574f3SArvind Sankar 	if (status != EFI_SUCCESS)
58514c574f3SArvind Sankar 		return status;
58614c574f3SArvind Sankar 	efi_set_event_at(events, 1, timer);
58714c574f3SArvind Sankar 
58814c574f3SArvind Sankar 	status = efi_bs_call(wait_for_event, 2, events, &index);
58914c574f3SArvind Sankar 	if (status == EFI_SUCCESS) {
59014c574f3SArvind Sankar 		if (index == 0)
59114c574f3SArvind Sankar 			status = efi_call_proto(con_in, read_keystroke, key);
59214c574f3SArvind Sankar 		else
59314c574f3SArvind Sankar 			status = EFI_TIMEOUT;
59414c574f3SArvind Sankar 	}
59514c574f3SArvind Sankar 
59614c574f3SArvind Sankar 	efi_bs_call(close_event, timer);
59714c574f3SArvind Sankar 
59814c574f3SArvind Sankar 	return status;
59914c574f3SArvind Sankar }
600