15193a33dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0 25193a33dSArd Biesheuvel /* 35193a33dSArd Biesheuvel * Helper functions used by the EFI stub on multiple 45193a33dSArd Biesheuvel * architectures. This should be #included by the EFI stub 55193a33dSArd Biesheuvel * implementation files. 65193a33dSArd Biesheuvel * 75193a33dSArd Biesheuvel * Copyright 2011 Intel Corporation; author Matt Fleming 85193a33dSArd Biesheuvel */ 95193a33dSArd Biesheuvel 105193a33dSArd Biesheuvel #include <linux/efi.h> 115193a33dSArd Biesheuvel #include <asm/efi.h> 125193a33dSArd Biesheuvel 135193a33dSArd Biesheuvel #include "efistub.h" 145193a33dSArd Biesheuvel 159302c1bbSArd Biesheuvel #define MAX_FILENAME_SIZE 256 169302c1bbSArd Biesheuvel 175193a33dSArd Biesheuvel /* 185193a33dSArd Biesheuvel * Some firmware implementations have problems reading files in one go. 195193a33dSArd Biesheuvel * A read chunk size of 1MB seems to work for most platforms. 205193a33dSArd Biesheuvel * 215193a33dSArd Biesheuvel * Unfortunately, reading files in chunks triggers *other* bugs on some 225193a33dSArd Biesheuvel * platforms, so we provide a way to disable this workaround, which can 235193a33dSArd Biesheuvel * be done by passing "efi=nochunk" on the EFI boot stub command line. 245193a33dSArd Biesheuvel * 255193a33dSArd Biesheuvel * If you experience issues with initrd images being corrupt it's worth 265193a33dSArd Biesheuvel * trying efi=nochunk, but chunking is enabled by default on x86 because 275193a33dSArd Biesheuvel * there are far more machines that require the workaround than those that 285193a33dSArd Biesheuvel * break with it enabled. 295193a33dSArd Biesheuvel */ 305193a33dSArd Biesheuvel #define EFI_READ_CHUNK_SIZE SZ_1M 315193a33dSArd Biesheuvel 32464fb126SArd Biesheuvel struct finfo { 33464fb126SArd Biesheuvel efi_file_info_t info; 34464fb126SArd Biesheuvel efi_char16_t filename[MAX_FILENAME_SIZE]; 35464fb126SArd Biesheuvel }; 36464fb126SArd Biesheuvel 379302c1bbSArd Biesheuvel static efi_status_t efi_open_file(efi_file_protocol_t *volume, 38464fb126SArd Biesheuvel struct finfo *fi, 399302c1bbSArd Biesheuvel efi_file_protocol_t **handle, 409302c1bbSArd Biesheuvel unsigned long *file_size) 415193a33dSArd Biesheuvel { 425193a33dSArd Biesheuvel efi_guid_t info_guid = EFI_FILE_INFO_ID; 439302c1bbSArd Biesheuvel efi_file_protocol_t *fh; 445193a33dSArd Biesheuvel unsigned long info_sz; 459302c1bbSArd Biesheuvel efi_status_t status; 46*70912985SArd Biesheuvel efi_char16_t *c; 47*70912985SArd Biesheuvel 48*70912985SArd Biesheuvel /* Replace UNIX dir separators with EFI standard ones */ 49*70912985SArd Biesheuvel for (c = fi->filename; *c != L'\0'; c++) { 50*70912985SArd Biesheuvel if (*c == L'/') 51*70912985SArd Biesheuvel *c = L'\\'; 52*70912985SArd Biesheuvel } 535193a33dSArd Biesheuvel 54464fb126SArd Biesheuvel status = volume->open(volume, &fh, fi->filename, EFI_FILE_MODE_READ, 0); 555193a33dSArd Biesheuvel if (status != EFI_SUCCESS) { 56a713979eSArvind Sankar efi_err("Failed to open file: %ls\n", fi->filename); 575193a33dSArd Biesheuvel return status; 585193a33dSArd Biesheuvel } 595193a33dSArd Biesheuvel 60464fb126SArd Biesheuvel info_sz = sizeof(struct finfo); 61464fb126SArd Biesheuvel status = fh->get_info(fh, &info_guid, &info_sz, fi); 625193a33dSArd Biesheuvel if (status != EFI_SUCCESS) { 63793473c2SArvind Sankar efi_err("Failed to get file info\n"); 649302c1bbSArd Biesheuvel fh->close(fh); 655193a33dSArd Biesheuvel return status; 665193a33dSArd Biesheuvel } 675193a33dSArd Biesheuvel 689302c1bbSArd Biesheuvel *handle = fh; 69464fb126SArd Biesheuvel *file_size = fi->info.file_size; 709302c1bbSArd Biesheuvel return EFI_SUCCESS; 715193a33dSArd Biesheuvel } 725193a33dSArd Biesheuvel 735193a33dSArd Biesheuvel static efi_status_t efi_open_volume(efi_loaded_image_t *image, 749302c1bbSArd Biesheuvel efi_file_protocol_t **fh) 755193a33dSArd Biesheuvel { 765193a33dSArd Biesheuvel efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; 779302c1bbSArd Biesheuvel efi_simple_file_system_protocol_t *io; 785193a33dSArd Biesheuvel efi_status_t status; 795193a33dSArd Biesheuvel 809302c1bbSArd Biesheuvel status = efi_bs_call(handle_protocol, image->device_handle, &fs_proto, 819302c1bbSArd Biesheuvel (void **)&io); 825193a33dSArd Biesheuvel if (status != EFI_SUCCESS) { 83793473c2SArvind Sankar efi_err("Failed to handle fs_proto\n"); 845193a33dSArd Biesheuvel return status; 855193a33dSArd Biesheuvel } 865193a33dSArd Biesheuvel 879302c1bbSArd Biesheuvel status = io->open_volume(io, fh); 885193a33dSArd Biesheuvel if (status != EFI_SUCCESS) 89793473c2SArvind Sankar efi_err("Failed to open volume\n"); 905193a33dSArd Biesheuvel 915193a33dSArd Biesheuvel return status; 925193a33dSArd Biesheuvel } 935193a33dSArd Biesheuvel 949302c1bbSArd Biesheuvel static int find_file_option(const efi_char16_t *cmdline, int cmdline_len, 959302c1bbSArd Biesheuvel const efi_char16_t *prefix, int prefix_size, 969302c1bbSArd Biesheuvel efi_char16_t *result, int result_len) 979302c1bbSArd Biesheuvel { 989302c1bbSArd Biesheuvel int prefix_len = prefix_size / 2; 999302c1bbSArd Biesheuvel bool found = false; 1009302c1bbSArd Biesheuvel int i; 1019302c1bbSArd Biesheuvel 1029302c1bbSArd Biesheuvel for (i = prefix_len; i < cmdline_len; i++) { 1039302c1bbSArd Biesheuvel if (!memcmp(&cmdline[i - prefix_len], prefix, prefix_size)) { 1049302c1bbSArd Biesheuvel found = true; 1059302c1bbSArd Biesheuvel break; 1069302c1bbSArd Biesheuvel } 1079302c1bbSArd Biesheuvel } 1089302c1bbSArd Biesheuvel 1099302c1bbSArd Biesheuvel if (!found) 1109302c1bbSArd Biesheuvel return 0; 1119302c1bbSArd Biesheuvel 1127a88a622SPhilipp Fent /* Skip any leading slashes */ 113c4039b29SDan Carpenter while (i < cmdline_len && (cmdline[i] == L'/' || cmdline[i] == L'\\')) 1147a88a622SPhilipp Fent i++; 1157a88a622SPhilipp Fent 1169302c1bbSArd Biesheuvel while (--result_len > 0 && i < cmdline_len) { 1177a88a622SPhilipp Fent efi_char16_t c = cmdline[i++]; 1187a88a622SPhilipp Fent 1197a88a622SPhilipp Fent if (c == L'\0' || c == L'\n' || c == L' ') 1209302c1bbSArd Biesheuvel break; 1217a88a622SPhilipp Fent *result++ = c; 1229302c1bbSArd Biesheuvel } 1239302c1bbSArd Biesheuvel *result = L'\0'; 1249302c1bbSArd Biesheuvel return i; 1259302c1bbSArd Biesheuvel } 1269302c1bbSArd Biesheuvel 127*70912985SArd Biesheuvel static efi_status_t efi_open_device_path(efi_file_protocol_t **volume, 128*70912985SArd Biesheuvel struct finfo *fi) 129*70912985SArd Biesheuvel { 130*70912985SArd Biesheuvel efi_guid_t text_to_dp_guid = EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL_GUID; 131*70912985SArd Biesheuvel static efi_device_path_from_text_protocol_t *text_to_dp = NULL; 132*70912985SArd Biesheuvel efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; 133*70912985SArd Biesheuvel efi_device_path_protocol_t *initrd_dp; 134*70912985SArd Biesheuvel efi_simple_file_system_protocol_t *io; 135*70912985SArd Biesheuvel struct efi_file_path_dev_path *fpath; 136*70912985SArd Biesheuvel efi_handle_t handle; 137*70912985SArd Biesheuvel efi_status_t status; 138*70912985SArd Biesheuvel 139*70912985SArd Biesheuvel /* See if the text to device path protocol exists */ 140*70912985SArd Biesheuvel if (!text_to_dp && 141*70912985SArd Biesheuvel efi_bs_call(locate_protocol, &text_to_dp_guid, NULL, 142*70912985SArd Biesheuvel (void **)&text_to_dp) != EFI_SUCCESS) 143*70912985SArd Biesheuvel return EFI_UNSUPPORTED; 144*70912985SArd Biesheuvel 145*70912985SArd Biesheuvel 146*70912985SArd Biesheuvel /* Convert the filename wide string into a device path */ 147*70912985SArd Biesheuvel initrd_dp = text_to_dp->convert_text_to_device_path(fi->filename); 148*70912985SArd Biesheuvel 149*70912985SArd Biesheuvel /* Check whether the device path in question implements simple FS */ 150*70912985SArd Biesheuvel if ((efi_bs_call(locate_device_path, &fs_proto, &initrd_dp, &handle) ?: 151*70912985SArd Biesheuvel efi_bs_call(handle_protocol, handle, &fs_proto, (void **)&io)) 152*70912985SArd Biesheuvel != EFI_SUCCESS) 153*70912985SArd Biesheuvel return EFI_NOT_FOUND; 154*70912985SArd Biesheuvel 155*70912985SArd Biesheuvel /* Check whether the remaining device path is a file device path */ 156*70912985SArd Biesheuvel if (initrd_dp->type != EFI_DEV_MEDIA || 157*70912985SArd Biesheuvel initrd_dp->sub_type != EFI_DEV_MEDIA_FILE) { 158*70912985SArd Biesheuvel efi_warn("Unexpected device path node type: (%x, %x)\n", 159*70912985SArd Biesheuvel initrd_dp->type, initrd_dp->sub_type); 160*70912985SArd Biesheuvel return EFI_LOAD_ERROR; 161*70912985SArd Biesheuvel } 162*70912985SArd Biesheuvel 163*70912985SArd Biesheuvel /* Copy the remaining file path into the fi structure */ 164*70912985SArd Biesheuvel fpath = (struct efi_file_path_dev_path *)initrd_dp; 165*70912985SArd Biesheuvel memcpy(fi->filename, fpath->filename, 166*70912985SArd Biesheuvel min(sizeof(fi->filename), 167*70912985SArd Biesheuvel fpath->header.length - sizeof(fpath->header))); 168*70912985SArd Biesheuvel 169*70912985SArd Biesheuvel status = io->open_volume(io, volume); 170*70912985SArd Biesheuvel if (status != EFI_SUCCESS) 171*70912985SArd Biesheuvel efi_err("Failed to open volume\n"); 172*70912985SArd Biesheuvel 173*70912985SArd Biesheuvel return status; 174*70912985SArd Biesheuvel } 175*70912985SArd Biesheuvel 1765193a33dSArd Biesheuvel /* 1775193a33dSArd Biesheuvel * Check the cmdline for a LILO-style file= arguments. 1785193a33dSArd Biesheuvel * 1795193a33dSArd Biesheuvel * We only support loading a file from the same filesystem as 1805193a33dSArd Biesheuvel * the kernel image. 1815193a33dSArd Biesheuvel */ 182cf6b8366SArd Biesheuvel efi_status_t handle_cmdline_files(efi_loaded_image_t *image, 1839302c1bbSArd Biesheuvel const efi_char16_t *optstr, 1849302c1bbSArd Biesheuvel int optstr_size, 18531f5e546SArd Biesheuvel unsigned long soft_limit, 18631f5e546SArd Biesheuvel unsigned long hard_limit, 1875193a33dSArd Biesheuvel unsigned long *load_addr, 1885193a33dSArd Biesheuvel unsigned long *load_size) 1895193a33dSArd Biesheuvel { 1909302c1bbSArd Biesheuvel const efi_char16_t *cmdline = image->load_options; 191a241d94bSArd Biesheuvel u32 cmdline_len = image->load_options_size; 1925193a33dSArd Biesheuvel unsigned long efi_chunk_size = ULONG_MAX; 1939302c1bbSArd Biesheuvel efi_file_protocol_t *volume = NULL; 1949302c1bbSArd Biesheuvel efi_file_protocol_t *file; 1959302c1bbSArd Biesheuvel unsigned long alloc_addr; 1969302c1bbSArd Biesheuvel unsigned long alloc_size; 1975193a33dSArd Biesheuvel efi_status_t status; 1989302c1bbSArd Biesheuvel int offset; 1995193a33dSArd Biesheuvel 2005193a33dSArd Biesheuvel if (!load_addr || !load_size) 2015193a33dSArd Biesheuvel return EFI_INVALID_PARAMETER; 2025193a33dSArd Biesheuvel 2034a568ce2SArvind Sankar efi_apply_loadoptions_quirk((const void **)&cmdline, &cmdline_len); 2044a568ce2SArvind Sankar cmdline_len /= sizeof(*cmdline); 2054a568ce2SArvind Sankar 206980771f6SArd Biesheuvel if (IS_ENABLED(CONFIG_X86) && !efi_nochunk) 2079302c1bbSArd Biesheuvel efi_chunk_size = EFI_READ_CHUNK_SIZE; 2085193a33dSArd Biesheuvel 2099302c1bbSArd Biesheuvel alloc_addr = alloc_size = 0; 2109302c1bbSArd Biesheuvel do { 211464fb126SArd Biesheuvel struct finfo fi; 2129302c1bbSArd Biesheuvel unsigned long size; 2139302c1bbSArd Biesheuvel void *addr; 2145193a33dSArd Biesheuvel 2159302c1bbSArd Biesheuvel offset = find_file_option(cmdline, cmdline_len, 2169302c1bbSArd Biesheuvel optstr, optstr_size, 217464fb126SArd Biesheuvel fi.filename, ARRAY_SIZE(fi.filename)); 2189302c1bbSArd Biesheuvel 2199302c1bbSArd Biesheuvel if (!offset) 2205193a33dSArd Biesheuvel break; 2215193a33dSArd Biesheuvel 2229302c1bbSArd Biesheuvel cmdline += offset; 2239302c1bbSArd Biesheuvel cmdline_len -= offset; 2245193a33dSArd Biesheuvel 225*70912985SArd Biesheuvel status = efi_open_device_path(&volume, &fi); 226*70912985SArd Biesheuvel if (status == EFI_UNSUPPORTED || status == EFI_NOT_FOUND) 227*70912985SArd Biesheuvel /* try the volume that holds the kernel itself */ 2289302c1bbSArd Biesheuvel status = efi_open_volume(image, &volume); 229*70912985SArd Biesheuvel 2305193a33dSArd Biesheuvel if (status != EFI_SUCCESS) 231*70912985SArd Biesheuvel goto err_free_alloc; 2325193a33dSArd Biesheuvel 233464fb126SArd Biesheuvel status = efi_open_file(volume, &fi, &file, &size); 2345193a33dSArd Biesheuvel if (status != EFI_SUCCESS) 2359302c1bbSArd Biesheuvel goto err_close_volume; 2365193a33dSArd Biesheuvel 2375193a33dSArd Biesheuvel /* 2389302c1bbSArd Biesheuvel * Check whether the existing allocation can contain the next 2399302c1bbSArd Biesheuvel * file. This condition will also trigger naturally during the 2409302c1bbSArd Biesheuvel * first (and typically only) iteration of the loop, given that 2419302c1bbSArd Biesheuvel * alloc_size == 0 in that case. 2425193a33dSArd Biesheuvel */ 2439302c1bbSArd Biesheuvel if (round_up(alloc_size + size, EFI_ALLOC_ALIGN) > 2449302c1bbSArd Biesheuvel round_up(alloc_size, EFI_ALLOC_ALIGN)) { 2459302c1bbSArd Biesheuvel unsigned long old_addr = alloc_addr; 2469302c1bbSArd Biesheuvel 24731f5e546SArd Biesheuvel status = EFI_OUT_OF_RESOURCES; 24831f5e546SArd Biesheuvel if (soft_limit < hard_limit) 24931f5e546SArd Biesheuvel status = efi_allocate_pages(alloc_size + size, 25031f5e546SArd Biesheuvel &alloc_addr, 25131f5e546SArd Biesheuvel soft_limit); 25231f5e546SArd Biesheuvel if (status == EFI_OUT_OF_RESOURCES) 25331f5e546SArd Biesheuvel status = efi_allocate_pages(alloc_size + size, 25431f5e546SArd Biesheuvel &alloc_addr, 25531f5e546SArd Biesheuvel hard_limit); 2565193a33dSArd Biesheuvel if (status != EFI_SUCCESS) { 257793473c2SArvind Sankar efi_err("Failed to allocate memory for files\n"); 2589302c1bbSArd Biesheuvel goto err_close_file; 2595193a33dSArd Biesheuvel } 2605193a33dSArd Biesheuvel 2619302c1bbSArd Biesheuvel if (old_addr != 0) { 2629302c1bbSArd Biesheuvel /* 2639302c1bbSArd Biesheuvel * This is not the first time we've gone 2649302c1bbSArd Biesheuvel * around this loop, and so we are loading 2659302c1bbSArd Biesheuvel * multiple files that need to be concatenated 2669302c1bbSArd Biesheuvel * and returned in a single buffer. 2679302c1bbSArd Biesheuvel */ 2689302c1bbSArd Biesheuvel memcpy((void *)alloc_addr, (void *)old_addr, alloc_size); 2699302c1bbSArd Biesheuvel efi_free(alloc_size, old_addr); 2709302c1bbSArd Biesheuvel } 2715193a33dSArd Biesheuvel } 2725193a33dSArd Biesheuvel 2739302c1bbSArd Biesheuvel addr = (void *)alloc_addr + alloc_size; 2749302c1bbSArd Biesheuvel alloc_size += size; 2755193a33dSArd Biesheuvel 2765193a33dSArd Biesheuvel while (size) { 2779302c1bbSArd Biesheuvel unsigned long chunksize = min(size, efi_chunk_size); 2785193a33dSArd Biesheuvel 2799302c1bbSArd Biesheuvel status = file->read(file, &chunksize, addr); 2805193a33dSArd Biesheuvel if (status != EFI_SUCCESS) { 281793473c2SArvind Sankar efi_err("Failed to read file\n"); 2829302c1bbSArd Biesheuvel goto err_close_file; 2835193a33dSArd Biesheuvel } 2845193a33dSArd Biesheuvel addr += chunksize; 2855193a33dSArd Biesheuvel size -= chunksize; 2865193a33dSArd Biesheuvel } 2879302c1bbSArd Biesheuvel file->close(file); 288*70912985SArd Biesheuvel volume->close(volume); 2899302c1bbSArd Biesheuvel } while (offset > 0); 2905193a33dSArd Biesheuvel 2919302c1bbSArd Biesheuvel *load_addr = alloc_addr; 2929302c1bbSArd Biesheuvel *load_size = alloc_size; 2939302c1bbSArd Biesheuvel 294f4dc7fffSArd Biesheuvel if (*load_size == 0) 295f4dc7fffSArd Biesheuvel return EFI_NOT_READY; 2969302c1bbSArd Biesheuvel return EFI_SUCCESS; 2979302c1bbSArd Biesheuvel 2989302c1bbSArd Biesheuvel err_close_file: 2999302c1bbSArd Biesheuvel file->close(file); 3009302c1bbSArd Biesheuvel 3019302c1bbSArd Biesheuvel err_close_volume: 3029302c1bbSArd Biesheuvel volume->close(volume); 303*70912985SArd Biesheuvel 304*70912985SArd Biesheuvel err_free_alloc: 3059302c1bbSArd Biesheuvel efi_free(alloc_size, alloc_addr); 3069302c1bbSArd Biesheuvel return status; 3075193a33dSArd Biesheuvel } 308