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>
14 #include <linux/overflow.h>
16 #include <asm/setup.h>
21 bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE);
24 static bool efi_noinitrd;
25 static bool efi_nosoftreserve;
26 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_parse_options() - Parse EFI command line options
37 * @cmdline: kernel command line
39 * Parse the ASCII string @cmdline for EFI options, denoted by the efi=
40 * option, e.g. efi=nochunk.
42 * It should be noted that efi= is parsed in two very different
43 * environments, first in the early boot environment of the EFI boot
44 * stub, and subsequently during the kernel boot.
48 efi_status_t efi_parse_options(char const *cmdline)
57 len = strnlen(cmdline, COMMAND_LINE_SIZE - 1) + 1;
58 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
59 if (status != EFI_SUCCESS)
62 memcpy(buf, cmdline, len - 1);
64 str = skip_spaces(buf);
69 str = next_arg(str, ¶m, &val);
70 if (!val && !strcmp(param, "--"))
73 if (!strcmp(param, "nokaslr")) {
75 } else if (!strcmp(param, "quiet")) {
76 efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
77 } else if (!strcmp(param, "noinitrd")) {
79 } else if (IS_ENABLED(CONFIG_X86_64) && !strcmp(param, "no5lvl")) {
81 } else if (IS_ENABLED(CONFIG_ARCH_HAS_MEM_ENCRYPT) &&
82 !strcmp(param, "mem_encrypt") && val) {
83 if (parse_option_str(val, "on"))
85 else if (parse_option_str(val, "off"))
87 } else if (!strcmp(param, "efi") && val) {
88 efi_nochunk = parse_option_str(val, "nochunk");
89 efi_novamap |= parse_option_str(val, "novamap");
91 efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
92 parse_option_str(val, "nosoftreserve");
94 if (parse_option_str(val, "disable_early_pci_dma"))
95 efi_disable_pci_dma = true;
96 if (parse_option_str(val, "no_disable_early_pci_dma"))
97 efi_disable_pci_dma = false;
98 if (parse_option_str(val, "debug"))
99 efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
100 } else if (!strcmp(param, "video") &&
101 val && strstarts(val, "efifb:")) {
102 efi_parse_option_graphics(val + strlen("efifb:"));
105 efi_bs_call(free_pool, buf);
110 * The EFI_LOAD_OPTION descriptor has the following layout:
112 * u16 FilePathListLength;
114 * efi_device_path_protocol_t FilePathList[];
117 * This function validates and unpacks the variable-size data fields.
120 bool efi_load_option_unpack(efi_load_option_unpacked_t *dest,
121 const efi_load_option_t *src, size_t size)
125 efi_device_path_protocol_t header;
126 const efi_char16_t *description;
127 const efi_device_path_protocol_t *file_path_list;
129 if (size < offsetof(efi_load_option_t, variable_data))
131 pos = src->variable_data;
132 size -= offsetof(efi_load_option_t, variable_data);
134 if ((src->attributes & ~EFI_LOAD_OPTION_MASK) != 0)
137 /* Scan description. */
140 if (size < sizeof(c))
142 c = *(const u16 *)pos;
145 } while (c != L'\0');
147 /* Scan file_path_list. */
148 file_path_list = pos;
150 if (size < sizeof(header))
152 header = *(const efi_device_path_protocol_t *)pos;
153 if (header.length < sizeof(header))
155 if (size < header.length)
157 pos += header.length;
158 size -= header.length;
159 } while ((header.type != EFI_DEV_END_PATH && header.type != EFI_DEV_END_PATH2) ||
160 (header.sub_type != EFI_DEV_END_ENTIRE));
161 if (pos != (const void *)file_path_list + src->file_path_list_length)
164 dest->attributes = src->attributes;
165 dest->file_path_list_length = src->file_path_list_length;
166 dest->description = description;
167 dest->file_path_list = file_path_list;
168 dest->optional_data_size = size;
169 dest->optional_data = size ? pos : NULL;
175 * At least some versions of Dell firmware pass the entire contents of the
176 * Boot#### variable, i.e. the EFI_LOAD_OPTION descriptor, rather than just the
177 * OptionalData field.
179 * Detect this case and extract OptionalData.
181 void efi_apply_loadoptions_quirk(const void **load_options, u32 *load_options_size)
183 const efi_load_option_t *load_option = *load_options;
184 efi_load_option_unpacked_t load_option_unpacked;
186 if (!IS_ENABLED(CONFIG_X86))
190 if (*load_options_size < sizeof(*load_option))
192 if ((load_option->attributes & ~EFI_LOAD_OPTION_BOOT_MASK) != 0)
195 if (!efi_load_option_unpack(&load_option_unpacked, load_option, *load_options_size))
198 efi_warn_once(FW_BUG "LoadOptions is an EFI_LOAD_OPTION descriptor\n");
199 efi_warn_once(FW_BUG "Using OptionalData as a workaround\n");
201 *load_options = load_option_unpacked.optional_data;
202 *load_options_size = load_option_unpacked.optional_data_size;
205 enum efistub_event_type {
207 EFISTUB_EVT_LOAD_OPTIONS,
211 #define STR_WITH_SIZE(s) sizeof(s), s
213 static const struct {
219 [EFISTUB_EVT_INITRD] = {
222 STR_WITH_SIZE("Linux initrd")
224 [EFISTUB_EVT_LOAD_OPTIONS] = {
226 LOAD_OPTIONS_EVENT_TAG_ID,
227 STR_WITH_SIZE("LOADED_IMAGE::LoadOptions")
231 static_assert(sizeof(efi_tcg2_event_t) == sizeof(efi_cc_event_t));
233 union efistub_event {
234 efi_tcg2_event_t tcg2_data;
235 efi_cc_event_t cc_data;
238 struct efistub_measured_event {
239 union efistub_event event_data;
240 TCG_PCClientTaggedEvent tagged_event __packed;
243 static efi_status_t efi_measure_tagged_event(unsigned long load_addr,
244 unsigned long load_size,
245 enum efistub_event_type event)
249 (__efiapi *hash_log_extend_event)(void *, u64, efi_physical_addr_t,
250 u64, const union efistub_event *);
251 struct { u32 hash_log_extend_event; } mixed_mode;
253 struct efistub_measured_event *evt;
254 int size = struct_size(evt, tagged_event.tagged_event_data,
255 events[event].event_data_len);
256 efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
257 efi_tcg2_protocol_t *tcg2 = NULL;
258 union efistub_event ev;
262 efi_bs_call(locate_protocol, &tcg2_guid, NULL, (void **)&tcg2);
264 ev.tcg2_data = (struct efi_tcg2_event){
266 .event_header.header_size = sizeof(ev.tcg2_data.event_header),
267 .event_header.header_version = EFI_TCG2_EVENT_HEADER_VERSION,
268 .event_header.pcr_index = events[event].pcr_index,
269 .event_header.event_type = EV_EVENT_TAG,
272 method.hash_log_extend_event =
273 (void *)efi_table_attr(tcg2, hash_log_extend_event);
275 efi_guid_t cc_guid = EFI_CC_MEASUREMENT_PROTOCOL_GUID;
276 efi_cc_protocol_t *cc = NULL;
278 efi_bs_call(locate_protocol, &cc_guid, NULL, (void **)&cc);
280 return EFI_UNSUPPORTED;
282 ev.cc_data = (struct efi_cc_event){
284 .event_header.header_size = sizeof(ev.cc_data.event_header),
285 .event_header.header_version = EFI_CC_EVENT_HEADER_VERSION,
286 .event_header.event_type = EV_EVENT_TAG,
289 status = efi_call_proto(cc, map_pcr_to_mr_index,
290 events[event].pcr_index,
291 &ev.cc_data.event_header.mr_index);
292 if (status != EFI_SUCCESS)
296 method.hash_log_extend_event =
297 (void *)efi_table_attr(cc, hash_log_extend_event);
300 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, (void **)&evt);
301 if (status != EFI_SUCCESS)
304 *evt = (struct efistub_measured_event) {
306 .tagged_event.tagged_event_id = events[event].event_id,
307 .tagged_event.tagged_event_data_size = events[event].event_data_len,
310 memcpy(evt->tagged_event.tagged_event_data, events[event].event_data,
311 events[event].event_data_len);
313 status = efi_fn_call(&method, hash_log_extend_event, protocol, 0,
314 load_addr, load_size, &evt->event_data);
315 efi_bs_call(free_pool, evt);
317 if (status == EFI_SUCCESS)
321 efi_warn("Failed to measure data for event %d: 0x%lx\n", event, status);
326 * Convert the unicode UEFI command line to ASCII to pass to kernel.
327 * Size of memory allocated return in *cmd_line_len.
328 * Returns NULL on error.
330 char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len)
332 const efi_char16_t *options = efi_table_attr(image, load_options);
333 u32 options_size = efi_table_attr(image, load_options_size);
334 int options_bytes = 0, safe_options_bytes = 0; /* UTF-8 bytes */
335 unsigned long cmdline_addr = 0;
336 const efi_char16_t *s2;
337 bool in_quote = false;
341 if (options_size > 0)
342 efi_measure_tagged_event((unsigned long)options, options_size,
343 EFISTUB_EVT_LOAD_OPTIONS);
345 efi_apply_loadoptions_quirk((const void **)&options, &options_size);
346 options_chars = options_size / sizeof(efi_char16_t);
350 while (options_bytes < COMMAND_LINE_SIZE && options_chars--) {
351 efi_char16_t c = *s2++;
354 if (c == L'\0' || c == L'\n')
357 in_quote = !in_quote;
358 else if (!in_quote && isspace((char)c))
359 safe_options_bytes = options_bytes;
366 * Get the number of UTF-8 bytes corresponding to a
368 * The first part handles everything in the BMP.
370 options_bytes += 2 + (c >= 0x800);
372 * Add one more byte for valid surrogate pairs. Invalid
373 * surrogates will be replaced with 0xfffd and take up
376 if ((c & 0xfc00) == 0xd800) {
378 * If the very last word is a high surrogate,
379 * we must ignore it since we can't access the
382 if (!options_chars) {
384 } else if ((*s2 & 0xfc00) == 0xdc00) {
391 if (options_bytes >= COMMAND_LINE_SIZE) {
392 options_bytes = safe_options_bytes;
393 efi_err("Command line is too long: truncated to %d bytes\n",
398 options_bytes++; /* NUL termination */
400 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, options_bytes,
401 (void **)&cmdline_addr);
402 if (status != EFI_SUCCESS)
405 snprintf((char *)cmdline_addr, options_bytes, "%.*ls",
406 options_bytes - 1, options);
408 *cmd_line_len = options_bytes;
409 return (char *)cmdline_addr;
413 * efi_exit_boot_services() - Exit boot services
414 * @handle: handle of the exiting image
415 * @priv: argument to be passed to @priv_func
416 * @priv_func: function to process the memory map before exiting boot services
418 * Handle calling ExitBootServices according to the requirements set out by the
419 * spec. Obtains the current memory map, and returns that info after calling
420 * ExitBootServices. The client must specify a function to perform any
421 * processing of the memory map data prior to ExitBootServices. A client
422 * specific structure may be passed to the function via priv. The client
423 * function may be called multiple times.
425 * Return: status code
427 efi_status_t efi_exit_boot_services(void *handle, void *priv,
428 efi_exit_boot_map_processing priv_func)
430 struct efi_boot_memmap *map;
433 if (efi_disable_pci_dma)
434 efi_pci_disable_bridge_busmaster();
436 status = efi_get_memory_map(&map, true);
437 if (status != EFI_SUCCESS)
440 status = priv_func(map, priv);
441 if (status != EFI_SUCCESS) {
442 efi_bs_call(free_pool, map);
446 status = efi_bs_call(exit_boot_services, handle, map->map_key);
448 if (status == EFI_INVALID_PARAMETER) {
450 * The memory map changed between efi_get_memory_map() and
451 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
452 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
453 * updated map, and try again. The spec implies one retry
454 * should be sufficent, which is confirmed against the EDK2
455 * implementation. Per the spec, we can only invoke
456 * get_memory_map() and exit_boot_services() - we cannot alloc
457 * so efi_get_memory_map() cannot be used, and we must reuse
458 * the buffer. For all practical purposes, the headroom in the
459 * buffer should account for any changes in the map so the call
460 * to get_memory_map() is expected to succeed here.
462 map->map_size = map->buff_size;
463 status = efi_bs_call(get_memory_map,
470 /* exit_boot_services() was called, thus cannot free */
471 if (status != EFI_SUCCESS)
474 status = priv_func(map, priv);
475 /* exit_boot_services() was called, thus cannot free */
476 if (status != EFI_SUCCESS)
479 status = efi_bs_call(exit_boot_services, handle, map->map_key);
486 * get_efi_config_table() - retrieve UEFI configuration table
487 * @guid: GUID of the configuration table to be retrieved
488 * Return: pointer to the configuration table or NULL
490 void *get_efi_config_table(efi_guid_t guid)
492 unsigned long tables = efi_table_attr(efi_system_table, tables);
493 int nr_tables = efi_table_attr(efi_system_table, nr_tables);
496 for (i = 0; i < nr_tables; i++) {
497 efi_config_table_t *t = (void *)tables;
499 if (efi_guidcmp(t->guid, guid) == 0)
500 return efi_table_attr(t, table);
502 tables += efi_is_native() ? sizeof(efi_config_table_t)
503 : sizeof(efi_config_table_32_t);
509 * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
510 * for the firmware or bootloader to expose the initrd data directly to the stub
511 * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
512 * very easy to implement. It is a simple Linux initrd specific conduit between
513 * kernel and firmware, allowing us to put the EFI stub (being part of the
514 * kernel) in charge of where and when to load the initrd, while leaving it up
515 * to the firmware to decide whether it needs to expose its filesystem hierarchy
518 static const struct {
519 struct efi_vendor_dev_path vendor;
520 struct efi_generic_dev_path end;
521 } __packed initrd_dev_path = {
525 EFI_DEV_MEDIA_VENDOR,
526 sizeof(struct efi_vendor_dev_path),
528 LINUX_EFI_INITRD_MEDIA_GUID
532 sizeof(struct efi_generic_dev_path)
537 * efi_load_initrd_dev_path() - load the initrd from the Linux initrd device path
538 * @initrd: pointer of struct to store the address where the initrd was loaded
539 * and the size of the loaded initrd
540 * @max: upper limit for the initrd memory allocation
543 * * %EFI_SUCCESS if the initrd was loaded successfully, in which
544 * case @load_addr and @load_size are assigned accordingly
545 * * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd device path
546 * * %EFI_OUT_OF_RESOURCES if memory allocation failed
547 * * %EFI_LOAD_ERROR in all other cases
550 efi_status_t efi_load_initrd_dev_path(struct linux_efi_initrd *initrd,
553 efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
554 efi_device_path_protocol_t *dp;
555 efi_load_file2_protocol_t *lf2;
559 dp = (efi_device_path_protocol_t *)&initrd_dev_path;
560 status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
561 if (status != EFI_SUCCESS)
564 status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
566 if (status != EFI_SUCCESS)
570 status = efi_call_proto(lf2, load_file, dp, false, &initrd->size, NULL);
571 if (status != EFI_BUFFER_TOO_SMALL)
572 return EFI_LOAD_ERROR;
574 status = efi_allocate_pages(initrd->size, &initrd->base, max);
575 if (status != EFI_SUCCESS)
578 status = efi_call_proto(lf2, load_file, dp, false, &initrd->size,
579 (void *)initrd->base);
580 if (status != EFI_SUCCESS) {
581 efi_free(initrd->size, initrd->base);
582 return EFI_LOAD_ERROR;
588 efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
589 struct linux_efi_initrd *initrd,
590 unsigned long soft_limit,
591 unsigned long hard_limit)
594 return EFI_UNSUPPORTED;
596 return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
597 soft_limit, hard_limit,
598 &initrd->base, &initrd->size);
602 * efi_load_initrd() - Load initial RAM disk
603 * @image: EFI loaded image protocol
604 * @soft_limit: preferred address for loading the initrd
605 * @hard_limit: upper limit address for loading the initrd
607 * Return: status code
609 efi_status_t efi_load_initrd(efi_loaded_image_t *image,
610 unsigned long soft_limit,
611 unsigned long hard_limit,
612 const struct linux_efi_initrd **out)
614 efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID;
615 efi_status_t status = EFI_SUCCESS;
616 struct linux_efi_initrd initrd, *tbl;
618 if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD) || efi_noinitrd)
621 status = efi_load_initrd_dev_path(&initrd, hard_limit);
622 if (status == EFI_SUCCESS) {
623 efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
624 if (initrd.size > 0 &&
625 efi_measure_tagged_event(initrd.base, initrd.size,
626 EFISTUB_EVT_INITRD) == EFI_SUCCESS)
627 efi_info("Measured initrd data into PCR 9\n");
628 } else if (status == EFI_NOT_FOUND) {
629 status = efi_load_initrd_cmdline(image, &initrd, soft_limit,
631 /* command line loader disabled or no initrd= passed? */
632 if (status == EFI_UNSUPPORTED || status == EFI_NOT_READY)
634 if (status == EFI_SUCCESS)
635 efi_info("Loaded initrd from command line option\n");
637 if (status != EFI_SUCCESS)
640 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(initrd),
642 if (status != EFI_SUCCESS)
646 status = efi_bs_call(install_configuration_table, &tbl_guid, tbl);
647 if (status != EFI_SUCCESS)
655 efi_bs_call(free_pool, tbl);
657 efi_free(initrd.size, initrd.base);
659 efi_err("Failed to load initrd: 0x%lx\n", status);
664 * efi_wait_for_key() - Wait for key stroke
665 * @usec: number of microseconds to wait for key stroke
668 * Wait for up to @usec microseconds for a key stroke.
670 * Return: status code, EFI_SUCCESS if key received
672 efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
674 efi_event_t events[2], timer;
676 efi_simple_text_input_protocol_t *con_in;
679 con_in = efi_table_attr(efi_system_table, con_in);
681 return EFI_UNSUPPORTED;
682 efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
684 status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
685 if (status != EFI_SUCCESS)
688 status = efi_bs_call(set_timer, timer, EfiTimerRelative,
689 EFI_100NSEC_PER_USEC * usec);
690 if (status != EFI_SUCCESS)
692 efi_set_event_at(events, 1, timer);
694 status = efi_bs_call(wait_for_event, 2, events, &index);
695 if (status == EFI_SUCCESS) {
697 status = efi_call_proto(con_in, read_keystroke, key);
699 status = EFI_TIMEOUT;
702 efi_bs_call(close_event, timer);
708 * efi_remap_image - Remap a loaded image with the appropriate permissions
711 * @image_base: the base of the image in memory
712 * @alloc_size: the size of the area in memory occupied by the image
713 * @code_size: the size of the leading part of the image containing code
716 * efi_remap_image() uses the EFI memory attribute protocol to remap the code
717 * region of the loaded image read-only/executable, and the remainder
718 * read-write/non-executable. The code region is assumed to start at the base
719 * of the image, and will therefore cover the PE/COFF header as well.
721 void efi_remap_image(unsigned long image_base, unsigned alloc_size,
722 unsigned long code_size)
724 efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
725 efi_memory_attribute_protocol_t *memattr;
730 * If the firmware implements the EFI_MEMORY_ATTRIBUTE_PROTOCOL, let's
731 * invoke it to remap the text/rodata region of the decompressed image
732 * as read-only and the data/bss region as non-executable.
734 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
735 if (status != EFI_SUCCESS)
738 // Get the current attributes for the entire region
739 status = memattr->get_memory_attributes(memattr, image_base,
741 if (status != EFI_SUCCESS) {
742 efi_warn("Failed to retrieve memory attributes for image region: 0x%lx\n",
747 // Mark the code region as read-only
748 status = memattr->set_memory_attributes(memattr, image_base, code_size,
750 if (status != EFI_SUCCESS) {
751 efi_warn("Failed to remap code region read-only\n");
755 // If the entire region was already mapped as non-exec, clear the
756 // attribute from the code region. Otherwise, set it on the data
758 if (attr & EFI_MEMORY_XP) {
759 status = memattr->clear_memory_attributes(memattr, image_base,
762 if (status != EFI_SUCCESS)
763 efi_warn("Failed to remap code region executable\n");
765 status = memattr->set_memory_attributes(memattr,
766 image_base + code_size,
767 alloc_size - code_size,
769 if (status != EFI_SUCCESS)
770 efi_warn("Failed to remap data region non-executable\n");