4 * Copyright (c) 2017 Rob Clark
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <efi_loader.h>
16 struct efi_simple_file_system_protocol base;
17 struct efi_device_path *dp;
18 struct blk_desc *desc;
21 #define to_fs(x) container_of(x, struct file_system, base)
24 struct efi_file_handle base;
25 struct file_system *fs;
26 loff_t offset; /* current file position/cursor */
29 /* for reading a directory: */
30 struct fs_dir_stream *dirs;
31 struct fs_dirent *dent;
35 #define to_fh(x) container_of(x, struct file_handle, base)
37 static const struct efi_file_handle efi_file_handle_protocol;
39 static char *basename(struct file_handle *fh)
41 char *s = strrchr(fh->path, '/');
47 static int set_blk_dev(struct file_handle *fh)
49 return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
52 static int is_dir(struct file_handle *fh)
54 struct fs_dir_stream *dirs;
57 dirs = fs_opendir(fh->path);
67 * Normalize a path which may include either back or fwd slashes,
68 * double slashes, . or .. entries in the path, etc.
70 static int sanitize_path(char *path)
74 /* backslash to slash: */
76 while ((p = strchr(p, '\\')))
79 /* handle double-slashes: */
81 while ((p = strstr(p, "//"))) {
83 memmove(p, src, strlen(src) + 1);
86 /* handle extra /.'s */
88 while ((p = strstr(p, "/."))) {
90 * You'd be tempted to do this *after* handling ".."s
91 * below to avoid having to check if "/." is start of
92 * a "/..", but that won't have the correct results..
93 * for example, "/foo/./../bar" would get resolved to
94 * "/foo/bar" if you did these two passes in the other
102 memmove(p, src, strlen(src) + 1);
105 /* handle extra /..'s: */
107 while ((p = strstr(p, "/.."))) {
112 /* find beginning of previous path entry: */
121 memmove(p, src, strlen(src) + 1);
127 /* NOTE: despite what you would expect, 'file_name' is actually a path.
128 * With windoze style backlashes, ofc.
130 static struct efi_file_handle *file_open(struct file_system *fs,
131 struct file_handle *parent, s16 *file_name, u64 mode)
133 struct file_handle *fh;
134 char f0[MAX_UTF8_PER_UTF16] = {0};
139 utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1);
140 flen = utf16_strlen((u16 *)file_name);
143 /* we could have a parent, but also an absolute path: */
147 plen = strlen(parent->path) + 1;
150 /* +2 is for null and '/' */
151 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
153 fh->base = efi_file_handle_protocol;
160 strcpy(p, parent->path);
165 utf16_to_utf8((u8 *)p, (u16 *)file_name, flen);
167 if (sanitize_path(fh->path))
170 /* check if file exists: */
174 if (!((mode & EFI_FILE_MODE_CREATE) || fs_exists(fh->path)))
177 /* figure out if file is a directory: */
178 fh->isdir = is_dir(fh);
181 strcpy(fh->path, "");
191 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
192 struct efi_file_handle **new_handle,
193 s16 *file_name, u64 open_mode, u64 attributes)
195 struct file_handle *fh = to_fh(file);
197 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name,
198 open_mode, attributes);
200 *new_handle = file_open(fh->fs, fh, file_name, open_mode);
202 return EFI_EXIT(EFI_NOT_FOUND);
204 return EFI_EXIT(EFI_SUCCESS);
207 static efi_status_t file_close(struct file_handle *fh)
209 fs_closedir(fh->dirs);
214 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
216 struct file_handle *fh = to_fh(file);
217 EFI_ENTRY("%p", file);
218 return EFI_EXIT(file_close(fh));
221 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
223 struct file_handle *fh = to_fh(file);
224 EFI_ENTRY("%p", file);
226 return EFI_EXIT(EFI_WARN_DELETE_FAILURE);
229 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
234 if (fs_read(fh->path, (ulong)buffer, fh->offset,
235 *buffer_size, &actread))
236 return EFI_DEVICE_ERROR;
238 *buffer_size = actread;
239 fh->offset += actread;
244 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
247 struct efi_file_info *info = buffer;
248 struct fs_dirent *dent;
249 unsigned int required_size;
252 assert(fh->offset == 0);
253 fh->dirs = fs_opendir(fh->path);
255 return EFI_DEVICE_ERROR;
259 * So this is a bit awkward. Since fs layer is stateful and we
260 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
261 * we might have to return without consuming the dent.. so we
262 * have to stash it for next call.
268 dent = fs_readdir(fh->dirs);
273 /* no more files in directory: */
274 /* workaround shim.efi bug/quirk.. as find_boot_csv()
275 * loops through directory contents, it initially calls
276 * read w/ zero length buffer to find out how much mem
277 * to allocate for the EFI_FILE_INFO, then allocates,
278 * and then calls a 2nd time. If we return size of
279 * zero the first time, it happily passes that to
280 * AllocateZeroPool(), and when that returns NULL it
281 * thinks it is EFI_OUT_OF_RESOURCES. So on first
282 * call return a non-zero size:
284 if (*buffer_size == 0)
285 *buffer_size = sizeof(*info);
291 /* check buffer size: */
292 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
293 if (*buffer_size < required_size) {
294 *buffer_size = required_size;
296 return EFI_BUFFER_TOO_SMALL;
299 *buffer_size = required_size;
300 memset(info, 0, required_size);
302 info->size = required_size;
303 info->file_size = dent->size;
304 info->physical_size = dent->size;
306 if (dent->type == FS_DT_DIR)
307 info->attribute |= EFI_FILE_DIRECTORY;
309 ascii2unicode((u16 *)info->file_name, dent->name);
316 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
317 efi_uintn_t *buffer_size, void *buffer)
319 struct file_handle *fh = to_fh(file);
320 efi_status_t ret = EFI_SUCCESS;
323 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
325 if (!buffer_size || !buffer) {
326 ret = EFI_INVALID_PARAMETER;
330 if (set_blk_dev(fh)) {
331 ret = EFI_DEVICE_ERROR;
337 ret = dir_read(fh, &bs, buffer);
339 ret = file_read(fh, &bs, buffer);
343 *buffer_size = SIZE_MAX;
346 return EFI_EXIT(ret);
349 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
350 efi_uintn_t *buffer_size,
353 struct file_handle *fh = to_fh(file);
354 efi_status_t ret = EFI_SUCCESS;
357 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
359 if (set_blk_dev(fh)) {
360 ret = EFI_DEVICE_ERROR;
364 if (fs_write(fh->path, (ulong)buffer, fh->offset, *buffer_size,
366 ret = EFI_DEVICE_ERROR;
370 *buffer_size = actwrite;
371 fh->offset += actwrite;
374 return EFI_EXIT(ret);
377 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
380 struct file_handle *fh = to_fh(file);
382 EFI_ENTRY("%p, %p", file, pos);
384 if (fh->offset <= SIZE_MAX) {
386 return EFI_EXIT(EFI_SUCCESS);
388 return EFI_EXIT(EFI_DEVICE_ERROR);
392 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
395 struct file_handle *fh = to_fh(file);
396 efi_status_t ret = EFI_SUCCESS;
398 EFI_ENTRY("%p, %zu", file, pos);
402 ret = EFI_UNSUPPORTED;
405 fs_closedir(fh->dirs);
412 if (set_blk_dev(fh)) {
413 ret = EFI_DEVICE_ERROR;
417 if (fs_size(fh->path, &file_size)) {
418 ret = EFI_DEVICE_ERROR;
428 return EFI_EXIT(ret);
431 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
432 const efi_guid_t *info_type,
433 efi_uintn_t *buffer_size,
436 struct file_handle *fh = to_fh(file);
437 efi_status_t ret = EFI_SUCCESS;
439 EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer);
441 if (!guidcmp(info_type, &efi_file_info_guid)) {
442 struct efi_file_info *info = buffer;
443 char *filename = basename(fh);
444 unsigned int required_size;
447 /* check buffer size: */
448 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
449 if (*buffer_size < required_size) {
450 *buffer_size = required_size;
451 ret = EFI_BUFFER_TOO_SMALL;
455 if (set_blk_dev(fh)) {
456 ret = EFI_DEVICE_ERROR;
460 if (fs_size(fh->path, &file_size)) {
461 ret = EFI_DEVICE_ERROR;
465 memset(info, 0, required_size);
467 info->size = required_size;
468 info->file_size = file_size;
469 info->physical_size = file_size;
472 info->attribute |= EFI_FILE_DIRECTORY;
474 ascii2unicode((u16 *)info->file_name, filename);
476 ret = EFI_UNSUPPORTED;
480 return EFI_EXIT(ret);
483 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
484 const efi_guid_t *info_type,
485 efi_uintn_t buffer_size,
488 EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
490 return EFI_EXIT(EFI_UNSUPPORTED);
493 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
495 EFI_ENTRY("%p", file);
496 return EFI_EXIT(EFI_SUCCESS);
499 static const struct efi_file_handle efi_file_handle_protocol = {
500 .rev = EFI_FILE_PROTOCOL_REVISION,
501 .open = efi_file_open,
502 .close = efi_file_close,
503 .delete = efi_file_delete,
504 .read = efi_file_read,
505 .write = efi_file_write,
506 .getpos = efi_file_getpos,
507 .setpos = efi_file_setpos,
508 .getinfo = efi_file_getinfo,
509 .setinfo = efi_file_setinfo,
510 .flush = efi_file_flush,
513 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
515 struct efi_simple_file_system_protocol *v;
516 struct efi_file_handle *f;
519 v = efi_fs_from_path(fp);
523 EFI_CALL(ret = v->open_volume(v, &f));
524 if (ret != EFI_SUCCESS)
527 /* skip over device-path nodes before the file path: */
528 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
529 fp = efi_dp_next(fp);
532 struct efi_device_path_file_path *fdp =
533 container_of(fp, struct efi_device_path_file_path, dp);
534 struct efi_file_handle *f2;
536 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
537 printf("bad file path!\n");
542 EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
543 EFI_FILE_MODE_READ, 0));
544 if (ret != EFI_SUCCESS)
547 fp = efi_dp_next(fp);
549 EFI_CALL(f->close(f));
556 static efi_status_t EFIAPI
557 efi_open_volume(struct efi_simple_file_system_protocol *this,
558 struct efi_file_handle **root)
560 struct file_system *fs = to_fs(this);
562 EFI_ENTRY("%p, %p", this, root);
564 *root = file_open(fs, NULL, NULL, 0);
566 return EFI_EXIT(EFI_SUCCESS);
569 struct efi_simple_file_system_protocol *
570 efi_simple_file_system(struct blk_desc *desc, int part,
571 struct efi_device_path *dp)
573 struct file_system *fs;
575 fs = calloc(1, sizeof(*fs));
576 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
577 fs->base.open_volume = efi_open_volume;