1 // SPDX-License-Identifier: GPL-2.0
3 * Helper functions used by the EFI stub on multiple
4 * architectures. This should be #included by the EFI stub
5 * implementation files.
7 * Copyright 2011 Intel Corporation; author Matt Fleming
12 #include <linux/ctype.h>
13 #include <linux/efi.h>
14 #include <linux/kernel.h>
15 #include <linux/printk.h> /* For CONSOLE_LOGLEVEL_* */
17 #include <asm/setup.h>
22 bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE);
24 int efi_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
27 static bool efi_nosoftreserve;
28 static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
30 bool __pure __efi_soft_reserve_enabled(void)
32 return !efi_nosoftreserve;
36 * efi_char16_puts() - Write a UCS-2 encoded string to the console
37 * @str: UCS-2 encoded string
39 void efi_char16_puts(efi_char16_t *str)
41 efi_call_proto(efi_table_attr(efi_system_table, con_out),
46 u32 utf8_to_utf32(const u8 **s8)
54 * The position of the most-significant 0 bit gives us the length of
55 * a multi-octet encoding.
57 for (clen = 0; cx & 0x80; ++clen)
60 * If the 0 bit is in position 8, this is a valid single-octet
61 * encoding. If the 0 bit is in position 7 or positions 1-3, the
62 * encoding is invalid.
63 * In either case, we just return the first octet.
65 if (clen < 2 || clen > 4)
67 /* Get the bits from the first octet. */
69 for (i = 0; i < clen; ++i) {
70 /* Trailing octets must have 10 in most significant bits. */
74 c32 = (c32 << 6) | cx;
78 * - The character must be in the Unicode range.
79 * - It must not be a surrogate.
80 * - It must be encoded using the correct number of octets.
83 (c32 & 0xf800) == 0xd800 ||
84 clen != (c32 >= 0x80) + (c32 >= 0x800) + (c32 >= 0x10000))
91 * efi_puts() - Write a UTF-8 encoded string to the console
92 * @str: UTF-8 encoded string
94 void efi_puts(const char *str)
96 efi_char16_t buf[128];
97 size_t pos = 0, lim = ARRAY_SIZE(buf);
98 const u8 *s8 = (const u8 *)str;
104 c32 = utf8_to_utf32(&s8);
106 /* Characters in plane 0 use a single word. */
110 * Characters in other planes encode into a surrogate
113 buf[pos++] = (0xd800 - (0x10000 >> 10)) + (c32 >> 10);
114 buf[pos++] = 0xdc00 + (c32 & 0x3ff);
116 if (*s8 == '\0' || pos >= lim - 2) {
118 efi_char16_puts(buf);
125 * efi_printk() - Print a kernel message
126 * @fmt: format string
128 * The first letter of the format string is used to determine the logging level
129 * of the message. If the level is less then the current EFI logging level, the
130 * message is suppressed. The message will be truncated to 255 bytes.
132 * Return: number of printed characters
134 int efi_printk(const char *fmt, ...)
136 char printf_buf[256];
139 int loglevel = printk_get_level(fmt);
147 * Use loglevel -1 for cases where we just want to print to
154 if (loglevel >= efi_loglevel)
158 efi_puts("EFI stub: ");
160 fmt = printk_skip_level(fmt);
163 printed = vsnprintf(printf_buf, sizeof(printf_buf), fmt, args);
166 efi_puts(printf_buf);
167 if (printed >= sizeof(printf_buf)) {
168 efi_puts("[Message truncated]\n");
176 * efi_parse_options() - Parse EFI command line options
177 * @cmdline: kernel command line
179 * Parse the ASCII string @cmdline for EFI options, denoted by the efi=
180 * option, e.g. efi=nochunk.
182 * It should be noted that efi= is parsed in two very different
183 * environments, first in the early boot environment of the EFI boot
184 * stub, and subsequently during the kernel boot.
186 * Return: status code
188 efi_status_t efi_parse_options(char const *cmdline)
197 len = strnlen(cmdline, COMMAND_LINE_SIZE - 1) + 1;
198 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
199 if (status != EFI_SUCCESS)
202 memcpy(buf, cmdline, len - 1);
204 str = skip_spaces(buf);
209 str = next_arg(str, ¶m, &val);
210 if (!val && !strcmp(param, "--"))
213 if (!strcmp(param, "nokaslr")) {
215 } else if (!strcmp(param, "quiet")) {
216 efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
217 } else if (!strcmp(param, "noinitrd")) {
219 } else if (!strcmp(param, "efi") && val) {
220 efi_nochunk = parse_option_str(val, "nochunk");
221 efi_novamap = parse_option_str(val, "novamap");
223 efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
224 parse_option_str(val, "nosoftreserve");
226 if (parse_option_str(val, "disable_early_pci_dma"))
227 efi_disable_pci_dma = true;
228 if (parse_option_str(val, "no_disable_early_pci_dma"))
229 efi_disable_pci_dma = false;
230 if (parse_option_str(val, "debug"))
231 efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
232 } else if (!strcmp(param, "video") &&
233 val && strstarts(val, "efifb:")) {
234 efi_parse_option_graphics(val + strlen("efifb:"));
237 efi_bs_call(free_pool, buf);
242 * Convert the unicode UEFI command line to ASCII to pass to kernel.
243 * Size of memory allocated return in *cmd_line_len.
244 * Returns NULL on error.
246 char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len)
249 unsigned long cmdline_addr = 0;
250 int options_chars = efi_table_attr(image, load_options_size) / 2;
251 const u16 *options = efi_table_attr(image, load_options);
252 int options_bytes = 0, safe_options_bytes = 0; /* UTF-8 bytes */
253 bool in_quote = false;
258 while (options_bytes < COMMAND_LINE_SIZE && options_chars--) {
262 if (c == L'\0' || c == L'\n')
265 in_quote = !in_quote;
266 else if (!in_quote && isspace((char)c))
267 safe_options_bytes = options_bytes;
274 * Get the number of UTF-8 bytes corresponding to a
276 * The first part handles everything in the BMP.
278 options_bytes += 2 + (c >= 0x800);
280 * Add one more byte for valid surrogate pairs. Invalid
281 * surrogates will be replaced with 0xfffd and take up
284 if ((c & 0xfc00) == 0xd800) {
286 * If the very last word is a high surrogate,
287 * we must ignore it since we can't access the
290 if (!options_chars) {
292 } else if ((*s2 & 0xfc00) == 0xdc00) {
299 if (options_bytes >= COMMAND_LINE_SIZE) {
300 options_bytes = safe_options_bytes;
301 efi_err("Command line is too long: truncated to %d bytes\n",
306 options_bytes++; /* NUL termination */
308 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, options_bytes,
309 (void **)&cmdline_addr);
310 if (status != EFI_SUCCESS)
313 snprintf((char *)cmdline_addr, options_bytes, "%.*ls",
314 options_bytes - 1, options);
316 *cmd_line_len = options_bytes;
317 return (char *)cmdline_addr;
321 * efi_exit_boot_services() - Exit boot services
322 * @handle: handle of the exiting image
323 * @map: pointer to receive the memory map
324 * @priv: argument to be passed to @priv_func
325 * @priv_func: function to process the memory map before exiting boot services
327 * Handle calling ExitBootServices according to the requirements set out by the
328 * spec. Obtains the current memory map, and returns that info after calling
329 * ExitBootServices. The client must specify a function to perform any
330 * processing of the memory map data prior to ExitBootServices. A client
331 * specific structure may be passed to the function via priv. The client
332 * function may be called multiple times.
334 * Return: status code
336 efi_status_t efi_exit_boot_services(void *handle,
337 struct efi_boot_memmap *map,
339 efi_exit_boot_map_processing priv_func)
343 status = efi_get_memory_map(map);
345 if (status != EFI_SUCCESS)
348 status = priv_func(map, priv);
349 if (status != EFI_SUCCESS)
352 if (efi_disable_pci_dma)
353 efi_pci_disable_bridge_busmaster();
355 status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
357 if (status == EFI_INVALID_PARAMETER) {
359 * The memory map changed between efi_get_memory_map() and
360 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
361 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
362 * updated map, and try again. The spec implies one retry
363 * should be sufficent, which is confirmed against the EDK2
364 * implementation. Per the spec, we can only invoke
365 * get_memory_map() and exit_boot_services() - we cannot alloc
366 * so efi_get_memory_map() cannot be used, and we must reuse
367 * the buffer. For all practical purposes, the headroom in the
368 * buffer should account for any changes in the map so the call
369 * to get_memory_map() is expected to succeed here.
371 *map->map_size = *map->buff_size;
372 status = efi_bs_call(get_memory_map,
379 /* exit_boot_services() was called, thus cannot free */
380 if (status != EFI_SUCCESS)
383 status = priv_func(map, priv);
384 /* exit_boot_services() was called, thus cannot free */
385 if (status != EFI_SUCCESS)
388 status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
391 /* exit_boot_services() was called, thus cannot free */
392 if (status != EFI_SUCCESS)
398 efi_bs_call(free_pool, *map->map);
404 * get_efi_config_table() - retrieve UEFI configuration table
405 * @guid: GUID of the configuration table to be retrieved
406 * Return: pointer to the configuration table or NULL
408 void *get_efi_config_table(efi_guid_t guid)
410 unsigned long tables = efi_table_attr(efi_system_table, tables);
411 int nr_tables = efi_table_attr(efi_system_table, nr_tables);
414 for (i = 0; i < nr_tables; i++) {
415 efi_config_table_t *t = (void *)tables;
417 if (efi_guidcmp(t->guid, guid) == 0)
418 return efi_table_attr(t, table);
420 tables += efi_is_native() ? sizeof(efi_config_table_t)
421 : sizeof(efi_config_table_32_t);
427 * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
428 * for the firmware or bootloader to expose the initrd data directly to the stub
429 * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
430 * very easy to implement. It is a simple Linux initrd specific conduit between
431 * kernel and firmware, allowing us to put the EFI stub (being part of the
432 * kernel) in charge of where and when to load the initrd, while leaving it up
433 * to the firmware to decide whether it needs to expose its filesystem hierarchy
436 static const struct {
437 struct efi_vendor_dev_path vendor;
438 struct efi_generic_dev_path end;
439 } __packed initrd_dev_path = {
443 EFI_DEV_MEDIA_VENDOR,
444 sizeof(struct efi_vendor_dev_path),
446 LINUX_EFI_INITRD_MEDIA_GUID
450 sizeof(struct efi_generic_dev_path)
455 * efi_load_initrd_dev_path() - load the initrd from the Linux initrd device path
456 * @load_addr: pointer to store the address where the initrd was loaded
457 * @load_size: pointer to store the size of the loaded initrd
458 * @max: upper limit for the initrd memory allocation
461 * * %EFI_SUCCESS if the initrd was loaded successfully, in which
462 * case @load_addr and @load_size are assigned accordingly
463 * * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd device path
464 * * %EFI_INVALID_PARAMETER if load_addr == NULL or load_size == NULL
465 * * %EFI_OUT_OF_RESOURCES if memory allocation failed
466 * * %EFI_LOAD_ERROR in all other cases
469 efi_status_t efi_load_initrd_dev_path(unsigned long *load_addr,
470 unsigned long *load_size,
473 efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
474 efi_device_path_protocol_t *dp;
475 efi_load_file2_protocol_t *lf2;
476 unsigned long initrd_addr;
477 unsigned long initrd_size;
481 dp = (efi_device_path_protocol_t *)&initrd_dev_path;
482 status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
483 if (status != EFI_SUCCESS)
486 status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
488 if (status != EFI_SUCCESS)
491 status = efi_call_proto(lf2, load_file, dp, false, &initrd_size, NULL);
492 if (status != EFI_BUFFER_TOO_SMALL)
493 return EFI_LOAD_ERROR;
495 status = efi_allocate_pages(initrd_size, &initrd_addr, max);
496 if (status != EFI_SUCCESS)
499 status = efi_call_proto(lf2, load_file, dp, false, &initrd_size,
500 (void *)initrd_addr);
501 if (status != EFI_SUCCESS) {
502 efi_free(initrd_size, initrd_addr);
503 return EFI_LOAD_ERROR;
506 *load_addr = initrd_addr;
507 *load_size = initrd_size;
512 efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
513 unsigned long *load_addr,
514 unsigned long *load_size,
515 unsigned long soft_limit,
516 unsigned long hard_limit)
518 if (!IS_ENABLED(CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER) ||
519 (IS_ENABLED(CONFIG_X86) && (!efi_is_native() || image == NULL))) {
520 *load_addr = *load_size = 0;
524 return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
525 soft_limit, hard_limit,
526 load_addr, load_size);
530 * efi_load_initrd() - Load initial RAM disk
531 * @image: EFI loaded image protocol
532 * @load_addr: pointer to loaded initrd
533 * @load_size: size of loaded initrd
534 * @soft_limit: preferred size of allocated memory for loading the initrd
535 * @hard_limit: minimum size of allocated memory
537 * Return: status code
539 efi_status_t efi_load_initrd(efi_loaded_image_t *image,
540 unsigned long *load_addr,
541 unsigned long *load_size,
542 unsigned long soft_limit,
543 unsigned long hard_limit)
547 if (!load_addr || !load_size)
548 return EFI_INVALID_PARAMETER;
550 status = efi_load_initrd_dev_path(load_addr, load_size, hard_limit);
551 if (status == EFI_SUCCESS) {
552 efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
553 } else if (status == EFI_NOT_FOUND) {
554 status = efi_load_initrd_cmdline(image, load_addr, load_size,
555 soft_limit, hard_limit);
556 if (status == EFI_SUCCESS && *load_size > 0)
557 efi_info("Loaded initrd from command line option\n");
564 * efi_wait_for_key() - Wait for key stroke
565 * @usec: number of microseconds to wait for key stroke
568 * Wait for up to @usec microseconds for a key stroke.
570 * Return: status code, EFI_SUCCESS if key received
572 efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
574 efi_event_t events[2], timer;
576 efi_simple_text_input_protocol_t *con_in;
579 con_in = efi_table_attr(efi_system_table, con_in);
581 return EFI_UNSUPPORTED;
582 efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
584 status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
585 if (status != EFI_SUCCESS)
588 status = efi_bs_call(set_timer, timer, EfiTimerRelative,
589 EFI_100NSEC_PER_USEC * usec);
590 if (status != EFI_SUCCESS)
592 efi_set_event_at(events, 1, timer);
594 status = efi_bs_call(wait_for_event, 2, events, &index);
595 if (status == EFI_SUCCESS) {
597 status = efi_call_proto(con_in, read_keystroke, key);
599 status = EFI_TIMEOUT;
602 efi_bs_call(close_event, timer);