1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
10 #include <efi_loader.h>
15 /* GUID for file system information */
16 const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
18 /* GUID to obtain the volume label */
19 const efi_guid_t efi_system_volume_label_id = EFI_FILE_SYSTEM_VOLUME_LABEL_ID;
22 struct efi_simple_file_system_protocol base;
23 struct efi_device_path *dp;
24 struct blk_desc *desc;
27 #define to_fs(x) container_of(x, struct file_system, base)
30 struct efi_file_handle base;
31 struct file_system *fs;
32 loff_t offset; /* current file position/cursor */
36 /* for reading a directory: */
37 struct fs_dir_stream *dirs;
38 struct fs_dirent *dent;
42 #define to_fh(x) container_of(x, struct file_handle, base)
44 static const struct efi_file_handle efi_file_handle_protocol;
46 static char *basename(struct file_handle *fh)
48 char *s = strrchr(fh->path, '/');
54 static int set_blk_dev(struct file_handle *fh)
56 return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
60 * is_dir() - check if file handle points to directory
62 * We assume that set_blk_dev(fh) has been called already.
65 * Return: true if file handle points to a directory
67 static int is_dir(struct file_handle *fh)
69 struct fs_dir_stream *dirs;
71 dirs = fs_opendir(fh->path);
81 * Normalize a path which may include either back or fwd slashes,
82 * double slashes, . or .. entries in the path, etc.
84 static int sanitize_path(char *path)
88 /* backslash to slash: */
90 while ((p = strchr(p, '\\')))
93 /* handle double-slashes: */
95 while ((p = strstr(p, "//"))) {
97 memmove(p, src, strlen(src) + 1);
100 /* handle extra /.'s */
102 while ((p = strstr(p, "/."))) {
104 * You'd be tempted to do this *after* handling ".."s
105 * below to avoid having to check if "/." is start of
106 * a "/..", but that won't have the correct results..
107 * for example, "/foo/./../bar" would get resolved to
108 * "/foo/bar" if you did these two passes in the other
116 memmove(p, src, strlen(src) + 1);
119 /* handle extra /..'s: */
121 while ((p = strstr(p, "/.."))) {
126 /* find beginning of previous path entry: */
135 memmove(p, src, strlen(src) + 1);
142 * efi_create_file() - create file or directory
145 * @attributes: attributes for newly created file
146 * Returns: 0 for success
148 static int efi_create_file(struct file_handle *fh, u64 attributes)
151 void *buffer = &actwrite;
153 if (attributes & EFI_FILE_DIRECTORY)
154 return fs_mkdir(fh->path);
156 return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
161 * file_open() - open a file handle
164 * @parent: directory relative to which the file is to be opened
165 * @file_name: path of the file to be opened. '\', '.', or '..' may
166 * be used as modifiers. A leading backslash indicates an
168 * @open_mode: bit mask indicating the access mode (read, write,
170 * @attributes: attributes for newly created file
171 * Returns: handle to the opened file or NULL
173 static struct efi_file_handle *file_open(struct file_system *fs,
174 struct file_handle *parent, u16 *file_name, u64 open_mode,
177 struct file_handle *fh;
178 char f0[MAX_UTF8_PER_UTF16] = {0};
183 utf16_to_utf8((u8 *)f0, file_name, 1);
184 flen = u16_strlen(file_name);
187 /* we could have a parent, but also an absolute path: */
191 plen = strlen(parent->path) + 1;
194 /* +2 is for null and '/' */
195 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
197 fh->open_mode = open_mode;
198 fh->base = efi_file_handle_protocol;
206 strcpy(p, parent->path);
211 utf16_to_utf8((u8 *)p, file_name, flen);
213 if (sanitize_path(fh->path))
216 /* check if file exists: */
220 exists = fs_exists(fh->path);
221 /* fs_exists() calls fs_close(), so open file system again */
226 if (!(open_mode & EFI_FILE_MODE_CREATE) ||
227 efi_create_file(fh, attributes))
233 /* figure out if file is a directory: */
234 fh->isdir = is_dir(fh);
237 strcpy(fh->path, "");
247 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
248 struct efi_file_handle **new_handle,
249 u16 *file_name, u64 open_mode, u64 attributes)
251 struct file_handle *fh = to_fh(file);
254 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
255 file_name, open_mode, attributes);
257 /* Check parameters */
258 if (!file || !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);
293 return EFI_EXIT(ret);
296 static efi_status_t file_close(struct file_handle *fh)
298 fs_closedir(fh->dirs);
303 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
305 struct file_handle *fh = to_fh(file);
306 EFI_ENTRY("%p", file);
307 return EFI_EXIT(file_close(fh));
310 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
312 struct file_handle *fh = to_fh(file);
313 efi_status_t ret = EFI_SUCCESS;
315 EFI_ENTRY("%p", file);
317 if (set_blk_dev(fh) || fs_unlink(fh->path))
318 ret = EFI_WARN_DELETE_FAILURE;
321 return EFI_EXIT(ret);
325 * efi_get_file_size() - determine the size of a file
328 * @file_size: pointer to receive file size
329 * Return: status code
331 static efi_status_t efi_get_file_size(struct file_handle *fh,
335 return EFI_DEVICE_ERROR;
337 if (fs_size(fh->path, file_size))
338 return EFI_DEVICE_ERROR;
343 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
350 ret = efi_get_file_size(fh, &file_size);
351 if (ret != EFI_SUCCESS)
353 if (file_size < fh->offset) {
354 ret = EFI_DEVICE_ERROR;
359 return EFI_DEVICE_ERROR;
360 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
361 *buffer_size, &actread))
362 return EFI_DEVICE_ERROR;
364 *buffer_size = actread;
365 fh->offset += actread;
370 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
373 struct efi_file_info *info = buffer;
374 struct fs_dirent *dent;
379 return EFI_DEVICE_ERROR;
382 assert(fh->offset == 0);
383 fh->dirs = fs_opendir(fh->path);
385 return EFI_DEVICE_ERROR;
390 * So this is a bit awkward. Since fs layer is stateful and we
391 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
392 * we might have to return without consuming the dent.. so we
393 * have to stash it for next call.
398 dent = fs_readdir(fh->dirs);
402 /* no more files in directory */
407 /* check buffer size: */
408 required_size = sizeof(*info) +
409 2 * (utf8_utf16_strlen(dent->name) + 1);
410 if (*buffer_size < required_size) {
411 *buffer_size = required_size;
413 return EFI_BUFFER_TOO_SMALL;
417 *buffer_size = required_size;
418 memset(info, 0, required_size);
420 info->size = required_size;
421 info->file_size = dent->size;
422 info->physical_size = dent->size;
424 if (dent->type == FS_DT_DIR)
425 info->attribute |= EFI_FILE_DIRECTORY;
427 dst = info->file_name;
428 utf8_utf16_strcpy(&dst, dent->name);
435 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
436 efi_uintn_t *buffer_size, void *buffer)
438 struct file_handle *fh = to_fh(file);
439 efi_status_t ret = EFI_SUCCESS;
442 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
444 if (!buffer_size || !buffer) {
445 ret = EFI_INVALID_PARAMETER;
451 ret = dir_read(fh, &bs, buffer);
453 ret = file_read(fh, &bs, buffer);
457 *buffer_size = SIZE_MAX;
460 return EFI_EXIT(ret);
464 * efi_file_write() - write to file
466 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
468 * See the Unified Extensible Firmware Interface (UEFI) specification for
472 * @buffer_size: number of bytes to write
473 * @buffer: buffer with the bytes to write
474 * Return: status code
476 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
477 efi_uintn_t *buffer_size,
480 struct file_handle *fh = to_fh(file);
481 efi_status_t ret = EFI_SUCCESS;
484 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
486 if (!file || !buffer_size || !buffer) {
487 ret = EFI_INVALID_PARAMETER;
491 ret = EFI_UNSUPPORTED;
494 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
495 ret = EFI_ACCESS_DENIED;
502 if (set_blk_dev(fh)) {
503 ret = EFI_DEVICE_ERROR;
506 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
508 ret = EFI_DEVICE_ERROR;
511 *buffer_size = actwrite;
512 fh->offset += actwrite;
515 return EFI_EXIT(ret);
519 * efi_file_getpos() - get current position in file
521 * This function implements the GetPosition service of the EFI file protocol.
522 * See the UEFI spec for details.
525 * @pos: pointer to file position
526 * Return: status code
528 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
531 efi_status_t ret = EFI_SUCCESS;
532 struct file_handle *fh = to_fh(file);
534 EFI_ENTRY("%p, %p", file, pos);
537 ret = EFI_UNSUPPORTED;
543 return EFI_EXIT(ret);
547 * efi_file_setpos() - set current position in file
549 * This function implements the SetPosition service of the EFI file protocol.
550 * See the UEFI spec for details.
553 * @pos: new file position
554 * Return: status code
556 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
559 struct file_handle *fh = to_fh(file);
560 efi_status_t ret = EFI_SUCCESS;
562 EFI_ENTRY("%p, %llu", file, pos);
566 ret = EFI_UNSUPPORTED;
569 fs_closedir(fh->dirs);
576 ret = efi_get_file_size(fh, &file_size);
577 if (ret != EFI_SUCCESS)
585 return EFI_EXIT(ret);
588 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
589 const efi_guid_t *info_type,
590 efi_uintn_t *buffer_size,
593 struct file_handle *fh = to_fh(file);
594 efi_status_t ret = EFI_SUCCESS;
597 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
599 if (!file || !info_type || !buffer_size ||
600 (*buffer_size && !buffer)) {
601 ret = EFI_INVALID_PARAMETER;
605 if (!guidcmp(info_type, &efi_file_info_guid)) {
606 struct efi_file_info *info = buffer;
607 char *filename = basename(fh);
608 unsigned int required_size;
611 /* check buffer size: */
612 required_size = sizeof(*info) +
613 2 * (utf8_utf16_strlen(filename) + 1);
614 if (*buffer_size < required_size) {
615 *buffer_size = required_size;
616 ret = EFI_BUFFER_TOO_SMALL;
620 ret = efi_get_file_size(fh, &file_size);
621 if (ret != EFI_SUCCESS)
624 memset(info, 0, required_size);
626 info->size = required_size;
627 info->file_size = file_size;
628 info->physical_size = file_size;
631 info->attribute |= EFI_FILE_DIRECTORY;
633 dst = info->file_name;
634 utf8_utf16_strcpy(&dst, filename);
635 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
636 struct efi_file_system_info *info = buffer;
637 disk_partition_t part;
638 efi_uintn_t required_size;
641 if (fh->fs->part >= 1)
642 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
644 r = part_get_info_whole_disk(fh->fs->desc, &part);
646 ret = EFI_DEVICE_ERROR;
649 required_size = sizeof(*info) + 2;
650 if (*buffer_size < required_size) {
651 *buffer_size = required_size;
652 ret = EFI_BUFFER_TOO_SMALL;
656 memset(info, 0, required_size);
658 info->size = required_size;
659 info->read_only = true;
660 info->volume_size = part.size * part.blksz;
661 info->free_space = 0;
662 info->block_size = part.blksz;
664 * TODO: The volume label is not available in U-Boot.
666 info->volume_label[0] = 0;
667 } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
668 if (*buffer_size < 2) {
670 ret = EFI_BUFFER_TOO_SMALL;
675 ret = EFI_UNSUPPORTED;
679 return EFI_EXIT(ret);
682 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
683 const efi_guid_t *info_type,
684 efi_uintn_t buffer_size,
687 struct file_handle *fh = to_fh(file);
688 efi_status_t ret = EFI_UNSUPPORTED;
690 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
692 if (!guidcmp(info_type, &efi_file_info_guid)) {
693 struct efi_file_info *info = (struct efi_file_info *)buffer;
694 char *filename = basename(fh);
695 char *new_file_name, *pos;
698 /* The buffer will always contain a file name. */
699 if (buffer_size < sizeof(struct efi_file_info) + 2 ||
700 buffer_size < info->size) {
701 ret = EFI_BAD_BUFFER_SIZE;
704 /* We cannot change the directory attribute */
705 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
706 ret = EFI_ACCESS_DENIED;
709 /* Check for renaming */
710 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
711 if (!new_file_name) {
712 ret = EFI_OUT_OF_RESOURCES;
716 utf16_utf8_strcpy(&pos, info->file_name);
717 if (strcmp(new_file_name, filename)) {
718 /* TODO: we do not support renaming */
719 EFI_PRINT("Renaming not supported\n");
721 ret = EFI_ACCESS_DENIED;
725 /* Check for truncation */
726 ret = efi_get_file_size(fh, &file_size);
727 if (ret != EFI_SUCCESS)
729 if (file_size != info->file_size) {
730 /* TODO: we do not support truncation */
731 EFI_PRINT("Truncation not supported\n");
732 ret = EFI_ACCESS_DENIED;
736 * We do not care for the other attributes
737 * TODO: Support read only
741 /* TODO: We do not support changing the volume label */
742 ret = EFI_UNSUPPORTED;
745 return EFI_EXIT(ret);
748 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
750 EFI_ENTRY("%p", file);
751 return EFI_EXIT(EFI_SUCCESS);
754 static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *file,
755 struct efi_file_handle **new_handle,
756 u16 *file_name, u64 open_mode, u64 attributes,
757 struct efi_file_io_token *token)
759 return EFI_UNSUPPORTED;
762 static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *file,
763 struct efi_file_io_token *token)
765 return EFI_UNSUPPORTED;
768 static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *file,
769 struct efi_file_io_token *token)
771 return EFI_UNSUPPORTED;
774 static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *file,
775 struct efi_file_io_token *token)
777 return EFI_UNSUPPORTED;
780 static const struct efi_file_handle efi_file_handle_protocol = {
781 .rev = EFI_FILE_PROTOCOL_REVISION2,
782 .open = efi_file_open,
783 .close = efi_file_close,
784 .delete = efi_file_delete,
785 .read = efi_file_read,
786 .write = efi_file_write,
787 .getpos = efi_file_getpos,
788 .setpos = efi_file_setpos,
789 .getinfo = efi_file_getinfo,
790 .setinfo = efi_file_setinfo,
791 .flush = efi_file_flush,
792 .open_ex = efi_file_open_ex,
793 .read_ex = efi_file_read_ex,
794 .write_ex = efi_file_write_ex,
795 .flush_ex = efi_file_flush_ex,
799 * efi_file_from_path() - open file via device path
802 * @return: EFI_FILE_PROTOCOL for the file or NULL
804 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
806 struct efi_simple_file_system_protocol *v;
807 struct efi_file_handle *f;
810 v = efi_fs_from_path(fp);
814 EFI_CALL(ret = v->open_volume(v, &f));
815 if (ret != EFI_SUCCESS)
818 /* Skip over device-path nodes before the file path. */
819 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
820 fp = efi_dp_next(fp);
823 * Step through the nodes of the directory path until the actual file
824 * node is reached which is the final node in the device path.
827 struct efi_device_path_file_path *fdp =
828 container_of(fp, struct efi_device_path_file_path, dp);
829 struct efi_file_handle *f2;
832 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
833 printf("bad file path!\n");
838 filename = u16_strdup(fdp->str);
841 EFI_CALL(ret = f->open(f, &f2, filename,
842 EFI_FILE_MODE_READ, 0));
844 if (ret != EFI_SUCCESS)
847 fp = efi_dp_next(fp);
849 EFI_CALL(f->close(f));
856 static efi_status_t EFIAPI
857 efi_open_volume(struct efi_simple_file_system_protocol *this,
858 struct efi_file_handle **root)
860 struct file_system *fs = to_fs(this);
862 EFI_ENTRY("%p, %p", this, root);
864 *root = file_open(fs, NULL, NULL, 0, 0);
866 return EFI_EXIT(EFI_SUCCESS);
869 struct efi_simple_file_system_protocol *
870 efi_simple_file_system(struct blk_desc *desc, int part,
871 struct efi_device_path *dp)
873 struct file_system *fs;
875 fs = calloc(1, sizeof(*fs));
876 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
877 fs->base.open_volume = efi_open_volume;