]> Git Repo - linux.git/blob - drivers/firmware/efi/libstub/file.c
Linux 6.14-rc3
[linux.git] / drivers / firmware / efi / libstub / file.c
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 = 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
74 static 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
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)
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
128 static 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 #ifndef CONFIG_CMDLINE
179 #define CONFIG_CMDLINE
180 #endif
181
182 static const efi_char16_t builtin_cmdline[] = L"" CONFIG_CMDLINE;
183
184 /*
185  * Check the cmdline for a LILO-style file= arguments.
186  *
187  * We only support loading a file from the same filesystem as
188  * the kernel image.
189  */
190 efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
191                                   const efi_char16_t *optstr,
192                                   int optstr_size,
193                                   unsigned long soft_limit,
194                                   unsigned long hard_limit,
195                                   unsigned long *load_addr,
196                                   unsigned long *load_size)
197 {
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;
207         efi_status_t status;
208         bool twopass;
209         int offset;
210
211         if (!load_addr || !load_size)
212                 return EFI_INVALID_PARAMETER;
213
214         efi_apply_loadoptions_quirk((const void **)&cmdline, &cmdline_len);
215         cmdline_len /= sizeof(*cmdline);
216
217         if (IS_ENABLED(CONFIG_X86) && !efi_nochunk)
218                 efi_chunk_size = EFI_READ_CHUNK_SIZE;
219
220         alloc_addr = alloc_size = 0;
221
222         if (!ignore_load_options && cmdline_len > 0) {
223                 twopass = IS_ENABLED(CONFIG_CMDLINE_BOOL) ||
224                           IS_ENABLED(CONFIG_CMDLINE_EXTEND);
225         } else {
226 do_builtin:     cmdline     = builtin_cmdline;
227                 cmdline_len = ARRAY_SIZE(builtin_cmdline) - 1;
228                 twopass     = false;
229         }
230
231         do {
232                 struct finfo fi;
233                 unsigned long size;
234                 void *addr;
235
236                 offset = find_file_option(cmdline, cmdline_len,
237                                           optstr, optstr_size,
238                                           fi.filename, ARRAY_SIZE(fi.filename));
239
240                 if (!offset)
241                         break;
242
243                 cmdline += offset;
244                 cmdline_len -= offset;
245
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);
250
251                 if (status != EFI_SUCCESS)
252                         goto err_free_alloc;
253
254                 status = efi_open_file(volume, &fi, &file, &size);
255                 if (status != EFI_SUCCESS)
256                         goto err_close_volume;
257
258                 /*
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.
263                  */
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;
267
268                         status = EFI_OUT_OF_RESOURCES;
269                         if (soft_limit < hard_limit)
270                                 status = efi_allocate_pages(alloc_size + size,
271                                                             &alloc_addr,
272                                                             soft_limit);
273                         if (status == EFI_OUT_OF_RESOURCES)
274                                 status = efi_allocate_pages(alloc_size + size,
275                                                             &alloc_addr,
276                                                             hard_limit);
277                         if (status != EFI_SUCCESS) {
278                                 efi_err("Failed to allocate memory for files\n");
279                                 goto err_close_file;
280                         }
281
282                         if (old_addr != 0) {
283                                 /*
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.
288                                  */
289                                 memcpy((void *)alloc_addr, (void *)old_addr, alloc_size);
290                                 efi_free(alloc_size, old_addr);
291                         }
292                 }
293
294                 addr = (void *)alloc_addr + alloc_size;
295                 alloc_size += size;
296
297                 while (size) {
298                         unsigned long chunksize = min(size, efi_chunk_size);
299
300                         status = efi_call_proto(file, read, &chunksize, addr);
301                         if (status != EFI_SUCCESS) {
302                                 efi_err("Failed to read file\n");
303                                 goto err_close_file;
304                         }
305                         addr += chunksize;
306                         size -= chunksize;
307                 }
308                 efi_call_proto(file, close);
309                 efi_call_proto(volume, close);
310         } while (offset > 0);
311
312         if (twopass)
313                 goto do_builtin;
314
315         *load_addr = alloc_addr;
316         *load_size = alloc_size;
317
318         if (*load_size == 0)
319                 return EFI_NOT_READY;
320         return EFI_SUCCESS;
321
322 err_close_file:
323         efi_call_proto(file, close);
324
325 err_close_volume:
326         efi_call_proto(volume, close);
327
328 err_free_alloc:
329         efi_free(alloc_size, alloc_addr);
330         return status;
331 }
This page took 0.048893 seconds and 4 git commands to generate.