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