blob: 7a7077e6d03223ccb6aeec8c38bf7bb9bec4fd8e [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clark53142032017-09-13 18:05:34 -04002/*
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +02003 * EFI_FILE_PROTOCOL
Rob Clark53142032017-09-13 18:05:34 -04004 *
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +02005 * Copyright (c) 2017 Rob Clark
Rob Clark53142032017-09-13 18:05:34 -04006 */
7
8#include <common.h>
9#include <charset.h>
10#include <efi_loader.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Rob Clark53142032017-09-13 18:05:34 -040012#include <malloc.h>
Alexander Grafab133c72018-08-08 03:54:32 -060013#include <mapmem.h>
Rob Clark53142032017-09-13 18:05:34 -040014#include <fs.h>
Simon Glass655306c2020-05-10 11:39:58 -060015#include <part.h>
Rob Clark53142032017-09-13 18:05:34 -040016
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +020017/* GUID for file system information */
18const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
19
Heinrich Schuchardt0c236c42019-09-08 10:32:54 +020020/* GUID to obtain the volume label */
21const efi_guid_t efi_system_volume_label_id = EFI_FILE_SYSTEM_VOLUME_LABEL_ID;
22
Rob Clark53142032017-09-13 18:05:34 -040023struct file_system {
24 struct efi_simple_file_system_protocol base;
25 struct efi_device_path *dp;
26 struct blk_desc *desc;
27 int part;
28};
29#define to_fs(x) container_of(x, struct file_system, base)
30
31struct file_handle {
32 struct efi_file_handle base;
33 struct file_system *fs;
34 loff_t offset; /* current file position/cursor */
35 int isdir;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +020036 u64 open_mode;
Rob Clark53142032017-09-13 18:05:34 -040037
38 /* for reading a directory: */
39 struct fs_dir_stream *dirs;
40 struct fs_dirent *dent;
41
42 char path[0];
43};
44#define to_fh(x) container_of(x, struct file_handle, base)
45
46static const struct efi_file_handle efi_file_handle_protocol;
47
48static char *basename(struct file_handle *fh)
49{
50 char *s = strrchr(fh->path, '/');
51 if (s)
52 return s + 1;
53 return fh->path;
54}
55
56static int set_blk_dev(struct file_handle *fh)
57{
58 return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
59}
60
Heinrich Schuchardt80f834d2018-10-02 05:57:32 +020061/**
62 * is_dir() - check if file handle points to directory
63 *
64 * We assume that set_blk_dev(fh) has been called already.
65 *
66 * @fh: file handle
67 * Return: true if file handle points to a directory
68 */
Rob Clark53142032017-09-13 18:05:34 -040069static int is_dir(struct file_handle *fh)
70{
71 struct fs_dir_stream *dirs;
72
Rob Clark53142032017-09-13 18:05:34 -040073 dirs = fs_opendir(fh->path);
74 if (!dirs)
75 return 0;
76
77 fs_closedir(dirs);
78
79 return 1;
80}
81
82/*
83 * Normalize a path which may include either back or fwd slashes,
84 * double slashes, . or .. entries in the path, etc.
85 */
86static int sanitize_path(char *path)
87{
88 char *p;
89
90 /* backslash to slash: */
91 p = path;
92 while ((p = strchr(p, '\\')))
93 *p++ = '/';
94
95 /* handle double-slashes: */
96 p = path;
97 while ((p = strstr(p, "//"))) {
98 char *src = p + 1;
99 memmove(p, src, strlen(src) + 1);
100 }
101
102 /* handle extra /.'s */
103 p = path;
104 while ((p = strstr(p, "/."))) {
105 /*
106 * You'd be tempted to do this *after* handling ".."s
107 * below to avoid having to check if "/." is start of
108 * a "/..", but that won't have the correct results..
109 * for example, "/foo/./../bar" would get resolved to
110 * "/foo/bar" if you did these two passes in the other
111 * order
112 */
113 if (p[2] == '.') {
114 p += 2;
115 continue;
116 }
117 char *src = p + 2;
118 memmove(p, src, strlen(src) + 1);
119 }
120
121 /* handle extra /..'s: */
122 p = path;
123 while ((p = strstr(p, "/.."))) {
124 char *src = p + 3;
125
126 p--;
127
128 /* find beginning of previous path entry: */
129 while (true) {
130 if (p < path)
131 return -1;
132 if (*p == '/')
133 break;
134 p--;
135 }
136
137 memmove(p, src, strlen(src) + 1);
138 }
139
140 return 0;
141}
142
Heinrich Schuchardt1af423b2018-09-12 19:00:02 +0200143/**
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200144 * efi_create_file() - create file or directory
145 *
146 * @fh: file handle
147 * @attributes: attributes for newly created file
148 * Returns: 0 for success
149 */
150static int efi_create_file(struct file_handle *fh, u64 attributes)
151{
152 loff_t actwrite;
153 void *buffer = &actwrite;
154
155 if (attributes & EFI_FILE_DIRECTORY)
156 return fs_mkdir(fh->path);
157 else
158 return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
159 &actwrite);
160}
161
162/**
Heinrich Schuchardt1af423b2018-09-12 19:00:02 +0200163 * file_open() - open a file handle
164 *
165 * @fs: file system
166 * @parent: directory relative to which the file is to be opened
167 * @file_name: path of the file to be opened. '\', '.', or '..' may
168 * be used as modifiers. A leading backslash indicates an
169 * absolute path.
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200170 * @open_mode: bit mask indicating the access mode (read, write,
Heinrich Schuchardt1af423b2018-09-12 19:00:02 +0200171 * create)
172 * @attributes: attributes for newly created file
173 * Returns: handle to the opened file or NULL
Rob Clark53142032017-09-13 18:05:34 -0400174 */
175static struct efi_file_handle *file_open(struct file_system *fs,
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200176 struct file_handle *parent, u16 *file_name, u64 open_mode,
AKASHI Takahiro0c672b02018-09-11 15:59:12 +0900177 u64 attributes)
Rob Clark53142032017-09-13 18:05:34 -0400178{
179 struct file_handle *fh;
180 char f0[MAX_UTF8_PER_UTF16] = {0};
181 int plen = 0;
182 int flen = 0;
183
184 if (file_name) {
Heinrich Schuchardtdb47c342019-01-12 12:02:33 +0100185 utf16_to_utf8((u8 *)f0, file_name, 1);
186 flen = u16_strlen(file_name);
Rob Clark53142032017-09-13 18:05:34 -0400187 }
188
189 /* we could have a parent, but also an absolute path: */
190 if (f0[0] == '\\') {
191 plen = 0;
192 } else if (parent) {
193 plen = strlen(parent->path) + 1;
194 }
195
196 /* +2 is for null and '/' */
197 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
198
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200199 fh->open_mode = open_mode;
Rob Clark53142032017-09-13 18:05:34 -0400200 fh->base = efi_file_handle_protocol;
201 fh->fs = fs;
202
203 if (parent) {
204 char *p = fh->path;
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200205 int exists;
Rob Clark53142032017-09-13 18:05:34 -0400206
207 if (plen > 0) {
208 strcpy(p, parent->path);
209 p += plen - 1;
210 *p++ = '/';
211 }
212
Heinrich Schuchardtdb47c342019-01-12 12:02:33 +0100213 utf16_to_utf8((u8 *)p, file_name, flen);
Rob Clark53142032017-09-13 18:05:34 -0400214
215 if (sanitize_path(fh->path))
216 goto error;
217
218 /* check if file exists: */
219 if (set_blk_dev(fh))
220 goto error;
221
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200222 exists = fs_exists(fh->path);
Heinrich Schuchardt3ab87ac2019-02-09 22:23:48 +0100223 /* fs_exists() calls fs_close(), so open file system again */
224 if (set_blk_dev(fh))
225 goto error;
226
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200227 if (!exists) {
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200228 if (!(open_mode & EFI_FILE_MODE_CREATE) ||
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200229 efi_create_file(fh, attributes))
230 goto error;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200231 if (set_blk_dev(fh))
232 goto error;
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200233 }
234
Rob Clark53142032017-09-13 18:05:34 -0400235 /* figure out if file is a directory: */
236 fh->isdir = is_dir(fh);
237 } else {
238 fh->isdir = 1;
239 strcpy(fh->path, "");
240 }
241
242 return &fh->base;
243
244error:
245 free(fh);
246 return NULL;
247}
248
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100249static efi_status_t efi_file_open_int(struct efi_file_handle *this,
250 struct efi_file_handle **new_handle,
251 u16 *file_name, u64 open_mode,
252 u64 attributes)
Rob Clark53142032017-09-13 18:05:34 -0400253{
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100254 struct file_handle *fh = to_fh(this);
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200255 efi_status_t ret;
Rob Clark53142032017-09-13 18:05:34 -0400256
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200257 /* Check parameters */
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100258 if (!this || !new_handle || !file_name) {
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200259 ret = EFI_INVALID_PARAMETER;
260 goto out;
261 }
262 if (open_mode != EFI_FILE_MODE_READ &&
263 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
264 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
265 EFI_FILE_MODE_CREATE)) {
266 ret = EFI_INVALID_PARAMETER;
267 goto out;
268 }
Heinrich Schuchardt662d0be2018-09-13 21:31:49 +0200269 /*
270 * The UEFI spec requires that attributes are only set in create mode.
271 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
272 * read mode. EDK2 does not check that attributes are zero if not in
273 * create mode.
274 *
275 * So here we only check attributes in create mode and do not check
276 * that they are zero otherwise.
277 */
278 if ((open_mode & EFI_FILE_MODE_CREATE) &&
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200279 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
280 ret = EFI_INVALID_PARAMETER;
281 goto out;
282 }
Rob Clark53142032017-09-13 18:05:34 -0400283
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200284 /* Open file */
285 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200286 if (*new_handle) {
287 EFI_PRINT("file handle %p\n", *new_handle);
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200288 ret = EFI_SUCCESS;
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200289 } else {
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200290 ret = EFI_NOT_FOUND;
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200291 }
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100292out:
293 return ret;
294}
295
296/**
297 * efi_file_open_()
298 *
299 * This function implements the Open service of the File Protocol.
300 * See the UEFI spec for details.
301 *
302 * @this: EFI_FILE_PROTOCOL instance
303 * @new_handle: on return pointer to file handle
304 * @file_name: file name
305 * @open_mode: mode to open the file (read, read/write, create/read/write)
306 * @attributes: attributes for newly created file
307 */
308static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *this,
309 struct efi_file_handle **new_handle,
310 u16 *file_name, u64 open_mode,
311 u64 attributes)
312{
313 efi_status_t ret;
314
315 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", this, new_handle,
316 file_name, open_mode, attributes);
317
318 ret = efi_file_open_int(this, new_handle, file_name, open_mode,
319 attributes);
320
321 return EFI_EXIT(ret);
322}
323
324/**
325 * efi_file_open_ex() - open file asynchronously
326 *
327 * This function implements the OpenEx service of the File Protocol.
328 * See the UEFI spec for details.
329 *
330 * @this: EFI_FILE_PROTOCOL instance
331 * @new_handle: on return pointer to file handle
332 * @file_name: file name
333 * @open_mode: mode to open the file (read, read/write, create/read/write)
334 * @attributes: attributes for newly created file
335 * @token: transaction token
336 */
337static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *this,
338 struct efi_file_handle **new_handle,
339 u16 *file_name, u64 open_mode,
340 u64 attributes,
341 struct efi_file_io_token *token)
342{
343 efi_status_t ret;
344
345 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu, %p", this, new_handle,
346 file_name, open_mode, attributes, token);
347
348 if (!token) {
349 ret = EFI_INVALID_PARAMETER;
350 goto out;
351 }
352
353 ret = efi_file_open_int(this, new_handle, file_name, open_mode,
354 attributes);
355
356 if (ret == EFI_SUCCESS && token->event) {
357 token->status = EFI_SUCCESS;
358 efi_signal_event(token->event);
359 }
360
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200361out:
362 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400363}
364
365static efi_status_t file_close(struct file_handle *fh)
366{
367 fs_closedir(fh->dirs);
368 free(fh);
369 return EFI_SUCCESS;
370}
371
372static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
373{
374 struct file_handle *fh = to_fh(file);
375 EFI_ENTRY("%p", file);
376 return EFI_EXIT(file_close(fh));
377}
378
379static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
380{
381 struct file_handle *fh = to_fh(file);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900382 efi_status_t ret = EFI_SUCCESS;
383
Rob Clark53142032017-09-13 18:05:34 -0400384 EFI_ENTRY("%p", file);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900385
Heinrich Schuchardt646faf32019-06-17 22:00:13 +0200386 if (set_blk_dev(fh) || fs_unlink(fh->path))
387 ret = EFI_WARN_DELETE_FAILURE;
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900388
Rob Clark53142032017-09-13 18:05:34 -0400389 file_close(fh);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900390 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400391}
392
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200393/**
394 * efi_get_file_size() - determine the size of a file
395 *
396 * @fh: file handle
397 * @file_size: pointer to receive file size
398 * Return: status code
399 */
400static efi_status_t efi_get_file_size(struct file_handle *fh,
401 loff_t *file_size)
402{
403 if (set_blk_dev(fh))
404 return EFI_DEVICE_ERROR;
405
406 if (fs_size(fh->path, file_size))
407 return EFI_DEVICE_ERROR;
408
409 return EFI_SUCCESS;
410}
411
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +0200412/**
413 * efi_file_size() - Get the size of a file using an EFI file handle
414 *
415 * @fh: EFI file handle
416 * @size: buffer to fill in the discovered size
417 *
418 * Return: size of the file
419 */
420efi_status_t efi_file_size(struct efi_file_handle *fh, efi_uintn_t *size)
421{
422 struct efi_file_info *info = NULL;
423 efi_uintn_t bs = 0;
424 efi_status_t ret;
425
426 *size = 0;
427 ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
428 info));
429 if (ret != EFI_BUFFER_TOO_SMALL) {
430 ret = EFI_DEVICE_ERROR;
431 goto out;
432 }
433
434 info = malloc(bs);
435 if (!info) {
436 ret = EFI_OUT_OF_RESOURCES;
437 goto out;
438 }
439 ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
440 info));
441 if (ret != EFI_SUCCESS)
442 goto out;
443
444 *size = info->file_size;
445
446out:
447 free(info);
448 return ret;
449}
450
Rob Clark53142032017-09-13 18:05:34 -0400451static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
452 void *buffer)
453{
454 loff_t actread;
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200455 efi_status_t ret;
456 loff_t file_size;
Rob Clark53142032017-09-13 18:05:34 -0400457
Stefan Sørensen6b398d02020-07-22 09:43:31 +0200458 if (!buffer) {
459 ret = EFI_INVALID_PARAMETER;
460 return ret;
461 }
462
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200463 ret = efi_get_file_size(fh, &file_size);
464 if (ret != EFI_SUCCESS)
465 return ret;
466 if (file_size < fh->offset) {
467 ret = EFI_DEVICE_ERROR;
468 return ret;
469 }
470
471 if (set_blk_dev(fh))
472 return EFI_DEVICE_ERROR;
Simon Glass849688f2018-09-15 00:51:00 -0600473 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
Rob Clark53142032017-09-13 18:05:34 -0400474 *buffer_size, &actread))
475 return EFI_DEVICE_ERROR;
476
477 *buffer_size = actread;
478 fh->offset += actread;
479
480 return EFI_SUCCESS;
481}
482
Heinrich Schuchardt69f722b2021-05-15 22:41:26 +0200483static void rtc2efi(struct efi_time *time, struct rtc_time *tm)
484{
485 memset(time, 0, sizeof(struct efi_time));
486 time->year = tm->tm_year;
487 time->month = tm->tm_mon;
488 time->day = tm->tm_mday;
489 time->hour = tm->tm_hour;
490 time->minute = tm->tm_min;
491 time->second = tm->tm_sec;
492}
493
Rob Clark53142032017-09-13 18:05:34 -0400494static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
495 void *buffer)
496{
497 struct efi_file_info *info = buffer;
498 struct fs_dirent *dent;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200499 u64 required_size;
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200500 u16 *dst;
Rob Clark53142032017-09-13 18:05:34 -0400501
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200502 if (set_blk_dev(fh))
503 return EFI_DEVICE_ERROR;
504
Rob Clark53142032017-09-13 18:05:34 -0400505 if (!fh->dirs) {
506 assert(fh->offset == 0);
507 fh->dirs = fs_opendir(fh->path);
508 if (!fh->dirs)
509 return EFI_DEVICE_ERROR;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200510 fh->dent = NULL;
Rob Clark53142032017-09-13 18:05:34 -0400511 }
512
513 /*
514 * So this is a bit awkward. Since fs layer is stateful and we
515 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
516 * we might have to return without consuming the dent.. so we
517 * have to stash it for next call.
518 */
519 if (fh->dent) {
520 dent = fh->dent;
Rob Clark53142032017-09-13 18:05:34 -0400521 } else {
522 dent = fs_readdir(fh->dirs);
523 }
524
Rob Clark53142032017-09-13 18:05:34 -0400525 if (!dent) {
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200526 /* no more files in directory */
527 *buffer_size = 0;
Rob Clark53142032017-09-13 18:05:34 -0400528 return EFI_SUCCESS;
529 }
530
531 /* check buffer size: */
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200532 required_size = sizeof(*info) +
533 2 * (utf8_utf16_strlen(dent->name) + 1);
Rob Clark53142032017-09-13 18:05:34 -0400534 if (*buffer_size < required_size) {
535 *buffer_size = required_size;
536 fh->dent = dent;
537 return EFI_BUFFER_TOO_SMALL;
538 }
Stefan Sørensen6b398d02020-07-22 09:43:31 +0200539 if (!buffer)
540 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200541 fh->dent = NULL;
Rob Clark53142032017-09-13 18:05:34 -0400542
543 *buffer_size = required_size;
544 memset(info, 0, required_size);
545
546 info->size = required_size;
547 info->file_size = dent->size;
548 info->physical_size = dent->size;
Heinrich Schuchardt69f722b2021-05-15 22:41:26 +0200549 info->attribute = dent->attr;
550 rtc2efi(&info->create_time, &dent->create_time);
551 rtc2efi(&info->modification_time, &dent->change_time);
552 rtc2efi(&info->last_access_time, &dent->access_time);
Rob Clark53142032017-09-13 18:05:34 -0400553
554 if (dent->type == FS_DT_DIR)
555 info->attribute |= EFI_FILE_DIRECTORY;
556
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200557 dst = info->file_name;
558 utf8_utf16_strcpy(&dst, dent->name);
Rob Clark53142032017-09-13 18:05:34 -0400559
560 fh->offset++;
561
562 return EFI_SUCCESS;
563}
564
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100565static efi_status_t efi_file_read_int(struct efi_file_handle *this,
566 efi_uintn_t *buffer_size, void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400567{
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100568 struct file_handle *fh = to_fh(this);
Rob Clark53142032017-09-13 18:05:34 -0400569 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200570 u64 bs;
Rob Clark53142032017-09-13 18:05:34 -0400571
Peng Fanca02db22021-04-28 21:54:01 +0800572 if (!this || !buffer_size)
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100573 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200574
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200575 bs = *buffer_size;
Rob Clark53142032017-09-13 18:05:34 -0400576 if (fh->isdir)
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200577 ret = dir_read(fh, &bs, buffer);
Rob Clark53142032017-09-13 18:05:34 -0400578 else
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200579 ret = file_read(fh, &bs, buffer);
580 if (bs <= SIZE_MAX)
581 *buffer_size = bs;
582 else
583 *buffer_size = SIZE_MAX;
Rob Clark53142032017-09-13 18:05:34 -0400584
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100585 return ret;
586}
587
588/**
589 * efi_file_read() - read file
590 *
591 * This function implements the Read() service of the EFI_FILE_PROTOCOL.
592 *
593 * See the Unified Extensible Firmware Interface (UEFI) specification for
594 * details.
595 *
596 * @this: file protocol instance
597 * @buffer_size: number of bytes to read
598 * @buffer: read buffer
599 * Return: status code
600 */
601static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *this,
602 efi_uintn_t *buffer_size, void *buffer)
603{
604 efi_status_t ret;
605
606 EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
607
608 ret = efi_file_read_int(this, buffer_size, buffer);
609
Rob Clark53142032017-09-13 18:05:34 -0400610 return EFI_EXIT(ret);
611}
612
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200613/**
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100614 * efi_file_read_ex() - read file asynchonously
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200615 *
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100616 * This function implements the ReadEx() service of the EFI_FILE_PROTOCOL.
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200617 *
618 * See the Unified Extensible Firmware Interface (UEFI) specification for
619 * details.
620 *
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100621 * @this: file protocol instance
622 * @token: transaction token
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200623 * Return: status code
624 */
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100625static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *this,
626 struct efi_file_io_token *token)
Rob Clark53142032017-09-13 18:05:34 -0400627{
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100628 efi_status_t ret;
629
630 EFI_ENTRY("%p, %p", this, token);
631
632 if (!token) {
633 ret = EFI_INVALID_PARAMETER;
634 goto out;
635 }
636
637 ret = efi_file_read_int(this, &token->buffer_size, token->buffer);
638
639 if (ret == EFI_SUCCESS && token->event) {
640 token->status = EFI_SUCCESS;
641 efi_signal_event(token->event);
642 }
643
644out:
645 return EFI_EXIT(ret);
646}
647
648static efi_status_t efi_file_write_int(struct efi_file_handle *this,
649 efi_uintn_t *buffer_size, void *buffer)
650{
651 struct file_handle *fh = to_fh(this);
Rob Clark53142032017-09-13 18:05:34 -0400652 efi_status_t ret = EFI_SUCCESS;
653 loff_t actwrite;
654
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100655 if (!this || !buffer_size || !buffer) {
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200656 ret = EFI_INVALID_PARAMETER;
657 goto out;
658 }
659 if (fh->isdir) {
660 ret = EFI_UNSUPPORTED;
661 goto out;
662 }
663 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
664 ret = EFI_ACCESS_DENIED;
665 goto out;
666 }
667
668 if (!*buffer_size)
669 goto out;
670
Rob Clark53142032017-09-13 18:05:34 -0400671 if (set_blk_dev(fh)) {
672 ret = EFI_DEVICE_ERROR;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200673 goto out;
Rob Clark53142032017-09-13 18:05:34 -0400674 }
Simon Glass849688f2018-09-15 00:51:00 -0600675 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
Rob Clark53142032017-09-13 18:05:34 -0400676 &actwrite)) {
677 ret = EFI_DEVICE_ERROR;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200678 goto out;
Rob Clark53142032017-09-13 18:05:34 -0400679 }
Rob Clark53142032017-09-13 18:05:34 -0400680 *buffer_size = actwrite;
681 fh->offset += actwrite;
682
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200683out:
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100684 return ret;
685}
686
687/**
688 * efi_file_write() - write to file
689 *
690 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
691 *
692 * See the Unified Extensible Firmware Interface (UEFI) specification for
693 * details.
694 *
695 * @this: file protocol instance
696 * @buffer_size: number of bytes to write
697 * @buffer: buffer with the bytes to write
698 * Return: status code
699 */
700static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *this,
701 efi_uintn_t *buffer_size,
702 void *buffer)
703{
704 efi_status_t ret;
705
706 EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
707
708 ret = efi_file_write_int(this, buffer_size, buffer);
709
Rob Clark53142032017-09-13 18:05:34 -0400710 return EFI_EXIT(ret);
711}
712
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200713/**
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100714 * efi_file_write_ex() - write to file
715 *
716 * This function implements the WriteEx() service of the EFI_FILE_PROTOCOL.
717 *
718 * See the Unified Extensible Firmware Interface (UEFI) specification for
719 * details.
720 *
721 * @this: file protocol instance
722 * @token: transaction token
723 * Return: status code
724 */
725static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *this,
726 struct efi_file_io_token *token)
727{
728 efi_status_t ret;
729
730 EFI_ENTRY("%p, %p", this, token);
731
732 if (!token) {
733 ret = EFI_INVALID_PARAMETER;
734 goto out;
735 }
736
737 ret = efi_file_write_int(this, &token->buffer_size, token->buffer);
738
739 if (ret == EFI_SUCCESS && token->event) {
740 token->status = EFI_SUCCESS;
741 efi_signal_event(token->event);
742 }
743
744out:
745 return EFI_EXIT(ret);
746}
747
748/**
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200749 * efi_file_getpos() - get current position in file
750 *
751 * This function implements the GetPosition service of the EFI file protocol.
752 * See the UEFI spec for details.
753 *
754 * @file: file handle
755 * @pos: pointer to file position
756 * Return: status code
757 */
Rob Clark53142032017-09-13 18:05:34 -0400758static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200759 u64 *pos)
Rob Clark53142032017-09-13 18:05:34 -0400760{
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200761 efi_status_t ret = EFI_SUCCESS;
Rob Clark53142032017-09-13 18:05:34 -0400762 struct file_handle *fh = to_fh(file);
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200763
Rob Clark53142032017-09-13 18:05:34 -0400764 EFI_ENTRY("%p, %p", file, pos);
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200765
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200766 if (fh->isdir) {
767 ret = EFI_UNSUPPORTED;
768 goto out;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200769 }
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200770
771 *pos = fh->offset;
772out:
773 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400774}
775
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200776/**
777 * efi_file_setpos() - set current position in file
778 *
779 * This function implements the SetPosition service of the EFI file protocol.
780 * See the UEFI spec for details.
781 *
782 * @file: file handle
783 * @pos: new file position
784 * Return: status code
785 */
Rob Clark53142032017-09-13 18:05:34 -0400786static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200787 u64 pos)
Rob Clark53142032017-09-13 18:05:34 -0400788{
789 struct file_handle *fh = to_fh(file);
790 efi_status_t ret = EFI_SUCCESS;
791
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200792 EFI_ENTRY("%p, %llu", file, pos);
Rob Clark53142032017-09-13 18:05:34 -0400793
794 if (fh->isdir) {
795 if (pos != 0) {
796 ret = EFI_UNSUPPORTED;
797 goto error;
798 }
799 fs_closedir(fh->dirs);
800 fh->dirs = NULL;
801 }
802
803 if (pos == ~0ULL) {
804 loff_t file_size;
805
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200806 ret = efi_get_file_size(fh, &file_size);
807 if (ret != EFI_SUCCESS)
Rob Clark53142032017-09-13 18:05:34 -0400808 goto error;
Rob Clark53142032017-09-13 18:05:34 -0400809 pos = file_size;
810 }
811
812 fh->offset = pos;
813
814error:
815 return EFI_EXIT(ret);
816}
817
818static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
Heinrich Schuchardte86380d2018-04-04 15:42:09 +0200819 const efi_guid_t *info_type,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200820 efi_uintn_t *buffer_size,
821 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400822{
823 struct file_handle *fh = to_fh(file);
824 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200825 u16 *dst;
Rob Clark53142032017-09-13 18:05:34 -0400826
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100827 EFI_ENTRY("%p, %pUs, %p, %p", file, info_type, buffer_size, buffer);
Rob Clark53142032017-09-13 18:05:34 -0400828
Heinrich Schuchardtc540a0e2019-09-08 10:45:31 +0200829 if (!file || !info_type || !buffer_size ||
830 (*buffer_size && !buffer)) {
831 ret = EFI_INVALID_PARAMETER;
832 goto error;
833 }
834
Rob Clark53142032017-09-13 18:05:34 -0400835 if (!guidcmp(info_type, &efi_file_info_guid)) {
836 struct efi_file_info *info = buffer;
837 char *filename = basename(fh);
838 unsigned int required_size;
839 loff_t file_size;
840
841 /* check buffer size: */
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200842 required_size = sizeof(*info) +
843 2 * (utf8_utf16_strlen(filename) + 1);
Rob Clark53142032017-09-13 18:05:34 -0400844 if (*buffer_size < required_size) {
845 *buffer_size = required_size;
846 ret = EFI_BUFFER_TOO_SMALL;
847 goto error;
848 }
849
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200850 ret = efi_get_file_size(fh, &file_size);
851 if (ret != EFI_SUCCESS)
Rob Clark53142032017-09-13 18:05:34 -0400852 goto error;
Rob Clark53142032017-09-13 18:05:34 -0400853
854 memset(info, 0, required_size);
855
856 info->size = required_size;
857 info->file_size = file_size;
858 info->physical_size = file_size;
859
860 if (fh->isdir)
861 info->attribute |= EFI_FILE_DIRECTORY;
862
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200863 dst = info->file_name;
864 utf8_utf16_strcpy(&dst, filename);
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200865 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
866 struct efi_file_system_info *info = buffer;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600867 struct disk_partition part;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200868 efi_uintn_t required_size;
869 int r;
870
871 if (fh->fs->part >= 1)
872 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
873 else
874 r = part_get_info_whole_disk(fh->fs->desc, &part);
875 if (r < 0) {
876 ret = EFI_DEVICE_ERROR;
877 goto error;
878 }
Heinrich Schuchardt0c236c42019-09-08 10:32:54 +0200879 required_size = sizeof(*info) + 2;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200880 if (*buffer_size < required_size) {
881 *buffer_size = required_size;
882 ret = EFI_BUFFER_TOO_SMALL;
883 goto error;
884 }
885
886 memset(info, 0, required_size);
887
888 info->size = required_size;
Heinrich Schuchardta821b0c2019-12-08 10:02:37 +0100889 /*
890 * TODO: We cannot determine if the volume can be written to.
891 */
892 info->read_only = false;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200893 info->volume_size = part.size * part.blksz;
Heinrich Schuchardta821b0c2019-12-08 10:02:37 +0100894 /*
895 * TODO: We currently have no function to determine the free
896 * space. The volume size is the best upper bound we have.
897 */
898 info->free_space = info->volume_size;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200899 info->block_size = part.blksz;
900 /*
901 * TODO: The volume label is not available in U-Boot.
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200902 */
Heinrich Schuchardt0c236c42019-09-08 10:32:54 +0200903 info->volume_label[0] = 0;
904 } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
905 if (*buffer_size < 2) {
906 *buffer_size = 2;
907 ret = EFI_BUFFER_TOO_SMALL;
908 goto error;
909 }
910 *(u16 *)buffer = 0;
Rob Clark53142032017-09-13 18:05:34 -0400911 } else {
912 ret = EFI_UNSUPPORTED;
913 }
914
915error:
916 return EFI_EXIT(ret);
917}
918
919static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
Heinrich Schuchardte86380d2018-04-04 15:42:09 +0200920 const efi_guid_t *info_type,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200921 efi_uintn_t buffer_size,
922 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400923{
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200924 struct file_handle *fh = to_fh(file);
925 efi_status_t ret = EFI_UNSUPPORTED;
926
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100927 EFI_ENTRY("%p, %pUs, %zu, %p", file, info_type, buffer_size, buffer);
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200928
929 if (!guidcmp(info_type, &efi_file_info_guid)) {
930 struct efi_file_info *info = (struct efi_file_info *)buffer;
931 char *filename = basename(fh);
932 char *new_file_name, *pos;
933 loff_t file_size;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200934
Heinrich Schuchardtdd106d02019-09-08 11:37:07 +0200935 /* The buffer will always contain a file name. */
936 if (buffer_size < sizeof(struct efi_file_info) + 2 ||
937 buffer_size < info->size) {
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200938 ret = EFI_BAD_BUFFER_SIZE;
939 goto out;
940 }
941 /* We cannot change the directory attribute */
942 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
943 ret = EFI_ACCESS_DENIED;
944 goto out;
945 }
946 /* Check for renaming */
Heinrich Schuchardt344bc072020-11-10 07:24:16 +0100947 new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1);
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200948 if (!new_file_name) {
949 ret = EFI_OUT_OF_RESOURCES;
950 goto out;
951 }
952 pos = new_file_name;
953 utf16_utf8_strcpy(&pos, info->file_name);
954 if (strcmp(new_file_name, filename)) {
955 /* TODO: we do not support renaming */
956 EFI_PRINT("Renaming not supported\n");
957 free(new_file_name);
958 ret = EFI_ACCESS_DENIED;
959 goto out;
960 }
961 free(new_file_name);
962 /* Check for truncation */
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200963 ret = efi_get_file_size(fh, &file_size);
964 if (ret != EFI_SUCCESS)
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200965 goto out;
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200966 if (file_size != info->file_size) {
967 /* TODO: we do not support truncation */
968 EFI_PRINT("Truncation not supported\n");
969 ret = EFI_ACCESS_DENIED;
970 goto out;
971 }
972 /*
973 * We do not care for the other attributes
974 * TODO: Support read only
975 */
976 ret = EFI_SUCCESS;
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200977 } else {
Heinrich Schuchardtdd106d02019-09-08 11:37:07 +0200978 /* TODO: We do not support changing the volume label */
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200979 ret = EFI_UNSUPPORTED;
980 }
981out:
982 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400983}
984
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100985/**
986 * efi_file_flush_int() - flush file
987 *
988 * This is the internal implementation of the Flush() and FlushEx() services of
989 * the EFI_FILE_PROTOCOL.
990 *
991 * @this: file protocol instance
992 * Return: status code
993 */
994static efi_status_t efi_file_flush_int(struct efi_file_handle *this)
Rob Clark53142032017-09-13 18:05:34 -0400995{
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100996 struct file_handle *fh = to_fh(this);
Rob Clark53142032017-09-13 18:05:34 -0400997
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100998 if (!this)
999 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001000
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001001 if (!(fh->open_mode & EFI_FILE_MODE_WRITE))
1002 return EFI_ACCESS_DENIED;
1003
1004 /* TODO: flush for file position after end of file */
1005 return EFI_SUCCESS;
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001006}
1007
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001008/**
1009 * efi_file_flush() - flush file
1010 *
1011 * This function implements the Flush() service of the EFI_FILE_PROTOCOL.
1012 *
1013 * See the Unified Extensible Firmware Interface (UEFI) specification for
1014 * details.
1015 *
1016 * @this: file protocol instance
1017 * Return: status code
1018 */
1019static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *this)
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001020{
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001021 efi_status_t ret;
1022
1023 EFI_ENTRY("%p", this);
1024
1025 ret = efi_file_flush_int(this);
1026
1027 return EFI_EXIT(ret);
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001028}
1029
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001030/**
1031 * efi_file_flush_ex() - flush file
1032 *
1033 * This function implements the FlushEx() service of the EFI_FILE_PROTOCOL.
1034 *
1035 * See the Unified Extensible Firmware Interface (UEFI) specification for
1036 * details.
1037 *
1038 * @this: file protocol instance
1039 * @token: transaction token
1040 * Return: status code
1041 */
1042static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *this,
1043 struct efi_file_io_token *token)
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001044{
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001045 efi_status_t ret;
1046
1047 EFI_ENTRY("%p, %p", this, token);
1048
1049 if (!token) {
1050 ret = EFI_INVALID_PARAMETER;
1051 goto out;
1052 }
1053
1054 ret = efi_file_flush_int(this);
1055
1056 if (ret == EFI_SUCCESS && token->event) {
1057 token->status = EFI_SUCCESS;
1058 efi_signal_event(token->event);
1059 }
1060
1061out:
1062 return EFI_EXIT(ret);
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001063}
1064
Rob Clark53142032017-09-13 18:05:34 -04001065static const struct efi_file_handle efi_file_handle_protocol = {
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001066 .rev = EFI_FILE_PROTOCOL_REVISION2,
Rob Clark53142032017-09-13 18:05:34 -04001067 .open = efi_file_open,
1068 .close = efi_file_close,
1069 .delete = efi_file_delete,
1070 .read = efi_file_read,
1071 .write = efi_file_write,
1072 .getpos = efi_file_getpos,
1073 .setpos = efi_file_setpos,
1074 .getinfo = efi_file_getinfo,
1075 .setinfo = efi_file_setinfo,
1076 .flush = efi_file_flush,
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001077 .open_ex = efi_file_open_ex,
1078 .read_ex = efi_file_read_ex,
1079 .write_ex = efi_file_write_ex,
1080 .flush_ex = efi_file_flush_ex,
Rob Clark53142032017-09-13 18:05:34 -04001081};
1082
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001083/**
1084 * efi_file_from_path() - open file via device path
1085 *
1086 * @fp: device path
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +01001087 * Return: EFI_FILE_PROTOCOL for the file or NULL
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001088 */
Rob Clark53142032017-09-13 18:05:34 -04001089struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
1090{
1091 struct efi_simple_file_system_protocol *v;
1092 struct efi_file_handle *f;
1093 efi_status_t ret;
1094
1095 v = efi_fs_from_path(fp);
1096 if (!v)
1097 return NULL;
1098
1099 EFI_CALL(ret = v->open_volume(v, &f));
1100 if (ret != EFI_SUCCESS)
1101 return NULL;
1102
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001103 /* Skip over device-path nodes before the file path. */
Rob Clark53142032017-09-13 18:05:34 -04001104 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
1105 fp = efi_dp_next(fp);
1106
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001107 /*
1108 * Step through the nodes of the directory path until the actual file
1109 * node is reached which is the final node in the device path.
1110 */
Rob Clark53142032017-09-13 18:05:34 -04001111 while (fp) {
1112 struct efi_device_path_file_path *fdp =
1113 container_of(fp, struct efi_device_path_file_path, dp);
1114 struct efi_file_handle *f2;
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +02001115 u16 *filename;
Rob Clark53142032017-09-13 18:05:34 -04001116
1117 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
1118 printf("bad file path!\n");
1119 f->close(f);
1120 return NULL;
1121 }
1122
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +02001123 filename = u16_strdup(fdp->str);
1124 if (!filename)
1125 return NULL;
1126 EFI_CALL(ret = f->open(f, &f2, filename,
Rob Clark53142032017-09-13 18:05:34 -04001127 EFI_FILE_MODE_READ, 0));
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +02001128 free(filename);
Rob Clark53142032017-09-13 18:05:34 -04001129 if (ret != EFI_SUCCESS)
1130 return NULL;
1131
1132 fp = efi_dp_next(fp);
1133
1134 EFI_CALL(f->close(f));
1135 f = f2;
1136 }
1137
1138 return f;
1139}
1140
1141static efi_status_t EFIAPI
1142efi_open_volume(struct efi_simple_file_system_protocol *this,
1143 struct efi_file_handle **root)
1144{
1145 struct file_system *fs = to_fs(this);
1146
1147 EFI_ENTRY("%p, %p", this, root);
1148
AKASHI Takahiro0c672b02018-09-11 15:59:12 +09001149 *root = file_open(fs, NULL, NULL, 0, 0);
Rob Clark53142032017-09-13 18:05:34 -04001150
1151 return EFI_EXIT(EFI_SUCCESS);
1152}
1153
1154struct efi_simple_file_system_protocol *
1155efi_simple_file_system(struct blk_desc *desc, int part,
1156 struct efi_device_path *dp)
1157{
1158 struct file_system *fs;
1159
1160 fs = calloc(1, sizeof(*fs));
1161 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
1162 fs->base.open_volume = efi_open_volume;
1163 fs->desc = desc;
1164 fs->part = part;
1165 fs->dp = dp;
1166
1167 return &fs->base;
1168}