1 // SPDX-License-Identifier: GPL-2.0
3 * Helper functions used by the EFI stub on multiple
4 * architectures. This should be #included by the EFI stub
5 * implementation files.
7 * Copyright 2011 Intel Corporation; author Matt Fleming
10 #include <linux/efi.h>
15 #define MAX_FILENAME_SIZE 256
18 * Some firmware implementations have problems reading files in one go.
19 * A read chunk size of 1MB seems to work for most platforms.
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.
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.
30 #define EFI_READ_CHUNK_SIZE SZ_1M
34 efi_char16_t filename[MAX_FILENAME_SIZE];
37 static efi_status_t efi_open_file(efi_file_protocol_t *volume,
39 efi_file_protocol_t **handle,
40 unsigned long *file_size)
42 efi_guid_t info_guid = EFI_FILE_INFO_ID;
43 efi_file_protocol_t *fh;
44 unsigned long info_sz;
48 /* Replace UNIX dir separators with EFI standard ones */
49 for (c = fi->filename; *c != L'\0'; c++) {
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);
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);
70 *file_size = fi->info.file_size;
74 static efi_status_t efi_open_volume(efi_loaded_image_t *image,
75 efi_file_protocol_t **fh)
77 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
78 efi_simple_file_system_protocol_t *io;
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");
88 status = efi_call_proto(io, open_volume, fh);
89 if (status != EFI_SUCCESS)
90 efi_err("Failed to open volume\n");
95 static 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)
99 int prefix_len = prefix_size / 2;
103 for (i = prefix_len; i < cmdline_len; i++) {
104 if (!memcmp(&cmdline[i - prefix_len], prefix, prefix_size)) {
113 /* Skip any leading slashes */
114 while (i < cmdline_len && (cmdline[i] == L'/' || cmdline[i] == L'\\'))
117 while (--result_len > 0 && i < cmdline_len) {
118 efi_char16_t c = cmdline[i++];
120 if (c == L'\0' || c == L'\n' || c == L' ')
128 static efi_status_t efi_open_device_path(efi_file_protocol_t **volume,
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;
140 /* See if the text to device path protocol exists */
142 efi_bs_call(locate_protocol, &text_to_dp_guid, NULL,
143 (void **)&text_to_dp) != EFI_SUCCESS)
144 return EFI_UNSUPPORTED;
147 /* Convert the filename wide string into a device path */
148 initrd_dp = efi_fn_call(text_to_dp, convert_text_to_device_path,
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))
155 return EFI_NOT_FOUND;
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;
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)));
171 status = efi_call_proto(io, open_volume, volume);
172 if (status != EFI_SUCCESS)
173 efi_err("Failed to open volume\n");
178 #ifndef CONFIG_CMDLINE
179 #define CONFIG_CMDLINE
182 static const efi_char16_t builtin_cmdline[] = L"" CONFIG_CMDLINE;
185 * Check the cmdline for a LILO-style file= arguments.
187 * We only support loading a file from the same filesystem as
190 efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
191 const efi_char16_t *optstr,
193 unsigned long soft_limit,
194 unsigned long hard_limit,
195 unsigned long *load_addr,
196 unsigned long *load_size)
198 const bool ignore_load_options = IS_ENABLED(CONFIG_CMDLINE_OVERRIDE) ||
199 IS_ENABLED(CONFIG_CMDLINE_FORCE);
200 const efi_char16_t *cmdline = efi_table_attr(image, load_options);
201 u32 cmdline_len = efi_table_attr(image, load_options_size);
202 unsigned long efi_chunk_size = ULONG_MAX;
203 efi_file_protocol_t *volume = NULL;
204 efi_file_protocol_t *file;
205 unsigned long alloc_addr;
206 unsigned long alloc_size;
211 if (!load_addr || !load_size)
212 return EFI_INVALID_PARAMETER;
214 efi_apply_loadoptions_quirk((const void **)&cmdline, &cmdline_len);
215 cmdline_len /= sizeof(*cmdline);
217 if (IS_ENABLED(CONFIG_X86) && !efi_nochunk)
218 efi_chunk_size = EFI_READ_CHUNK_SIZE;
220 alloc_addr = alloc_size = 0;
222 if (!ignore_load_options && cmdline_len > 0) {
223 twopass = IS_ENABLED(CONFIG_CMDLINE_BOOL) ||
224 IS_ENABLED(CONFIG_CMDLINE_EXTEND);
226 do_builtin: cmdline = builtin_cmdline;
227 cmdline_len = ARRAY_SIZE(builtin_cmdline) - 1;
236 offset = find_file_option(cmdline, cmdline_len,
238 fi.filename, ARRAY_SIZE(fi.filename));
244 cmdline_len -= offset;
246 status = efi_open_device_path(&volume, &fi);
247 if (status == EFI_UNSUPPORTED || status == EFI_NOT_FOUND)
248 /* try the volume that holds the kernel itself */
249 status = efi_open_volume(image, &volume);
251 if (status != EFI_SUCCESS)
254 status = efi_open_file(volume, &fi, &file, &size);
255 if (status != EFI_SUCCESS)
256 goto err_close_volume;
259 * Check whether the existing allocation can contain the next
260 * file. This condition will also trigger naturally during the
261 * first (and typically only) iteration of the loop, given that
262 * alloc_size == 0 in that case.
264 if (round_up(alloc_size + size, EFI_ALLOC_ALIGN) >
265 round_up(alloc_size, EFI_ALLOC_ALIGN)) {
266 unsigned long old_addr = alloc_addr;
268 status = EFI_OUT_OF_RESOURCES;
269 if (soft_limit < hard_limit)
270 status = efi_allocate_pages(alloc_size + size,
273 if (status == EFI_OUT_OF_RESOURCES)
274 status = efi_allocate_pages(alloc_size + size,
277 if (status != EFI_SUCCESS) {
278 efi_err("Failed to allocate memory for files\n");
284 * This is not the first time we've gone
285 * around this loop, and so we are loading
286 * multiple files that need to be concatenated
287 * and returned in a single buffer.
289 memcpy((void *)alloc_addr, (void *)old_addr, alloc_size);
290 efi_free(alloc_size, old_addr);
294 addr = (void *)alloc_addr + alloc_size;
298 unsigned long chunksize = min(size, efi_chunk_size);
300 status = efi_call_proto(file, read, &chunksize, addr);
301 if (status != EFI_SUCCESS) {
302 efi_err("Failed to read file\n");
308 efi_call_proto(file, close);
309 efi_call_proto(volume, close);
310 } while (offset > 0);
315 *load_addr = alloc_addr;
316 *load_size = alloc_size;
319 return EFI_NOT_READY;
323 efi_call_proto(file, close);
326 efi_call_proto(volume, close);
329 efi_free(alloc_size, alloc_addr);