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,
352 ret = efi_get_file_size(fh, &file_size);
353 if (ret != EFI_SUCCESS)
355 if (file_size < fh->offset) {
356 ret = EFI_DEVICE_ERROR;
361 return EFI_DEVICE_ERROR;
362 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
363 *buffer_size, &actread))
364 return EFI_DEVICE_ERROR;
366 *buffer_size = actread;
367 fh->offset += actread;
372 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
375 struct efi_file_info *info = buffer;
376 struct fs_dirent *dent;
381 return EFI_DEVICE_ERROR;
384 assert(fh->offset == 0);
385 fh->dirs = fs_opendir(fh->path);
387 return EFI_DEVICE_ERROR;
392 * So this is a bit awkward. Since fs layer is stateful and we
393 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
394 * we might have to return without consuming the dent.. so we
395 * have to stash it for next call.
400 dent = fs_readdir(fh->dirs);
404 /* no more files in directory */
409 /* check buffer size: */
410 required_size = sizeof(*info) +
411 2 * (utf8_utf16_strlen(dent->name) + 1);
412 if (*buffer_size < required_size) {
413 *buffer_size = required_size;
415 return EFI_BUFFER_TOO_SMALL;
419 *buffer_size = required_size;
420 memset(info, 0, required_size);
422 info->size = required_size;
423 info->file_size = dent->size;
424 info->physical_size = dent->size;
426 if (dent->type == FS_DT_DIR)
427 info->attribute |= EFI_FILE_DIRECTORY;
429 dst = info->file_name;
430 utf8_utf16_strcpy(&dst, dent->name);
437 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
438 efi_uintn_t *buffer_size, void *buffer)
440 struct file_handle *fh = to_fh(file);
441 efi_status_t ret = EFI_SUCCESS;
444 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
446 if (!buffer_size || !buffer) {
447 ret = EFI_INVALID_PARAMETER;
453 ret = dir_read(fh, &bs, buffer);
455 ret = file_read(fh, &bs, buffer);
459 *buffer_size = SIZE_MAX;
462 return EFI_EXIT(ret);
466 * efi_file_write() - write to file
468 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
470 * See the Unified Extensible Firmware Interface (UEFI) specification for
474 * @buffer_size: number of bytes to write
475 * @buffer: buffer with the bytes to write
476 * Return: status code
478 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
479 efi_uintn_t *buffer_size,
482 struct file_handle *fh = to_fh(file);
483 efi_status_t ret = EFI_SUCCESS;
486 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
488 if (!file || !buffer_size || !buffer) {
489 ret = EFI_INVALID_PARAMETER;
493 ret = EFI_UNSUPPORTED;
496 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
497 ret = EFI_ACCESS_DENIED;
504 if (set_blk_dev(fh)) {
505 ret = EFI_DEVICE_ERROR;
508 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
510 ret = EFI_DEVICE_ERROR;
513 *buffer_size = actwrite;
514 fh->offset += actwrite;
517 return EFI_EXIT(ret);
521 * efi_file_getpos() - get current position in file
523 * This function implements the GetPosition service of the EFI file protocol.
524 * See the UEFI spec for details.
527 * @pos: pointer to file position
528 * Return: status code
530 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
533 efi_status_t ret = EFI_SUCCESS;
534 struct file_handle *fh = to_fh(file);
536 EFI_ENTRY("%p, %p", file, pos);
539 ret = EFI_UNSUPPORTED;
545 return EFI_EXIT(ret);
549 * efi_file_setpos() - set current position in file
551 * This function implements the SetPosition service of the EFI file protocol.
552 * See the UEFI spec for details.
555 * @pos: new file position
556 * Return: status code
558 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
561 struct file_handle *fh = to_fh(file);
562 efi_status_t ret = EFI_SUCCESS;
564 EFI_ENTRY("%p, %llu", file, pos);
568 ret = EFI_UNSUPPORTED;
571 fs_closedir(fh->dirs);
578 ret = efi_get_file_size(fh, &file_size);
579 if (ret != EFI_SUCCESS)
587 return EFI_EXIT(ret);
590 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
591 const efi_guid_t *info_type,
592 efi_uintn_t *buffer_size,
595 struct file_handle *fh = to_fh(file);
596 efi_status_t ret = EFI_SUCCESS;
599 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
601 if (!file || !info_type || !buffer_size ||
602 (*buffer_size && !buffer)) {
603 ret = EFI_INVALID_PARAMETER;
607 if (!guidcmp(info_type, &efi_file_info_guid)) {
608 struct efi_file_info *info = buffer;
609 char *filename = basename(fh);
610 unsigned int required_size;
613 /* check buffer size: */
614 required_size = sizeof(*info) +
615 2 * (utf8_utf16_strlen(filename) + 1);
616 if (*buffer_size < required_size) {
617 *buffer_size = required_size;
618 ret = EFI_BUFFER_TOO_SMALL;
622 ret = efi_get_file_size(fh, &file_size);
623 if (ret != EFI_SUCCESS)
626 memset(info, 0, required_size);
628 info->size = required_size;
629 info->file_size = file_size;
630 info->physical_size = file_size;
633 info->attribute |= EFI_FILE_DIRECTORY;
635 dst = info->file_name;
636 utf8_utf16_strcpy(&dst, filename);
637 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
638 struct efi_file_system_info *info = buffer;
639 struct disk_partition part;
640 efi_uintn_t required_size;
643 if (fh->fs->part >= 1)
644 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
646 r = part_get_info_whole_disk(fh->fs->desc, &part);
648 ret = EFI_DEVICE_ERROR;
651 required_size = sizeof(*info) + 2;
652 if (*buffer_size < required_size) {
653 *buffer_size = required_size;
654 ret = EFI_BUFFER_TOO_SMALL;
658 memset(info, 0, required_size);
660 info->size = required_size;
662 * TODO: We cannot determine if the volume can be written to.
664 info->read_only = false;
665 info->volume_size = part.size * part.blksz;
667 * TODO: We currently have no function to determine the free
668 * space. The volume size is the best upper bound we have.
670 info->free_space = info->volume_size;
671 info->block_size = part.blksz;
673 * TODO: The volume label is not available in U-Boot.
675 info->volume_label[0] = 0;
676 } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
677 if (*buffer_size < 2) {
679 ret = EFI_BUFFER_TOO_SMALL;
684 ret = EFI_UNSUPPORTED;
688 return EFI_EXIT(ret);
691 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
692 const efi_guid_t *info_type,
693 efi_uintn_t buffer_size,
696 struct file_handle *fh = to_fh(file);
697 efi_status_t ret = EFI_UNSUPPORTED;
699 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
701 if (!guidcmp(info_type, &efi_file_info_guid)) {
702 struct efi_file_info *info = (struct efi_file_info *)buffer;
703 char *filename = basename(fh);
704 char *new_file_name, *pos;
707 /* The buffer will always contain a file name. */
708 if (buffer_size < sizeof(struct efi_file_info) + 2 ||
709 buffer_size < info->size) {
710 ret = EFI_BAD_BUFFER_SIZE;
713 /* We cannot change the directory attribute */
714 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
715 ret = EFI_ACCESS_DENIED;
718 /* Check for renaming */
719 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
720 if (!new_file_name) {
721 ret = EFI_OUT_OF_RESOURCES;
725 utf16_utf8_strcpy(&pos, info->file_name);
726 if (strcmp(new_file_name, filename)) {
727 /* TODO: we do not support renaming */
728 EFI_PRINT("Renaming not supported\n");
730 ret = EFI_ACCESS_DENIED;
734 /* Check for truncation */
735 ret = efi_get_file_size(fh, &file_size);
736 if (ret != EFI_SUCCESS)
738 if (file_size != info->file_size) {
739 /* TODO: we do not support truncation */
740 EFI_PRINT("Truncation not supported\n");
741 ret = EFI_ACCESS_DENIED;
745 * We do not care for the other attributes
746 * TODO: Support read only
750 /* TODO: We do not support changing the volume label */
751 ret = EFI_UNSUPPORTED;
754 return EFI_EXIT(ret);
757 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
759 EFI_ENTRY("%p", file);
760 return EFI_EXIT(EFI_SUCCESS);
763 static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *file,
764 struct efi_file_handle **new_handle,
765 u16 *file_name, u64 open_mode, u64 attributes,
766 struct efi_file_io_token *token)
768 return EFI_UNSUPPORTED;
771 static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *file,
772 struct efi_file_io_token *token)
774 return EFI_UNSUPPORTED;
777 static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *file,
778 struct efi_file_io_token *token)
780 return EFI_UNSUPPORTED;
783 static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *file,
784 struct efi_file_io_token *token)
786 return EFI_UNSUPPORTED;
789 static const struct efi_file_handle efi_file_handle_protocol = {
790 .rev = EFI_FILE_PROTOCOL_REVISION2,
791 .open = efi_file_open,
792 .close = efi_file_close,
793 .delete = efi_file_delete,
794 .read = efi_file_read,
795 .write = efi_file_write,
796 .getpos = efi_file_getpos,
797 .setpos = efi_file_setpos,
798 .getinfo = efi_file_getinfo,
799 .setinfo = efi_file_setinfo,
800 .flush = efi_file_flush,
801 .open_ex = efi_file_open_ex,
802 .read_ex = efi_file_read_ex,
803 .write_ex = efi_file_write_ex,
804 .flush_ex = efi_file_flush_ex,
808 * efi_file_from_path() - open file via device path
811 * @return: EFI_FILE_PROTOCOL for the file or NULL
813 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
815 struct efi_simple_file_system_protocol *v;
816 struct efi_file_handle *f;
819 v = efi_fs_from_path(fp);
823 EFI_CALL(ret = v->open_volume(v, &f));
824 if (ret != EFI_SUCCESS)
827 /* Skip over device-path nodes before the file path. */
828 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
829 fp = efi_dp_next(fp);
832 * Step through the nodes of the directory path until the actual file
833 * node is reached which is the final node in the device path.
836 struct efi_device_path_file_path *fdp =
837 container_of(fp, struct efi_device_path_file_path, dp);
838 struct efi_file_handle *f2;
841 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
842 printf("bad file path!\n");
847 filename = u16_strdup(fdp->str);
850 EFI_CALL(ret = f->open(f, &f2, filename,
851 EFI_FILE_MODE_READ, 0));
853 if (ret != EFI_SUCCESS)
856 fp = efi_dp_next(fp);
858 EFI_CALL(f->close(f));
865 static efi_status_t EFIAPI
866 efi_open_volume(struct efi_simple_file_system_protocol *this,
867 struct efi_file_handle **root)
869 struct file_system *fs = to_fs(this);
871 EFI_ENTRY("%p, %p", this, root);
873 *root = file_open(fs, NULL, NULL, 0, 0);
875 return EFI_EXIT(EFI_SUCCESS);
878 struct efi_simple_file_system_protocol *
879 efi_simple_file_system(struct blk_desc *desc, int part,
880 struct efi_device_path *dp)
882 struct file_system *fs;
884 fs = calloc(1, sizeof(*fs));
885 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
886 fs->base.open_volume = efi_open_volume;