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
efi_open_file(efi_file_protocol_t * volume,struct finfo * fi,efi_file_protocol_t ** handle,unsigned long * file_size)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;
4670912985SArd Biesheuvel efi_char16_t *c;
4770912985SArd Biesheuvel
4870912985SArd Biesheuvel /* Replace UNIX dir separators with EFI standard ones */
4970912985SArd Biesheuvel for (c = fi->filename; *c != L'\0'; c++) {
5070912985SArd Biesheuvel if (*c == L'/')
5170912985SArd Biesheuvel *c = L'\\';
5270912985SArd Biesheuvel }
535193a33dSArd Biesheuvel
54*f8a31244SArd Biesheuvel status = efi_call_proto(volume, open, &fh, fi->filename,
55*f8a31244SArd Biesheuvel EFI_FILE_MODE_READ, 0);
565193a33dSArd Biesheuvel if (status != EFI_SUCCESS) {
57a713979eSArvind Sankar efi_err("Failed to open file: %ls\n", fi->filename);
585193a33dSArd Biesheuvel return status;
595193a33dSArd Biesheuvel }
605193a33dSArd Biesheuvel
61464fb126SArd Biesheuvel info_sz = sizeof(struct finfo);
62*f8a31244SArd Biesheuvel status = efi_call_proto(fh, get_info, &info_guid, &info_sz, fi);
635193a33dSArd Biesheuvel if (status != EFI_SUCCESS) {
64793473c2SArvind Sankar efi_err("Failed to get file info\n");
65*f8a31244SArd Biesheuvel efi_call_proto(fh, close);
665193a33dSArd Biesheuvel return status;
675193a33dSArd Biesheuvel }
685193a33dSArd Biesheuvel
699302c1bbSArd Biesheuvel *handle = fh;
70464fb126SArd Biesheuvel *file_size = fi->info.file_size;
719302c1bbSArd Biesheuvel return EFI_SUCCESS;
725193a33dSArd Biesheuvel }
735193a33dSArd Biesheuvel
efi_open_volume(efi_loaded_image_t * image,efi_file_protocol_t ** fh)745193a33dSArd Biesheuvel static efi_status_t efi_open_volume(efi_loaded_image_t *image,
759302c1bbSArd Biesheuvel efi_file_protocol_t **fh)
765193a33dSArd Biesheuvel {
775193a33dSArd Biesheuvel efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
789302c1bbSArd Biesheuvel efi_simple_file_system_protocol_t *io;
795193a33dSArd Biesheuvel efi_status_t status;
805193a33dSArd Biesheuvel
81*f8a31244SArd Biesheuvel status = efi_bs_call(handle_protocol, efi_table_attr(image, device_handle),
82*f8a31244SArd Biesheuvel &fs_proto, (void **)&io);
835193a33dSArd Biesheuvel if (status != EFI_SUCCESS) {
84793473c2SArvind Sankar efi_err("Failed to handle fs_proto\n");
855193a33dSArd Biesheuvel return status;
865193a33dSArd Biesheuvel }
875193a33dSArd Biesheuvel
88*f8a31244SArd Biesheuvel status = efi_call_proto(io, open_volume, fh);
895193a33dSArd Biesheuvel if (status != EFI_SUCCESS)
90793473c2SArvind Sankar efi_err("Failed to open volume\n");
915193a33dSArd Biesheuvel
925193a33dSArd Biesheuvel return status;
935193a33dSArd Biesheuvel }
945193a33dSArd Biesheuvel
find_file_option(const efi_char16_t * cmdline,int cmdline_len,const efi_char16_t * prefix,int prefix_size,efi_char16_t * result,int result_len)959302c1bbSArd Biesheuvel static int find_file_option(const efi_char16_t *cmdline, int cmdline_len,
969302c1bbSArd Biesheuvel const efi_char16_t *prefix, int prefix_size,
979302c1bbSArd Biesheuvel efi_char16_t *result, int result_len)
989302c1bbSArd Biesheuvel {
999302c1bbSArd Biesheuvel int prefix_len = prefix_size / 2;
1009302c1bbSArd Biesheuvel bool found = false;
1019302c1bbSArd Biesheuvel int i;
1029302c1bbSArd Biesheuvel
1039302c1bbSArd Biesheuvel for (i = prefix_len; i < cmdline_len; i++) {
1049302c1bbSArd Biesheuvel if (!memcmp(&cmdline[i - prefix_len], prefix, prefix_size)) {
1059302c1bbSArd Biesheuvel found = true;
1069302c1bbSArd Biesheuvel break;
1079302c1bbSArd Biesheuvel }
1089302c1bbSArd Biesheuvel }
1099302c1bbSArd Biesheuvel
1109302c1bbSArd Biesheuvel if (!found)
1119302c1bbSArd Biesheuvel return 0;
1129302c1bbSArd Biesheuvel
1137a88a622SPhilipp Fent /* Skip any leading slashes */
114c4039b29SDan Carpenter while (i < cmdline_len && (cmdline[i] == L'/' || cmdline[i] == L'\\'))
1157a88a622SPhilipp Fent i++;
1167a88a622SPhilipp Fent
1179302c1bbSArd Biesheuvel while (--result_len > 0 && i < cmdline_len) {
1187a88a622SPhilipp Fent efi_char16_t c = cmdline[i++];
1197a88a622SPhilipp Fent
1207a88a622SPhilipp Fent if (c == L'\0' || c == L'\n' || c == L' ')
1219302c1bbSArd Biesheuvel break;
1227a88a622SPhilipp Fent *result++ = c;
1239302c1bbSArd Biesheuvel }
1249302c1bbSArd Biesheuvel *result = L'\0';
1259302c1bbSArd Biesheuvel return i;
1269302c1bbSArd Biesheuvel }
1279302c1bbSArd Biesheuvel
efi_open_device_path(efi_file_protocol_t ** volume,struct finfo * fi)12870912985SArd Biesheuvel static efi_status_t efi_open_device_path(efi_file_protocol_t **volume,
12970912985SArd Biesheuvel struct finfo *fi)
13070912985SArd Biesheuvel {
13170912985SArd Biesheuvel efi_guid_t text_to_dp_guid = EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL_GUID;
13270912985SArd Biesheuvel static efi_device_path_from_text_protocol_t *text_to_dp = NULL;
13370912985SArd Biesheuvel efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
13470912985SArd Biesheuvel efi_device_path_protocol_t *initrd_dp;
13570912985SArd Biesheuvel efi_simple_file_system_protocol_t *io;
13670912985SArd Biesheuvel struct efi_file_path_dev_path *fpath;
13770912985SArd Biesheuvel efi_handle_t handle;
13870912985SArd Biesheuvel efi_status_t status;
13970912985SArd Biesheuvel
14070912985SArd Biesheuvel /* See if the text to device path protocol exists */
14170912985SArd Biesheuvel if (!text_to_dp &&
14270912985SArd Biesheuvel efi_bs_call(locate_protocol, &text_to_dp_guid, NULL,
14370912985SArd Biesheuvel (void **)&text_to_dp) != EFI_SUCCESS)
14470912985SArd Biesheuvel return EFI_UNSUPPORTED;
14570912985SArd Biesheuvel
14670912985SArd Biesheuvel
14770912985SArd Biesheuvel /* Convert the filename wide string into a device path */
148*f8a31244SArd Biesheuvel initrd_dp = efi_fn_call(text_to_dp, convert_text_to_device_path,
149*f8a31244SArd Biesheuvel fi->filename);
15070912985SArd Biesheuvel
15170912985SArd Biesheuvel /* Check whether the device path in question implements simple FS */
15270912985SArd Biesheuvel if ((efi_bs_call(locate_device_path, &fs_proto, &initrd_dp, &handle) ?:
15370912985SArd Biesheuvel efi_bs_call(handle_protocol, handle, &fs_proto, (void **)&io))
15470912985SArd Biesheuvel != EFI_SUCCESS)
15570912985SArd Biesheuvel return EFI_NOT_FOUND;
15670912985SArd Biesheuvel
15770912985SArd Biesheuvel /* Check whether the remaining device path is a file device path */
15870912985SArd Biesheuvel if (initrd_dp->type != EFI_DEV_MEDIA ||
15970912985SArd Biesheuvel initrd_dp->sub_type != EFI_DEV_MEDIA_FILE) {
16070912985SArd Biesheuvel efi_warn("Unexpected device path node type: (%x, %x)\n",
16170912985SArd Biesheuvel initrd_dp->type, initrd_dp->sub_type);
16270912985SArd Biesheuvel return EFI_LOAD_ERROR;
16370912985SArd Biesheuvel }
16470912985SArd Biesheuvel
16570912985SArd Biesheuvel /* Copy the remaining file path into the fi structure */
16670912985SArd Biesheuvel fpath = (struct efi_file_path_dev_path *)initrd_dp;
16770912985SArd Biesheuvel memcpy(fi->filename, fpath->filename,
16870912985SArd Biesheuvel min(sizeof(fi->filename),
16970912985SArd Biesheuvel fpath->header.length - sizeof(fpath->header)));
17070912985SArd Biesheuvel
171*f8a31244SArd Biesheuvel status = efi_call_proto(io, open_volume, volume);
17270912985SArd Biesheuvel if (status != EFI_SUCCESS)
17370912985SArd Biesheuvel efi_err("Failed to open volume\n");
17470912985SArd Biesheuvel
17570912985SArd Biesheuvel return status;
17670912985SArd Biesheuvel }
17770912985SArd Biesheuvel
1785193a33dSArd Biesheuvel /*
1795193a33dSArd Biesheuvel * Check the cmdline for a LILO-style file= arguments.
1805193a33dSArd Biesheuvel *
1815193a33dSArd Biesheuvel * We only support loading a file from the same filesystem as
1825193a33dSArd Biesheuvel * the kernel image.
1835193a33dSArd Biesheuvel */
handle_cmdline_files(efi_loaded_image_t * image,const efi_char16_t * optstr,int optstr_size,unsigned long soft_limit,unsigned long hard_limit,unsigned long * load_addr,unsigned long * load_size)184cf6b8366SArd Biesheuvel efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
1859302c1bbSArd Biesheuvel const efi_char16_t *optstr,
1869302c1bbSArd Biesheuvel int optstr_size,
18731f5e546SArd Biesheuvel unsigned long soft_limit,
18831f5e546SArd Biesheuvel unsigned long hard_limit,
1895193a33dSArd Biesheuvel unsigned long *load_addr,
1905193a33dSArd Biesheuvel unsigned long *load_size)
1915193a33dSArd Biesheuvel {
192*f8a31244SArd Biesheuvel const efi_char16_t *cmdline = efi_table_attr(image, load_options);
193*f8a31244SArd Biesheuvel u32 cmdline_len = efi_table_attr(image, load_options_size);
1945193a33dSArd Biesheuvel unsigned long efi_chunk_size = ULONG_MAX;
1959302c1bbSArd Biesheuvel efi_file_protocol_t *volume = NULL;
1969302c1bbSArd Biesheuvel efi_file_protocol_t *file;
1979302c1bbSArd Biesheuvel unsigned long alloc_addr;
1989302c1bbSArd Biesheuvel unsigned long alloc_size;
1995193a33dSArd Biesheuvel efi_status_t status;
2009302c1bbSArd Biesheuvel int offset;
2015193a33dSArd Biesheuvel
2025193a33dSArd Biesheuvel if (!load_addr || !load_size)
2035193a33dSArd Biesheuvel return EFI_INVALID_PARAMETER;
2045193a33dSArd Biesheuvel
2054a568ce2SArvind Sankar efi_apply_loadoptions_quirk((const void **)&cmdline, &cmdline_len);
2064a568ce2SArvind Sankar cmdline_len /= sizeof(*cmdline);
2074a568ce2SArvind Sankar
208980771f6SArd Biesheuvel if (IS_ENABLED(CONFIG_X86) && !efi_nochunk)
2099302c1bbSArd Biesheuvel efi_chunk_size = EFI_READ_CHUNK_SIZE;
2105193a33dSArd Biesheuvel
2119302c1bbSArd Biesheuvel alloc_addr = alloc_size = 0;
2129302c1bbSArd Biesheuvel do {
213464fb126SArd Biesheuvel struct finfo fi;
2149302c1bbSArd Biesheuvel unsigned long size;
2159302c1bbSArd Biesheuvel void *addr;
2165193a33dSArd Biesheuvel
2179302c1bbSArd Biesheuvel offset = find_file_option(cmdline, cmdline_len,
2189302c1bbSArd Biesheuvel optstr, optstr_size,
219464fb126SArd Biesheuvel fi.filename, ARRAY_SIZE(fi.filename));
2209302c1bbSArd Biesheuvel
2219302c1bbSArd Biesheuvel if (!offset)
2225193a33dSArd Biesheuvel break;
2235193a33dSArd Biesheuvel
2249302c1bbSArd Biesheuvel cmdline += offset;
2259302c1bbSArd Biesheuvel cmdline_len -= offset;
2265193a33dSArd Biesheuvel
22770912985SArd Biesheuvel status = efi_open_device_path(&volume, &fi);
22870912985SArd Biesheuvel if (status == EFI_UNSUPPORTED || status == EFI_NOT_FOUND)
22970912985SArd Biesheuvel /* try the volume that holds the kernel itself */
2309302c1bbSArd Biesheuvel status = efi_open_volume(image, &volume);
23170912985SArd Biesheuvel
2325193a33dSArd Biesheuvel if (status != EFI_SUCCESS)
23370912985SArd Biesheuvel goto err_free_alloc;
2345193a33dSArd Biesheuvel
235464fb126SArd Biesheuvel status = efi_open_file(volume, &fi, &file, &size);
2365193a33dSArd Biesheuvel if (status != EFI_SUCCESS)
2379302c1bbSArd Biesheuvel goto err_close_volume;
2385193a33dSArd Biesheuvel
2395193a33dSArd Biesheuvel /*
2409302c1bbSArd Biesheuvel * Check whether the existing allocation can contain the next
2419302c1bbSArd Biesheuvel * file. This condition will also trigger naturally during the
2429302c1bbSArd Biesheuvel * first (and typically only) iteration of the loop, given that
2439302c1bbSArd Biesheuvel * alloc_size == 0 in that case.
2445193a33dSArd Biesheuvel */
2459302c1bbSArd Biesheuvel if (round_up(alloc_size + size, EFI_ALLOC_ALIGN) >
2469302c1bbSArd Biesheuvel round_up(alloc_size, EFI_ALLOC_ALIGN)) {
2479302c1bbSArd Biesheuvel unsigned long old_addr = alloc_addr;
2489302c1bbSArd Biesheuvel
24931f5e546SArd Biesheuvel status = EFI_OUT_OF_RESOURCES;
25031f5e546SArd Biesheuvel if (soft_limit < hard_limit)
25131f5e546SArd Biesheuvel status = efi_allocate_pages(alloc_size + size,
25231f5e546SArd Biesheuvel &alloc_addr,
25331f5e546SArd Biesheuvel soft_limit);
25431f5e546SArd Biesheuvel if (status == EFI_OUT_OF_RESOURCES)
25531f5e546SArd Biesheuvel status = efi_allocate_pages(alloc_size + size,
25631f5e546SArd Biesheuvel &alloc_addr,
25731f5e546SArd Biesheuvel hard_limit);
2585193a33dSArd Biesheuvel if (status != EFI_SUCCESS) {
259793473c2SArvind Sankar efi_err("Failed to allocate memory for files\n");
2609302c1bbSArd Biesheuvel goto err_close_file;
2615193a33dSArd Biesheuvel }
2625193a33dSArd Biesheuvel
2639302c1bbSArd Biesheuvel if (old_addr != 0) {
2649302c1bbSArd Biesheuvel /*
2659302c1bbSArd Biesheuvel * This is not the first time we've gone
2669302c1bbSArd Biesheuvel * around this loop, and so we are loading
2679302c1bbSArd Biesheuvel * multiple files that need to be concatenated
2689302c1bbSArd Biesheuvel * and returned in a single buffer.
2699302c1bbSArd Biesheuvel */
2709302c1bbSArd Biesheuvel memcpy((void *)alloc_addr, (void *)old_addr, alloc_size);
2719302c1bbSArd Biesheuvel efi_free(alloc_size, old_addr);
2729302c1bbSArd Biesheuvel }
2735193a33dSArd Biesheuvel }
2745193a33dSArd Biesheuvel
2759302c1bbSArd Biesheuvel addr = (void *)alloc_addr + alloc_size;
2769302c1bbSArd Biesheuvel alloc_size += size;
2775193a33dSArd Biesheuvel
2785193a33dSArd Biesheuvel while (size) {
2799302c1bbSArd Biesheuvel unsigned long chunksize = min(size, efi_chunk_size);
2805193a33dSArd Biesheuvel
281*f8a31244SArd Biesheuvel status = efi_call_proto(file, read, &chunksize, addr);
2825193a33dSArd Biesheuvel if (status != EFI_SUCCESS) {
283793473c2SArvind Sankar efi_err("Failed to read file\n");
2849302c1bbSArd Biesheuvel goto err_close_file;
2855193a33dSArd Biesheuvel }
2865193a33dSArd Biesheuvel addr += chunksize;
2875193a33dSArd Biesheuvel size -= chunksize;
2885193a33dSArd Biesheuvel }
289*f8a31244SArd Biesheuvel efi_call_proto(file, close);
290*f8a31244SArd Biesheuvel efi_call_proto(volume, close);
2919302c1bbSArd Biesheuvel } while (offset > 0);
2925193a33dSArd Biesheuvel
2939302c1bbSArd Biesheuvel *load_addr = alloc_addr;
2949302c1bbSArd Biesheuvel *load_size = alloc_size;
2959302c1bbSArd Biesheuvel
296f4dc7fffSArd Biesheuvel if (*load_size == 0)
297f4dc7fffSArd Biesheuvel return EFI_NOT_READY;
2989302c1bbSArd Biesheuvel return EFI_SUCCESS;
2999302c1bbSArd Biesheuvel
3009302c1bbSArd Biesheuvel err_close_file:
301*f8a31244SArd Biesheuvel efi_call_proto(file, close);
3029302c1bbSArd Biesheuvel
3039302c1bbSArd Biesheuvel err_close_volume:
304*f8a31244SArd Biesheuvel efi_call_proto(volume, close);
30570912985SArd Biesheuvel
30670912985SArd Biesheuvel err_free_alloc:
3079302c1bbSArd Biesheuvel efi_free(alloc_size, alloc_addr);
3089302c1bbSArd Biesheuvel return status;
3095193a33dSArd Biesheuvel }
310