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 efi_char16_t *c; 47 48 /* Replace UNIX dir separators with EFI standard ones */ 49 for (c = fi->filename; *c != L'\0'; c++) { 50 if (*c == L'/') 51 *c = L'\\'; 52 } 53 54 status = volume->open(volume, &fh, fi->filename, EFI_FILE_MODE_READ, 0); 55 if (status != EFI_SUCCESS) { 56 efi_err("Failed to open file: %ls\n", fi->filename); 57 return status; 58 } 59 60 info_sz = sizeof(struct finfo); 61 status = fh->get_info(fh, &info_guid, &info_sz, fi); 62 if (status != EFI_SUCCESS) { 63 efi_err("Failed to get file info\n"); 64 fh->close(fh); 65 return status; 66 } 67 68 *handle = fh; 69 *file_size = fi->info.file_size; 70 return EFI_SUCCESS; 71 } 72 73 static efi_status_t efi_open_volume(efi_loaded_image_t *image, 74 efi_file_protocol_t **fh) 75 { 76 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; 77 efi_simple_file_system_protocol_t *io; 78 efi_status_t status; 79 80 status = efi_bs_call(handle_protocol, image->device_handle, &fs_proto, 81 (void **)&io); 82 if (status != EFI_SUCCESS) { 83 efi_err("Failed to handle fs_proto\n"); 84 return status; 85 } 86 87 status = io->open_volume(io, fh); 88 if (status != EFI_SUCCESS) 89 efi_err("Failed to open volume\n"); 90 91 return status; 92 } 93 94 static int find_file_option(const efi_char16_t *cmdline, int cmdline_len, 95 const efi_char16_t *prefix, int prefix_size, 96 efi_char16_t *result, int result_len) 97 { 98 int prefix_len = prefix_size / 2; 99 bool found = false; 100 int i; 101 102 for (i = prefix_len; i < cmdline_len; i++) { 103 if (!memcmp(&cmdline[i - prefix_len], prefix, prefix_size)) { 104 found = true; 105 break; 106 } 107 } 108 109 if (!found) 110 return 0; 111 112 /* Skip any leading slashes */ 113 while (i < cmdline_len && (cmdline[i] == L'/' || cmdline[i] == L'\\')) 114 i++; 115 116 while (--result_len > 0 && i < cmdline_len) { 117 efi_char16_t c = cmdline[i++]; 118 119 if (c == L'\0' || c == L'\n' || c == L' ') 120 break; 121 *result++ = c; 122 } 123 *result = L'\0'; 124 return i; 125 } 126 127 static efi_status_t efi_open_device_path(efi_file_protocol_t **volume, 128 struct finfo *fi) 129 { 130 efi_guid_t text_to_dp_guid = EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL_GUID; 131 static efi_device_path_from_text_protocol_t *text_to_dp = NULL; 132 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; 133 efi_device_path_protocol_t *initrd_dp; 134 efi_simple_file_system_protocol_t *io; 135 struct efi_file_path_dev_path *fpath; 136 efi_handle_t handle; 137 efi_status_t status; 138 139 /* See if the text to device path protocol exists */ 140 if (!text_to_dp && 141 efi_bs_call(locate_protocol, &text_to_dp_guid, NULL, 142 (void **)&text_to_dp) != EFI_SUCCESS) 143 return EFI_UNSUPPORTED; 144 145 146 /* Convert the filename wide string into a device path */ 147 initrd_dp = text_to_dp->convert_text_to_device_path(fi->filename); 148 149 /* Check whether the device path in question implements simple FS */ 150 if ((efi_bs_call(locate_device_path, &fs_proto, &initrd_dp, &handle) ?: 151 efi_bs_call(handle_protocol, handle, &fs_proto, (void **)&io)) 152 != EFI_SUCCESS) 153 return EFI_NOT_FOUND; 154 155 /* Check whether the remaining device path is a file device path */ 156 if (initrd_dp->type != EFI_DEV_MEDIA || 157 initrd_dp->sub_type != EFI_DEV_MEDIA_FILE) { 158 efi_warn("Unexpected device path node type: (%x, %x)\n", 159 initrd_dp->type, initrd_dp->sub_type); 160 return EFI_LOAD_ERROR; 161 } 162 163 /* Copy the remaining file path into the fi structure */ 164 fpath = (struct efi_file_path_dev_path *)initrd_dp; 165 memcpy(fi->filename, fpath->filename, 166 min(sizeof(fi->filename), 167 fpath->header.length - sizeof(fpath->header))); 168 169 status = io->open_volume(io, volume); 170 if (status != EFI_SUCCESS) 171 efi_err("Failed to open volume\n"); 172 173 return status; 174 } 175 176 /* 177 * Check the cmdline for a LILO-style file= arguments. 178 * 179 * We only support loading a file from the same filesystem as 180 * the kernel image. 181 */ 182 efi_status_t handle_cmdline_files(efi_loaded_image_t *image, 183 const efi_char16_t *optstr, 184 int optstr_size, 185 unsigned long soft_limit, 186 unsigned long hard_limit, 187 unsigned long *load_addr, 188 unsigned long *load_size) 189 { 190 const efi_char16_t *cmdline = image->load_options; 191 u32 cmdline_len = image->load_options_size; 192 unsigned long efi_chunk_size = ULONG_MAX; 193 efi_file_protocol_t *volume = NULL; 194 efi_file_protocol_t *file; 195 unsigned long alloc_addr; 196 unsigned long alloc_size; 197 efi_status_t status; 198 int offset; 199 200 if (!load_addr || !load_size) 201 return EFI_INVALID_PARAMETER; 202 203 efi_apply_loadoptions_quirk((const void **)&cmdline, &cmdline_len); 204 cmdline_len /= sizeof(*cmdline); 205 206 if (IS_ENABLED(CONFIG_X86) && !efi_nochunk) 207 efi_chunk_size = EFI_READ_CHUNK_SIZE; 208 209 alloc_addr = alloc_size = 0; 210 do { 211 struct finfo fi; 212 unsigned long size; 213 void *addr; 214 215 offset = find_file_option(cmdline, cmdline_len, 216 optstr, optstr_size, 217 fi.filename, ARRAY_SIZE(fi.filename)); 218 219 if (!offset) 220 break; 221 222 cmdline += offset; 223 cmdline_len -= offset; 224 225 status = efi_open_device_path(&volume, &fi); 226 if (status == EFI_UNSUPPORTED || status == EFI_NOT_FOUND) 227 /* try the volume that holds the kernel itself */ 228 status = efi_open_volume(image, &volume); 229 230 if (status != EFI_SUCCESS) 231 goto err_free_alloc; 232 233 status = efi_open_file(volume, &fi, &file, &size); 234 if (status != EFI_SUCCESS) 235 goto err_close_volume; 236 237 /* 238 * Check whether the existing allocation can contain the next 239 * file. This condition will also trigger naturally during the 240 * first (and typically only) iteration of the loop, given that 241 * alloc_size == 0 in that case. 242 */ 243 if (round_up(alloc_size + size, EFI_ALLOC_ALIGN) > 244 round_up(alloc_size, EFI_ALLOC_ALIGN)) { 245 unsigned long old_addr = alloc_addr; 246 247 status = EFI_OUT_OF_RESOURCES; 248 if (soft_limit < hard_limit) 249 status = efi_allocate_pages(alloc_size + size, 250 &alloc_addr, 251 soft_limit); 252 if (status == EFI_OUT_OF_RESOURCES) 253 status = efi_allocate_pages(alloc_size + size, 254 &alloc_addr, 255 hard_limit); 256 if (status != EFI_SUCCESS) { 257 efi_err("Failed to allocate memory for files\n"); 258 goto err_close_file; 259 } 260 261 if (old_addr != 0) { 262 /* 263 * This is not the first time we've gone 264 * around this loop, and so we are loading 265 * multiple files that need to be concatenated 266 * and returned in a single buffer. 267 */ 268 memcpy((void *)alloc_addr, (void *)old_addr, alloc_size); 269 efi_free(alloc_size, old_addr); 270 } 271 } 272 273 addr = (void *)alloc_addr + alloc_size; 274 alloc_size += size; 275 276 while (size) { 277 unsigned long chunksize = min(size, efi_chunk_size); 278 279 status = file->read(file, &chunksize, addr); 280 if (status != EFI_SUCCESS) { 281 efi_err("Failed to read file\n"); 282 goto err_close_file; 283 } 284 addr += chunksize; 285 size -= chunksize; 286 } 287 file->close(file); 288 volume->close(volume); 289 } while (offset > 0); 290 291 *load_addr = alloc_addr; 292 *load_size = alloc_size; 293 294 if (*load_size == 0) 295 return EFI_NOT_READY; 296 return EFI_SUCCESS; 297 298 err_close_file: 299 file->close(file); 300 301 err_close_volume: 302 volume->close(volume); 303 304 err_free_alloc: 305 efi_free(alloc_size, alloc_addr); 306 return status; 307 } 308