18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Helper functions used by the EFI stub on multiple 48c2ecf20Sopenharmony_ci * architectures. This should be #included by the EFI stub 58c2ecf20Sopenharmony_ci * implementation files. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Copyright 2011 Intel Corporation; author Matt Fleming 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/efi.h> 118c2ecf20Sopenharmony_ci#include <asm/efi.h> 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include "efistub.h" 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define MAX_FILENAME_SIZE 256 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci/* 188c2ecf20Sopenharmony_ci * Some firmware implementations have problems reading files in one go. 198c2ecf20Sopenharmony_ci * A read chunk size of 1MB seems to work for most platforms. 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * Unfortunately, reading files in chunks triggers *other* bugs on some 228c2ecf20Sopenharmony_ci * platforms, so we provide a way to disable this workaround, which can 238c2ecf20Sopenharmony_ci * be done by passing "efi=nochunk" on the EFI boot stub command line. 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci * If you experience issues with initrd images being corrupt it's worth 268c2ecf20Sopenharmony_ci * trying efi=nochunk, but chunking is enabled by default on x86 because 278c2ecf20Sopenharmony_ci * there are far more machines that require the workaround than those that 288c2ecf20Sopenharmony_ci * break with it enabled. 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ci#define EFI_READ_CHUNK_SIZE SZ_1M 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cistruct finfo { 338c2ecf20Sopenharmony_ci efi_file_info_t info; 348c2ecf20Sopenharmony_ci efi_char16_t filename[MAX_FILENAME_SIZE]; 358c2ecf20Sopenharmony_ci}; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistatic efi_status_t efi_open_file(efi_file_protocol_t *volume, 388c2ecf20Sopenharmony_ci struct finfo *fi, 398c2ecf20Sopenharmony_ci efi_file_protocol_t **handle, 408c2ecf20Sopenharmony_ci unsigned long *file_size) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci efi_guid_t info_guid = EFI_FILE_INFO_ID; 438c2ecf20Sopenharmony_ci efi_file_protocol_t *fh; 448c2ecf20Sopenharmony_ci unsigned long info_sz; 458c2ecf20Sopenharmony_ci efi_status_t status; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci status = volume->open(volume, &fh, fi->filename, EFI_FILE_MODE_READ, 0); 488c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS) { 498c2ecf20Sopenharmony_ci efi_err("Failed to open file: %ls\n", fi->filename); 508c2ecf20Sopenharmony_ci return status; 518c2ecf20Sopenharmony_ci } 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci info_sz = sizeof(struct finfo); 548c2ecf20Sopenharmony_ci status = fh->get_info(fh, &info_guid, &info_sz, fi); 558c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS) { 568c2ecf20Sopenharmony_ci efi_err("Failed to get file info\n"); 578c2ecf20Sopenharmony_ci fh->close(fh); 588c2ecf20Sopenharmony_ci return status; 598c2ecf20Sopenharmony_ci } 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci *handle = fh; 628c2ecf20Sopenharmony_ci *file_size = fi->info.file_size; 638c2ecf20Sopenharmony_ci return EFI_SUCCESS; 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic efi_status_t efi_open_volume(efi_loaded_image_t *image, 678c2ecf20Sopenharmony_ci efi_file_protocol_t **fh) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; 708c2ecf20Sopenharmony_ci efi_simple_file_system_protocol_t *io; 718c2ecf20Sopenharmony_ci efi_status_t status; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci status = efi_bs_call(handle_protocol, image->device_handle, &fs_proto, 748c2ecf20Sopenharmony_ci (void **)&io); 758c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS) { 768c2ecf20Sopenharmony_ci efi_err("Failed to handle fs_proto\n"); 778c2ecf20Sopenharmony_ci return status; 788c2ecf20Sopenharmony_ci } 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci status = io->open_volume(io, fh); 818c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS) 828c2ecf20Sopenharmony_ci efi_err("Failed to open volume\n"); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci return status; 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic int find_file_option(const efi_char16_t *cmdline, int cmdline_len, 888c2ecf20Sopenharmony_ci const efi_char16_t *prefix, int prefix_size, 898c2ecf20Sopenharmony_ci efi_char16_t *result, int result_len) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci int prefix_len = prefix_size / 2; 928c2ecf20Sopenharmony_ci bool found = false; 938c2ecf20Sopenharmony_ci int i; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci for (i = prefix_len; i < cmdline_len; i++) { 968c2ecf20Sopenharmony_ci if (!memcmp(&cmdline[i - prefix_len], prefix, prefix_size)) { 978c2ecf20Sopenharmony_ci found = true; 988c2ecf20Sopenharmony_ci break; 998c2ecf20Sopenharmony_ci } 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci if (!found) 1038c2ecf20Sopenharmony_ci return 0; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci /* Skip any leading slashes */ 1068c2ecf20Sopenharmony_ci while (i < cmdline_len && (cmdline[i] == L'/' || cmdline[i] == L'\\')) 1078c2ecf20Sopenharmony_ci i++; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci while (--result_len > 0 && i < cmdline_len) { 1108c2ecf20Sopenharmony_ci efi_char16_t c = cmdline[i++]; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci if (c == L'\0' || c == L'\n' || c == L' ') 1138c2ecf20Sopenharmony_ci break; 1148c2ecf20Sopenharmony_ci else if (c == L'/') 1158c2ecf20Sopenharmony_ci /* Replace UNIX dir separators with EFI standard ones */ 1168c2ecf20Sopenharmony_ci *result++ = L'\\'; 1178c2ecf20Sopenharmony_ci else 1188c2ecf20Sopenharmony_ci *result++ = c; 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci *result = L'\0'; 1218c2ecf20Sopenharmony_ci return i; 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci/* 1258c2ecf20Sopenharmony_ci * Check the cmdline for a LILO-style file= arguments. 1268c2ecf20Sopenharmony_ci * 1278c2ecf20Sopenharmony_ci * We only support loading a file from the same filesystem as 1288c2ecf20Sopenharmony_ci * the kernel image. 1298c2ecf20Sopenharmony_ci */ 1308c2ecf20Sopenharmony_ciefi_status_t handle_cmdline_files(efi_loaded_image_t *image, 1318c2ecf20Sopenharmony_ci const efi_char16_t *optstr, 1328c2ecf20Sopenharmony_ci int optstr_size, 1338c2ecf20Sopenharmony_ci unsigned long soft_limit, 1348c2ecf20Sopenharmony_ci unsigned long hard_limit, 1358c2ecf20Sopenharmony_ci unsigned long *load_addr, 1368c2ecf20Sopenharmony_ci unsigned long *load_size) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci const efi_char16_t *cmdline = image->load_options; 1398c2ecf20Sopenharmony_ci int cmdline_len = image->load_options_size; 1408c2ecf20Sopenharmony_ci unsigned long efi_chunk_size = ULONG_MAX; 1418c2ecf20Sopenharmony_ci efi_file_protocol_t *volume = NULL; 1428c2ecf20Sopenharmony_ci efi_file_protocol_t *file; 1438c2ecf20Sopenharmony_ci unsigned long alloc_addr; 1448c2ecf20Sopenharmony_ci unsigned long alloc_size; 1458c2ecf20Sopenharmony_ci efi_status_t status; 1468c2ecf20Sopenharmony_ci int offset; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci if (!load_addr || !load_size) 1498c2ecf20Sopenharmony_ci return EFI_INVALID_PARAMETER; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci efi_apply_loadoptions_quirk((const void **)&cmdline, &cmdline_len); 1528c2ecf20Sopenharmony_ci cmdline_len /= sizeof(*cmdline); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_X86) && !efi_nochunk) 1558c2ecf20Sopenharmony_ci efi_chunk_size = EFI_READ_CHUNK_SIZE; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci alloc_addr = alloc_size = 0; 1588c2ecf20Sopenharmony_ci do { 1598c2ecf20Sopenharmony_ci struct finfo fi; 1608c2ecf20Sopenharmony_ci unsigned long size; 1618c2ecf20Sopenharmony_ci void *addr; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci offset = find_file_option(cmdline, cmdline_len, 1648c2ecf20Sopenharmony_ci optstr, optstr_size, 1658c2ecf20Sopenharmony_ci fi.filename, ARRAY_SIZE(fi.filename)); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci if (!offset) 1688c2ecf20Sopenharmony_ci break; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci cmdline += offset; 1718c2ecf20Sopenharmony_ci cmdline_len -= offset; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci if (!volume) { 1748c2ecf20Sopenharmony_ci status = efi_open_volume(image, &volume); 1758c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS) 1768c2ecf20Sopenharmony_ci return status; 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci status = efi_open_file(volume, &fi, &file, &size); 1808c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS) 1818c2ecf20Sopenharmony_ci goto err_close_volume; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci /* 1848c2ecf20Sopenharmony_ci * Check whether the existing allocation can contain the next 1858c2ecf20Sopenharmony_ci * file. This condition will also trigger naturally during the 1868c2ecf20Sopenharmony_ci * first (and typically only) iteration of the loop, given that 1878c2ecf20Sopenharmony_ci * alloc_size == 0 in that case. 1888c2ecf20Sopenharmony_ci */ 1898c2ecf20Sopenharmony_ci if (round_up(alloc_size + size, EFI_ALLOC_ALIGN) > 1908c2ecf20Sopenharmony_ci round_up(alloc_size, EFI_ALLOC_ALIGN)) { 1918c2ecf20Sopenharmony_ci unsigned long old_addr = alloc_addr; 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci status = EFI_OUT_OF_RESOURCES; 1948c2ecf20Sopenharmony_ci if (soft_limit < hard_limit) 1958c2ecf20Sopenharmony_ci status = efi_allocate_pages(alloc_size + size, 1968c2ecf20Sopenharmony_ci &alloc_addr, 1978c2ecf20Sopenharmony_ci soft_limit); 1988c2ecf20Sopenharmony_ci if (status == EFI_OUT_OF_RESOURCES) 1998c2ecf20Sopenharmony_ci status = efi_allocate_pages(alloc_size + size, 2008c2ecf20Sopenharmony_ci &alloc_addr, 2018c2ecf20Sopenharmony_ci hard_limit); 2028c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS) { 2038c2ecf20Sopenharmony_ci efi_err("Failed to allocate memory for files\n"); 2048c2ecf20Sopenharmony_ci goto err_close_file; 2058c2ecf20Sopenharmony_ci } 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci if (old_addr != 0) { 2088c2ecf20Sopenharmony_ci /* 2098c2ecf20Sopenharmony_ci * This is not the first time we've gone 2108c2ecf20Sopenharmony_ci * around this loop, and so we are loading 2118c2ecf20Sopenharmony_ci * multiple files that need to be concatenated 2128c2ecf20Sopenharmony_ci * and returned in a single buffer. 2138c2ecf20Sopenharmony_ci */ 2148c2ecf20Sopenharmony_ci memcpy((void *)alloc_addr, (void *)old_addr, alloc_size); 2158c2ecf20Sopenharmony_ci efi_free(alloc_size, old_addr); 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci } 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci addr = (void *)alloc_addr + alloc_size; 2208c2ecf20Sopenharmony_ci alloc_size += size; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci while (size) { 2238c2ecf20Sopenharmony_ci unsigned long chunksize = min(size, efi_chunk_size); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci status = file->read(file, &chunksize, addr); 2268c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS) { 2278c2ecf20Sopenharmony_ci efi_err("Failed to read file\n"); 2288c2ecf20Sopenharmony_ci goto err_close_file; 2298c2ecf20Sopenharmony_ci } 2308c2ecf20Sopenharmony_ci addr += chunksize; 2318c2ecf20Sopenharmony_ci size -= chunksize; 2328c2ecf20Sopenharmony_ci } 2338c2ecf20Sopenharmony_ci file->close(file); 2348c2ecf20Sopenharmony_ci } while (offset > 0); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci *load_addr = alloc_addr; 2378c2ecf20Sopenharmony_ci *load_size = alloc_size; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci if (volume) 2408c2ecf20Sopenharmony_ci volume->close(volume); 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci if (*load_size == 0) 2438c2ecf20Sopenharmony_ci return EFI_NOT_READY; 2448c2ecf20Sopenharmony_ci return EFI_SUCCESS; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_cierr_close_file: 2478c2ecf20Sopenharmony_ci file->close(file); 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_cierr_close_volume: 2508c2ecf20Sopenharmony_ci volume->close(volume); 2518c2ecf20Sopenharmony_ci efi_free(alloc_size, alloc_addr); 2528c2ecf20Sopenharmony_ci return status; 2538c2ecf20Sopenharmony_ci} 254