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