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) || fs_unlink(fh->path))
311 ret = EFI_WARN_DELETE_FAILURE;
314 return EFI_EXIT(ret);
317 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
322 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
323 *buffer_size, &actread))
324 return EFI_DEVICE_ERROR;
326 *buffer_size = actread;
327 fh->offset += actread;
332 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
335 struct efi_file_info *info = buffer;
336 struct fs_dirent *dent;
337 unsigned int required_size;
340 assert(fh->offset == 0);
341 fh->dirs = fs_opendir(fh->path);
343 return EFI_DEVICE_ERROR;
347 * So this is a bit awkward. Since fs layer is stateful and we
348 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
349 * we might have to return without consuming the dent.. so we
350 * have to stash it for next call.
356 dent = fs_readdir(fh->dirs);
361 /* no more files in directory: */
362 /* workaround shim.efi bug/quirk.. as find_boot_csv()
363 * loops through directory contents, it initially calls
364 * read w/ zero length buffer to find out how much mem
365 * to allocate for the EFI_FILE_INFO, then allocates,
366 * and then calls a 2nd time. If we return size of
367 * zero the first time, it happily passes that to
368 * AllocateZeroPool(), and when that returns NULL it
369 * thinks it is EFI_OUT_OF_RESOURCES. So on first
370 * call return a non-zero size:
372 if (*buffer_size == 0)
373 *buffer_size = sizeof(*info);
379 /* check buffer size: */
380 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
381 if (*buffer_size < required_size) {
382 *buffer_size = required_size;
384 return EFI_BUFFER_TOO_SMALL;
387 *buffer_size = required_size;
388 memset(info, 0, required_size);
390 info->size = required_size;
391 info->file_size = dent->size;
392 info->physical_size = dent->size;
394 if (dent->type == FS_DT_DIR)
395 info->attribute |= EFI_FILE_DIRECTORY;
397 ascii2unicode(info->file_name, dent->name);
404 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
405 efi_uintn_t *buffer_size, void *buffer)
407 struct file_handle *fh = to_fh(file);
408 efi_status_t ret = EFI_SUCCESS;
411 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
413 if (!buffer_size || !buffer) {
414 ret = EFI_INVALID_PARAMETER;
418 if (set_blk_dev(fh)) {
419 ret = EFI_DEVICE_ERROR;
425 ret = dir_read(fh, &bs, buffer);
427 ret = file_read(fh, &bs, buffer);
431 *buffer_size = SIZE_MAX;
434 return EFI_EXIT(ret);
437 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
438 efi_uintn_t *buffer_size,
441 struct file_handle *fh = to_fh(file);
442 efi_status_t ret = EFI_SUCCESS;
445 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
447 if (set_blk_dev(fh)) {
448 ret = EFI_DEVICE_ERROR;
452 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
454 ret = EFI_DEVICE_ERROR;
458 *buffer_size = actwrite;
459 fh->offset += actwrite;
462 return EFI_EXIT(ret);
466 * efi_file_getpos() - get current position in file
468 * This function implements the GetPosition service of the EFI file protocol.
469 * See the UEFI spec for details.
472 * @pos: pointer to file position
473 * Return: status code
475 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
478 efi_status_t ret = EFI_SUCCESS;
479 struct file_handle *fh = to_fh(file);
481 EFI_ENTRY("%p, %p", file, pos);
484 ret = EFI_UNSUPPORTED;
490 return EFI_EXIT(ret);
494 * efi_file_setpos() - set current position in file
496 * This function implements the SetPosition service of the EFI file protocol.
497 * See the UEFI spec for details.
500 * @pos: new file position
501 * Return: status code
503 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
506 struct file_handle *fh = to_fh(file);
507 efi_status_t ret = EFI_SUCCESS;
509 EFI_ENTRY("%p, %llu", file, pos);
513 ret = EFI_UNSUPPORTED;
516 fs_closedir(fh->dirs);
523 if (set_blk_dev(fh)) {
524 ret = EFI_DEVICE_ERROR;
528 if (fs_size(fh->path, &file_size)) {
529 ret = EFI_DEVICE_ERROR;
539 return EFI_EXIT(ret);
542 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
543 const efi_guid_t *info_type,
544 efi_uintn_t *buffer_size,
547 struct file_handle *fh = to_fh(file);
548 efi_status_t ret = EFI_SUCCESS;
550 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
552 if (!guidcmp(info_type, &efi_file_info_guid)) {
553 struct efi_file_info *info = buffer;
554 char *filename = basename(fh);
555 unsigned int required_size;
558 /* check buffer size: */
559 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
560 if (*buffer_size < required_size) {
561 *buffer_size = required_size;
562 ret = EFI_BUFFER_TOO_SMALL;
566 if (set_blk_dev(fh)) {
567 ret = EFI_DEVICE_ERROR;
571 if (fs_size(fh->path, &file_size)) {
572 ret = EFI_DEVICE_ERROR;
576 memset(info, 0, required_size);
578 info->size = required_size;
579 info->file_size = file_size;
580 info->physical_size = file_size;
583 info->attribute |= EFI_FILE_DIRECTORY;
585 ascii2unicode(info->file_name, filename);
586 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
587 struct efi_file_system_info *info = buffer;
588 disk_partition_t part;
589 efi_uintn_t required_size;
592 if (fh->fs->part >= 1)
593 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
595 r = part_get_info_whole_disk(fh->fs->desc, &part);
597 ret = EFI_DEVICE_ERROR;
600 required_size = sizeof(info) + 2 *
601 (strlen((const char *)part.name) + 1);
602 if (*buffer_size < required_size) {
603 *buffer_size = required_size;
604 ret = EFI_BUFFER_TOO_SMALL;
608 memset(info, 0, required_size);
610 info->size = required_size;
611 info->read_only = true;
612 info->volume_size = part.size * part.blksz;
613 info->free_space = 0;
614 info->block_size = part.blksz;
616 * TODO: The volume label is not available in U-Boot.
617 * Use the partition name as substitute.
619 ascii2unicode((u16 *)info->volume_label,
620 (const char *)part.name);
622 ret = EFI_UNSUPPORTED;
626 return EFI_EXIT(ret);
629 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
630 const efi_guid_t *info_type,
631 efi_uintn_t buffer_size,
634 struct file_handle *fh = to_fh(file);
635 efi_status_t ret = EFI_UNSUPPORTED;
637 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
639 if (!guidcmp(info_type, &efi_file_info_guid)) {
640 struct efi_file_info *info = (struct efi_file_info *)buffer;
641 char *filename = basename(fh);
642 char *new_file_name, *pos;
645 if (buffer_size < sizeof(struct efi_file_info)) {
646 ret = EFI_BAD_BUFFER_SIZE;
649 /* We cannot change the directory attribute */
650 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
651 ret = EFI_ACCESS_DENIED;
654 /* Check for renaming */
655 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
656 if (!new_file_name) {
657 ret = EFI_OUT_OF_RESOURCES;
661 utf16_utf8_strcpy(&pos, info->file_name);
662 if (strcmp(new_file_name, filename)) {
663 /* TODO: we do not support renaming */
664 EFI_PRINT("Renaming not supported\n");
666 ret = EFI_ACCESS_DENIED;
670 /* Check for truncation */
671 if (set_blk_dev(fh)) {
672 ret = EFI_DEVICE_ERROR;
675 if (fs_size(fh->path, &file_size)) {
676 ret = EFI_DEVICE_ERROR;
679 if (file_size != info->file_size) {
680 /* TODO: we do not support truncation */
681 EFI_PRINT("Truncation not supported\n");
682 ret = EFI_ACCESS_DENIED;
686 * We do not care for the other attributes
687 * TODO: Support read only
690 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
691 if (buffer_size < sizeof(struct efi_file_system_info)) {
692 ret = EFI_BAD_BUFFER_SIZE;
696 ret = EFI_UNSUPPORTED;
699 return EFI_EXIT(ret);
702 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
704 EFI_ENTRY("%p", file);
705 return EFI_EXIT(EFI_SUCCESS);
708 static const struct efi_file_handle efi_file_handle_protocol = {
710 * TODO: We currently only support EFI file protocol revision 0x00010000
711 * while UEFI specs 2.4 - 2.7 prescribe revision 0x00020000.
713 .rev = EFI_FILE_PROTOCOL_REVISION,
714 .open = efi_file_open,
715 .close = efi_file_close,
716 .delete = efi_file_delete,
717 .read = efi_file_read,
718 .write = efi_file_write,
719 .getpos = efi_file_getpos,
720 .setpos = efi_file_setpos,
721 .getinfo = efi_file_getinfo,
722 .setinfo = efi_file_setinfo,
723 .flush = efi_file_flush,
727 * efi_file_from_path() - open file via device path
730 * @return: EFI_FILE_PROTOCOL for the file or NULL
732 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
734 struct efi_simple_file_system_protocol *v;
735 struct efi_file_handle *f;
738 v = efi_fs_from_path(fp);
742 EFI_CALL(ret = v->open_volume(v, &f));
743 if (ret != EFI_SUCCESS)
746 /* Skip over device-path nodes before the file path. */
747 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
748 fp = efi_dp_next(fp);
751 * Step through the nodes of the directory path until the actual file
752 * node is reached which is the final node in the device path.
755 struct efi_device_path_file_path *fdp =
756 container_of(fp, struct efi_device_path_file_path, dp);
757 struct efi_file_handle *f2;
760 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
761 printf("bad file path!\n");
766 filename = u16_strdup(fdp->str);
769 EFI_CALL(ret = f->open(f, &f2, filename,
770 EFI_FILE_MODE_READ, 0));
772 if (ret != EFI_SUCCESS)
775 fp = efi_dp_next(fp);
777 EFI_CALL(f->close(f));
784 static efi_status_t EFIAPI
785 efi_open_volume(struct efi_simple_file_system_protocol *this,
786 struct efi_file_handle **root)
788 struct file_system *fs = to_fs(this);
790 EFI_ENTRY("%p, %p", this, root);
792 *root = file_open(fs, NULL, NULL, 0, 0);
794 return EFI_EXIT(EFI_SUCCESS);
797 struct efi_simple_file_system_protocol *
798 efi_simple_file_system(struct blk_desc *desc, int part,
799 struct efi_device_path *dp)
801 struct file_system *fs;
803 fs = calloc(1, sizeof(*fs));
804 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
805 fs->base.open_volume = efi_open_volume;