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 
12f4f75ad5SArd Biesheuvel #include <linux/efi.h>
13fd0528a2SArvind Sankar #include <linux/kernel.h>
1423d5b73fSArvind Sankar #include <linux/printk.h> /* For CONSOLE_LOGLEVEL_* */
15f4f75ad5SArd Biesheuvel #include <asm/efi.h>
16f4f75ad5SArd Biesheuvel 
17f4f75ad5SArd Biesheuvel #include "efistub.h"
18f4f75ad5SArd Biesheuvel 
19980771f6SArd Biesheuvel bool efi_nochunk;
20980771f6SArd Biesheuvel bool efi_nokaslr;
21980771f6SArd Biesheuvel bool efi_noinitrd;
2223d5b73fSArvind Sankar int efi_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
23980771f6SArd Biesheuvel bool efi_novamap;
24980771f6SArd Biesheuvel 
2554439370SArvind Sankar static bool efi_nosoftreserve;
2654439370SArvind Sankar static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
2760f38de7SArd Biesheuvel 
28b617c526SDan Williams bool __pure __efi_soft_reserve_enabled(void)
29b617c526SDan Williams {
30b617c526SDan Williams 	return !efi_nosoftreserve;
31b617c526SDan Williams }
3260f38de7SArd Biesheuvel 
33cb8c90a0SArvind Sankar void efi_char16_puts(efi_char16_t *str)
34cb8c90a0SArvind Sankar {
35cb8c90a0SArvind Sankar 	efi_call_proto(efi_table_attr(efi_system_table, con_out),
36cb8c90a0SArvind Sankar 		       output_string, str);
37cb8c90a0SArvind Sankar }
38cb8c90a0SArvind Sankar 
39cb8c90a0SArvind Sankar void efi_puts(const char *str)
40f4f75ad5SArd Biesheuvel {
41fd0528a2SArvind Sankar 	efi_char16_t buf[128];
42fd0528a2SArvind Sankar 	size_t pos = 0, lim = ARRAY_SIZE(buf);
43f4f75ad5SArd Biesheuvel 
44fd0528a2SArvind Sankar 	while (*str) {
45fd0528a2SArvind Sankar 		if (*str == '\n')
46fd0528a2SArvind Sankar 			buf[pos++] = L'\r';
47fd0528a2SArvind Sankar 		/* Cast to unsigned char to avoid sign-extension */
48fd0528a2SArvind Sankar 		buf[pos++] = (unsigned char)(*str++);
49fd0528a2SArvind Sankar 		if (*str == '\0' || pos >= lim - 2) {
50fd0528a2SArvind Sankar 			buf[pos] = L'\0';
51fd0528a2SArvind Sankar 			efi_char16_puts(buf);
52fd0528a2SArvind Sankar 			pos = 0;
53fd0528a2SArvind Sankar 		}
54f4f75ad5SArd Biesheuvel 	}
55f4f75ad5SArd Biesheuvel }
56f4f75ad5SArd Biesheuvel 
572c7d1e30SArvind Sankar int efi_printk(const char *fmt, ...)
582c7d1e30SArvind Sankar {
592c7d1e30SArvind Sankar 	char printf_buf[256];
602c7d1e30SArvind Sankar 	va_list args;
612c7d1e30SArvind Sankar 	int printed;
6223d5b73fSArvind Sankar 	int loglevel = printk_get_level(fmt);
6323d5b73fSArvind Sankar 
6423d5b73fSArvind Sankar 	switch (loglevel) {
6523d5b73fSArvind Sankar 	case '0' ... '9':
6623d5b73fSArvind Sankar 		loglevel -= '0';
6723d5b73fSArvind Sankar 		break;
6823d5b73fSArvind Sankar 	default:
6923d5b73fSArvind Sankar 		/*
7023d5b73fSArvind Sankar 		 * Use loglevel -1 for cases where we just want to print to
7123d5b73fSArvind Sankar 		 * the screen.
7223d5b73fSArvind Sankar 		 */
7323d5b73fSArvind Sankar 		loglevel = -1;
7423d5b73fSArvind Sankar 		break;
7523d5b73fSArvind Sankar 	}
7623d5b73fSArvind Sankar 
7723d5b73fSArvind Sankar 	if (loglevel >= efi_loglevel)
7823d5b73fSArvind Sankar 		return 0;
7923d5b73fSArvind Sankar 
8023d5b73fSArvind Sankar 	if (loglevel >= 0)
8123d5b73fSArvind Sankar 		efi_puts("EFI stub: ");
8223d5b73fSArvind Sankar 
8323d5b73fSArvind Sankar 	fmt = printk_skip_level(fmt);
842c7d1e30SArvind Sankar 
852c7d1e30SArvind Sankar 	va_start(args, fmt);
868fb331e1SArvind Sankar 	printed = vsnprintf(printf_buf, sizeof(printf_buf), fmt, args);
872c7d1e30SArvind Sankar 	va_end(args);
882c7d1e30SArvind Sankar 
892c7d1e30SArvind Sankar 	efi_puts(printf_buf);
908fb331e1SArvind Sankar 	if (printed >= sizeof(printf_buf)) {
918fb331e1SArvind Sankar 		efi_puts("[Message truncated]\n");
928fb331e1SArvind Sankar 		return -1;
938fb331e1SArvind Sankar 	}
942c7d1e30SArvind Sankar 
952c7d1e30SArvind Sankar 	return printed;
962c7d1e30SArvind Sankar }
972c7d1e30SArvind Sankar 
985a17dae4SMatt Fleming /*
995a17dae4SMatt Fleming  * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
1005a17dae4SMatt Fleming  * option, e.g. efi=nochunk.
1015a17dae4SMatt Fleming  *
1025a17dae4SMatt Fleming  * It should be noted that efi= is parsed in two very different
1035a17dae4SMatt Fleming  * environments, first in the early boot environment of the EFI boot
1045a17dae4SMatt Fleming  * stub, and subsequently during the kernel boot.
1055a17dae4SMatt Fleming  */
10660f38de7SArd Biesheuvel efi_status_t efi_parse_options(char const *cmdline)
1075a17dae4SMatt Fleming {
10891d150c0SArd Biesheuvel 	size_t len = strlen(cmdline) + 1;
10991d150c0SArd Biesheuvel 	efi_status_t status;
11091d150c0SArd Biesheuvel 	char *str, *buf;
1115a17dae4SMatt Fleming 
11291d150c0SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
11391d150c0SArd Biesheuvel 	if (status != EFI_SUCCESS)
11491d150c0SArd Biesheuvel 		return status;
11591d150c0SArd Biesheuvel 
11691d150c0SArd Biesheuvel 	str = skip_spaces(memcpy(buf, cmdline, len));
11791d150c0SArd Biesheuvel 
11891d150c0SArd Biesheuvel 	while (*str) {
11991d150c0SArd Biesheuvel 		char *param, *val;
12091d150c0SArd Biesheuvel 
12191d150c0SArd Biesheuvel 		str = next_arg(str, &param, &val);
12291d150c0SArd Biesheuvel 
12391d150c0SArd Biesheuvel 		if (!strcmp(param, "nokaslr")) {
1247d4e323dSArd Biesheuvel 			efi_nokaslr = true;
12591d150c0SArd Biesheuvel 		} else if (!strcmp(param, "quiet")) {
12623d5b73fSArvind Sankar 			efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
12779d3219dSArd Biesheuvel 		} else if (!strcmp(param, "noinitrd")) {
12879d3219dSArd Biesheuvel 			efi_noinitrd = true;
12991d150c0SArd Biesheuvel 		} else if (!strcmp(param, "efi") && val) {
13091d150c0SArd Biesheuvel 			efi_nochunk = parse_option_str(val, "nochunk");
13191d150c0SArd Biesheuvel 			efi_novamap = parse_option_str(val, "novamap");
132eeff7d63SArd Biesheuvel 
13391d150c0SArd Biesheuvel 			efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
13491d150c0SArd Biesheuvel 					    parse_option_str(val, "nosoftreserve");
1355a17dae4SMatt Fleming 
13691d150c0SArd Biesheuvel 			if (parse_option_str(val, "disable_early_pci_dma"))
1374444f854SMatthew Garrett 				efi_disable_pci_dma = true;
13891d150c0SArd Biesheuvel 			if (parse_option_str(val, "no_disable_early_pci_dma"))
1394444f854SMatthew Garrett 				efi_disable_pci_dma = false;
14023d5b73fSArvind Sankar 			if (parse_option_str(val, "debug"))
14123d5b73fSArvind Sankar 				efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
142fffb6804SArvind Sankar 		} else if (!strcmp(param, "video") &&
143fffb6804SArvind Sankar 			   val && strstarts(val, "efifb:")) {
144fffb6804SArvind Sankar 			efi_parse_option_graphics(val + strlen("efifb:"));
1454444f854SMatthew Garrett 		}
1465a17dae4SMatt Fleming 	}
14791d150c0SArd Biesheuvel 	efi_bs_call(free_pool, buf);
1485a17dae4SMatt Fleming 	return EFI_SUCCESS;
1495a17dae4SMatt Fleming }
150f4f75ad5SArd Biesheuvel 
151f4f75ad5SArd Biesheuvel /*
152f4f75ad5SArd Biesheuvel  * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
153f4f75ad5SArd Biesheuvel  * This overestimates for surrogates, but that is okay.
154f4f75ad5SArd Biesheuvel  */
155f4f75ad5SArd Biesheuvel static int efi_utf8_bytes(u16 c)
156f4f75ad5SArd Biesheuvel {
157f4f75ad5SArd Biesheuvel 	return 1 + (c >= 0x80) + (c >= 0x800);
158f4f75ad5SArd Biesheuvel }
159f4f75ad5SArd Biesheuvel 
160f4f75ad5SArd Biesheuvel /*
161f4f75ad5SArd Biesheuvel  * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
162f4f75ad5SArd Biesheuvel  */
163f4f75ad5SArd Biesheuvel static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
164f4f75ad5SArd Biesheuvel {
165f4f75ad5SArd Biesheuvel 	unsigned int c;
166f4f75ad5SArd Biesheuvel 
167f4f75ad5SArd Biesheuvel 	while (n--) {
168f4f75ad5SArd Biesheuvel 		c = *src++;
169f4f75ad5SArd Biesheuvel 		if (n && c >= 0xd800 && c <= 0xdbff &&
170f4f75ad5SArd Biesheuvel 		    *src >= 0xdc00 && *src <= 0xdfff) {
171f4f75ad5SArd Biesheuvel 			c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
172f4f75ad5SArd Biesheuvel 			src++;
173f4f75ad5SArd Biesheuvel 			n--;
174f4f75ad5SArd Biesheuvel 		}
175f4f75ad5SArd Biesheuvel 		if (c >= 0xd800 && c <= 0xdfff)
176f4f75ad5SArd Biesheuvel 			c = 0xfffd; /* Unmatched surrogate */
177f4f75ad5SArd Biesheuvel 		if (c < 0x80) {
178f4f75ad5SArd Biesheuvel 			*dst++ = c;
179f4f75ad5SArd Biesheuvel 			continue;
180f4f75ad5SArd Biesheuvel 		}
181f4f75ad5SArd Biesheuvel 		if (c < 0x800) {
182f4f75ad5SArd Biesheuvel 			*dst++ = 0xc0 + (c >> 6);
183f4f75ad5SArd Biesheuvel 			goto t1;
184f4f75ad5SArd Biesheuvel 		}
185f4f75ad5SArd Biesheuvel 		if (c < 0x10000) {
186f4f75ad5SArd Biesheuvel 			*dst++ = 0xe0 + (c >> 12);
187f4f75ad5SArd Biesheuvel 			goto t2;
188f4f75ad5SArd Biesheuvel 		}
189f4f75ad5SArd Biesheuvel 		*dst++ = 0xf0 + (c >> 18);
190f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + ((c >> 12) & 0x3f);
191f4f75ad5SArd Biesheuvel 	t2:
192f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + ((c >> 6) & 0x3f);
193f4f75ad5SArd Biesheuvel 	t1:
194f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + (c & 0x3f);
195f4f75ad5SArd Biesheuvel 	}
196f4f75ad5SArd Biesheuvel 
197f4f75ad5SArd Biesheuvel 	return dst;
198f4f75ad5SArd Biesheuvel }
199f4f75ad5SArd Biesheuvel 
200f4f75ad5SArd Biesheuvel /*
201f4f75ad5SArd Biesheuvel  * Convert the unicode UEFI command line to ASCII to pass to kernel.
202f4f75ad5SArd Biesheuvel  * Size of memory allocated return in *cmd_line_len.
203f4f75ad5SArd Biesheuvel  * Returns NULL on error.
204f4f75ad5SArd Biesheuvel  */
205cd33a5c1SArd Biesheuvel char *efi_convert_cmdline(efi_loaded_image_t *image,
2061e45bf73SArd Biesheuvel 			  int *cmd_line_len, unsigned long max_addr)
207f4f75ad5SArd Biesheuvel {
208f4f75ad5SArd Biesheuvel 	const u16 *s2;
209f4f75ad5SArd Biesheuvel 	u8 *s1 = NULL;
210f4f75ad5SArd Biesheuvel 	unsigned long cmdline_addr = 0;
211f7b85b33SArd Biesheuvel 	int load_options_chars = efi_table_attr(image, load_options_size) / 2;
212f7b85b33SArd Biesheuvel 	const u16 *options = efi_table_attr(image, load_options);
213f4f75ad5SArd Biesheuvel 	int options_bytes = 0;  /* UTF-8 bytes */
214f4f75ad5SArd Biesheuvel 	int options_chars = 0;  /* UTF-16 chars */
215f4f75ad5SArd Biesheuvel 	efi_status_t status;
216f4f75ad5SArd Biesheuvel 	u16 zero = 0;
217f4f75ad5SArd Biesheuvel 
218f4f75ad5SArd Biesheuvel 	if (options) {
219f4f75ad5SArd Biesheuvel 		s2 = options;
220f4f75ad5SArd Biesheuvel 		while (*s2 && *s2 != '\n'
221f4f75ad5SArd Biesheuvel 		       && options_chars < load_options_chars) {
222f4f75ad5SArd Biesheuvel 			options_bytes += efi_utf8_bytes(*s2++);
223f4f75ad5SArd Biesheuvel 			options_chars++;
224f4f75ad5SArd Biesheuvel 		}
225f4f75ad5SArd Biesheuvel 	}
226f4f75ad5SArd Biesheuvel 
227f4f75ad5SArd Biesheuvel 	if (!options_chars) {
228f4f75ad5SArd Biesheuvel 		/* No command line options, so return empty string*/
229f4f75ad5SArd Biesheuvel 		options = &zero;
230f4f75ad5SArd Biesheuvel 	}
231f4f75ad5SArd Biesheuvel 
232f4f75ad5SArd Biesheuvel 	options_bytes++;	/* NUL termination */
233f4f75ad5SArd Biesheuvel 
2341e45bf73SArd Biesheuvel 	status = efi_allocate_pages(options_bytes, &cmdline_addr, max_addr);
235f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
236f4f75ad5SArd Biesheuvel 		return NULL;
237f4f75ad5SArd Biesheuvel 
238f4f75ad5SArd Biesheuvel 	s1 = (u8 *)cmdline_addr;
239f4f75ad5SArd Biesheuvel 	s2 = (const u16 *)options;
240f4f75ad5SArd Biesheuvel 
241f4f75ad5SArd Biesheuvel 	s1 = efi_utf16_to_utf8(s1, s2, options_chars);
242f4f75ad5SArd Biesheuvel 	*s1 = '\0';
243f4f75ad5SArd Biesheuvel 
244f4f75ad5SArd Biesheuvel 	*cmd_line_len = options_bytes;
245f4f75ad5SArd Biesheuvel 	return (char *)cmdline_addr;
246f4f75ad5SArd Biesheuvel }
247fc07716bSJeffrey Hugo 
248fc07716bSJeffrey Hugo /*
249fc07716bSJeffrey Hugo  * Handle calling ExitBootServices according to the requirements set out by the
250fc07716bSJeffrey Hugo  * spec.  Obtains the current memory map, and returns that info after calling
251fc07716bSJeffrey Hugo  * ExitBootServices.  The client must specify a function to perform any
252fc07716bSJeffrey Hugo  * processing of the memory map data prior to ExitBootServices.  A client
253fc07716bSJeffrey Hugo  * specific structure may be passed to the function via priv.  The client
254fc07716bSJeffrey Hugo  * function may be called multiple times.
255fc07716bSJeffrey Hugo  */
256cd33a5c1SArd Biesheuvel efi_status_t efi_exit_boot_services(void *handle,
257fc07716bSJeffrey Hugo 				    struct efi_boot_memmap *map,
258fc07716bSJeffrey Hugo 				    void *priv,
259fc07716bSJeffrey Hugo 				    efi_exit_boot_map_processing priv_func)
260fc07716bSJeffrey Hugo {
261fc07716bSJeffrey Hugo 	efi_status_t status;
262fc07716bSJeffrey Hugo 
263cd33a5c1SArd Biesheuvel 	status = efi_get_memory_map(map);
264fc07716bSJeffrey Hugo 
265fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
266fc07716bSJeffrey Hugo 		goto fail;
267fc07716bSJeffrey Hugo 
268cd33a5c1SArd Biesheuvel 	status = priv_func(map, priv);
269fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
270fc07716bSJeffrey Hugo 		goto free_map;
271fc07716bSJeffrey Hugo 
2724444f854SMatthew Garrett 	if (efi_disable_pci_dma)
2734444f854SMatthew Garrett 		efi_pci_disable_bridge_busmaster();
2744444f854SMatthew Garrett 
275966291f6SArd Biesheuvel 	status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
276fc07716bSJeffrey Hugo 
277fc07716bSJeffrey Hugo 	if (status == EFI_INVALID_PARAMETER) {
278fc07716bSJeffrey Hugo 		/*
279fc07716bSJeffrey Hugo 		 * The memory map changed between efi_get_memory_map() and
280fc07716bSJeffrey Hugo 		 * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
281fc07716bSJeffrey Hugo 		 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
282fc07716bSJeffrey Hugo 		 * updated map, and try again.  The spec implies one retry
283fc07716bSJeffrey Hugo 		 * should be sufficent, which is confirmed against the EDK2
284fc07716bSJeffrey Hugo 		 * implementation.  Per the spec, we can only invoke
285fc07716bSJeffrey Hugo 		 * get_memory_map() and exit_boot_services() - we cannot alloc
286fc07716bSJeffrey Hugo 		 * so efi_get_memory_map() cannot be used, and we must reuse
287fc07716bSJeffrey Hugo 		 * the buffer.  For all practical purposes, the headroom in the
288fc07716bSJeffrey Hugo 		 * buffer should account for any changes in the map so the call
289fc07716bSJeffrey Hugo 		 * to get_memory_map() is expected to succeed here.
290fc07716bSJeffrey Hugo 		 */
291fc07716bSJeffrey Hugo 		*map->map_size = *map->buff_size;
292966291f6SArd Biesheuvel 		status = efi_bs_call(get_memory_map,
293fc07716bSJeffrey Hugo 				     map->map_size,
294fc07716bSJeffrey Hugo 				     *map->map,
295fc07716bSJeffrey Hugo 				     map->key_ptr,
296fc07716bSJeffrey Hugo 				     map->desc_size,
297fc07716bSJeffrey Hugo 				     map->desc_ver);
298fc07716bSJeffrey Hugo 
299fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
300fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
301fc07716bSJeffrey Hugo 			goto fail;
302fc07716bSJeffrey Hugo 
303cd33a5c1SArd Biesheuvel 		status = priv_func(map, priv);
304fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
305fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
306fc07716bSJeffrey Hugo 			goto fail;
307fc07716bSJeffrey Hugo 
308966291f6SArd Biesheuvel 		status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
309fc07716bSJeffrey Hugo 	}
310fc07716bSJeffrey Hugo 
311fc07716bSJeffrey Hugo 	/* exit_boot_services() was called, thus cannot free */
312fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
313fc07716bSJeffrey Hugo 		goto fail;
314fc07716bSJeffrey Hugo 
315fc07716bSJeffrey Hugo 	return EFI_SUCCESS;
316fc07716bSJeffrey Hugo 
317fc07716bSJeffrey Hugo free_map:
318966291f6SArd Biesheuvel 	efi_bs_call(free_pool, *map->map);
319fc07716bSJeffrey Hugo fail:
320fc07716bSJeffrey Hugo 	return status;
321fc07716bSJeffrey Hugo }
32282d736acSMatthew Garrett 
323cd33a5c1SArd Biesheuvel void *get_efi_config_table(efi_guid_t guid)
32482d736acSMatthew Garrett {
325ccc27ae7SArd Biesheuvel 	unsigned long tables = efi_table_attr(efi_system_table, tables);
326ccc27ae7SArd Biesheuvel 	int nr_tables = efi_table_attr(efi_system_table, nr_tables);
327f958efe9SArd Biesheuvel 	int i;
328f958efe9SArd Biesheuvel 
329f958efe9SArd Biesheuvel 	for (i = 0; i < nr_tables; i++) {
330f958efe9SArd Biesheuvel 		efi_config_table_t *t = (void *)tables;
331f958efe9SArd Biesheuvel 
332f958efe9SArd Biesheuvel 		if (efi_guidcmp(t->guid, guid) == 0)
33399ea8b1dSArd Biesheuvel 			return efi_table_attr(t, table);
334f958efe9SArd Biesheuvel 
335f958efe9SArd Biesheuvel 		tables += efi_is_native() ? sizeof(efi_config_table_t)
336f958efe9SArd Biesheuvel 					  : sizeof(efi_config_table_32_t);
337f958efe9SArd Biesheuvel 	}
338f958efe9SArd Biesheuvel 	return NULL;
33982d736acSMatthew Garrett }
340dc29da14SArd Biesheuvel 
341ec93fc37SArd Biesheuvel /*
342ec93fc37SArd Biesheuvel  * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
343ec93fc37SArd Biesheuvel  * for the firmware or bootloader to expose the initrd data directly to the stub
344ec93fc37SArd Biesheuvel  * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
345ec93fc37SArd Biesheuvel  * very easy to implement. It is a simple Linux initrd specific conduit between
346ec93fc37SArd Biesheuvel  * kernel and firmware, allowing us to put the EFI stub (being part of the
347ec93fc37SArd Biesheuvel  * kernel) in charge of where and when to load the initrd, while leaving it up
348ec93fc37SArd Biesheuvel  * to the firmware to decide whether it needs to expose its filesystem hierarchy
349ec93fc37SArd Biesheuvel  * via EFI protocols.
350ec93fc37SArd Biesheuvel  */
351ec93fc37SArd Biesheuvel static const struct {
352ec93fc37SArd Biesheuvel 	struct efi_vendor_dev_path	vendor;
353ec93fc37SArd Biesheuvel 	struct efi_generic_dev_path	end;
354ec93fc37SArd Biesheuvel } __packed initrd_dev_path = {
355ec93fc37SArd Biesheuvel 	{
356ec93fc37SArd Biesheuvel 		{
357ec93fc37SArd Biesheuvel 			EFI_DEV_MEDIA,
358ec93fc37SArd Biesheuvel 			EFI_DEV_MEDIA_VENDOR,
359ec93fc37SArd Biesheuvel 			sizeof(struct efi_vendor_dev_path),
360ec93fc37SArd Biesheuvel 		},
361ec93fc37SArd Biesheuvel 		LINUX_EFI_INITRD_MEDIA_GUID
362ec93fc37SArd Biesheuvel 	}, {
363ec93fc37SArd Biesheuvel 		EFI_DEV_END_PATH,
364ec93fc37SArd Biesheuvel 		EFI_DEV_END_ENTIRE,
365ec93fc37SArd Biesheuvel 		sizeof(struct efi_generic_dev_path)
366ec93fc37SArd Biesheuvel 	}
367ec93fc37SArd Biesheuvel };
368ec93fc37SArd Biesheuvel 
369ec93fc37SArd Biesheuvel /**
370ec93fc37SArd Biesheuvel  * efi_load_initrd_dev_path - load the initrd from the Linux initrd device path
371ec93fc37SArd Biesheuvel  * @load_addr:	pointer to store the address where the initrd was loaded
372ec93fc37SArd Biesheuvel  * @load_size:	pointer to store the size of the loaded initrd
373ec93fc37SArd Biesheuvel  * @max:	upper limit for the initrd memory allocation
374ec93fc37SArd Biesheuvel  * @return:	%EFI_SUCCESS if the initrd was loaded successfully, in which
375ec93fc37SArd Biesheuvel  *		case @load_addr and @load_size are assigned accordingly
376ec93fc37SArd Biesheuvel  *		%EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd
377ec93fc37SArd Biesheuvel  *		device path
378ec93fc37SArd Biesheuvel  *		%EFI_INVALID_PARAMETER if load_addr == NULL or load_size == NULL
379ec93fc37SArd Biesheuvel  *		%EFI_OUT_OF_RESOURCES if memory allocation failed
380ec93fc37SArd Biesheuvel  *		%EFI_LOAD_ERROR in all other cases
381ec93fc37SArd Biesheuvel  */
382f61900fdSArvind Sankar static
383ec93fc37SArd Biesheuvel efi_status_t efi_load_initrd_dev_path(unsigned long *load_addr,
384ec93fc37SArd Biesheuvel 				      unsigned long *load_size,
385ec93fc37SArd Biesheuvel 				      unsigned long max)
386ec93fc37SArd Biesheuvel {
387ec93fc37SArd Biesheuvel 	efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
388ec93fc37SArd Biesheuvel 	efi_device_path_protocol_t *dp;
389ec93fc37SArd Biesheuvel 	efi_load_file2_protocol_t *lf2;
390ec93fc37SArd Biesheuvel 	unsigned long initrd_addr;
391ec93fc37SArd Biesheuvel 	unsigned long initrd_size;
392ec93fc37SArd Biesheuvel 	efi_handle_t handle;
393ec93fc37SArd Biesheuvel 	efi_status_t status;
394ec93fc37SArd Biesheuvel 
395ec93fc37SArd Biesheuvel 	dp = (efi_device_path_protocol_t *)&initrd_dev_path;
396ec93fc37SArd Biesheuvel 	status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
397ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
398ec93fc37SArd Biesheuvel 		return status;
399ec93fc37SArd Biesheuvel 
400ec93fc37SArd Biesheuvel 	status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
401ec93fc37SArd Biesheuvel 			     (void **)&lf2);
402ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
403ec93fc37SArd Biesheuvel 		return status;
404ec93fc37SArd Biesheuvel 
405ec93fc37SArd Biesheuvel 	status = efi_call_proto(lf2, load_file, dp, false, &initrd_size, NULL);
406ec93fc37SArd Biesheuvel 	if (status != EFI_BUFFER_TOO_SMALL)
407ec93fc37SArd Biesheuvel 		return EFI_LOAD_ERROR;
408ec93fc37SArd Biesheuvel 
409ec93fc37SArd Biesheuvel 	status = efi_allocate_pages(initrd_size, &initrd_addr, max);
410ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
411ec93fc37SArd Biesheuvel 		return status;
412ec93fc37SArd Biesheuvel 
413ec93fc37SArd Biesheuvel 	status = efi_call_proto(lf2, load_file, dp, false, &initrd_size,
414ec93fc37SArd Biesheuvel 				(void *)initrd_addr);
415ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS) {
416ec93fc37SArd Biesheuvel 		efi_free(initrd_size, initrd_addr);
417ec93fc37SArd Biesheuvel 		return EFI_LOAD_ERROR;
418ec93fc37SArd Biesheuvel 	}
419ec93fc37SArd Biesheuvel 
420ec93fc37SArd Biesheuvel 	*load_addr = initrd_addr;
421ec93fc37SArd Biesheuvel 	*load_size = initrd_size;
422ec93fc37SArd Biesheuvel 	return EFI_SUCCESS;
423ec93fc37SArd Biesheuvel }
424f61900fdSArvind Sankar 
425f61900fdSArvind Sankar static
426f61900fdSArvind Sankar efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
427f61900fdSArvind Sankar 				     unsigned long *load_addr,
428f61900fdSArvind Sankar 				     unsigned long *load_size,
429f61900fdSArvind Sankar 				     unsigned long soft_limit,
430f61900fdSArvind Sankar 				     unsigned long hard_limit)
431f61900fdSArvind Sankar {
432f61900fdSArvind Sankar 	if (!IS_ENABLED(CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER) ||
433f61900fdSArvind Sankar 	    (IS_ENABLED(CONFIG_X86) && (!efi_is_native() || image == NULL))) {
434f61900fdSArvind Sankar 		*load_addr = *load_size = 0;
435f61900fdSArvind Sankar 		return EFI_SUCCESS;
436f61900fdSArvind Sankar 	}
437f61900fdSArvind Sankar 
438f61900fdSArvind Sankar 	return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
439f61900fdSArvind Sankar 				    soft_limit, hard_limit,
440f61900fdSArvind Sankar 				    load_addr, load_size);
441f61900fdSArvind Sankar }
442f61900fdSArvind Sankar 
443f61900fdSArvind Sankar efi_status_t efi_load_initrd(efi_loaded_image_t *image,
444f61900fdSArvind Sankar 			     unsigned long *load_addr,
445f61900fdSArvind Sankar 			     unsigned long *load_size,
446f61900fdSArvind Sankar 			     unsigned long soft_limit,
447f61900fdSArvind Sankar 			     unsigned long hard_limit)
448f61900fdSArvind Sankar {
449f61900fdSArvind Sankar 	efi_status_t status;
450f61900fdSArvind Sankar 
451f61900fdSArvind Sankar 	if (!load_addr || !load_size)
452f61900fdSArvind Sankar 		return EFI_INVALID_PARAMETER;
453f61900fdSArvind Sankar 
454f61900fdSArvind Sankar 	status = efi_load_initrd_dev_path(load_addr, load_size, hard_limit);
455f61900fdSArvind Sankar 	if (status == EFI_SUCCESS) {
456f61900fdSArvind Sankar 		efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
457f61900fdSArvind Sankar 	} else if (status == EFI_NOT_FOUND) {
458f61900fdSArvind Sankar 		status = efi_load_initrd_cmdline(image, load_addr, load_size,
459f61900fdSArvind Sankar 						 soft_limit, hard_limit);
460f61900fdSArvind Sankar 		if (status == EFI_SUCCESS && *load_size > 0)
461f61900fdSArvind Sankar 			efi_info("Loaded initrd from command line option\n");
462f61900fdSArvind Sankar 	}
463f61900fdSArvind Sankar 
464f61900fdSArvind Sankar 	return status;
465f61900fdSArvind Sankar }
46614c574f3SArvind Sankar 
46714c574f3SArvind Sankar efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
46814c574f3SArvind Sankar {
46914c574f3SArvind Sankar 	efi_event_t events[2], timer;
47014c574f3SArvind Sankar 	unsigned long index;
47114c574f3SArvind Sankar 	efi_simple_text_input_protocol_t *con_in;
47214c574f3SArvind Sankar 	efi_status_t status;
47314c574f3SArvind Sankar 
47414c574f3SArvind Sankar 	con_in = efi_table_attr(efi_system_table, con_in);
47514c574f3SArvind Sankar 	if (!con_in)
47614c574f3SArvind Sankar 		return EFI_UNSUPPORTED;
47714c574f3SArvind Sankar 	efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
47814c574f3SArvind Sankar 
47914c574f3SArvind Sankar 	status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
48014c574f3SArvind Sankar 	if (status != EFI_SUCCESS)
48114c574f3SArvind Sankar 		return status;
48214c574f3SArvind Sankar 
48314c574f3SArvind Sankar 	status = efi_bs_call(set_timer, timer, EfiTimerRelative,
48414c574f3SArvind Sankar 			     EFI_100NSEC_PER_USEC * usec);
48514c574f3SArvind Sankar 	if (status != EFI_SUCCESS)
48614c574f3SArvind Sankar 		return status;
48714c574f3SArvind Sankar 	efi_set_event_at(events, 1, timer);
48814c574f3SArvind Sankar 
48914c574f3SArvind Sankar 	status = efi_bs_call(wait_for_event, 2, events, &index);
49014c574f3SArvind Sankar 	if (status == EFI_SUCCESS) {
49114c574f3SArvind Sankar 		if (index == 0)
49214c574f3SArvind Sankar 			status = efi_call_proto(con_in, read_keystroke, key);
49314c574f3SArvind Sankar 		else
49414c574f3SArvind Sankar 			status = EFI_TIMEOUT;
49514c574f3SArvind Sankar 	}
49614c574f3SArvind Sankar 
49714c574f3SArvind Sankar 	efi_bs_call(close_event, timer);
49814c574f3SArvind Sankar 
49914c574f3SArvind Sankar 	return status;
50014c574f3SArvind Sankar }
501