4 * Copyright (c) 2017 Rob Clark
6 * SPDX-License-Identifier: GPL-2.0+
11 #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)
136 struct file_handle *fh;
137 char f0[MAX_UTF8_PER_UTF16] = {0};
142 utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1);
143 flen = utf16_strlen((u16 *)file_name);
146 /* we could have a parent, but also an absolute path: */
150 plen = strlen(parent->path) + 1;
153 /* +2 is for null and '/' */
154 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
156 fh->base = efi_file_handle_protocol;
163 strcpy(p, parent->path);
168 utf16_to_utf8((u8 *)p, (u16 *)file_name, flen);
170 if (sanitize_path(fh->path))
173 /* check if file exists: */
177 if (!((mode & EFI_FILE_MODE_CREATE) || fs_exists(fh->path)))
180 /* figure out if file is a directory: */
181 fh->isdir = is_dir(fh);
184 strcpy(fh->path, "");
194 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
195 struct efi_file_handle **new_handle,
196 s16 *file_name, u64 open_mode, u64 attributes)
198 struct file_handle *fh = to_fh(file);
200 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name,
201 open_mode, attributes);
203 *new_handle = file_open(fh->fs, fh, file_name, open_mode);
205 return EFI_EXIT(EFI_NOT_FOUND);
207 return EFI_EXIT(EFI_SUCCESS);
210 static efi_status_t file_close(struct file_handle *fh)
212 fs_closedir(fh->dirs);
217 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
219 struct file_handle *fh = to_fh(file);
220 EFI_ENTRY("%p", file);
221 return EFI_EXIT(file_close(fh));
224 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
226 struct file_handle *fh = to_fh(file);
227 EFI_ENTRY("%p", file);
229 return EFI_EXIT(EFI_WARN_DELETE_FAILURE);
232 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
237 if (fs_read(fh->path, (ulong)buffer, fh->offset,
238 *buffer_size, &actread))
239 return EFI_DEVICE_ERROR;
241 *buffer_size = actread;
242 fh->offset += actread;
247 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
250 struct efi_file_info *info = buffer;
251 struct fs_dirent *dent;
252 unsigned int required_size;
255 assert(fh->offset == 0);
256 fh->dirs = fs_opendir(fh->path);
258 return EFI_DEVICE_ERROR;
262 * So this is a bit awkward. Since fs layer is stateful and we
263 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
264 * we might have to return without consuming the dent.. so we
265 * have to stash it for next call.
271 dent = fs_readdir(fh->dirs);
276 /* no more files in directory: */
277 /* workaround shim.efi bug/quirk.. as find_boot_csv()
278 * loops through directory contents, it initially calls
279 * read w/ zero length buffer to find out how much mem
280 * to allocate for the EFI_FILE_INFO, then allocates,
281 * and then calls a 2nd time. If we return size of
282 * zero the first time, it happily passes that to
283 * AllocateZeroPool(), and when that returns NULL it
284 * thinks it is EFI_OUT_OF_RESOURCES. So on first
285 * call return a non-zero size:
287 if (*buffer_size == 0)
288 *buffer_size = sizeof(*info);
294 /* check buffer size: */
295 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
296 if (*buffer_size < required_size) {
297 *buffer_size = required_size;
299 return EFI_BUFFER_TOO_SMALL;
302 *buffer_size = required_size;
303 memset(info, 0, required_size);
305 info->size = required_size;
306 info->file_size = dent->size;
307 info->physical_size = dent->size;
309 if (dent->type == FS_DT_DIR)
310 info->attribute |= EFI_FILE_DIRECTORY;
312 ascii2unicode((u16 *)info->file_name, dent->name);
319 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
320 efi_uintn_t *buffer_size, void *buffer)
322 struct file_handle *fh = to_fh(file);
323 efi_status_t ret = EFI_SUCCESS;
326 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
328 if (!buffer_size || !buffer) {
329 ret = EFI_INVALID_PARAMETER;
333 if (set_blk_dev(fh)) {
334 ret = EFI_DEVICE_ERROR;
340 ret = dir_read(fh, &bs, buffer);
342 ret = file_read(fh, &bs, buffer);
346 *buffer_size = SIZE_MAX;
349 return EFI_EXIT(ret);
352 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
353 efi_uintn_t *buffer_size,
356 struct file_handle *fh = to_fh(file);
357 efi_status_t ret = EFI_SUCCESS;
360 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
362 if (set_blk_dev(fh)) {
363 ret = EFI_DEVICE_ERROR;
367 if (fs_write(fh->path, (ulong)buffer, fh->offset, *buffer_size,
369 ret = EFI_DEVICE_ERROR;
373 *buffer_size = actwrite;
374 fh->offset += actwrite;
377 return EFI_EXIT(ret);
380 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
383 struct file_handle *fh = to_fh(file);
385 EFI_ENTRY("%p, %p", file, pos);
387 if (fh->offset <= SIZE_MAX) {
389 return EFI_EXIT(EFI_SUCCESS);
391 return EFI_EXIT(EFI_DEVICE_ERROR);
395 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
398 struct file_handle *fh = to_fh(file);
399 efi_status_t ret = EFI_SUCCESS;
401 EFI_ENTRY("%p, %zu", file, pos);
405 ret = EFI_UNSUPPORTED;
408 fs_closedir(fh->dirs);
415 if (set_blk_dev(fh)) {
416 ret = EFI_DEVICE_ERROR;
420 if (fs_size(fh->path, &file_size)) {
421 ret = EFI_DEVICE_ERROR;
431 return EFI_EXIT(ret);
434 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
435 const efi_guid_t *info_type,
436 efi_uintn_t *buffer_size,
439 struct file_handle *fh = to_fh(file);
440 efi_status_t ret = EFI_SUCCESS;
442 EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer);
444 if (!guidcmp(info_type, &efi_file_info_guid)) {
445 struct efi_file_info *info = buffer;
446 char *filename = basename(fh);
447 unsigned int required_size;
450 /* check buffer size: */
451 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
452 if (*buffer_size < required_size) {
453 *buffer_size = required_size;
454 ret = EFI_BUFFER_TOO_SMALL;
458 if (set_blk_dev(fh)) {
459 ret = EFI_DEVICE_ERROR;
463 if (fs_size(fh->path, &file_size)) {
464 ret = EFI_DEVICE_ERROR;
468 memset(info, 0, required_size);
470 info->size = required_size;
471 info->file_size = file_size;
472 info->physical_size = file_size;
475 info->attribute |= EFI_FILE_DIRECTORY;
477 ascii2unicode((u16 *)info->file_name, filename);
478 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
479 struct efi_file_system_info *info = buffer;
480 disk_partition_t part;
481 efi_uintn_t required_size;
484 if (fh->fs->part >= 1)
485 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
487 r = part_get_info_whole_disk(fh->fs->desc, &part);
489 ret = EFI_DEVICE_ERROR;
492 required_size = sizeof(info) + 2 *
493 (strlen((const char *)part.name) + 1);
494 if (*buffer_size < required_size) {
495 *buffer_size = required_size;
496 ret = EFI_BUFFER_TOO_SMALL;
500 memset(info, 0, required_size);
502 info->size = required_size;
503 info->read_only = true;
504 info->volume_size = part.size * part.blksz;
505 info->free_space = 0;
506 info->block_size = part.blksz;
508 * TODO: The volume label is not available in U-Boot.
509 * Use the partition name as substitute.
511 ascii2unicode((u16 *)info->volume_label,
512 (const char *)part.name);
514 ret = EFI_UNSUPPORTED;
518 return EFI_EXIT(ret);
521 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
522 const efi_guid_t *info_type,
523 efi_uintn_t buffer_size,
526 EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
528 return EFI_EXIT(EFI_UNSUPPORTED);
531 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
533 EFI_ENTRY("%p", file);
534 return EFI_EXIT(EFI_SUCCESS);
537 static const struct efi_file_handle efi_file_handle_protocol = {
538 .rev = EFI_FILE_PROTOCOL_REVISION,
539 .open = efi_file_open,
540 .close = efi_file_close,
541 .delete = efi_file_delete,
542 .read = efi_file_read,
543 .write = efi_file_write,
544 .getpos = efi_file_getpos,
545 .setpos = efi_file_setpos,
546 .getinfo = efi_file_getinfo,
547 .setinfo = efi_file_setinfo,
548 .flush = efi_file_flush,
551 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
553 struct efi_simple_file_system_protocol *v;
554 struct efi_file_handle *f;
557 v = efi_fs_from_path(fp);
561 EFI_CALL(ret = v->open_volume(v, &f));
562 if (ret != EFI_SUCCESS)
565 /* skip over device-path nodes before the file path: */
566 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
567 fp = efi_dp_next(fp);
570 struct efi_device_path_file_path *fdp =
571 container_of(fp, struct efi_device_path_file_path, dp);
572 struct efi_file_handle *f2;
574 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
575 printf("bad file path!\n");
580 EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
581 EFI_FILE_MODE_READ, 0));
582 if (ret != EFI_SUCCESS)
585 fp = efi_dp_next(fp);
587 EFI_CALL(f->close(f));
594 static efi_status_t EFIAPI
595 efi_open_volume(struct efi_simple_file_system_protocol *this,
596 struct efi_file_handle **root)
598 struct file_system *fs = to_fs(this);
600 EFI_ENTRY("%p, %p", this, root);
602 *root = file_open(fs, NULL, NULL, 0);
604 return EFI_EXIT(EFI_SUCCESS);
607 struct efi_simple_file_system_protocol *
608 efi_simple_file_system(struct blk_desc *desc, int part,
609 struct efi_device_path *dp)
611 struct file_system *fs;
613 fs = calloc(1, sizeof(*fs));
614 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
615 fs->base.open_volume = efi_open_volume;