blob: 95b3c890ee96d39f5bb1061e4e14beb6727a29ef [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clark53142032017-09-13 18:05:34 -04002/*
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +02003 * EFI_FILE_PROTOCOL
Rob Clark53142032017-09-13 18:05:34 -04004 *
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +02005 * Copyright (c) 2017 Rob Clark
Rob Clark53142032017-09-13 18:05:34 -04006 */
7
Rob Clark53142032017-09-13 18:05:34 -04008#include <charset.h>
9#include <efi_loader.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Rob Clark53142032017-09-13 18:05:34 -040011#include <malloc.h>
Alexander Grafab133c72018-08-08 03:54:32 -060012#include <mapmem.h>
Rob Clark53142032017-09-13 18:05:34 -040013#include <fs.h>
Simon Glass655306c2020-05-10 11:39:58 -060014#include <part.h>
Rob Clark53142032017-09-13 18:05:34 -040015
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +020016/* GUID for file system information */
17const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
18
Heinrich Schuchardt0c236c42019-09-08 10:32:54 +020019/* GUID to obtain the volume label */
20const efi_guid_t efi_system_volume_label_id = EFI_FILE_SYSTEM_VOLUME_LABEL_ID;
21
Rob Clark53142032017-09-13 18:05:34 -040022struct file_system {
23 struct efi_simple_file_system_protocol base;
24 struct efi_device_path *dp;
25 struct blk_desc *desc;
26 int part;
27};
28#define to_fs(x) container_of(x, struct file_system, base)
29
30struct file_handle {
31 struct efi_file_handle base;
32 struct file_system *fs;
33 loff_t offset; /* current file position/cursor */
34 int isdir;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +020035 u64 open_mode;
Rob Clark53142032017-09-13 18:05:34 -040036
37 /* for reading a directory: */
38 struct fs_dir_stream *dirs;
39 struct fs_dirent *dent;
40
41 char path[0];
42};
43#define to_fh(x) container_of(x, struct file_handle, base)
44
45static const struct efi_file_handle efi_file_handle_protocol;
46
47static char *basename(struct file_handle *fh)
48{
49 char *s = strrchr(fh->path, '/');
50 if (s)
51 return s + 1;
52 return fh->path;
53}
54
55static int set_blk_dev(struct file_handle *fh)
56{
57 return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
58}
59
Heinrich Schuchardt80f834d2018-10-02 05:57:32 +020060/**
61 * is_dir() - check if file handle points to directory
62 *
63 * We assume that set_blk_dev(fh) has been called already.
64 *
65 * @fh: file handle
66 * Return: true if file handle points to a directory
67 */
Rob Clark53142032017-09-13 18:05:34 -040068static int is_dir(struct file_handle *fh)
69{
70 struct fs_dir_stream *dirs;
71
Rob Clark53142032017-09-13 18:05:34 -040072 dirs = fs_opendir(fh->path);
73 if (!dirs)
74 return 0;
75
76 fs_closedir(dirs);
77
78 return 1;
79}
80
81/*
82 * Normalize a path which may include either back or fwd slashes,
83 * double slashes, . or .. entries in the path, etc.
84 */
85static int sanitize_path(char *path)
86{
87 char *p;
88
89 /* backslash to slash: */
90 p = path;
91 while ((p = strchr(p, '\\')))
92 *p++ = '/';
93
94 /* handle double-slashes: */
95 p = path;
96 while ((p = strstr(p, "//"))) {
97 char *src = p + 1;
98 memmove(p, src, strlen(src) + 1);
99 }
100
101 /* handle extra /.'s */
102 p = path;
103 while ((p = strstr(p, "/."))) {
104 /*
105 * You'd be tempted to do this *after* handling ".."s
106 * below to avoid having to check if "/." is start of
107 * a "/..", but that won't have the correct results..
108 * for example, "/foo/./../bar" would get resolved to
109 * "/foo/bar" if you did these two passes in the other
110 * order
111 */
112 if (p[2] == '.') {
113 p += 2;
114 continue;
115 }
116 char *src = p + 2;
117 memmove(p, src, strlen(src) + 1);
118 }
119
120 /* handle extra /..'s: */
121 p = path;
122 while ((p = strstr(p, "/.."))) {
123 char *src = p + 3;
124
125 p--;
126
127 /* find beginning of previous path entry: */
128 while (true) {
129 if (p < path)
130 return -1;
131 if (*p == '/')
132 break;
133 p--;
134 }
135
136 memmove(p, src, strlen(src) + 1);
137 }
138
139 return 0;
140}
141
Heinrich Schuchardt1af423b2018-09-12 19:00:02 +0200142/**
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200143 * efi_create_file() - create file or directory
144 *
145 * @fh: file handle
146 * @attributes: attributes for newly created file
147 * Returns: 0 for success
148 */
149static int efi_create_file(struct file_handle *fh, u64 attributes)
150{
151 loff_t actwrite;
152 void *buffer = &actwrite;
153
154 if (attributes & EFI_FILE_DIRECTORY)
155 return fs_mkdir(fh->path);
156 else
157 return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
158 &actwrite);
159}
160
161/**
Heinrich Schuchardt1af423b2018-09-12 19:00:02 +0200162 * file_open() - open a file handle
163 *
164 * @fs: file system
165 * @parent: directory relative to which the file is to be opened
166 * @file_name: path of the file to be opened. '\', '.', or '..' may
167 * be used as modifiers. A leading backslash indicates an
168 * absolute path.
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200169 * @open_mode: bit mask indicating the access mode (read, write,
Heinrich Schuchardt1af423b2018-09-12 19:00:02 +0200170 * create)
171 * @attributes: attributes for newly created file
172 * Returns: handle to the opened file or NULL
Rob Clark53142032017-09-13 18:05:34 -0400173 */
174static struct efi_file_handle *file_open(struct file_system *fs,
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200175 struct file_handle *parent, u16 *file_name, u64 open_mode,
AKASHI Takahiro0c672b02018-09-11 15:59:12 +0900176 u64 attributes)
Rob Clark53142032017-09-13 18:05:34 -0400177{
178 struct file_handle *fh;
179 char f0[MAX_UTF8_PER_UTF16] = {0};
180 int plen = 0;
181 int flen = 0;
182
183 if (file_name) {
Heinrich Schuchardtdb47c342019-01-12 12:02:33 +0100184 utf16_to_utf8((u8 *)f0, file_name, 1);
185 flen = u16_strlen(file_name);
Rob Clark53142032017-09-13 18:05:34 -0400186 }
187
188 /* we could have a parent, but also an absolute path: */
189 if (f0[0] == '\\') {
190 plen = 0;
191 } else if (parent) {
192 plen = strlen(parent->path) + 1;
193 }
194
195 /* +2 is for null and '/' */
196 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
Heinrich Schuchardtf407af82023-07-30 14:06:06 +0200197 if (!fh)
198 return NULL;
Rob Clark53142032017-09-13 18:05:34 -0400199
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200200 fh->open_mode = open_mode;
Rob Clark53142032017-09-13 18:05:34 -0400201 fh->base = efi_file_handle_protocol;
202 fh->fs = fs;
203
204 if (parent) {
205 char *p = fh->path;
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200206 int exists;
Rob Clark53142032017-09-13 18:05:34 -0400207
208 if (plen > 0) {
209 strcpy(p, parent->path);
210 p += plen - 1;
211 *p++ = '/';
212 }
213
Heinrich Schuchardtdb47c342019-01-12 12:02:33 +0100214 utf16_to_utf8((u8 *)p, file_name, flen);
Rob Clark53142032017-09-13 18:05:34 -0400215
216 if (sanitize_path(fh->path))
217 goto error;
218
219 /* check if file exists: */
220 if (set_blk_dev(fh))
221 goto error;
222
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200223 exists = fs_exists(fh->path);
Heinrich Schuchardt3ab87ac2019-02-09 22:23:48 +0100224 /* fs_exists() calls fs_close(), so open file system again */
225 if (set_blk_dev(fh))
226 goto error;
227
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200228 if (!exists) {
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200229 if (!(open_mode & EFI_FILE_MODE_CREATE) ||
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200230 efi_create_file(fh, attributes))
231 goto error;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200232 if (set_blk_dev(fh))
233 goto error;
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200234 }
235
Rob Clark53142032017-09-13 18:05:34 -0400236 /* figure out if file is a directory: */
237 fh->isdir = is_dir(fh);
238 } else {
239 fh->isdir = 1;
240 strcpy(fh->path, "");
241 }
242
243 return &fh->base;
244
245error:
246 free(fh);
247 return NULL;
248}
249
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900250efi_status_t efi_file_open_int(struct efi_file_handle *this,
251 struct efi_file_handle **new_handle,
252 u16 *file_name, u64 open_mode,
253 u64 attributes)
Rob Clark53142032017-09-13 18:05:34 -0400254{
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100255 struct file_handle *fh = to_fh(this);
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200256 efi_status_t ret;
Rob Clark53142032017-09-13 18:05:34 -0400257
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200258 /* Check parameters */
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100259 if (!this || !new_handle || !file_name) {
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200260 ret = EFI_INVALID_PARAMETER;
261 goto out;
262 }
263 if (open_mode != EFI_FILE_MODE_READ &&
264 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
265 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
266 EFI_FILE_MODE_CREATE)) {
267 ret = EFI_INVALID_PARAMETER;
268 goto out;
269 }
Heinrich Schuchardt662d0be2018-09-13 21:31:49 +0200270 /*
271 * The UEFI spec requires that attributes are only set in create mode.
272 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
273 * read mode. EDK2 does not check that attributes are zero if not in
274 * create mode.
275 *
276 * So here we only check attributes in create mode and do not check
277 * that they are zero otherwise.
278 */
279 if ((open_mode & EFI_FILE_MODE_CREATE) &&
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200280 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
281 ret = EFI_INVALID_PARAMETER;
282 goto out;
283 }
Rob Clark53142032017-09-13 18:05:34 -0400284
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200285 /* Open file */
286 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200287 if (*new_handle) {
288 EFI_PRINT("file handle %p\n", *new_handle);
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200289 ret = EFI_SUCCESS;
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200290 } else {
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200291 ret = EFI_NOT_FOUND;
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200292 }
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100293out:
294 return ret;
295}
296
297/**
Heinrich Schuchardtb0102502024-09-18 23:37:28 +0200298 * efi_file_open() - open file synchronously
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100299 *
300 * This function implements the Open service of the File Protocol.
301 * See the UEFI spec for details.
302 *
303 * @this: EFI_FILE_PROTOCOL instance
304 * @new_handle: on return pointer to file handle
305 * @file_name: file name
306 * @open_mode: mode to open the file (read, read/write, create/read/write)
307 * @attributes: attributes for newly created file
308 */
309static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *this,
310 struct efi_file_handle **new_handle,
311 u16 *file_name, u64 open_mode,
312 u64 attributes)
313{
314 efi_status_t ret;
315
316 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", this, new_handle,
317 file_name, open_mode, attributes);
318
319 ret = efi_file_open_int(this, new_handle, file_name, open_mode,
320 attributes);
321
322 return EFI_EXIT(ret);
323}
324
325/**
326 * efi_file_open_ex() - open file asynchronously
327 *
328 * This function implements the OpenEx service of the File Protocol.
329 * See the UEFI spec for details.
330 *
331 * @this: EFI_FILE_PROTOCOL instance
332 * @new_handle: on return pointer to file handle
333 * @file_name: file name
334 * @open_mode: mode to open the file (read, read/write, create/read/write)
335 * @attributes: attributes for newly created file
336 * @token: transaction token
337 */
338static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *this,
339 struct efi_file_handle **new_handle,
340 u16 *file_name, u64 open_mode,
341 u64 attributes,
342 struct efi_file_io_token *token)
343{
344 efi_status_t ret;
345
346 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu, %p", this, new_handle,
347 file_name, open_mode, attributes, token);
348
349 if (!token) {
350 ret = EFI_INVALID_PARAMETER;
351 goto out;
352 }
353
354 ret = efi_file_open_int(this, new_handle, file_name, open_mode,
355 attributes);
356
357 if (ret == EFI_SUCCESS && token->event) {
358 token->status = EFI_SUCCESS;
359 efi_signal_event(token->event);
360 }
361
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200362out:
363 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400364}
365
366static efi_status_t file_close(struct file_handle *fh)
367{
368 fs_closedir(fh->dirs);
369 free(fh);
370 return EFI_SUCCESS;
371}
372
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900373efi_status_t efi_file_close_int(struct efi_file_handle *file)
Rob Clark53142032017-09-13 18:05:34 -0400374{
375 struct file_handle *fh = to_fh(file);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900376
377 return file_close(fh);
378}
379
380static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
381{
Rob Clark53142032017-09-13 18:05:34 -0400382 EFI_ENTRY("%p", file);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900383 return EFI_EXIT(efi_file_close_int(file));
Rob Clark53142032017-09-13 18:05:34 -0400384}
385
386static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
387{
388 struct file_handle *fh = to_fh(file);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900389 efi_status_t ret = EFI_SUCCESS;
390
Rob Clark53142032017-09-13 18:05:34 -0400391 EFI_ENTRY("%p", file);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900392
Heinrich Schuchardt646faf32019-06-17 22:00:13 +0200393 if (set_blk_dev(fh) || fs_unlink(fh->path))
394 ret = EFI_WARN_DELETE_FAILURE;
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900395
Rob Clark53142032017-09-13 18:05:34 -0400396 file_close(fh);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900397 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400398}
399
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200400/**
401 * efi_get_file_size() - determine the size of a file
402 *
403 * @fh: file handle
404 * @file_size: pointer to receive file size
405 * Return: status code
406 */
407static efi_status_t efi_get_file_size(struct file_handle *fh,
408 loff_t *file_size)
409{
410 if (set_blk_dev(fh))
411 return EFI_DEVICE_ERROR;
412
413 if (fs_size(fh->path, file_size))
414 return EFI_DEVICE_ERROR;
415
416 return EFI_SUCCESS;
417}
418
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +0200419/**
420 * efi_file_size() - Get the size of a file using an EFI file handle
421 *
422 * @fh: EFI file handle
423 * @size: buffer to fill in the discovered size
424 *
425 * Return: size of the file
426 */
427efi_status_t efi_file_size(struct efi_file_handle *fh, efi_uintn_t *size)
428{
429 struct efi_file_info *info = NULL;
430 efi_uintn_t bs = 0;
431 efi_status_t ret;
432
433 *size = 0;
434 ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
435 info));
436 if (ret != EFI_BUFFER_TOO_SMALL) {
437 ret = EFI_DEVICE_ERROR;
438 goto out;
439 }
440
441 info = malloc(bs);
442 if (!info) {
443 ret = EFI_OUT_OF_RESOURCES;
444 goto out;
445 }
446 ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
447 info));
448 if (ret != EFI_SUCCESS)
449 goto out;
450
451 *size = info->file_size;
452
453out:
454 free(info);
455 return ret;
456}
457
Rob Clark53142032017-09-13 18:05:34 -0400458static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
459 void *buffer)
460{
461 loff_t actread;
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200462 efi_status_t ret;
463 loff_t file_size;
Rob Clark53142032017-09-13 18:05:34 -0400464
Stefan Sørensen6b398d02020-07-22 09:43:31 +0200465 if (!buffer) {
466 ret = EFI_INVALID_PARAMETER;
467 return ret;
468 }
469
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200470 ret = efi_get_file_size(fh, &file_size);
471 if (ret != EFI_SUCCESS)
472 return ret;
473 if (file_size < fh->offset) {
474 ret = EFI_DEVICE_ERROR;
475 return ret;
476 }
477
478 if (set_blk_dev(fh))
479 return EFI_DEVICE_ERROR;
Simon Glass849688f2018-09-15 00:51:00 -0600480 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
Rob Clark53142032017-09-13 18:05:34 -0400481 *buffer_size, &actread))
482 return EFI_DEVICE_ERROR;
483
484 *buffer_size = actread;
485 fh->offset += actread;
486
487 return EFI_SUCCESS;
488}
489
Heinrich Schuchardt69f722b2021-05-15 22:41:26 +0200490static void rtc2efi(struct efi_time *time, struct rtc_time *tm)
491{
492 memset(time, 0, sizeof(struct efi_time));
493 time->year = tm->tm_year;
494 time->month = tm->tm_mon;
495 time->day = tm->tm_mday;
496 time->hour = tm->tm_hour;
497 time->minute = tm->tm_min;
498 time->second = tm->tm_sec;
499}
500
Rob Clark53142032017-09-13 18:05:34 -0400501static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
502 void *buffer)
503{
504 struct efi_file_info *info = buffer;
505 struct fs_dirent *dent;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200506 u64 required_size;
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200507 u16 *dst;
Rob Clark53142032017-09-13 18:05:34 -0400508
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200509 if (set_blk_dev(fh))
510 return EFI_DEVICE_ERROR;
511
Rob Clark53142032017-09-13 18:05:34 -0400512 if (!fh->dirs) {
513 assert(fh->offset == 0);
514 fh->dirs = fs_opendir(fh->path);
515 if (!fh->dirs)
516 return EFI_DEVICE_ERROR;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200517 fh->dent = NULL;
Rob Clark53142032017-09-13 18:05:34 -0400518 }
519
520 /*
521 * So this is a bit awkward. Since fs layer is stateful and we
522 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
523 * we might have to return without consuming the dent.. so we
524 * have to stash it for next call.
525 */
526 if (fh->dent) {
527 dent = fh->dent;
Rob Clark53142032017-09-13 18:05:34 -0400528 } else {
529 dent = fs_readdir(fh->dirs);
530 }
531
Rob Clark53142032017-09-13 18:05:34 -0400532 if (!dent) {
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200533 /* no more files in directory */
534 *buffer_size = 0;
Rob Clark53142032017-09-13 18:05:34 -0400535 return EFI_SUCCESS;
536 }
537
538 /* check buffer size: */
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200539 required_size = sizeof(*info) +
540 2 * (utf8_utf16_strlen(dent->name) + 1);
Rob Clark53142032017-09-13 18:05:34 -0400541 if (*buffer_size < required_size) {
542 *buffer_size = required_size;
543 fh->dent = dent;
544 return EFI_BUFFER_TOO_SMALL;
545 }
Stefan Sørensen6b398d02020-07-22 09:43:31 +0200546 if (!buffer)
547 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200548 fh->dent = NULL;
Rob Clark53142032017-09-13 18:05:34 -0400549
550 *buffer_size = required_size;
551 memset(info, 0, required_size);
552
553 info->size = required_size;
554 info->file_size = dent->size;
555 info->physical_size = dent->size;
Heinrich Schuchardt69f722b2021-05-15 22:41:26 +0200556 info->attribute = dent->attr;
557 rtc2efi(&info->create_time, &dent->create_time);
558 rtc2efi(&info->modification_time, &dent->change_time);
559 rtc2efi(&info->last_access_time, &dent->access_time);
Rob Clark53142032017-09-13 18:05:34 -0400560
561 if (dent->type == FS_DT_DIR)
562 info->attribute |= EFI_FILE_DIRECTORY;
563
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200564 dst = info->file_name;
565 utf8_utf16_strcpy(&dst, dent->name);
Rob Clark53142032017-09-13 18:05:34 -0400566
567 fh->offset++;
568
569 return EFI_SUCCESS;
570}
571
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900572efi_status_t efi_file_read_int(struct efi_file_handle *this,
573 efi_uintn_t *buffer_size, void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400574{
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100575 struct file_handle *fh = to_fh(this);
Rob Clark53142032017-09-13 18:05:34 -0400576 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200577 u64 bs;
Rob Clark53142032017-09-13 18:05:34 -0400578
Peng Fanca02db22021-04-28 21:54:01 +0800579 if (!this || !buffer_size)
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100580 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200581
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200582 bs = *buffer_size;
Rob Clark53142032017-09-13 18:05:34 -0400583 if (fh->isdir)
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200584 ret = dir_read(fh, &bs, buffer);
Rob Clark53142032017-09-13 18:05:34 -0400585 else
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200586 ret = file_read(fh, &bs, buffer);
587 if (bs <= SIZE_MAX)
588 *buffer_size = bs;
589 else
590 *buffer_size = SIZE_MAX;
Rob Clark53142032017-09-13 18:05:34 -0400591
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100592 return ret;
593}
594
595/**
596 * efi_file_read() - read file
597 *
598 * This function implements the Read() service of the EFI_FILE_PROTOCOL.
599 *
600 * See the Unified Extensible Firmware Interface (UEFI) specification for
601 * details.
602 *
603 * @this: file protocol instance
604 * @buffer_size: number of bytes to read
605 * @buffer: read buffer
606 * Return: status code
607 */
608static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *this,
609 efi_uintn_t *buffer_size, void *buffer)
610{
611 efi_status_t ret;
612
613 EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
614
615 ret = efi_file_read_int(this, buffer_size, buffer);
616
Rob Clark53142032017-09-13 18:05:34 -0400617 return EFI_EXIT(ret);
618}
619
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200620/**
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100621 * efi_file_read_ex() - read file asynchonously
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200622 *
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100623 * This function implements the ReadEx() service of the EFI_FILE_PROTOCOL.
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200624 *
625 * See the Unified Extensible Firmware Interface (UEFI) specification for
626 * details.
627 *
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100628 * @this: file protocol instance
629 * @token: transaction token
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200630 * Return: status code
631 */
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100632static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *this,
633 struct efi_file_io_token *token)
Rob Clark53142032017-09-13 18:05:34 -0400634{
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100635 efi_status_t ret;
636
637 EFI_ENTRY("%p, %p", this, token);
638
639 if (!token) {
640 ret = EFI_INVALID_PARAMETER;
641 goto out;
642 }
643
644 ret = efi_file_read_int(this, &token->buffer_size, token->buffer);
645
646 if (ret == EFI_SUCCESS && token->event) {
647 token->status = EFI_SUCCESS;
648 efi_signal_event(token->event);
649 }
650
651out:
652 return EFI_EXIT(ret);
653}
654
655static efi_status_t efi_file_write_int(struct efi_file_handle *this,
656 efi_uintn_t *buffer_size, void *buffer)
657{
658 struct file_handle *fh = to_fh(this);
Rob Clark53142032017-09-13 18:05:34 -0400659 efi_status_t ret = EFI_SUCCESS;
660 loff_t actwrite;
661
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100662 if (!this || !buffer_size || !buffer) {
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200663 ret = EFI_INVALID_PARAMETER;
664 goto out;
665 }
666 if (fh->isdir) {
667 ret = EFI_UNSUPPORTED;
668 goto out;
669 }
670 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
671 ret = EFI_ACCESS_DENIED;
672 goto out;
673 }
674
675 if (!*buffer_size)
676 goto out;
677
Rob Clark53142032017-09-13 18:05:34 -0400678 if (set_blk_dev(fh)) {
679 ret = EFI_DEVICE_ERROR;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200680 goto out;
Rob Clark53142032017-09-13 18:05:34 -0400681 }
Simon Glass849688f2018-09-15 00:51:00 -0600682 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
Rob Clark53142032017-09-13 18:05:34 -0400683 &actwrite)) {
684 ret = EFI_DEVICE_ERROR;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200685 goto out;
Rob Clark53142032017-09-13 18:05:34 -0400686 }
Rob Clark53142032017-09-13 18:05:34 -0400687 *buffer_size = actwrite;
688 fh->offset += actwrite;
689
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200690out:
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100691 return ret;
692}
693
694/**
695 * efi_file_write() - write to file
696 *
697 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
698 *
699 * See the Unified Extensible Firmware Interface (UEFI) specification for
700 * details.
701 *
702 * @this: file protocol instance
703 * @buffer_size: number of bytes to write
704 * @buffer: buffer with the bytes to write
705 * Return: status code
706 */
707static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *this,
708 efi_uintn_t *buffer_size,
709 void *buffer)
710{
711 efi_status_t ret;
712
713 EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
714
715 ret = efi_file_write_int(this, buffer_size, buffer);
716
Rob Clark53142032017-09-13 18:05:34 -0400717 return EFI_EXIT(ret);
718}
719
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200720/**
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100721 * efi_file_write_ex() - write to file
722 *
723 * This function implements the WriteEx() service of the EFI_FILE_PROTOCOL.
724 *
725 * See the Unified Extensible Firmware Interface (UEFI) specification for
726 * details.
727 *
728 * @this: file protocol instance
729 * @token: transaction token
730 * Return: status code
731 */
732static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *this,
733 struct efi_file_io_token *token)
734{
735 efi_status_t ret;
736
737 EFI_ENTRY("%p, %p", this, token);
738
739 if (!token) {
740 ret = EFI_INVALID_PARAMETER;
741 goto out;
742 }
743
744 ret = efi_file_write_int(this, &token->buffer_size, token->buffer);
745
746 if (ret == EFI_SUCCESS && token->event) {
747 token->status = EFI_SUCCESS;
748 efi_signal_event(token->event);
749 }
750
751out:
752 return EFI_EXIT(ret);
753}
754
755/**
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200756 * efi_file_getpos() - get current position in file
757 *
758 * This function implements the GetPosition service of the EFI file protocol.
759 * See the UEFI spec for details.
760 *
761 * @file: file handle
762 * @pos: pointer to file position
763 * Return: status code
764 */
Rob Clark53142032017-09-13 18:05:34 -0400765static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200766 u64 *pos)
Rob Clark53142032017-09-13 18:05:34 -0400767{
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200768 efi_status_t ret = EFI_SUCCESS;
Rob Clark53142032017-09-13 18:05:34 -0400769 struct file_handle *fh = to_fh(file);
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200770
Rob Clark53142032017-09-13 18:05:34 -0400771 EFI_ENTRY("%p, %p", file, pos);
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200772
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200773 if (fh->isdir) {
774 ret = EFI_UNSUPPORTED;
775 goto out;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200776 }
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200777
778 *pos = fh->offset;
779out:
780 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400781}
782
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900783efi_status_t efi_file_setpos_int(struct efi_file_handle *file, u64 pos)
Rob Clark53142032017-09-13 18:05:34 -0400784{
785 struct file_handle *fh = to_fh(file);
786 efi_status_t ret = EFI_SUCCESS;
787
Rob Clark53142032017-09-13 18:05:34 -0400788 if (fh->isdir) {
789 if (pos != 0) {
790 ret = EFI_UNSUPPORTED;
791 goto error;
792 }
793 fs_closedir(fh->dirs);
794 fh->dirs = NULL;
795 }
796
797 if (pos == ~0ULL) {
798 loff_t file_size;
799
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200800 ret = efi_get_file_size(fh, &file_size);
801 if (ret != EFI_SUCCESS)
Rob Clark53142032017-09-13 18:05:34 -0400802 goto error;
Rob Clark53142032017-09-13 18:05:34 -0400803 pos = file_size;
804 }
805
806 fh->offset = pos;
807
808error:
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900809 return ret;
810}
811
812/**
813 * efi_file_setpos() - set current position in file
814 *
815 * This function implements the SetPosition service of the EFI file protocol.
816 * See the UEFI spec for details.
817 *
818 * @file: file handle
819 * @pos: new file position
820 * Return: status code
821 */
822static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
823 u64 pos)
824{
825 efi_status_t ret = EFI_SUCCESS;
826
827 EFI_ENTRY("%p, %llu", file, pos);
828
829 ret = efi_file_setpos_int(file, pos);
830
Rob Clark53142032017-09-13 18:05:34 -0400831 return EFI_EXIT(ret);
832}
833
834static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
Heinrich Schuchardte86380d2018-04-04 15:42:09 +0200835 const efi_guid_t *info_type,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200836 efi_uintn_t *buffer_size,
837 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400838{
839 struct file_handle *fh = to_fh(file);
840 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200841 u16 *dst;
Rob Clark53142032017-09-13 18:05:34 -0400842
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100843 EFI_ENTRY("%p, %pUs, %p, %p", file, info_type, buffer_size, buffer);
Rob Clark53142032017-09-13 18:05:34 -0400844
Heinrich Schuchardtc540a0e2019-09-08 10:45:31 +0200845 if (!file || !info_type || !buffer_size ||
846 (*buffer_size && !buffer)) {
847 ret = EFI_INVALID_PARAMETER;
848 goto error;
849 }
850
Rob Clark53142032017-09-13 18:05:34 -0400851 if (!guidcmp(info_type, &efi_file_info_guid)) {
852 struct efi_file_info *info = buffer;
853 char *filename = basename(fh);
854 unsigned int required_size;
855 loff_t file_size;
856
857 /* check buffer size: */
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200858 required_size = sizeof(*info) +
859 2 * (utf8_utf16_strlen(filename) + 1);
Rob Clark53142032017-09-13 18:05:34 -0400860 if (*buffer_size < required_size) {
861 *buffer_size = required_size;
862 ret = EFI_BUFFER_TOO_SMALL;
863 goto error;
864 }
865
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200866 ret = efi_get_file_size(fh, &file_size);
Heinrich Schuchardt6e3f70b2024-10-26 08:40:47 +0200867 if (ret != EFI_SUCCESS) {
868 if (!fh->isdir)
869 goto error;
870 /*
871 * Some file drivers don't implement fs_size() for
872 * directories. Use a dummy non-zero value.
873 */
874 file_size = 4096;
875 ret = EFI_SUCCESS;
876 }
Rob Clark53142032017-09-13 18:05:34 -0400877
878 memset(info, 0, required_size);
879
880 info->size = required_size;
881 info->file_size = file_size;
882 info->physical_size = file_size;
883
884 if (fh->isdir)
885 info->attribute |= EFI_FILE_DIRECTORY;
886
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200887 dst = info->file_name;
888 utf8_utf16_strcpy(&dst, filename);
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200889 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
890 struct efi_file_system_info *info = buffer;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600891 struct disk_partition part;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200892 efi_uintn_t required_size;
893 int r;
894
895 if (fh->fs->part >= 1)
896 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
897 else
898 r = part_get_info_whole_disk(fh->fs->desc, &part);
899 if (r < 0) {
900 ret = EFI_DEVICE_ERROR;
901 goto error;
902 }
Heinrich Schuchardt0c236c42019-09-08 10:32:54 +0200903 required_size = sizeof(*info) + 2;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200904 if (*buffer_size < required_size) {
905 *buffer_size = required_size;
906 ret = EFI_BUFFER_TOO_SMALL;
907 goto error;
908 }
909
910 memset(info, 0, required_size);
911
912 info->size = required_size;
Heinrich Schuchardta821b0c2019-12-08 10:02:37 +0100913 /*
914 * TODO: We cannot determine if the volume can be written to.
915 */
916 info->read_only = false;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200917 info->volume_size = part.size * part.blksz;
Heinrich Schuchardta821b0c2019-12-08 10:02:37 +0100918 /*
919 * TODO: We currently have no function to determine the free
920 * space. The volume size is the best upper bound we have.
921 */
922 info->free_space = info->volume_size;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200923 info->block_size = part.blksz;
924 /*
925 * TODO: The volume label is not available in U-Boot.
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200926 */
Heinrich Schuchardt0c236c42019-09-08 10:32:54 +0200927 info->volume_label[0] = 0;
928 } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
929 if (*buffer_size < 2) {
930 *buffer_size = 2;
931 ret = EFI_BUFFER_TOO_SMALL;
932 goto error;
933 }
934 *(u16 *)buffer = 0;
Rob Clark53142032017-09-13 18:05:34 -0400935 } else {
936 ret = EFI_UNSUPPORTED;
937 }
938
939error:
940 return EFI_EXIT(ret);
941}
942
943static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
Heinrich Schuchardte86380d2018-04-04 15:42:09 +0200944 const efi_guid_t *info_type,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200945 efi_uintn_t buffer_size,
946 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400947{
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200948 struct file_handle *fh = to_fh(file);
949 efi_status_t ret = EFI_UNSUPPORTED;
950
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100951 EFI_ENTRY("%p, %pUs, %zu, %p", file, info_type, buffer_size, buffer);
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200952
953 if (!guidcmp(info_type, &efi_file_info_guid)) {
954 struct efi_file_info *info = (struct efi_file_info *)buffer;
955 char *filename = basename(fh);
956 char *new_file_name, *pos;
957 loff_t file_size;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200958
Heinrich Schuchardtdd106d02019-09-08 11:37:07 +0200959 /* The buffer will always contain a file name. */
960 if (buffer_size < sizeof(struct efi_file_info) + 2 ||
961 buffer_size < info->size) {
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200962 ret = EFI_BAD_BUFFER_SIZE;
963 goto out;
964 }
965 /* We cannot change the directory attribute */
966 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
967 ret = EFI_ACCESS_DENIED;
968 goto out;
969 }
970 /* Check for renaming */
Heinrich Schuchardt344bc072020-11-10 07:24:16 +0100971 new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1);
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200972 if (!new_file_name) {
973 ret = EFI_OUT_OF_RESOURCES;
974 goto out;
975 }
976 pos = new_file_name;
977 utf16_utf8_strcpy(&pos, info->file_name);
978 if (strcmp(new_file_name, filename)) {
979 /* TODO: we do not support renaming */
980 EFI_PRINT("Renaming not supported\n");
981 free(new_file_name);
982 ret = EFI_ACCESS_DENIED;
983 goto out;
984 }
985 free(new_file_name);
986 /* Check for truncation */
Heinrich Schuchardt6e3f70b2024-10-26 08:40:47 +0200987 if (!fh->isdir) {
988 ret = efi_get_file_size(fh, &file_size);
989 if (ret != EFI_SUCCESS)
990 goto out;
991 if (file_size != info->file_size) {
992 /* TODO: we do not support truncation */
993 EFI_PRINT("Truncation not supported\n");
994 ret = EFI_ACCESS_DENIED;
995 goto out;
996 }
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200997 }
998 /*
999 * We do not care for the other attributes
1000 * TODO: Support read only
1001 */
1002 ret = EFI_SUCCESS;
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +02001003 } else {
Heinrich Schuchardtdd106d02019-09-08 11:37:07 +02001004 /* TODO: We do not support changing the volume label */
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +02001005 ret = EFI_UNSUPPORTED;
1006 }
1007out:
1008 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -04001009}
1010
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001011/**
1012 * efi_file_flush_int() - flush file
1013 *
1014 * This is the internal implementation of the Flush() and FlushEx() services of
1015 * the EFI_FILE_PROTOCOL.
1016 *
1017 * @this: file protocol instance
1018 * Return: status code
1019 */
1020static efi_status_t efi_file_flush_int(struct efi_file_handle *this)
Rob Clark53142032017-09-13 18:05:34 -04001021{
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001022 struct file_handle *fh = to_fh(this);
Rob Clark53142032017-09-13 18:05:34 -04001023
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001024 if (!this)
1025 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001026
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001027 if (!(fh->open_mode & EFI_FILE_MODE_WRITE))
1028 return EFI_ACCESS_DENIED;
1029
1030 /* TODO: flush for file position after end of file */
1031 return EFI_SUCCESS;
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001032}
1033
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001034/**
1035 * efi_file_flush() - flush file
1036 *
1037 * This function implements the Flush() service of the EFI_FILE_PROTOCOL.
1038 *
1039 * See the Unified Extensible Firmware Interface (UEFI) specification for
1040 * details.
1041 *
1042 * @this: file protocol instance
1043 * Return: status code
1044 */
1045static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *this)
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001046{
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001047 efi_status_t ret;
1048
1049 EFI_ENTRY("%p", this);
1050
1051 ret = efi_file_flush_int(this);
1052
1053 return EFI_EXIT(ret);
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001054}
1055
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001056/**
1057 * efi_file_flush_ex() - flush file
1058 *
1059 * This function implements the FlushEx() service of the EFI_FILE_PROTOCOL.
1060 *
1061 * See the Unified Extensible Firmware Interface (UEFI) specification for
1062 * details.
1063 *
1064 * @this: file protocol instance
1065 * @token: transaction token
1066 * Return: status code
1067 */
1068static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *this,
1069 struct efi_file_io_token *token)
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001070{
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001071 efi_status_t ret;
1072
1073 EFI_ENTRY("%p, %p", this, token);
1074
1075 if (!token) {
1076 ret = EFI_INVALID_PARAMETER;
1077 goto out;
1078 }
1079
1080 ret = efi_file_flush_int(this);
1081
1082 if (ret == EFI_SUCCESS && token->event) {
1083 token->status = EFI_SUCCESS;
1084 efi_signal_event(token->event);
1085 }
1086
1087out:
1088 return EFI_EXIT(ret);
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001089}
1090
Rob Clark53142032017-09-13 18:05:34 -04001091static const struct efi_file_handle efi_file_handle_protocol = {
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001092 .rev = EFI_FILE_PROTOCOL_REVISION2,
Rob Clark53142032017-09-13 18:05:34 -04001093 .open = efi_file_open,
1094 .close = efi_file_close,
1095 .delete = efi_file_delete,
1096 .read = efi_file_read,
1097 .write = efi_file_write,
1098 .getpos = efi_file_getpos,
1099 .setpos = efi_file_setpos,
1100 .getinfo = efi_file_getinfo,
1101 .setinfo = efi_file_setinfo,
1102 .flush = efi_file_flush,
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001103 .open_ex = efi_file_open_ex,
1104 .read_ex = efi_file_read_ex,
1105 .write_ex = efi_file_write_ex,
1106 .flush_ex = efi_file_flush_ex,
Rob Clark53142032017-09-13 18:05:34 -04001107};
1108
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001109/**
1110 * efi_file_from_path() - open file via device path
1111 *
Heinrich Schuchardt4395adb2022-11-10 14:14:06 +01001112 * The device path @fp consists of the device path of the handle with the
1113 * simple file system protocol and one or more file path device path nodes.
1114 * The concatenation of all file path names provides the total file path.
1115 *
1116 * The code starts at the first file path node and tries to open that file or
1117 * directory. If there is a succeding file path node, the code opens it relative
1118 * to this directory and continues iterating until reaching the last file path
1119 * node.
1120 *
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001121 * @fp: device path
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +01001122 * Return: EFI_FILE_PROTOCOL for the file or NULL
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001123 */
Rob Clark53142032017-09-13 18:05:34 -04001124struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
1125{
1126 struct efi_simple_file_system_protocol *v;
1127 struct efi_file_handle *f;
1128 efi_status_t ret;
1129
1130 v = efi_fs_from_path(fp);
1131 if (!v)
1132 return NULL;
1133
1134 EFI_CALL(ret = v->open_volume(v, &f));
1135 if (ret != EFI_SUCCESS)
1136 return NULL;
1137
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001138 /* Skip over device-path nodes before the file path. */
Rob Clark53142032017-09-13 18:05:34 -04001139 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
1140 fp = efi_dp_next(fp);
1141
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001142 /*
1143 * Step through the nodes of the directory path until the actual file
1144 * node is reached which is the final node in the device path.
1145 */
Rob Clark53142032017-09-13 18:05:34 -04001146 while (fp) {
1147 struct efi_device_path_file_path *fdp =
1148 container_of(fp, struct efi_device_path_file_path, dp);
1149 struct efi_file_handle *f2;
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +02001150 u16 *filename;
Ilias Apalodimas50279dc2022-11-11 20:04:31 +02001151 size_t filename_sz;
Rob Clark53142032017-09-13 18:05:34 -04001152
1153 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
1154 printf("bad file path!\n");
Ilias Apalodimas08d1af22022-11-11 18:20:37 +02001155 EFI_CALL(f->close(f));
Rob Clark53142032017-09-13 18:05:34 -04001156 return NULL;
1157 }
1158
Ilias Apalodimas9124bc62022-11-10 15:31:30 +02001159 /*
1160 * UEFI specification requires pointers that are passed to
1161 * protocol member functions to be aligned. So memcpy it
1162 * unconditionally
1163 */
Ilias Apalodimas50279dc2022-11-11 20:04:31 +02001164 if (fdp->dp.length <= offsetof(struct efi_device_path_file_path, str))
1165 return NULL;
1166 filename_sz = fdp->dp.length -
1167 offsetof(struct efi_device_path_file_path, str);
1168 filename = malloc(filename_sz);
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +02001169 if (!filename)
1170 return NULL;
Ilias Apalodimas50279dc2022-11-11 20:04:31 +02001171 memcpy(filename, fdp->str, filename_sz);
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +02001172 EFI_CALL(ret = f->open(f, &f2, filename,
Rob Clark53142032017-09-13 18:05:34 -04001173 EFI_FILE_MODE_READ, 0));
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +02001174 free(filename);
Rob Clark53142032017-09-13 18:05:34 -04001175 if (ret != EFI_SUCCESS)
1176 return NULL;
1177
1178 fp = efi_dp_next(fp);
1179
1180 EFI_CALL(f->close(f));
1181 f = f2;
1182 }
1183
1184 return f;
1185}
1186
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001187efi_status_t efi_open_volume_int(struct efi_simple_file_system_protocol *this,
1188 struct efi_file_handle **root)
1189{
1190 struct file_system *fs = to_fs(this);
1191
1192 *root = file_open(fs, NULL, NULL, 0, 0);
1193
1194 return EFI_SUCCESS;
1195}
1196
Rob Clark53142032017-09-13 18:05:34 -04001197static efi_status_t EFIAPI
1198efi_open_volume(struct efi_simple_file_system_protocol *this,
1199 struct efi_file_handle **root)
1200{
Rob Clark53142032017-09-13 18:05:34 -04001201 EFI_ENTRY("%p, %p", this, root);
1202
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001203 return EFI_EXIT(efi_open_volume_int(this, root));
Rob Clark53142032017-09-13 18:05:34 -04001204}
1205
Heinrich Schuchardtf9e8ada2023-07-30 14:03:53 +02001206efi_status_t
1207efi_create_simple_file_system(struct blk_desc *desc, int part,
1208 struct efi_device_path *dp,
1209 struct efi_simple_file_system_protocol **fsp)
Rob Clark53142032017-09-13 18:05:34 -04001210{
1211 struct file_system *fs;
1212
1213 fs = calloc(1, sizeof(*fs));
Heinrich Schuchardtf9e8ada2023-07-30 14:03:53 +02001214 if (!fs)
1215 return EFI_OUT_OF_RESOURCES;
Rob Clark53142032017-09-13 18:05:34 -04001216 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
1217 fs->base.open_volume = efi_open_volume;
1218 fs->desc = desc;
1219 fs->part = part;
1220 fs->dp = dp;
Heinrich Schuchardtf9e8ada2023-07-30 14:03:53 +02001221 *fsp = &fs->base;
Rob Clark53142032017-09-13 18:05:34 -04001222
Heinrich Schuchardtf9e8ada2023-07-30 14:03:53 +02001223 return EFI_SUCCESS;
Rob Clark53142032017-09-13 18:05:34 -04001224}