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; 465193a33dSArd Biesheuvel 47464fb126SArd Biesheuvel status = volume->open(volume, &fh, fi->filename, EFI_FILE_MODE_READ, 0); 485193a33dSArd Biesheuvel if (status != EFI_SUCCESS) { 49a713979eSArvind Sankar efi_err("Failed to open file: %ls\n", fi->filename); 505193a33dSArd Biesheuvel return status; 515193a33dSArd Biesheuvel } 525193a33dSArd Biesheuvel 53464fb126SArd Biesheuvel info_sz = sizeof(struct finfo); 54464fb126SArd Biesheuvel status = fh->get_info(fh, &info_guid, &info_sz, fi); 555193a33dSArd Biesheuvel if (status != EFI_SUCCESS) { 56793473c2SArvind Sankar efi_err("Failed to get file info\n"); 579302c1bbSArd Biesheuvel fh->close(fh); 585193a33dSArd Biesheuvel return status; 595193a33dSArd Biesheuvel } 605193a33dSArd Biesheuvel 619302c1bbSArd Biesheuvel *handle = fh; 62464fb126SArd Biesheuvel *file_size = fi->info.file_size; 639302c1bbSArd Biesheuvel return EFI_SUCCESS; 645193a33dSArd Biesheuvel } 655193a33dSArd Biesheuvel 665193a33dSArd Biesheuvel static efi_status_t efi_open_volume(efi_loaded_image_t *image, 679302c1bbSArd Biesheuvel efi_file_protocol_t **fh) 685193a33dSArd Biesheuvel { 695193a33dSArd Biesheuvel efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; 709302c1bbSArd Biesheuvel efi_simple_file_system_protocol_t *io; 715193a33dSArd Biesheuvel efi_status_t status; 725193a33dSArd Biesheuvel 739302c1bbSArd Biesheuvel status = efi_bs_call(handle_protocol, image->device_handle, &fs_proto, 749302c1bbSArd Biesheuvel (void **)&io); 755193a33dSArd Biesheuvel if (status != EFI_SUCCESS) { 76793473c2SArvind Sankar efi_err("Failed to handle fs_proto\n"); 775193a33dSArd Biesheuvel return status; 785193a33dSArd Biesheuvel } 795193a33dSArd Biesheuvel 809302c1bbSArd Biesheuvel status = io->open_volume(io, fh); 815193a33dSArd Biesheuvel if (status != EFI_SUCCESS) 82793473c2SArvind Sankar efi_err("Failed to open volume\n"); 835193a33dSArd Biesheuvel 845193a33dSArd Biesheuvel return status; 855193a33dSArd Biesheuvel } 865193a33dSArd Biesheuvel 879302c1bbSArd Biesheuvel static int find_file_option(const efi_char16_t *cmdline, int cmdline_len, 889302c1bbSArd Biesheuvel const efi_char16_t *prefix, int prefix_size, 899302c1bbSArd Biesheuvel efi_char16_t *result, int result_len) 909302c1bbSArd Biesheuvel { 919302c1bbSArd Biesheuvel int prefix_len = prefix_size / 2; 929302c1bbSArd Biesheuvel bool found = false; 939302c1bbSArd Biesheuvel int i; 949302c1bbSArd Biesheuvel 959302c1bbSArd Biesheuvel for (i = prefix_len; i < cmdline_len; i++) { 969302c1bbSArd Biesheuvel if (!memcmp(&cmdline[i - prefix_len], prefix, prefix_size)) { 979302c1bbSArd Biesheuvel found = true; 989302c1bbSArd Biesheuvel break; 999302c1bbSArd Biesheuvel } 1009302c1bbSArd Biesheuvel } 1019302c1bbSArd Biesheuvel 1029302c1bbSArd Biesheuvel if (!found) 1039302c1bbSArd Biesheuvel return 0; 1049302c1bbSArd Biesheuvel 1057a88a622SPhilipp Fent /* Skip any leading slashes */ 106*c4039b29SDan Carpenter while (i < cmdline_len && (cmdline[i] == L'/' || cmdline[i] == L'\\')) 1077a88a622SPhilipp Fent i++; 1087a88a622SPhilipp Fent 1099302c1bbSArd Biesheuvel while (--result_len > 0 && i < cmdline_len) { 1107a88a622SPhilipp Fent efi_char16_t c = cmdline[i++]; 1117a88a622SPhilipp Fent 1127a88a622SPhilipp Fent if (c == L'\0' || c == L'\n' || c == L' ') 1139302c1bbSArd Biesheuvel break; 1147a88a622SPhilipp Fent else if (c == L'/') 1157a88a622SPhilipp Fent /* Replace UNIX dir separators with EFI standard ones */ 1167a88a622SPhilipp Fent *result++ = L'\\'; 1177a88a622SPhilipp Fent else 1187a88a622SPhilipp Fent *result++ = c; 1199302c1bbSArd Biesheuvel } 1209302c1bbSArd Biesheuvel *result = L'\0'; 1219302c1bbSArd Biesheuvel return i; 1229302c1bbSArd Biesheuvel } 1239302c1bbSArd Biesheuvel 1245193a33dSArd Biesheuvel /* 1255193a33dSArd Biesheuvel * Check the cmdline for a LILO-style file= arguments. 1265193a33dSArd Biesheuvel * 1275193a33dSArd Biesheuvel * We only support loading a file from the same filesystem as 1285193a33dSArd Biesheuvel * the kernel image. 1295193a33dSArd Biesheuvel */ 130cf6b8366SArd Biesheuvel efi_status_t handle_cmdline_files(efi_loaded_image_t *image, 1319302c1bbSArd Biesheuvel const efi_char16_t *optstr, 1329302c1bbSArd Biesheuvel int optstr_size, 13331f5e546SArd Biesheuvel unsigned long soft_limit, 13431f5e546SArd Biesheuvel unsigned long hard_limit, 1355193a33dSArd Biesheuvel unsigned long *load_addr, 1365193a33dSArd Biesheuvel unsigned long *load_size) 1375193a33dSArd Biesheuvel { 1389302c1bbSArd Biesheuvel const efi_char16_t *cmdline = image->load_options; 1394a568ce2SArvind Sankar int cmdline_len = image->load_options_size; 1405193a33dSArd Biesheuvel unsigned long efi_chunk_size = ULONG_MAX; 1419302c1bbSArd Biesheuvel efi_file_protocol_t *volume = NULL; 1429302c1bbSArd Biesheuvel efi_file_protocol_t *file; 1439302c1bbSArd Biesheuvel unsigned long alloc_addr; 1449302c1bbSArd Biesheuvel unsigned long alloc_size; 1455193a33dSArd Biesheuvel efi_status_t status; 1469302c1bbSArd Biesheuvel int offset; 1475193a33dSArd Biesheuvel 1485193a33dSArd Biesheuvel if (!load_addr || !load_size) 1495193a33dSArd Biesheuvel return EFI_INVALID_PARAMETER; 1505193a33dSArd Biesheuvel 1514a568ce2SArvind Sankar efi_apply_loadoptions_quirk((const void **)&cmdline, &cmdline_len); 1524a568ce2SArvind Sankar cmdline_len /= sizeof(*cmdline); 1534a568ce2SArvind Sankar 154980771f6SArd Biesheuvel if (IS_ENABLED(CONFIG_X86) && !efi_nochunk) 1559302c1bbSArd Biesheuvel efi_chunk_size = EFI_READ_CHUNK_SIZE; 1565193a33dSArd Biesheuvel 1579302c1bbSArd Biesheuvel alloc_addr = alloc_size = 0; 1589302c1bbSArd Biesheuvel do { 159464fb126SArd Biesheuvel struct finfo fi; 1609302c1bbSArd Biesheuvel unsigned long size; 1619302c1bbSArd Biesheuvel void *addr; 1625193a33dSArd Biesheuvel 1639302c1bbSArd Biesheuvel offset = find_file_option(cmdline, cmdline_len, 1649302c1bbSArd Biesheuvel optstr, optstr_size, 165464fb126SArd Biesheuvel fi.filename, ARRAY_SIZE(fi.filename)); 1669302c1bbSArd Biesheuvel 1679302c1bbSArd Biesheuvel if (!offset) 1685193a33dSArd Biesheuvel break; 1695193a33dSArd Biesheuvel 1709302c1bbSArd Biesheuvel cmdline += offset; 1719302c1bbSArd Biesheuvel cmdline_len -= offset; 1725193a33dSArd Biesheuvel 1739302c1bbSArd Biesheuvel if (!volume) { 1749302c1bbSArd Biesheuvel status = efi_open_volume(image, &volume); 1755193a33dSArd Biesheuvel if (status != EFI_SUCCESS) 1769302c1bbSArd Biesheuvel return status; 1775193a33dSArd Biesheuvel } 1785193a33dSArd Biesheuvel 179464fb126SArd Biesheuvel status = efi_open_file(volume, &fi, &file, &size); 1805193a33dSArd Biesheuvel if (status != EFI_SUCCESS) 1819302c1bbSArd Biesheuvel goto err_close_volume; 1825193a33dSArd Biesheuvel 1835193a33dSArd Biesheuvel /* 1849302c1bbSArd Biesheuvel * Check whether the existing allocation can contain the next 1859302c1bbSArd Biesheuvel * file. This condition will also trigger naturally during the 1869302c1bbSArd Biesheuvel * first (and typically only) iteration of the loop, given that 1879302c1bbSArd Biesheuvel * alloc_size == 0 in that case. 1885193a33dSArd Biesheuvel */ 1899302c1bbSArd Biesheuvel if (round_up(alloc_size + size, EFI_ALLOC_ALIGN) > 1909302c1bbSArd Biesheuvel round_up(alloc_size, EFI_ALLOC_ALIGN)) { 1919302c1bbSArd Biesheuvel unsigned long old_addr = alloc_addr; 1929302c1bbSArd Biesheuvel 19331f5e546SArd Biesheuvel status = EFI_OUT_OF_RESOURCES; 19431f5e546SArd Biesheuvel if (soft_limit < hard_limit) 19531f5e546SArd Biesheuvel status = efi_allocate_pages(alloc_size + size, 19631f5e546SArd Biesheuvel &alloc_addr, 19731f5e546SArd Biesheuvel soft_limit); 19831f5e546SArd Biesheuvel if (status == EFI_OUT_OF_RESOURCES) 19931f5e546SArd Biesheuvel status = efi_allocate_pages(alloc_size + size, 20031f5e546SArd Biesheuvel &alloc_addr, 20131f5e546SArd Biesheuvel hard_limit); 2025193a33dSArd Biesheuvel if (status != EFI_SUCCESS) { 203793473c2SArvind Sankar efi_err("Failed to allocate memory for files\n"); 2049302c1bbSArd Biesheuvel goto err_close_file; 2055193a33dSArd Biesheuvel } 2065193a33dSArd Biesheuvel 2079302c1bbSArd Biesheuvel if (old_addr != 0) { 2089302c1bbSArd Biesheuvel /* 2099302c1bbSArd Biesheuvel * This is not the first time we've gone 2109302c1bbSArd Biesheuvel * around this loop, and so we are loading 2119302c1bbSArd Biesheuvel * multiple files that need to be concatenated 2129302c1bbSArd Biesheuvel * and returned in a single buffer. 2139302c1bbSArd Biesheuvel */ 2149302c1bbSArd Biesheuvel memcpy((void *)alloc_addr, (void *)old_addr, alloc_size); 2159302c1bbSArd Biesheuvel efi_free(alloc_size, old_addr); 2169302c1bbSArd Biesheuvel } 2175193a33dSArd Biesheuvel } 2185193a33dSArd Biesheuvel 2199302c1bbSArd Biesheuvel addr = (void *)alloc_addr + alloc_size; 2209302c1bbSArd Biesheuvel alloc_size += size; 2215193a33dSArd Biesheuvel 2225193a33dSArd Biesheuvel while (size) { 2239302c1bbSArd Biesheuvel unsigned long chunksize = min(size, efi_chunk_size); 2245193a33dSArd Biesheuvel 2259302c1bbSArd Biesheuvel status = file->read(file, &chunksize, addr); 2265193a33dSArd Biesheuvel if (status != EFI_SUCCESS) { 227793473c2SArvind Sankar efi_err("Failed to read file\n"); 2289302c1bbSArd Biesheuvel goto err_close_file; 2295193a33dSArd Biesheuvel } 2305193a33dSArd Biesheuvel addr += chunksize; 2315193a33dSArd Biesheuvel size -= chunksize; 2325193a33dSArd Biesheuvel } 2339302c1bbSArd Biesheuvel file->close(file); 2349302c1bbSArd Biesheuvel } while (offset > 0); 2355193a33dSArd Biesheuvel 2369302c1bbSArd Biesheuvel *load_addr = alloc_addr; 2379302c1bbSArd Biesheuvel *load_size = alloc_size; 2389302c1bbSArd Biesheuvel 2399302c1bbSArd Biesheuvel if (volume) 2409302c1bbSArd Biesheuvel volume->close(volume); 2419302c1bbSArd Biesheuvel return EFI_SUCCESS; 2429302c1bbSArd Biesheuvel 2439302c1bbSArd Biesheuvel err_close_file: 2449302c1bbSArd Biesheuvel file->close(file); 2459302c1bbSArd Biesheuvel 2469302c1bbSArd Biesheuvel err_close_volume: 2479302c1bbSArd Biesheuvel volume->close(volume); 2489302c1bbSArd Biesheuvel efi_free(alloc_size, alloc_addr); 2499302c1bbSArd Biesheuvel return status; 2505193a33dSArd Biesheuvel } 251