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 
10f4f75ad5SArd Biesheuvel #include <linux/efi.h>
11f4f75ad5SArd Biesheuvel #include <asm/efi.h>
12f4f75ad5SArd Biesheuvel 
13f4f75ad5SArd Biesheuvel #include "efistub.h"
14f4f75ad5SArd Biesheuvel 
155193a33dSArd Biesheuvel static bool __efistub_global efi_nochunk;
167d4e323dSArd Biesheuvel static bool __efistub_global efi_nokaslr;
1779d3219dSArd Biesheuvel static bool __efistub_global efi_noinitrd;
187d4e323dSArd Biesheuvel static bool __efistub_global efi_quiet;
197d4e323dSArd Biesheuvel static bool __efistub_global efi_novamap;
207d4e323dSArd Biesheuvel static bool __efistub_global efi_nosoftreserve;
214444f854SMatthew Garrett static bool __efistub_global efi_disable_pci_dma =
224444f854SMatthew Garrett 					IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
2360f38de7SArd Biesheuvel 
245193a33dSArd Biesheuvel bool __pure nochunk(void)
255193a33dSArd Biesheuvel {
265193a33dSArd Biesheuvel 	return efi_nochunk;
275193a33dSArd Biesheuvel }
287d4e323dSArd Biesheuvel bool __pure nokaslr(void)
2960f38de7SArd Biesheuvel {
307d4e323dSArd Biesheuvel 	return efi_nokaslr;
3160f38de7SArd Biesheuvel }
3279d3219dSArd Biesheuvel bool __pure noinitrd(void)
3379d3219dSArd Biesheuvel {
3479d3219dSArd Biesheuvel 	return efi_noinitrd;
3579d3219dSArd Biesheuvel }
367d4e323dSArd Biesheuvel bool __pure is_quiet(void)
37eeff7d63SArd Biesheuvel {
387d4e323dSArd Biesheuvel 	return efi_quiet;
39eeff7d63SArd Biesheuvel }
407d4e323dSArd Biesheuvel bool __pure novamap(void)
414e46c2a9SArd Biesheuvel {
427d4e323dSArd Biesheuvel 	return efi_novamap;
434e46c2a9SArd Biesheuvel }
44b617c526SDan Williams bool __pure __efi_soft_reserve_enabled(void)
45b617c526SDan Williams {
46b617c526SDan Williams 	return !efi_nosoftreserve;
47b617c526SDan Williams }
4860f38de7SArd Biesheuvel 
498173ec79SArd Biesheuvel void efi_printk(char *str)
50f4f75ad5SArd Biesheuvel {
51f4f75ad5SArd Biesheuvel 	char *s8;
52f4f75ad5SArd Biesheuvel 
53f4f75ad5SArd Biesheuvel 	for (s8 = str; *s8; s8++) {
54f4f75ad5SArd Biesheuvel 		efi_char16_t ch[2] = { 0 };
55f4f75ad5SArd Biesheuvel 
56f4f75ad5SArd Biesheuvel 		ch[0] = *s8;
57f4f75ad5SArd Biesheuvel 		if (*s8 == '\n') {
58f4f75ad5SArd Biesheuvel 			efi_char16_t nl[2] = { '\r', 0 };
598173ec79SArd Biesheuvel 			efi_char16_printk(nl);
60f4f75ad5SArd Biesheuvel 		}
61f4f75ad5SArd Biesheuvel 
628173ec79SArd Biesheuvel 		efi_char16_printk(ch);
63f4f75ad5SArd Biesheuvel 	}
64f4f75ad5SArd Biesheuvel }
65f4f75ad5SArd Biesheuvel 
665a17dae4SMatt Fleming /*
675a17dae4SMatt Fleming  * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
685a17dae4SMatt Fleming  * option, e.g. efi=nochunk.
695a17dae4SMatt Fleming  *
705a17dae4SMatt Fleming  * It should be noted that efi= is parsed in two very different
715a17dae4SMatt Fleming  * environments, first in the early boot environment of the EFI boot
725a17dae4SMatt Fleming  * stub, and subsequently during the kernel boot.
735a17dae4SMatt Fleming  */
7460f38de7SArd Biesheuvel efi_status_t efi_parse_options(char const *cmdline)
755a17dae4SMatt Fleming {
7691d150c0SArd Biesheuvel 	size_t len = strlen(cmdline) + 1;
7791d150c0SArd Biesheuvel 	efi_status_t status;
7891d150c0SArd Biesheuvel 	char *str, *buf;
795a17dae4SMatt Fleming 
8091d150c0SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
8191d150c0SArd Biesheuvel 	if (status != EFI_SUCCESS)
8291d150c0SArd Biesheuvel 		return status;
8391d150c0SArd Biesheuvel 
8491d150c0SArd Biesheuvel 	str = skip_spaces(memcpy(buf, cmdline, len));
8591d150c0SArd Biesheuvel 
8691d150c0SArd Biesheuvel 	while (*str) {
8791d150c0SArd Biesheuvel 		char *param, *val;
8891d150c0SArd Biesheuvel 
8991d150c0SArd Biesheuvel 		str = next_arg(str, &param, &val);
9091d150c0SArd Biesheuvel 
9191d150c0SArd Biesheuvel 		if (!strcmp(param, "nokaslr")) {
927d4e323dSArd Biesheuvel 			efi_nokaslr = true;
9391d150c0SArd Biesheuvel 		} else if (!strcmp(param, "quiet")) {
947d4e323dSArd Biesheuvel 			efi_quiet = true;
9579d3219dSArd Biesheuvel 		} else if (!strcmp(param, "noinitrd")) {
9679d3219dSArd Biesheuvel 			efi_noinitrd = true;
9791d150c0SArd Biesheuvel 		} else if (!strcmp(param, "efi") && val) {
9891d150c0SArd Biesheuvel 			efi_nochunk = parse_option_str(val, "nochunk");
9991d150c0SArd Biesheuvel 			efi_novamap = parse_option_str(val, "novamap");
100eeff7d63SArd Biesheuvel 
10191d150c0SArd Biesheuvel 			efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
10291d150c0SArd Biesheuvel 					    parse_option_str(val, "nosoftreserve");
1035a17dae4SMatt Fleming 
10491d150c0SArd Biesheuvel 			if (parse_option_str(val, "disable_early_pci_dma"))
1054444f854SMatthew Garrett 				efi_disable_pci_dma = true;
10691d150c0SArd Biesheuvel 			if (parse_option_str(val, "no_disable_early_pci_dma"))
1074444f854SMatthew Garrett 				efi_disable_pci_dma = false;
1084444f854SMatthew Garrett 		}
1095a17dae4SMatt Fleming 	}
11091d150c0SArd Biesheuvel 	efi_bs_call(free_pool, buf);
1115a17dae4SMatt Fleming 	return EFI_SUCCESS;
1125a17dae4SMatt Fleming }
113f4f75ad5SArd Biesheuvel 
114f4f75ad5SArd Biesheuvel /*
115f4f75ad5SArd Biesheuvel  * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
116f4f75ad5SArd Biesheuvel  * This overestimates for surrogates, but that is okay.
117f4f75ad5SArd Biesheuvel  */
118f4f75ad5SArd Biesheuvel static int efi_utf8_bytes(u16 c)
119f4f75ad5SArd Biesheuvel {
120f4f75ad5SArd Biesheuvel 	return 1 + (c >= 0x80) + (c >= 0x800);
121f4f75ad5SArd Biesheuvel }
122f4f75ad5SArd Biesheuvel 
123f4f75ad5SArd Biesheuvel /*
124f4f75ad5SArd Biesheuvel  * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
125f4f75ad5SArd Biesheuvel  */
126f4f75ad5SArd Biesheuvel static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
127f4f75ad5SArd Biesheuvel {
128f4f75ad5SArd Biesheuvel 	unsigned int c;
129f4f75ad5SArd Biesheuvel 
130f4f75ad5SArd Biesheuvel 	while (n--) {
131f4f75ad5SArd Biesheuvel 		c = *src++;
132f4f75ad5SArd Biesheuvel 		if (n && c >= 0xd800 && c <= 0xdbff &&
133f4f75ad5SArd Biesheuvel 		    *src >= 0xdc00 && *src <= 0xdfff) {
134f4f75ad5SArd Biesheuvel 			c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
135f4f75ad5SArd Biesheuvel 			src++;
136f4f75ad5SArd Biesheuvel 			n--;
137f4f75ad5SArd Biesheuvel 		}
138f4f75ad5SArd Biesheuvel 		if (c >= 0xd800 && c <= 0xdfff)
139f4f75ad5SArd Biesheuvel 			c = 0xfffd; /* Unmatched surrogate */
140f4f75ad5SArd Biesheuvel 		if (c < 0x80) {
141f4f75ad5SArd Biesheuvel 			*dst++ = c;
142f4f75ad5SArd Biesheuvel 			continue;
143f4f75ad5SArd Biesheuvel 		}
144f4f75ad5SArd Biesheuvel 		if (c < 0x800) {
145f4f75ad5SArd Biesheuvel 			*dst++ = 0xc0 + (c >> 6);
146f4f75ad5SArd Biesheuvel 			goto t1;
147f4f75ad5SArd Biesheuvel 		}
148f4f75ad5SArd Biesheuvel 		if (c < 0x10000) {
149f4f75ad5SArd Biesheuvel 			*dst++ = 0xe0 + (c >> 12);
150f4f75ad5SArd Biesheuvel 			goto t2;
151f4f75ad5SArd Biesheuvel 		}
152f4f75ad5SArd Biesheuvel 		*dst++ = 0xf0 + (c >> 18);
153f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + ((c >> 12) & 0x3f);
154f4f75ad5SArd Biesheuvel 	t2:
155f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + ((c >> 6) & 0x3f);
156f4f75ad5SArd Biesheuvel 	t1:
157f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + (c & 0x3f);
158f4f75ad5SArd Biesheuvel 	}
159f4f75ad5SArd Biesheuvel 
160f4f75ad5SArd Biesheuvel 	return dst;
161f4f75ad5SArd Biesheuvel }
162f4f75ad5SArd Biesheuvel 
163f4f75ad5SArd Biesheuvel /*
164f4f75ad5SArd Biesheuvel  * Convert the unicode UEFI command line to ASCII to pass to kernel.
165f4f75ad5SArd Biesheuvel  * Size of memory allocated return in *cmd_line_len.
166f4f75ad5SArd Biesheuvel  * Returns NULL on error.
167f4f75ad5SArd Biesheuvel  */
168cd33a5c1SArd Biesheuvel char *efi_convert_cmdline(efi_loaded_image_t *image,
1691e45bf73SArd Biesheuvel 			  int *cmd_line_len, unsigned long max_addr)
170f4f75ad5SArd Biesheuvel {
171f4f75ad5SArd Biesheuvel 	const u16 *s2;
172f4f75ad5SArd Biesheuvel 	u8 *s1 = NULL;
173f4f75ad5SArd Biesheuvel 	unsigned long cmdline_addr = 0;
174f4f75ad5SArd Biesheuvel 	int load_options_chars = image->load_options_size / 2; /* UTF-16 */
175f4f75ad5SArd Biesheuvel 	const u16 *options = image->load_options;
176f4f75ad5SArd Biesheuvel 	int options_bytes = 0;  /* UTF-8 bytes */
177f4f75ad5SArd Biesheuvel 	int options_chars = 0;  /* UTF-16 chars */
178f4f75ad5SArd Biesheuvel 	efi_status_t status;
179f4f75ad5SArd Biesheuvel 	u16 zero = 0;
180f4f75ad5SArd Biesheuvel 
181f4f75ad5SArd Biesheuvel 	if (options) {
182f4f75ad5SArd Biesheuvel 		s2 = options;
183f4f75ad5SArd Biesheuvel 		while (*s2 && *s2 != '\n'
184f4f75ad5SArd Biesheuvel 		       && options_chars < load_options_chars) {
185f4f75ad5SArd Biesheuvel 			options_bytes += efi_utf8_bytes(*s2++);
186f4f75ad5SArd Biesheuvel 			options_chars++;
187f4f75ad5SArd Biesheuvel 		}
188f4f75ad5SArd Biesheuvel 	}
189f4f75ad5SArd Biesheuvel 
190f4f75ad5SArd Biesheuvel 	if (!options_chars) {
191f4f75ad5SArd Biesheuvel 		/* No command line options, so return empty string*/
192f4f75ad5SArd Biesheuvel 		options = &zero;
193f4f75ad5SArd Biesheuvel 	}
194f4f75ad5SArd Biesheuvel 
195f4f75ad5SArd Biesheuvel 	options_bytes++;	/* NUL termination */
196f4f75ad5SArd Biesheuvel 
1971e45bf73SArd Biesheuvel 	status = efi_allocate_pages(options_bytes, &cmdline_addr, max_addr);
198f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
199f4f75ad5SArd Biesheuvel 		return NULL;
200f4f75ad5SArd Biesheuvel 
201f4f75ad5SArd Biesheuvel 	s1 = (u8 *)cmdline_addr;
202f4f75ad5SArd Biesheuvel 	s2 = (const u16 *)options;
203f4f75ad5SArd Biesheuvel 
204f4f75ad5SArd Biesheuvel 	s1 = efi_utf16_to_utf8(s1, s2, options_chars);
205f4f75ad5SArd Biesheuvel 	*s1 = '\0';
206f4f75ad5SArd Biesheuvel 
207f4f75ad5SArd Biesheuvel 	*cmd_line_len = options_bytes;
208f4f75ad5SArd Biesheuvel 	return (char *)cmdline_addr;
209f4f75ad5SArd Biesheuvel }
210fc07716bSJeffrey Hugo 
211fc07716bSJeffrey Hugo /*
212fc07716bSJeffrey Hugo  * Handle calling ExitBootServices according to the requirements set out by the
213fc07716bSJeffrey Hugo  * spec.  Obtains the current memory map, and returns that info after calling
214fc07716bSJeffrey Hugo  * ExitBootServices.  The client must specify a function to perform any
215fc07716bSJeffrey Hugo  * processing of the memory map data prior to ExitBootServices.  A client
216fc07716bSJeffrey Hugo  * specific structure may be passed to the function via priv.  The client
217fc07716bSJeffrey Hugo  * function may be called multiple times.
218fc07716bSJeffrey Hugo  */
219cd33a5c1SArd Biesheuvel efi_status_t efi_exit_boot_services(void *handle,
220fc07716bSJeffrey Hugo 				    struct efi_boot_memmap *map,
221fc07716bSJeffrey Hugo 				    void *priv,
222fc07716bSJeffrey Hugo 				    efi_exit_boot_map_processing priv_func)
223fc07716bSJeffrey Hugo {
224fc07716bSJeffrey Hugo 	efi_status_t status;
225fc07716bSJeffrey Hugo 
226cd33a5c1SArd Biesheuvel 	status = efi_get_memory_map(map);
227fc07716bSJeffrey Hugo 
228fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
229fc07716bSJeffrey Hugo 		goto fail;
230fc07716bSJeffrey Hugo 
231cd33a5c1SArd Biesheuvel 	status = priv_func(map, priv);
232fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
233fc07716bSJeffrey Hugo 		goto free_map;
234fc07716bSJeffrey Hugo 
2354444f854SMatthew Garrett 	if (efi_disable_pci_dma)
2364444f854SMatthew Garrett 		efi_pci_disable_bridge_busmaster();
2374444f854SMatthew Garrett 
238966291f6SArd Biesheuvel 	status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
239fc07716bSJeffrey Hugo 
240fc07716bSJeffrey Hugo 	if (status == EFI_INVALID_PARAMETER) {
241fc07716bSJeffrey Hugo 		/*
242fc07716bSJeffrey Hugo 		 * The memory map changed between efi_get_memory_map() and
243fc07716bSJeffrey Hugo 		 * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
244fc07716bSJeffrey Hugo 		 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
245fc07716bSJeffrey Hugo 		 * updated map, and try again.  The spec implies one retry
246fc07716bSJeffrey Hugo 		 * should be sufficent, which is confirmed against the EDK2
247fc07716bSJeffrey Hugo 		 * implementation.  Per the spec, we can only invoke
248fc07716bSJeffrey Hugo 		 * get_memory_map() and exit_boot_services() - we cannot alloc
249fc07716bSJeffrey Hugo 		 * so efi_get_memory_map() cannot be used, and we must reuse
250fc07716bSJeffrey Hugo 		 * the buffer.  For all practical purposes, the headroom in the
251fc07716bSJeffrey Hugo 		 * buffer should account for any changes in the map so the call
252fc07716bSJeffrey Hugo 		 * to get_memory_map() is expected to succeed here.
253fc07716bSJeffrey Hugo 		 */
254fc07716bSJeffrey Hugo 		*map->map_size = *map->buff_size;
255966291f6SArd Biesheuvel 		status = efi_bs_call(get_memory_map,
256fc07716bSJeffrey Hugo 				     map->map_size,
257fc07716bSJeffrey Hugo 				     *map->map,
258fc07716bSJeffrey Hugo 				     map->key_ptr,
259fc07716bSJeffrey Hugo 				     map->desc_size,
260fc07716bSJeffrey Hugo 				     map->desc_ver);
261fc07716bSJeffrey Hugo 
262fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
263fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
264fc07716bSJeffrey Hugo 			goto fail;
265fc07716bSJeffrey Hugo 
266cd33a5c1SArd Biesheuvel 		status = priv_func(map, priv);
267fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
268fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
269fc07716bSJeffrey Hugo 			goto fail;
270fc07716bSJeffrey Hugo 
271966291f6SArd Biesheuvel 		status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
272fc07716bSJeffrey Hugo 	}
273fc07716bSJeffrey Hugo 
274fc07716bSJeffrey Hugo 	/* exit_boot_services() was called, thus cannot free */
275fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
276fc07716bSJeffrey Hugo 		goto fail;
277fc07716bSJeffrey Hugo 
278fc07716bSJeffrey Hugo 	return EFI_SUCCESS;
279fc07716bSJeffrey Hugo 
280fc07716bSJeffrey Hugo free_map:
281966291f6SArd Biesheuvel 	efi_bs_call(free_pool, *map->map);
282fc07716bSJeffrey Hugo fail:
283fc07716bSJeffrey Hugo 	return status;
284fc07716bSJeffrey Hugo }
28582d736acSMatthew Garrett 
286cd33a5c1SArd Biesheuvel void *get_efi_config_table(efi_guid_t guid)
28782d736acSMatthew Garrett {
28899ea8b1dSArd Biesheuvel 	unsigned long tables = efi_table_attr(efi_system_table(), tables);
28999ea8b1dSArd Biesheuvel 	int nr_tables = efi_table_attr(efi_system_table(), nr_tables);
290f958efe9SArd Biesheuvel 	int i;
291f958efe9SArd Biesheuvel 
292f958efe9SArd Biesheuvel 	for (i = 0; i < nr_tables; i++) {
293f958efe9SArd Biesheuvel 		efi_config_table_t *t = (void *)tables;
294f958efe9SArd Biesheuvel 
295f958efe9SArd Biesheuvel 		if (efi_guidcmp(t->guid, guid) == 0)
29699ea8b1dSArd Biesheuvel 			return efi_table_attr(t, table);
297f958efe9SArd Biesheuvel 
298f958efe9SArd Biesheuvel 		tables += efi_is_native() ? sizeof(efi_config_table_t)
299f958efe9SArd Biesheuvel 					  : sizeof(efi_config_table_32_t);
300f958efe9SArd Biesheuvel 	}
301f958efe9SArd Biesheuvel 	return NULL;
30282d736acSMatthew Garrett }
303dc29da14SArd Biesheuvel 
3048173ec79SArd Biesheuvel void efi_char16_printk(efi_char16_t *str)
305dc29da14SArd Biesheuvel {
30699ea8b1dSArd Biesheuvel 	efi_call_proto(efi_table_attr(efi_system_table(), con_out),
30747c0fd39SArd Biesheuvel 		       output_string, str);
308dc29da14SArd Biesheuvel }
309ec93fc37SArd Biesheuvel 
310ec93fc37SArd Biesheuvel /*
311ec93fc37SArd Biesheuvel  * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
312ec93fc37SArd Biesheuvel  * for the firmware or bootloader to expose the initrd data directly to the stub
313ec93fc37SArd Biesheuvel  * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
314ec93fc37SArd Biesheuvel  * very easy to implement. It is a simple Linux initrd specific conduit between
315ec93fc37SArd Biesheuvel  * kernel and firmware, allowing us to put the EFI stub (being part of the
316ec93fc37SArd Biesheuvel  * kernel) in charge of where and when to load the initrd, while leaving it up
317ec93fc37SArd Biesheuvel  * to the firmware to decide whether it needs to expose its filesystem hierarchy
318ec93fc37SArd Biesheuvel  * via EFI protocols.
319ec93fc37SArd Biesheuvel  */
320ec93fc37SArd Biesheuvel static const struct {
321ec93fc37SArd Biesheuvel 	struct efi_vendor_dev_path	vendor;
322ec93fc37SArd Biesheuvel 	struct efi_generic_dev_path	end;
323ec93fc37SArd Biesheuvel } __packed initrd_dev_path = {
324ec93fc37SArd Biesheuvel 	{
325ec93fc37SArd Biesheuvel 		{
326ec93fc37SArd Biesheuvel 			EFI_DEV_MEDIA,
327ec93fc37SArd Biesheuvel 			EFI_DEV_MEDIA_VENDOR,
328ec93fc37SArd Biesheuvel 			sizeof(struct efi_vendor_dev_path),
329ec93fc37SArd Biesheuvel 		},
330ec93fc37SArd Biesheuvel 		LINUX_EFI_INITRD_MEDIA_GUID
331ec93fc37SArd Biesheuvel 	}, {
332ec93fc37SArd Biesheuvel 		EFI_DEV_END_PATH,
333ec93fc37SArd Biesheuvel 		EFI_DEV_END_ENTIRE,
334ec93fc37SArd Biesheuvel 		sizeof(struct efi_generic_dev_path)
335ec93fc37SArd Biesheuvel 	}
336ec93fc37SArd Biesheuvel };
337ec93fc37SArd Biesheuvel 
338ec93fc37SArd Biesheuvel /**
339ec93fc37SArd Biesheuvel  * efi_load_initrd_dev_path - load the initrd from the Linux initrd device path
340ec93fc37SArd Biesheuvel  * @load_addr:	pointer to store the address where the initrd was loaded
341ec93fc37SArd Biesheuvel  * @load_size:	pointer to store the size of the loaded initrd
342ec93fc37SArd Biesheuvel  * @max:	upper limit for the initrd memory allocation
343ec93fc37SArd Biesheuvel  * @return:	%EFI_SUCCESS if the initrd was loaded successfully, in which
344ec93fc37SArd Biesheuvel  *		case @load_addr and @load_size are assigned accordingly
345ec93fc37SArd Biesheuvel  *		%EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd
346ec93fc37SArd Biesheuvel  *		device path
347ec93fc37SArd Biesheuvel  *		%EFI_INVALID_PARAMETER if load_addr == NULL or load_size == NULL
348ec93fc37SArd Biesheuvel  *		%EFI_OUT_OF_RESOURCES if memory allocation failed
349ec93fc37SArd Biesheuvel  *		%EFI_LOAD_ERROR in all other cases
350ec93fc37SArd Biesheuvel  */
351ec93fc37SArd Biesheuvel efi_status_t efi_load_initrd_dev_path(unsigned long *load_addr,
352ec93fc37SArd Biesheuvel 				      unsigned long *load_size,
353ec93fc37SArd Biesheuvel 				      unsigned long max)
354ec93fc37SArd Biesheuvel {
355ec93fc37SArd Biesheuvel 	efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
356ec93fc37SArd Biesheuvel 	efi_device_path_protocol_t *dp;
357ec93fc37SArd Biesheuvel 	efi_load_file2_protocol_t *lf2;
358ec93fc37SArd Biesheuvel 	unsigned long initrd_addr;
359ec93fc37SArd Biesheuvel 	unsigned long initrd_size;
360ec93fc37SArd Biesheuvel 	efi_handle_t handle;
361ec93fc37SArd Biesheuvel 	efi_status_t status;
362ec93fc37SArd Biesheuvel 
363ec93fc37SArd Biesheuvel 	if (!load_addr || !load_size)
364ec93fc37SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
365ec93fc37SArd Biesheuvel 
366ec93fc37SArd Biesheuvel 	dp = (efi_device_path_protocol_t *)&initrd_dev_path;
367ec93fc37SArd Biesheuvel 	status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
368ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
369ec93fc37SArd Biesheuvel 		return status;
370ec93fc37SArd Biesheuvel 
371ec93fc37SArd Biesheuvel 	status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
372ec93fc37SArd Biesheuvel 			     (void **)&lf2);
373ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
374ec93fc37SArd Biesheuvel 		return status;
375ec93fc37SArd Biesheuvel 
376ec93fc37SArd Biesheuvel 	status = efi_call_proto(lf2, load_file, dp, false, &initrd_size, NULL);
377ec93fc37SArd Biesheuvel 	if (status != EFI_BUFFER_TOO_SMALL)
378ec93fc37SArd Biesheuvel 		return EFI_LOAD_ERROR;
379ec93fc37SArd Biesheuvel 
380ec93fc37SArd Biesheuvel 	status = efi_allocate_pages(initrd_size, &initrd_addr, max);
381ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS)
382ec93fc37SArd Biesheuvel 		return status;
383ec93fc37SArd Biesheuvel 
384ec93fc37SArd Biesheuvel 	status = efi_call_proto(lf2, load_file, dp, false, &initrd_size,
385ec93fc37SArd Biesheuvel 				(void *)initrd_addr);
386ec93fc37SArd Biesheuvel 	if (status != EFI_SUCCESS) {
387ec93fc37SArd Biesheuvel 		efi_free(initrd_size, initrd_addr);
388ec93fc37SArd Biesheuvel 		return EFI_LOAD_ERROR;
389ec93fc37SArd Biesheuvel 	}
390ec93fc37SArd Biesheuvel 
391ec93fc37SArd Biesheuvel 	*load_addr = initrd_addr;
392ec93fc37SArd Biesheuvel 	*load_size = initrd_size;
393ec93fc37SArd Biesheuvel 	return EFI_SUCCESS;
394ec93fc37SArd Biesheuvel }
395