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> 11fd0528a2SArvind Sankar #include <linux/kernel.h> 12f4f75ad5SArd Biesheuvel #include <asm/efi.h> 13f4f75ad5SArd Biesheuvel 14f4f75ad5SArd Biesheuvel #include "efistub.h" 15f4f75ad5SArd Biesheuvel 16980771f6SArd Biesheuvel bool efi_nochunk; 17980771f6SArd Biesheuvel bool efi_nokaslr; 18980771f6SArd Biesheuvel bool efi_noinitrd; 19980771f6SArd Biesheuvel bool efi_quiet; 20980771f6SArd Biesheuvel bool efi_novamap; 21980771f6SArd Biesheuvel 2254439370SArvind Sankar static bool efi_nosoftreserve; 2354439370SArvind Sankar static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA); 2460f38de7SArd Biesheuvel 25b617c526SDan Williams bool __pure __efi_soft_reserve_enabled(void) 26b617c526SDan Williams { 27b617c526SDan Williams return !efi_nosoftreserve; 28b617c526SDan Williams } 2960f38de7SArd Biesheuvel 30cb8c90a0SArvind Sankar void efi_char16_puts(efi_char16_t *str) 31cb8c90a0SArvind Sankar { 32cb8c90a0SArvind Sankar efi_call_proto(efi_table_attr(efi_system_table, con_out), 33cb8c90a0SArvind Sankar output_string, str); 34cb8c90a0SArvind Sankar } 35cb8c90a0SArvind Sankar 36cb8c90a0SArvind Sankar void efi_puts(const char *str) 37f4f75ad5SArd Biesheuvel { 38fd0528a2SArvind Sankar efi_char16_t buf[128]; 39fd0528a2SArvind Sankar size_t pos = 0, lim = ARRAY_SIZE(buf); 40f4f75ad5SArd Biesheuvel 41fd0528a2SArvind Sankar while (*str) { 42fd0528a2SArvind Sankar if (*str == '\n') 43fd0528a2SArvind Sankar buf[pos++] = L'\r'; 44fd0528a2SArvind Sankar /* Cast to unsigned char to avoid sign-extension */ 45fd0528a2SArvind Sankar buf[pos++] = (unsigned char)(*str++); 46fd0528a2SArvind Sankar if (*str == '\0' || pos >= lim - 2) { 47fd0528a2SArvind Sankar buf[pos] = L'\0'; 48fd0528a2SArvind Sankar efi_char16_puts(buf); 49fd0528a2SArvind Sankar pos = 0; 50fd0528a2SArvind Sankar } 51f4f75ad5SArd Biesheuvel } 52f4f75ad5SArd Biesheuvel } 53f4f75ad5SArd Biesheuvel 545a17dae4SMatt Fleming /* 555a17dae4SMatt Fleming * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi= 565a17dae4SMatt Fleming * option, e.g. efi=nochunk. 575a17dae4SMatt Fleming * 585a17dae4SMatt Fleming * It should be noted that efi= is parsed in two very different 595a17dae4SMatt Fleming * environments, first in the early boot environment of the EFI boot 605a17dae4SMatt Fleming * stub, and subsequently during the kernel boot. 615a17dae4SMatt Fleming */ 6260f38de7SArd Biesheuvel efi_status_t efi_parse_options(char const *cmdline) 635a17dae4SMatt Fleming { 6491d150c0SArd Biesheuvel size_t len = strlen(cmdline) + 1; 6591d150c0SArd Biesheuvel efi_status_t status; 6691d150c0SArd Biesheuvel char *str, *buf; 675a17dae4SMatt Fleming 6891d150c0SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf); 6991d150c0SArd Biesheuvel if (status != EFI_SUCCESS) 7091d150c0SArd Biesheuvel return status; 7191d150c0SArd Biesheuvel 7291d150c0SArd Biesheuvel str = skip_spaces(memcpy(buf, cmdline, len)); 7391d150c0SArd Biesheuvel 7491d150c0SArd Biesheuvel while (*str) { 7591d150c0SArd Biesheuvel char *param, *val; 7691d150c0SArd Biesheuvel 7791d150c0SArd Biesheuvel str = next_arg(str, ¶m, &val); 7891d150c0SArd Biesheuvel 7991d150c0SArd Biesheuvel if (!strcmp(param, "nokaslr")) { 807d4e323dSArd Biesheuvel efi_nokaslr = true; 8191d150c0SArd Biesheuvel } else if (!strcmp(param, "quiet")) { 827d4e323dSArd Biesheuvel efi_quiet = true; 8379d3219dSArd Biesheuvel } else if (!strcmp(param, "noinitrd")) { 8479d3219dSArd Biesheuvel efi_noinitrd = true; 8591d150c0SArd Biesheuvel } else if (!strcmp(param, "efi") && val) { 8691d150c0SArd Biesheuvel efi_nochunk = parse_option_str(val, "nochunk"); 8791d150c0SArd Biesheuvel efi_novamap = parse_option_str(val, "novamap"); 88eeff7d63SArd Biesheuvel 8991d150c0SArd Biesheuvel efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) && 9091d150c0SArd Biesheuvel parse_option_str(val, "nosoftreserve"); 915a17dae4SMatt Fleming 9291d150c0SArd Biesheuvel if (parse_option_str(val, "disable_early_pci_dma")) 934444f854SMatthew Garrett efi_disable_pci_dma = true; 9491d150c0SArd Biesheuvel if (parse_option_str(val, "no_disable_early_pci_dma")) 954444f854SMatthew Garrett efi_disable_pci_dma = false; 96fffb6804SArvind Sankar } else if (!strcmp(param, "video") && 97fffb6804SArvind Sankar val && strstarts(val, "efifb:")) { 98fffb6804SArvind Sankar efi_parse_option_graphics(val + strlen("efifb:")); 994444f854SMatthew Garrett } 1005a17dae4SMatt Fleming } 10191d150c0SArd Biesheuvel efi_bs_call(free_pool, buf); 1025a17dae4SMatt Fleming return EFI_SUCCESS; 1035a17dae4SMatt Fleming } 104f4f75ad5SArd Biesheuvel 105f4f75ad5SArd Biesheuvel /* 106f4f75ad5SArd Biesheuvel * Get the number of UTF-8 bytes corresponding to an UTF-16 character. 107f4f75ad5SArd Biesheuvel * This overestimates for surrogates, but that is okay. 108f4f75ad5SArd Biesheuvel */ 109f4f75ad5SArd Biesheuvel static int efi_utf8_bytes(u16 c) 110f4f75ad5SArd Biesheuvel { 111f4f75ad5SArd Biesheuvel return 1 + (c >= 0x80) + (c >= 0x800); 112f4f75ad5SArd Biesheuvel } 113f4f75ad5SArd Biesheuvel 114f4f75ad5SArd Biesheuvel /* 115f4f75ad5SArd Biesheuvel * Convert an UTF-16 string, not necessarily null terminated, to UTF-8. 116f4f75ad5SArd Biesheuvel */ 117f4f75ad5SArd Biesheuvel static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n) 118f4f75ad5SArd Biesheuvel { 119f4f75ad5SArd Biesheuvel unsigned int c; 120f4f75ad5SArd Biesheuvel 121f4f75ad5SArd Biesheuvel while (n--) { 122f4f75ad5SArd Biesheuvel c = *src++; 123f4f75ad5SArd Biesheuvel if (n && c >= 0xd800 && c <= 0xdbff && 124f4f75ad5SArd Biesheuvel *src >= 0xdc00 && *src <= 0xdfff) { 125f4f75ad5SArd Biesheuvel c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff); 126f4f75ad5SArd Biesheuvel src++; 127f4f75ad5SArd Biesheuvel n--; 128f4f75ad5SArd Biesheuvel } 129f4f75ad5SArd Biesheuvel if (c >= 0xd800 && c <= 0xdfff) 130f4f75ad5SArd Biesheuvel c = 0xfffd; /* Unmatched surrogate */ 131f4f75ad5SArd Biesheuvel if (c < 0x80) { 132f4f75ad5SArd Biesheuvel *dst++ = c; 133f4f75ad5SArd Biesheuvel continue; 134f4f75ad5SArd Biesheuvel } 135f4f75ad5SArd Biesheuvel if (c < 0x800) { 136f4f75ad5SArd Biesheuvel *dst++ = 0xc0 + (c >> 6); 137f4f75ad5SArd Biesheuvel goto t1; 138f4f75ad5SArd Biesheuvel } 139f4f75ad5SArd Biesheuvel if (c < 0x10000) { 140f4f75ad5SArd Biesheuvel *dst++ = 0xe0 + (c >> 12); 141f4f75ad5SArd Biesheuvel goto t2; 142f4f75ad5SArd Biesheuvel } 143f4f75ad5SArd Biesheuvel *dst++ = 0xf0 + (c >> 18); 144f4f75ad5SArd Biesheuvel *dst++ = 0x80 + ((c >> 12) & 0x3f); 145f4f75ad5SArd Biesheuvel t2: 146f4f75ad5SArd Biesheuvel *dst++ = 0x80 + ((c >> 6) & 0x3f); 147f4f75ad5SArd Biesheuvel t1: 148f4f75ad5SArd Biesheuvel *dst++ = 0x80 + (c & 0x3f); 149f4f75ad5SArd Biesheuvel } 150f4f75ad5SArd Biesheuvel 151f4f75ad5SArd Biesheuvel return dst; 152f4f75ad5SArd Biesheuvel } 153f4f75ad5SArd Biesheuvel 154f4f75ad5SArd Biesheuvel /* 155f4f75ad5SArd Biesheuvel * Convert the unicode UEFI command line to ASCII to pass to kernel. 156f4f75ad5SArd Biesheuvel * Size of memory allocated return in *cmd_line_len. 157f4f75ad5SArd Biesheuvel * Returns NULL on error. 158f4f75ad5SArd Biesheuvel */ 159cd33a5c1SArd Biesheuvel char *efi_convert_cmdline(efi_loaded_image_t *image, 1601e45bf73SArd Biesheuvel int *cmd_line_len, unsigned long max_addr) 161f4f75ad5SArd Biesheuvel { 162f4f75ad5SArd Biesheuvel const u16 *s2; 163f4f75ad5SArd Biesheuvel u8 *s1 = NULL; 164f4f75ad5SArd Biesheuvel unsigned long cmdline_addr = 0; 165f7b85b33SArd Biesheuvel int load_options_chars = efi_table_attr(image, load_options_size) / 2; 166f7b85b33SArd Biesheuvel const u16 *options = efi_table_attr(image, load_options); 167f4f75ad5SArd Biesheuvel int options_bytes = 0; /* UTF-8 bytes */ 168f4f75ad5SArd Biesheuvel int options_chars = 0; /* UTF-16 chars */ 169f4f75ad5SArd Biesheuvel efi_status_t status; 170f4f75ad5SArd Biesheuvel u16 zero = 0; 171f4f75ad5SArd Biesheuvel 172f4f75ad5SArd Biesheuvel if (options) { 173f4f75ad5SArd Biesheuvel s2 = options; 174f4f75ad5SArd Biesheuvel while (*s2 && *s2 != '\n' 175f4f75ad5SArd Biesheuvel && options_chars < load_options_chars) { 176f4f75ad5SArd Biesheuvel options_bytes += efi_utf8_bytes(*s2++); 177f4f75ad5SArd Biesheuvel options_chars++; 178f4f75ad5SArd Biesheuvel } 179f4f75ad5SArd Biesheuvel } 180f4f75ad5SArd Biesheuvel 181f4f75ad5SArd Biesheuvel if (!options_chars) { 182f4f75ad5SArd Biesheuvel /* No command line options, so return empty string*/ 183f4f75ad5SArd Biesheuvel options = &zero; 184f4f75ad5SArd Biesheuvel } 185f4f75ad5SArd Biesheuvel 186f4f75ad5SArd Biesheuvel options_bytes++; /* NUL termination */ 187f4f75ad5SArd Biesheuvel 1881e45bf73SArd Biesheuvel status = efi_allocate_pages(options_bytes, &cmdline_addr, max_addr); 189f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) 190f4f75ad5SArd Biesheuvel return NULL; 191f4f75ad5SArd Biesheuvel 192f4f75ad5SArd Biesheuvel s1 = (u8 *)cmdline_addr; 193f4f75ad5SArd Biesheuvel s2 = (const u16 *)options; 194f4f75ad5SArd Biesheuvel 195f4f75ad5SArd Biesheuvel s1 = efi_utf16_to_utf8(s1, s2, options_chars); 196f4f75ad5SArd Biesheuvel *s1 = '\0'; 197f4f75ad5SArd Biesheuvel 198f4f75ad5SArd Biesheuvel *cmd_line_len = options_bytes; 199f4f75ad5SArd Biesheuvel return (char *)cmdline_addr; 200f4f75ad5SArd Biesheuvel } 201fc07716bSJeffrey Hugo 202fc07716bSJeffrey Hugo /* 203fc07716bSJeffrey Hugo * Handle calling ExitBootServices according to the requirements set out by the 204fc07716bSJeffrey Hugo * spec. Obtains the current memory map, and returns that info after calling 205fc07716bSJeffrey Hugo * ExitBootServices. The client must specify a function to perform any 206fc07716bSJeffrey Hugo * processing of the memory map data prior to ExitBootServices. A client 207fc07716bSJeffrey Hugo * specific structure may be passed to the function via priv. The client 208fc07716bSJeffrey Hugo * function may be called multiple times. 209fc07716bSJeffrey Hugo */ 210cd33a5c1SArd Biesheuvel efi_status_t efi_exit_boot_services(void *handle, 211fc07716bSJeffrey Hugo struct efi_boot_memmap *map, 212fc07716bSJeffrey Hugo void *priv, 213fc07716bSJeffrey Hugo efi_exit_boot_map_processing priv_func) 214fc07716bSJeffrey Hugo { 215fc07716bSJeffrey Hugo efi_status_t status; 216fc07716bSJeffrey Hugo 217cd33a5c1SArd Biesheuvel status = efi_get_memory_map(map); 218fc07716bSJeffrey Hugo 219fc07716bSJeffrey Hugo if (status != EFI_SUCCESS) 220fc07716bSJeffrey Hugo goto fail; 221fc07716bSJeffrey Hugo 222cd33a5c1SArd Biesheuvel status = priv_func(map, priv); 223fc07716bSJeffrey Hugo if (status != EFI_SUCCESS) 224fc07716bSJeffrey Hugo goto free_map; 225fc07716bSJeffrey Hugo 2264444f854SMatthew Garrett if (efi_disable_pci_dma) 2274444f854SMatthew Garrett efi_pci_disable_bridge_busmaster(); 2284444f854SMatthew Garrett 229966291f6SArd Biesheuvel status = efi_bs_call(exit_boot_services, handle, *map->key_ptr); 230fc07716bSJeffrey Hugo 231fc07716bSJeffrey Hugo if (status == EFI_INVALID_PARAMETER) { 232fc07716bSJeffrey Hugo /* 233fc07716bSJeffrey Hugo * The memory map changed between efi_get_memory_map() and 234fc07716bSJeffrey Hugo * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4: 235fc07716bSJeffrey Hugo * EFI_BOOT_SERVICES.ExitBootServices we need to get the 236fc07716bSJeffrey Hugo * updated map, and try again. The spec implies one retry 237fc07716bSJeffrey Hugo * should be sufficent, which is confirmed against the EDK2 238fc07716bSJeffrey Hugo * implementation. Per the spec, we can only invoke 239fc07716bSJeffrey Hugo * get_memory_map() and exit_boot_services() - we cannot alloc 240fc07716bSJeffrey Hugo * so efi_get_memory_map() cannot be used, and we must reuse 241fc07716bSJeffrey Hugo * the buffer. For all practical purposes, the headroom in the 242fc07716bSJeffrey Hugo * buffer should account for any changes in the map so the call 243fc07716bSJeffrey Hugo * to get_memory_map() is expected to succeed here. 244fc07716bSJeffrey Hugo */ 245fc07716bSJeffrey Hugo *map->map_size = *map->buff_size; 246966291f6SArd Biesheuvel status = efi_bs_call(get_memory_map, 247fc07716bSJeffrey Hugo map->map_size, 248fc07716bSJeffrey Hugo *map->map, 249fc07716bSJeffrey Hugo map->key_ptr, 250fc07716bSJeffrey Hugo map->desc_size, 251fc07716bSJeffrey Hugo map->desc_ver); 252fc07716bSJeffrey Hugo 253fc07716bSJeffrey Hugo /* exit_boot_services() was called, thus cannot free */ 254fc07716bSJeffrey Hugo if (status != EFI_SUCCESS) 255fc07716bSJeffrey Hugo goto fail; 256fc07716bSJeffrey Hugo 257cd33a5c1SArd Biesheuvel status = priv_func(map, priv); 258fc07716bSJeffrey Hugo /* exit_boot_services() was called, thus cannot free */ 259fc07716bSJeffrey Hugo if (status != EFI_SUCCESS) 260fc07716bSJeffrey Hugo goto fail; 261fc07716bSJeffrey Hugo 262966291f6SArd Biesheuvel status = efi_bs_call(exit_boot_services, handle, *map->key_ptr); 263fc07716bSJeffrey Hugo } 264fc07716bSJeffrey Hugo 265fc07716bSJeffrey Hugo /* exit_boot_services() was called, thus cannot free */ 266fc07716bSJeffrey Hugo if (status != EFI_SUCCESS) 267fc07716bSJeffrey Hugo goto fail; 268fc07716bSJeffrey Hugo 269fc07716bSJeffrey Hugo return EFI_SUCCESS; 270fc07716bSJeffrey Hugo 271fc07716bSJeffrey Hugo free_map: 272966291f6SArd Biesheuvel efi_bs_call(free_pool, *map->map); 273fc07716bSJeffrey Hugo fail: 274fc07716bSJeffrey Hugo return status; 275fc07716bSJeffrey Hugo } 27682d736acSMatthew Garrett 277cd33a5c1SArd Biesheuvel void *get_efi_config_table(efi_guid_t guid) 27882d736acSMatthew Garrett { 279ccc27ae7SArd Biesheuvel unsigned long tables = efi_table_attr(efi_system_table, tables); 280ccc27ae7SArd Biesheuvel int nr_tables = efi_table_attr(efi_system_table, nr_tables); 281f958efe9SArd Biesheuvel int i; 282f958efe9SArd Biesheuvel 283f958efe9SArd Biesheuvel for (i = 0; i < nr_tables; i++) { 284f958efe9SArd Biesheuvel efi_config_table_t *t = (void *)tables; 285f958efe9SArd Biesheuvel 286f958efe9SArd Biesheuvel if (efi_guidcmp(t->guid, guid) == 0) 28799ea8b1dSArd Biesheuvel return efi_table_attr(t, table); 288f958efe9SArd Biesheuvel 289f958efe9SArd Biesheuvel tables += efi_is_native() ? sizeof(efi_config_table_t) 290f958efe9SArd Biesheuvel : sizeof(efi_config_table_32_t); 291f958efe9SArd Biesheuvel } 292f958efe9SArd Biesheuvel return NULL; 29382d736acSMatthew Garrett } 294dc29da14SArd Biesheuvel 295ec93fc37SArd Biesheuvel /* 296ec93fc37SArd Biesheuvel * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way 297ec93fc37SArd Biesheuvel * for the firmware or bootloader to expose the initrd data directly to the stub 298ec93fc37SArd Biesheuvel * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is 299ec93fc37SArd Biesheuvel * very easy to implement. It is a simple Linux initrd specific conduit between 300ec93fc37SArd Biesheuvel * kernel and firmware, allowing us to put the EFI stub (being part of the 301ec93fc37SArd Biesheuvel * kernel) in charge of where and when to load the initrd, while leaving it up 302ec93fc37SArd Biesheuvel * to the firmware to decide whether it needs to expose its filesystem hierarchy 303ec93fc37SArd Biesheuvel * via EFI protocols. 304ec93fc37SArd Biesheuvel */ 305ec93fc37SArd Biesheuvel static const struct { 306ec93fc37SArd Biesheuvel struct efi_vendor_dev_path vendor; 307ec93fc37SArd Biesheuvel struct efi_generic_dev_path end; 308ec93fc37SArd Biesheuvel } __packed initrd_dev_path = { 309ec93fc37SArd Biesheuvel { 310ec93fc37SArd Biesheuvel { 311ec93fc37SArd Biesheuvel EFI_DEV_MEDIA, 312ec93fc37SArd Biesheuvel EFI_DEV_MEDIA_VENDOR, 313ec93fc37SArd Biesheuvel sizeof(struct efi_vendor_dev_path), 314ec93fc37SArd Biesheuvel }, 315ec93fc37SArd Biesheuvel LINUX_EFI_INITRD_MEDIA_GUID 316ec93fc37SArd Biesheuvel }, { 317ec93fc37SArd Biesheuvel EFI_DEV_END_PATH, 318ec93fc37SArd Biesheuvel EFI_DEV_END_ENTIRE, 319ec93fc37SArd Biesheuvel sizeof(struct efi_generic_dev_path) 320ec93fc37SArd Biesheuvel } 321ec93fc37SArd Biesheuvel }; 322ec93fc37SArd Biesheuvel 323ec93fc37SArd Biesheuvel /** 324ec93fc37SArd Biesheuvel * efi_load_initrd_dev_path - load the initrd from the Linux initrd device path 325ec93fc37SArd Biesheuvel * @load_addr: pointer to store the address where the initrd was loaded 326ec93fc37SArd Biesheuvel * @load_size: pointer to store the size of the loaded initrd 327ec93fc37SArd Biesheuvel * @max: upper limit for the initrd memory allocation 328ec93fc37SArd Biesheuvel * @return: %EFI_SUCCESS if the initrd was loaded successfully, in which 329ec93fc37SArd Biesheuvel * case @load_addr and @load_size are assigned accordingly 330ec93fc37SArd Biesheuvel * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd 331ec93fc37SArd Biesheuvel * device path 332ec93fc37SArd Biesheuvel * %EFI_INVALID_PARAMETER if load_addr == NULL or load_size == NULL 333ec93fc37SArd Biesheuvel * %EFI_OUT_OF_RESOURCES if memory allocation failed 334ec93fc37SArd Biesheuvel * %EFI_LOAD_ERROR in all other cases 335ec93fc37SArd Biesheuvel */ 336f61900fdSArvind Sankar static 337ec93fc37SArd Biesheuvel efi_status_t efi_load_initrd_dev_path(unsigned long *load_addr, 338ec93fc37SArd Biesheuvel unsigned long *load_size, 339ec93fc37SArd Biesheuvel unsigned long max) 340ec93fc37SArd Biesheuvel { 341ec93fc37SArd Biesheuvel efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID; 342ec93fc37SArd Biesheuvel efi_device_path_protocol_t *dp; 343ec93fc37SArd Biesheuvel efi_load_file2_protocol_t *lf2; 344ec93fc37SArd Biesheuvel unsigned long initrd_addr; 345ec93fc37SArd Biesheuvel unsigned long initrd_size; 346ec93fc37SArd Biesheuvel efi_handle_t handle; 347ec93fc37SArd Biesheuvel efi_status_t status; 348ec93fc37SArd Biesheuvel 349ec93fc37SArd Biesheuvel dp = (efi_device_path_protocol_t *)&initrd_dev_path; 350ec93fc37SArd Biesheuvel status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle); 351ec93fc37SArd Biesheuvel if (status != EFI_SUCCESS) 352ec93fc37SArd Biesheuvel return status; 353ec93fc37SArd Biesheuvel 354ec93fc37SArd Biesheuvel status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid, 355ec93fc37SArd Biesheuvel (void **)&lf2); 356ec93fc37SArd Biesheuvel if (status != EFI_SUCCESS) 357ec93fc37SArd Biesheuvel return status; 358ec93fc37SArd Biesheuvel 359ec93fc37SArd Biesheuvel status = efi_call_proto(lf2, load_file, dp, false, &initrd_size, NULL); 360ec93fc37SArd Biesheuvel if (status != EFI_BUFFER_TOO_SMALL) 361ec93fc37SArd Biesheuvel return EFI_LOAD_ERROR; 362ec93fc37SArd Biesheuvel 363ec93fc37SArd Biesheuvel status = efi_allocate_pages(initrd_size, &initrd_addr, max); 364ec93fc37SArd Biesheuvel if (status != EFI_SUCCESS) 365ec93fc37SArd Biesheuvel return status; 366ec93fc37SArd Biesheuvel 367ec93fc37SArd Biesheuvel status = efi_call_proto(lf2, load_file, dp, false, &initrd_size, 368ec93fc37SArd Biesheuvel (void *)initrd_addr); 369ec93fc37SArd Biesheuvel if (status != EFI_SUCCESS) { 370ec93fc37SArd Biesheuvel efi_free(initrd_size, initrd_addr); 371ec93fc37SArd Biesheuvel return EFI_LOAD_ERROR; 372ec93fc37SArd Biesheuvel } 373ec93fc37SArd Biesheuvel 374ec93fc37SArd Biesheuvel *load_addr = initrd_addr; 375ec93fc37SArd Biesheuvel *load_size = initrd_size; 376ec93fc37SArd Biesheuvel return EFI_SUCCESS; 377ec93fc37SArd Biesheuvel } 378f61900fdSArvind Sankar 379f61900fdSArvind Sankar static 380f61900fdSArvind Sankar efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image, 381f61900fdSArvind Sankar unsigned long *load_addr, 382f61900fdSArvind Sankar unsigned long *load_size, 383f61900fdSArvind Sankar unsigned long soft_limit, 384f61900fdSArvind Sankar unsigned long hard_limit) 385f61900fdSArvind Sankar { 386f61900fdSArvind Sankar if (!IS_ENABLED(CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER) || 387f61900fdSArvind Sankar (IS_ENABLED(CONFIG_X86) && (!efi_is_native() || image == NULL))) { 388f61900fdSArvind Sankar *load_addr = *load_size = 0; 389f61900fdSArvind Sankar return EFI_SUCCESS; 390f61900fdSArvind Sankar } 391f61900fdSArvind Sankar 392f61900fdSArvind Sankar return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2, 393f61900fdSArvind Sankar soft_limit, hard_limit, 394f61900fdSArvind Sankar load_addr, load_size); 395f61900fdSArvind Sankar } 396f61900fdSArvind Sankar 397f61900fdSArvind Sankar efi_status_t efi_load_initrd(efi_loaded_image_t *image, 398f61900fdSArvind Sankar unsigned long *load_addr, 399f61900fdSArvind Sankar unsigned long *load_size, 400f61900fdSArvind Sankar unsigned long soft_limit, 401f61900fdSArvind Sankar unsigned long hard_limit) 402f61900fdSArvind Sankar { 403f61900fdSArvind Sankar efi_status_t status; 404f61900fdSArvind Sankar 405f61900fdSArvind Sankar if (!load_addr || !load_size) 406f61900fdSArvind Sankar return EFI_INVALID_PARAMETER; 407f61900fdSArvind Sankar 408f61900fdSArvind Sankar status = efi_load_initrd_dev_path(load_addr, load_size, hard_limit); 409f61900fdSArvind Sankar if (status == EFI_SUCCESS) { 410f61900fdSArvind Sankar efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n"); 411f61900fdSArvind Sankar } else if (status == EFI_NOT_FOUND) { 412f61900fdSArvind Sankar status = efi_load_initrd_cmdline(image, load_addr, load_size, 413f61900fdSArvind Sankar soft_limit, hard_limit); 414f61900fdSArvind Sankar if (status == EFI_SUCCESS && *load_size > 0) 415f61900fdSArvind Sankar efi_info("Loaded initrd from command line option\n"); 416f61900fdSArvind Sankar } 417f61900fdSArvind Sankar 418f61900fdSArvind Sankar return status; 419f61900fdSArvind Sankar } 420