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
10 #include <linux/stdarg.h>
12 #include <linux/efi.h>
13 #include <linux/kernel.h>
15 #include <asm/setup.h>
20 bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE);
23 static bool efi_noinitrd;
24 static bool efi_nosoftreserve;
25 static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
27 bool __pure __efi_soft_reserve_enabled(void)
29 return !efi_nosoftreserve;
33 * efi_parse_options() - Parse EFI command line options
34 * @cmdline: kernel command line
36 * Parse the ASCII string @cmdline for EFI options, denoted by the efi=
37 * option, e.g. efi=nochunk.
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.
45 efi_status_t efi_parse_options(char const *cmdline)
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)
59 memcpy(buf, cmdline, len - 1);
61 str = skip_spaces(buf);
66 str = next_arg(str, ¶m, &val);
67 if (!val && !strcmp(param, "--"))
70 if (!strcmp(param, "nokaslr")) {
72 } else if (!strcmp(param, "quiet")) {
73 efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
74 } else if (!strcmp(param, "noinitrd")) {
76 } else if (!strcmp(param, "efi") && val) {
77 efi_nochunk = parse_option_str(val, "nochunk");
78 efi_novamap |= parse_option_str(val, "novamap");
80 efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
81 parse_option_str(val, "nosoftreserve");
83 if (parse_option_str(val, "disable_early_pci_dma"))
84 efi_disable_pci_dma = true;
85 if (parse_option_str(val, "no_disable_early_pci_dma"))
86 efi_disable_pci_dma = false;
87 if (parse_option_str(val, "debug"))
88 efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
89 } else if (!strcmp(param, "video") &&
90 val && strstarts(val, "efifb:")) {
91 efi_parse_option_graphics(val + strlen("efifb:"));
94 efi_bs_call(free_pool, buf);
99 * The EFI_LOAD_OPTION descriptor has the following layout:
101 * u16 FilePathListLength;
103 * efi_device_path_protocol_t FilePathList[];
106 * This function validates and unpacks the variable-size data fields.
109 bool efi_load_option_unpack(efi_load_option_unpacked_t *dest,
110 const efi_load_option_t *src, size_t size)
114 efi_device_path_protocol_t header;
115 const efi_char16_t *description;
116 const efi_device_path_protocol_t *file_path_list;
118 if (size < offsetof(efi_load_option_t, variable_data))
120 pos = src->variable_data;
121 size -= offsetof(efi_load_option_t, variable_data);
123 if ((src->attributes & ~EFI_LOAD_OPTION_MASK) != 0)
126 /* Scan description. */
129 if (size < sizeof(c))
131 c = *(const u16 *)pos;
134 } while (c != L'\0');
136 /* Scan file_path_list. */
137 file_path_list = pos;
139 if (size < sizeof(header))
141 header = *(const efi_device_path_protocol_t *)pos;
142 if (header.length < sizeof(header))
144 if (size < header.length)
146 pos += header.length;
147 size -= header.length;
148 } while ((header.type != EFI_DEV_END_PATH && header.type != EFI_DEV_END_PATH2) ||
149 (header.sub_type != EFI_DEV_END_ENTIRE));
150 if (pos != (const void *)file_path_list + src->file_path_list_length)
153 dest->attributes = src->attributes;
154 dest->file_path_list_length = src->file_path_list_length;
155 dest->description = description;
156 dest->file_path_list = file_path_list;
157 dest->optional_data_size = size;
158 dest->optional_data = size ? pos : NULL;
164 * At least some versions of Dell firmware pass the entire contents of the
165 * Boot#### variable, i.e. the EFI_LOAD_OPTION descriptor, rather than just the
166 * OptionalData field.
168 * Detect this case and extract OptionalData.
170 void efi_apply_loadoptions_quirk(const void **load_options, u32 *load_options_size)
172 const efi_load_option_t *load_option = *load_options;
173 efi_load_option_unpacked_t load_option_unpacked;
175 if (!IS_ENABLED(CONFIG_X86))
179 if (*load_options_size < sizeof(*load_option))
181 if ((load_option->attributes & ~EFI_LOAD_OPTION_BOOT_MASK) != 0)
184 if (!efi_load_option_unpack(&load_option_unpacked, load_option, *load_options_size))
187 efi_warn_once(FW_BUG "LoadOptions is an EFI_LOAD_OPTION descriptor\n");
188 efi_warn_once(FW_BUG "Using OptionalData as a workaround\n");
190 *load_options = load_option_unpacked.optional_data;
191 *load_options_size = load_option_unpacked.optional_data_size;
196 EFISTUB_EVT_LOAD_OPTIONS,
200 #define STR_WITH_SIZE(s) sizeof(s), s
202 static const struct {
208 [EFISTUB_EVT_INITRD] = {
211 STR_WITH_SIZE("Linux initrd")
213 [EFISTUB_EVT_LOAD_OPTIONS] = {
215 LOAD_OPTIONS_EVENT_TAG_ID,
216 STR_WITH_SIZE("LOADED_IMAGE::LoadOptions")
220 static efi_status_t efi_measure_tagged_event(unsigned long load_addr,
221 unsigned long load_size,
222 enum efistub_event event)
224 efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
225 efi_tcg2_protocol_t *tcg2 = NULL;
228 efi_bs_call(locate_protocol, &tcg2_guid, NULL, (void **)&tcg2);
230 struct efi_measured_event {
231 efi_tcg2_event_t event_data;
232 efi_tcg2_tagged_event_t tagged_event;
233 u8 tagged_event_data[];
235 int size = sizeof(*evt) + events[event].event_data_len;
237 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
239 if (status != EFI_SUCCESS)
242 evt->event_data = (struct efi_tcg2_event){
244 .event_header.header_size = sizeof(evt->event_data.event_header),
245 .event_header.header_version = EFI_TCG2_EVENT_HEADER_VERSION,
246 .event_header.pcr_index = events[event].pcr_index,
247 .event_header.event_type = EV_EVENT_TAG,
250 evt->tagged_event = (struct efi_tcg2_tagged_event){
251 .tagged_event_id = events[event].event_id,
252 .tagged_event_data_size = events[event].event_data_len,
255 memcpy(evt->tagged_event_data, events[event].event_data,
256 events[event].event_data_len);
258 status = efi_call_proto(tcg2, hash_log_extend_event, 0,
259 load_addr, load_size, &evt->event_data);
260 efi_bs_call(free_pool, evt);
262 if (status != EFI_SUCCESS)
267 return EFI_UNSUPPORTED;
269 efi_warn("Failed to measure data for event %d: 0x%lx\n", event, status);
274 * Convert the unicode UEFI command line to ASCII to pass to kernel.
275 * Size of memory allocated return in *cmd_line_len.
276 * Returns NULL on error.
278 char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len)
280 const efi_char16_t *options = efi_table_attr(image, load_options);
281 u32 options_size = efi_table_attr(image, load_options_size);
282 int options_bytes = 0, safe_options_bytes = 0; /* UTF-8 bytes */
283 unsigned long cmdline_addr = 0;
284 const efi_char16_t *s2;
285 bool in_quote = false;
289 if (options_size > 0)
290 efi_measure_tagged_event((unsigned long)options, options_size,
291 EFISTUB_EVT_LOAD_OPTIONS);
293 efi_apply_loadoptions_quirk((const void **)&options, &options_size);
294 options_chars = options_size / sizeof(efi_char16_t);
298 while (options_bytes < COMMAND_LINE_SIZE && options_chars--) {
299 efi_char16_t c = *s2++;
302 if (c == L'\0' || c == L'\n')
305 in_quote = !in_quote;
306 else if (!in_quote && isspace((char)c))
307 safe_options_bytes = options_bytes;
314 * Get the number of UTF-8 bytes corresponding to a
316 * The first part handles everything in the BMP.
318 options_bytes += 2 + (c >= 0x800);
320 * Add one more byte for valid surrogate pairs. Invalid
321 * surrogates will be replaced with 0xfffd and take up
324 if ((c & 0xfc00) == 0xd800) {
326 * If the very last word is a high surrogate,
327 * we must ignore it since we can't access the
330 if (!options_chars) {
332 } else if ((*s2 & 0xfc00) == 0xdc00) {
339 if (options_bytes >= COMMAND_LINE_SIZE) {
340 options_bytes = safe_options_bytes;
341 efi_err("Command line is too long: truncated to %d bytes\n",
346 options_bytes++; /* NUL termination */
348 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, options_bytes,
349 (void **)&cmdline_addr);
350 if (status != EFI_SUCCESS)
353 snprintf((char *)cmdline_addr, options_bytes, "%.*ls",
354 options_bytes - 1, options);
356 *cmd_line_len = options_bytes;
357 return (char *)cmdline_addr;
361 * efi_exit_boot_services() - Exit boot services
362 * @handle: handle of the exiting image
363 * @priv: argument to be passed to @priv_func
364 * @priv_func: function to process the memory map before exiting boot services
366 * Handle calling ExitBootServices according to the requirements set out by the
367 * spec. Obtains the current memory map, and returns that info after calling
368 * ExitBootServices. The client must specify a function to perform any
369 * processing of the memory map data prior to ExitBootServices. A client
370 * specific structure may be passed to the function via priv. The client
371 * function may be called multiple times.
373 * Return: status code
375 efi_status_t efi_exit_boot_services(void *handle, void *priv,
376 efi_exit_boot_map_processing priv_func)
378 struct efi_boot_memmap *map;
381 if (efi_disable_pci_dma)
382 efi_pci_disable_bridge_busmaster();
384 status = efi_get_memory_map(&map, true);
385 if (status != EFI_SUCCESS)
388 status = priv_func(map, priv);
389 if (status != EFI_SUCCESS) {
390 efi_bs_call(free_pool, map);
394 status = efi_bs_call(exit_boot_services, handle, map->map_key);
396 if (status == EFI_INVALID_PARAMETER) {
398 * The memory map changed between efi_get_memory_map() and
399 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
400 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
401 * updated map, and try again. The spec implies one retry
402 * should be sufficent, which is confirmed against the EDK2
403 * implementation. Per the spec, we can only invoke
404 * get_memory_map() and exit_boot_services() - we cannot alloc
405 * so efi_get_memory_map() cannot be used, and we must reuse
406 * the buffer. For all practical purposes, the headroom in the
407 * buffer should account for any changes in the map so the call
408 * to get_memory_map() is expected to succeed here.
410 map->map_size = map->buff_size;
411 status = efi_bs_call(get_memory_map,
418 /* exit_boot_services() was called, thus cannot free */
419 if (status != EFI_SUCCESS)
422 status = priv_func(map, priv);
423 /* exit_boot_services() was called, thus cannot free */
424 if (status != EFI_SUCCESS)
427 status = efi_bs_call(exit_boot_services, handle, map->map_key);
434 * get_efi_config_table() - retrieve UEFI configuration table
435 * @guid: GUID of the configuration table to be retrieved
436 * Return: pointer to the configuration table or NULL
438 void *get_efi_config_table(efi_guid_t guid)
440 unsigned long tables = efi_table_attr(efi_system_table, tables);
441 int nr_tables = efi_table_attr(efi_system_table, nr_tables);
444 for (i = 0; i < nr_tables; i++) {
445 efi_config_table_t *t = (void *)tables;
447 if (efi_guidcmp(t->guid, guid) == 0)
448 return efi_table_attr(t, table);
450 tables += efi_is_native() ? sizeof(efi_config_table_t)
451 : sizeof(efi_config_table_32_t);
457 * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
458 * for the firmware or bootloader to expose the initrd data directly to the stub
459 * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
460 * very easy to implement. It is a simple Linux initrd specific conduit between
461 * kernel and firmware, allowing us to put the EFI stub (being part of the
462 * kernel) in charge of where and when to load the initrd, while leaving it up
463 * to the firmware to decide whether it needs to expose its filesystem hierarchy
466 static const struct {
467 struct efi_vendor_dev_path vendor;
468 struct efi_generic_dev_path end;
469 } __packed initrd_dev_path = {
473 EFI_DEV_MEDIA_VENDOR,
474 sizeof(struct efi_vendor_dev_path),
476 LINUX_EFI_INITRD_MEDIA_GUID
480 sizeof(struct efi_generic_dev_path)
485 * efi_load_initrd_dev_path() - load the initrd from the Linux initrd device path
486 * @initrd: pointer of struct to store the address where the initrd was loaded
487 * and the size of the loaded initrd
488 * @max: upper limit for the initrd memory allocation
491 * * %EFI_SUCCESS if the initrd was loaded successfully, in which
492 * case @load_addr and @load_size are assigned accordingly
493 * * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd device path
494 * * %EFI_OUT_OF_RESOURCES if memory allocation failed
495 * * %EFI_LOAD_ERROR in all other cases
498 efi_status_t efi_load_initrd_dev_path(struct linux_efi_initrd *initrd,
501 efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
502 efi_device_path_protocol_t *dp;
503 efi_load_file2_protocol_t *lf2;
507 dp = (efi_device_path_protocol_t *)&initrd_dev_path;
508 status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
509 if (status != EFI_SUCCESS)
512 status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
514 if (status != EFI_SUCCESS)
518 status = efi_call_proto(lf2, load_file, dp, false, &initrd->size, NULL);
519 if (status != EFI_BUFFER_TOO_SMALL)
520 return EFI_LOAD_ERROR;
522 status = efi_allocate_pages(initrd->size, &initrd->base, max);
523 if (status != EFI_SUCCESS)
526 status = efi_call_proto(lf2, load_file, dp, false, &initrd->size,
527 (void *)initrd->base);
528 if (status != EFI_SUCCESS) {
529 efi_free(initrd->size, initrd->base);
530 return EFI_LOAD_ERROR;
536 efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
537 struct linux_efi_initrd *initrd,
538 unsigned long soft_limit,
539 unsigned long hard_limit)
542 return EFI_UNSUPPORTED;
544 return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
545 soft_limit, hard_limit,
546 &initrd->base, &initrd->size);
550 * efi_load_initrd() - Load initial RAM disk
551 * @image: EFI loaded image protocol
552 * @soft_limit: preferred address for loading the initrd
553 * @hard_limit: upper limit address for loading the initrd
555 * Return: status code
557 efi_status_t efi_load_initrd(efi_loaded_image_t *image,
558 unsigned long soft_limit,
559 unsigned long hard_limit,
560 const struct linux_efi_initrd **out)
562 efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID;
563 efi_status_t status = EFI_SUCCESS;
564 struct linux_efi_initrd initrd, *tbl;
566 if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD) || efi_noinitrd)
569 status = efi_load_initrd_dev_path(&initrd, hard_limit);
570 if (status == EFI_SUCCESS) {
571 efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
572 if (initrd.size > 0 &&
573 efi_measure_tagged_event(initrd.base, initrd.size,
574 EFISTUB_EVT_INITRD) == EFI_SUCCESS)
575 efi_info("Measured initrd data into PCR 9\n");
576 } else if (status == EFI_NOT_FOUND) {
577 status = efi_load_initrd_cmdline(image, &initrd, soft_limit,
579 /* command line loader disabled or no initrd= passed? */
580 if (status == EFI_UNSUPPORTED || status == EFI_NOT_READY)
582 if (status == EFI_SUCCESS)
583 efi_info("Loaded initrd from command line option\n");
585 if (status != EFI_SUCCESS)
588 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(initrd),
590 if (status != EFI_SUCCESS)
594 status = efi_bs_call(install_configuration_table, &tbl_guid, tbl);
595 if (status != EFI_SUCCESS)
603 efi_bs_call(free_pool, tbl);
605 efi_free(initrd.size, initrd.base);
607 efi_err("Failed to load initrd: 0x%lx\n", status);
612 * efi_wait_for_key() - Wait for key stroke
613 * @usec: number of microseconds to wait for key stroke
616 * Wait for up to @usec microseconds for a key stroke.
618 * Return: status code, EFI_SUCCESS if key received
620 efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
622 efi_event_t events[2], timer;
624 efi_simple_text_input_protocol_t *con_in;
627 con_in = efi_table_attr(efi_system_table, con_in);
629 return EFI_UNSUPPORTED;
630 efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
632 status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
633 if (status != EFI_SUCCESS)
636 status = efi_bs_call(set_timer, timer, EfiTimerRelative,
637 EFI_100NSEC_PER_USEC * usec);
638 if (status != EFI_SUCCESS)
640 efi_set_event_at(events, 1, timer);
642 status = efi_bs_call(wait_for_event, 2, events, &index);
643 if (status == EFI_SUCCESS) {
645 status = efi_call_proto(con_in, read_keystroke, key);
647 status = EFI_TIMEOUT;
650 efi_bs_call(close_event, timer);
656 * efi_remap_image - Remap a loaded image with the appropriate permissions
659 * @image_base: the base of the image in memory
660 * @alloc_size: the size of the area in memory occupied by the image
661 * @code_size: the size of the leading part of the image containing code
664 * efi_remap_image() uses the EFI memory attribute protocol to remap the code
665 * region of the loaded image read-only/executable, and the remainder
666 * read-write/non-executable. The code region is assumed to start at the base
667 * of the image, and will therefore cover the PE/COFF header as well.
669 void efi_remap_image(unsigned long image_base, unsigned alloc_size,
670 unsigned long code_size)
672 efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
673 efi_memory_attribute_protocol_t *memattr;
678 * If the firmware implements the EFI_MEMORY_ATTRIBUTE_PROTOCOL, let's
679 * invoke it to remap the text/rodata region of the decompressed image
680 * as read-only and the data/bss region as non-executable.
682 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
683 if (status != EFI_SUCCESS)
686 // Get the current attributes for the entire region
687 status = memattr->get_memory_attributes(memattr, image_base,
689 if (status != EFI_SUCCESS) {
690 efi_warn("Failed to retrieve memory attributes for image region: 0x%lx\n",
695 // Mark the code region as read-only
696 status = memattr->set_memory_attributes(memattr, image_base, code_size,
698 if (status != EFI_SUCCESS) {
699 efi_warn("Failed to remap code region read-only\n");
703 // If the entire region was already mapped as non-exec, clear the
704 // attribute from the code region. Otherwise, set it on the data
706 if (attr & EFI_MEMORY_XP) {
707 status = memattr->clear_memory_attributes(memattr, image_base,
710 if (status != EFI_SUCCESS)
711 efi_warn("Failed to remap code region executable\n");
713 status = memattr->set_memory_attributes(memattr,
714 image_base + code_size,
715 alloc_size - code_size,
717 if (status != EFI_SUCCESS)
718 efi_warn("Failed to remap data region non-executable\n");