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 || !file || !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;
232 if ((!(open_mode & EFI_FILE_MODE_CREATE) && attributes) ||
233 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
234 ret = EFI_INVALID_PARAMETER;
239 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
245 return EFI_EXIT(ret);
248 static efi_status_t file_close(struct file_handle *fh)
250 fs_closedir(fh->dirs);
255 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
257 struct file_handle *fh = to_fh(file);
258 EFI_ENTRY("%p", file);
259 return EFI_EXIT(file_close(fh));
262 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
264 struct file_handle *fh = to_fh(file);
265 efi_status_t ret = EFI_SUCCESS;
267 EFI_ENTRY("%p", file);
269 if (set_blk_dev(fh)) {
270 ret = EFI_DEVICE_ERROR;
274 if (fs_unlink(fh->path))
275 ret = EFI_DEVICE_ERROR;
279 return EFI_EXIT(ret);
282 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
286 /* fs_read expects buffer address, not pointer */
287 uintptr_t buffer_addr = (uintptr_t)map_to_sysmem(buffer);
289 if (fs_read(fh->path, buffer_addr, fh->offset,
290 *buffer_size, &actread))
291 return EFI_DEVICE_ERROR;
293 *buffer_size = actread;
294 fh->offset += actread;
299 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
302 struct efi_file_info *info = buffer;
303 struct fs_dirent *dent;
304 unsigned int required_size;
307 assert(fh->offset == 0);
308 fh->dirs = fs_opendir(fh->path);
310 return EFI_DEVICE_ERROR;
314 * So this is a bit awkward. Since fs layer is stateful and we
315 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
316 * we might have to return without consuming the dent.. so we
317 * have to stash it for next call.
323 dent = fs_readdir(fh->dirs);
328 /* no more files in directory: */
329 /* workaround shim.efi bug/quirk.. as find_boot_csv()
330 * loops through directory contents, it initially calls
331 * read w/ zero length buffer to find out how much mem
332 * to allocate for the EFI_FILE_INFO, then allocates,
333 * and then calls a 2nd time. If we return size of
334 * zero the first time, it happily passes that to
335 * AllocateZeroPool(), and when that returns NULL it
336 * thinks it is EFI_OUT_OF_RESOURCES. So on first
337 * call return a non-zero size:
339 if (*buffer_size == 0)
340 *buffer_size = sizeof(*info);
346 /* check buffer size: */
347 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
348 if (*buffer_size < required_size) {
349 *buffer_size = required_size;
351 return EFI_BUFFER_TOO_SMALL;
354 *buffer_size = required_size;
355 memset(info, 0, required_size);
357 info->size = required_size;
358 info->file_size = dent->size;
359 info->physical_size = dent->size;
361 if (dent->type == FS_DT_DIR)
362 info->attribute |= EFI_FILE_DIRECTORY;
364 ascii2unicode((u16 *)info->file_name, dent->name);
371 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
372 efi_uintn_t *buffer_size, void *buffer)
374 struct file_handle *fh = to_fh(file);
375 efi_status_t ret = EFI_SUCCESS;
378 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
380 if (!buffer_size || !buffer) {
381 ret = EFI_INVALID_PARAMETER;
385 if (set_blk_dev(fh)) {
386 ret = EFI_DEVICE_ERROR;
392 ret = dir_read(fh, &bs, buffer);
394 ret = file_read(fh, &bs, buffer);
398 *buffer_size = SIZE_MAX;
401 return EFI_EXIT(ret);
404 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
405 efi_uintn_t *buffer_size,
408 struct file_handle *fh = to_fh(file);
409 efi_status_t ret = EFI_SUCCESS;
412 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
414 if (set_blk_dev(fh)) {
415 ret = EFI_DEVICE_ERROR;
419 if (fs_write(fh->path, (ulong)buffer, fh->offset, *buffer_size,
421 ret = EFI_DEVICE_ERROR;
425 *buffer_size = actwrite;
426 fh->offset += actwrite;
429 return EFI_EXIT(ret);
432 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
435 struct file_handle *fh = to_fh(file);
437 EFI_ENTRY("%p, %p", file, pos);
439 if (fh->offset <= SIZE_MAX) {
441 return EFI_EXIT(EFI_SUCCESS);
443 return EFI_EXIT(EFI_DEVICE_ERROR);
447 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
450 struct file_handle *fh = to_fh(file);
451 efi_status_t ret = EFI_SUCCESS;
453 EFI_ENTRY("%p, %zu", file, pos);
457 ret = EFI_UNSUPPORTED;
460 fs_closedir(fh->dirs);
467 if (set_blk_dev(fh)) {
468 ret = EFI_DEVICE_ERROR;
472 if (fs_size(fh->path, &file_size)) {
473 ret = EFI_DEVICE_ERROR;
483 return EFI_EXIT(ret);
486 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
487 const efi_guid_t *info_type,
488 efi_uintn_t *buffer_size,
491 struct file_handle *fh = to_fh(file);
492 efi_status_t ret = EFI_SUCCESS;
494 EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer);
496 if (!guidcmp(info_type, &efi_file_info_guid)) {
497 struct efi_file_info *info = buffer;
498 char *filename = basename(fh);
499 unsigned int required_size;
502 /* check buffer size: */
503 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
504 if (*buffer_size < required_size) {
505 *buffer_size = required_size;
506 ret = EFI_BUFFER_TOO_SMALL;
510 if (set_blk_dev(fh)) {
511 ret = EFI_DEVICE_ERROR;
515 if (fs_size(fh->path, &file_size)) {
516 ret = EFI_DEVICE_ERROR;
520 memset(info, 0, required_size);
522 info->size = required_size;
523 info->file_size = file_size;
524 info->physical_size = file_size;
527 info->attribute |= EFI_FILE_DIRECTORY;
529 ascii2unicode((u16 *)info->file_name, filename);
530 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
531 struct efi_file_system_info *info = buffer;
532 disk_partition_t part;
533 efi_uintn_t required_size;
536 if (fh->fs->part >= 1)
537 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
539 r = part_get_info_whole_disk(fh->fs->desc, &part);
541 ret = EFI_DEVICE_ERROR;
544 required_size = sizeof(info) + 2 *
545 (strlen((const char *)part.name) + 1);
546 if (*buffer_size < required_size) {
547 *buffer_size = required_size;
548 ret = EFI_BUFFER_TOO_SMALL;
552 memset(info, 0, required_size);
554 info->size = required_size;
555 info->read_only = true;
556 info->volume_size = part.size * part.blksz;
557 info->free_space = 0;
558 info->block_size = part.blksz;
560 * TODO: The volume label is not available in U-Boot.
561 * Use the partition name as substitute.
563 ascii2unicode((u16 *)info->volume_label,
564 (const char *)part.name);
566 ret = EFI_UNSUPPORTED;
570 return EFI_EXIT(ret);
573 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
574 const efi_guid_t *info_type,
575 efi_uintn_t buffer_size,
578 EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
580 return EFI_EXIT(EFI_UNSUPPORTED);
583 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
585 EFI_ENTRY("%p", file);
586 return EFI_EXIT(EFI_SUCCESS);
589 static const struct efi_file_handle efi_file_handle_protocol = {
590 .rev = EFI_FILE_PROTOCOL_REVISION,
591 .open = efi_file_open,
592 .close = efi_file_close,
593 .delete = efi_file_delete,
594 .read = efi_file_read,
595 .write = efi_file_write,
596 .getpos = efi_file_getpos,
597 .setpos = efi_file_setpos,
598 .getinfo = efi_file_getinfo,
599 .setinfo = efi_file_setinfo,
600 .flush = efi_file_flush,
603 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
605 struct efi_simple_file_system_protocol *v;
606 struct efi_file_handle *f;
609 v = efi_fs_from_path(fp);
613 EFI_CALL(ret = v->open_volume(v, &f));
614 if (ret != EFI_SUCCESS)
617 /* skip over device-path nodes before the file path: */
618 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
619 fp = efi_dp_next(fp);
622 struct efi_device_path_file_path *fdp =
623 container_of(fp, struct efi_device_path_file_path, dp);
624 struct efi_file_handle *f2;
626 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
627 printf("bad file path!\n");
632 EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
633 EFI_FILE_MODE_READ, 0));
634 if (ret != EFI_SUCCESS)
637 fp = efi_dp_next(fp);
639 EFI_CALL(f->close(f));
646 static efi_status_t EFIAPI
647 efi_open_volume(struct efi_simple_file_system_protocol *this,
648 struct efi_file_handle **root)
650 struct file_system *fs = to_fs(this);
652 EFI_ENTRY("%p, %p", this, root);
654 *root = file_open(fs, NULL, NULL, 0, 0);
656 return EFI_EXIT(EFI_SUCCESS);
659 struct efi_simple_file_system_protocol *
660 efi_simple_file_system(struct blk_desc *desc, int part,
661 struct efi_device_path *dp)
663 struct file_system *fs;
665 fs = calloc(1, sizeof(*fs));
666 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
667 fs->base.open_volume = efi_open_volume;