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 <stdarg.h>
11 
12 #include <linux/efi.h>
13 #include <linux/kernel.h>
14 #include <linux/printk.h> /* For CONSOLE_LOGLEVEL_* */
15 #include <asm/efi.h>
16 
17 #include "efistub.h"
18 
19 bool efi_nochunk;
20 bool efi_nokaslr;
21 bool efi_noinitrd;
22 int efi_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
23 bool efi_novamap;
24 
25 static bool efi_nosoftreserve;
26 static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
27 
28 bool __pure __efi_soft_reserve_enabled(void)
29 {
30 	return !efi_nosoftreserve;
31 }
32 
33 void efi_char16_puts(efi_char16_t *str)
34 {
35 	efi_call_proto(efi_table_attr(efi_system_table, con_out),
36 		       output_string, str);
37 }
38 
39 static
40 u32 utf8_to_utf32(const u8 **s8)
41 {
42 	u32 c32;
43 	u8 c0, cx;
44 	size_t clen, i;
45 
46 	c0 = cx = *(*s8)++;
47 	/*
48 	 * The position of the most-significant 0 bit gives us the length of
49 	 * a multi-octet encoding.
50 	 */
51 	for (clen = 0; cx & 0x80; ++clen)
52 		cx <<= 1;
53 	/*
54 	 * If the 0 bit is in position 8, this is a valid single-octet
55 	 * encoding. If the 0 bit is in position 7 or positions 1-3, the
56 	 * encoding is invalid.
57 	 * In either case, we just return the first octet.
58 	 */
59 	if (clen < 2 || clen > 4)
60 		return c0;
61 	/* Get the bits from the first octet. */
62 	c32 = cx >> clen--;
63 	for (i = 0; i < clen; ++i) {
64 		/* Trailing octets must have 10 in most significant bits. */
65 		cx = (*s8)[i] ^ 0x80;
66 		if (cx & 0xc0)
67 			return c0;
68 		c32 = (c32 << 6) | cx;
69 	}
70 	/*
71 	 * Check for validity:
72 	 * - The character must be in the Unicode range.
73 	 * - It must not be a surrogate.
74 	 * - It must be encoded using the correct number of octets.
75 	 */
76 	if (c32 > 0x10ffff ||
77 	    (c32 & 0xf800) == 0xd800 ||
78 	    clen != (c32 >= 0x80) + (c32 >= 0x800) + (c32 >= 0x10000))
79 		return c0;
80 	*s8 += clen;
81 	return c32;
82 }
83 
84 void efi_puts(const char *str)
85 {
86 	efi_char16_t buf[128];
87 	size_t pos = 0, lim = ARRAY_SIZE(buf);
88 	const u8 *s8 = (const u8 *)str;
89 	u32 c32;
90 
91 	while (*s8) {
92 		if (*s8 == '\n')
93 			buf[pos++] = L'\r';
94 		c32 = utf8_to_utf32(&s8);
95 		if (c32 < 0x10000) {
96 			/* Characters in plane 0 use a single word. */
97 			buf[pos++] = c32;
98 		} else {
99 			/*
100 			 * Characters in other planes encode into a surrogate
101 			 * pair.
102 			 */
103 			buf[pos++] = (0xd800 - (0x10000 >> 10)) + (c32 >> 10);
104 			buf[pos++] = 0xdc00 + (c32 & 0x3ff);
105 		}
106 		if (*s8 == '\0' || pos >= lim - 2) {
107 			buf[pos] = L'\0';
108 			efi_char16_puts(buf);
109 			pos = 0;
110 		}
111 	}
112 }
113 
114 int efi_printk(const char *fmt, ...)
115 {
116 	char printf_buf[256];
117 	va_list args;
118 	int printed;
119 	int loglevel = printk_get_level(fmt);
120 
121 	switch (loglevel) {
122 	case '0' ... '9':
123 		loglevel -= '0';
124 		break;
125 	default:
126 		/*
127 		 * Use loglevel -1 for cases where we just want to print to
128 		 * the screen.
129 		 */
130 		loglevel = -1;
131 		break;
132 	}
133 
134 	if (loglevel >= efi_loglevel)
135 		return 0;
136 
137 	if (loglevel >= 0)
138 		efi_puts("EFI stub: ");
139 
140 	fmt = printk_skip_level(fmt);
141 
142 	va_start(args, fmt);
143 	printed = vsnprintf(printf_buf, sizeof(printf_buf), fmt, args);
144 	va_end(args);
145 
146 	efi_puts(printf_buf);
147 	if (printed >= sizeof(printf_buf)) {
148 		efi_puts("[Message truncated]\n");
149 		return -1;
150 	}
151 
152 	return printed;
153 }
154 
155 /*
156  * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
157  * option, e.g. efi=nochunk.
158  *
159  * It should be noted that efi= is parsed in two very different
160  * environments, first in the early boot environment of the EFI boot
161  * stub, and subsequently during the kernel boot.
162  */
163 efi_status_t efi_parse_options(char const *cmdline)
164 {
165 	size_t len = strlen(cmdline) + 1;
166 	efi_status_t status;
167 	char *str, *buf;
168 
169 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
170 	if (status != EFI_SUCCESS)
171 		return status;
172 
173 	str = skip_spaces(memcpy(buf, cmdline, len));
174 
175 	while (*str) {
176 		char *param, *val;
177 
178 		str = next_arg(str, &param, &val);
179 
180 		if (!strcmp(param, "nokaslr")) {
181 			efi_nokaslr = true;
182 		} else if (!strcmp(param, "quiet")) {
183 			efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
184 		} else if (!strcmp(param, "noinitrd")) {
185 			efi_noinitrd = true;
186 		} else if (!strcmp(param, "efi") && val) {
187 			efi_nochunk = parse_option_str(val, "nochunk");
188 			efi_novamap = parse_option_str(val, "novamap");
189 
190 			efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
191 					    parse_option_str(val, "nosoftreserve");
192 
193 			if (parse_option_str(val, "disable_early_pci_dma"))
194 				efi_disable_pci_dma = true;
195 			if (parse_option_str(val, "no_disable_early_pci_dma"))
196 				efi_disable_pci_dma = false;
197 			if (parse_option_str(val, "debug"))
198 				efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
199 		} else if (!strcmp(param, "video") &&
200 			   val && strstarts(val, "efifb:")) {
201 			efi_parse_option_graphics(val + strlen("efifb:"));
202 		}
203 	}
204 	efi_bs_call(free_pool, buf);
205 	return EFI_SUCCESS;
206 }
207 
208 /*
209  * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
210  */
211 static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
212 {
213 	unsigned int c;
214 
215 	while (n--) {
216 		c = *src++;
217 		if (n && c >= 0xd800 && c <= 0xdbff &&
218 		    *src >= 0xdc00 && *src <= 0xdfff) {
219 			c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
220 			src++;
221 			n--;
222 		}
223 		if (c >= 0xd800 && c <= 0xdfff)
224 			c = 0xfffd; /* Unmatched surrogate */
225 		if (c < 0x80) {
226 			*dst++ = c;
227 			continue;
228 		}
229 		if (c < 0x800) {
230 			*dst++ = 0xc0 + (c >> 6);
231 			goto t1;
232 		}
233 		if (c < 0x10000) {
234 			*dst++ = 0xe0 + (c >> 12);
235 			goto t2;
236 		}
237 		*dst++ = 0xf0 + (c >> 18);
238 		*dst++ = 0x80 + ((c >> 12) & 0x3f);
239 	t2:
240 		*dst++ = 0x80 + ((c >> 6) & 0x3f);
241 	t1:
242 		*dst++ = 0x80 + (c & 0x3f);
243 	}
244 
245 	return dst;
246 }
247 
248 /*
249  * Convert the unicode UEFI command line to ASCII to pass to kernel.
250  * Size of memory allocated return in *cmd_line_len.
251  * Returns NULL on error.
252  */
253 char *efi_convert_cmdline(efi_loaded_image_t *image,
254 			  int *cmd_line_len, unsigned long max_addr)
255 {
256 	const u16 *s2;
257 	u8 *s1 = NULL;
258 	unsigned long cmdline_addr = 0;
259 	int load_options_chars = efi_table_attr(image, load_options_size) / 2;
260 	const u16 *options = efi_table_attr(image, load_options);
261 	int options_bytes = 0;  /* UTF-8 bytes */
262 	int options_chars = 0;  /* UTF-16 chars */
263 	efi_status_t status;
264 	u16 zero = 0;
265 
266 	if (options) {
267 		s2 = options;
268 		while (options_chars < load_options_chars) {
269 			u16 c = *s2++;
270 
271 			if (c == L'\0' || c == L'\n')
272 				break;
273 			/*
274 			 * Get the number of UTF-8 bytes corresponding to a
275 			 * UTF-16 character.
276 			 * The first part handles everything in the BMP.
277 			 */
278 			options_bytes += 1 + (c >= 0x80) + (c >= 0x800);
279 			options_chars++;
280 			/*
281 			 * Add one more byte for valid surrogate pairs. Invalid
282 			 * surrogates will be replaced with 0xfffd and take up
283 			 * only 3 bytes.
284 			 */
285 			if ((c & 0xfc00) == 0xd800) {
286 				/*
287 				 * If the very last word is a high surrogate,
288 				 * we must ignore it since we can't access the
289 				 * low surrogate.
290 				 */
291 				if (options_chars == load_options_chars) {
292 					options_bytes -= 3;
293 					options_chars--;
294 					break;
295 				} else if ((*s2 & 0xfc00) == 0xdc00) {
296 					options_bytes++;
297 					options_chars++;
298 					s2++;
299 				}
300 			}
301 		}
302 	}
303 
304 	if (!options_chars) {
305 		/* No command line options, so return empty string*/
306 		options = &zero;
307 	}
308 
309 	options_bytes++;	/* NUL termination */
310 
311 	status = efi_allocate_pages(options_bytes, &cmdline_addr, max_addr);
312 	if (status != EFI_SUCCESS)
313 		return NULL;
314 
315 	s1 = (u8 *)cmdline_addr;
316 	s2 = (const u16 *)options;
317 
318 	s1 = efi_utf16_to_utf8(s1, s2, options_chars);
319 	*s1 = '\0';
320 
321 	*cmd_line_len = options_bytes;
322 	return (char *)cmdline_addr;
323 }
324 
325 /*
326  * Handle calling ExitBootServices according to the requirements set out by the
327  * spec.  Obtains the current memory map, and returns that info after calling
328  * ExitBootServices.  The client must specify a function to perform any
329  * processing of the memory map data prior to ExitBootServices.  A client
330  * specific structure may be passed to the function via priv.  The client
331  * function may be called multiple times.
332  */
333 efi_status_t efi_exit_boot_services(void *handle,
334 				    struct efi_boot_memmap *map,
335 				    void *priv,
336 				    efi_exit_boot_map_processing priv_func)
337 {
338 	efi_status_t status;
339 
340 	status = efi_get_memory_map(map);
341 
342 	if (status != EFI_SUCCESS)
343 		goto fail;
344 
345 	status = priv_func(map, priv);
346 	if (status != EFI_SUCCESS)
347 		goto free_map;
348 
349 	if (efi_disable_pci_dma)
350 		efi_pci_disable_bridge_busmaster();
351 
352 	status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
353 
354 	if (status == EFI_INVALID_PARAMETER) {
355 		/*
356 		 * The memory map changed between efi_get_memory_map() and
357 		 * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
358 		 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
359 		 * updated map, and try again.  The spec implies one retry
360 		 * should be sufficent, which is confirmed against the EDK2
361 		 * implementation.  Per the spec, we can only invoke
362 		 * get_memory_map() and exit_boot_services() - we cannot alloc
363 		 * so efi_get_memory_map() cannot be used, and we must reuse
364 		 * the buffer.  For all practical purposes, the headroom in the
365 		 * buffer should account for any changes in the map so the call
366 		 * to get_memory_map() is expected to succeed here.
367 		 */
368 		*map->map_size = *map->buff_size;
369 		status = efi_bs_call(get_memory_map,
370 				     map->map_size,
371 				     *map->map,
372 				     map->key_ptr,
373 				     map->desc_size,
374 				     map->desc_ver);
375 
376 		/* exit_boot_services() was called, thus cannot free */
377 		if (status != EFI_SUCCESS)
378 			goto fail;
379 
380 		status = priv_func(map, priv);
381 		/* exit_boot_services() was called, thus cannot free */
382 		if (status != EFI_SUCCESS)
383 			goto fail;
384 
385 		status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
386 	}
387 
388 	/* exit_boot_services() was called, thus cannot free */
389 	if (status != EFI_SUCCESS)
390 		goto fail;
391 
392 	return EFI_SUCCESS;
393 
394 free_map:
395 	efi_bs_call(free_pool, *map->map);
396 fail:
397 	return status;
398 }
399 
400 void *get_efi_config_table(efi_guid_t guid)
401 {
402 	unsigned long tables = efi_table_attr(efi_system_table, tables);
403 	int nr_tables = efi_table_attr(efi_system_table, nr_tables);
404 	int i;
405 
406 	for (i = 0; i < nr_tables; i++) {
407 		efi_config_table_t *t = (void *)tables;
408 
409 		if (efi_guidcmp(t->guid, guid) == 0)
410 			return efi_table_attr(t, table);
411 
412 		tables += efi_is_native() ? sizeof(efi_config_table_t)
413 					  : sizeof(efi_config_table_32_t);
414 	}
415 	return NULL;
416 }
417 
418 /*
419  * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
420  * for the firmware or bootloader to expose the initrd data directly to the stub
421  * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
422  * very easy to implement. It is a simple Linux initrd specific conduit between
423  * kernel and firmware, allowing us to put the EFI stub (being part of the
424  * kernel) in charge of where and when to load the initrd, while leaving it up
425  * to the firmware to decide whether it needs to expose its filesystem hierarchy
426  * via EFI protocols.
427  */
428 static const struct {
429 	struct efi_vendor_dev_path	vendor;
430 	struct efi_generic_dev_path	end;
431 } __packed initrd_dev_path = {
432 	{
433 		{
434 			EFI_DEV_MEDIA,
435 			EFI_DEV_MEDIA_VENDOR,
436 			sizeof(struct efi_vendor_dev_path),
437 		},
438 		LINUX_EFI_INITRD_MEDIA_GUID
439 	}, {
440 		EFI_DEV_END_PATH,
441 		EFI_DEV_END_ENTIRE,
442 		sizeof(struct efi_generic_dev_path)
443 	}
444 };
445 
446 /**
447  * efi_load_initrd_dev_path - load the initrd from the Linux initrd device path
448  * @load_addr:	pointer to store the address where the initrd was loaded
449  * @load_size:	pointer to store the size of the loaded initrd
450  * @max:	upper limit for the initrd memory allocation
451  * @return:	%EFI_SUCCESS if the initrd was loaded successfully, in which
452  *		case @load_addr and @load_size are assigned accordingly
453  *		%EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd
454  *		device path
455  *		%EFI_INVALID_PARAMETER if load_addr == NULL or load_size == NULL
456  *		%EFI_OUT_OF_RESOURCES if memory allocation failed
457  *		%EFI_LOAD_ERROR in all other cases
458  */
459 static
460 efi_status_t efi_load_initrd_dev_path(unsigned long *load_addr,
461 				      unsigned long *load_size,
462 				      unsigned long max)
463 {
464 	efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
465 	efi_device_path_protocol_t *dp;
466 	efi_load_file2_protocol_t *lf2;
467 	unsigned long initrd_addr;
468 	unsigned long initrd_size;
469 	efi_handle_t handle;
470 	efi_status_t status;
471 
472 	dp = (efi_device_path_protocol_t *)&initrd_dev_path;
473 	status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
474 	if (status != EFI_SUCCESS)
475 		return status;
476 
477 	status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
478 			     (void **)&lf2);
479 	if (status != EFI_SUCCESS)
480 		return status;
481 
482 	status = efi_call_proto(lf2, load_file, dp, false, &initrd_size, NULL);
483 	if (status != EFI_BUFFER_TOO_SMALL)
484 		return EFI_LOAD_ERROR;
485 
486 	status = efi_allocate_pages(initrd_size, &initrd_addr, max);
487 	if (status != EFI_SUCCESS)
488 		return status;
489 
490 	status = efi_call_proto(lf2, load_file, dp, false, &initrd_size,
491 				(void *)initrd_addr);
492 	if (status != EFI_SUCCESS) {
493 		efi_free(initrd_size, initrd_addr);
494 		return EFI_LOAD_ERROR;
495 	}
496 
497 	*load_addr = initrd_addr;
498 	*load_size = initrd_size;
499 	return EFI_SUCCESS;
500 }
501 
502 static
503 efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
504 				     unsigned long *load_addr,
505 				     unsigned long *load_size,
506 				     unsigned long soft_limit,
507 				     unsigned long hard_limit)
508 {
509 	if (!IS_ENABLED(CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER) ||
510 	    (IS_ENABLED(CONFIG_X86) && (!efi_is_native() || image == NULL))) {
511 		*load_addr = *load_size = 0;
512 		return EFI_SUCCESS;
513 	}
514 
515 	return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
516 				    soft_limit, hard_limit,
517 				    load_addr, load_size);
518 }
519 
520 efi_status_t efi_load_initrd(efi_loaded_image_t *image,
521 			     unsigned long *load_addr,
522 			     unsigned long *load_size,
523 			     unsigned long soft_limit,
524 			     unsigned long hard_limit)
525 {
526 	efi_status_t status;
527 
528 	if (!load_addr || !load_size)
529 		return EFI_INVALID_PARAMETER;
530 
531 	status = efi_load_initrd_dev_path(load_addr, load_size, hard_limit);
532 	if (status == EFI_SUCCESS) {
533 		efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
534 	} else if (status == EFI_NOT_FOUND) {
535 		status = efi_load_initrd_cmdline(image, load_addr, load_size,
536 						 soft_limit, hard_limit);
537 		if (status == EFI_SUCCESS && *load_size > 0)
538 			efi_info("Loaded initrd from command line option\n");
539 	}
540 
541 	return status;
542 }
543 
544 efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
545 {
546 	efi_event_t events[2], timer;
547 	unsigned long index;
548 	efi_simple_text_input_protocol_t *con_in;
549 	efi_status_t status;
550 
551 	con_in = efi_table_attr(efi_system_table, con_in);
552 	if (!con_in)
553 		return EFI_UNSUPPORTED;
554 	efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
555 
556 	status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
557 	if (status != EFI_SUCCESS)
558 		return status;
559 
560 	status = efi_bs_call(set_timer, timer, EfiTimerRelative,
561 			     EFI_100NSEC_PER_USEC * usec);
562 	if (status != EFI_SUCCESS)
563 		return status;
564 	efi_set_event_at(events, 1, timer);
565 
566 	status = efi_bs_call(wait_for_event, 2, events, &index);
567 	if (status == EFI_SUCCESS) {
568 		if (index == 0)
569 			status = efi_call_proto(con_in, read_keystroke, key);
570 		else
571 			status = EFI_TIMEOUT;
572 	}
573 
574 	efi_bs_call(close_event, timer);
575 
576 	return status;
577 }
578