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;
660 * TODO: We cannot determine if the volume can be written to.
662 info->read_only = false;
663 info->volume_size = part.size * part.blksz;
665 * TODO: We currently have no function to determine the free
666 * space. The volume size is the best upper bound we have.
668 info->free_space = info->volume_size;
669 info->block_size = part.blksz;
671 * TODO: The volume label is not available in U-Boot.
673 info->volume_label[0] = 0;
674 } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
675 if (*buffer_size < 2) {
677 ret = EFI_BUFFER_TOO_SMALL;
682 ret = EFI_UNSUPPORTED;
686 return EFI_EXIT(ret);
689 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
690 const efi_guid_t *info_type,
691 efi_uintn_t buffer_size,
694 struct file_handle *fh = to_fh(file);
695 efi_status_t ret = EFI_UNSUPPORTED;
697 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
699 if (!guidcmp(info_type, &efi_file_info_guid)) {
700 struct efi_file_info *info = (struct efi_file_info *)buffer;
701 char *filename = basename(fh);
702 char *new_file_name, *pos;
705 /* The buffer will always contain a file name. */
706 if (buffer_size < sizeof(struct efi_file_info) + 2 ||
707 buffer_size < info->size) {
708 ret = EFI_BAD_BUFFER_SIZE;
711 /* We cannot change the directory attribute */
712 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
713 ret = EFI_ACCESS_DENIED;
716 /* Check for renaming */
717 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
718 if (!new_file_name) {
719 ret = EFI_OUT_OF_RESOURCES;
723 utf16_utf8_strcpy(&pos, info->file_name);
724 if (strcmp(new_file_name, filename)) {
725 /* TODO: we do not support renaming */
726 EFI_PRINT("Renaming not supported\n");
728 ret = EFI_ACCESS_DENIED;
732 /* Check for truncation */
733 ret = efi_get_file_size(fh, &file_size);
734 if (ret != EFI_SUCCESS)
736 if (file_size != info->file_size) {
737 /* TODO: we do not support truncation */
738 EFI_PRINT("Truncation not supported\n");
739 ret = EFI_ACCESS_DENIED;
743 * We do not care for the other attributes
744 * TODO: Support read only
748 /* TODO: We do not support changing the volume label */
749 ret = EFI_UNSUPPORTED;
752 return EFI_EXIT(ret);
755 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
757 EFI_ENTRY("%p", file);
758 return EFI_EXIT(EFI_SUCCESS);
761 static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *file,
762 struct efi_file_handle **new_handle,
763 u16 *file_name, u64 open_mode, u64 attributes,
764 struct efi_file_io_token *token)
766 return EFI_UNSUPPORTED;
769 static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *file,
770 struct efi_file_io_token *token)
772 return EFI_UNSUPPORTED;
775 static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *file,
776 struct efi_file_io_token *token)
778 return EFI_UNSUPPORTED;
781 static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *file,
782 struct efi_file_io_token *token)
784 return EFI_UNSUPPORTED;
787 static const struct efi_file_handle efi_file_handle_protocol = {
788 .rev = EFI_FILE_PROTOCOL_REVISION2,
789 .open = efi_file_open,
790 .close = efi_file_close,
791 .delete = efi_file_delete,
792 .read = efi_file_read,
793 .write = efi_file_write,
794 .getpos = efi_file_getpos,
795 .setpos = efi_file_setpos,
796 .getinfo = efi_file_getinfo,
797 .setinfo = efi_file_setinfo,
798 .flush = efi_file_flush,
799 .open_ex = efi_file_open_ex,
800 .read_ex = efi_file_read_ex,
801 .write_ex = efi_file_write_ex,
802 .flush_ex = efi_file_flush_ex,
806 * efi_file_from_path() - open file via device path
809 * @return: EFI_FILE_PROTOCOL for the file or NULL
811 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
813 struct efi_simple_file_system_protocol *v;
814 struct efi_file_handle *f;
817 v = efi_fs_from_path(fp);
821 EFI_CALL(ret = v->open_volume(v, &f));
822 if (ret != EFI_SUCCESS)
825 /* Skip over device-path nodes before the file path. */
826 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
827 fp = efi_dp_next(fp);
830 * Step through the nodes of the directory path until the actual file
831 * node is reached which is the final node in the device path.
834 struct efi_device_path_file_path *fdp =
835 container_of(fp, struct efi_device_path_file_path, dp);
836 struct efi_file_handle *f2;
839 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
840 printf("bad file path!\n");
845 filename = u16_strdup(fdp->str);
848 EFI_CALL(ret = f->open(f, &f2, filename,
849 EFI_FILE_MODE_READ, 0));
851 if (ret != EFI_SUCCESS)
854 fp = efi_dp_next(fp);
856 EFI_CALL(f->close(f));
863 static efi_status_t EFIAPI
864 efi_open_volume(struct efi_simple_file_system_protocol *this,
865 struct efi_file_handle **root)
867 struct file_system *fs = to_fs(this);
869 EFI_ENTRY("%p, %p", this, root);
871 *root = file_open(fs, NULL, NULL, 0, 0);
873 return EFI_EXIT(EFI_SUCCESS);
876 struct efi_simple_file_system_protocol *
877 efi_simple_file_system(struct blk_desc *desc, int part,
878 struct efi_device_path *dp)
880 struct file_system *fs;
882 fs = calloc(1, sizeof(*fs));
883 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
884 fs->base.open_volume = efi_open_volume;