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 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
250 struct efi_file_handle **new_handle,
251 u16 *file_name, u64 open_mode, u64 attributes)
253 struct file_handle *fh = to_fh(file);
256 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
257 file_name, open_mode, attributes);
259 /* Check parameters */
260 if (!file || !new_handle || !file_name) {
261 ret = EFI_INVALID_PARAMETER;
264 if (open_mode != EFI_FILE_MODE_READ &&
265 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
266 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
267 EFI_FILE_MODE_CREATE)) {
268 ret = EFI_INVALID_PARAMETER;
272 * The UEFI spec requires that attributes are only set in create mode.
273 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
274 * read mode. EDK2 does not check that attributes are zero if not in
277 * So here we only check attributes in create mode and do not check
278 * that they are zero otherwise.
280 if ((open_mode & EFI_FILE_MODE_CREATE) &&
281 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
282 ret = EFI_INVALID_PARAMETER;
287 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
289 EFI_PRINT("file handle %p\n", *new_handle);
295 return EFI_EXIT(ret);
298 static efi_status_t file_close(struct file_handle *fh)
300 fs_closedir(fh->dirs);
305 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
307 struct file_handle *fh = to_fh(file);
308 EFI_ENTRY("%p", file);
309 return EFI_EXIT(file_close(fh));
312 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
314 struct file_handle *fh = to_fh(file);
315 efi_status_t ret = EFI_SUCCESS;
317 EFI_ENTRY("%p", file);
319 if (set_blk_dev(fh) || fs_unlink(fh->path))
320 ret = EFI_WARN_DELETE_FAILURE;
323 return EFI_EXIT(ret);
327 * efi_get_file_size() - determine the size of a file
330 * @file_size: pointer to receive file size
331 * Return: status code
333 static efi_status_t efi_get_file_size(struct file_handle *fh,
337 return EFI_DEVICE_ERROR;
339 if (fs_size(fh->path, file_size))
340 return EFI_DEVICE_ERROR;
345 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
353 ret = EFI_INVALID_PARAMETER;
357 ret = efi_get_file_size(fh, &file_size);
358 if (ret != EFI_SUCCESS)
360 if (file_size < fh->offset) {
361 ret = EFI_DEVICE_ERROR;
366 return EFI_DEVICE_ERROR;
367 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
368 *buffer_size, &actread))
369 return EFI_DEVICE_ERROR;
371 *buffer_size = actread;
372 fh->offset += actread;
377 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
380 struct efi_file_info *info = buffer;
381 struct fs_dirent *dent;
386 return EFI_DEVICE_ERROR;
389 assert(fh->offset == 0);
390 fh->dirs = fs_opendir(fh->path);
392 return EFI_DEVICE_ERROR;
397 * So this is a bit awkward. Since fs layer is stateful and we
398 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
399 * we might have to return without consuming the dent.. so we
400 * have to stash it for next call.
405 dent = fs_readdir(fh->dirs);
409 /* no more files in directory */
414 /* check buffer size: */
415 required_size = sizeof(*info) +
416 2 * (utf8_utf16_strlen(dent->name) + 1);
417 if (*buffer_size < required_size) {
418 *buffer_size = required_size;
420 return EFI_BUFFER_TOO_SMALL;
423 return EFI_INVALID_PARAMETER;
426 *buffer_size = required_size;
427 memset(info, 0, required_size);
429 info->size = required_size;
430 info->file_size = dent->size;
431 info->physical_size = dent->size;
433 if (dent->type == FS_DT_DIR)
434 info->attribute |= EFI_FILE_DIRECTORY;
436 dst = info->file_name;
437 utf8_utf16_strcpy(&dst, dent->name);
444 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
445 efi_uintn_t *buffer_size, void *buffer)
447 struct file_handle *fh = to_fh(file);
448 efi_status_t ret = EFI_SUCCESS;
451 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
454 ret = EFI_INVALID_PARAMETER;
460 ret = dir_read(fh, &bs, buffer);
462 ret = file_read(fh, &bs, buffer);
466 *buffer_size = SIZE_MAX;
469 return EFI_EXIT(ret);
473 * efi_file_write() - write to file
475 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
477 * See the Unified Extensible Firmware Interface (UEFI) specification for
481 * @buffer_size: number of bytes to write
482 * @buffer: buffer with the bytes to write
483 * Return: status code
485 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
486 efi_uintn_t *buffer_size,
489 struct file_handle *fh = to_fh(file);
490 efi_status_t ret = EFI_SUCCESS;
493 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
495 if (!file || !buffer_size || !buffer) {
496 ret = EFI_INVALID_PARAMETER;
500 ret = EFI_UNSUPPORTED;
503 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
504 ret = EFI_ACCESS_DENIED;
511 if (set_blk_dev(fh)) {
512 ret = EFI_DEVICE_ERROR;
515 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
517 ret = EFI_DEVICE_ERROR;
520 *buffer_size = actwrite;
521 fh->offset += actwrite;
524 return EFI_EXIT(ret);
528 * efi_file_getpos() - get current position in file
530 * This function implements the GetPosition service of the EFI file protocol.
531 * See the UEFI spec for details.
534 * @pos: pointer to file position
535 * Return: status code
537 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
540 efi_status_t ret = EFI_SUCCESS;
541 struct file_handle *fh = to_fh(file);
543 EFI_ENTRY("%p, %p", file, pos);
546 ret = EFI_UNSUPPORTED;
552 return EFI_EXIT(ret);
556 * efi_file_setpos() - set current position in file
558 * This function implements the SetPosition service of the EFI file protocol.
559 * See the UEFI spec for details.
562 * @pos: new file position
563 * Return: status code
565 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
568 struct file_handle *fh = to_fh(file);
569 efi_status_t ret = EFI_SUCCESS;
571 EFI_ENTRY("%p, %llu", file, pos);
575 ret = EFI_UNSUPPORTED;
578 fs_closedir(fh->dirs);
585 ret = efi_get_file_size(fh, &file_size);
586 if (ret != EFI_SUCCESS)
594 return EFI_EXIT(ret);
597 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
598 const efi_guid_t *info_type,
599 efi_uintn_t *buffer_size,
602 struct file_handle *fh = to_fh(file);
603 efi_status_t ret = EFI_SUCCESS;
606 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
608 if (!file || !info_type || !buffer_size ||
609 (*buffer_size && !buffer)) {
610 ret = EFI_INVALID_PARAMETER;
614 if (!guidcmp(info_type, &efi_file_info_guid)) {
615 struct efi_file_info *info = buffer;
616 char *filename = basename(fh);
617 unsigned int required_size;
620 /* check buffer size: */
621 required_size = sizeof(*info) +
622 2 * (utf8_utf16_strlen(filename) + 1);
623 if (*buffer_size < required_size) {
624 *buffer_size = required_size;
625 ret = EFI_BUFFER_TOO_SMALL;
629 ret = efi_get_file_size(fh, &file_size);
630 if (ret != EFI_SUCCESS)
633 memset(info, 0, required_size);
635 info->size = required_size;
636 info->file_size = file_size;
637 info->physical_size = file_size;
640 info->attribute |= EFI_FILE_DIRECTORY;
642 dst = info->file_name;
643 utf8_utf16_strcpy(&dst, filename);
644 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
645 struct efi_file_system_info *info = buffer;
646 struct disk_partition part;
647 efi_uintn_t required_size;
650 if (fh->fs->part >= 1)
651 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
653 r = part_get_info_whole_disk(fh->fs->desc, &part);
655 ret = EFI_DEVICE_ERROR;
658 required_size = sizeof(*info) + 2;
659 if (*buffer_size < required_size) {
660 *buffer_size = required_size;
661 ret = EFI_BUFFER_TOO_SMALL;
665 memset(info, 0, required_size);
667 info->size = required_size;
669 * TODO: We cannot determine if the volume can be written to.
671 info->read_only = false;
672 info->volume_size = part.size * part.blksz;
674 * TODO: We currently have no function to determine the free
675 * space. The volume size is the best upper bound we have.
677 info->free_space = info->volume_size;
678 info->block_size = part.blksz;
680 * TODO: The volume label is not available in U-Boot.
682 info->volume_label[0] = 0;
683 } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
684 if (*buffer_size < 2) {
686 ret = EFI_BUFFER_TOO_SMALL;
691 ret = EFI_UNSUPPORTED;
695 return EFI_EXIT(ret);
698 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
699 const efi_guid_t *info_type,
700 efi_uintn_t buffer_size,
703 struct file_handle *fh = to_fh(file);
704 efi_status_t ret = EFI_UNSUPPORTED;
706 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
708 if (!guidcmp(info_type, &efi_file_info_guid)) {
709 struct efi_file_info *info = (struct efi_file_info *)buffer;
710 char *filename = basename(fh);
711 char *new_file_name, *pos;
714 /* The buffer will always contain a file name. */
715 if (buffer_size < sizeof(struct efi_file_info) + 2 ||
716 buffer_size < info->size) {
717 ret = EFI_BAD_BUFFER_SIZE;
720 /* We cannot change the directory attribute */
721 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
722 ret = EFI_ACCESS_DENIED;
725 /* Check for renaming */
726 new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1);
727 if (!new_file_name) {
728 ret = EFI_OUT_OF_RESOURCES;
732 utf16_utf8_strcpy(&pos, info->file_name);
733 if (strcmp(new_file_name, filename)) {
734 /* TODO: we do not support renaming */
735 EFI_PRINT("Renaming not supported\n");
737 ret = EFI_ACCESS_DENIED;
741 /* Check for truncation */
742 ret = efi_get_file_size(fh, &file_size);
743 if (ret != EFI_SUCCESS)
745 if (file_size != info->file_size) {
746 /* TODO: we do not support truncation */
747 EFI_PRINT("Truncation not supported\n");
748 ret = EFI_ACCESS_DENIED;
752 * We do not care for the other attributes
753 * TODO: Support read only
757 /* TODO: We do not support changing the volume label */
758 ret = EFI_UNSUPPORTED;
761 return EFI_EXIT(ret);
764 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
766 EFI_ENTRY("%p", file);
767 return EFI_EXIT(EFI_SUCCESS);
770 static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *file,
771 struct efi_file_handle **new_handle,
772 u16 *file_name, u64 open_mode, u64 attributes,
773 struct efi_file_io_token *token)
775 return EFI_UNSUPPORTED;
778 static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *file,
779 struct efi_file_io_token *token)
781 return EFI_UNSUPPORTED;
784 static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *file,
785 struct efi_file_io_token *token)
787 return EFI_UNSUPPORTED;
790 static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *file,
791 struct efi_file_io_token *token)
793 return EFI_UNSUPPORTED;
796 static const struct efi_file_handle efi_file_handle_protocol = {
797 .rev = EFI_FILE_PROTOCOL_REVISION2,
798 .open = efi_file_open,
799 .close = efi_file_close,
800 .delete = efi_file_delete,
801 .read = efi_file_read,
802 .write = efi_file_write,
803 .getpos = efi_file_getpos,
804 .setpos = efi_file_setpos,
805 .getinfo = efi_file_getinfo,
806 .setinfo = efi_file_setinfo,
807 .flush = efi_file_flush,
808 .open_ex = efi_file_open_ex,
809 .read_ex = efi_file_read_ex,
810 .write_ex = efi_file_write_ex,
811 .flush_ex = efi_file_flush_ex,
815 * efi_file_from_path() - open file via device path
818 * @return: EFI_FILE_PROTOCOL for the file or NULL
820 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
822 struct efi_simple_file_system_protocol *v;
823 struct efi_file_handle *f;
826 v = efi_fs_from_path(fp);
830 EFI_CALL(ret = v->open_volume(v, &f));
831 if (ret != EFI_SUCCESS)
834 /* Skip over device-path nodes before the file path. */
835 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
836 fp = efi_dp_next(fp);
839 * Step through the nodes of the directory path until the actual file
840 * node is reached which is the final node in the device path.
843 struct efi_device_path_file_path *fdp =
844 container_of(fp, struct efi_device_path_file_path, dp);
845 struct efi_file_handle *f2;
848 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
849 printf("bad file path!\n");
854 filename = u16_strdup(fdp->str);
857 EFI_CALL(ret = f->open(f, &f2, filename,
858 EFI_FILE_MODE_READ, 0));
860 if (ret != EFI_SUCCESS)
863 fp = efi_dp_next(fp);
865 EFI_CALL(f->close(f));
872 static efi_status_t EFIAPI
873 efi_open_volume(struct efi_simple_file_system_protocol *this,
874 struct efi_file_handle **root)
876 struct file_system *fs = to_fs(this);
878 EFI_ENTRY("%p, %p", this, root);
880 *root = file_open(fs, NULL, NULL, 0, 0);
882 return EFI_EXIT(EFI_SUCCESS);
885 struct efi_simple_file_system_protocol *
886 efi_simple_file_system(struct blk_desc *desc, int part,
887 struct efi_device_path *dp)
889 struct file_system *fs;
891 fs = calloc(1, sizeof(*fs));
892 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
893 fs->base.open_volume = efi_open_volume;