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);
55 static int is_dir(struct file_handle *fh)
57 struct fs_dir_stream *dirs;
60 dirs = fs_opendir(fh->path);
70 * Normalize a path which may include either back or fwd slashes,
71 * double slashes, . or .. entries in the path, etc.
73 static int sanitize_path(char *path)
77 /* backslash to slash: */
79 while ((p = strchr(p, '\\')))
82 /* handle double-slashes: */
84 while ((p = strstr(p, "//"))) {
86 memmove(p, src, strlen(src) + 1);
89 /* handle extra /.'s */
91 while ((p = strstr(p, "/."))) {
93 * You'd be tempted to do this *after* handling ".."s
94 * below to avoid having to check if "/." is start of
95 * a "/..", but that won't have the correct results..
96 * for example, "/foo/./../bar" would get resolved to
97 * "/foo/bar" if you did these two passes in the other
105 memmove(p, src, strlen(src) + 1);
108 /* handle extra /..'s: */
110 while ((p = strstr(p, "/.."))) {
115 /* find beginning of previous path entry: */
124 memmove(p, src, strlen(src) + 1);
131 * file_open() - open a file handle
134 * @parent: directory relative to which the file is to be opened
135 * @file_name: path of the file to be opened. '\', '.', or '..' may
136 * be used as modifiers. A leading backslash indicates an
138 * @mode: bit mask indicating the access mode (read, write,
140 * @attributes: attributes for newly created file
141 * Returns: handle to the opened file or NULL
143 static struct efi_file_handle *file_open(struct file_system *fs,
144 struct file_handle *parent, s16 *file_name, u64 mode,
147 struct file_handle *fh;
148 char f0[MAX_UTF8_PER_UTF16] = {0};
153 utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1);
154 flen = u16_strlen((u16 *)file_name);
157 /* we could have a parent, but also an absolute path: */
161 plen = strlen(parent->path) + 1;
164 /* +2 is for null and '/' */
165 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
167 fh->base = efi_file_handle_protocol;
174 strcpy(p, parent->path);
179 utf16_to_utf8((u8 *)p, (u16 *)file_name, flen);
181 if (sanitize_path(fh->path))
184 /* check if file exists: */
188 if ((mode & EFI_FILE_MODE_CREATE) &&
189 (attributes & EFI_FILE_DIRECTORY)) {
190 if (fs_mkdir(fh->path))
192 } else if (!((mode & EFI_FILE_MODE_CREATE) ||
193 fs_exists(fh->path)))
196 /* figure out if file is a directory: */
197 fh->isdir = is_dir(fh);
200 strcpy(fh->path, "");
210 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
211 struct efi_file_handle **new_handle,
212 s16 *file_name, u64 open_mode, u64 attributes)
214 struct file_handle *fh = to_fh(file);
217 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name,
218 open_mode, attributes);
220 /* Check parameters */
221 if (!file || !new_handle || !file_name) {
222 ret = EFI_INVALID_PARAMETER;
225 if (open_mode != EFI_FILE_MODE_READ &&
226 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
227 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
228 EFI_FILE_MODE_CREATE)) {
229 ret = EFI_INVALID_PARAMETER;
233 * The UEFI spec requires that attributes are only set in create mode.
234 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
235 * read mode. EDK2 does not check that attributes are zero if not in
238 * So here we only check attributes in create mode and do not check
239 * that they are zero otherwise.
241 if ((open_mode & EFI_FILE_MODE_CREATE) &&
242 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
243 ret = EFI_INVALID_PARAMETER;
248 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
254 return EFI_EXIT(ret);
257 static efi_status_t file_close(struct file_handle *fh)
259 fs_closedir(fh->dirs);
264 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
266 struct file_handle *fh = to_fh(file);
267 EFI_ENTRY("%p", file);
268 return EFI_EXIT(file_close(fh));
271 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
273 struct file_handle *fh = to_fh(file);
274 efi_status_t ret = EFI_SUCCESS;
276 EFI_ENTRY("%p", file);
278 if (set_blk_dev(fh)) {
279 ret = EFI_DEVICE_ERROR;
283 if (fs_unlink(fh->path))
284 ret = EFI_DEVICE_ERROR;
288 return EFI_EXIT(ret);
291 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
296 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
297 *buffer_size, &actread))
298 return EFI_DEVICE_ERROR;
300 *buffer_size = actread;
301 fh->offset += actread;
306 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
309 struct efi_file_info *info = buffer;
310 struct fs_dirent *dent;
311 unsigned int required_size;
314 assert(fh->offset == 0);
315 fh->dirs = fs_opendir(fh->path);
317 return EFI_DEVICE_ERROR;
321 * So this is a bit awkward. Since fs layer is stateful and we
322 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
323 * we might have to return without consuming the dent.. so we
324 * have to stash it for next call.
330 dent = fs_readdir(fh->dirs);
335 /* no more files in directory: */
336 /* workaround shim.efi bug/quirk.. as find_boot_csv()
337 * loops through directory contents, it initially calls
338 * read w/ zero length buffer to find out how much mem
339 * to allocate for the EFI_FILE_INFO, then allocates,
340 * and then calls a 2nd time. If we return size of
341 * zero the first time, it happily passes that to
342 * AllocateZeroPool(), and when that returns NULL it
343 * thinks it is EFI_OUT_OF_RESOURCES. So on first
344 * call return a non-zero size:
346 if (*buffer_size == 0)
347 *buffer_size = sizeof(*info);
353 /* check buffer size: */
354 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
355 if (*buffer_size < required_size) {
356 *buffer_size = required_size;
358 return EFI_BUFFER_TOO_SMALL;
361 *buffer_size = required_size;
362 memset(info, 0, required_size);
364 info->size = required_size;
365 info->file_size = dent->size;
366 info->physical_size = dent->size;
368 if (dent->type == FS_DT_DIR)
369 info->attribute |= EFI_FILE_DIRECTORY;
371 ascii2unicode((u16 *)info->file_name, dent->name);
378 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
379 efi_uintn_t *buffer_size, void *buffer)
381 struct file_handle *fh = to_fh(file);
382 efi_status_t ret = EFI_SUCCESS;
385 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
387 if (!buffer_size || !buffer) {
388 ret = EFI_INVALID_PARAMETER;
392 if (set_blk_dev(fh)) {
393 ret = EFI_DEVICE_ERROR;
399 ret = dir_read(fh, &bs, buffer);
401 ret = file_read(fh, &bs, buffer);
405 *buffer_size = SIZE_MAX;
408 return EFI_EXIT(ret);
411 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
412 efi_uintn_t *buffer_size,
415 struct file_handle *fh = to_fh(file);
416 efi_status_t ret = EFI_SUCCESS;
419 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
421 if (set_blk_dev(fh)) {
422 ret = EFI_DEVICE_ERROR;
426 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
428 ret = EFI_DEVICE_ERROR;
432 *buffer_size = actwrite;
433 fh->offset += actwrite;
436 return EFI_EXIT(ret);
439 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
442 struct file_handle *fh = to_fh(file);
444 EFI_ENTRY("%p, %p", file, pos);
446 if (fh->offset <= SIZE_MAX) {
448 return EFI_EXIT(EFI_SUCCESS);
450 return EFI_EXIT(EFI_DEVICE_ERROR);
454 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
457 struct file_handle *fh = to_fh(file);
458 efi_status_t ret = EFI_SUCCESS;
460 EFI_ENTRY("%p, %zu", file, pos);
464 ret = EFI_UNSUPPORTED;
467 fs_closedir(fh->dirs);
474 if (set_blk_dev(fh)) {
475 ret = EFI_DEVICE_ERROR;
479 if (fs_size(fh->path, &file_size)) {
480 ret = EFI_DEVICE_ERROR;
490 return EFI_EXIT(ret);
493 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
494 const efi_guid_t *info_type,
495 efi_uintn_t *buffer_size,
498 struct file_handle *fh = to_fh(file);
499 efi_status_t ret = EFI_SUCCESS;
501 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
503 if (!guidcmp(info_type, &efi_file_info_guid)) {
504 struct efi_file_info *info = buffer;
505 char *filename = basename(fh);
506 unsigned int required_size;
509 /* check buffer size: */
510 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
511 if (*buffer_size < required_size) {
512 *buffer_size = required_size;
513 ret = EFI_BUFFER_TOO_SMALL;
517 if (set_blk_dev(fh)) {
518 ret = EFI_DEVICE_ERROR;
522 if (fs_size(fh->path, &file_size)) {
523 ret = EFI_DEVICE_ERROR;
527 memset(info, 0, required_size);
529 info->size = required_size;
530 info->file_size = file_size;
531 info->physical_size = file_size;
534 info->attribute |= EFI_FILE_DIRECTORY;
536 ascii2unicode((u16 *)info->file_name, filename);
537 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
538 struct efi_file_system_info *info = buffer;
539 disk_partition_t part;
540 efi_uintn_t required_size;
543 if (fh->fs->part >= 1)
544 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
546 r = part_get_info_whole_disk(fh->fs->desc, &part);
548 ret = EFI_DEVICE_ERROR;
551 required_size = sizeof(info) + 2 *
552 (strlen((const char *)part.name) + 1);
553 if (*buffer_size < required_size) {
554 *buffer_size = required_size;
555 ret = EFI_BUFFER_TOO_SMALL;
559 memset(info, 0, required_size);
561 info->size = required_size;
562 info->read_only = true;
563 info->volume_size = part.size * part.blksz;
564 info->free_space = 0;
565 info->block_size = part.blksz;
567 * TODO: The volume label is not available in U-Boot.
568 * Use the partition name as substitute.
570 ascii2unicode((u16 *)info->volume_label,
571 (const char *)part.name);
573 ret = EFI_UNSUPPORTED;
577 return EFI_EXIT(ret);
580 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
581 const efi_guid_t *info_type,
582 efi_uintn_t buffer_size,
585 EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
587 return EFI_EXIT(EFI_UNSUPPORTED);
590 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
592 EFI_ENTRY("%p", file);
593 return EFI_EXIT(EFI_SUCCESS);
596 static const struct efi_file_handle efi_file_handle_protocol = {
597 .rev = EFI_FILE_PROTOCOL_REVISION,
598 .open = efi_file_open,
599 .close = efi_file_close,
600 .delete = efi_file_delete,
601 .read = efi_file_read,
602 .write = efi_file_write,
603 .getpos = efi_file_getpos,
604 .setpos = efi_file_setpos,
605 .getinfo = efi_file_getinfo,
606 .setinfo = efi_file_setinfo,
607 .flush = efi_file_flush,
610 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
612 struct efi_simple_file_system_protocol *v;
613 struct efi_file_handle *f;
616 v = efi_fs_from_path(fp);
620 EFI_CALL(ret = v->open_volume(v, &f));
621 if (ret != EFI_SUCCESS)
624 /* skip over device-path nodes before the file path: */
625 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
626 fp = efi_dp_next(fp);
629 struct efi_device_path_file_path *fdp =
630 container_of(fp, struct efi_device_path_file_path, dp);
631 struct efi_file_handle *f2;
633 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
634 printf("bad file path!\n");
639 EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
640 EFI_FILE_MODE_READ, 0));
641 if (ret != EFI_SUCCESS)
644 fp = efi_dp_next(fp);
646 EFI_CALL(f->close(f));
653 static efi_status_t EFIAPI
654 efi_open_volume(struct efi_simple_file_system_protocol *this,
655 struct efi_file_handle **root)
657 struct file_system *fs = to_fs(this);
659 EFI_ENTRY("%p, %p", this, root);
661 *root = file_open(fs, NULL, NULL, 0, 0);
663 return EFI_EXIT(EFI_SUCCESS);
666 struct efi_simple_file_system_protocol *
667 efi_simple_file_system(struct blk_desc *desc, int part,
668 struct efi_device_path *dp)
670 struct file_system *fs;
672 fs = calloc(1, sizeof(*fs));
673 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
674 fs->base.open_volume = efi_open_volume;