1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
10 #include <efi_loader.h>
17 /* GUID for file system information */
18 const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
20 /* GUID to obtain the volume label */
21 const efi_guid_t efi_system_volume_label_id = EFI_FILE_SYSTEM_VOLUME_LABEL_ID;
24 struct efi_simple_file_system_protocol base;
25 struct efi_device_path *dp;
26 struct blk_desc *desc;
29 #define to_fs(x) container_of(x, struct file_system, base)
32 struct efi_file_handle base;
33 struct file_system *fs;
34 loff_t offset; /* current file position/cursor */
38 /* for reading a directory: */
39 struct fs_dir_stream *dirs;
40 struct fs_dirent *dent;
44 #define to_fh(x) container_of(x, struct file_handle, base)
46 static const struct efi_file_handle efi_file_handle_protocol;
48 static char *basename(struct file_handle *fh)
50 char *s = strrchr(fh->path, '/');
56 static int set_blk_dev(struct file_handle *fh)
58 return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
62 * is_dir() - check if file handle points to directory
64 * We assume that set_blk_dev(fh) has been called already.
67 * Return: true if file handle points to a directory
69 static int is_dir(struct file_handle *fh)
71 struct fs_dir_stream *dirs;
73 dirs = fs_opendir(fh->path);
83 * Normalize a path which may include either back or fwd slashes,
84 * double slashes, . or .. entries in the path, etc.
86 static int sanitize_path(char *path)
90 /* backslash to slash: */
92 while ((p = strchr(p, '\\')))
95 /* handle double-slashes: */
97 while ((p = strstr(p, "//"))) {
99 memmove(p, src, strlen(src) + 1);
102 /* handle extra /.'s */
104 while ((p = strstr(p, "/."))) {
106 * You'd be tempted to do this *after* handling ".."s
107 * below to avoid having to check if "/." is start of
108 * a "/..", but that won't have the correct results..
109 * for example, "/foo/./../bar" would get resolved to
110 * "/foo/bar" if you did these two passes in the other
118 memmove(p, src, strlen(src) + 1);
121 /* handle extra /..'s: */
123 while ((p = strstr(p, "/.."))) {
128 /* find beginning of previous path entry: */
137 memmove(p, src, strlen(src) + 1);
144 * efi_create_file() - create file or directory
147 * @attributes: attributes for newly created file
148 * Returns: 0 for success
150 static int efi_create_file(struct file_handle *fh, u64 attributes)
153 void *buffer = &actwrite;
155 if (attributes & EFI_FILE_DIRECTORY)
156 return fs_mkdir(fh->path);
158 return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
163 * file_open() - open a file handle
166 * @parent: directory relative to which the file is to be opened
167 * @file_name: path of the file to be opened. '\', '.', or '..' may
168 * be used as modifiers. A leading backslash indicates an
170 * @open_mode: bit mask indicating the access mode (read, write,
172 * @attributes: attributes for newly created file
173 * Returns: handle to the opened file or NULL
175 static struct efi_file_handle *file_open(struct file_system *fs,
176 struct file_handle *parent, u16 *file_name, u64 open_mode,
179 struct file_handle *fh;
180 char f0[MAX_UTF8_PER_UTF16] = {0};
185 utf16_to_utf8((u8 *)f0, file_name, 1);
186 flen = u16_strlen(file_name);
189 /* we could have a parent, but also an absolute path: */
193 plen = strlen(parent->path) + 1;
196 /* +2 is for null and '/' */
197 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
199 fh->open_mode = open_mode;
200 fh->base = efi_file_handle_protocol;
208 strcpy(p, parent->path);
213 utf16_to_utf8((u8 *)p, file_name, flen);
215 if (sanitize_path(fh->path))
218 /* check if file exists: */
222 exists = fs_exists(fh->path);
223 /* fs_exists() calls fs_close(), so open file system again */
228 if (!(open_mode & EFI_FILE_MODE_CREATE) ||
229 efi_create_file(fh, attributes))
235 /* figure out if file is a directory: */
236 fh->isdir = is_dir(fh);
239 strcpy(fh->path, "");
249 efi_status_t efi_file_open_int(struct efi_file_handle *this,
250 struct efi_file_handle **new_handle,
251 u16 *file_name, u64 open_mode,
254 struct file_handle *fh = to_fh(this);
257 /* Check parameters */
258 if (!this || !new_handle || !file_name) {
259 ret = EFI_INVALID_PARAMETER;
262 if (open_mode != EFI_FILE_MODE_READ &&
263 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
264 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
265 EFI_FILE_MODE_CREATE)) {
266 ret = EFI_INVALID_PARAMETER;
270 * The UEFI spec requires that attributes are only set in create mode.
271 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
272 * read mode. EDK2 does not check that attributes are zero if not in
275 * So here we only check attributes in create mode and do not check
276 * that they are zero otherwise.
278 if ((open_mode & EFI_FILE_MODE_CREATE) &&
279 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
280 ret = EFI_INVALID_PARAMETER;
285 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
287 EFI_PRINT("file handle %p\n", *new_handle);
299 * This function implements the Open service of the File Protocol.
300 * See the UEFI spec for details.
302 * @this: EFI_FILE_PROTOCOL instance
303 * @new_handle: on return pointer to file handle
304 * @file_name: file name
305 * @open_mode: mode to open the file (read, read/write, create/read/write)
306 * @attributes: attributes for newly created file
308 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *this,
309 struct efi_file_handle **new_handle,
310 u16 *file_name, u64 open_mode,
315 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", this, new_handle,
316 file_name, open_mode, attributes);
318 ret = efi_file_open_int(this, new_handle, file_name, open_mode,
321 return EFI_EXIT(ret);
325 * efi_file_open_ex() - open file asynchronously
327 * This function implements the OpenEx service of the File Protocol.
328 * See the UEFI spec for details.
330 * @this: EFI_FILE_PROTOCOL instance
331 * @new_handle: on return pointer to file handle
332 * @file_name: file name
333 * @open_mode: mode to open the file (read, read/write, create/read/write)
334 * @attributes: attributes for newly created file
335 * @token: transaction token
337 static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *this,
338 struct efi_file_handle **new_handle,
339 u16 *file_name, u64 open_mode,
341 struct efi_file_io_token *token)
345 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu, %p", this, new_handle,
346 file_name, open_mode, attributes, token);
349 ret = EFI_INVALID_PARAMETER;
353 ret = efi_file_open_int(this, new_handle, file_name, open_mode,
356 if (ret == EFI_SUCCESS && token->event) {
357 token->status = EFI_SUCCESS;
358 efi_signal_event(token->event);
362 return EFI_EXIT(ret);
365 static efi_status_t file_close(struct file_handle *fh)
367 fs_closedir(fh->dirs);
372 efi_status_t efi_file_close_int(struct efi_file_handle *file)
374 struct file_handle *fh = to_fh(file);
376 return file_close(fh);
379 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
381 EFI_ENTRY("%p", file);
382 return EFI_EXIT(efi_file_close_int(file));
385 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
387 struct file_handle *fh = to_fh(file);
388 efi_status_t ret = EFI_SUCCESS;
390 EFI_ENTRY("%p", file);
392 if (set_blk_dev(fh) || fs_unlink(fh->path))
393 ret = EFI_WARN_DELETE_FAILURE;
396 return EFI_EXIT(ret);
400 * efi_get_file_size() - determine the size of a file
403 * @file_size: pointer to receive file size
404 * Return: status code
406 static efi_status_t efi_get_file_size(struct file_handle *fh,
410 return EFI_DEVICE_ERROR;
412 if (fs_size(fh->path, file_size))
413 return EFI_DEVICE_ERROR;
419 * efi_file_size() - Get the size of a file using an EFI file handle
421 * @fh: EFI file handle
422 * @size: buffer to fill in the discovered size
424 * Return: size of the file
426 efi_status_t efi_file_size(struct efi_file_handle *fh, efi_uintn_t *size)
428 struct efi_file_info *info = NULL;
433 ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
435 if (ret != EFI_BUFFER_TOO_SMALL) {
436 ret = EFI_DEVICE_ERROR;
442 ret = EFI_OUT_OF_RESOURCES;
445 ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
447 if (ret != EFI_SUCCESS)
450 *size = info->file_size;
457 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
465 ret = EFI_INVALID_PARAMETER;
469 ret = efi_get_file_size(fh, &file_size);
470 if (ret != EFI_SUCCESS)
472 if (file_size < fh->offset) {
473 ret = EFI_DEVICE_ERROR;
478 return EFI_DEVICE_ERROR;
479 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
480 *buffer_size, &actread))
481 return EFI_DEVICE_ERROR;
483 *buffer_size = actread;
484 fh->offset += actread;
489 static void rtc2efi(struct efi_time *time, struct rtc_time *tm)
491 memset(time, 0, sizeof(struct efi_time));
492 time->year = tm->tm_year;
493 time->month = tm->tm_mon;
494 time->day = tm->tm_mday;
495 time->hour = tm->tm_hour;
496 time->minute = tm->tm_min;
497 time->second = tm->tm_sec;
500 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
503 struct efi_file_info *info = buffer;
504 struct fs_dirent *dent;
509 return EFI_DEVICE_ERROR;
512 assert(fh->offset == 0);
513 fh->dirs = fs_opendir(fh->path);
515 return EFI_DEVICE_ERROR;
520 * So this is a bit awkward. Since fs layer is stateful and we
521 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
522 * we might have to return without consuming the dent.. so we
523 * have to stash it for next call.
528 dent = fs_readdir(fh->dirs);
532 /* no more files in directory */
537 /* check buffer size: */
538 required_size = sizeof(*info) +
539 2 * (utf8_utf16_strlen(dent->name) + 1);
540 if (*buffer_size < required_size) {
541 *buffer_size = required_size;
543 return EFI_BUFFER_TOO_SMALL;
546 return EFI_INVALID_PARAMETER;
549 *buffer_size = required_size;
550 memset(info, 0, required_size);
552 info->size = required_size;
553 info->file_size = dent->size;
554 info->physical_size = dent->size;
555 info->attribute = dent->attr;
556 rtc2efi(&info->create_time, &dent->create_time);
557 rtc2efi(&info->modification_time, &dent->change_time);
558 rtc2efi(&info->last_access_time, &dent->access_time);
560 if (dent->type == FS_DT_DIR)
561 info->attribute |= EFI_FILE_DIRECTORY;
563 dst = info->file_name;
564 utf8_utf16_strcpy(&dst, dent->name);
571 efi_status_t efi_file_read_int(struct efi_file_handle *this,
572 efi_uintn_t *buffer_size, void *buffer)
574 struct file_handle *fh = to_fh(this);
575 efi_status_t ret = EFI_SUCCESS;
578 if (!this || !buffer_size)
579 return EFI_INVALID_PARAMETER;
583 ret = dir_read(fh, &bs, buffer);
585 ret = file_read(fh, &bs, buffer);
589 *buffer_size = SIZE_MAX;
595 * efi_file_read() - read file
597 * This function implements the Read() service of the EFI_FILE_PROTOCOL.
599 * See the Unified Extensible Firmware Interface (UEFI) specification for
602 * @this: file protocol instance
603 * @buffer_size: number of bytes to read
604 * @buffer: read buffer
605 * Return: status code
607 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *this,
608 efi_uintn_t *buffer_size, void *buffer)
612 EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
614 ret = efi_file_read_int(this, buffer_size, buffer);
616 return EFI_EXIT(ret);
620 * efi_file_read_ex() - read file asynchonously
622 * This function implements the ReadEx() service of the EFI_FILE_PROTOCOL.
624 * See the Unified Extensible Firmware Interface (UEFI) specification for
627 * @this: file protocol instance
628 * @token: transaction token
629 * Return: status code
631 static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *this,
632 struct efi_file_io_token *token)
636 EFI_ENTRY("%p, %p", this, token);
639 ret = EFI_INVALID_PARAMETER;
643 ret = efi_file_read_int(this, &token->buffer_size, token->buffer);
645 if (ret == EFI_SUCCESS && token->event) {
646 token->status = EFI_SUCCESS;
647 efi_signal_event(token->event);
651 return EFI_EXIT(ret);
654 static efi_status_t efi_file_write_int(struct efi_file_handle *this,
655 efi_uintn_t *buffer_size, void *buffer)
657 struct file_handle *fh = to_fh(this);
658 efi_status_t ret = EFI_SUCCESS;
661 if (!this || !buffer_size || !buffer) {
662 ret = EFI_INVALID_PARAMETER;
666 ret = EFI_UNSUPPORTED;
669 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
670 ret = EFI_ACCESS_DENIED;
677 if (set_blk_dev(fh)) {
678 ret = EFI_DEVICE_ERROR;
681 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
683 ret = EFI_DEVICE_ERROR;
686 *buffer_size = actwrite;
687 fh->offset += actwrite;
694 * efi_file_write() - write to file
696 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
698 * See the Unified Extensible Firmware Interface (UEFI) specification for
701 * @this: file protocol instance
702 * @buffer_size: number of bytes to write
703 * @buffer: buffer with the bytes to write
704 * Return: status code
706 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *this,
707 efi_uintn_t *buffer_size,
712 EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
714 ret = efi_file_write_int(this, buffer_size, buffer);
716 return EFI_EXIT(ret);
720 * efi_file_write_ex() - write to file
722 * This function implements the WriteEx() service of the EFI_FILE_PROTOCOL.
724 * See the Unified Extensible Firmware Interface (UEFI) specification for
727 * @this: file protocol instance
728 * @token: transaction token
729 * Return: status code
731 static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *this,
732 struct efi_file_io_token *token)
736 EFI_ENTRY("%p, %p", this, token);
739 ret = EFI_INVALID_PARAMETER;
743 ret = efi_file_write_int(this, &token->buffer_size, token->buffer);
745 if (ret == EFI_SUCCESS && token->event) {
746 token->status = EFI_SUCCESS;
747 efi_signal_event(token->event);
751 return EFI_EXIT(ret);
755 * efi_file_getpos() - get current position in file
757 * This function implements the GetPosition service of the EFI file protocol.
758 * See the UEFI spec for details.
761 * @pos: pointer to file position
762 * Return: status code
764 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
767 efi_status_t ret = EFI_SUCCESS;
768 struct file_handle *fh = to_fh(file);
770 EFI_ENTRY("%p, %p", file, pos);
773 ret = EFI_UNSUPPORTED;
779 return EFI_EXIT(ret);
782 efi_status_t efi_file_setpos_int(struct efi_file_handle *file, u64 pos)
784 struct file_handle *fh = to_fh(file);
785 efi_status_t ret = EFI_SUCCESS;
789 ret = EFI_UNSUPPORTED;
792 fs_closedir(fh->dirs);
799 ret = efi_get_file_size(fh, &file_size);
800 if (ret != EFI_SUCCESS)
812 * efi_file_setpos() - set current position in file
814 * This function implements the SetPosition service of the EFI file protocol.
815 * See the UEFI spec for details.
818 * @pos: new file position
819 * Return: status code
821 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
824 efi_status_t ret = EFI_SUCCESS;
826 EFI_ENTRY("%p, %llu", file, pos);
828 ret = efi_file_setpos_int(file, pos);
830 return EFI_EXIT(ret);
833 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
834 const efi_guid_t *info_type,
835 efi_uintn_t *buffer_size,
838 struct file_handle *fh = to_fh(file);
839 efi_status_t ret = EFI_SUCCESS;
842 EFI_ENTRY("%p, %pUs, %p, %p", file, info_type, buffer_size, buffer);
844 if (!file || !info_type || !buffer_size ||
845 (*buffer_size && !buffer)) {
846 ret = EFI_INVALID_PARAMETER;
850 if (!guidcmp(info_type, &efi_file_info_guid)) {
851 struct efi_file_info *info = buffer;
852 char *filename = basename(fh);
853 unsigned int required_size;
856 /* check buffer size: */
857 required_size = sizeof(*info) +
858 2 * (utf8_utf16_strlen(filename) + 1);
859 if (*buffer_size < required_size) {
860 *buffer_size = required_size;
861 ret = EFI_BUFFER_TOO_SMALL;
865 ret = efi_get_file_size(fh, &file_size);
866 if (ret != EFI_SUCCESS)
869 memset(info, 0, required_size);
871 info->size = required_size;
872 info->file_size = file_size;
873 info->physical_size = file_size;
876 info->attribute |= EFI_FILE_DIRECTORY;
878 dst = info->file_name;
879 utf8_utf16_strcpy(&dst, filename);
880 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
881 struct efi_file_system_info *info = buffer;
882 struct disk_partition part;
883 efi_uintn_t required_size;
886 if (fh->fs->part >= 1)
887 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
889 r = part_get_info_whole_disk(fh->fs->desc, &part);
891 ret = EFI_DEVICE_ERROR;
894 required_size = sizeof(*info) + 2;
895 if (*buffer_size < required_size) {
896 *buffer_size = required_size;
897 ret = EFI_BUFFER_TOO_SMALL;
901 memset(info, 0, required_size);
903 info->size = required_size;
905 * TODO: We cannot determine if the volume can be written to.
907 info->read_only = false;
908 info->volume_size = part.size * part.blksz;
910 * TODO: We currently have no function to determine the free
911 * space. The volume size is the best upper bound we have.
913 info->free_space = info->volume_size;
914 info->block_size = part.blksz;
916 * TODO: The volume label is not available in U-Boot.
918 info->volume_label[0] = 0;
919 } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
920 if (*buffer_size < 2) {
922 ret = EFI_BUFFER_TOO_SMALL;
927 ret = EFI_UNSUPPORTED;
931 return EFI_EXIT(ret);
934 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
935 const efi_guid_t *info_type,
936 efi_uintn_t buffer_size,
939 struct file_handle *fh = to_fh(file);
940 efi_status_t ret = EFI_UNSUPPORTED;
942 EFI_ENTRY("%p, %pUs, %zu, %p", file, info_type, buffer_size, buffer);
944 if (!guidcmp(info_type, &efi_file_info_guid)) {
945 struct efi_file_info *info = (struct efi_file_info *)buffer;
946 char *filename = basename(fh);
947 char *new_file_name, *pos;
950 /* The buffer will always contain a file name. */
951 if (buffer_size < sizeof(struct efi_file_info) + 2 ||
952 buffer_size < info->size) {
953 ret = EFI_BAD_BUFFER_SIZE;
956 /* We cannot change the directory attribute */
957 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
958 ret = EFI_ACCESS_DENIED;
961 /* Check for renaming */
962 new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1);
963 if (!new_file_name) {
964 ret = EFI_OUT_OF_RESOURCES;
968 utf16_utf8_strcpy(&pos, info->file_name);
969 if (strcmp(new_file_name, filename)) {
970 /* TODO: we do not support renaming */
971 EFI_PRINT("Renaming not supported\n");
973 ret = EFI_ACCESS_DENIED;
977 /* Check for truncation */
978 ret = efi_get_file_size(fh, &file_size);
979 if (ret != EFI_SUCCESS)
981 if (file_size != info->file_size) {
982 /* TODO: we do not support truncation */
983 EFI_PRINT("Truncation not supported\n");
984 ret = EFI_ACCESS_DENIED;
988 * We do not care for the other attributes
989 * TODO: Support read only
993 /* TODO: We do not support changing the volume label */
994 ret = EFI_UNSUPPORTED;
997 return EFI_EXIT(ret);
1001 * efi_file_flush_int() - flush file
1003 * This is the internal implementation of the Flush() and FlushEx() services of
1004 * the EFI_FILE_PROTOCOL.
1006 * @this: file protocol instance
1007 * Return: status code
1009 static efi_status_t efi_file_flush_int(struct efi_file_handle *this)
1011 struct file_handle *fh = to_fh(this);
1014 return EFI_INVALID_PARAMETER;
1016 if (!(fh->open_mode & EFI_FILE_MODE_WRITE))
1017 return EFI_ACCESS_DENIED;
1019 /* TODO: flush for file position after end of file */
1024 * efi_file_flush() - flush file
1026 * This function implements the Flush() service of the EFI_FILE_PROTOCOL.
1028 * See the Unified Extensible Firmware Interface (UEFI) specification for
1031 * @this: file protocol instance
1032 * Return: status code
1034 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *this)
1038 EFI_ENTRY("%p", this);
1040 ret = efi_file_flush_int(this);
1042 return EFI_EXIT(ret);
1046 * efi_file_flush_ex() - flush file
1048 * This function implements the FlushEx() service of the EFI_FILE_PROTOCOL.
1050 * See the Unified Extensible Firmware Interface (UEFI) specification for
1053 * @this: file protocol instance
1054 * @token: transaction token
1055 * Return: status code
1057 static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *this,
1058 struct efi_file_io_token *token)
1062 EFI_ENTRY("%p, %p", this, token);
1065 ret = EFI_INVALID_PARAMETER;
1069 ret = efi_file_flush_int(this);
1071 if (ret == EFI_SUCCESS && token->event) {
1072 token->status = EFI_SUCCESS;
1073 efi_signal_event(token->event);
1077 return EFI_EXIT(ret);
1080 static const struct efi_file_handle efi_file_handle_protocol = {
1081 .rev = EFI_FILE_PROTOCOL_REVISION2,
1082 .open = efi_file_open,
1083 .close = efi_file_close,
1084 .delete = efi_file_delete,
1085 .read = efi_file_read,
1086 .write = efi_file_write,
1087 .getpos = efi_file_getpos,
1088 .setpos = efi_file_setpos,
1089 .getinfo = efi_file_getinfo,
1090 .setinfo = efi_file_setinfo,
1091 .flush = efi_file_flush,
1092 .open_ex = efi_file_open_ex,
1093 .read_ex = efi_file_read_ex,
1094 .write_ex = efi_file_write_ex,
1095 .flush_ex = efi_file_flush_ex,
1099 * efi_file_from_path() - open file via device path
1101 * The device path @fp consists of the device path of the handle with the
1102 * simple file system protocol and one or more file path device path nodes.
1103 * The concatenation of all file path names provides the total file path.
1105 * The code starts at the first file path node and tries to open that file or
1106 * directory. If there is a succeding file path node, the code opens it relative
1107 * to this directory and continues iterating until reaching the last file path
1111 * Return: EFI_FILE_PROTOCOL for the file or NULL
1113 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
1115 struct efi_simple_file_system_protocol *v;
1116 struct efi_file_handle *f;
1119 v = efi_fs_from_path(fp);
1123 EFI_CALL(ret = v->open_volume(v, &f));
1124 if (ret != EFI_SUCCESS)
1127 /* Skip over device-path nodes before the file path. */
1128 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
1129 fp = efi_dp_next(fp);
1132 * Step through the nodes of the directory path until the actual file
1133 * node is reached which is the final node in the device path.
1136 struct efi_device_path_file_path *fdp =
1137 container_of(fp, struct efi_device_path_file_path, dp);
1138 struct efi_file_handle *f2;
1141 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
1142 printf("bad file path!\n");
1143 EFI_CALL(f->close(f));
1148 * UEFI specification requires pointers that are passed to
1149 * protocol member functions to be aligned. So memcpy it
1152 filename = u16_strdup(fdp->str);
1155 EFI_CALL(ret = f->open(f, &f2, filename,
1156 EFI_FILE_MODE_READ, 0));
1158 if (ret != EFI_SUCCESS)
1161 fp = efi_dp_next(fp);
1163 EFI_CALL(f->close(f));
1170 efi_status_t efi_open_volume_int(struct efi_simple_file_system_protocol *this,
1171 struct efi_file_handle **root)
1173 struct file_system *fs = to_fs(this);
1175 *root = file_open(fs, NULL, NULL, 0, 0);
1180 static efi_status_t EFIAPI
1181 efi_open_volume(struct efi_simple_file_system_protocol *this,
1182 struct efi_file_handle **root)
1184 EFI_ENTRY("%p, %p", this, root);
1186 return EFI_EXIT(efi_open_volume_int(this, root));
1189 struct efi_simple_file_system_protocol *
1190 efi_simple_file_system(struct blk_desc *desc, int part,
1191 struct efi_device_path *dp)
1193 struct file_system *fs;
1195 fs = calloc(1, sizeof(*fs));
1196 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
1197 fs->base.open_volume = efi_open_volume;