Merge branch 'master' of git://git.denx.de/u-boot-sunxi
[oweals/u-boot.git] / lib / efi_loader / efi_file.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI utils
4  *
5  *  Copyright (c) 2017 Rob Clark
6  */
7
8 #include <common.h>
9 #include <charset.h>
10 #include <efi_loader.h>
11 #include <malloc.h>
12 #include <mapmem.h>
13 #include <fs.h>
14
15 /* GUID for file system information */
16 const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
17
18 struct file_system {
19         struct efi_simple_file_system_protocol base;
20         struct efi_device_path *dp;
21         struct blk_desc *desc;
22         int part;
23 };
24 #define to_fs(x) container_of(x, struct file_system, base)
25
26 struct file_handle {
27         struct efi_file_handle base;
28         struct file_system *fs;
29         loff_t offset;       /* current file position/cursor */
30         int isdir;
31
32         /* for reading a directory: */
33         struct fs_dir_stream *dirs;
34         struct fs_dirent *dent;
35
36         char path[0];
37 };
38 #define to_fh(x) container_of(x, struct file_handle, base)
39
40 static const struct efi_file_handle efi_file_handle_protocol;
41
42 static char *basename(struct file_handle *fh)
43 {
44         char *s = strrchr(fh->path, '/');
45         if (s)
46                 return s + 1;
47         return fh->path;
48 }
49
50 static int set_blk_dev(struct file_handle *fh)
51 {
52         return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
53 }
54
55 /**
56  * is_dir() - check if file handle points to directory
57  *
58  * We assume that set_blk_dev(fh) has been called already.
59  *
60  * @fh:         file handle
61  * Return:      true if file handle points to a directory
62  */
63 static int is_dir(struct file_handle *fh)
64 {
65         struct fs_dir_stream *dirs;
66
67         dirs = fs_opendir(fh->path);
68         if (!dirs)
69                 return 0;
70
71         fs_closedir(dirs);
72
73         return 1;
74 }
75
76 /*
77  * Normalize a path which may include either back or fwd slashes,
78  * double slashes, . or .. entries in the path, etc.
79  */
80 static int sanitize_path(char *path)
81 {
82         char *p;
83
84         /* backslash to slash: */
85         p = path;
86         while ((p = strchr(p, '\\')))
87                 *p++ = '/';
88
89         /* handle double-slashes: */
90         p = path;
91         while ((p = strstr(p, "//"))) {
92                 char *src = p + 1;
93                 memmove(p, src, strlen(src) + 1);
94         }
95
96         /* handle extra /.'s */
97         p = path;
98         while ((p = strstr(p, "/."))) {
99                 /*
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
105                  * order
106                  */
107                 if (p[2] == '.') {
108                         p += 2;
109                         continue;
110                 }
111                 char *src = p + 2;
112                 memmove(p, src, strlen(src) + 1);
113         }
114
115         /* handle extra /..'s: */
116         p = path;
117         while ((p = strstr(p, "/.."))) {
118                 char *src = p + 3;
119
120                 p--;
121
122                 /* find beginning of previous path entry: */
123                 while (true) {
124                         if (p < path)
125                                 return -1;
126                         if (*p == '/')
127                                 break;
128                         p--;
129                 }
130
131                 memmove(p, src, strlen(src) + 1);
132         }
133
134         return 0;
135 }
136
137 /**
138  * efi_create_file() - create file or directory
139  *
140  * @fh:                 file handle
141  * @attributes:         attributes for newly created file
142  * Returns:             0 for success
143  */
144 static int efi_create_file(struct file_handle *fh, u64 attributes)
145 {
146         loff_t actwrite;
147         void *buffer = &actwrite;
148
149         if (attributes & EFI_FILE_DIRECTORY)
150                 return fs_mkdir(fh->path);
151         else
152                 return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
153                                 &actwrite);
154 }
155
156 /**
157  * file_open() - open a file handle
158  *
159  * @fs:                 file system
160  * @parent:             directory relative to which the file is to be opened
161  * @file_name:          path of the file to be opened. '\', '.', or '..' may
162  *                      be used as modifiers. A leading backslash indicates an
163  *                      absolute path.
164  * @mode:               bit mask indicating the access mode (read, write,
165  *                      create)
166  * @attributes:         attributes for newly created file
167  * Returns:             handle to the opened file or NULL
168  */
169 static struct efi_file_handle *file_open(struct file_system *fs,
170                 struct file_handle *parent, u16 *file_name, u64 mode,
171                 u64 attributes)
172 {
173         struct file_handle *fh;
174         char f0[MAX_UTF8_PER_UTF16] = {0};
175         int plen = 0;
176         int flen = 0;
177
178         if (file_name) {
179                 utf16_to_utf8((u8 *)f0, file_name, 1);
180                 flen = u16_strlen(file_name);
181         }
182
183         /* we could have a parent, but also an absolute path: */
184         if (f0[0] == '\\') {
185                 plen = 0;
186         } else if (parent) {
187                 plen = strlen(parent->path) + 1;
188         }
189
190         /* +2 is for null and '/' */
191         fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
192
193         fh->base = efi_file_handle_protocol;
194         fh->fs = fs;
195
196         if (parent) {
197                 char *p = fh->path;
198                 int exists;
199
200                 if (plen > 0) {
201                         strcpy(p, parent->path);
202                         p += plen - 1;
203                         *p++ = '/';
204                 }
205
206                 utf16_to_utf8((u8 *)p, file_name, flen);
207
208                 if (sanitize_path(fh->path))
209                         goto error;
210
211                 /* check if file exists: */
212                 if (set_blk_dev(fh))
213                         goto error;
214
215                 exists = fs_exists(fh->path);
216                 /* fs_exists() calls fs_close(), so open file system again */
217                 if (set_blk_dev(fh))
218                         goto error;
219
220                 if (!exists) {
221                         if (!(mode & EFI_FILE_MODE_CREATE) ||
222                             efi_create_file(fh, attributes))
223                                 goto error;
224                 }
225
226                 /* figure out if file is a directory: */
227                 fh->isdir = is_dir(fh);
228         } else {
229                 fh->isdir = 1;
230                 strcpy(fh->path, "");
231         }
232
233         return &fh->base;
234
235 error:
236         free(fh);
237         return NULL;
238 }
239
240 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
241                 struct efi_file_handle **new_handle,
242                 u16 *file_name, u64 open_mode, u64 attributes)
243 {
244         struct file_handle *fh = to_fh(file);
245         efi_status_t ret;
246
247         EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
248                   file_name, open_mode, attributes);
249
250         /* Check parameters */
251         if (!file || !new_handle || !file_name) {
252                 ret = EFI_INVALID_PARAMETER;
253                 goto out;
254         }
255         if (open_mode != EFI_FILE_MODE_READ &&
256             open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
257             open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
258                          EFI_FILE_MODE_CREATE)) {
259                 ret = EFI_INVALID_PARAMETER;
260                 goto out;
261         }
262         /*
263          * The UEFI spec requires that attributes are only set in create mode.
264          * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
265          * read mode. EDK2 does not check that attributes are zero if not in
266          * create mode.
267          *
268          * So here we only check attributes in create mode and do not check
269          * that they are zero otherwise.
270          */
271         if ((open_mode & EFI_FILE_MODE_CREATE) &&
272             (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
273                 ret = EFI_INVALID_PARAMETER;
274                 goto out;
275         }
276
277         /* Open file */
278         *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
279         if (*new_handle) {
280                 EFI_PRINT("file handle %p\n", *new_handle);
281                 ret = EFI_SUCCESS;
282         } else {
283                 ret = EFI_NOT_FOUND;
284         }
285 out:
286         return EFI_EXIT(ret);
287 }
288
289 static efi_status_t file_close(struct file_handle *fh)
290 {
291         fs_closedir(fh->dirs);
292         free(fh);
293         return EFI_SUCCESS;
294 }
295
296 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
297 {
298         struct file_handle *fh = to_fh(file);
299         EFI_ENTRY("%p", file);
300         return EFI_EXIT(file_close(fh));
301 }
302
303 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
304 {
305         struct file_handle *fh = to_fh(file);
306         efi_status_t ret = EFI_SUCCESS;
307
308         EFI_ENTRY("%p", file);
309
310         if (set_blk_dev(fh)) {
311                 ret = EFI_DEVICE_ERROR;
312                 goto error;
313         }
314
315         if (fs_unlink(fh->path))
316                 ret = EFI_DEVICE_ERROR;
317         file_close(fh);
318
319 error:
320         return EFI_EXIT(ret);
321 }
322
323 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
324                 void *buffer)
325 {
326         loff_t actread;
327
328         if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
329                     *buffer_size, &actread))
330                 return EFI_DEVICE_ERROR;
331
332         *buffer_size = actread;
333         fh->offset += actread;
334
335         return EFI_SUCCESS;
336 }
337
338 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
339                 void *buffer)
340 {
341         struct efi_file_info *info = buffer;
342         struct fs_dirent *dent;
343         unsigned int required_size;
344
345         if (!fh->dirs) {
346                 assert(fh->offset == 0);
347                 fh->dirs = fs_opendir(fh->path);
348                 if (!fh->dirs)
349                         return EFI_DEVICE_ERROR;
350         }
351
352         /*
353          * So this is a bit awkward.  Since fs layer is stateful and we
354          * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
355          * we might have to return without consuming the dent.. so we
356          * have to stash it for next call.
357          */
358         if (fh->dent) {
359                 dent = fh->dent;
360                 fh->dent = NULL;
361         } else {
362                 dent = fs_readdir(fh->dirs);
363         }
364
365
366         if (!dent) {
367                 /* no more files in directory: */
368                 /* workaround shim.efi bug/quirk.. as find_boot_csv()
369                  * loops through directory contents, it initially calls
370                  * read w/ zero length buffer to find out how much mem
371                  * to allocate for the EFI_FILE_INFO, then allocates,
372                  * and then calls a 2nd time.  If we return size of
373                  * zero the first time, it happily passes that to
374                  * AllocateZeroPool(), and when that returns NULL it
375                  * thinks it is EFI_OUT_OF_RESOURCES.  So on first
376                  * call return a non-zero size:
377                  */
378                 if (*buffer_size == 0)
379                         *buffer_size = sizeof(*info);
380                 else
381                         *buffer_size = 0;
382                 return EFI_SUCCESS;
383         }
384
385         /* check buffer size: */
386         required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
387         if (*buffer_size < required_size) {
388                 *buffer_size = required_size;
389                 fh->dent = dent;
390                 return EFI_BUFFER_TOO_SMALL;
391         }
392
393         *buffer_size = required_size;
394         memset(info, 0, required_size);
395
396         info->size = required_size;
397         info->file_size = dent->size;
398         info->physical_size = dent->size;
399
400         if (dent->type == FS_DT_DIR)
401                 info->attribute |= EFI_FILE_DIRECTORY;
402
403         ascii2unicode(info->file_name, dent->name);
404
405         fh->offset++;
406
407         return EFI_SUCCESS;
408 }
409
410 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
411                                          efi_uintn_t *buffer_size, void *buffer)
412 {
413         struct file_handle *fh = to_fh(file);
414         efi_status_t ret = EFI_SUCCESS;
415         u64 bs;
416
417         EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
418
419         if (!buffer_size || !buffer) {
420                 ret = EFI_INVALID_PARAMETER;
421                 goto error;
422         }
423
424         if (set_blk_dev(fh)) {
425                 ret = EFI_DEVICE_ERROR;
426                 goto error;
427         }
428
429         bs = *buffer_size;
430         if (fh->isdir)
431                 ret = dir_read(fh, &bs, buffer);
432         else
433                 ret = file_read(fh, &bs, buffer);
434         if (bs <= SIZE_MAX)
435                 *buffer_size = bs;
436         else
437                 *buffer_size = SIZE_MAX;
438
439 error:
440         return EFI_EXIT(ret);
441 }
442
443 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
444                                           efi_uintn_t *buffer_size,
445                                           void *buffer)
446 {
447         struct file_handle *fh = to_fh(file);
448         efi_status_t ret = EFI_SUCCESS;
449         loff_t actwrite;
450
451         EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
452
453         if (set_blk_dev(fh)) {
454                 ret = EFI_DEVICE_ERROR;
455                 goto error;
456         }
457
458         if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
459                      &actwrite)) {
460                 ret = EFI_DEVICE_ERROR;
461                 goto error;
462         }
463
464         *buffer_size = actwrite;
465         fh->offset += actwrite;
466
467 error:
468         return EFI_EXIT(ret);
469 }
470
471 /**
472  * efi_file_getpos() - get current position in file
473  *
474  * This function implements the GetPosition service of the EFI file protocol.
475  * See the UEFI spec for details.
476  *
477  * @file:       file handle
478  * @pos:        pointer to file position
479  * Return:      status code
480  */
481 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
482                                            u64 *pos)
483 {
484         efi_status_t ret = EFI_SUCCESS;
485         struct file_handle *fh = to_fh(file);
486
487         EFI_ENTRY("%p, %p", file, pos);
488
489         if (fh->isdir) {
490                 ret = EFI_UNSUPPORTED;
491                 goto out;
492         }
493
494         *pos = fh->offset;
495 out:
496         return EFI_EXIT(ret);
497 }
498
499 /**
500  * efi_file_setpos() - set current position in file
501  *
502  * This function implements the SetPosition service of the EFI file protocol.
503  * See the UEFI spec for details.
504  *
505  * @file:       file handle
506  * @pos:        new file position
507  * Return:      status code
508  */
509 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
510                                            u64 pos)
511 {
512         struct file_handle *fh = to_fh(file);
513         efi_status_t ret = EFI_SUCCESS;
514
515         EFI_ENTRY("%p, %llu", file, pos);
516
517         if (fh->isdir) {
518                 if (pos != 0) {
519                         ret = EFI_UNSUPPORTED;
520                         goto error;
521                 }
522                 fs_closedir(fh->dirs);
523                 fh->dirs = NULL;
524         }
525
526         if (pos == ~0ULL) {
527                 loff_t file_size;
528
529                 if (set_blk_dev(fh)) {
530                         ret = EFI_DEVICE_ERROR;
531                         goto error;
532                 }
533
534                 if (fs_size(fh->path, &file_size)) {
535                         ret = EFI_DEVICE_ERROR;
536                         goto error;
537                 }
538
539                 pos = file_size;
540         }
541
542         fh->offset = pos;
543
544 error:
545         return EFI_EXIT(ret);
546 }
547
548 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
549                                             const efi_guid_t *info_type,
550                                             efi_uintn_t *buffer_size,
551                                             void *buffer)
552 {
553         struct file_handle *fh = to_fh(file);
554         efi_status_t ret = EFI_SUCCESS;
555
556         EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
557
558         if (!guidcmp(info_type, &efi_file_info_guid)) {
559                 struct efi_file_info *info = buffer;
560                 char *filename = basename(fh);
561                 unsigned int required_size;
562                 loff_t file_size;
563
564                 /* check buffer size: */
565                 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
566                 if (*buffer_size < required_size) {
567                         *buffer_size = required_size;
568                         ret = EFI_BUFFER_TOO_SMALL;
569                         goto error;
570                 }
571
572                 if (set_blk_dev(fh)) {
573                         ret = EFI_DEVICE_ERROR;
574                         goto error;
575                 }
576
577                 if (fs_size(fh->path, &file_size)) {
578                         ret = EFI_DEVICE_ERROR;
579                         goto error;
580                 }
581
582                 memset(info, 0, required_size);
583
584                 info->size = required_size;
585                 info->file_size = file_size;
586                 info->physical_size = file_size;
587
588                 if (fh->isdir)
589                         info->attribute |= EFI_FILE_DIRECTORY;
590
591                 ascii2unicode(info->file_name, filename);
592         } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
593                 struct efi_file_system_info *info = buffer;
594                 disk_partition_t part;
595                 efi_uintn_t required_size;
596                 int r;
597
598                 if (fh->fs->part >= 1)
599                         r = part_get_info(fh->fs->desc, fh->fs->part, &part);
600                 else
601                         r = part_get_info_whole_disk(fh->fs->desc, &part);
602                 if (r < 0) {
603                         ret = EFI_DEVICE_ERROR;
604                         goto error;
605                 }
606                 required_size = sizeof(info) + 2 *
607                                 (strlen((const char *)part.name) + 1);
608                 if (*buffer_size < required_size) {
609                         *buffer_size = required_size;
610                         ret = EFI_BUFFER_TOO_SMALL;
611                         goto error;
612                 }
613
614                 memset(info, 0, required_size);
615
616                 info->size = required_size;
617                 info->read_only = true;
618                 info->volume_size = part.size * part.blksz;
619                 info->free_space = 0;
620                 info->block_size = part.blksz;
621                 /*
622                  * TODO: The volume label is not available in U-Boot.
623                  * Use the partition name as substitute.
624                  */
625                 ascii2unicode((u16 *)info->volume_label,
626                               (const char *)part.name);
627         } else {
628                 ret = EFI_UNSUPPORTED;
629         }
630
631 error:
632         return EFI_EXIT(ret);
633 }
634
635 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
636                                             const efi_guid_t *info_type,
637                                             efi_uintn_t buffer_size,
638                                             void *buffer)
639 {
640         struct file_handle *fh = to_fh(file);
641         efi_status_t ret = EFI_UNSUPPORTED;
642
643         EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
644
645         if (!guidcmp(info_type, &efi_file_info_guid)) {
646                 struct efi_file_info *info = (struct efi_file_info *)buffer;
647                 char *filename = basename(fh);
648                 char *new_file_name, *pos;
649                 loff_t file_size;
650
651                 if (buffer_size < sizeof(struct efi_file_info)) {
652                         ret = EFI_BAD_BUFFER_SIZE;
653                         goto out;
654                 }
655                 /* We cannot change the directory attribute */
656                 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
657                         ret = EFI_ACCESS_DENIED;
658                         goto out;
659                 }
660                 /* Check for renaming */
661                 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
662                 if (!new_file_name) {
663                         ret = EFI_OUT_OF_RESOURCES;
664                         goto out;
665                 }
666                 pos = new_file_name;
667                 utf16_utf8_strcpy(&pos, info->file_name);
668                 if (strcmp(new_file_name, filename)) {
669                         /* TODO: we do not support renaming */
670                         EFI_PRINT("Renaming not supported\n");
671                         free(new_file_name);
672                         ret = EFI_ACCESS_DENIED;
673                         goto out;
674                 }
675                 free(new_file_name);
676                 /* Check for truncation */
677                 if (set_blk_dev(fh)) {
678                         ret = EFI_DEVICE_ERROR;
679                         goto out;
680                 }
681                 if (fs_size(fh->path, &file_size)) {
682                         ret = EFI_DEVICE_ERROR;
683                         goto out;
684                 }
685                 if (file_size != info->file_size) {
686                         /* TODO: we do not support truncation */
687                         EFI_PRINT("Truncation not supported\n");
688                         ret = EFI_ACCESS_DENIED;
689                         goto out;
690                 }
691                 /*
692                  * We do not care for the other attributes
693                  * TODO: Support read only
694                  */
695                 ret = EFI_SUCCESS;
696         } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
697                 if (buffer_size < sizeof(struct efi_file_system_info)) {
698                         ret = EFI_BAD_BUFFER_SIZE;
699                         goto out;
700                 }
701         } else {
702                 ret = EFI_UNSUPPORTED;
703         }
704 out:
705         return EFI_EXIT(ret);
706 }
707
708 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
709 {
710         EFI_ENTRY("%p", file);
711         return EFI_EXIT(EFI_SUCCESS);
712 }
713
714 static const struct efi_file_handle efi_file_handle_protocol = {
715         /*
716          * TODO: We currently only support EFI file protocol revision 0x00010000
717          *       while UEFI specs 2.4 - 2.7 prescribe revision 0x00020000.
718          */
719         .rev = EFI_FILE_PROTOCOL_REVISION,
720         .open = efi_file_open,
721         .close = efi_file_close,
722         .delete = efi_file_delete,
723         .read = efi_file_read,
724         .write = efi_file_write,
725         .getpos = efi_file_getpos,
726         .setpos = efi_file_setpos,
727         .getinfo = efi_file_getinfo,
728         .setinfo = efi_file_setinfo,
729         .flush = efi_file_flush,
730 };
731
732 /**
733  * efi_file_from_path() - open file via device path
734  *
735  * @fp:         device path
736  * @return:     EFI_FILE_PROTOCOL for the file or NULL
737  */
738 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
739 {
740         struct efi_simple_file_system_protocol *v;
741         struct efi_file_handle *f;
742         efi_status_t ret;
743
744         v = efi_fs_from_path(fp);
745         if (!v)
746                 return NULL;
747
748         EFI_CALL(ret = v->open_volume(v, &f));
749         if (ret != EFI_SUCCESS)
750                 return NULL;
751
752         /* Skip over device-path nodes before the file path. */
753         while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
754                 fp = efi_dp_next(fp);
755
756         /*
757          * Step through the nodes of the directory path until the actual file
758          * node is reached which is the final node in the device path.
759          */
760         while (fp) {
761                 struct efi_device_path_file_path *fdp =
762                         container_of(fp, struct efi_device_path_file_path, dp);
763                 struct efi_file_handle *f2;
764
765                 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
766                         printf("bad file path!\n");
767                         f->close(f);
768                         return NULL;
769                 }
770
771                 EFI_CALL(ret = f->open(f, &f2, fdp->str,
772                                        EFI_FILE_MODE_READ, 0));
773                 if (ret != EFI_SUCCESS)
774                         return NULL;
775
776                 fp = efi_dp_next(fp);
777
778                 EFI_CALL(f->close(f));
779                 f = f2;
780         }
781
782         return f;
783 }
784
785 static efi_status_t EFIAPI
786 efi_open_volume(struct efi_simple_file_system_protocol *this,
787                 struct efi_file_handle **root)
788 {
789         struct file_system *fs = to_fs(this);
790
791         EFI_ENTRY("%p, %p", this, root);
792
793         *root = file_open(fs, NULL, NULL, 0, 0);
794
795         return EFI_EXIT(EFI_SUCCESS);
796 }
797
798 struct efi_simple_file_system_protocol *
799 efi_simple_file_system(struct blk_desc *desc, int part,
800                        struct efi_device_path *dp)
801 {
802         struct file_system *fs;
803
804         fs = calloc(1, sizeof(*fs));
805         fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
806         fs->base.open_volume = efi_open_volume;
807         fs->desc = desc;
808         fs->part = part;
809         fs->dp = dp;
810
811         return &fs->base;
812 }