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 u64 *buffer_size, void *buffer)
319 struct file_handle *fh = to_fh(file);
320 efi_status_t ret = EFI_SUCCESS;
322 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
324 if (set_blk_dev(fh)) {
325 ret = EFI_DEVICE_ERROR;
330 ret = dir_read(fh, buffer_size, buffer);
332 ret = file_read(fh, buffer_size, buffer);
335 return EFI_EXIT(ret);
338 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
339 u64 *buffer_size, void *buffer)
341 struct file_handle *fh = to_fh(file);
342 efi_status_t ret = EFI_SUCCESS;
345 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
347 if (set_blk_dev(fh)) {
348 ret = EFI_DEVICE_ERROR;
352 if (fs_write(fh->path, (ulong)buffer, fh->offset, *buffer_size,
354 ret = EFI_DEVICE_ERROR;
358 *buffer_size = actwrite;
359 fh->offset += actwrite;
362 return EFI_EXIT(ret);
365 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
368 struct file_handle *fh = to_fh(file);
369 EFI_ENTRY("%p, %p", file, pos);
371 return EFI_EXIT(EFI_SUCCESS);
374 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
377 struct file_handle *fh = to_fh(file);
378 efi_status_t ret = EFI_SUCCESS;
380 EFI_ENTRY("%p, %llu", file, pos);
384 ret = EFI_UNSUPPORTED;
387 fs_closedir(fh->dirs);
394 if (set_blk_dev(fh)) {
395 ret = EFI_DEVICE_ERROR;
399 if (fs_size(fh->path, &file_size)) {
400 ret = EFI_DEVICE_ERROR;
410 return EFI_EXIT(ret);
413 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
414 efi_guid_t *info_type, u64 *buffer_size, void *buffer)
416 struct file_handle *fh = to_fh(file);
417 efi_status_t ret = EFI_SUCCESS;
419 EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer);
421 if (!guidcmp(info_type, &efi_file_info_guid)) {
422 struct efi_file_info *info = buffer;
423 char *filename = basename(fh);
424 unsigned int required_size;
427 /* check buffer size: */
428 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
429 if (*buffer_size < required_size) {
430 *buffer_size = required_size;
431 ret = EFI_BUFFER_TOO_SMALL;
435 if (set_blk_dev(fh)) {
436 ret = EFI_DEVICE_ERROR;
440 if (fs_size(fh->path, &file_size)) {
441 ret = EFI_DEVICE_ERROR;
445 memset(info, 0, required_size);
447 info->size = required_size;
448 info->file_size = file_size;
449 info->physical_size = file_size;
452 info->attribute |= EFI_FILE_DIRECTORY;
454 ascii2unicode((u16 *)info->file_name, filename);
456 ret = EFI_UNSUPPORTED;
460 return EFI_EXIT(ret);
463 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
464 efi_guid_t *info_type, u64 buffer_size, void *buffer)
466 EFI_ENTRY("%p, %p, %llu, %p", file, info_type, buffer_size, buffer);
467 return EFI_EXIT(EFI_UNSUPPORTED);
470 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
472 EFI_ENTRY("%p", file);
473 return EFI_EXIT(EFI_SUCCESS);
476 static const struct efi_file_handle efi_file_handle_protocol = {
477 .rev = EFI_FILE_PROTOCOL_REVISION,
478 .open = efi_file_open,
479 .close = efi_file_close,
480 .delete = efi_file_delete,
481 .read = efi_file_read,
482 .write = efi_file_write,
483 .getpos = efi_file_getpos,
484 .setpos = efi_file_setpos,
485 .getinfo = efi_file_getinfo,
486 .setinfo = efi_file_setinfo,
487 .flush = efi_file_flush,
490 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
492 struct efi_simple_file_system_protocol *v;
493 struct efi_file_handle *f;
496 v = efi_fs_from_path(fp);
500 EFI_CALL(ret = v->open_volume(v, &f));
501 if (ret != EFI_SUCCESS)
504 /* skip over device-path nodes before the file path: */
505 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
506 fp = efi_dp_next(fp);
509 struct efi_device_path_file_path *fdp =
510 container_of(fp, struct efi_device_path_file_path, dp);
511 struct efi_file_handle *f2;
513 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
514 printf("bad file path!\n");
519 EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
520 EFI_FILE_MODE_READ, 0));
521 if (ret != EFI_SUCCESS)
524 fp = efi_dp_next(fp);
526 EFI_CALL(f->close(f));
533 static efi_status_t EFIAPI
534 efi_open_volume(struct efi_simple_file_system_protocol *this,
535 struct efi_file_handle **root)
537 struct file_system *fs = to_fs(this);
539 EFI_ENTRY("%p, %p", this, root);
541 *root = file_open(fs, NULL, NULL, 0);
543 return EFI_EXIT(EFI_SUCCESS);
546 struct efi_simple_file_system_protocol *
547 efi_simple_file_system(struct blk_desc *desc, int part,
548 struct efi_device_path *dp)
550 struct file_system *fs;
552 fs = calloc(1, sizeof(*fs));
553 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
554 fs->base.open_volume = efi_open_volume;