1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
10 #include <efi_loader.h>
14 /* GUID for file system information */
15 const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
18 struct efi_simple_file_system_protocol base;
19 struct efi_device_path *dp;
20 struct blk_desc *desc;
23 #define to_fs(x) container_of(x, struct file_system, base)
26 struct efi_file_handle base;
27 struct file_system *fs;
28 loff_t offset; /* current file position/cursor */
31 /* for reading a directory: */
32 struct fs_dir_stream *dirs;
33 struct fs_dirent *dent;
37 #define to_fh(x) container_of(x, struct file_handle, base)
39 static const struct efi_file_handle efi_file_handle_protocol;
41 static char *basename(struct file_handle *fh)
43 char *s = strrchr(fh->path, '/');
49 static int set_blk_dev(struct file_handle *fh)
51 return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
54 static int is_dir(struct file_handle *fh)
56 struct fs_dir_stream *dirs;
59 dirs = fs_opendir(fh->path);
69 * Normalize a path which may include either back or fwd slashes,
70 * double slashes, . or .. entries in the path, etc.
72 static int sanitize_path(char *path)
76 /* backslash to slash: */
78 while ((p = strchr(p, '\\')))
81 /* handle double-slashes: */
83 while ((p = strstr(p, "//"))) {
85 memmove(p, src, strlen(src) + 1);
88 /* handle extra /.'s */
90 while ((p = strstr(p, "/."))) {
92 * You'd be tempted to do this *after* handling ".."s
93 * below to avoid having to check if "/." is start of
94 * a "/..", but that won't have the correct results..
95 * for example, "/foo/./../bar" would get resolved to
96 * "/foo/bar" if you did these two passes in the other
104 memmove(p, src, strlen(src) + 1);
107 /* handle extra /..'s: */
109 while ((p = strstr(p, "/.."))) {
114 /* find beginning of previous path entry: */
123 memmove(p, src, strlen(src) + 1);
129 /* NOTE: despite what you would expect, 'file_name' is actually a path.
130 * With windoze style backlashes, ofc.
132 static struct efi_file_handle *file_open(struct file_system *fs,
133 struct file_handle *parent, s16 *file_name, u64 mode)
135 struct file_handle *fh;
136 char f0[MAX_UTF8_PER_UTF16] = {0};
141 utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1);
142 flen = utf16_strlen((u16 *)file_name);
145 /* we could have a parent, but also an absolute path: */
149 plen = strlen(parent->path) + 1;
152 /* +2 is for null and '/' */
153 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
155 fh->base = efi_file_handle_protocol;
162 strcpy(p, parent->path);
167 utf16_to_utf8((u8 *)p, (u16 *)file_name, flen);
169 if (sanitize_path(fh->path))
172 /* check if file exists: */
176 if (!((mode & EFI_FILE_MODE_CREATE) || fs_exists(fh->path)))
179 /* figure out if file is a directory: */
180 fh->isdir = is_dir(fh);
183 strcpy(fh->path, "");
193 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
194 struct efi_file_handle **new_handle,
195 s16 *file_name, u64 open_mode, u64 attributes)
197 struct file_handle *fh = to_fh(file);
199 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name,
200 open_mode, attributes);
202 *new_handle = file_open(fh->fs, fh, file_name, open_mode);
204 return EFI_EXIT(EFI_NOT_FOUND);
206 return EFI_EXIT(EFI_SUCCESS);
209 static efi_status_t file_close(struct file_handle *fh)
211 fs_closedir(fh->dirs);
216 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
218 struct file_handle *fh = to_fh(file);
219 EFI_ENTRY("%p", file);
220 return EFI_EXIT(file_close(fh));
223 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
225 struct file_handle *fh = to_fh(file);
226 EFI_ENTRY("%p", file);
228 return EFI_EXIT(EFI_WARN_DELETE_FAILURE);
231 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
236 if (fs_read(fh->path, (ulong)buffer, fh->offset,
237 *buffer_size, &actread))
238 return EFI_DEVICE_ERROR;
240 *buffer_size = actread;
241 fh->offset += actread;
246 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
249 struct efi_file_info *info = buffer;
250 struct fs_dirent *dent;
251 unsigned int required_size;
254 assert(fh->offset == 0);
255 fh->dirs = fs_opendir(fh->path);
257 return EFI_DEVICE_ERROR;
261 * So this is a bit awkward. Since fs layer is stateful and we
262 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
263 * we might have to return without consuming the dent.. so we
264 * have to stash it for next call.
270 dent = fs_readdir(fh->dirs);
275 /* no more files in directory: */
276 /* workaround shim.efi bug/quirk.. as find_boot_csv()
277 * loops through directory contents, it initially calls
278 * read w/ zero length buffer to find out how much mem
279 * to allocate for the EFI_FILE_INFO, then allocates,
280 * and then calls a 2nd time. If we return size of
281 * zero the first time, it happily passes that to
282 * AllocateZeroPool(), and when that returns NULL it
283 * thinks it is EFI_OUT_OF_RESOURCES. So on first
284 * call return a non-zero size:
286 if (*buffer_size == 0)
287 *buffer_size = sizeof(*info);
293 /* check buffer size: */
294 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
295 if (*buffer_size < required_size) {
296 *buffer_size = required_size;
298 return EFI_BUFFER_TOO_SMALL;
301 *buffer_size = required_size;
302 memset(info, 0, required_size);
304 info->size = required_size;
305 info->file_size = dent->size;
306 info->physical_size = dent->size;
308 if (dent->type == FS_DT_DIR)
309 info->attribute |= EFI_FILE_DIRECTORY;
311 ascii2unicode((u16 *)info->file_name, dent->name);
318 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
319 efi_uintn_t *buffer_size, void *buffer)
321 struct file_handle *fh = to_fh(file);
322 efi_status_t ret = EFI_SUCCESS;
325 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
327 if (!buffer_size || !buffer) {
328 ret = EFI_INVALID_PARAMETER;
332 if (set_blk_dev(fh)) {
333 ret = EFI_DEVICE_ERROR;
339 ret = dir_read(fh, &bs, buffer);
341 ret = file_read(fh, &bs, buffer);
345 *buffer_size = SIZE_MAX;
348 return EFI_EXIT(ret);
351 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
352 efi_uintn_t *buffer_size,
355 struct file_handle *fh = to_fh(file);
356 efi_status_t ret = EFI_SUCCESS;
359 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
361 if (set_blk_dev(fh)) {
362 ret = EFI_DEVICE_ERROR;
366 if (fs_write(fh->path, (ulong)buffer, fh->offset, *buffer_size,
368 ret = EFI_DEVICE_ERROR;
372 *buffer_size = actwrite;
373 fh->offset += actwrite;
376 return EFI_EXIT(ret);
379 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
382 struct file_handle *fh = to_fh(file);
384 EFI_ENTRY("%p, %p", file, pos);
386 if (fh->offset <= SIZE_MAX) {
388 return EFI_EXIT(EFI_SUCCESS);
390 return EFI_EXIT(EFI_DEVICE_ERROR);
394 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
397 struct file_handle *fh = to_fh(file);
398 efi_status_t ret = EFI_SUCCESS;
400 EFI_ENTRY("%p, %zu", file, pos);
404 ret = EFI_UNSUPPORTED;
407 fs_closedir(fh->dirs);
414 if (set_blk_dev(fh)) {
415 ret = EFI_DEVICE_ERROR;
419 if (fs_size(fh->path, &file_size)) {
420 ret = EFI_DEVICE_ERROR;
430 return EFI_EXIT(ret);
433 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
434 const efi_guid_t *info_type,
435 efi_uintn_t *buffer_size,
438 struct file_handle *fh = to_fh(file);
439 efi_status_t ret = EFI_SUCCESS;
441 EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer);
443 if (!guidcmp(info_type, &efi_file_info_guid)) {
444 struct efi_file_info *info = buffer;
445 char *filename = basename(fh);
446 unsigned int required_size;
449 /* check buffer size: */
450 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
451 if (*buffer_size < required_size) {
452 *buffer_size = required_size;
453 ret = EFI_BUFFER_TOO_SMALL;
457 if (set_blk_dev(fh)) {
458 ret = EFI_DEVICE_ERROR;
462 if (fs_size(fh->path, &file_size)) {
463 ret = EFI_DEVICE_ERROR;
467 memset(info, 0, required_size);
469 info->size = required_size;
470 info->file_size = file_size;
471 info->physical_size = file_size;
474 info->attribute |= EFI_FILE_DIRECTORY;
476 ascii2unicode((u16 *)info->file_name, filename);
477 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
478 struct efi_file_system_info *info = buffer;
479 disk_partition_t part;
480 efi_uintn_t required_size;
483 if (fh->fs->part >= 1)
484 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
486 r = part_get_info_whole_disk(fh->fs->desc, &part);
488 ret = EFI_DEVICE_ERROR;
491 required_size = sizeof(info) + 2 *
492 (strlen((const char *)part.name) + 1);
493 if (*buffer_size < required_size) {
494 *buffer_size = required_size;
495 ret = EFI_BUFFER_TOO_SMALL;
499 memset(info, 0, required_size);
501 info->size = required_size;
502 info->read_only = true;
503 info->volume_size = part.size * part.blksz;
504 info->free_space = 0;
505 info->block_size = part.blksz;
507 * TODO: The volume label is not available in U-Boot.
508 * Use the partition name as substitute.
510 ascii2unicode((u16 *)info->volume_label,
511 (const char *)part.name);
513 ret = EFI_UNSUPPORTED;
517 return EFI_EXIT(ret);
520 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
521 const efi_guid_t *info_type,
522 efi_uintn_t buffer_size,
525 EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
527 return EFI_EXIT(EFI_UNSUPPORTED);
530 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
532 EFI_ENTRY("%p", file);
533 return EFI_EXIT(EFI_SUCCESS);
536 static const struct efi_file_handle efi_file_handle_protocol = {
537 .rev = EFI_FILE_PROTOCOL_REVISION,
538 .open = efi_file_open,
539 .close = efi_file_close,
540 .delete = efi_file_delete,
541 .read = efi_file_read,
542 .write = efi_file_write,
543 .getpos = efi_file_getpos,
544 .setpos = efi_file_setpos,
545 .getinfo = efi_file_getinfo,
546 .setinfo = efi_file_setinfo,
547 .flush = efi_file_flush,
550 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
552 struct efi_simple_file_system_protocol *v;
553 struct efi_file_handle *f;
556 v = efi_fs_from_path(fp);
560 EFI_CALL(ret = v->open_volume(v, &f));
561 if (ret != EFI_SUCCESS)
564 /* skip over device-path nodes before the file path: */
565 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
566 fp = efi_dp_next(fp);
569 struct efi_device_path_file_path *fdp =
570 container_of(fp, struct efi_device_path_file_path, dp);
571 struct efi_file_handle *f2;
573 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
574 printf("bad file path!\n");
579 EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
580 EFI_FILE_MODE_READ, 0));
581 if (ret != EFI_SUCCESS)
584 fp = efi_dp_next(fp);
586 EFI_CALL(f->close(f));
593 static efi_status_t EFIAPI
594 efi_open_volume(struct efi_simple_file_system_protocol *this,
595 struct efi_file_handle **root)
597 struct file_system *fs = to_fs(this);
599 EFI_ENTRY("%p, %p", this, root);
601 *root = file_open(fs, NULL, NULL, 0);
603 return EFI_EXIT(EFI_SUCCESS);
606 struct efi_simple_file_system_protocol *
607 efi_simple_file_system(struct blk_desc *desc, int part,
608 struct efi_device_path *dp)
610 struct file_system *fs;
612 fs = calloc(1, sizeof(*fs));
613 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
614 fs->base.open_volume = efi_open_volume;