14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0 2f4f75ad5SArd Biesheuvel /* 3f4f75ad5SArd Biesheuvel * Helper functions used by the EFI stub on multiple 4f4f75ad5SArd Biesheuvel * architectures. This should be #included by the EFI stub 5f4f75ad5SArd Biesheuvel * implementation files. 6f4f75ad5SArd Biesheuvel * 7f4f75ad5SArd Biesheuvel * Copyright 2011 Intel Corporation; author Matt Fleming 8f4f75ad5SArd Biesheuvel */ 9f4f75ad5SArd Biesheuvel 10f4f75ad5SArd Biesheuvel #include <linux/efi.h> 11f4f75ad5SArd Biesheuvel #include <asm/efi.h> 12f4f75ad5SArd Biesheuvel 13f4f75ad5SArd Biesheuvel #include "efistub.h" 14f4f75ad5SArd Biesheuvel 155193a33dSArd Biesheuvel static bool __efistub_global efi_nochunk; 167d4e323dSArd Biesheuvel static bool __efistub_global efi_nokaslr; 177d4e323dSArd Biesheuvel static bool __efistub_global efi_quiet; 187d4e323dSArd Biesheuvel static bool __efistub_global efi_novamap; 197d4e323dSArd Biesheuvel static bool __efistub_global efi_nosoftreserve; 204444f854SMatthew Garrett static bool __efistub_global efi_disable_pci_dma = 214444f854SMatthew Garrett IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA); 2260f38de7SArd Biesheuvel 235193a33dSArd Biesheuvel bool __pure nochunk(void) 245193a33dSArd Biesheuvel { 255193a33dSArd Biesheuvel return efi_nochunk; 265193a33dSArd Biesheuvel } 277d4e323dSArd Biesheuvel bool __pure nokaslr(void) 2860f38de7SArd Biesheuvel { 297d4e323dSArd Biesheuvel return efi_nokaslr; 3060f38de7SArd Biesheuvel } 317d4e323dSArd Biesheuvel bool __pure is_quiet(void) 32eeff7d63SArd Biesheuvel { 337d4e323dSArd Biesheuvel return efi_quiet; 34eeff7d63SArd Biesheuvel } 357d4e323dSArd Biesheuvel bool __pure novamap(void) 364e46c2a9SArd Biesheuvel { 377d4e323dSArd Biesheuvel return efi_novamap; 384e46c2a9SArd Biesheuvel } 39b617c526SDan Williams bool __pure __efi_soft_reserve_enabled(void) 40b617c526SDan Williams { 41b617c526SDan Williams return !efi_nosoftreserve; 42b617c526SDan Williams } 4360f38de7SArd Biesheuvel 448173ec79SArd Biesheuvel void efi_printk(char *str) 45f4f75ad5SArd Biesheuvel { 46f4f75ad5SArd Biesheuvel char *s8; 47f4f75ad5SArd Biesheuvel 48f4f75ad5SArd Biesheuvel for (s8 = str; *s8; s8++) { 49f4f75ad5SArd Biesheuvel efi_char16_t ch[2] = { 0 }; 50f4f75ad5SArd Biesheuvel 51f4f75ad5SArd Biesheuvel ch[0] = *s8; 52f4f75ad5SArd Biesheuvel if (*s8 == '\n') { 53f4f75ad5SArd Biesheuvel efi_char16_t nl[2] = { '\r', 0 }; 548173ec79SArd Biesheuvel efi_char16_printk(nl); 55f4f75ad5SArd Biesheuvel } 56f4f75ad5SArd Biesheuvel 578173ec79SArd Biesheuvel efi_char16_printk(ch); 58f4f75ad5SArd Biesheuvel } 59f4f75ad5SArd Biesheuvel } 60f4f75ad5SArd Biesheuvel 615a17dae4SMatt Fleming /* 625a17dae4SMatt Fleming * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi= 635a17dae4SMatt Fleming * option, e.g. efi=nochunk. 645a17dae4SMatt Fleming * 655a17dae4SMatt Fleming * It should be noted that efi= is parsed in two very different 665a17dae4SMatt Fleming * environments, first in the early boot environment of the EFI boot 675a17dae4SMatt Fleming * stub, and subsequently during the kernel boot. 685a17dae4SMatt Fleming */ 6960f38de7SArd Biesheuvel efi_status_t efi_parse_options(char const *cmdline) 705a17dae4SMatt Fleming { 7191d150c0SArd Biesheuvel size_t len = strlen(cmdline) + 1; 7291d150c0SArd Biesheuvel efi_status_t status; 7391d150c0SArd Biesheuvel char *str, *buf; 745a17dae4SMatt Fleming 7591d150c0SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf); 7691d150c0SArd Biesheuvel if (status != EFI_SUCCESS) 7791d150c0SArd Biesheuvel return status; 7891d150c0SArd Biesheuvel 7991d150c0SArd Biesheuvel str = skip_spaces(memcpy(buf, cmdline, len)); 8091d150c0SArd Biesheuvel 8191d150c0SArd Biesheuvel while (*str) { 8291d150c0SArd Biesheuvel char *param, *val; 8391d150c0SArd Biesheuvel 8491d150c0SArd Biesheuvel str = next_arg(str, ¶m, &val); 8591d150c0SArd Biesheuvel 8691d150c0SArd Biesheuvel if (!strcmp(param, "nokaslr")) { 877d4e323dSArd Biesheuvel efi_nokaslr = true; 8891d150c0SArd Biesheuvel } else if (!strcmp(param, "quiet")) { 897d4e323dSArd Biesheuvel efi_quiet = true; 9091d150c0SArd Biesheuvel } else if (!strcmp(param, "efi") && val) { 9191d150c0SArd Biesheuvel efi_nochunk = parse_option_str(val, "nochunk"); 9291d150c0SArd Biesheuvel efi_novamap = parse_option_str(val, "novamap"); 93eeff7d63SArd Biesheuvel 9491d150c0SArd Biesheuvel efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) && 9591d150c0SArd Biesheuvel parse_option_str(val, "nosoftreserve"); 965a17dae4SMatt Fleming 9791d150c0SArd Biesheuvel if (parse_option_str(val, "disable_early_pci_dma")) 984444f854SMatthew Garrett efi_disable_pci_dma = true; 9991d150c0SArd Biesheuvel if (parse_option_str(val, "no_disable_early_pci_dma")) 1004444f854SMatthew Garrett efi_disable_pci_dma = false; 1014444f854SMatthew Garrett } 1025a17dae4SMatt Fleming } 10391d150c0SArd Biesheuvel efi_bs_call(free_pool, buf); 1045a17dae4SMatt Fleming return EFI_SUCCESS; 1055a17dae4SMatt Fleming } 106f4f75ad5SArd Biesheuvel 107f4f75ad5SArd Biesheuvel /* 108f4f75ad5SArd Biesheuvel * Get the number of UTF-8 bytes corresponding to an UTF-16 character. 109f4f75ad5SArd Biesheuvel * This overestimates for surrogates, but that is okay. 110f4f75ad5SArd Biesheuvel */ 111f4f75ad5SArd Biesheuvel static int efi_utf8_bytes(u16 c) 112f4f75ad5SArd Biesheuvel { 113f4f75ad5SArd Biesheuvel return 1 + (c >= 0x80) + (c >= 0x800); 114f4f75ad5SArd Biesheuvel } 115f4f75ad5SArd Biesheuvel 116f4f75ad5SArd Biesheuvel /* 117f4f75ad5SArd Biesheuvel * Convert an UTF-16 string, not necessarily null terminated, to UTF-8. 118f4f75ad5SArd Biesheuvel */ 119f4f75ad5SArd Biesheuvel static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n) 120f4f75ad5SArd Biesheuvel { 121f4f75ad5SArd Biesheuvel unsigned int c; 122f4f75ad5SArd Biesheuvel 123f4f75ad5SArd Biesheuvel while (n--) { 124f4f75ad5SArd Biesheuvel c = *src++; 125f4f75ad5SArd Biesheuvel if (n && c >= 0xd800 && c <= 0xdbff && 126f4f75ad5SArd Biesheuvel *src >= 0xdc00 && *src <= 0xdfff) { 127f4f75ad5SArd Biesheuvel c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff); 128f4f75ad5SArd Biesheuvel src++; 129f4f75ad5SArd Biesheuvel n--; 130f4f75ad5SArd Biesheuvel } 131f4f75ad5SArd Biesheuvel if (c >= 0xd800 && c <= 0xdfff) 132f4f75ad5SArd Biesheuvel c = 0xfffd; /* Unmatched surrogate */ 133f4f75ad5SArd Biesheuvel if (c < 0x80) { 134f4f75ad5SArd Biesheuvel *dst++ = c; 135f4f75ad5SArd Biesheuvel continue; 136f4f75ad5SArd Biesheuvel } 137f4f75ad5SArd Biesheuvel if (c < 0x800) { 138f4f75ad5SArd Biesheuvel *dst++ = 0xc0 + (c >> 6); 139f4f75ad5SArd Biesheuvel goto t1; 140f4f75ad5SArd Biesheuvel } 141f4f75ad5SArd Biesheuvel if (c < 0x10000) { 142f4f75ad5SArd Biesheuvel *dst++ = 0xe0 + (c >> 12); 143f4f75ad5SArd Biesheuvel goto t2; 144f4f75ad5SArd Biesheuvel } 145f4f75ad5SArd Biesheuvel *dst++ = 0xf0 + (c >> 18); 146f4f75ad5SArd Biesheuvel *dst++ = 0x80 + ((c >> 12) & 0x3f); 147f4f75ad5SArd Biesheuvel t2: 148f4f75ad5SArd Biesheuvel *dst++ = 0x80 + ((c >> 6) & 0x3f); 149f4f75ad5SArd Biesheuvel t1: 150f4f75ad5SArd Biesheuvel *dst++ = 0x80 + (c & 0x3f); 151f4f75ad5SArd Biesheuvel } 152f4f75ad5SArd Biesheuvel 153f4f75ad5SArd Biesheuvel return dst; 154f4f75ad5SArd Biesheuvel } 155f4f75ad5SArd Biesheuvel 156f4f75ad5SArd Biesheuvel /* 157f4f75ad5SArd Biesheuvel * Convert the unicode UEFI command line to ASCII to pass to kernel. 158f4f75ad5SArd Biesheuvel * Size of memory allocated return in *cmd_line_len. 159f4f75ad5SArd Biesheuvel * Returns NULL on error. 160f4f75ad5SArd Biesheuvel */ 161cd33a5c1SArd Biesheuvel char *efi_convert_cmdline(efi_loaded_image_t *image, 1621e45bf73SArd Biesheuvel int *cmd_line_len, unsigned long max_addr) 163f4f75ad5SArd Biesheuvel { 164f4f75ad5SArd Biesheuvel const u16 *s2; 165f4f75ad5SArd Biesheuvel u8 *s1 = NULL; 166f4f75ad5SArd Biesheuvel unsigned long cmdline_addr = 0; 167f4f75ad5SArd Biesheuvel int load_options_chars = image->load_options_size / 2; /* UTF-16 */ 168f4f75ad5SArd Biesheuvel const u16 *options = image->load_options; 169f4f75ad5SArd Biesheuvel int options_bytes = 0; /* UTF-8 bytes */ 170f4f75ad5SArd Biesheuvel int options_chars = 0; /* UTF-16 chars */ 171f4f75ad5SArd Biesheuvel efi_status_t status; 172f4f75ad5SArd Biesheuvel u16 zero = 0; 173f4f75ad5SArd Biesheuvel 174f4f75ad5SArd Biesheuvel if (options) { 175f4f75ad5SArd Biesheuvel s2 = options; 176f4f75ad5SArd Biesheuvel while (*s2 && *s2 != '\n' 177f4f75ad5SArd Biesheuvel && options_chars < load_options_chars) { 178f4f75ad5SArd Biesheuvel options_bytes += efi_utf8_bytes(*s2++); 179f4f75ad5SArd Biesheuvel options_chars++; 180f4f75ad5SArd Biesheuvel } 181f4f75ad5SArd Biesheuvel } 182f4f75ad5SArd Biesheuvel 183f4f75ad5SArd Biesheuvel if (!options_chars) { 184f4f75ad5SArd Biesheuvel /* No command line options, so return empty string*/ 185f4f75ad5SArd Biesheuvel options = &zero; 186f4f75ad5SArd Biesheuvel } 187f4f75ad5SArd Biesheuvel 188f4f75ad5SArd Biesheuvel options_bytes++; /* NUL termination */ 189f4f75ad5SArd Biesheuvel 1901e45bf73SArd Biesheuvel status = efi_allocate_pages(options_bytes, &cmdline_addr, max_addr); 191f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) 192f4f75ad5SArd Biesheuvel return NULL; 193f4f75ad5SArd Biesheuvel 194f4f75ad5SArd Biesheuvel s1 = (u8 *)cmdline_addr; 195f4f75ad5SArd Biesheuvel s2 = (const u16 *)options; 196f4f75ad5SArd Biesheuvel 197f4f75ad5SArd Biesheuvel s1 = efi_utf16_to_utf8(s1, s2, options_chars); 198f4f75ad5SArd Biesheuvel *s1 = '\0'; 199f4f75ad5SArd Biesheuvel 200f4f75ad5SArd Biesheuvel *cmd_line_len = options_bytes; 201f4f75ad5SArd Biesheuvel return (char *)cmdline_addr; 202f4f75ad5SArd Biesheuvel } 203fc07716bSJeffrey Hugo 204fc07716bSJeffrey Hugo /* 205fc07716bSJeffrey Hugo * Handle calling ExitBootServices according to the requirements set out by the 206fc07716bSJeffrey Hugo * spec. Obtains the current memory map, and returns that info after calling 207fc07716bSJeffrey Hugo * ExitBootServices. The client must specify a function to perform any 208fc07716bSJeffrey Hugo * processing of the memory map data prior to ExitBootServices. A client 209fc07716bSJeffrey Hugo * specific structure may be passed to the function via priv. The client 210fc07716bSJeffrey Hugo * function may be called multiple times. 211fc07716bSJeffrey Hugo */ 212cd33a5c1SArd Biesheuvel efi_status_t efi_exit_boot_services(void *handle, 213fc07716bSJeffrey Hugo struct efi_boot_memmap *map, 214fc07716bSJeffrey Hugo void *priv, 215fc07716bSJeffrey Hugo efi_exit_boot_map_processing priv_func) 216fc07716bSJeffrey Hugo { 217fc07716bSJeffrey Hugo efi_status_t status; 218fc07716bSJeffrey Hugo 219cd33a5c1SArd Biesheuvel status = efi_get_memory_map(map); 220fc07716bSJeffrey Hugo 221fc07716bSJeffrey Hugo if (status != EFI_SUCCESS) 222fc07716bSJeffrey Hugo goto fail; 223fc07716bSJeffrey Hugo 224cd33a5c1SArd Biesheuvel status = priv_func(map, priv); 225fc07716bSJeffrey Hugo if (status != EFI_SUCCESS) 226fc07716bSJeffrey Hugo goto free_map; 227fc07716bSJeffrey Hugo 2284444f854SMatthew Garrett if (efi_disable_pci_dma) 2294444f854SMatthew Garrett efi_pci_disable_bridge_busmaster(); 2304444f854SMatthew Garrett 231966291f6SArd Biesheuvel status = efi_bs_call(exit_boot_services, handle, *map->key_ptr); 232fc07716bSJeffrey Hugo 233fc07716bSJeffrey Hugo if (status == EFI_INVALID_PARAMETER) { 234fc07716bSJeffrey Hugo /* 235fc07716bSJeffrey Hugo * The memory map changed between efi_get_memory_map() and 236fc07716bSJeffrey Hugo * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4: 237fc07716bSJeffrey Hugo * EFI_BOOT_SERVICES.ExitBootServices we need to get the 238fc07716bSJeffrey Hugo * updated map, and try again. The spec implies one retry 239fc07716bSJeffrey Hugo * should be sufficent, which is confirmed against the EDK2 240fc07716bSJeffrey Hugo * implementation. Per the spec, we can only invoke 241fc07716bSJeffrey Hugo * get_memory_map() and exit_boot_services() - we cannot alloc 242fc07716bSJeffrey Hugo * so efi_get_memory_map() cannot be used, and we must reuse 243fc07716bSJeffrey Hugo * the buffer. For all practical purposes, the headroom in the 244fc07716bSJeffrey Hugo * buffer should account for any changes in the map so the call 245fc07716bSJeffrey Hugo * to get_memory_map() is expected to succeed here. 246fc07716bSJeffrey Hugo */ 247fc07716bSJeffrey Hugo *map->map_size = *map->buff_size; 248966291f6SArd Biesheuvel status = efi_bs_call(get_memory_map, 249fc07716bSJeffrey Hugo map->map_size, 250fc07716bSJeffrey Hugo *map->map, 251fc07716bSJeffrey Hugo map->key_ptr, 252fc07716bSJeffrey Hugo map->desc_size, 253fc07716bSJeffrey Hugo map->desc_ver); 254fc07716bSJeffrey Hugo 255fc07716bSJeffrey Hugo /* exit_boot_services() was called, thus cannot free */ 256fc07716bSJeffrey Hugo if (status != EFI_SUCCESS) 257fc07716bSJeffrey Hugo goto fail; 258fc07716bSJeffrey Hugo 259cd33a5c1SArd Biesheuvel status = priv_func(map, priv); 260fc07716bSJeffrey Hugo /* exit_boot_services() was called, thus cannot free */ 261fc07716bSJeffrey Hugo if (status != EFI_SUCCESS) 262fc07716bSJeffrey Hugo goto fail; 263fc07716bSJeffrey Hugo 264966291f6SArd Biesheuvel status = efi_bs_call(exit_boot_services, handle, *map->key_ptr); 265fc07716bSJeffrey Hugo } 266fc07716bSJeffrey Hugo 267fc07716bSJeffrey Hugo /* exit_boot_services() was called, thus cannot free */ 268fc07716bSJeffrey Hugo if (status != EFI_SUCCESS) 269fc07716bSJeffrey Hugo goto fail; 270fc07716bSJeffrey Hugo 271fc07716bSJeffrey Hugo return EFI_SUCCESS; 272fc07716bSJeffrey Hugo 273fc07716bSJeffrey Hugo free_map: 274966291f6SArd Biesheuvel efi_bs_call(free_pool, *map->map); 275fc07716bSJeffrey Hugo fail: 276fc07716bSJeffrey Hugo return status; 277fc07716bSJeffrey Hugo } 27882d736acSMatthew Garrett 279cd33a5c1SArd Biesheuvel void *get_efi_config_table(efi_guid_t guid) 28082d736acSMatthew Garrett { 28199ea8b1dSArd Biesheuvel unsigned long tables = efi_table_attr(efi_system_table(), tables); 28299ea8b1dSArd Biesheuvel int nr_tables = efi_table_attr(efi_system_table(), nr_tables); 283f958efe9SArd Biesheuvel int i; 284f958efe9SArd Biesheuvel 285f958efe9SArd Biesheuvel for (i = 0; i < nr_tables; i++) { 286f958efe9SArd Biesheuvel efi_config_table_t *t = (void *)tables; 287f958efe9SArd Biesheuvel 288f958efe9SArd Biesheuvel if (efi_guidcmp(t->guid, guid) == 0) 28999ea8b1dSArd Biesheuvel return efi_table_attr(t, table); 290f958efe9SArd Biesheuvel 291f958efe9SArd Biesheuvel tables += efi_is_native() ? sizeof(efi_config_table_t) 292f958efe9SArd Biesheuvel : sizeof(efi_config_table_32_t); 293f958efe9SArd Biesheuvel } 294f958efe9SArd Biesheuvel return NULL; 29582d736acSMatthew Garrett } 296dc29da14SArd Biesheuvel 2978173ec79SArd Biesheuvel void efi_char16_printk(efi_char16_t *str) 298dc29da14SArd Biesheuvel { 29999ea8b1dSArd Biesheuvel efi_call_proto(efi_table_attr(efi_system_table(), con_out), 30047c0fd39SArd Biesheuvel output_string, str); 301dc29da14SArd Biesheuvel } 302