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