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 */
32 /* for reading a directory: */
33 struct fs_dir_stream *dirs;
34 struct fs_dirent *dent;
38 #define to_fh(x) container_of(x, struct file_handle, base)
40 static const struct efi_file_handle efi_file_handle_protocol;
42 static char *basename(struct file_handle *fh)
44 char *s = strrchr(fh->path, '/');
50 static int set_blk_dev(struct file_handle *fh)
52 return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
56 * is_dir() - check if file handle points to directory
58 * We assume that set_blk_dev(fh) has been called already.
61 * Return: true if file handle points to a directory
63 static int is_dir(struct file_handle *fh)
65 struct fs_dir_stream *dirs;
67 dirs = fs_opendir(fh->path);
77 * Normalize a path which may include either back or fwd slashes,
78 * double slashes, . or .. entries in the path, etc.
80 static int sanitize_path(char *path)
84 /* backslash to slash: */
86 while ((p = strchr(p, '\\')))
89 /* handle double-slashes: */
91 while ((p = strstr(p, "//"))) {
93 memmove(p, src, strlen(src) + 1);
96 /* handle extra /.'s */
98 while ((p = strstr(p, "/."))) {
100 * You'd be tempted to do this *after* handling ".."s
101 * below to avoid having to check if "/." is start of
102 * a "/..", but that won't have the correct results..
103 * for example, "/foo/./../bar" would get resolved to
104 * "/foo/bar" if you did these two passes in the other
112 memmove(p, src, strlen(src) + 1);
115 /* handle extra /..'s: */
117 while ((p = strstr(p, "/.."))) {
122 /* find beginning of previous path entry: */
131 memmove(p, src, strlen(src) + 1);
138 * efi_create_file() - create file or directory
141 * @attributes: attributes for newly created file
142 * Returns: 0 for success
144 static int efi_create_file(struct file_handle *fh, u64 attributes)
147 void *buffer = &actwrite;
149 if (attributes & EFI_FILE_DIRECTORY)
150 return fs_mkdir(fh->path);
152 return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
157 * file_open() - open a file handle
160 * @parent: directory relative to which the file is to be opened
161 * @file_name: path of the file to be opened. '\', '.', or '..' may
162 * be used as modifiers. A leading backslash indicates an
164 * @mode: bit mask indicating the access mode (read, write,
166 * @attributes: attributes for newly created file
167 * Returns: handle to the opened file or NULL
169 static struct efi_file_handle *file_open(struct file_system *fs,
170 struct file_handle *parent, u16 *file_name, u64 mode,
173 struct file_handle *fh;
174 char f0[MAX_UTF8_PER_UTF16] = {0};
179 utf16_to_utf8((u8 *)f0, file_name, 1);
180 flen = u16_strlen(file_name);
183 /* we could have a parent, but also an absolute path: */
187 plen = strlen(parent->path) + 1;
190 /* +2 is for null and '/' */
191 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
193 fh->base = efi_file_handle_protocol;
201 strcpy(p, parent->path);
206 utf16_to_utf8((u8 *)p, file_name, flen);
208 if (sanitize_path(fh->path))
211 /* check if file exists: */
215 exists = fs_exists(fh->path);
216 /* fs_exists() calls fs_close(), so open file system again */
221 if (!(mode & EFI_FILE_MODE_CREATE) ||
222 efi_create_file(fh, attributes))
226 /* figure out if file is a directory: */
227 fh->isdir = is_dir(fh);
230 strcpy(fh->path, "");
240 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
241 struct efi_file_handle **new_handle,
242 u16 *file_name, u64 open_mode, u64 attributes)
244 struct file_handle *fh = to_fh(file);
247 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
248 file_name, open_mode, attributes);
250 /* Check parameters */
251 if (!file || !new_handle || !file_name) {
252 ret = EFI_INVALID_PARAMETER;
255 if (open_mode != EFI_FILE_MODE_READ &&
256 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
257 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
258 EFI_FILE_MODE_CREATE)) {
259 ret = EFI_INVALID_PARAMETER;
263 * The UEFI spec requires that attributes are only set in create mode.
264 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
265 * read mode. EDK2 does not check that attributes are zero if not in
268 * So here we only check attributes in create mode and do not check
269 * that they are zero otherwise.
271 if ((open_mode & EFI_FILE_MODE_CREATE) &&
272 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
273 ret = EFI_INVALID_PARAMETER;
278 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
280 EFI_PRINT("file handle %p\n", *new_handle);
286 return EFI_EXIT(ret);
289 static efi_status_t file_close(struct file_handle *fh)
291 fs_closedir(fh->dirs);
296 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
298 struct file_handle *fh = to_fh(file);
299 EFI_ENTRY("%p", file);
300 return EFI_EXIT(file_close(fh));
303 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
305 struct file_handle *fh = to_fh(file);
306 efi_status_t ret = EFI_SUCCESS;
308 EFI_ENTRY("%p", file);
310 if (set_blk_dev(fh)) {
311 ret = EFI_DEVICE_ERROR;
315 if (fs_unlink(fh->path))
316 ret = EFI_DEVICE_ERROR;
320 return EFI_EXIT(ret);
323 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
328 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
329 *buffer_size, &actread))
330 return EFI_DEVICE_ERROR;
332 *buffer_size = actread;
333 fh->offset += actread;
338 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
341 struct efi_file_info *info = buffer;
342 struct fs_dirent *dent;
343 unsigned int required_size;
346 assert(fh->offset == 0);
347 fh->dirs = fs_opendir(fh->path);
349 return EFI_DEVICE_ERROR;
353 * So this is a bit awkward. Since fs layer is stateful and we
354 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
355 * we might have to return without consuming the dent.. so we
356 * have to stash it for next call.
362 dent = fs_readdir(fh->dirs);
367 /* no more files in directory: */
368 /* workaround shim.efi bug/quirk.. as find_boot_csv()
369 * loops through directory contents, it initially calls
370 * read w/ zero length buffer to find out how much mem
371 * to allocate for the EFI_FILE_INFO, then allocates,
372 * and then calls a 2nd time. If we return size of
373 * zero the first time, it happily passes that to
374 * AllocateZeroPool(), and when that returns NULL it
375 * thinks it is EFI_OUT_OF_RESOURCES. So on first
376 * call return a non-zero size:
378 if (*buffer_size == 0)
379 *buffer_size = sizeof(*info);
385 /* check buffer size: */
386 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
387 if (*buffer_size < required_size) {
388 *buffer_size = required_size;
390 return EFI_BUFFER_TOO_SMALL;
393 *buffer_size = required_size;
394 memset(info, 0, required_size);
396 info->size = required_size;
397 info->file_size = dent->size;
398 info->physical_size = dent->size;
400 if (dent->type == FS_DT_DIR)
401 info->attribute |= EFI_FILE_DIRECTORY;
403 ascii2unicode(info->file_name, dent->name);
410 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
411 efi_uintn_t *buffer_size, void *buffer)
413 struct file_handle *fh = to_fh(file);
414 efi_status_t ret = EFI_SUCCESS;
417 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
419 if (!buffer_size || !buffer) {
420 ret = EFI_INVALID_PARAMETER;
424 if (set_blk_dev(fh)) {
425 ret = EFI_DEVICE_ERROR;
431 ret = dir_read(fh, &bs, buffer);
433 ret = file_read(fh, &bs, buffer);
437 *buffer_size = SIZE_MAX;
440 return EFI_EXIT(ret);
443 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
444 efi_uintn_t *buffer_size,
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);
453 if (set_blk_dev(fh)) {
454 ret = EFI_DEVICE_ERROR;
458 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
460 ret = EFI_DEVICE_ERROR;
464 *buffer_size = actwrite;
465 fh->offset += actwrite;
468 return EFI_EXIT(ret);
472 * efi_file_getpos() - get current position in file
474 * This function implements the GetPosition service of the EFI file protocol.
475 * See the UEFI spec for details.
478 * @pos: pointer to file position
479 * Return: status code
481 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
484 efi_status_t ret = EFI_SUCCESS;
485 struct file_handle *fh = to_fh(file);
487 EFI_ENTRY("%p, %p", file, pos);
490 ret = EFI_UNSUPPORTED;
496 return EFI_EXIT(ret);
500 * efi_file_setpos() - set current position in file
502 * This function implements the SetPosition service of the EFI file protocol.
503 * See the UEFI spec for details.
506 * @pos: new file position
507 * Return: status code
509 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
512 struct file_handle *fh = to_fh(file);
513 efi_status_t ret = EFI_SUCCESS;
515 EFI_ENTRY("%p, %llu", file, pos);
519 ret = EFI_UNSUPPORTED;
522 fs_closedir(fh->dirs);
529 if (set_blk_dev(fh)) {
530 ret = EFI_DEVICE_ERROR;
534 if (fs_size(fh->path, &file_size)) {
535 ret = EFI_DEVICE_ERROR;
545 return EFI_EXIT(ret);
548 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
549 const efi_guid_t *info_type,
550 efi_uintn_t *buffer_size,
553 struct file_handle *fh = to_fh(file);
554 efi_status_t ret = EFI_SUCCESS;
556 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
558 if (!guidcmp(info_type, &efi_file_info_guid)) {
559 struct efi_file_info *info = buffer;
560 char *filename = basename(fh);
561 unsigned int required_size;
564 /* check buffer size: */
565 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
566 if (*buffer_size < required_size) {
567 *buffer_size = required_size;
568 ret = EFI_BUFFER_TOO_SMALL;
572 if (set_blk_dev(fh)) {
573 ret = EFI_DEVICE_ERROR;
577 if (fs_size(fh->path, &file_size)) {
578 ret = EFI_DEVICE_ERROR;
582 memset(info, 0, required_size);
584 info->size = required_size;
585 info->file_size = file_size;
586 info->physical_size = file_size;
589 info->attribute |= EFI_FILE_DIRECTORY;
591 ascii2unicode(info->file_name, filename);
592 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
593 struct efi_file_system_info *info = buffer;
594 disk_partition_t part;
595 efi_uintn_t required_size;
598 if (fh->fs->part >= 1)
599 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
601 r = part_get_info_whole_disk(fh->fs->desc, &part);
603 ret = EFI_DEVICE_ERROR;
606 required_size = sizeof(info) + 2 *
607 (strlen((const char *)part.name) + 1);
608 if (*buffer_size < required_size) {
609 *buffer_size = required_size;
610 ret = EFI_BUFFER_TOO_SMALL;
614 memset(info, 0, required_size);
616 info->size = required_size;
617 info->read_only = true;
618 info->volume_size = part.size * part.blksz;
619 info->free_space = 0;
620 info->block_size = part.blksz;
622 * TODO: The volume label is not available in U-Boot.
623 * Use the partition name as substitute.
625 ascii2unicode((u16 *)info->volume_label,
626 (const char *)part.name);
628 ret = EFI_UNSUPPORTED;
632 return EFI_EXIT(ret);
635 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
636 const efi_guid_t *info_type,
637 efi_uintn_t buffer_size,
640 struct file_handle *fh = to_fh(file);
641 efi_status_t ret = EFI_UNSUPPORTED;
643 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
645 if (!guidcmp(info_type, &efi_file_info_guid)) {
646 struct efi_file_info *info = (struct efi_file_info *)buffer;
647 char *filename = basename(fh);
648 char *new_file_name, *pos;
651 if (buffer_size < sizeof(struct efi_file_info)) {
652 ret = EFI_BAD_BUFFER_SIZE;
655 /* We cannot change the directory attribute */
656 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
657 ret = EFI_ACCESS_DENIED;
660 /* Check for renaming */
661 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
662 if (!new_file_name) {
663 ret = EFI_OUT_OF_RESOURCES;
667 utf16_utf8_strcpy(&pos, info->file_name);
668 if (strcmp(new_file_name, filename)) {
669 /* TODO: we do not support renaming */
670 EFI_PRINT("Renaming not supported\n");
672 ret = EFI_ACCESS_DENIED;
676 /* Check for truncation */
677 if (set_blk_dev(fh)) {
678 ret = EFI_DEVICE_ERROR;
681 if (fs_size(fh->path, &file_size)) {
682 ret = EFI_DEVICE_ERROR;
685 if (file_size != info->file_size) {
686 /* TODO: we do not support truncation */
687 EFI_PRINT("Truncation not supported\n");
688 ret = EFI_ACCESS_DENIED;
692 * We do not care for the other attributes
693 * TODO: Support read only
696 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
697 if (buffer_size < sizeof(struct efi_file_system_info)) {
698 ret = EFI_BAD_BUFFER_SIZE;
702 ret = EFI_UNSUPPORTED;
705 return EFI_EXIT(ret);
708 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
710 EFI_ENTRY("%p", file);
711 return EFI_EXIT(EFI_SUCCESS);
714 static const struct efi_file_handle efi_file_handle_protocol = {
716 * TODO: We currently only support EFI file protocol revision 0x00010000
717 * while UEFI specs 2.4 - 2.7 prescribe revision 0x00020000.
719 .rev = EFI_FILE_PROTOCOL_REVISION,
720 .open = efi_file_open,
721 .close = efi_file_close,
722 .delete = efi_file_delete,
723 .read = efi_file_read,
724 .write = efi_file_write,
725 .getpos = efi_file_getpos,
726 .setpos = efi_file_setpos,
727 .getinfo = efi_file_getinfo,
728 .setinfo = efi_file_setinfo,
729 .flush = efi_file_flush,
733 * efi_file_from_path() - open file via device path
736 * @return: EFI_FILE_PROTOCOL for the file or NULL
738 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
740 struct efi_simple_file_system_protocol *v;
741 struct efi_file_handle *f;
744 v = efi_fs_from_path(fp);
748 EFI_CALL(ret = v->open_volume(v, &f));
749 if (ret != EFI_SUCCESS)
752 /* Skip over device-path nodes before the file path. */
753 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
754 fp = efi_dp_next(fp);
757 * Step through the nodes of the directory path until the actual file
758 * node is reached which is the final node in the device path.
761 struct efi_device_path_file_path *fdp =
762 container_of(fp, struct efi_device_path_file_path, dp);
763 struct efi_file_handle *f2;
765 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
766 printf("bad file path!\n");
771 EFI_CALL(ret = f->open(f, &f2, fdp->str,
772 EFI_FILE_MODE_READ, 0));
773 if (ret != EFI_SUCCESS)
776 fp = efi_dp_next(fp);
778 EFI_CALL(f->close(f));
785 static efi_status_t EFIAPI
786 efi_open_volume(struct efi_simple_file_system_protocol *this,
787 struct efi_file_handle **root)
789 struct file_system *fs = to_fs(this);
791 EFI_ENTRY("%p, %p", this, root);
793 *root = file_open(fs, NULL, NULL, 0, 0);
795 return EFI_EXIT(EFI_SUCCESS);
798 struct efi_simple_file_system_protocol *
799 efi_simple_file_system(struct blk_desc *desc, int part,
800 struct efi_device_path *dp)
802 struct file_system *fs;
804 fs = calloc(1, sizeof(*fs));
805 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
806 fs->base.open_volume = efi_open_volume;