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;
22980771f6SArd Biesheuvel bool efi_nokaslr;
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 
35cb8c90a0SArvind Sankar void efi_char16_puts(efi_char16_t *str)
36cb8c90a0SArvind Sankar {
37cb8c90a0SArvind Sankar 	efi_call_proto(efi_table_attr(efi_system_table, con_out),
38cb8c90a0SArvind Sankar 		       output_string, str);
39cb8c90a0SArvind Sankar }
40cb8c90a0SArvind Sankar 
414b75bd36SArvind Sankar static
424b75bd36SArvind Sankar u32 utf8_to_utf32(const u8 **s8)
434b75bd36SArvind Sankar {
444b75bd36SArvind Sankar 	u32 c32;
454b75bd36SArvind Sankar 	u8 c0, cx;
464b75bd36SArvind Sankar 	size_t clen, i;
474b75bd36SArvind Sankar 
484b75bd36SArvind Sankar 	c0 = cx = *(*s8)++;
494b75bd36SArvind Sankar 	/*
504b75bd36SArvind Sankar 	 * The position of the most-significant 0 bit gives us the length of
514b75bd36SArvind Sankar 	 * a multi-octet encoding.
524b75bd36SArvind Sankar 	 */
534b75bd36SArvind Sankar 	for (clen = 0; cx & 0x80; ++clen)
544b75bd36SArvind Sankar 		cx <<= 1;
554b75bd36SArvind Sankar 	/*
564b75bd36SArvind Sankar 	 * If the 0 bit is in position 8, this is a valid single-octet
574b75bd36SArvind Sankar 	 * encoding. If the 0 bit is in position 7 or positions 1-3, the
584b75bd36SArvind Sankar 	 * encoding is invalid.
594b75bd36SArvind Sankar 	 * In either case, we just return the first octet.
604b75bd36SArvind Sankar 	 */
614b75bd36SArvind Sankar 	if (clen < 2 || clen > 4)
624b75bd36SArvind Sankar 		return c0;
634b75bd36SArvind Sankar 	/* Get the bits from the first octet. */
644b75bd36SArvind Sankar 	c32 = cx >> clen--;
654b75bd36SArvind Sankar 	for (i = 0; i < clen; ++i) {
664b75bd36SArvind Sankar 		/* Trailing octets must have 10 in most significant bits. */
674b75bd36SArvind Sankar 		cx = (*s8)[i] ^ 0x80;
684b75bd36SArvind Sankar 		if (cx & 0xc0)
694b75bd36SArvind Sankar 			return c0;
704b75bd36SArvind Sankar 		c32 = (c32 << 6) | cx;
714b75bd36SArvind Sankar 	}
724b75bd36SArvind Sankar 	/*
734b75bd36SArvind Sankar 	 * Check for validity:
744b75bd36SArvind Sankar 	 * - The character must be in the Unicode range.
754b75bd36SArvind Sankar 	 * - It must not be a surrogate.
764b75bd36SArvind Sankar 	 * - It must be encoded using the correct number of octets.
774b75bd36SArvind Sankar 	 */
784b75bd36SArvind Sankar 	if (c32 > 0x10ffff ||
794b75bd36SArvind Sankar 	    (c32 & 0xf800) == 0xd800 ||
804b75bd36SArvind Sankar 	    clen != (c32 >= 0x80) + (c32 >= 0x800) + (c32 >= 0x10000))
814b75bd36SArvind Sankar 		return c0;
824b75bd36SArvind Sankar 	*s8 += clen;
834b75bd36SArvind Sankar 	return c32;
844b75bd36SArvind Sankar }
854b75bd36SArvind Sankar 
86cb8c90a0SArvind Sankar void efi_puts(const char *str)
87f4f75ad5SArd Biesheuvel {
88fd0528a2SArvind Sankar 	efi_char16_t buf[128];
89fd0528a2SArvind Sankar 	size_t pos = 0, lim = ARRAY_SIZE(buf);
904b75bd36SArvind Sankar 	const u8 *s8 = (const u8 *)str;
914b75bd36SArvind Sankar 	u32 c32;
92f4f75ad5SArd Biesheuvel 
934b75bd36SArvind Sankar 	while (*s8) {
944b75bd36SArvind Sankar 		if (*s8 == '\n')
95fd0528a2SArvind Sankar 			buf[pos++] = L'\r';
964b75bd36SArvind Sankar 		c32 = utf8_to_utf32(&s8);
974b75bd36SArvind Sankar 		if (c32 < 0x10000) {
984b75bd36SArvind Sankar 			/* Characters in plane 0 use a single word. */
994b75bd36SArvind Sankar 			buf[pos++] = c32;
1004b75bd36SArvind Sankar 		} else {
1014b75bd36SArvind Sankar 			/*
1024b75bd36SArvind Sankar 			 * Characters in other planes encode into a surrogate
1034b75bd36SArvind Sankar 			 * pair.
1044b75bd36SArvind Sankar 			 */
1054b75bd36SArvind Sankar 			buf[pos++] = (0xd800 - (0x10000 >> 10)) + (c32 >> 10);
1064b75bd36SArvind Sankar 			buf[pos++] = 0xdc00 + (c32 & 0x3ff);
1074b75bd36SArvind Sankar 		}
1084b75bd36SArvind Sankar 		if (*s8 == '\0' || pos >= lim - 2) {
109fd0528a2SArvind Sankar 			buf[pos] = L'\0';
110fd0528a2SArvind Sankar 			efi_char16_puts(buf);
111fd0528a2SArvind Sankar 			pos = 0;
112fd0528a2SArvind Sankar 		}
113f4f75ad5SArd Biesheuvel 	}
114f4f75ad5SArd Biesheuvel }
115f4f75ad5SArd Biesheuvel 
1162c7d1e30SArvind Sankar int efi_printk(const char *fmt, ...)
1172c7d1e30SArvind Sankar {
1182c7d1e30SArvind Sankar 	char printf_buf[256];
1192c7d1e30SArvind Sankar 	va_list args;
1202c7d1e30SArvind Sankar 	int printed;
12123d5b73fSArvind Sankar 	int loglevel = printk_get_level(fmt);
12223d5b73fSArvind Sankar 
12323d5b73fSArvind Sankar 	switch (loglevel) {
12423d5b73fSArvind Sankar 	case '0' ... '9':
12523d5b73fSArvind Sankar 		loglevel -= '0';
12623d5b73fSArvind Sankar 		break;
12723d5b73fSArvind Sankar 	default:
12823d5b73fSArvind Sankar 		/*
12923d5b73fSArvind Sankar 		 * Use loglevel -1 for cases where we just want to print to
13023d5b73fSArvind Sankar 		 * the screen.
13123d5b73fSArvind Sankar 		 */
13223d5b73fSArvind Sankar 		loglevel = -1;
13323d5b73fSArvind Sankar 		break;
13423d5b73fSArvind Sankar 	}
13523d5b73fSArvind Sankar 
13623d5b73fSArvind Sankar 	if (loglevel >= efi_loglevel)
13723d5b73fSArvind Sankar 		return 0;
13823d5b73fSArvind Sankar 
13923d5b73fSArvind Sankar 	if (loglevel >= 0)
14023d5b73fSArvind Sankar 		efi_puts("EFI stub: ");
14123d5b73fSArvind Sankar 
14223d5b73fSArvind Sankar 	fmt = printk_skip_level(fmt);
1432c7d1e30SArvind Sankar 
1442c7d1e30SArvind Sankar 	va_start(args, fmt);
1458fb331e1SArvind Sankar 	printed = vsnprintf(printf_buf, sizeof(printf_buf), fmt, args);
1462c7d1e30SArvind Sankar 	va_end(args);
1472c7d1e30SArvind Sankar 
1482c7d1e30SArvind Sankar 	efi_puts(printf_buf);
1498fb331e1SArvind Sankar 	if (printed >= sizeof(printf_buf)) {
1508fb331e1SArvind Sankar 		efi_puts("[Message truncated]\n");
1518fb331e1SArvind Sankar 		return -1;
1528fb331e1SArvind Sankar 	}
1532c7d1e30SArvind Sankar 
1542c7d1e30SArvind Sankar 	return printed;
1552c7d1e30SArvind Sankar }
1562c7d1e30SArvind Sankar 
1575a17dae4SMatt Fleming /*
1585a17dae4SMatt Fleming  * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
1595a17dae4SMatt Fleming  * option, e.g. efi=nochunk.
1605a17dae4SMatt Fleming  *
1615a17dae4SMatt Fleming  * It should be noted that efi= is parsed in two very different
1625a17dae4SMatt Fleming  * environments, first in the early boot environment of the EFI boot
1635a17dae4SMatt Fleming  * stub, and subsequently during the kernel boot.
1645a17dae4SMatt Fleming  */
16560f38de7SArd Biesheuvel efi_status_t efi_parse_options(char const *cmdline)
1665a17dae4SMatt Fleming {
16791d150c0SArd Biesheuvel 	size_t len = strlen(cmdline) + 1;
16891d150c0SArd Biesheuvel 	efi_status_t status;
16991d150c0SArd Biesheuvel 	char *str, *buf;
1705a17dae4SMatt Fleming 
17191d150c0SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
17291d150c0SArd Biesheuvel 	if (status != EFI_SUCCESS)
17391d150c0SArd Biesheuvel 		return status;
17491d150c0SArd Biesheuvel 
17591d150c0SArd Biesheuvel 	str = skip_spaces(memcpy(buf, cmdline, len));
17691d150c0SArd Biesheuvel 
17791d150c0SArd Biesheuvel 	while (*str) {
17891d150c0SArd Biesheuvel 		char *param, *val;
17991d150c0SArd Biesheuvel 
18091d150c0SArd Biesheuvel 		str = next_arg(str, &param, &val);
18191d150c0SArd Biesheuvel 
18291d150c0SArd Biesheuvel 		if (!strcmp(param, "nokaslr")) {
1837d4e323dSArd Biesheuvel 			efi_nokaslr = true;
18491d150c0SArd Biesheuvel 		} else if (!strcmp(param, "quiet")) {
18523d5b73fSArvind Sankar 			efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
18679d3219dSArd Biesheuvel 		} else if (!strcmp(param, "noinitrd")) {
18779d3219dSArd Biesheuvel 			efi_noinitrd = true;
18891d150c0SArd Biesheuvel 		} else if (!strcmp(param, "efi") && val) {
18991d150c0SArd Biesheuvel 			efi_nochunk = parse_option_str(val, "nochunk");
19091d150c0SArd Biesheuvel 			efi_novamap = parse_option_str(val, "novamap");
191eeff7d63SArd Biesheuvel 
19291d150c0SArd Biesheuvel 			efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
19391d150c0SArd Biesheuvel 					    parse_option_str(val, "nosoftreserve");
1945a17dae4SMatt Fleming 
19591d150c0SArd Biesheuvel 			if (parse_option_str(val, "disable_early_pci_dma"))
1964444f854SMatthew Garrett 				efi_disable_pci_dma = true;
19791d150c0SArd Biesheuvel 			if (parse_option_str(val, "no_disable_early_pci_dma"))
1984444f854SMatthew Garrett 				efi_disable_pci_dma = false;
19923d5b73fSArvind Sankar 			if (parse_option_str(val, "debug"))
20023d5b73fSArvind Sankar 				efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
201fffb6804SArvind Sankar 		} else if (!strcmp(param, "video") &&
202fffb6804SArvind Sankar 			   val && strstarts(val, "efifb:")) {
203fffb6804SArvind Sankar 			efi_parse_option_graphics(val + strlen("efifb:"));
2044444f854SMatthew Garrett 		}
2055a17dae4SMatt Fleming 	}
20691d150c0SArd Biesheuvel 	efi_bs_call(free_pool, buf);
2075a17dae4SMatt Fleming 	return EFI_SUCCESS;
2085a17dae4SMatt Fleming }
209f4f75ad5SArd Biesheuvel 
210f4f75ad5SArd Biesheuvel /*
211f4f75ad5SArd Biesheuvel  * Convert the unicode UEFI command line to ASCII to pass to kernel.
212f4f75ad5SArd Biesheuvel  * Size of memory allocated return in *cmd_line_len.
213f4f75ad5SArd Biesheuvel  * Returns NULL on error.
214f4f75ad5SArd Biesheuvel  */
215cd33a5c1SArd Biesheuvel char *efi_convert_cmdline(efi_loaded_image_t *image,
2161e45bf73SArd Biesheuvel 			  int *cmd_line_len, unsigned long max_addr)
217f4f75ad5SArd Biesheuvel {
218f4f75ad5SArd Biesheuvel 	const u16 *s2;
219f4f75ad5SArd Biesheuvel 	unsigned long cmdline_addr = 0;
22004b24409SArvind Sankar 	int options_chars = efi_table_attr(image, load_options_size) / 2;
221f7b85b33SArd Biesheuvel 	const u16 *options = efi_table_attr(image, load_options);
22280b1bfe1SArvind Sankar 	int options_bytes = 0, safe_options_bytes = 0;  /* UTF-8 bytes */
22380b1bfe1SArvind Sankar 	bool in_quote = false;
224f4f75ad5SArd Biesheuvel 	efi_status_t status;
225f4f75ad5SArd Biesheuvel 
226f4f75ad5SArd Biesheuvel 	if (options) {
227f4f75ad5SArd Biesheuvel 		s2 = options;
22880b1bfe1SArvind Sankar 		while (options_bytes < COMMAND_LINE_SIZE && options_chars--) {
22915c316bcSArvind Sankar 			u16 c = *s2++;
23015c316bcSArvind Sankar 
23180b1bfe1SArvind Sankar 			if (c < 0x80) {
23215c316bcSArvind Sankar 				if (c == L'\0' || c == L'\n')
23315c316bcSArvind Sankar 					break;
23480b1bfe1SArvind Sankar 				if (c == L'"')
23580b1bfe1SArvind Sankar 					in_quote = !in_quote;
23680b1bfe1SArvind Sankar 				else if (!in_quote && isspace((char)c))
23780b1bfe1SArvind Sankar 					safe_options_bytes = options_bytes;
23880b1bfe1SArvind Sankar 
23980b1bfe1SArvind Sankar 				options_bytes++;
24080b1bfe1SArvind Sankar 				continue;
24180b1bfe1SArvind Sankar 			}
24280b1bfe1SArvind Sankar 
24315c316bcSArvind Sankar 			/*
24415c316bcSArvind Sankar 			 * Get the number of UTF-8 bytes corresponding to a
24515c316bcSArvind Sankar 			 * UTF-16 character.
24615c316bcSArvind Sankar 			 * The first part handles everything in the BMP.
24715c316bcSArvind Sankar 			 */
24880b1bfe1SArvind Sankar 			options_bytes += 2 + (c >= 0x800);
24915c316bcSArvind Sankar 			/*
25015c316bcSArvind Sankar 			 * Add one more byte for valid surrogate pairs. Invalid
25115c316bcSArvind Sankar 			 * surrogates will be replaced with 0xfffd and take up
25215c316bcSArvind Sankar 			 * only 3 bytes.
25315c316bcSArvind Sankar 			 */
25415c316bcSArvind Sankar 			if ((c & 0xfc00) == 0xd800) {
25515c316bcSArvind Sankar 				/*
25615c316bcSArvind Sankar 				 * If the very last word is a high surrogate,
25715c316bcSArvind Sankar 				 * we must ignore it since we can't access the
25815c316bcSArvind Sankar 				 * low surrogate.
25915c316bcSArvind Sankar 				 */
26004b24409SArvind Sankar 				if (!options_chars) {
26115c316bcSArvind Sankar 					options_bytes -= 3;
26215c316bcSArvind Sankar 				} else if ((*s2 & 0xfc00) == 0xdc00) {
26315c316bcSArvind Sankar 					options_bytes++;
26404b24409SArvind Sankar 					options_chars--;
26515c316bcSArvind Sankar 					s2++;
26615c316bcSArvind Sankar 				}
26715c316bcSArvind Sankar 			}
268f4f75ad5SArd Biesheuvel 		}
26980b1bfe1SArvind Sankar 		if (options_bytes >= COMMAND_LINE_SIZE) {
27080b1bfe1SArvind Sankar 			options_bytes = safe_options_bytes;
27180b1bfe1SArvind Sankar 			efi_err("Command line is too long: truncated to %d bytes\n",
27280b1bfe1SArvind Sankar 				options_bytes);
27380b1bfe1SArvind Sankar 		}
274f4f75ad5SArd Biesheuvel 	}
275f4f75ad5SArd Biesheuvel 
276f4f75ad5SArd Biesheuvel 	options_bytes++;	/* NUL termination */
277f4f75ad5SArd Biesheuvel 
2781e45bf73SArd Biesheuvel 	status = efi_allocate_pages(options_bytes, &cmdline_addr, max_addr);
279f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
280f4f75ad5SArd Biesheuvel 		return NULL;
281f4f75ad5SArd Biesheuvel 
28204b24409SArvind Sankar 	snprintf((char *)cmdline_addr, options_bytes, "%.*ls",
28304b24409SArvind Sankar 		 options_bytes - 1, options);
284f4f75ad5SArd Biesheuvel 
285f4f75ad5SArd Biesheuvel 	*cmd_line_len = options_bytes;
286f4f75ad5SArd Biesheuvel 	return (char *)cmdline_addr;
287f4f75ad5SArd Biesheuvel }
288fc07716bSJeffrey Hugo 
289fc07716bSJeffrey Hugo /*
290fc07716bSJeffrey Hugo  * Handle calling ExitBootServices according to the requirements set out by the
291fc07716bSJeffrey Hugo  * spec.  Obtains the current memory map, and returns that info after calling
292fc07716bSJeffrey Hugo  * ExitBootServices.  The client must specify a function to perform any
293fc07716bSJeffrey Hugo  * processing of the memory map data prior to ExitBootServices.  A client
294fc07716bSJeffrey Hugo  * specific structure may be passed to the function via priv.  The client
295fc07716bSJeffrey Hugo  * function may be called multiple times.
296fc07716bSJeffrey Hugo  */
297cd33a5c1SArd Biesheuvel efi_status_t efi_exit_boot_services(void *handle,
298fc07716bSJeffrey Hugo 				    struct efi_boot_memmap *map,
299fc07716bSJeffrey Hugo 				    void *priv,
300fc07716bSJeffrey Hugo 				    efi_exit_boot_map_processing priv_func)
301fc07716bSJeffrey Hugo {
302fc07716bSJeffrey Hugo 	efi_status_t status;
303fc07716bSJeffrey Hugo 
304cd33a5c1SArd Biesheuvel 	status = efi_get_memory_map(map);
305fc07716bSJeffrey Hugo 
306fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
307fc07716bSJeffrey Hugo 		goto fail;
308fc07716bSJeffrey Hugo 
309cd33a5c1SArd Biesheuvel 	status = priv_func(map, priv);
310fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
311fc07716bSJeffrey Hugo 		goto free_map;
312fc07716bSJeffrey Hugo 
3134444f854SMatthew Garrett 	if (efi_disable_pci_dma)
3144444f854SMatthew Garrett 		efi_pci_disable_bridge_busmaster();
3154444f854SMatthew Garrett 
316966291f6SArd Biesheuvel 	status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
317fc07716bSJeffrey Hugo 
318fc07716bSJeffrey Hugo 	if (status == EFI_INVALID_PARAMETER) {
319fc07716bSJeffrey Hugo 		/*
320fc07716bSJeffrey Hugo 		 * The memory map changed between efi_get_memory_map() and
321fc07716bSJeffrey Hugo 		 * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
322fc07716bSJeffrey Hugo 		 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
323fc07716bSJeffrey Hugo 		 * updated map, and try again.  The spec implies one retry
324fc07716bSJeffrey Hugo 		 * should be sufficent, which is confirmed against the EDK2
325fc07716bSJeffrey Hugo 		 * implementation.  Per the spec, we can only invoke
326fc07716bSJeffrey Hugo 		 * get_memory_map() and exit_boot_services() - we cannot alloc
327fc07716bSJeffrey Hugo 		 * so efi_get_memory_map() cannot be used, and we must reuse
328fc07716bSJeffrey Hugo 		 * the buffer.  For all practical purposes, the headroom in the
329fc07716bSJeffrey Hugo 		 * buffer should account for any changes in the map so the call
330fc07716bSJeffrey Hugo 		 * to get_memory_map() is expected to succeed here.
331fc07716bSJeffrey Hugo 		 */
332fc07716bSJeffrey Hugo 		*map->map_size = *map->buff_size;
333966291f6SArd Biesheuvel 		status = efi_bs_call(get_memory_map,
334fc07716bSJeffrey Hugo 				     map->map_size,
335fc07716bSJeffrey Hugo 				     *map->map,
336fc07716bSJeffrey Hugo 				     map->key_ptr,
337fc07716bSJeffrey Hugo 				     map->desc_size,
338fc07716bSJeffrey Hugo 				     map->desc_ver);
339fc07716bSJeffrey Hugo 
340fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
341fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
342fc07716bSJeffrey Hugo 			goto fail;
343fc07716bSJeffrey Hugo 
344cd33a5c1SArd Biesheuvel 		status = priv_func(map, priv);
345fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
346fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
347fc07716bSJeffrey Hugo 			goto fail;
348fc07716bSJeffrey Hugo 
349966291f6SArd Biesheuvel 		status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
350fc07716bSJeffrey Hugo 	}
351fc07716bSJeffrey Hugo 
352fc07716bSJeffrey Hugo 	/* exit_boot_services() was called, thus cannot free */
353fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
354fc07716bSJeffrey Hugo 		goto fail;
355fc07716bSJeffrey Hugo 
356fc07716bSJeffrey Hugo 	return EFI_SUCCESS;
357fc07716bSJeffrey Hugo 
358fc07716bSJeffrey Hugo free_map:
359966291f6SArd Biesheuvel 	efi_bs_call(free_pool, *map->map);
360fc07716bSJeffrey Hugo fail:
361fc07716bSJeffrey Hugo 	return status;
362fc07716bSJeffrey Hugo }
36382d736acSMatthew Garrett 
364cd33a5c1SArd Biesheuvel void *get_efi_config_table(efi_guid_t guid)
36582d736acSMatthew Garrett {
366ccc27ae7SArd Biesheuvel 	unsigned long tables = efi_table_attr(efi_system_table, tables);
367ccc27ae7SArd Biesheuvel 	int nr_tables = efi_table_attr(efi_system_table, nr_tables);
368f958efe9SArd Biesheuvel 	int i;
369f958efe9SArd Biesheuvel 
370f958efe9SArd Biesheuvel 	for (i = 0; i < nr_tables; i++) {
371f958efe9SArd Biesheuvel 		efi_config_table_t *t = (void *)tables;
372f958efe9SArd Biesheuvel 
373f958efe9SArd Biesheuvel 		if (efi_guidcmp(t->guid, guid) == 0)
37499ea8b1dSArd Biesheuvel 			return efi_table_attr(t, table);
375f958efe9SArd Biesheuvel 
376f958efe9SArd Biesheuvel 		tables += efi_is_native() ? sizeof(efi_config_table_t)
377f958efe9SArd Biesheuvel 					  : sizeof(efi_config_table_32_t);
378f958efe9SArd Biesheuvel 	}
379f958efe9SArd Biesheuvel 	return NULL;
38082d736acSMatthew Garrett }
381dc29da14SArd Biesheuvel 
382ec93fc37SArd Biesheuvel /*
383ec93fc37SArd Biesheuvel  * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
384ec93fc37SArd Biesheuvel  * for the firmware or bootloader to expose the initrd data directly to the stub
385ec93fc37SArd Biesheuvel  * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
386ec93fc37SArd Biesheuvel  * very easy to implement. It is a simple Linux initrd specific conduit between
387ec93fc37SArd Biesheuvel  * kernel and firmware, allowing us to put the EFI stub (being part of the
388ec93fc37SArd Biesheuvel  * kernel) in charge of where and when to load the initrd, while leaving it up
389ec93fc37SArd Biesheuvel  * to the firmware to decide whether it needs to expose its filesystem hierarchy
390ec93fc37SArd Biesheuvel  * via EFI protocols.
391ec93fc37SArd Biesheuvel  */
392ec93fc37SArd Biesheuvel static const struct {
393ec93fc37SArd Biesheuvel 	struct efi_vendor_dev_path	vendor;
394ec93fc37SArd Biesheuvel 	struct efi_generic_dev_path	end;
395ec93fc37SArd Biesheuvel } __packed initrd_dev_path = {
396ec93fc37SArd Biesheuvel 	{
397ec93fc37SArd Biesheuvel 		{
398ec93fc37SArd Biesheuvel 			EFI_DEV_MEDIA,
399ec93fc37SArd Biesheuvel 			EFI_DEV_MEDIA_VENDOR,
400ec93fc37SArd Biesheuvel 			sizeof(struct efi_vendor_dev_path),
401ec93fc37SArd Biesheuvel 		},
402ec93fc37SArd Biesheuvel 		LINUX_EFI_INITRD_MEDIA_GUID
403ec93fc37SArd Biesheuvel 	}, {
404ec93fc37SArd Biesheuvel 		EFI_DEV_END_PATH,
405ec93fc37SArd Biesheuvel 		EFI_DEV_END_ENTIRE,
406ec93fc37SArd Biesheuvel 		sizeof(struct efi_generic_dev_path)
407ec93fc37SArd Biesheuvel 	}
408ec93fc37SArd Biesheuvel };
409ec93fc37SArd Biesheuvel 
410ec93fc37SArd Biesheuvel /**
411ec93fc37SArd Biesheuvel  * efi_load_initrd_dev_path - load the initrd from the Linux initrd device path
412ec93fc37SArd Biesheuvel  * @load_addr:	pointer to store the address where the initrd was loaded
413ec93fc37SArd Biesheuvel  * @load_size:	pointer to store the size of the loaded initrd
414ec93fc37SArd Biesheuvel  * @max:	upper limit for the initrd memory allocation
415ec93fc37SArd Biesheuvel  * @return:	%EFI_SUCCESS if the initrd was loaded successfully, in which
416ec93fc37SArd Biesheuvel  *		case @load_addr and @load_size are assigned accordingly
417ec93fc37SArd Biesheuvel  *		%EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd
418ec93fc37SArd Biesheuvel  *		device path
419ec93fc37SArd Biesheuvel  *		%EFI_INVALID_PARAMETER if load_addr == NULL or load_size == NULL
420ec93fc37SArd Biesheuvel  *		%EFI_OUT_OF_RESOURCES if memory allocation failed
421ec93fc37SArd Biesheuvel  *		%EFI_LOAD_ERROR in all other cases
422ec93fc37SArd Biesheuvel  */
423f61900fdSArvind Sankar static
424ec93fc37SArd Biesheuvel efi_status_t efi_load_initrd_dev_path(unsigned long *load_addr,
425ec93fc37SArd Biesheuvel 				      unsigned long *load_size,
426ec93fc37SArd Biesheuvel 				      unsigned long max)
427ec93fc37SArd Biesheuvel {
428ec93fc37SArd Biesheuvel 	efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
429ec93fc37SArd Biesheuvel 	efi_device_path_protocol_t *dp;
430ec93fc37SArd Biesheuvel 	efi_load_file2_protocol_t *lf2;
431ec93fc37SArd Biesheuvel 	unsigned long initrd_addr;
432ec93fc37SArd Biesheuvel 	unsigned long initrd_size;
433ec93fc37SArd Biesheuvel 	efi_handle_t handle;
434ec93fc37SArd Biesheuvel 	efi_status_t status;
435ec93fc37SArd Biesheuvel 
436ec93fc37SArd Biesheuvel 	dp = (efi_device_path_protocol_t *)&initrd_dev_path;
437ec93fc37SArd Biesheuvel 	status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
438ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
439ec93fc37SArd Biesheuvel 		return status;
440ec93fc37SArd Biesheuvel 
441ec93fc37SArd Biesheuvel 	status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
442ec93fc37SArd Biesheuvel 			     (void **)&lf2);
443ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
444ec93fc37SArd Biesheuvel 		return status;
445ec93fc37SArd Biesheuvel 
446ec93fc37SArd Biesheuvel 	status = efi_call_proto(lf2, load_file, dp, false, &initrd_size, NULL);
447ec93fc37SArd Biesheuvel 	if (status != EFI_BUFFER_TOO_SMALL)
448ec93fc37SArd Biesheuvel 		return EFI_LOAD_ERROR;
449ec93fc37SArd Biesheuvel 
450ec93fc37SArd Biesheuvel 	status = efi_allocate_pages(initrd_size, &initrd_addr, max);
451ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
452ec93fc37SArd Biesheuvel 		return status;
453ec93fc37SArd Biesheuvel 
454ec93fc37SArd Biesheuvel 	status = efi_call_proto(lf2, load_file, dp, false, &initrd_size,
455ec93fc37SArd Biesheuvel 				(void *)initrd_addr);
456ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS) {
457ec93fc37SArd Biesheuvel 		efi_free(initrd_size, initrd_addr);
458ec93fc37SArd Biesheuvel 		return EFI_LOAD_ERROR;
459ec93fc37SArd Biesheuvel 	}
460ec93fc37SArd Biesheuvel 
461ec93fc37SArd Biesheuvel 	*load_addr = initrd_addr;
462ec93fc37SArd Biesheuvel 	*load_size = initrd_size;
463ec93fc37SArd Biesheuvel 	return EFI_SUCCESS;
464ec93fc37SArd Biesheuvel }
465f61900fdSArvind Sankar 
466f61900fdSArvind Sankar static
467f61900fdSArvind Sankar efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
468f61900fdSArvind Sankar 				     unsigned long *load_addr,
469f61900fdSArvind Sankar 				     unsigned long *load_size,
470f61900fdSArvind Sankar 				     unsigned long soft_limit,
471f61900fdSArvind Sankar 				     unsigned long hard_limit)
472f61900fdSArvind Sankar {
473f61900fdSArvind Sankar 	if (!IS_ENABLED(CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER) ||
474f61900fdSArvind Sankar 	    (IS_ENABLED(CONFIG_X86) && (!efi_is_native() || image == NULL))) {
475f61900fdSArvind Sankar 		*load_addr = *load_size = 0;
476f61900fdSArvind Sankar 		return EFI_SUCCESS;
477f61900fdSArvind Sankar 	}
478f61900fdSArvind Sankar 
479f61900fdSArvind Sankar 	return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
480f61900fdSArvind Sankar 				    soft_limit, hard_limit,
481f61900fdSArvind Sankar 				    load_addr, load_size);
482f61900fdSArvind Sankar }
483f61900fdSArvind Sankar 
484f61900fdSArvind Sankar efi_status_t efi_load_initrd(efi_loaded_image_t *image,
485f61900fdSArvind Sankar 			     unsigned long *load_addr,
486f61900fdSArvind Sankar 			     unsigned long *load_size,
487f61900fdSArvind Sankar 			     unsigned long soft_limit,
488f61900fdSArvind Sankar 			     unsigned long hard_limit)
489f61900fdSArvind Sankar {
490f61900fdSArvind Sankar 	efi_status_t status;
491f61900fdSArvind Sankar 
492f61900fdSArvind Sankar 	if (!load_addr || !load_size)
493f61900fdSArvind Sankar 		return EFI_INVALID_PARAMETER;
494f61900fdSArvind Sankar 
495f61900fdSArvind Sankar 	status = efi_load_initrd_dev_path(load_addr, load_size, hard_limit);
496f61900fdSArvind Sankar 	if (status == EFI_SUCCESS) {
497f61900fdSArvind Sankar 		efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
498f61900fdSArvind Sankar 	} else if (status == EFI_NOT_FOUND) {
499f61900fdSArvind Sankar 		status = efi_load_initrd_cmdline(image, load_addr, load_size,
500f61900fdSArvind Sankar 						 soft_limit, hard_limit);
501f61900fdSArvind Sankar 		if (status == EFI_SUCCESS && *load_size > 0)
502f61900fdSArvind Sankar 			efi_info("Loaded initrd from command line option\n");
503f61900fdSArvind Sankar 	}
504f61900fdSArvind Sankar 
505f61900fdSArvind Sankar 	return status;
506f61900fdSArvind Sankar }
50714c574f3SArvind Sankar 
50814c574f3SArvind Sankar efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
50914c574f3SArvind Sankar {
51014c574f3SArvind Sankar 	efi_event_t events[2], timer;
51114c574f3SArvind Sankar 	unsigned long index;
51214c574f3SArvind Sankar 	efi_simple_text_input_protocol_t *con_in;
51314c574f3SArvind Sankar 	efi_status_t status;
51414c574f3SArvind Sankar 
51514c574f3SArvind Sankar 	con_in = efi_table_attr(efi_system_table, con_in);
51614c574f3SArvind Sankar 	if (!con_in)
51714c574f3SArvind Sankar 		return EFI_UNSUPPORTED;
51814c574f3SArvind Sankar 	efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
51914c574f3SArvind Sankar 
52014c574f3SArvind Sankar 	status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
52114c574f3SArvind Sankar 	if (status != EFI_SUCCESS)
52214c574f3SArvind Sankar 		return status;
52314c574f3SArvind Sankar 
52414c574f3SArvind Sankar 	status = efi_bs_call(set_timer, timer, EfiTimerRelative,
52514c574f3SArvind Sankar 			     EFI_100NSEC_PER_USEC * usec);
52614c574f3SArvind Sankar 	if (status != EFI_SUCCESS)
52714c574f3SArvind Sankar 		return status;
52814c574f3SArvind Sankar 	efi_set_event_at(events, 1, timer);
52914c574f3SArvind Sankar 
53014c574f3SArvind Sankar 	status = efi_bs_call(wait_for_event, 2, events, &index);
53114c574f3SArvind Sankar 	if (status == EFI_SUCCESS) {
53214c574f3SArvind Sankar 		if (index == 0)
53314c574f3SArvind Sankar 			status = efi_call_proto(con_in, read_keystroke, key);
53414c574f3SArvind Sankar 		else
53514c574f3SArvind Sankar 			status = EFI_TIMEOUT;
53614c574f3SArvind Sankar 	}
53714c574f3SArvind Sankar 
53814c574f3SArvind Sankar 	efi_bs_call(close_event, timer);
53914c574f3SArvind Sankar 
54014c574f3SArvind Sankar 	return status;
54114c574f3SArvind Sankar }
542