1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Helper functions used by the EFI stub on multiple 4 * architectures. This should be #included by the EFI stub 5 * implementation files. 6 * 7 * Copyright 2011 Intel Corporation; author Matt Fleming 8 */ 9 10 #include <linux/efi.h> 11 #include <asm/efi.h> 12 13 #include "efistub.h" 14 15 #define MAX_FILENAME_SIZE 256 16 17 /* 18 * Some firmware implementations have problems reading files in one go. 19 * A read chunk size of 1MB seems to work for most platforms. 20 * 21 * Unfortunately, reading files in chunks triggers *other* bugs on some 22 * platforms, so we provide a way to disable this workaround, which can 23 * be done by passing "efi=nochunk" on the EFI boot stub command line. 24 * 25 * If you experience issues with initrd images being corrupt it's worth 26 * trying efi=nochunk, but chunking is enabled by default on x86 because 27 * there are far more machines that require the workaround than those that 28 * break with it enabled. 29 */ 30 #define EFI_READ_CHUNK_SIZE SZ_1M 31 32 struct finfo { 33 efi_file_info_t info; 34 efi_char16_t filename[MAX_FILENAME_SIZE]; 35 }; 36 37 static efi_status_t efi_open_file(efi_file_protocol_t *volume, 38 struct finfo *fi, 39 efi_file_protocol_t **handle, 40 unsigned long *file_size) 41 { 42 efi_guid_t info_guid = EFI_FILE_INFO_ID; 43 efi_file_protocol_t *fh; 44 unsigned long info_sz; 45 efi_status_t status; 46 47 status = volume->open(volume, &fh, fi->filename, EFI_FILE_MODE_READ, 0); 48 if (status != EFI_SUCCESS) { 49 efi_err("Failed to open file: %ls\n", fi->filename); 50 return status; 51 } 52 53 info_sz = sizeof(struct finfo); 54 status = fh->get_info(fh, &info_guid, &info_sz, fi); 55 if (status != EFI_SUCCESS) { 56 efi_err("Failed to get file info\n"); 57 fh->close(fh); 58 return status; 59 } 60 61 *handle = fh; 62 *file_size = fi->info.file_size; 63 return EFI_SUCCESS; 64 } 65 66 static efi_status_t efi_open_volume(efi_loaded_image_t *image, 67 efi_file_protocol_t **fh) 68 { 69 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; 70 efi_simple_file_system_protocol_t *io; 71 efi_status_t status; 72 73 status = efi_bs_call(handle_protocol, image->device_handle, &fs_proto, 74 (void **)&io); 75 if (status != EFI_SUCCESS) { 76 efi_err("Failed to handle fs_proto\n"); 77 return status; 78 } 79 80 status = io->open_volume(io, fh); 81 if (status != EFI_SUCCESS) 82 efi_err("Failed to open volume\n"); 83 84 return status; 85 } 86 87 static int find_file_option(const efi_char16_t *cmdline, int cmdline_len, 88 const efi_char16_t *prefix, int prefix_size, 89 efi_char16_t *result, int result_len) 90 { 91 int prefix_len = prefix_size / 2; 92 bool found = false; 93 int i; 94 95 for (i = prefix_len; i < cmdline_len; i++) { 96 if (!memcmp(&cmdline[i - prefix_len], prefix, prefix_size)) { 97 found = true; 98 break; 99 } 100 } 101 102 if (!found) 103 return 0; 104 105 while (--result_len > 0 && i < cmdline_len) { 106 if (cmdline[i] == L'\0' || 107 cmdline[i] == L'\n' || 108 cmdline[i] == L' ') 109 break; 110 *result++ = cmdline[i++]; 111 } 112 *result = L'\0'; 113 return i; 114 } 115 116 /* 117 * Check the cmdline for a LILO-style file= arguments. 118 * 119 * We only support loading a file from the same filesystem as 120 * the kernel image. 121 */ 122 efi_status_t handle_cmdline_files(efi_loaded_image_t *image, 123 const efi_char16_t *optstr, 124 int optstr_size, 125 unsigned long soft_limit, 126 unsigned long hard_limit, 127 unsigned long *load_addr, 128 unsigned long *load_size) 129 { 130 const efi_char16_t *cmdline = image->load_options; 131 int cmdline_len = image->load_options_size / 2; 132 unsigned long efi_chunk_size = ULONG_MAX; 133 efi_file_protocol_t *volume = NULL; 134 efi_file_protocol_t *file; 135 unsigned long alloc_addr; 136 unsigned long alloc_size; 137 efi_status_t status; 138 int offset; 139 140 if (!load_addr || !load_size) 141 return EFI_INVALID_PARAMETER; 142 143 if (IS_ENABLED(CONFIG_X86) && !efi_nochunk) 144 efi_chunk_size = EFI_READ_CHUNK_SIZE; 145 146 alloc_addr = alloc_size = 0; 147 do { 148 struct finfo fi; 149 unsigned long size; 150 void *addr; 151 152 offset = find_file_option(cmdline, cmdline_len, 153 optstr, optstr_size, 154 fi.filename, ARRAY_SIZE(fi.filename)); 155 156 if (!offset) 157 break; 158 159 cmdline += offset; 160 cmdline_len -= offset; 161 162 if (!volume) { 163 status = efi_open_volume(image, &volume); 164 if (status != EFI_SUCCESS) 165 return status; 166 } 167 168 status = efi_open_file(volume, &fi, &file, &size); 169 if (status != EFI_SUCCESS) 170 goto err_close_volume; 171 172 /* 173 * Check whether the existing allocation can contain the next 174 * file. This condition will also trigger naturally during the 175 * first (and typically only) iteration of the loop, given that 176 * alloc_size == 0 in that case. 177 */ 178 if (round_up(alloc_size + size, EFI_ALLOC_ALIGN) > 179 round_up(alloc_size, EFI_ALLOC_ALIGN)) { 180 unsigned long old_addr = alloc_addr; 181 182 status = EFI_OUT_OF_RESOURCES; 183 if (soft_limit < hard_limit) 184 status = efi_allocate_pages(alloc_size + size, 185 &alloc_addr, 186 soft_limit); 187 if (status == EFI_OUT_OF_RESOURCES) 188 status = efi_allocate_pages(alloc_size + size, 189 &alloc_addr, 190 hard_limit); 191 if (status != EFI_SUCCESS) { 192 efi_err("Failed to allocate memory for files\n"); 193 goto err_close_file; 194 } 195 196 if (old_addr != 0) { 197 /* 198 * This is not the first time we've gone 199 * around this loop, and so we are loading 200 * multiple files that need to be concatenated 201 * and returned in a single buffer. 202 */ 203 memcpy((void *)alloc_addr, (void *)old_addr, alloc_size); 204 efi_free(alloc_size, old_addr); 205 } 206 } 207 208 addr = (void *)alloc_addr + alloc_size; 209 alloc_size += size; 210 211 while (size) { 212 unsigned long chunksize = min(size, efi_chunk_size); 213 214 status = file->read(file, &chunksize, addr); 215 if (status != EFI_SUCCESS) { 216 efi_err("Failed to read file\n"); 217 goto err_close_file; 218 } 219 addr += chunksize; 220 size -= chunksize; 221 } 222 file->close(file); 223 } while (offset > 0); 224 225 *load_addr = alloc_addr; 226 *load_size = alloc_size; 227 228 if (volume) 229 volume->close(volume); 230 return EFI_SUCCESS; 231 232 err_close_file: 233 file->close(file); 234 235 err_close_volume: 236 volume->close(volume); 237 efi_free(alloc_size, alloc_addr); 238 return status; 239 } 240