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 * file_open() - open a file handle
141 * @parent: directory relative to which the file is to be opened
142 * @file_name: path of the file to be opened. '\', '.', or '..' may
143 * be used as modifiers. A leading backslash indicates an
145 * @mode: bit mask indicating the access mode (read, write,
147 * @attributes: attributes for newly created file
148 * Returns: handle to the opened file or NULL
150 static struct efi_file_handle *file_open(struct file_system *fs,
151 struct file_handle *parent, u16 *file_name, u64 mode,
154 struct file_handle *fh;
155 char f0[MAX_UTF8_PER_UTF16] = {0};
160 utf16_to_utf8((u8 *)f0, file_name, 1);
161 flen = u16_strlen(file_name);
164 /* we could have a parent, but also an absolute path: */
168 plen = strlen(parent->path) + 1;
171 /* +2 is for null and '/' */
172 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
174 fh->base = efi_file_handle_protocol;
181 strcpy(p, parent->path);
186 utf16_to_utf8((u8 *)p, file_name, flen);
188 if (sanitize_path(fh->path))
191 /* check if file exists: */
195 if ((mode & EFI_FILE_MODE_CREATE) &&
196 (attributes & EFI_FILE_DIRECTORY)) {
197 if (fs_mkdir(fh->path))
199 } else if (!((mode & EFI_FILE_MODE_CREATE) ||
200 fs_exists(fh->path)))
203 /* figure out if file is a directory: */
204 fh->isdir = is_dir(fh);
207 strcpy(fh->path, "");
217 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
218 struct efi_file_handle **new_handle,
219 u16 *file_name, u64 open_mode, u64 attributes)
221 struct file_handle *fh = to_fh(file);
224 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
225 (wchar_t *)file_name, open_mode, attributes);
227 /* Check parameters */
228 if (!file || !new_handle || !file_name) {
229 ret = EFI_INVALID_PARAMETER;
232 if (open_mode != EFI_FILE_MODE_READ &&
233 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
234 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
235 EFI_FILE_MODE_CREATE)) {
236 ret = EFI_INVALID_PARAMETER;
240 * The UEFI spec requires that attributes are only set in create mode.
241 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
242 * read mode. EDK2 does not check that attributes are zero if not in
245 * So here we only check attributes in create mode and do not check
246 * that they are zero otherwise.
248 if ((open_mode & EFI_FILE_MODE_CREATE) &&
249 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
250 ret = EFI_INVALID_PARAMETER;
255 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
261 return EFI_EXIT(ret);
264 static efi_status_t file_close(struct file_handle *fh)
266 fs_closedir(fh->dirs);
271 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
273 struct file_handle *fh = to_fh(file);
274 EFI_ENTRY("%p", file);
275 return EFI_EXIT(file_close(fh));
278 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
280 struct file_handle *fh = to_fh(file);
281 efi_status_t ret = EFI_SUCCESS;
283 EFI_ENTRY("%p", file);
285 if (set_blk_dev(fh)) {
286 ret = EFI_DEVICE_ERROR;
290 if (fs_unlink(fh->path))
291 ret = EFI_DEVICE_ERROR;
295 return EFI_EXIT(ret);
298 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
303 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
304 *buffer_size, &actread))
305 return EFI_DEVICE_ERROR;
307 *buffer_size = actread;
308 fh->offset += actread;
313 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
316 struct efi_file_info *info = buffer;
317 struct fs_dirent *dent;
318 unsigned int required_size;
321 assert(fh->offset == 0);
322 fh->dirs = fs_opendir(fh->path);
324 return EFI_DEVICE_ERROR;
328 * So this is a bit awkward. Since fs layer is stateful and we
329 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
330 * we might have to return without consuming the dent.. so we
331 * have to stash it for next call.
337 dent = fs_readdir(fh->dirs);
342 /* no more files in directory: */
343 /* workaround shim.efi bug/quirk.. as find_boot_csv()
344 * loops through directory contents, it initially calls
345 * read w/ zero length buffer to find out how much mem
346 * to allocate for the EFI_FILE_INFO, then allocates,
347 * and then calls a 2nd time. If we return size of
348 * zero the first time, it happily passes that to
349 * AllocateZeroPool(), and when that returns NULL it
350 * thinks it is EFI_OUT_OF_RESOURCES. So on first
351 * call return a non-zero size:
353 if (*buffer_size == 0)
354 *buffer_size = sizeof(*info);
360 /* check buffer size: */
361 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
362 if (*buffer_size < required_size) {
363 *buffer_size = required_size;
365 return EFI_BUFFER_TOO_SMALL;
368 *buffer_size = required_size;
369 memset(info, 0, required_size);
371 info->size = required_size;
372 info->file_size = dent->size;
373 info->physical_size = dent->size;
375 if (dent->type == FS_DT_DIR)
376 info->attribute |= EFI_FILE_DIRECTORY;
378 ascii2unicode(info->file_name, dent->name);
385 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
386 efi_uintn_t *buffer_size, void *buffer)
388 struct file_handle *fh = to_fh(file);
389 efi_status_t ret = EFI_SUCCESS;
392 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
394 if (!buffer_size || !buffer) {
395 ret = EFI_INVALID_PARAMETER;
399 if (set_blk_dev(fh)) {
400 ret = EFI_DEVICE_ERROR;
406 ret = dir_read(fh, &bs, buffer);
408 ret = file_read(fh, &bs, buffer);
412 *buffer_size = SIZE_MAX;
415 return EFI_EXIT(ret);
418 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
419 efi_uintn_t *buffer_size,
422 struct file_handle *fh = to_fh(file);
423 efi_status_t ret = EFI_SUCCESS;
426 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
428 if (set_blk_dev(fh)) {
429 ret = EFI_DEVICE_ERROR;
433 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
435 ret = EFI_DEVICE_ERROR;
439 *buffer_size = actwrite;
440 fh->offset += actwrite;
443 return EFI_EXIT(ret);
447 * efi_file_getpos() - get current position in file
449 * This function implements the GetPosition service of the EFI file protocol.
450 * See the UEFI spec for details.
453 * @pos: pointer to file position
454 * Return: status code
456 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
459 efi_status_t ret = EFI_SUCCESS;
460 struct file_handle *fh = to_fh(file);
462 EFI_ENTRY("%p, %p", file, pos);
465 ret = EFI_UNSUPPORTED;
471 return EFI_EXIT(ret);
475 * efi_file_setpos() - set current position in file
477 * This function implements the SetPosition service of the EFI file protocol.
478 * See the UEFI spec for details.
481 * @pos: new file position
482 * Return: status code
484 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
487 struct file_handle *fh = to_fh(file);
488 efi_status_t ret = EFI_SUCCESS;
490 EFI_ENTRY("%p, %llu", file, pos);
494 ret = EFI_UNSUPPORTED;
497 fs_closedir(fh->dirs);
504 if (set_blk_dev(fh)) {
505 ret = EFI_DEVICE_ERROR;
509 if (fs_size(fh->path, &file_size)) {
510 ret = EFI_DEVICE_ERROR;
520 return EFI_EXIT(ret);
523 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
524 const efi_guid_t *info_type,
525 efi_uintn_t *buffer_size,
528 struct file_handle *fh = to_fh(file);
529 efi_status_t ret = EFI_SUCCESS;
531 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
533 if (!guidcmp(info_type, &efi_file_info_guid)) {
534 struct efi_file_info *info = buffer;
535 char *filename = basename(fh);
536 unsigned int required_size;
539 /* check buffer size: */
540 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
541 if (*buffer_size < required_size) {
542 *buffer_size = required_size;
543 ret = EFI_BUFFER_TOO_SMALL;
547 if (set_blk_dev(fh)) {
548 ret = EFI_DEVICE_ERROR;
552 if (fs_size(fh->path, &file_size)) {
553 ret = EFI_DEVICE_ERROR;
557 memset(info, 0, required_size);
559 info->size = required_size;
560 info->file_size = file_size;
561 info->physical_size = file_size;
564 info->attribute |= EFI_FILE_DIRECTORY;
566 ascii2unicode(info->file_name, filename);
567 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
568 struct efi_file_system_info *info = buffer;
569 disk_partition_t part;
570 efi_uintn_t required_size;
573 if (fh->fs->part >= 1)
574 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
576 r = part_get_info_whole_disk(fh->fs->desc, &part);
578 ret = EFI_DEVICE_ERROR;
581 required_size = sizeof(info) + 2 *
582 (strlen((const char *)part.name) + 1);
583 if (*buffer_size < required_size) {
584 *buffer_size = required_size;
585 ret = EFI_BUFFER_TOO_SMALL;
589 memset(info, 0, required_size);
591 info->size = required_size;
592 info->read_only = true;
593 info->volume_size = part.size * part.blksz;
594 info->free_space = 0;
595 info->block_size = part.blksz;
597 * TODO: The volume label is not available in U-Boot.
598 * Use the partition name as substitute.
600 ascii2unicode((u16 *)info->volume_label,
601 (const char *)part.name);
603 ret = EFI_UNSUPPORTED;
607 return EFI_EXIT(ret);
610 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
611 const efi_guid_t *info_type,
612 efi_uintn_t buffer_size,
615 EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
617 return EFI_EXIT(EFI_UNSUPPORTED);
620 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
622 EFI_ENTRY("%p", file);
623 return EFI_EXIT(EFI_SUCCESS);
626 static const struct efi_file_handle efi_file_handle_protocol = {
627 .rev = EFI_FILE_PROTOCOL_REVISION,
628 .open = efi_file_open,
629 .close = efi_file_close,
630 .delete = efi_file_delete,
631 .read = efi_file_read,
632 .write = efi_file_write,
633 .getpos = efi_file_getpos,
634 .setpos = efi_file_setpos,
635 .getinfo = efi_file_getinfo,
636 .setinfo = efi_file_setinfo,
637 .flush = efi_file_flush,
640 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
642 struct efi_simple_file_system_protocol *v;
643 struct efi_file_handle *f;
646 v = efi_fs_from_path(fp);
650 EFI_CALL(ret = v->open_volume(v, &f));
651 if (ret != EFI_SUCCESS)
654 /* skip over device-path nodes before the file path: */
655 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
656 fp = efi_dp_next(fp);
659 struct efi_device_path_file_path *fdp =
660 container_of(fp, struct efi_device_path_file_path, dp);
661 struct efi_file_handle *f2;
663 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
664 printf("bad file path!\n");
669 EFI_CALL(ret = f->open(f, &f2, fdp->str,
670 EFI_FILE_MODE_READ, 0));
671 if (ret != EFI_SUCCESS)
674 fp = efi_dp_next(fp);
676 EFI_CALL(f->close(f));
683 static efi_status_t EFIAPI
684 efi_open_volume(struct efi_simple_file_system_protocol *this,
685 struct efi_file_handle **root)
687 struct file_system *fs = to_fs(this);
689 EFI_ENTRY("%p, %p", this, root);
691 *root = file_open(fs, NULL, NULL, 0, 0);
693 return EFI_EXIT(EFI_SUCCESS);
696 struct efi_simple_file_system_protocol *
697 efi_simple_file_system(struct blk_desc *desc, int part,
698 struct efi_device_path *dp)
700 struct file_system *fs;
702 fs = calloc(1, sizeof(*fs));
703 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
704 fs->base.open_volume = efi_open_volume;