1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
10 #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);
198 fh->open_mode = open_mode;
199 fh->base = efi_file_handle_protocol;
207 strcpy(p, parent->path);
212 utf16_to_utf8((u8 *)p, file_name, flen);
214 if (sanitize_path(fh->path))
217 /* check if file exists: */
221 exists = fs_exists(fh->path);
222 /* fs_exists() calls fs_close(), so open file system again */
227 if (!(open_mode & EFI_FILE_MODE_CREATE) ||
228 efi_create_file(fh, attributes))
234 /* figure out if file is a directory: */
235 fh->isdir = is_dir(fh);
238 strcpy(fh->path, "");
248 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
249 struct efi_file_handle **new_handle,
250 u16 *file_name, u64 open_mode, u64 attributes)
252 struct file_handle *fh = to_fh(file);
255 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
256 file_name, open_mode, attributes);
258 /* Check parameters */
259 if (!file || !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);
294 return EFI_EXIT(ret);
297 static efi_status_t file_close(struct file_handle *fh)
299 fs_closedir(fh->dirs);
304 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
306 struct file_handle *fh = to_fh(file);
307 EFI_ENTRY("%p", file);
308 return EFI_EXIT(file_close(fh));
311 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
313 struct file_handle *fh = to_fh(file);
314 efi_status_t ret = EFI_SUCCESS;
316 EFI_ENTRY("%p", file);
318 if (set_blk_dev(fh) || fs_unlink(fh->path))
319 ret = EFI_WARN_DELETE_FAILURE;
322 return EFI_EXIT(ret);
326 * efi_get_file_size() - determine the size of a file
329 * @file_size: pointer to receive file size
330 * Return: status code
332 static efi_status_t efi_get_file_size(struct file_handle *fh,
336 return EFI_DEVICE_ERROR;
338 if (fs_size(fh->path, file_size))
339 return EFI_DEVICE_ERROR;
344 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
351 ret = efi_get_file_size(fh, &file_size);
352 if (ret != EFI_SUCCESS)
354 if (file_size < fh->offset) {
355 ret = EFI_DEVICE_ERROR;
360 return EFI_DEVICE_ERROR;
361 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
362 *buffer_size, &actread))
363 return EFI_DEVICE_ERROR;
365 *buffer_size = actread;
366 fh->offset += actread;
371 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
374 struct efi_file_info *info = buffer;
375 struct fs_dirent *dent;
380 return EFI_DEVICE_ERROR;
383 assert(fh->offset == 0);
384 fh->dirs = fs_opendir(fh->path);
386 return EFI_DEVICE_ERROR;
391 * So this is a bit awkward. Since fs layer is stateful and we
392 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
393 * we might have to return without consuming the dent.. so we
394 * have to stash it for next call.
399 dent = fs_readdir(fh->dirs);
403 /* no more files in directory */
408 /* check buffer size: */
409 required_size = sizeof(*info) +
410 2 * (utf8_utf16_strlen(dent->name) + 1);
411 if (*buffer_size < required_size) {
412 *buffer_size = required_size;
414 return EFI_BUFFER_TOO_SMALL;
418 *buffer_size = required_size;
419 memset(info, 0, required_size);
421 info->size = required_size;
422 info->file_size = dent->size;
423 info->physical_size = dent->size;
425 if (dent->type == FS_DT_DIR)
426 info->attribute |= EFI_FILE_DIRECTORY;
428 dst = info->file_name;
429 utf8_utf16_strcpy(&dst, dent->name);
436 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
437 efi_uintn_t *buffer_size, void *buffer)
439 struct file_handle *fh = to_fh(file);
440 efi_status_t ret = EFI_SUCCESS;
443 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
445 if (!buffer_size || !buffer) {
446 ret = EFI_INVALID_PARAMETER;
452 ret = dir_read(fh, &bs, buffer);
454 ret = file_read(fh, &bs, buffer);
458 *buffer_size = SIZE_MAX;
461 return EFI_EXIT(ret);
465 * efi_file_write() - write to file
467 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
469 * See the Unified Extensible Firmware Interface (UEFI) specification for
473 * @buffer_size: number of bytes to write
474 * @buffer: buffer with the bytes to write
475 * Return: status code
477 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
478 efi_uintn_t *buffer_size,
481 struct file_handle *fh = to_fh(file);
482 efi_status_t ret = EFI_SUCCESS;
485 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
487 if (!file || !buffer_size || !buffer) {
488 ret = EFI_INVALID_PARAMETER;
492 ret = EFI_UNSUPPORTED;
495 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
496 ret = EFI_ACCESS_DENIED;
503 if (set_blk_dev(fh)) {
504 ret = EFI_DEVICE_ERROR;
507 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
509 ret = EFI_DEVICE_ERROR;
512 *buffer_size = actwrite;
513 fh->offset += actwrite;
516 return EFI_EXIT(ret);
520 * efi_file_getpos() - get current position in file
522 * This function implements the GetPosition service of the EFI file protocol.
523 * See the UEFI spec for details.
526 * @pos: pointer to file position
527 * Return: status code
529 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
532 efi_status_t ret = EFI_SUCCESS;
533 struct file_handle *fh = to_fh(file);
535 EFI_ENTRY("%p, %p", file, pos);
538 ret = EFI_UNSUPPORTED;
544 return EFI_EXIT(ret);
548 * efi_file_setpos() - set current position in file
550 * This function implements the SetPosition service of the EFI file protocol.
551 * See the UEFI spec for details.
554 * @pos: new file position
555 * Return: status code
557 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
560 struct file_handle *fh = to_fh(file);
561 efi_status_t ret = EFI_SUCCESS;
563 EFI_ENTRY("%p, %llu", file, pos);
567 ret = EFI_UNSUPPORTED;
570 fs_closedir(fh->dirs);
577 ret = efi_get_file_size(fh, &file_size);
578 if (ret != EFI_SUCCESS)
586 return EFI_EXIT(ret);
589 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
590 const efi_guid_t *info_type,
591 efi_uintn_t *buffer_size,
594 struct file_handle *fh = to_fh(file);
595 efi_status_t ret = EFI_SUCCESS;
598 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
600 if (!file || !info_type || !buffer_size ||
601 (*buffer_size && !buffer)) {
602 ret = EFI_INVALID_PARAMETER;
606 if (!guidcmp(info_type, &efi_file_info_guid)) {
607 struct efi_file_info *info = buffer;
608 char *filename = basename(fh);
609 unsigned int required_size;
612 /* check buffer size: */
613 required_size = sizeof(*info) +
614 2 * (utf8_utf16_strlen(filename) + 1);
615 if (*buffer_size < required_size) {
616 *buffer_size = required_size;
617 ret = EFI_BUFFER_TOO_SMALL;
621 ret = efi_get_file_size(fh, &file_size);
622 if (ret != EFI_SUCCESS)
625 memset(info, 0, required_size);
627 info->size = required_size;
628 info->file_size = file_size;
629 info->physical_size = file_size;
632 info->attribute |= EFI_FILE_DIRECTORY;
634 dst = info->file_name;
635 utf8_utf16_strcpy(&dst, filename);
636 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
637 struct efi_file_system_info *info = buffer;
638 struct disk_partition part;
639 efi_uintn_t required_size;
642 if (fh->fs->part >= 1)
643 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
645 r = part_get_info_whole_disk(fh->fs->desc, &part);
647 ret = EFI_DEVICE_ERROR;
650 required_size = sizeof(*info) + 2;
651 if (*buffer_size < required_size) {
652 *buffer_size = required_size;
653 ret = EFI_BUFFER_TOO_SMALL;
657 memset(info, 0, required_size);
659 info->size = required_size;
661 * TODO: We cannot determine if the volume can be written to.
663 info->read_only = false;
664 info->volume_size = part.size * part.blksz;
666 * TODO: We currently have no function to determine the free
667 * space. The volume size is the best upper bound we have.
669 info->free_space = info->volume_size;
670 info->block_size = part.blksz;
672 * TODO: The volume label is not available in U-Boot.
674 info->volume_label[0] = 0;
675 } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
676 if (*buffer_size < 2) {
678 ret = EFI_BUFFER_TOO_SMALL;
683 ret = EFI_UNSUPPORTED;
687 return EFI_EXIT(ret);
690 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
691 const efi_guid_t *info_type,
692 efi_uintn_t buffer_size,
695 struct file_handle *fh = to_fh(file);
696 efi_status_t ret = EFI_UNSUPPORTED;
698 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
700 if (!guidcmp(info_type, &efi_file_info_guid)) {
701 struct efi_file_info *info = (struct efi_file_info *)buffer;
702 char *filename = basename(fh);
703 char *new_file_name, *pos;
706 /* The buffer will always contain a file name. */
707 if (buffer_size < sizeof(struct efi_file_info) + 2 ||
708 buffer_size < info->size) {
709 ret = EFI_BAD_BUFFER_SIZE;
712 /* We cannot change the directory attribute */
713 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
714 ret = EFI_ACCESS_DENIED;
717 /* Check for renaming */
718 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
719 if (!new_file_name) {
720 ret = EFI_OUT_OF_RESOURCES;
724 utf16_utf8_strcpy(&pos, info->file_name);
725 if (strcmp(new_file_name, filename)) {
726 /* TODO: we do not support renaming */
727 EFI_PRINT("Renaming not supported\n");
729 ret = EFI_ACCESS_DENIED;
733 /* Check for truncation */
734 ret = efi_get_file_size(fh, &file_size);
735 if (ret != EFI_SUCCESS)
737 if (file_size != info->file_size) {
738 /* TODO: we do not support truncation */
739 EFI_PRINT("Truncation not supported\n");
740 ret = EFI_ACCESS_DENIED;
744 * We do not care for the other attributes
745 * TODO: Support read only
749 /* TODO: We do not support changing the volume label */
750 ret = EFI_UNSUPPORTED;
753 return EFI_EXIT(ret);
756 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
758 EFI_ENTRY("%p", file);
759 return EFI_EXIT(EFI_SUCCESS);
762 static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *file,
763 struct efi_file_handle **new_handle,
764 u16 *file_name, u64 open_mode, u64 attributes,
765 struct efi_file_io_token *token)
767 return EFI_UNSUPPORTED;
770 static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *file,
771 struct efi_file_io_token *token)
773 return EFI_UNSUPPORTED;
776 static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *file,
777 struct efi_file_io_token *token)
779 return EFI_UNSUPPORTED;
782 static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *file,
783 struct efi_file_io_token *token)
785 return EFI_UNSUPPORTED;
788 static const struct efi_file_handle efi_file_handle_protocol = {
789 .rev = EFI_FILE_PROTOCOL_REVISION2,
790 .open = efi_file_open,
791 .close = efi_file_close,
792 .delete = efi_file_delete,
793 .read = efi_file_read,
794 .write = efi_file_write,
795 .getpos = efi_file_getpos,
796 .setpos = efi_file_setpos,
797 .getinfo = efi_file_getinfo,
798 .setinfo = efi_file_setinfo,
799 .flush = efi_file_flush,
800 .open_ex = efi_file_open_ex,
801 .read_ex = efi_file_read_ex,
802 .write_ex = efi_file_write_ex,
803 .flush_ex = efi_file_flush_ex,
807 * efi_file_from_path() - open file via device path
810 * @return: EFI_FILE_PROTOCOL for the file or NULL
812 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
814 struct efi_simple_file_system_protocol *v;
815 struct efi_file_handle *f;
818 v = efi_fs_from_path(fp);
822 EFI_CALL(ret = v->open_volume(v, &f));
823 if (ret != EFI_SUCCESS)
826 /* Skip over device-path nodes before the file path. */
827 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
828 fp = efi_dp_next(fp);
831 * Step through the nodes of the directory path until the actual file
832 * node is reached which is the final node in the device path.
835 struct efi_device_path_file_path *fdp =
836 container_of(fp, struct efi_device_path_file_path, dp);
837 struct efi_file_handle *f2;
840 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
841 printf("bad file path!\n");
846 filename = u16_strdup(fdp->str);
849 EFI_CALL(ret = f->open(f, &f2, filename,
850 EFI_FILE_MODE_READ, 0));
852 if (ret != EFI_SUCCESS)
855 fp = efi_dp_next(fp);
857 EFI_CALL(f->close(f));
864 static efi_status_t EFIAPI
865 efi_open_volume(struct efi_simple_file_system_protocol *this,
866 struct efi_file_handle **root)
868 struct file_system *fs = to_fs(this);
870 EFI_ENTRY("%p, %p", this, root);
872 *root = file_open(fs, NULL, NULL, 0, 0);
874 return EFI_EXIT(EFI_SUCCESS);
877 struct efi_simple_file_system_protocol *
878 efi_simple_file_system(struct blk_desc *desc, int part,
879 struct efi_device_path *dp)
881 struct file_system *fs;
883 fs = calloc(1, sizeof(*fs));
884 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
885 fs->base.open_volume = efi_open_volume;