blob: c1e285e9f7c96fb1df919724f6337b2350a8686d [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clark2a920802017-09-13 18:05:34 -04002/*
3 * EFI utils
4 *
5 * Copyright (c) 2017 Rob Clark
Rob Clark2a920802017-09-13 18:05:34 -04006 */
7
8#include <common.h>
9#include <charset.h>
10#include <efi_loader.h>
11#include <malloc.h>
Alexander Grafd5a5a5a2018-08-08 03:54:32 -060012#include <mapmem.h>
Rob Clark2a920802017-09-13 18:05:34 -040013#include <fs.h>
14
Heinrich Schuchardt9e6835e2018-04-04 15:42:11 +020015/* GUID for file system information */
16const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
17
Rob Clark2a920802017-09-13 18:05:34 -040018struct 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
26struct 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
40static const struct efi_file_handle efi_file_handle_protocol;
41
42static 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
50static 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
55static int is_dir(struct file_handle *fh)
56{
57 struct fs_dir_stream *dirs;
58
59 set_blk_dev(fh);
60 dirs = fs_opendir(fh->path);
61 if (!dirs)
62 return 0;
63
64 fs_closedir(dirs);
65
66 return 1;
67}
68
69/*
70 * Normalize a path which may include either back or fwd slashes,
71 * double slashes, . or .. entries in the path, etc.
72 */
73static int sanitize_path(char *path)
74{
75 char *p;
76
77 /* backslash to slash: */
78 p = path;
79 while ((p = strchr(p, '\\')))
80 *p++ = '/';
81
82 /* handle double-slashes: */
83 p = path;
84 while ((p = strstr(p, "//"))) {
85 char *src = p + 1;
86 memmove(p, src, strlen(src) + 1);
87 }
88
89 /* handle extra /.'s */
90 p = path;
91 while ((p = strstr(p, "/."))) {
92 /*
93 * You'd be tempted to do this *after* handling ".."s
94 * below to avoid having to check if "/." is start of
95 * a "/..", but that won't have the correct results..
96 * for example, "/foo/./../bar" would get resolved to
97 * "/foo/bar" if you did these two passes in the other
98 * order
99 */
100 if (p[2] == '.') {
101 p += 2;
102 continue;
103 }
104 char *src = p + 2;
105 memmove(p, src, strlen(src) + 1);
106 }
107
108 /* handle extra /..'s: */
109 p = path;
110 while ((p = strstr(p, "/.."))) {
111 char *src = p + 3;
112
113 p--;
114
115 /* find beginning of previous path entry: */
116 while (true) {
117 if (p < path)
118 return -1;
119 if (*p == '/')
120 break;
121 p--;
122 }
123
124 memmove(p, src, strlen(src) + 1);
125 }
126
127 return 0;
128}
129
Heinrich Schuchardt050cea72018-09-12 19:00:02 +0200130/**
131 * file_open() - open a file handle
132 *
133 * @fs: file system
134 * @parent: directory relative to which the file is to be opened
135 * @file_name: path of the file to be opened. '\', '.', or '..' may
136 * be used as modifiers. A leading backslash indicates an
137 * absolute path.
138 * @mode: bit mask indicating the access mode (read, write,
139 * create)
140 * @attributes: attributes for newly created file
141 * Returns: handle to the opened file or NULL
Rob Clark2a920802017-09-13 18:05:34 -0400142 */
143static struct efi_file_handle *file_open(struct file_system *fs,
AKASHI Takahiro5bc84a12018-09-11 15:59:12 +0900144 struct file_handle *parent, s16 *file_name, u64 mode,
145 u64 attributes)
Rob Clark2a920802017-09-13 18:05:34 -0400146{
147 struct file_handle *fh;
148 char f0[MAX_UTF8_PER_UTF16] = {0};
149 int plen = 0;
150 int flen = 0;
151
152 if (file_name) {
153 utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1);
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +0200154 flen = u16_strlen((u16 *)file_name);
Rob Clark2a920802017-09-13 18:05:34 -0400155 }
156
157 /* we could have a parent, but also an absolute path: */
158 if (f0[0] == '\\') {
159 plen = 0;
160 } else if (parent) {
161 plen = strlen(parent->path) + 1;
162 }
163
164 /* +2 is for null and '/' */
165 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
166
167 fh->base = efi_file_handle_protocol;
168 fh->fs = fs;
169
170 if (parent) {
171 char *p = fh->path;
172
173 if (plen > 0) {
174 strcpy(p, parent->path);
175 p += plen - 1;
176 *p++ = '/';
177 }
178
179 utf16_to_utf8((u8 *)p, (u16 *)file_name, flen);
180
181 if (sanitize_path(fh->path))
182 goto error;
183
184 /* check if file exists: */
185 if (set_blk_dev(fh))
186 goto error;
187
AKASHI Takahiro5bc84a12018-09-11 15:59:12 +0900188 if ((mode & EFI_FILE_MODE_CREATE) &&
189 (attributes & EFI_FILE_DIRECTORY)) {
190 if (fs_mkdir(fh->path))
191 goto error;
192 } else if (!((mode & EFI_FILE_MODE_CREATE) ||
193 fs_exists(fh->path)))
Rob Clark2a920802017-09-13 18:05:34 -0400194 goto error;
195
196 /* figure out if file is a directory: */
197 fh->isdir = is_dir(fh);
198 } else {
199 fh->isdir = 1;
200 strcpy(fh->path, "");
201 }
202
203 return &fh->base;
204
205error:
206 free(fh);
207 return NULL;
208}
209
210static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
211 struct efi_file_handle **new_handle,
212 s16 *file_name, u64 open_mode, u64 attributes)
213{
214 struct file_handle *fh = to_fh(file);
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200215 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400216
217 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name,
218 open_mode, attributes);
219
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200220 /* Check parameters */
Heinrich Schuchardtd3dce352018-09-15 20:43:46 +0200221 if (!file || !new_handle || !file_name) {
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200222 ret = EFI_INVALID_PARAMETER;
223 goto out;
224 }
225 if (open_mode != EFI_FILE_MODE_READ &&
226 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
227 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
228 EFI_FILE_MODE_CREATE)) {
229 ret = EFI_INVALID_PARAMETER;
230 goto out;
231 }
Heinrich Schuchardtbd665882018-09-13 21:31:49 +0200232 /*
233 * The UEFI spec requires that attributes are only set in create mode.
234 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
235 * read mode. EDK2 does not check that attributes are zero if not in
236 * create mode.
237 *
238 * So here we only check attributes in create mode and do not check
239 * that they are zero otherwise.
240 */
241 if ((open_mode & EFI_FILE_MODE_CREATE) &&
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200242 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
243 ret = EFI_INVALID_PARAMETER;
244 goto out;
245 }
Rob Clark2a920802017-09-13 18:05:34 -0400246
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200247 /* Open file */
248 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
249 if (*new_handle)
250 ret = EFI_SUCCESS;
251 else
252 ret = EFI_NOT_FOUND;
253out:
254 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400255}
256
257static efi_status_t file_close(struct file_handle *fh)
258{
259 fs_closedir(fh->dirs);
260 free(fh);
261 return EFI_SUCCESS;
262}
263
264static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
265{
266 struct file_handle *fh = to_fh(file);
267 EFI_ENTRY("%p", file);
268 return EFI_EXIT(file_close(fh));
269}
270
271static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
272{
273 struct file_handle *fh = to_fh(file);
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900274 efi_status_t ret = EFI_SUCCESS;
275
Rob Clark2a920802017-09-13 18:05:34 -0400276 EFI_ENTRY("%p", file);
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900277
278 if (set_blk_dev(fh)) {
279 ret = EFI_DEVICE_ERROR;
280 goto error;
281 }
282
283 if (fs_unlink(fh->path))
284 ret = EFI_DEVICE_ERROR;
Rob Clark2a920802017-09-13 18:05:34 -0400285 file_close(fh);
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900286
287error:
288 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400289}
290
291static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
292 void *buffer)
293{
294 loff_t actread;
295
Simon Glass2ae843f2018-09-15 00:51:00 -0600296 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
Rob Clark2a920802017-09-13 18:05:34 -0400297 *buffer_size, &actread))
298 return EFI_DEVICE_ERROR;
299
300 *buffer_size = actread;
301 fh->offset += actread;
302
303 return EFI_SUCCESS;
304}
305
306static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
307 void *buffer)
308{
309 struct efi_file_info *info = buffer;
310 struct fs_dirent *dent;
311 unsigned int required_size;
312
313 if (!fh->dirs) {
314 assert(fh->offset == 0);
315 fh->dirs = fs_opendir(fh->path);
316 if (!fh->dirs)
317 return EFI_DEVICE_ERROR;
318 }
319
320 /*
321 * So this is a bit awkward. Since fs layer is stateful and we
322 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
323 * we might have to return without consuming the dent.. so we
324 * have to stash it for next call.
325 */
326 if (fh->dent) {
327 dent = fh->dent;
328 fh->dent = NULL;
329 } else {
330 dent = fs_readdir(fh->dirs);
331 }
332
333
334 if (!dent) {
335 /* no more files in directory: */
336 /* workaround shim.efi bug/quirk.. as find_boot_csv()
337 * loops through directory contents, it initially calls
338 * read w/ zero length buffer to find out how much mem
339 * to allocate for the EFI_FILE_INFO, then allocates,
340 * and then calls a 2nd time. If we return size of
341 * zero the first time, it happily passes that to
342 * AllocateZeroPool(), and when that returns NULL it
343 * thinks it is EFI_OUT_OF_RESOURCES. So on first
344 * call return a non-zero size:
345 */
346 if (*buffer_size == 0)
347 *buffer_size = sizeof(*info);
348 else
349 *buffer_size = 0;
350 return EFI_SUCCESS;
351 }
352
353 /* check buffer size: */
354 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
355 if (*buffer_size < required_size) {
356 *buffer_size = required_size;
357 fh->dent = dent;
358 return EFI_BUFFER_TOO_SMALL;
359 }
360
361 *buffer_size = required_size;
362 memset(info, 0, required_size);
363
364 info->size = required_size;
365 info->file_size = dent->size;
366 info->physical_size = dent->size;
367
368 if (dent->type == FS_DT_DIR)
369 info->attribute |= EFI_FILE_DIRECTORY;
370
371 ascii2unicode((u16 *)info->file_name, dent->name);
372
373 fh->offset++;
374
375 return EFI_SUCCESS;
376}
377
378static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200379 efi_uintn_t *buffer_size, void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400380{
381 struct file_handle *fh = to_fh(file);
382 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200383 u64 bs;
Rob Clark2a920802017-09-13 18:05:34 -0400384
385 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
386
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200387 if (!buffer_size || !buffer) {
388 ret = EFI_INVALID_PARAMETER;
389 goto error;
390 }
391
Rob Clark2a920802017-09-13 18:05:34 -0400392 if (set_blk_dev(fh)) {
393 ret = EFI_DEVICE_ERROR;
394 goto error;
395 }
396
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200397 bs = *buffer_size;
Rob Clark2a920802017-09-13 18:05:34 -0400398 if (fh->isdir)
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200399 ret = dir_read(fh, &bs, buffer);
Rob Clark2a920802017-09-13 18:05:34 -0400400 else
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200401 ret = file_read(fh, &bs, buffer);
402 if (bs <= SIZE_MAX)
403 *buffer_size = bs;
404 else
405 *buffer_size = SIZE_MAX;
Rob Clark2a920802017-09-13 18:05:34 -0400406
407error:
408 return EFI_EXIT(ret);
409}
410
411static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200412 efi_uintn_t *buffer_size,
413 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400414{
415 struct file_handle *fh = to_fh(file);
416 efi_status_t ret = EFI_SUCCESS;
417 loff_t actwrite;
418
419 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
420
421 if (set_blk_dev(fh)) {
422 ret = EFI_DEVICE_ERROR;
423 goto error;
424 }
425
Simon Glass2ae843f2018-09-15 00:51:00 -0600426 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
Rob Clark2a920802017-09-13 18:05:34 -0400427 &actwrite)) {
428 ret = EFI_DEVICE_ERROR;
429 goto error;
430 }
431
432 *buffer_size = actwrite;
433 fh->offset += actwrite;
434
435error:
436 return EFI_EXIT(ret);
437}
438
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200439/**
440 * efi_file_getpos() - get current position in file
441 *
442 * This function implements the GetPosition service of the EFI file protocol.
443 * See the UEFI spec for details.
444 *
445 * @file: file handle
446 * @pos: pointer to file position
447 * Return: status code
448 */
Rob Clark2a920802017-09-13 18:05:34 -0400449static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200450 u64 *pos)
Rob Clark2a920802017-09-13 18:05:34 -0400451{
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200452 efi_status_t ret = EFI_SUCCESS;
Rob Clark2a920802017-09-13 18:05:34 -0400453 struct file_handle *fh = to_fh(file);
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200454
Rob Clark2a920802017-09-13 18:05:34 -0400455 EFI_ENTRY("%p, %p", file, pos);
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200456
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200457 if (fh->isdir) {
458 ret = EFI_UNSUPPORTED;
459 goto out;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200460 }
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200461
462 *pos = fh->offset;
463out:
464 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400465}
466
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200467/**
468 * efi_file_setpos() - set current position in file
469 *
470 * This function implements the SetPosition service of the EFI file protocol.
471 * See the UEFI spec for details.
472 *
473 * @file: file handle
474 * @pos: new file position
475 * Return: status code
476 */
Rob Clark2a920802017-09-13 18:05:34 -0400477static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200478 u64 pos)
Rob Clark2a920802017-09-13 18:05:34 -0400479{
480 struct file_handle *fh = to_fh(file);
481 efi_status_t ret = EFI_SUCCESS;
482
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200483 EFI_ENTRY("%p, %llu", file, pos);
Rob Clark2a920802017-09-13 18:05:34 -0400484
485 if (fh->isdir) {
486 if (pos != 0) {
487 ret = EFI_UNSUPPORTED;
488 goto error;
489 }
490 fs_closedir(fh->dirs);
491 fh->dirs = NULL;
492 }
493
494 if (pos == ~0ULL) {
495 loff_t file_size;
496
497 if (set_blk_dev(fh)) {
498 ret = EFI_DEVICE_ERROR;
499 goto error;
500 }
501
502 if (fs_size(fh->path, &file_size)) {
503 ret = EFI_DEVICE_ERROR;
504 goto error;
505 }
506
507 pos = file_size;
508 }
509
510 fh->offset = pos;
511
512error:
513 return EFI_EXIT(ret);
514}
515
516static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
Heinrich Schuchardt9c9021e2018-04-04 15:42:09 +0200517 const efi_guid_t *info_type,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200518 efi_uintn_t *buffer_size,
519 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400520{
521 struct file_handle *fh = to_fh(file);
522 efi_status_t ret = EFI_SUCCESS;
523
Heinrich Schuchardt008fb482018-09-14 22:46:34 +0200524 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
Rob Clark2a920802017-09-13 18:05:34 -0400525
526 if (!guidcmp(info_type, &efi_file_info_guid)) {
527 struct efi_file_info *info = buffer;
528 char *filename = basename(fh);
529 unsigned int required_size;
530 loff_t file_size;
531
532 /* check buffer size: */
533 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
534 if (*buffer_size < required_size) {
535 *buffer_size = required_size;
536 ret = EFI_BUFFER_TOO_SMALL;
537 goto error;
538 }
539
540 if (set_blk_dev(fh)) {
541 ret = EFI_DEVICE_ERROR;
542 goto error;
543 }
544
545 if (fs_size(fh->path, &file_size)) {
546 ret = EFI_DEVICE_ERROR;
547 goto error;
548 }
549
550 memset(info, 0, required_size);
551
552 info->size = required_size;
553 info->file_size = file_size;
554 info->physical_size = file_size;
555
556 if (fh->isdir)
557 info->attribute |= EFI_FILE_DIRECTORY;
558
559 ascii2unicode((u16 *)info->file_name, filename);
Heinrich Schuchardt9e6835e2018-04-04 15:42:11 +0200560 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
561 struct efi_file_system_info *info = buffer;
562 disk_partition_t part;
563 efi_uintn_t required_size;
564 int r;
565
566 if (fh->fs->part >= 1)
567 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
568 else
569 r = part_get_info_whole_disk(fh->fs->desc, &part);
570 if (r < 0) {
571 ret = EFI_DEVICE_ERROR;
572 goto error;
573 }
574 required_size = sizeof(info) + 2 *
575 (strlen((const char *)part.name) + 1);
576 if (*buffer_size < required_size) {
577 *buffer_size = required_size;
578 ret = EFI_BUFFER_TOO_SMALL;
579 goto error;
580 }
581
582 memset(info, 0, required_size);
583
584 info->size = required_size;
585 info->read_only = true;
586 info->volume_size = part.size * part.blksz;
587 info->free_space = 0;
588 info->block_size = part.blksz;
589 /*
590 * TODO: The volume label is not available in U-Boot.
591 * Use the partition name as substitute.
592 */
593 ascii2unicode((u16 *)info->volume_label,
594 (const char *)part.name);
Rob Clark2a920802017-09-13 18:05:34 -0400595 } else {
596 ret = EFI_UNSUPPORTED;
597 }
598
599error:
600 return EFI_EXIT(ret);
601}
602
603static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
Heinrich Schuchardt9c9021e2018-04-04 15:42:09 +0200604 const efi_guid_t *info_type,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200605 efi_uintn_t buffer_size,
606 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400607{
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200608 EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
609
Rob Clark2a920802017-09-13 18:05:34 -0400610 return EFI_EXIT(EFI_UNSUPPORTED);
611}
612
613static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
614{
615 EFI_ENTRY("%p", file);
616 return EFI_EXIT(EFI_SUCCESS);
617}
618
619static const struct efi_file_handle efi_file_handle_protocol = {
620 .rev = EFI_FILE_PROTOCOL_REVISION,
621 .open = efi_file_open,
622 .close = efi_file_close,
623 .delete = efi_file_delete,
624 .read = efi_file_read,
625 .write = efi_file_write,
626 .getpos = efi_file_getpos,
627 .setpos = efi_file_setpos,
628 .getinfo = efi_file_getinfo,
629 .setinfo = efi_file_setinfo,
630 .flush = efi_file_flush,
631};
632
633struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
634{
635 struct efi_simple_file_system_protocol *v;
636 struct efi_file_handle *f;
637 efi_status_t ret;
638
639 v = efi_fs_from_path(fp);
640 if (!v)
641 return NULL;
642
643 EFI_CALL(ret = v->open_volume(v, &f));
644 if (ret != EFI_SUCCESS)
645 return NULL;
646
647 /* skip over device-path nodes before the file path: */
648 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
649 fp = efi_dp_next(fp);
650
651 while (fp) {
652 struct efi_device_path_file_path *fdp =
653 container_of(fp, struct efi_device_path_file_path, dp);
654 struct efi_file_handle *f2;
655
656 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
657 printf("bad file path!\n");
658 f->close(f);
659 return NULL;
660 }
661
662 EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
663 EFI_FILE_MODE_READ, 0));
664 if (ret != EFI_SUCCESS)
665 return NULL;
666
667 fp = efi_dp_next(fp);
668
669 EFI_CALL(f->close(f));
670 f = f2;
671 }
672
673 return f;
674}
675
676static efi_status_t EFIAPI
677efi_open_volume(struct efi_simple_file_system_protocol *this,
678 struct efi_file_handle **root)
679{
680 struct file_system *fs = to_fs(this);
681
682 EFI_ENTRY("%p, %p", this, root);
683
AKASHI Takahiro5bc84a12018-09-11 15:59:12 +0900684 *root = file_open(fs, NULL, NULL, 0, 0);
Rob Clark2a920802017-09-13 18:05:34 -0400685
686 return EFI_EXIT(EFI_SUCCESS);
687}
688
689struct efi_simple_file_system_protocol *
690efi_simple_file_system(struct blk_desc *desc, int part,
691 struct efi_device_path *dp)
692{
693 struct file_system *fs;
694
695 fs = calloc(1, sizeof(*fs));
696 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
697 fs->base.open_volume = efi_open_volume;
698 fs->desc = desc;
699 fs->part = part;
700 fs->dp = dp;
701
702 return &fs->base;
703}