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);
130 /* NOTE: despite what you would expect, 'file_name' is actually a path.
131 * With windoze style backlashes, ofc.
133 static struct efi_file_handle *file_open(struct file_system *fs,
134 struct file_handle *parent, s16 *file_name, u64 mode,
137 struct file_handle *fh;
138 char f0[MAX_UTF8_PER_UTF16] = {0};
143 utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1);
144 flen = u16_strlen((u16 *)file_name);
147 /* we could have a parent, but also an absolute path: */
151 plen = strlen(parent->path) + 1;
154 /* +2 is for null and '/' */
155 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
157 fh->base = efi_file_handle_protocol;
164 strcpy(p, parent->path);
169 utf16_to_utf8((u8 *)p, (u16 *)file_name, flen);
171 if (sanitize_path(fh->path))
174 /* check if file exists: */
178 if ((mode & EFI_FILE_MODE_CREATE) &&
179 (attributes & EFI_FILE_DIRECTORY)) {
180 if (fs_mkdir(fh->path))
182 } else if (!((mode & EFI_FILE_MODE_CREATE) ||
183 fs_exists(fh->path)))
186 /* figure out if file is a directory: */
187 fh->isdir = is_dir(fh);
190 strcpy(fh->path, "");
200 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
201 struct efi_file_handle **new_handle,
202 s16 *file_name, u64 open_mode, u64 attributes)
204 struct file_handle *fh = to_fh(file);
207 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name,
208 open_mode, attributes);
210 /* Check parameters */
211 if (!file || !file || !file_name) {
212 ret = EFI_INVALID_PARAMETER;
215 if (open_mode != EFI_FILE_MODE_READ &&
216 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
217 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
218 EFI_FILE_MODE_CREATE)) {
219 ret = EFI_INVALID_PARAMETER;
222 if ((!(open_mode & EFI_FILE_MODE_CREATE) && attributes) ||
223 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
224 ret = EFI_INVALID_PARAMETER;
229 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
235 return EFI_EXIT(ret);
238 static efi_status_t file_close(struct file_handle *fh)
240 fs_closedir(fh->dirs);
245 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
247 struct file_handle *fh = to_fh(file);
248 EFI_ENTRY("%p", file);
249 return EFI_EXIT(file_close(fh));
252 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
254 struct file_handle *fh = to_fh(file);
255 efi_status_t ret = EFI_SUCCESS;
257 EFI_ENTRY("%p", file);
259 if (set_blk_dev(fh)) {
260 ret = EFI_DEVICE_ERROR;
264 if (fs_unlink(fh->path))
265 ret = EFI_DEVICE_ERROR;
269 return EFI_EXIT(ret);
272 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
276 /* fs_read expects buffer address, not pointer */
277 uintptr_t buffer_addr = (uintptr_t)map_to_sysmem(buffer);
279 if (fs_read(fh->path, buffer_addr, fh->offset,
280 *buffer_size, &actread))
281 return EFI_DEVICE_ERROR;
283 *buffer_size = actread;
284 fh->offset += actread;
289 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
292 struct efi_file_info *info = buffer;
293 struct fs_dirent *dent;
294 unsigned int required_size;
297 assert(fh->offset == 0);
298 fh->dirs = fs_opendir(fh->path);
300 return EFI_DEVICE_ERROR;
304 * So this is a bit awkward. Since fs layer is stateful and we
305 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
306 * we might have to return without consuming the dent.. so we
307 * have to stash it for next call.
313 dent = fs_readdir(fh->dirs);
318 /* no more files in directory: */
319 /* workaround shim.efi bug/quirk.. as find_boot_csv()
320 * loops through directory contents, it initially calls
321 * read w/ zero length buffer to find out how much mem
322 * to allocate for the EFI_FILE_INFO, then allocates,
323 * and then calls a 2nd time. If we return size of
324 * zero the first time, it happily passes that to
325 * AllocateZeroPool(), and when that returns NULL it
326 * thinks it is EFI_OUT_OF_RESOURCES. So on first
327 * call return a non-zero size:
329 if (*buffer_size == 0)
330 *buffer_size = sizeof(*info);
336 /* check buffer size: */
337 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
338 if (*buffer_size < required_size) {
339 *buffer_size = required_size;
341 return EFI_BUFFER_TOO_SMALL;
344 *buffer_size = required_size;
345 memset(info, 0, required_size);
347 info->size = required_size;
348 info->file_size = dent->size;
349 info->physical_size = dent->size;
351 if (dent->type == FS_DT_DIR)
352 info->attribute |= EFI_FILE_DIRECTORY;
354 ascii2unicode((u16 *)info->file_name, dent->name);
361 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
362 efi_uintn_t *buffer_size, void *buffer)
364 struct file_handle *fh = to_fh(file);
365 efi_status_t ret = EFI_SUCCESS;
368 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
370 if (!buffer_size || !buffer) {
371 ret = EFI_INVALID_PARAMETER;
375 if (set_blk_dev(fh)) {
376 ret = EFI_DEVICE_ERROR;
382 ret = dir_read(fh, &bs, buffer);
384 ret = file_read(fh, &bs, buffer);
388 *buffer_size = SIZE_MAX;
391 return EFI_EXIT(ret);
394 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
395 efi_uintn_t *buffer_size,
398 struct file_handle *fh = to_fh(file);
399 efi_status_t ret = EFI_SUCCESS;
402 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
404 if (set_blk_dev(fh)) {
405 ret = EFI_DEVICE_ERROR;
409 if (fs_write(fh->path, (ulong)buffer, fh->offset, *buffer_size,
411 ret = EFI_DEVICE_ERROR;
415 *buffer_size = actwrite;
416 fh->offset += actwrite;
419 return EFI_EXIT(ret);
422 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
425 struct file_handle *fh = to_fh(file);
427 EFI_ENTRY("%p, %p", file, pos);
429 if (fh->offset <= SIZE_MAX) {
431 return EFI_EXIT(EFI_SUCCESS);
433 return EFI_EXIT(EFI_DEVICE_ERROR);
437 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
440 struct file_handle *fh = to_fh(file);
441 efi_status_t ret = EFI_SUCCESS;
443 EFI_ENTRY("%p, %zu", file, pos);
447 ret = EFI_UNSUPPORTED;
450 fs_closedir(fh->dirs);
457 if (set_blk_dev(fh)) {
458 ret = EFI_DEVICE_ERROR;
462 if (fs_size(fh->path, &file_size)) {
463 ret = EFI_DEVICE_ERROR;
473 return EFI_EXIT(ret);
476 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
477 const efi_guid_t *info_type,
478 efi_uintn_t *buffer_size,
481 struct file_handle *fh = to_fh(file);
482 efi_status_t ret = EFI_SUCCESS;
484 EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer);
486 if (!guidcmp(info_type, &efi_file_info_guid)) {
487 struct efi_file_info *info = buffer;
488 char *filename = basename(fh);
489 unsigned int required_size;
492 /* check buffer size: */
493 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
494 if (*buffer_size < required_size) {
495 *buffer_size = required_size;
496 ret = EFI_BUFFER_TOO_SMALL;
500 if (set_blk_dev(fh)) {
501 ret = EFI_DEVICE_ERROR;
505 if (fs_size(fh->path, &file_size)) {
506 ret = EFI_DEVICE_ERROR;
510 memset(info, 0, required_size);
512 info->size = required_size;
513 info->file_size = file_size;
514 info->physical_size = file_size;
517 info->attribute |= EFI_FILE_DIRECTORY;
519 ascii2unicode((u16 *)info->file_name, filename);
520 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
521 struct efi_file_system_info *info = buffer;
522 disk_partition_t part;
523 efi_uintn_t required_size;
526 if (fh->fs->part >= 1)
527 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
529 r = part_get_info_whole_disk(fh->fs->desc, &part);
531 ret = EFI_DEVICE_ERROR;
534 required_size = sizeof(info) + 2 *
535 (strlen((const char *)part.name) + 1);
536 if (*buffer_size < required_size) {
537 *buffer_size = required_size;
538 ret = EFI_BUFFER_TOO_SMALL;
542 memset(info, 0, required_size);
544 info->size = required_size;
545 info->read_only = true;
546 info->volume_size = part.size * part.blksz;
547 info->free_space = 0;
548 info->block_size = part.blksz;
550 * TODO: The volume label is not available in U-Boot.
551 * Use the partition name as substitute.
553 ascii2unicode((u16 *)info->volume_label,
554 (const char *)part.name);
556 ret = EFI_UNSUPPORTED;
560 return EFI_EXIT(ret);
563 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
564 const efi_guid_t *info_type,
565 efi_uintn_t buffer_size,
568 EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
570 return EFI_EXIT(EFI_UNSUPPORTED);
573 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
575 EFI_ENTRY("%p", file);
576 return EFI_EXIT(EFI_SUCCESS);
579 static const struct efi_file_handle efi_file_handle_protocol = {
580 .rev = EFI_FILE_PROTOCOL_REVISION,
581 .open = efi_file_open,
582 .close = efi_file_close,
583 .delete = efi_file_delete,
584 .read = efi_file_read,
585 .write = efi_file_write,
586 .getpos = efi_file_getpos,
587 .setpos = efi_file_setpos,
588 .getinfo = efi_file_getinfo,
589 .setinfo = efi_file_setinfo,
590 .flush = efi_file_flush,
593 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
595 struct efi_simple_file_system_protocol *v;
596 struct efi_file_handle *f;
599 v = efi_fs_from_path(fp);
603 EFI_CALL(ret = v->open_volume(v, &f));
604 if (ret != EFI_SUCCESS)
607 /* skip over device-path nodes before the file path: */
608 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
609 fp = efi_dp_next(fp);
612 struct efi_device_path_file_path *fdp =
613 container_of(fp, struct efi_device_path_file_path, dp);
614 struct efi_file_handle *f2;
616 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
617 printf("bad file path!\n");
622 EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
623 EFI_FILE_MODE_READ, 0));
624 if (ret != EFI_SUCCESS)
627 fp = efi_dp_next(fp);
629 EFI_CALL(f->close(f));
636 static efi_status_t EFIAPI
637 efi_open_volume(struct efi_simple_file_system_protocol *this,
638 struct efi_file_handle **root)
640 struct file_system *fs = to_fs(this);
642 EFI_ENTRY("%p, %p", this, root);
644 *root = file_open(fs, NULL, NULL, 0, 0);
646 return EFI_EXIT(EFI_SUCCESS);
649 struct efi_simple_file_system_protocol *
650 efi_simple_file_system(struct blk_desc *desc, int part,
651 struct efi_device_path *dp)
653 struct file_system *fs;
655 fs = calloc(1, sizeof(*fs));
656 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
657 fs->base.open_volume = efi_open_volume;