Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Helper functions used by the EFI stub on multiple
  4 * architectures. This should be #included by the EFI stub
  5 * implementation files.
  6 *
  7 * Copyright 2011 Intel Corporation; author Matt Fleming
 
 
 
 
  8 */
  9
 10#include <linux/stdarg.h>
 11
 12#include <linux/efi.h>
 13#include <linux/kernel.h>
 14#include <asm/efi.h>
 15#include <asm/setup.h>
 16
 17#include "efistub.h"
 18
 19bool efi_nochunk;
 20bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE);
 21bool efi_novamap;
 
 
 
 
 
 
 
 
 
 
 
 22
 23static bool efi_noinitrd;
 24static bool efi_nosoftreserve;
 25static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
 26
 27bool __pure __efi_soft_reserve_enabled(void)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 28{
 29	return !efi_nosoftreserve;
 
 
 
 
 
 
 
 
 
 
 
 
 30}
 31
 32/**
 33 * efi_parse_options() - Parse EFI command line options
 34 * @cmdline:	kernel command line
 35 *
 36 * Parse the ASCII string @cmdline for EFI options, denoted by the efi=
 37 * option, e.g. efi=nochunk.
 38 *
 39 * It should be noted that efi= is parsed in two very different
 40 * environments, first in the early boot environment of the EFI boot
 41 * stub, and subsequently during the kernel boot.
 42 *
 43 * Return:	status code
 44 */
 45efi_status_t efi_parse_options(char const *cmdline)
 46{
 47	size_t len;
 48	efi_status_t status;
 49	char *str, *buf;
 50
 51	if (!cmdline)
 52		return EFI_SUCCESS;
 53
 54	len = strnlen(cmdline, COMMAND_LINE_SIZE - 1) + 1;
 55	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
 
 
 
 
 
 
 
 56	if (status != EFI_SUCCESS)
 57		return status;
 58
 59	memcpy(buf, cmdline, len - 1);
 60	buf[len - 1] = '\0';
 61	str = skip_spaces(buf);
 
 
 
 
 
 62
 63	while (*str) {
 64		char *param, *val;
 65
 66		str = next_arg(str, &param, &val);
 67		if (!val && !strcmp(param, "--"))
 68			break;
 
 69
 70		if (!strcmp(param, "nokaslr")) {
 71			efi_nokaslr = true;
 72		} else if (!strcmp(param, "quiet")) {
 73			efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
 74		} else if (!strcmp(param, "noinitrd")) {
 75			efi_noinitrd = true;
 76		} else if (IS_ENABLED(CONFIG_X86_64) && !strcmp(param, "no5lvl")) {
 77			efi_no5lvl = true;
 78		} else if (!strcmp(param, "efi") && val) {
 79			efi_nochunk = parse_option_str(val, "nochunk");
 80			efi_novamap |= parse_option_str(val, "novamap");
 81
 82			efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
 83					    parse_option_str(val, "nosoftreserve");
 84
 85			if (parse_option_str(val, "disable_early_pci_dma"))
 86				efi_disable_pci_dma = true;
 87			if (parse_option_str(val, "no_disable_early_pci_dma"))
 88				efi_disable_pci_dma = false;
 89			if (parse_option_str(val, "debug"))
 90				efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
 91		} else if (!strcmp(param, "video") &&
 92			   val && strstarts(val, "efifb:")) {
 93			efi_parse_option_graphics(val + strlen("efifb:"));
 94		}
 95	}
 96	efi_bs_call(free_pool, buf);
 97	return EFI_SUCCESS;
 98}
 99
100/*
101 * The EFI_LOAD_OPTION descriptor has the following layout:
102 *	u32 Attributes;
103 *	u16 FilePathListLength;
104 *	u16 Description[];
105 *	efi_device_path_protocol_t FilePathList[];
106 *	u8 OptionalData[];
107 *
108 * This function validates and unpacks the variable-size data fields.
109 */
110static
111bool efi_load_option_unpack(efi_load_option_unpacked_t *dest,
112			    const efi_load_option_t *src, size_t size)
113{
114	const void *pos;
115	u16 c;
116	efi_device_path_protocol_t header;
117	const efi_char16_t *description;
118	const efi_device_path_protocol_t *file_path_list;
119
120	if (size < offsetof(efi_load_option_t, variable_data))
121		return false;
122	pos = src->variable_data;
123	size -= offsetof(efi_load_option_t, variable_data);
124
125	if ((src->attributes & ~EFI_LOAD_OPTION_MASK) != 0)
126		return false;
127
128	/* Scan description. */
129	description = pos;
130	do {
131		if (size < sizeof(c))
132			return false;
133		c = *(const u16 *)pos;
134		pos += sizeof(c);
135		size -= sizeof(c);
136	} while (c != L'\0');
137
138	/* Scan file_path_list. */
139	file_path_list = pos;
140	do {
141		if (size < sizeof(header))
142			return false;
143		header = *(const efi_device_path_protocol_t *)pos;
144		if (header.length < sizeof(header))
145			return false;
146		if (size < header.length)
147			return false;
148		pos += header.length;
149		size -= header.length;
150	} while ((header.type != EFI_DEV_END_PATH && header.type != EFI_DEV_END_PATH2) ||
151		 (header.sub_type != EFI_DEV_END_ENTIRE));
152	if (pos != (const void *)file_path_list + src->file_path_list_length)
153		return false;
154
155	dest->attributes = src->attributes;
156	dest->file_path_list_length = src->file_path_list_length;
157	dest->description = description;
158	dest->file_path_list = file_path_list;
159	dest->optional_data_size = size;
160	dest->optional_data = size ? pos : NULL;
161
162	return true;
163}
164
165/*
166 * At least some versions of Dell firmware pass the entire contents of the
167 * Boot#### variable, i.e. the EFI_LOAD_OPTION descriptor, rather than just the
168 * OptionalData field.
169 *
170 * Detect this case and extract OptionalData.
171 */
172void efi_apply_loadoptions_quirk(const void **load_options, u32 *load_options_size)
 
 
173{
174	const efi_load_option_t *load_option = *load_options;
175	efi_load_option_unpacked_t load_option_unpacked;
 
 
 
 
176
177	if (!IS_ENABLED(CONFIG_X86))
178		return;
179	if (!load_option)
180		return;
181	if (*load_options_size < sizeof(*load_option))
182		return;
183	if ((load_option->attributes & ~EFI_LOAD_OPTION_BOOT_MASK) != 0)
184		return;
185
186	if (!efi_load_option_unpack(&load_option_unpacked, load_option, *load_options_size))
187		return;
 
 
 
 
 
188
189	efi_warn_once(FW_BUG "LoadOptions is an EFI_LOAD_OPTION descriptor\n");
190	efi_warn_once(FW_BUG "Using OptionalData as a workaround\n");
 
 
 
 
191
192	*load_options = load_option_unpacked.optional_data;
193	*load_options_size = load_option_unpacked.optional_data_size;
194}
195
196enum efistub_event {
197	EFISTUB_EVT_INITRD,
198	EFISTUB_EVT_LOAD_OPTIONS,
199	EFISTUB_EVT_COUNT,
200};
201
202#define STR_WITH_SIZE(s)	sizeof(s), s
 
203
204static const struct {
205	u32		pcr_index;
206	u32		event_id;
207	u32		event_data_len;
208	u8		event_data[52];
209} events[] = {
210	[EFISTUB_EVT_INITRD] = {
211		9,
212		INITRD_EVENT_TAG_ID,
213		STR_WITH_SIZE("Linux initrd")
214	},
215	[EFISTUB_EVT_LOAD_OPTIONS] = {
216		9,
217		LOAD_OPTIONS_EVENT_TAG_ID,
218		STR_WITH_SIZE("LOADED_IMAGE::LoadOptions")
219	},
220};
221
222static efi_status_t efi_measure_tagged_event(unsigned long load_addr,
223					     unsigned long load_size,
224					     enum efistub_event event)
225{
226	efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
227	efi_tcg2_protocol_t *tcg2 = NULL;
228	efi_status_t status;
229
230	efi_bs_call(locate_protocol, &tcg2_guid, NULL, (void **)&tcg2);
231	if (tcg2) {
232		struct efi_measured_event {
233			efi_tcg2_event_t	event_data;
234			efi_tcg2_tagged_event_t tagged_event;
235			u8			tagged_event_data[];
236		} *evt;
237		int size = sizeof(*evt) + events[event].event_data_len;
238
239		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
240				     (void **)&evt);
241		if (status != EFI_SUCCESS)
242			goto fail;
243
244		evt->event_data = (struct efi_tcg2_event){
245			.event_size			= size,
246			.event_header.header_size	= sizeof(evt->event_data.event_header),
247			.event_header.header_version	= EFI_TCG2_EVENT_HEADER_VERSION,
248			.event_header.pcr_index		= events[event].pcr_index,
249			.event_header.event_type	= EV_EVENT_TAG,
250		};
251
252		evt->tagged_event = (struct efi_tcg2_tagged_event){
253			.tagged_event_id		= events[event].event_id,
254			.tagged_event_data_size		= events[event].event_data_len,
255		};
256
257		memcpy(evt->tagged_event_data, events[event].event_data,
258		       events[event].event_data_len);
259
260		status = efi_call_proto(tcg2, hash_log_extend_event, 0,
261					load_addr, load_size, &evt->event_data);
262		efi_bs_call(free_pool, evt);
263
264		if (status != EFI_SUCCESS)
265			goto fail;
266		return EFI_SUCCESS;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267	}
268
269	return EFI_UNSUPPORTED;
270fail:
271	efi_warn("Failed to measure data for event %d: 0x%lx\n", event, status);
272	return status;
273}
274
275/*
276 * Convert the unicode UEFI command line to ASCII to pass to kernel.
277 * Size of memory allocated return in *cmd_line_len.
278 * Returns NULL on error.
279 */
280char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len)
 
 
281{
282	const efi_char16_t *options = efi_table_attr(image, load_options);
283	u32 options_size = efi_table_attr(image, load_options_size);
284	int options_bytes = 0, safe_options_bytes = 0;  /* UTF-8 bytes */
285	unsigned long cmdline_addr = 0;
286	const efi_char16_t *s2;
287	bool in_quote = false;
288	efi_status_t status;
289	u32 options_chars;
 
 
 
 
 
 
290
291	if (options_size > 0)
292		efi_measure_tagged_event((unsigned long)options, options_size,
293					 EFISTUB_EVT_LOAD_OPTIONS);
 
 
 
 
294
295	efi_apply_loadoptions_quirk((const void **)&options, &options_size);
296	options_chars = options_size / sizeof(efi_char16_t);
 
 
 
297
298	if (options) {
299		s2 = options;
300		while (options_bytes < COMMAND_LINE_SIZE && options_chars--) {
301			efi_char16_t c = *s2++;
302
303			if (c < 0x80) {
304				if (c == L'\0' || c == L'\n')
305					break;
306				if (c == L'"')
307					in_quote = !in_quote;
308				else if (!in_quote && isspace((char)c))
309					safe_options_bytes = options_bytes;
310
311				options_bytes++;
312				continue;
313			}
314
315			/*
316			 * Get the number of UTF-8 bytes corresponding to a
317			 * UTF-16 character.
318			 * The first part handles everything in the BMP.
319			 */
320			options_bytes += 2 + (c >= 0x800);
321			/*
322			 * Add one more byte for valid surrogate pairs. Invalid
323			 * surrogates will be replaced with 0xfffd and take up
324			 * only 3 bytes.
325			 */
326			if ((c & 0xfc00) == 0xd800) {
327				/*
328				 * If the very last word is a high surrogate,
329				 * we must ignore it since we can't access the
330				 * low surrogate.
331				 */
332				if (!options_chars) {
333					options_bytes -= 3;
334				} else if ((*s2 & 0xfc00) == 0xdc00) {
335					options_bytes++;
336					options_chars--;
337					s2++;
338				}
339			}
340		}
341		if (options_bytes >= COMMAND_LINE_SIZE) {
342			options_bytes = safe_options_bytes;
343			efi_err("Command line is too long: truncated to %d bytes\n",
344				options_bytes);
345		}
346	}
347
348	options_bytes++;	/* NUL termination */
 
349
350	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, options_bytes,
351			     (void **)&cmdline_addr);
352	if (status != EFI_SUCCESS)
353		return NULL;
 
 
 
 
 
354
355	snprintf((char *)cmdline_addr, options_bytes, "%.*ls",
356		 options_bytes - 1, options);
357
358	*cmd_line_len = options_bytes;
359	return (char *)cmdline_addr;
360}
361
362/**
363 * efi_exit_boot_services() - Exit boot services
364 * @handle:	handle of the exiting image
365 * @priv:	argument to be passed to @priv_func
366 * @priv_func:	function to process the memory map before exiting boot services
367 *
368 * Handle calling ExitBootServices according to the requirements set out by the
369 * spec.  Obtains the current memory map, and returns that info after calling
370 * ExitBootServices.  The client must specify a function to perform any
371 * processing of the memory map data prior to ExitBootServices.  A client
372 * specific structure may be passed to the function via priv.  The client
373 * function may be called multiple times.
374 *
375 * Return:	status code
 
 
376 */
377efi_status_t efi_exit_boot_services(void *handle, void *priv,
378				    efi_exit_boot_map_processing priv_func)
379{
380	struct efi_boot_memmap *map;
381	efi_status_t status;
 
 
 
 
 
 
 
382
383	if (efi_disable_pci_dma)
384		efi_pci_disable_bridge_busmaster();
385
386	status = efi_get_memory_map(&map, true);
387	if (status != EFI_SUCCESS)
388		return status;
 
 
 
 
 
 
389
390	status = priv_func(map, priv);
391	if (status != EFI_SUCCESS) {
392		efi_bs_call(free_pool, map);
393		return status;
 
 
394	}
395
396	status = efi_bs_call(exit_boot_services, handle, map->map_key);
 
397
398	if (status == EFI_INVALID_PARAMETER) {
399		/*
400		 * The memory map changed between efi_get_memory_map() and
401		 * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
402		 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
403		 * updated map, and try again.  The spec implies one retry
404		 * should be sufficent, which is confirmed against the EDK2
405		 * implementation.  Per the spec, we can only invoke
406		 * get_memory_map() and exit_boot_services() - we cannot alloc
407		 * so efi_get_memory_map() cannot be used, and we must reuse
408		 * the buffer.  For all practical purposes, the headroom in the
409		 * buffer should account for any changes in the map so the call
410		 * to get_memory_map() is expected to succeed here.
411		 */
412		map->map_size = map->buff_size;
413		status = efi_bs_call(get_memory_map,
414				     &map->map_size,
415				     &map->map,
416				     &map->map_key,
417				     &map->desc_size,
418				     &map->desc_ver);
419
420		/* exit_boot_services() was called, thus cannot free */
421		if (status != EFI_SUCCESS)
422			return status;
423
424		status = priv_func(map, priv);
425		/* exit_boot_services() was called, thus cannot free */
426		if (status != EFI_SUCCESS)
427			return status;
428
429		status = efi_bs_call(exit_boot_services, handle, map->map_key);
430	}
431
432	return status;
433}
434
435/**
436 * get_efi_config_table() - retrieve UEFI configuration table
437 * @guid:	GUID of the configuration table to be retrieved
438 * Return:	pointer to the configuration table or NULL
439 */
440void *get_efi_config_table(efi_guid_t guid)
441{
442	unsigned long tables = efi_table_attr(efi_system_table, tables);
443	int nr_tables = efi_table_attr(efi_system_table, nr_tables);
444	int i;
445
446	for (i = 0; i < nr_tables; i++) {
447		efi_config_table_t *t = (void *)tables;
448
449		if (efi_guidcmp(t->guid, guid) == 0)
450			return efi_table_attr(t, table);
 
451
452		tables += efi_is_native() ? sizeof(efi_config_table_t)
453					  : sizeof(efi_config_table_32_t);
454	}
455	return NULL;
456}
457
458/*
459 * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
460 * for the firmware or bootloader to expose the initrd data directly to the stub
461 * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
462 * very easy to implement. It is a simple Linux initrd specific conduit between
463 * kernel and firmware, allowing us to put the EFI stub (being part of the
464 * kernel) in charge of where and when to load the initrd, while leaving it up
465 * to the firmware to decide whether it needs to expose its filesystem hierarchy
466 * via EFI protocols.
467 */
468static const struct {
469	struct efi_vendor_dev_path	vendor;
470	struct efi_generic_dev_path	end;
471} __packed initrd_dev_path = {
472	{
473		{
474			EFI_DEV_MEDIA,
475			EFI_DEV_MEDIA_VENDOR,
476			sizeof(struct efi_vendor_dev_path),
477		},
478		LINUX_EFI_INITRD_MEDIA_GUID
479	}, {
480		EFI_DEV_END_PATH,
481		EFI_DEV_END_ENTIRE,
482		sizeof(struct efi_generic_dev_path)
483	}
484};
485
486/**
487 * efi_load_initrd_dev_path() - load the initrd from the Linux initrd device path
488 * @initrd:	pointer of struct to store the address where the initrd was loaded
489 *		and the size of the loaded initrd
490 * @max:	upper limit for the initrd memory allocation
491 *
492 * Return:
493 * * %EFI_SUCCESS if the initrd was loaded successfully, in which
494 *   case @load_addr and @load_size are assigned accordingly
495 * * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd device path
496 * * %EFI_OUT_OF_RESOURCES if memory allocation failed
497 * * %EFI_LOAD_ERROR in all other cases
498 */
499static
500efi_status_t efi_load_initrd_dev_path(struct linux_efi_initrd *initrd,
501				      unsigned long max)
502{
503	efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
504	efi_device_path_protocol_t *dp;
505	efi_load_file2_protocol_t *lf2;
506	efi_handle_t handle;
507	efi_status_t status;
508
509	dp = (efi_device_path_protocol_t *)&initrd_dev_path;
510	status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
511	if (status != EFI_SUCCESS)
512		return status;
513
514	status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
515			     (void **)&lf2);
516	if (status != EFI_SUCCESS)
517		return status;
518
519	initrd->size = 0;
520	status = efi_call_proto(lf2, load_file, dp, false, &initrd->size, NULL);
521	if (status != EFI_BUFFER_TOO_SMALL)
522		return EFI_LOAD_ERROR;
523
524	status = efi_allocate_pages(initrd->size, &initrd->base, max);
525	if (status != EFI_SUCCESS)
526		return status;
 
 
 
 
 
 
 
 
 
 
 
 
527
528	status = efi_call_proto(lf2, load_file, dp, false, &initrd->size,
529				(void *)initrd->base);
530	if (status != EFI_SUCCESS) {
531		efi_free(initrd->size, initrd->base);
532		return EFI_LOAD_ERROR;
 
 
 
 
 
 
 
 
 
 
 
533	}
534	return EFI_SUCCESS;
535}
536
537static
538efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
539				     struct linux_efi_initrd *initrd,
540				     unsigned long soft_limit,
541				     unsigned long hard_limit)
542{
543	if (image == NULL)
544		return EFI_UNSUPPORTED;
545
546	return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
547				    soft_limit, hard_limit,
548				    &initrd->base, &initrd->size);
549}
550
551/**
552 * efi_load_initrd() - Load initial RAM disk
553 * @image:	EFI loaded image protocol
554 * @soft_limit:	preferred address for loading the initrd
555 * @hard_limit:	upper limit address for loading the initrd
556 *
557 * Return:	status code
558 */
559efi_status_t efi_load_initrd(efi_loaded_image_t *image,
560			     unsigned long soft_limit,
561			     unsigned long hard_limit,
562			     const struct linux_efi_initrd **out)
563{
564	efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID;
565	efi_status_t status = EFI_SUCCESS;
566	struct linux_efi_initrd initrd, *tbl;
567
568	if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD) || efi_noinitrd)
569		return EFI_SUCCESS;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570
571	status = efi_load_initrd_dev_path(&initrd, hard_limit);
572	if (status == EFI_SUCCESS) {
573		efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
574		if (initrd.size > 0 &&
575		    efi_measure_tagged_event(initrd.base, initrd.size,
576					     EFISTUB_EVT_INITRD) == EFI_SUCCESS)
577			efi_info("Measured initrd data into PCR 9\n");
578	} else if (status == EFI_NOT_FOUND) {
579		status = efi_load_initrd_cmdline(image, &initrd, soft_limit,
580						 hard_limit);
581		/* command line loader disabled or no initrd= passed? */
582		if (status == EFI_UNSUPPORTED || status == EFI_NOT_READY)
583			return EFI_SUCCESS;
584		if (status == EFI_SUCCESS)
585			efi_info("Loaded initrd from command line option\n");
586	}
587	if (status != EFI_SUCCESS)
588		goto failed;
589
590	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(initrd),
591			     (void **)&tbl);
592	if (status != EFI_SUCCESS)
593		goto free_initrd;
594
595	*tbl = initrd;
596	status = efi_bs_call(install_configuration_table, &tbl_guid, tbl);
597	if (status != EFI_SUCCESS)
598		goto free_tbl;
599
600	if (out)
601		*out = tbl;
602	return EFI_SUCCESS;
 
 
 
 
 
 
 
 
 
 
603
604free_tbl:
605	efi_bs_call(free_pool, tbl);
606free_initrd:
607	efi_free(initrd.size, initrd.base);
608failed:
609	efi_err("Failed to load initrd: 0x%lx\n", status);
610	return status;
611}
612
613/**
614 * efi_wait_for_key() - Wait for key stroke
615 * @usec:	number of microseconds to wait for key stroke
616 * @key:	key entered
617 *
618 * Wait for up to @usec microseconds for a key stroke.
619 *
620 * Return:	status code, EFI_SUCCESS if key received
621 */
622efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
 
 
 
 
 
623{
624	efi_event_t events[2], timer;
625	unsigned long index;
626	efi_simple_text_input_protocol_t *con_in;
627	efi_status_t status;
 
 
628
629	con_in = efi_table_attr(efi_system_table, con_in);
630	if (!con_in)
631		return EFI_UNSUPPORTED;
632	efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
633
634	status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
635	if (status != EFI_SUCCESS)
636		return status;
637
638	status = efi_bs_call(set_timer, timer, EfiTimerRelative,
639			     EFI_100NSEC_PER_USEC * usec);
640	if (status != EFI_SUCCESS)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
641		return status;
642	efi_set_event_at(events, 1, timer);
643
644	status = efi_bs_call(wait_for_event, 2, events, &index);
645	if (status == EFI_SUCCESS) {
646		if (index == 0)
647			status = efi_call_proto(con_in, read_keystroke, key);
648		else
649			status = EFI_TIMEOUT;
650	}
651
652	efi_bs_call(close_event, timer);
 
 
 
 
 
 
 
653
654	return status;
655}
656
657/**
658 * efi_remap_image - Remap a loaded image with the appropriate permissions
659 *                   for code and data
660 *
661 * @image_base:	the base of the image in memory
662 * @alloc_size:	the size of the area in memory occupied by the image
663 * @code_size:	the size of the leading part of the image containing code
664 * 		and read-only data
665 *
666 * efi_remap_image() uses the EFI memory attribute protocol to remap the code
667 * region of the loaded image read-only/executable, and the remainder
668 * read-write/non-executable. The code region is assumed to start at the base
669 * of the image, and will therefore cover the PE/COFF header as well.
670 */
671void efi_remap_image(unsigned long image_base, unsigned alloc_size,
672		     unsigned long code_size)
673{
674	efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
675	efi_memory_attribute_protocol_t *memattr;
676	efi_status_t status;
677	u64 attr;
678
679	/*
680	 * If the firmware implements the EFI_MEMORY_ATTRIBUTE_PROTOCOL, let's
681	 * invoke it to remap the text/rodata region of the decompressed image
682	 * as read-only and the data/bss region as non-executable.
683	 */
684	status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
685	if (status != EFI_SUCCESS)
686		return;
687
688	// Get the current attributes for the entire region
689	status = memattr->get_memory_attributes(memattr, image_base,
690						alloc_size, &attr);
691	if (status != EFI_SUCCESS) {
692		efi_warn("Failed to retrieve memory attributes for image region: 0x%lx\n",
693			 status);
694		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
695	}
696
697	// Mark the code region as read-only
698	status = memattr->set_memory_attributes(memattr, image_base, code_size,
699						EFI_MEMORY_RO);
700	if (status != EFI_SUCCESS) {
701		efi_warn("Failed to remap code region read-only\n");
702		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
703	}
704
705	// If the entire region was already mapped as non-exec, clear the
706	// attribute from the code region. Otherwise, set it on the data
707	// region.
708	if (attr & EFI_MEMORY_XP) {
709		status = memattr->clear_memory_attributes(memattr, image_base,
710							  code_size,
711							  EFI_MEMORY_XP);
712		if (status != EFI_SUCCESS)
713			efi_warn("Failed to remap code region executable\n");
714	} else {
715		status = memattr->set_memory_attributes(memattr,
716							image_base + code_size,
717							alloc_size - code_size,
718							EFI_MEMORY_XP);
719		if (status != EFI_SUCCESS)
720			efi_warn("Failed to remap data region non-executable\n");
721	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
722}
v4.6
 
  1/*
  2 * Helper functions used by the EFI stub on multiple
  3 * architectures. This should be #included by the EFI stub
  4 * implementation files.
  5 *
  6 * Copyright 2011 Intel Corporation; author Matt Fleming
  7 *
  8 * This file is part of the Linux kernel, and is made available
  9 * under the terms of the GNU General Public License version 2.
 10 *
 11 */
 12
 
 
 13#include <linux/efi.h>
 
 14#include <asm/efi.h>
 
 15
 16#include "efistub.h"
 17
 18/*
 19 * Some firmware implementations have problems reading files in one go.
 20 * A read chunk size of 1MB seems to work for most platforms.
 21 *
 22 * Unfortunately, reading files in chunks triggers *other* bugs on some
 23 * platforms, so we provide a way to disable this workaround, which can
 24 * be done by passing "efi=nochunk" on the EFI boot stub command line.
 25 *
 26 * If you experience issues with initrd images being corrupt it's worth
 27 * trying efi=nochunk, but chunking is enabled by default because there
 28 * are far more machines that require the workaround than those that
 29 * break with it enabled.
 30 */
 31#define EFI_READ_CHUNK_SIZE	(1024 * 1024)
 32
 33static unsigned long __chunk_size = EFI_READ_CHUNK_SIZE;
 
 
 34
 35/*
 36 * Allow the platform to override the allocation granularity: this allows
 37 * systems that have the capability to run with a larger page size to deal
 38 * with the allocations for initrd and fdt more efficiently.
 39 */
 40#ifndef EFI_ALLOC_ALIGN
 41#define EFI_ALLOC_ALIGN		EFI_PAGE_SIZE
 42#endif
 43
 44struct file_info {
 45	efi_file_handle_t *handle;
 46	u64 size;
 47};
 48
 49void efi_printk(efi_system_table_t *sys_table_arg, char *str)
 50{
 51	char *s8;
 52
 53	for (s8 = str; *s8; s8++) {
 54		efi_char16_t ch[2] = { 0 };
 55
 56		ch[0] = *s8;
 57		if (*s8 == '\n') {
 58			efi_char16_t nl[2] = { '\r', 0 };
 59			efi_char16_printk(sys_table_arg, nl);
 60		}
 61
 62		efi_char16_printk(sys_table_arg, ch);
 63	}
 64}
 65
 66efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
 67				efi_memory_desc_t **map,
 68				unsigned long *map_size,
 69				unsigned long *desc_size,
 70				u32 *desc_ver,
 71				unsigned long *key_ptr)
 
 
 
 
 
 
 
 
 72{
 73	efi_memory_desc_t *m = NULL;
 74	efi_status_t status;
 75	unsigned long key;
 76	u32 desc_version;
 
 
 77
 78	*map_size = sizeof(*m) * 32;
 79again:
 80	/*
 81	 * Add an additional efi_memory_desc_t because we're doing an
 82	 * allocation which may be in a new descriptor region.
 83	 */
 84	*map_size += sizeof(*m);
 85	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
 86				*map_size, (void **)&m);
 87	if (status != EFI_SUCCESS)
 88		goto fail;
 89
 90	*desc_size = 0;
 91	key = 0;
 92	status = efi_call_early(get_memory_map, map_size, m,
 93				&key, desc_size, &desc_version);
 94	if (status == EFI_BUFFER_TOO_SMALL) {
 95		efi_call_early(free_pool, m);
 96		goto again;
 97	}
 98
 99	if (status != EFI_SUCCESS)
100		efi_call_early(free_pool, m);
101
102	if (key_ptr && status == EFI_SUCCESS)
103		*key_ptr = key;
104	if (desc_ver && status == EFI_SUCCESS)
105		*desc_ver = desc_version;
106
107fail:
108	*map = m;
109	return status;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110}
111
112
113unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
114{
115	efi_status_t status;
116	unsigned long map_size;
117	unsigned long membase  = EFI_ERROR;
118	struct efi_memory_map map;
119	efi_memory_desc_t *md;
120
121	status = efi_get_memory_map(sys_table_arg, (efi_memory_desc_t **)&map.map,
122				    &map_size, &map.desc_size, NULL, NULL);
123	if (status != EFI_SUCCESS)
124		return membase;
125
126	map.map_end = map.map + map_size;
127
128	for_each_efi_memory_desc(&map, md)
129		if (md->attribute & EFI_MEMORY_WB)
130			if (membase > md->phys_addr)
131				membase = md->phys_addr;
132
133	efi_call_early(free_pool, map.map);
134
135	return membase;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136}
137
138/*
139 * Allocate at the highest possible address that is not above 'max'.
 
 
 
 
140 */
141efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
142			    unsigned long size, unsigned long align,
143			    unsigned long *addr, unsigned long max)
144{
145	unsigned long map_size, desc_size;
146	efi_memory_desc_t *map;
147	efi_status_t status;
148	unsigned long nr_pages;
149	u64 max_addr = 0;
150	int i;
151
152	status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
153				    NULL, NULL);
154	if (status != EFI_SUCCESS)
155		goto fail;
 
 
 
 
156
157	/*
158	 * Enforce minimum alignment that EFI requires when requesting
159	 * a specific address.  We are doing page-based allocations,
160	 * so we must be aligned to a page.
161	 */
162	if (align < EFI_ALLOC_ALIGN)
163		align = EFI_ALLOC_ALIGN;
164
165	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
166again:
167	for (i = 0; i < map_size / desc_size; i++) {
168		efi_memory_desc_t *desc;
169		unsigned long m = (unsigned long)map;
170		u64 start, end;
171
172		desc = (efi_memory_desc_t *)(m + (i * desc_size));
173		if (desc->type != EFI_CONVENTIONAL_MEMORY)
174			continue;
175
176		if (desc->num_pages < nr_pages)
177			continue;
 
 
 
178
179		start = desc->phys_addr;
180		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
181
182		if (end > max)
183			end = max;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
185		if ((start + size) > end)
186			continue;
 
 
 
 
 
187
188		if (round_down(end - size, align) < start)
189			continue;
 
 
 
 
 
 
190
191		start = round_down(end - size, align);
 
 
 
192
193		/*
194		 * Don't allocate at 0x0. It will confuse code that
195		 * checks pointers against NULL.
196		 */
197		if (start == 0x0)
198			continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
199
200		if (start > max_addr)
201			max_addr = start;
202	}
203
204	if (!max_addr)
205		status = EFI_NOT_FOUND;
206	else {
207		status = efi_call_early(allocate_pages,
208					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
209					nr_pages, &max_addr);
210		if (status != EFI_SUCCESS) {
211			max = max_addr;
212			max_addr = 0;
213			goto again;
214		}
215
216		*addr = max_addr;
217	}
218
219	efi_call_early(free_pool, map);
220fail:
 
221	return status;
222}
223
224/*
225 * Allocate at the lowest possible address.
 
 
226 */
227efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
228			   unsigned long size, unsigned long align,
229			   unsigned long *addr)
230{
231	unsigned long map_size, desc_size;
232	efi_memory_desc_t *map;
 
 
 
 
233	efi_status_t status;
234	unsigned long nr_pages;
235	int i;
236
237	status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
238				    NULL, NULL);
239	if (status != EFI_SUCCESS)
240		goto fail;
241
242	/*
243	 * Enforce minimum alignment that EFI requires when requesting
244	 * a specific address.  We are doing page-based allocations,
245	 * so we must be aligned to a page.
246	 */
247	if (align < EFI_ALLOC_ALIGN)
248		align = EFI_ALLOC_ALIGN;
249
250	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
251	for (i = 0; i < map_size / desc_size; i++) {
252		efi_memory_desc_t *desc;
253		unsigned long m = (unsigned long)map;
254		u64 start, end;
255
256		desc = (efi_memory_desc_t *)(m + (i * desc_size));
 
 
 
257
258		if (desc->type != EFI_CONVENTIONAL_MEMORY)
259			continue;
 
 
 
 
 
260
261		if (desc->num_pages < nr_pages)
262			continue;
 
263
264		start = desc->phys_addr;
265		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
266
267		/*
268		 * Don't allocate at 0x0. It will confuse code that
269		 * checks pointers against NULL. Skip the first 8
270		 * bytes so we start at a nice even number.
271		 */
272		if (start == 0x0)
273			start += 8;
274
275		start = round_up(start, align);
276		if ((start + size) > end)
277			continue;
278
279		status = efi_call_early(allocate_pages,
280					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
281					nr_pages, &start);
282		if (status == EFI_SUCCESS) {
283			*addr = start;
284			break;
 
 
 
 
 
 
 
 
 
285		}
286	}
287
288	if (i == map_size / desc_size)
289		status = EFI_NOT_FOUND;
290
291	efi_call_early(free_pool, map);
292fail:
293	return status;
294}
295
296void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
297	      unsigned long addr)
298{
299	unsigned long nr_pages;
300
301	if (!size)
302		return;
303
304	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
305	efi_call_early(free_pages, addr, nr_pages);
306}
307
308/*
309 * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
310 * option, e.g. efi=nochunk.
 
 
 
 
 
 
 
 
 
311 *
312 * It should be noted that efi= is parsed in two very different
313 * environments, first in the early boot environment of the EFI boot
314 * stub, and subsequently during the kernel boot.
315 */
316efi_status_t efi_parse_options(char *cmdline)
 
317{
318	char *str;
319
320	/*
321	 * If no EFI parameters were specified on the cmdline we've got
322	 * nothing to do.
323	 */
324	str = strstr(cmdline, "efi=");
325	if (!str)
326		return EFI_SUCCESS;
327
328	/* Skip ahead to first argument */
329	str += strlen("efi=");
330
331	/*
332	 * Remember, because efi= is also used by the kernel we need to
333	 * skip over arguments we don't understand.
334	 */
335	while (*str) {
336		if (!strncmp(str, "nochunk", 7)) {
337			str += strlen("nochunk");
338			__chunk_size = -1UL;
339		}
340
341		/* Group words together, delimited by "," */
342		while (*str && *str != ',')
343			str++;
344
345		if (*str == ',')
346			str++;
347	}
348
349	return EFI_SUCCESS;
350}
351
352/*
353 * Check the cmdline for a LILO-style file= arguments.
354 *
355 * We only support loading a file from the same filesystem as
356 * the kernel image.
357 */
358efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
359				  efi_loaded_image_t *image,
360				  char *cmd_line, char *option_string,
361				  unsigned long max_addr,
362				  unsigned long *load_addr,
363				  unsigned long *load_size)
364{
365	struct file_info *files;
366	unsigned long file_addr;
367	u64 file_size_total;
368	efi_file_handle_t *fh = NULL;
369	efi_status_t status;
370	int nr_files;
371	char *str;
372	int i, j, k;
373
374	file_addr = 0;
375	file_size_total = 0;
 
376
377	str = cmd_line;
 
 
 
378
379	j = 0;			/* See close_handles */
 
380
381	if (!load_addr || !load_size)
382		return EFI_INVALID_PARAMETER;
383
384	*load_addr = 0;
385	*load_size = 0;
386
387	if (!str || !*str)
388		return EFI_SUCCESS;
389
390	for (nr_files = 0; *str; nr_files++) {
391		str = strstr(str, option_string);
392		if (!str)
393			break;
394
395		str += strlen(option_string);
 
396
397		/* Skip any leading slashes */
398		while (*str == '/' || *str == '\\')
399			str++;
400
401		while (*str && *str != ' ' && *str != '\n')
402			str++;
403	}
 
 
404
405	if (!nr_files)
406		return EFI_SUCCESS;
407
408	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
409				nr_files * sizeof(*files), (void **)&files);
410	if (status != EFI_SUCCESS) {
411		pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
412		goto fail;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
413	}
 
414
415	str = cmd_line;
416	for (i = 0; i < nr_files; i++) {
417		struct file_info *file;
418		efi_char16_t filename_16[256];
419		efi_char16_t *p;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
421		str = strstr(str, option_string);
422		if (!str)
423			break;
 
424
425		str += strlen(option_string);
 
 
 
426
427		file = &files[i];
428		p = filename_16;
 
 
429
430		/* Skip any leading slashes */
431		while (*str == '/' || *str == '\\')
432			str++;
433
434		while (*str && *str != ' ' && *str != '\n') {
435			if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
436				break;
437
438			if (*str == '/') {
439				*p++ = '\\';
440				str++;
441			} else {
442				*p++ = *str++;
443			}
444		}
445
446		*p = '\0';
447
448		/* Only open the volume once. */
449		if (!i) {
450			status = efi_open_volume(sys_table_arg, image,
451						 (void **)&fh);
452			if (status != EFI_SUCCESS)
453				goto free_files;
454		}
455
456		status = efi_file_size(sys_table_arg, fh, filename_16,
457				       (void **)&file->handle, &file->size);
458		if (status != EFI_SUCCESS)
459			goto close_handles;
460
461		file_size_total += file->size;
462	}
 
 
463
464	if (file_size_total) {
465		unsigned long addr;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
466
467		/*
468		 * Multiple files need to be at consecutive addresses in memory,
469		 * so allocate enough memory for all the files.  This is used
470		 * for loading multiple files.
471		 */
472		status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
473				    &file_addr, max_addr);
474		if (status != EFI_SUCCESS) {
475			pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
476			goto close_handles;
477		}
478
479		/* We've run out of free low memory. */
480		if (file_addr > max_addr) {
481			pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
482			status = EFI_INVALID_PARAMETER;
483			goto free_file_total;
484		}
485
486		addr = file_addr;
487		for (j = 0; j < nr_files; j++) {
488			unsigned long size;
489
490			size = files[j].size;
491			while (size) {
492				unsigned long chunksize;
493				if (size > __chunk_size)
494					chunksize = __chunk_size;
495				else
496					chunksize = size;
497
498				status = efi_file_read(files[j].handle,
499						       &chunksize,
500						       (void *)addr);
501				if (status != EFI_SUCCESS) {
502					pr_efi_err(sys_table_arg, "Failed to read file\n");
503					goto free_file_total;
504				}
505				addr += chunksize;
506				size -= chunksize;
507			}
508
509			efi_file_close(files[j].handle);
510		}
511
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
512	}
 
 
513
514	efi_call_early(free_pool, files);
 
 
 
515
516	*load_addr = file_addr;
517	*load_size = file_size_total;
 
 
518
519	return status;
520
521free_file_total:
522	efi_free(sys_table_arg, file_size_total, file_addr);
523
524close_handles:
525	for (k = j; k < i; k++)
526		efi_file_close(files[k].handle);
527free_files:
528	efi_call_early(free_pool, files);
529fail:
530	*load_addr = 0;
531	*load_size = 0;
532
 
 
 
 
 
 
533	return status;
534}
535/*
536 * Relocate a kernel image, either compressed or uncompressed.
537 * In the ARM64 case, all kernel images are currently
538 * uncompressed, and as such when we relocate it we need to
539 * allocate additional space for the BSS segment. Any low
540 * memory that this function should avoid needs to be
541 * unavailable in the EFI memory map, as if the preferred
542 * address is not available the lowest available address will
543 * be used.
544 */
545efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
546				 unsigned long *image_addr,
547				 unsigned long image_size,
548				 unsigned long alloc_size,
549				 unsigned long preferred_addr,
550				 unsigned long alignment)
551{
552	unsigned long cur_image_addr;
553	unsigned long new_addr = 0;
 
554	efi_status_t status;
555	unsigned long nr_pages;
556	efi_physical_addr_t efi_addr = preferred_addr;
557
558	if (!image_addr || !image_size || !alloc_size)
559		return EFI_INVALID_PARAMETER;
560	if (alloc_size < image_size)
561		return EFI_INVALID_PARAMETER;
562
563	cur_image_addr = *image_addr;
 
 
564
565	/*
566	 * The EFI firmware loader could have placed the kernel image
567	 * anywhere in memory, but the kernel has restrictions on the
568	 * max physical address it can run at.  Some architectures
569	 * also have a prefered address, so first try to relocate
570	 * to the preferred address.  If that fails, allocate as low
571	 * as possible while respecting the required alignment.
572	 */
573	nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
574	status = efi_call_early(allocate_pages,
575				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
576				nr_pages, &efi_addr);
577	new_addr = efi_addr;
578	/*
579	 * If preferred address allocation failed allocate as low as
580	 * possible.
581	 */
582	if (status != EFI_SUCCESS) {
583		status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
584				       &new_addr);
585	}
586	if (status != EFI_SUCCESS) {
587		pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
588		return status;
 
 
 
 
 
 
 
 
589	}
590
591	/*
592	 * We know source/dest won't overlap since both memory ranges
593	 * have been allocated by UEFI, so we can safely use memcpy.
594	 */
595	memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
596
597	/* Return the new address of the relocated image. */
598	*image_addr = new_addr;
599
600	return status;
601}
602
603/*
604 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
605 * This overestimates for surrogates, but that is okay.
 
 
 
 
 
 
 
 
 
 
606 */
607static int efi_utf8_bytes(u16 c)
 
608{
609	return 1 + (c >= 0x80) + (c >= 0x800);
610}
 
 
611
612/*
613 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
614 */
615static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
616{
617	unsigned int c;
 
 
618
619	while (n--) {
620		c = *src++;
621		if (n && c >= 0xd800 && c <= 0xdbff &&
622		    *src >= 0xdc00 && *src <= 0xdfff) {
623			c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
624			src++;
625			n--;
626		}
627		if (c >= 0xd800 && c <= 0xdfff)
628			c = 0xfffd; /* Unmatched surrogate */
629		if (c < 0x80) {
630			*dst++ = c;
631			continue;
632		}
633		if (c < 0x800) {
634			*dst++ = 0xc0 + (c >> 6);
635			goto t1;
636		}
637		if (c < 0x10000) {
638			*dst++ = 0xe0 + (c >> 12);
639			goto t2;
640		}
641		*dst++ = 0xf0 + (c >> 18);
642		*dst++ = 0x80 + ((c >> 12) & 0x3f);
643	t2:
644		*dst++ = 0x80 + ((c >> 6) & 0x3f);
645	t1:
646		*dst++ = 0x80 + (c & 0x3f);
647	}
648
649	return dst;
650}
651
652#ifndef MAX_CMDLINE_ADDRESS
653#define MAX_CMDLINE_ADDRESS	ULONG_MAX
654#endif
655
656/*
657 * Convert the unicode UEFI command line to ASCII to pass to kernel.
658 * Size of memory allocated return in *cmd_line_len.
659 * Returns NULL on error.
660 */
661char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
662			  efi_loaded_image_t *image,
663			  int *cmd_line_len)
664{
665	const u16 *s2;
666	u8 *s1 = NULL;
667	unsigned long cmdline_addr = 0;
668	int load_options_chars = image->load_options_size / 2; /* UTF-16 */
669	const u16 *options = image->load_options;
670	int options_bytes = 0;  /* UTF-8 bytes */
671	int options_chars = 0;  /* UTF-16 chars */
672	efi_status_t status;
673	u16 zero = 0;
674
675	if (options) {
676		s2 = options;
677		while (*s2 && *s2 != '\n'
678		       && options_chars < load_options_chars) {
679			options_bytes += efi_utf8_bytes(*s2++);
680			options_chars++;
681		}
682	}
683
684	if (!options_chars) {
685		/* No command line options, so return empty string*/
686		options = &zero;
 
 
 
 
 
 
 
 
 
 
 
 
 
687	}
688
689	options_bytes++;	/* NUL termination */
690
691	status = efi_high_alloc(sys_table_arg, options_bytes, 0,
692				&cmdline_addr, MAX_CMDLINE_ADDRESS);
693	if (status != EFI_SUCCESS)
694		return NULL;
695
696	s1 = (u8 *)cmdline_addr;
697	s2 = (const u16 *)options;
698
699	s1 = efi_utf16_to_utf8(s1, s2, options_chars);
700	*s1 = '\0';
701
702	*cmd_line_len = options_bytes;
703	return (char *)cmdline_addr;
704}