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;
19 struct efi_simple_file_system_protocol base;
20 struct efi_device_path *dp;
21 struct blk_desc *desc;
24 #define to_fs(x) container_of(x, struct file_system, base)
27 struct efi_file_handle base;
28 struct file_system *fs;
29 loff_t offset; /* current file position/cursor */
33 /* for reading a directory: */
34 struct fs_dir_stream *dirs;
35 struct fs_dirent *dent;
39 #define to_fh(x) container_of(x, struct file_handle, base)
41 static const struct efi_file_handle efi_file_handle_protocol;
43 static char *basename(struct file_handle *fh)
45 char *s = strrchr(fh->path, '/');
51 static int set_blk_dev(struct file_handle *fh)
53 return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
57 * is_dir() - check if file handle points to directory
59 * We assume that set_blk_dev(fh) has been called already.
62 * Return: true if file handle points to a directory
64 static int is_dir(struct file_handle *fh)
66 struct fs_dir_stream *dirs;
68 dirs = fs_opendir(fh->path);
78 * Normalize a path which may include either back or fwd slashes,
79 * double slashes, . or .. entries in the path, etc.
81 static int sanitize_path(char *path)
85 /* backslash to slash: */
87 while ((p = strchr(p, '\\')))
90 /* handle double-slashes: */
92 while ((p = strstr(p, "//"))) {
94 memmove(p, src, strlen(src) + 1);
97 /* handle extra /.'s */
99 while ((p = strstr(p, "/."))) {
101 * You'd be tempted to do this *after* handling ".."s
102 * below to avoid having to check if "/." is start of
103 * a "/..", but that won't have the correct results..
104 * for example, "/foo/./../bar" would get resolved to
105 * "/foo/bar" if you did these two passes in the other
113 memmove(p, src, strlen(src) + 1);
116 /* handle extra /..'s: */
118 while ((p = strstr(p, "/.."))) {
123 /* find beginning of previous path entry: */
132 memmove(p, src, strlen(src) + 1);
139 * efi_create_file() - create file or directory
142 * @attributes: attributes for newly created file
143 * Returns: 0 for success
145 static int efi_create_file(struct file_handle *fh, u64 attributes)
148 void *buffer = &actwrite;
150 if (attributes & EFI_FILE_DIRECTORY)
151 return fs_mkdir(fh->path);
153 return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
158 * file_open() - open a file handle
161 * @parent: directory relative to which the file is to be opened
162 * @file_name: path of the file to be opened. '\', '.', or '..' may
163 * be used as modifiers. A leading backslash indicates an
165 * @open_mode: bit mask indicating the access mode (read, write,
167 * @attributes: attributes for newly created file
168 * Returns: handle to the opened file or NULL
170 static struct efi_file_handle *file_open(struct file_system *fs,
171 struct file_handle *parent, u16 *file_name, u64 open_mode,
174 struct file_handle *fh;
175 char f0[MAX_UTF8_PER_UTF16] = {0};
180 utf16_to_utf8((u8 *)f0, file_name, 1);
181 flen = u16_strlen(file_name);
184 /* we could have a parent, but also an absolute path: */
188 plen = strlen(parent->path) + 1;
191 /* +2 is for null and '/' */
192 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
194 fh->open_mode = open_mode;
195 fh->base = efi_file_handle_protocol;
203 strcpy(p, parent->path);
208 utf16_to_utf8((u8 *)p, file_name, flen);
210 if (sanitize_path(fh->path))
213 /* check if file exists: */
217 exists = fs_exists(fh->path);
218 /* fs_exists() calls fs_close(), so open file system again */
223 if (!(open_mode & EFI_FILE_MODE_CREATE) ||
224 efi_create_file(fh, attributes))
230 /* figure out if file is a directory: */
231 fh->isdir = is_dir(fh);
234 strcpy(fh->path, "");
244 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
245 struct efi_file_handle **new_handle,
246 u16 *file_name, u64 open_mode, u64 attributes)
248 struct file_handle *fh = to_fh(file);
251 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
252 file_name, open_mode, attributes);
254 /* Check parameters */
255 if (!file || !new_handle || !file_name) {
256 ret = EFI_INVALID_PARAMETER;
259 if (open_mode != EFI_FILE_MODE_READ &&
260 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
261 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
262 EFI_FILE_MODE_CREATE)) {
263 ret = EFI_INVALID_PARAMETER;
267 * The UEFI spec requires that attributes are only set in create mode.
268 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
269 * read mode. EDK2 does not check that attributes are zero if not in
272 * So here we only check attributes in create mode and do not check
273 * that they are zero otherwise.
275 if ((open_mode & EFI_FILE_MODE_CREATE) &&
276 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
277 ret = EFI_INVALID_PARAMETER;
282 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
284 EFI_PRINT("file handle %p\n", *new_handle);
290 return EFI_EXIT(ret);
293 static efi_status_t file_close(struct file_handle *fh)
295 fs_closedir(fh->dirs);
300 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
302 struct file_handle *fh = to_fh(file);
303 EFI_ENTRY("%p", file);
304 return EFI_EXIT(file_close(fh));
307 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
309 struct file_handle *fh = to_fh(file);
310 efi_status_t ret = EFI_SUCCESS;
312 EFI_ENTRY("%p", file);
314 if (set_blk_dev(fh) || fs_unlink(fh->path))
315 ret = EFI_WARN_DELETE_FAILURE;
318 return EFI_EXIT(ret);
321 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
326 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
327 *buffer_size, &actread))
328 return EFI_DEVICE_ERROR;
330 *buffer_size = actread;
331 fh->offset += actread;
336 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
339 struct efi_file_info *info = buffer;
340 struct fs_dirent *dent;
341 unsigned int required_size;
344 assert(fh->offset == 0);
345 fh->dirs = fs_opendir(fh->path);
347 return EFI_DEVICE_ERROR;
351 * So this is a bit awkward. Since fs layer is stateful and we
352 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
353 * we might have to return without consuming the dent.. so we
354 * have to stash it for next call.
360 dent = fs_readdir(fh->dirs);
365 /* no more files in directory: */
366 /* workaround shim.efi bug/quirk.. as find_boot_csv()
367 * loops through directory contents, it initially calls
368 * read w/ zero length buffer to find out how much mem
369 * to allocate for the EFI_FILE_INFO, then allocates,
370 * and then calls a 2nd time. If we return size of
371 * zero the first time, it happily passes that to
372 * AllocateZeroPool(), and when that returns NULL it
373 * thinks it is EFI_OUT_OF_RESOURCES. So on first
374 * call return a non-zero size:
376 if (*buffer_size == 0)
377 *buffer_size = sizeof(*info);
383 /* check buffer size: */
384 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
385 if (*buffer_size < required_size) {
386 *buffer_size = required_size;
388 return EFI_BUFFER_TOO_SMALL;
391 *buffer_size = required_size;
392 memset(info, 0, required_size);
394 info->size = required_size;
395 info->file_size = dent->size;
396 info->physical_size = dent->size;
398 if (dent->type == FS_DT_DIR)
399 info->attribute |= EFI_FILE_DIRECTORY;
401 ascii2unicode(info->file_name, dent->name);
408 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
409 efi_uintn_t *buffer_size, void *buffer)
411 struct file_handle *fh = to_fh(file);
412 efi_status_t ret = EFI_SUCCESS;
415 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
417 if (!buffer_size || !buffer) {
418 ret = EFI_INVALID_PARAMETER;
422 if (set_blk_dev(fh)) {
423 ret = EFI_DEVICE_ERROR;
429 ret = dir_read(fh, &bs, buffer);
431 ret = file_read(fh, &bs, buffer);
435 *buffer_size = SIZE_MAX;
438 return EFI_EXIT(ret);
442 * efi_file_write() - write to file
444 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
446 * See the Unified Extensible Firmware Interface (UEFI) specification for
450 * @buffer_size: number of bytes to write
451 * @buffer: buffer with the bytes to write
452 * Return: status code
454 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
455 efi_uintn_t *buffer_size,
458 struct file_handle *fh = to_fh(file);
459 efi_status_t ret = EFI_SUCCESS;
462 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
464 if (!file || !buffer_size || !buffer) {
465 ret = EFI_INVALID_PARAMETER;
469 ret = EFI_UNSUPPORTED;
472 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
473 ret = EFI_ACCESS_DENIED;
480 if (set_blk_dev(fh)) {
481 ret = EFI_DEVICE_ERROR;
484 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
486 ret = EFI_DEVICE_ERROR;
489 *buffer_size = actwrite;
490 fh->offset += actwrite;
493 return EFI_EXIT(ret);
497 * efi_file_getpos() - get current position in file
499 * This function implements the GetPosition service of the EFI file protocol.
500 * See the UEFI spec for details.
503 * @pos: pointer to file position
504 * Return: status code
506 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
509 efi_status_t ret = EFI_SUCCESS;
510 struct file_handle *fh = to_fh(file);
512 EFI_ENTRY("%p, %p", file, pos);
515 ret = EFI_UNSUPPORTED;
521 return EFI_EXIT(ret);
525 * efi_file_setpos() - set current position in file
527 * This function implements the SetPosition service of the EFI file protocol.
528 * See the UEFI spec for details.
531 * @pos: new file position
532 * Return: status code
534 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
537 struct file_handle *fh = to_fh(file);
538 efi_status_t ret = EFI_SUCCESS;
540 EFI_ENTRY("%p, %llu", file, pos);
544 ret = EFI_UNSUPPORTED;
547 fs_closedir(fh->dirs);
554 if (set_blk_dev(fh)) {
555 ret = EFI_DEVICE_ERROR;
559 if (fs_size(fh->path, &file_size)) {
560 ret = EFI_DEVICE_ERROR;
570 return EFI_EXIT(ret);
573 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
574 const efi_guid_t *info_type,
575 efi_uintn_t *buffer_size,
578 struct file_handle *fh = to_fh(file);
579 efi_status_t ret = EFI_SUCCESS;
581 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
583 if (!guidcmp(info_type, &efi_file_info_guid)) {
584 struct efi_file_info *info = buffer;
585 char *filename = basename(fh);
586 unsigned int required_size;
589 /* check buffer size: */
590 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
591 if (*buffer_size < required_size) {
592 *buffer_size = required_size;
593 ret = EFI_BUFFER_TOO_SMALL;
597 if (set_blk_dev(fh)) {
598 ret = EFI_DEVICE_ERROR;
602 if (fs_size(fh->path, &file_size)) {
603 ret = EFI_DEVICE_ERROR;
607 memset(info, 0, required_size);
609 info->size = required_size;
610 info->file_size = file_size;
611 info->physical_size = file_size;
614 info->attribute |= EFI_FILE_DIRECTORY;
616 ascii2unicode(info->file_name, filename);
617 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
618 struct efi_file_system_info *info = buffer;
619 disk_partition_t part;
620 efi_uintn_t required_size;
623 if (fh->fs->part >= 1)
624 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
626 r = part_get_info_whole_disk(fh->fs->desc, &part);
628 ret = EFI_DEVICE_ERROR;
631 required_size = sizeof(info) + 2 *
632 (strlen((const char *)part.name) + 1);
633 if (*buffer_size < required_size) {
634 *buffer_size = required_size;
635 ret = EFI_BUFFER_TOO_SMALL;
639 memset(info, 0, required_size);
641 info->size = required_size;
642 info->read_only = true;
643 info->volume_size = part.size * part.blksz;
644 info->free_space = 0;
645 info->block_size = part.blksz;
647 * TODO: The volume label is not available in U-Boot.
648 * Use the partition name as substitute.
650 ascii2unicode((u16 *)info->volume_label,
651 (const char *)part.name);
653 ret = EFI_UNSUPPORTED;
657 return EFI_EXIT(ret);
660 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
661 const efi_guid_t *info_type,
662 efi_uintn_t buffer_size,
665 struct file_handle *fh = to_fh(file);
666 efi_status_t ret = EFI_UNSUPPORTED;
668 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
670 if (!guidcmp(info_type, &efi_file_info_guid)) {
671 struct efi_file_info *info = (struct efi_file_info *)buffer;
672 char *filename = basename(fh);
673 char *new_file_name, *pos;
676 if (buffer_size < sizeof(struct efi_file_info)) {
677 ret = EFI_BAD_BUFFER_SIZE;
680 /* We cannot change the directory attribute */
681 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
682 ret = EFI_ACCESS_DENIED;
685 /* Check for renaming */
686 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
687 if (!new_file_name) {
688 ret = EFI_OUT_OF_RESOURCES;
692 utf16_utf8_strcpy(&pos, info->file_name);
693 if (strcmp(new_file_name, filename)) {
694 /* TODO: we do not support renaming */
695 EFI_PRINT("Renaming not supported\n");
697 ret = EFI_ACCESS_DENIED;
701 /* Check for truncation */
702 if (set_blk_dev(fh)) {
703 ret = EFI_DEVICE_ERROR;
706 if (fs_size(fh->path, &file_size)) {
707 ret = EFI_DEVICE_ERROR;
710 if (file_size != info->file_size) {
711 /* TODO: we do not support truncation */
712 EFI_PRINT("Truncation not supported\n");
713 ret = EFI_ACCESS_DENIED;
717 * We do not care for the other attributes
718 * TODO: Support read only
721 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
722 if (buffer_size < sizeof(struct efi_file_system_info)) {
723 ret = EFI_BAD_BUFFER_SIZE;
727 ret = EFI_UNSUPPORTED;
730 return EFI_EXIT(ret);
733 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
735 EFI_ENTRY("%p", file);
736 return EFI_EXIT(EFI_SUCCESS);
739 static const struct efi_file_handle efi_file_handle_protocol = {
741 * TODO: We currently only support EFI file protocol revision 0x00010000
742 * while UEFI specs 2.4 - 2.7 prescribe revision 0x00020000.
744 .rev = EFI_FILE_PROTOCOL_REVISION,
745 .open = efi_file_open,
746 .close = efi_file_close,
747 .delete = efi_file_delete,
748 .read = efi_file_read,
749 .write = efi_file_write,
750 .getpos = efi_file_getpos,
751 .setpos = efi_file_setpos,
752 .getinfo = efi_file_getinfo,
753 .setinfo = efi_file_setinfo,
754 .flush = efi_file_flush,
758 * efi_file_from_path() - open file via device path
761 * @return: EFI_FILE_PROTOCOL for the file or NULL
763 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
765 struct efi_simple_file_system_protocol *v;
766 struct efi_file_handle *f;
769 v = efi_fs_from_path(fp);
773 EFI_CALL(ret = v->open_volume(v, &f));
774 if (ret != EFI_SUCCESS)
777 /* Skip over device-path nodes before the file path. */
778 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
779 fp = efi_dp_next(fp);
782 * Step through the nodes of the directory path until the actual file
783 * node is reached which is the final node in the device path.
786 struct efi_device_path_file_path *fdp =
787 container_of(fp, struct efi_device_path_file_path, dp);
788 struct efi_file_handle *f2;
791 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
792 printf("bad file path!\n");
797 filename = u16_strdup(fdp->str);
800 EFI_CALL(ret = f->open(f, &f2, filename,
801 EFI_FILE_MODE_READ, 0));
803 if (ret != EFI_SUCCESS)
806 fp = efi_dp_next(fp);
808 EFI_CALL(f->close(f));
815 static efi_status_t EFIAPI
816 efi_open_volume(struct efi_simple_file_system_protocol *this,
817 struct efi_file_handle **root)
819 struct file_system *fs = to_fs(this);
821 EFI_ENTRY("%p, %p", this, root);
823 *root = file_open(fs, NULL, NULL, 0, 0);
825 return EFI_EXIT(EFI_SUCCESS);
828 struct efi_simple_file_system_protocol *
829 efi_simple_file_system(struct blk_desc *desc, int part,
830 struct efi_device_path *dp)
832 struct file_system *fs;
834 fs = calloc(1, sizeof(*fs));
835 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
836 fs->base.open_volume = efi_open_volume;