1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
9 #include <efi_loader.h>
16 /* GUID for file system information */
17 const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
19 /* GUID to obtain the volume label */
20 const efi_guid_t efi_system_volume_label_id = EFI_FILE_SYSTEM_VOLUME_LABEL_ID;
23 struct efi_simple_file_system_protocol base;
24 struct efi_device_path *dp;
25 struct blk_desc *desc;
28 #define to_fs(x) container_of(x, struct file_system, base)
31 struct efi_file_handle base;
32 struct file_system *fs;
33 loff_t offset; /* current file position/cursor */
37 /* for reading a directory: */
38 struct fs_dir_stream *dirs;
39 struct fs_dirent *dent;
43 #define to_fh(x) container_of(x, struct file_handle, base)
45 static const struct efi_file_handle efi_file_handle_protocol;
47 static char *basename(struct file_handle *fh)
49 char *s = strrchr(fh->path, '/');
55 static int set_blk_dev(struct file_handle *fh)
57 return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
61 * is_dir() - check if file handle points to directory
63 * We assume that set_blk_dev(fh) has been called already.
66 * Return: true if file handle points to a directory
68 static int is_dir(struct file_handle *fh)
70 struct fs_dir_stream *dirs;
72 dirs = fs_opendir(fh->path);
82 * Normalize a path which may include either back or fwd slashes,
83 * double slashes, . or .. entries in the path, etc.
85 static int sanitize_path(char *path)
89 /* backslash to slash: */
91 while ((p = strchr(p, '\\')))
94 /* handle double-slashes: */
96 while ((p = strstr(p, "//"))) {
98 memmove(p, src, strlen(src) + 1);
101 /* handle extra /.'s */
103 while ((p = strstr(p, "/."))) {
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
117 memmove(p, src, strlen(src) + 1);
120 /* handle extra /..'s: */
122 while ((p = strstr(p, "/.."))) {
127 /* find beginning of previous path entry: */
136 memmove(p, src, strlen(src) + 1);
143 * efi_create_file() - create file or directory
146 * @attributes: attributes for newly created file
147 * Returns: 0 for success
149 static int efi_create_file(struct file_handle *fh, u64 attributes)
152 void *buffer = &actwrite;
154 if (attributes & EFI_FILE_DIRECTORY)
155 return fs_mkdir(fh->path);
157 return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
162 * file_open() - open a file handle
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
169 * @open_mode: bit mask indicating the access mode (read, write,
171 * @attributes: attributes for newly created file
172 * Returns: handle to the opened file or NULL
174 static struct efi_file_handle *file_open(struct file_system *fs,
175 struct file_handle *parent, u16 *file_name, u64 open_mode,
178 struct file_handle *fh;
179 char f0[MAX_UTF8_PER_UTF16] = {0};
184 utf16_to_utf8((u8 *)f0, file_name, 1);
185 flen = u16_strlen(file_name);
188 /* we could have a parent, but also an absolute path: */
192 plen = strlen(parent->path) + 1;
195 /* +2 is for null and '/' */
196 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
200 fh->open_mode = open_mode;
201 fh->base = efi_file_handle_protocol;
209 strcpy(p, parent->path);
214 utf16_to_utf8((u8 *)p, file_name, flen);
216 if (sanitize_path(fh->path))
219 /* check if file exists: */
223 exists = fs_exists(fh->path);
224 /* fs_exists() calls fs_close(), so open file system again */
229 if (!(open_mode & EFI_FILE_MODE_CREATE) ||
230 efi_create_file(fh, attributes))
236 /* figure out if file is a directory: */
237 fh->isdir = is_dir(fh);
240 strcpy(fh->path, "");
250 efi_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,
255 struct file_handle *fh = to_fh(this);
258 /* Check parameters */
259 if (!this || !new_handle || !file_name) {
260 ret = EFI_INVALID_PARAMETER;
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;
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
276 * So here we only check attributes in create mode and do not check
277 * that they are zero otherwise.
279 if ((open_mode & EFI_FILE_MODE_CREATE) &&
280 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
281 ret = EFI_INVALID_PARAMETER;
286 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
288 EFI_PRINT("file handle %p\n", *new_handle);
298 * efi_file_open() - open file synchronously
300 * This function implements the Open service of the File Protocol.
301 * See the UEFI spec for details.
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
309 static 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,
316 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", this, new_handle,
317 file_name, open_mode, attributes);
319 ret = efi_file_open_int(this, new_handle, file_name, open_mode,
322 return EFI_EXIT(ret);
326 * efi_file_open_ex() - open file asynchronously
328 * This function implements the OpenEx service of the File Protocol.
329 * See the UEFI spec for details.
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
338 static 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,
342 struct efi_file_io_token *token)
346 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu, %p", this, new_handle,
347 file_name, open_mode, attributes, token);
350 ret = EFI_INVALID_PARAMETER;
354 ret = efi_file_open_int(this, new_handle, file_name, open_mode,
357 if (ret == EFI_SUCCESS && token->event) {
358 token->status = EFI_SUCCESS;
359 efi_signal_event(token->event);
363 return EFI_EXIT(ret);
366 static efi_status_t file_close(struct file_handle *fh)
368 fs_closedir(fh->dirs);
373 efi_status_t efi_file_close_int(struct efi_file_handle *file)
375 struct file_handle *fh = to_fh(file);
377 return file_close(fh);
380 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
382 EFI_ENTRY("%p", file);
383 return EFI_EXIT(efi_file_close_int(file));
386 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
388 struct file_handle *fh = to_fh(file);
389 efi_status_t ret = EFI_SUCCESS;
391 EFI_ENTRY("%p", file);
393 if (set_blk_dev(fh) || fs_unlink(fh->path))
394 ret = EFI_WARN_DELETE_FAILURE;
397 return EFI_EXIT(ret);
401 * efi_get_file_size() - determine the size of a file
404 * @file_size: pointer to receive file size
405 * Return: status code
407 static efi_status_t efi_get_file_size(struct file_handle *fh,
411 return EFI_DEVICE_ERROR;
413 if (fs_size(fh->path, file_size))
414 return EFI_DEVICE_ERROR;
420 * efi_file_size() - Get the size of a file using an EFI file handle
422 * @fh: EFI file handle
423 * @size: buffer to fill in the discovered size
425 * Return: size of the file
427 efi_status_t efi_file_size(struct efi_file_handle *fh, efi_uintn_t *size)
429 struct efi_file_info *info = NULL;
434 ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
436 if (ret != EFI_BUFFER_TOO_SMALL) {
437 ret = EFI_DEVICE_ERROR;
443 ret = EFI_OUT_OF_RESOURCES;
446 ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
448 if (ret != EFI_SUCCESS)
451 *size = info->file_size;
458 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
466 ret = EFI_INVALID_PARAMETER;
470 ret = efi_get_file_size(fh, &file_size);
471 if (ret != EFI_SUCCESS)
473 if (file_size < fh->offset) {
474 ret = EFI_DEVICE_ERROR;
479 return EFI_DEVICE_ERROR;
480 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
481 *buffer_size, &actread))
482 return EFI_DEVICE_ERROR;
484 *buffer_size = actread;
485 fh->offset += actread;
490 static void rtc2efi(struct efi_time *time, struct rtc_time *tm)
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;
501 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
504 struct efi_file_info *info = buffer;
505 struct fs_dirent *dent;
510 return EFI_DEVICE_ERROR;
513 assert(fh->offset == 0);
514 fh->dirs = fs_opendir(fh->path);
516 return EFI_DEVICE_ERROR;
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.
529 dent = fs_readdir(fh->dirs);
533 /* no more files in directory */
538 /* check buffer size: */
539 required_size = sizeof(*info) +
540 2 * (utf8_utf16_strlen(dent->name) + 1);
541 if (*buffer_size < required_size) {
542 *buffer_size = required_size;
544 return EFI_BUFFER_TOO_SMALL;
547 return EFI_INVALID_PARAMETER;
550 *buffer_size = required_size;
551 memset(info, 0, required_size);
553 info->size = required_size;
554 info->file_size = dent->size;
555 info->physical_size = dent->size;
556 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);
561 if (dent->type == FS_DT_DIR)
562 info->attribute |= EFI_FILE_DIRECTORY;
564 dst = info->file_name;
565 utf8_utf16_strcpy(&dst, dent->name);
572 efi_status_t efi_file_read_int(struct efi_file_handle *this,
573 efi_uintn_t *buffer_size, void *buffer)
575 struct file_handle *fh = to_fh(this);
576 efi_status_t ret = EFI_SUCCESS;
579 if (!this || !buffer_size)
580 return EFI_INVALID_PARAMETER;
584 ret = dir_read(fh, &bs, buffer);
586 ret = file_read(fh, &bs, buffer);
590 *buffer_size = SIZE_MAX;
596 * efi_file_read() - read file
598 * This function implements the Read() service of the EFI_FILE_PROTOCOL.
600 * See the Unified Extensible Firmware Interface (UEFI) specification for
603 * @this: file protocol instance
604 * @buffer_size: number of bytes to read
605 * @buffer: read buffer
606 * Return: status code
608 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *this,
609 efi_uintn_t *buffer_size, void *buffer)
613 EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
615 ret = efi_file_read_int(this, buffer_size, buffer);
617 return EFI_EXIT(ret);
621 * efi_file_read_ex() - read file asynchonously
623 * This function implements the ReadEx() service of the EFI_FILE_PROTOCOL.
625 * See the Unified Extensible Firmware Interface (UEFI) specification for
628 * @this: file protocol instance
629 * @token: transaction token
630 * Return: status code
632 static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *this,
633 struct efi_file_io_token *token)
637 EFI_ENTRY("%p, %p", this, token);
640 ret = EFI_INVALID_PARAMETER;
644 ret = efi_file_read_int(this, &token->buffer_size, token->buffer);
646 if (ret == EFI_SUCCESS && token->event) {
647 token->status = EFI_SUCCESS;
648 efi_signal_event(token->event);
652 return EFI_EXIT(ret);
655 static efi_status_t efi_file_write_int(struct efi_file_handle *this,
656 efi_uintn_t *buffer_size, void *buffer)
658 struct file_handle *fh = to_fh(this);
659 efi_status_t ret = EFI_SUCCESS;
662 if (!this || !buffer_size || !buffer) {
663 ret = EFI_INVALID_PARAMETER;
667 ret = EFI_UNSUPPORTED;
670 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
671 ret = EFI_ACCESS_DENIED;
678 if (set_blk_dev(fh)) {
679 ret = EFI_DEVICE_ERROR;
682 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
684 ret = EFI_DEVICE_ERROR;
687 *buffer_size = actwrite;
688 fh->offset += actwrite;
695 * efi_file_write() - write to file
697 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
699 * See the Unified Extensible Firmware Interface (UEFI) specification for
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
707 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *this,
708 efi_uintn_t *buffer_size,
713 EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
715 ret = efi_file_write_int(this, buffer_size, buffer);
717 return EFI_EXIT(ret);
721 * efi_file_write_ex() - write to file
723 * This function implements the WriteEx() service of the EFI_FILE_PROTOCOL.
725 * See the Unified Extensible Firmware Interface (UEFI) specification for
728 * @this: file protocol instance
729 * @token: transaction token
730 * Return: status code
732 static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *this,
733 struct efi_file_io_token *token)
737 EFI_ENTRY("%p, %p", this, token);
740 ret = EFI_INVALID_PARAMETER;
744 ret = efi_file_write_int(this, &token->buffer_size, token->buffer);
746 if (ret == EFI_SUCCESS && token->event) {
747 token->status = EFI_SUCCESS;
748 efi_signal_event(token->event);
752 return EFI_EXIT(ret);
756 * efi_file_getpos() - get current position in file
758 * This function implements the GetPosition service of the EFI file protocol.
759 * See the UEFI spec for details.
762 * @pos: pointer to file position
763 * Return: status code
765 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
768 efi_status_t ret = EFI_SUCCESS;
769 struct file_handle *fh = to_fh(file);
771 EFI_ENTRY("%p, %p", file, pos);
774 ret = EFI_UNSUPPORTED;
780 return EFI_EXIT(ret);
783 efi_status_t efi_file_setpos_int(struct efi_file_handle *file, u64 pos)
785 struct file_handle *fh = to_fh(file);
786 efi_status_t ret = EFI_SUCCESS;
790 ret = EFI_UNSUPPORTED;
793 fs_closedir(fh->dirs);
800 ret = efi_get_file_size(fh, &file_size);
801 if (ret != EFI_SUCCESS)
813 * efi_file_setpos() - set current position in file
815 * This function implements the SetPosition service of the EFI file protocol.
816 * See the UEFI spec for details.
819 * @pos: new file position
820 * Return: status code
822 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
825 efi_status_t ret = EFI_SUCCESS;
827 EFI_ENTRY("%p, %llu", file, pos);
829 ret = efi_file_setpos_int(file, pos);
831 return EFI_EXIT(ret);
834 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
835 const efi_guid_t *info_type,
836 efi_uintn_t *buffer_size,
839 struct file_handle *fh = to_fh(file);
840 efi_status_t ret = EFI_SUCCESS;
843 EFI_ENTRY("%p, %pUs, %p, %p", file, info_type, buffer_size, buffer);
845 if (!file || !info_type || !buffer_size ||
846 (*buffer_size && !buffer)) {
847 ret = EFI_INVALID_PARAMETER;
851 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;
857 /* check buffer size: */
858 required_size = sizeof(*info) +
859 2 * (utf8_utf16_strlen(filename) + 1);
860 if (*buffer_size < required_size) {
861 *buffer_size = required_size;
862 ret = EFI_BUFFER_TOO_SMALL;
866 ret = efi_get_file_size(fh, &file_size);
867 if (ret != EFI_SUCCESS)
870 memset(info, 0, required_size);
872 info->size = required_size;
873 info->file_size = file_size;
874 info->physical_size = file_size;
877 info->attribute |= EFI_FILE_DIRECTORY;
879 dst = info->file_name;
880 utf8_utf16_strcpy(&dst, filename);
881 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
882 struct efi_file_system_info *info = buffer;
883 struct disk_partition part;
884 efi_uintn_t required_size;
887 if (fh->fs->part >= 1)
888 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
890 r = part_get_info_whole_disk(fh->fs->desc, &part);
892 ret = EFI_DEVICE_ERROR;
895 required_size = sizeof(*info) + 2;
896 if (*buffer_size < required_size) {
897 *buffer_size = required_size;
898 ret = EFI_BUFFER_TOO_SMALL;
902 memset(info, 0, required_size);
904 info->size = required_size;
906 * TODO: We cannot determine if the volume can be written to.
908 info->read_only = false;
909 info->volume_size = part.size * part.blksz;
911 * TODO: We currently have no function to determine the free
912 * space. The volume size is the best upper bound we have.
914 info->free_space = info->volume_size;
915 info->block_size = part.blksz;
917 * TODO: The volume label is not available in U-Boot.
919 info->volume_label[0] = 0;
920 } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
921 if (*buffer_size < 2) {
923 ret = EFI_BUFFER_TOO_SMALL;
928 ret = EFI_UNSUPPORTED;
932 return EFI_EXIT(ret);
935 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
936 const efi_guid_t *info_type,
937 efi_uintn_t buffer_size,
940 struct file_handle *fh = to_fh(file);
941 efi_status_t ret = EFI_UNSUPPORTED;
943 EFI_ENTRY("%p, %pUs, %zu, %p", file, info_type, buffer_size, buffer);
945 if (!guidcmp(info_type, &efi_file_info_guid)) {
946 struct efi_file_info *info = (struct efi_file_info *)buffer;
947 char *filename = basename(fh);
948 char *new_file_name, *pos;
951 /* The buffer will always contain a file name. */
952 if (buffer_size < sizeof(struct efi_file_info) + 2 ||
953 buffer_size < info->size) {
954 ret = EFI_BAD_BUFFER_SIZE;
957 /* We cannot change the directory attribute */
958 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
959 ret = EFI_ACCESS_DENIED;
962 /* Check for renaming */
963 new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1);
964 if (!new_file_name) {
965 ret = EFI_OUT_OF_RESOURCES;
969 utf16_utf8_strcpy(&pos, info->file_name);
970 if (strcmp(new_file_name, filename)) {
971 /* TODO: we do not support renaming */
972 EFI_PRINT("Renaming not supported\n");
974 ret = EFI_ACCESS_DENIED;
978 /* Check for truncation */
979 ret = efi_get_file_size(fh, &file_size);
980 if (ret != EFI_SUCCESS)
982 if (file_size != info->file_size) {
983 /* TODO: we do not support truncation */
984 EFI_PRINT("Truncation not supported\n");
985 ret = EFI_ACCESS_DENIED;
989 * We do not care for the other attributes
990 * TODO: Support read only
994 /* TODO: We do not support changing the volume label */
995 ret = EFI_UNSUPPORTED;
998 return EFI_EXIT(ret);
1002 * efi_file_flush_int() - flush file
1004 * This is the internal implementation of the Flush() and FlushEx() services of
1005 * the EFI_FILE_PROTOCOL.
1007 * @this: file protocol instance
1008 * Return: status code
1010 static efi_status_t efi_file_flush_int(struct efi_file_handle *this)
1012 struct file_handle *fh = to_fh(this);
1015 return EFI_INVALID_PARAMETER;
1017 if (!(fh->open_mode & EFI_FILE_MODE_WRITE))
1018 return EFI_ACCESS_DENIED;
1020 /* TODO: flush for file position after end of file */
1025 * efi_file_flush() - flush file
1027 * This function implements the Flush() service of the EFI_FILE_PROTOCOL.
1029 * See the Unified Extensible Firmware Interface (UEFI) specification for
1032 * @this: file protocol instance
1033 * Return: status code
1035 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *this)
1039 EFI_ENTRY("%p", this);
1041 ret = efi_file_flush_int(this);
1043 return EFI_EXIT(ret);
1047 * efi_file_flush_ex() - flush file
1049 * This function implements the FlushEx() service of the EFI_FILE_PROTOCOL.
1051 * See the Unified Extensible Firmware Interface (UEFI) specification for
1054 * @this: file protocol instance
1055 * @token: transaction token
1056 * Return: status code
1058 static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *this,
1059 struct efi_file_io_token *token)
1063 EFI_ENTRY("%p, %p", this, token);
1066 ret = EFI_INVALID_PARAMETER;
1070 ret = efi_file_flush_int(this);
1072 if (ret == EFI_SUCCESS && token->event) {
1073 token->status = EFI_SUCCESS;
1074 efi_signal_event(token->event);
1078 return EFI_EXIT(ret);
1081 static const struct efi_file_handle efi_file_handle_protocol = {
1082 .rev = EFI_FILE_PROTOCOL_REVISION2,
1083 .open = efi_file_open,
1084 .close = efi_file_close,
1085 .delete = efi_file_delete,
1086 .read = efi_file_read,
1087 .write = efi_file_write,
1088 .getpos = efi_file_getpos,
1089 .setpos = efi_file_setpos,
1090 .getinfo = efi_file_getinfo,
1091 .setinfo = efi_file_setinfo,
1092 .flush = efi_file_flush,
1093 .open_ex = efi_file_open_ex,
1094 .read_ex = efi_file_read_ex,
1095 .write_ex = efi_file_write_ex,
1096 .flush_ex = efi_file_flush_ex,
1100 * efi_file_from_path() - open file via device path
1102 * The device path @fp consists of the device path of the handle with the
1103 * simple file system protocol and one or more file path device path nodes.
1104 * The concatenation of all file path names provides the total file path.
1106 * The code starts at the first file path node and tries to open that file or
1107 * directory. If there is a succeding file path node, the code opens it relative
1108 * to this directory and continues iterating until reaching the last file path
1112 * Return: EFI_FILE_PROTOCOL for the file or NULL
1114 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
1116 struct efi_simple_file_system_protocol *v;
1117 struct efi_file_handle *f;
1120 v = efi_fs_from_path(fp);
1124 EFI_CALL(ret = v->open_volume(v, &f));
1125 if (ret != EFI_SUCCESS)
1128 /* Skip over device-path nodes before the file path. */
1129 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
1130 fp = efi_dp_next(fp);
1133 * Step through the nodes of the directory path until the actual file
1134 * node is reached which is the final node in the device path.
1137 struct efi_device_path_file_path *fdp =
1138 container_of(fp, struct efi_device_path_file_path, dp);
1139 struct efi_file_handle *f2;
1143 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
1144 printf("bad file path!\n");
1145 EFI_CALL(f->close(f));
1150 * UEFI specification requires pointers that are passed to
1151 * protocol member functions to be aligned. So memcpy it
1154 if (fdp->dp.length <= offsetof(struct efi_device_path_file_path, str))
1156 filename_sz = fdp->dp.length -
1157 offsetof(struct efi_device_path_file_path, str);
1158 filename = malloc(filename_sz);
1161 memcpy(filename, fdp->str, filename_sz);
1162 EFI_CALL(ret = f->open(f, &f2, filename,
1163 EFI_FILE_MODE_READ, 0));
1165 if (ret != EFI_SUCCESS)
1168 fp = efi_dp_next(fp);
1170 EFI_CALL(f->close(f));
1177 efi_status_t efi_open_volume_int(struct efi_simple_file_system_protocol *this,
1178 struct efi_file_handle **root)
1180 struct file_system *fs = to_fs(this);
1182 *root = file_open(fs, NULL, NULL, 0, 0);
1187 static efi_status_t EFIAPI
1188 efi_open_volume(struct efi_simple_file_system_protocol *this,
1189 struct efi_file_handle **root)
1191 EFI_ENTRY("%p, %p", this, root);
1193 return EFI_EXIT(efi_open_volume_int(this, root));
1197 efi_create_simple_file_system(struct blk_desc *desc, int part,
1198 struct efi_device_path *dp,
1199 struct efi_simple_file_system_protocol **fsp)
1201 struct file_system *fs;
1203 fs = calloc(1, sizeof(*fs));
1205 return EFI_OUT_OF_RESOURCES;
1206 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
1207 fs->base.open_volume = efi_open_volume;