blob: 8ece8e71ee1c2ba2ed17a2c5aadb937371fb196c [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/*
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +02003 * EFI_FILE_PROTOCOL
Rob Clark2a920802017-09-13 18:05:34 -04004 *
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +02005 * 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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Rob Clark2a920802017-09-13 18:05:34 -040012#include <malloc.h>
Alexander Grafd5a5a5a2018-08-08 03:54:32 -060013#include <mapmem.h>
Rob Clark2a920802017-09-13 18:05:34 -040014#include <fs.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060015#include <part.h>
Rob Clark2a920802017-09-13 18:05:34 -040016
Heinrich Schuchardt9e6835e2018-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 Schuchardt632834c2019-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 Clark2a920802017-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 Schuchardtb0f1c722019-09-06 21:37:21 +020036 u64 open_mode;
Rob Clark2a920802017-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 Schuchardt2c61e0c2018-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 Clark2a920802017-09-13 18:05:34 -040069static int is_dir(struct file_handle *fh)
70{
71 struct fs_dir_stream *dirs;
72
Rob Clark2a920802017-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 Schuchardt050cea72018-09-12 19:00:02 +0200143/**
Heinrich Schuchardtcb0c2a72019-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 Schuchardt050cea72018-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 Schuchardtb0f1c722019-09-06 21:37:21 +0200170 * @open_mode: bit mask indicating the access mode (read, write,
Heinrich Schuchardt050cea72018-09-12 19:00:02 +0200171 * create)
172 * @attributes: attributes for newly created file
173 * Returns: handle to the opened file or NULL
Rob Clark2a920802017-09-13 18:05:34 -0400174 */
175static struct efi_file_handle *file_open(struct file_system *fs,
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200176 struct file_handle *parent, u16 *file_name, u64 open_mode,
AKASHI Takahiro5bc84a12018-09-11 15:59:12 +0900177 u64 attributes)
Rob Clark2a920802017-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 Schuchardtc82f8f62019-01-12 12:02:33 +0100185 utf16_to_utf8((u8 *)f0, file_name, 1);
186 flen = u16_strlen(file_name);
Rob Clark2a920802017-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 Schuchardtb0f1c722019-09-06 21:37:21 +0200199 fh->open_mode = open_mode;
Rob Clark2a920802017-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 Schuchardtcb0c2a72019-04-06 16:27:34 +0200205 int exists;
Rob Clark2a920802017-09-13 18:05:34 -0400206
207 if (plen > 0) {
208 strcpy(p, parent->path);
209 p += plen - 1;
210 *p++ = '/';
211 }
212
Heinrich Schuchardtc82f8f62019-01-12 12:02:33 +0100213 utf16_to_utf8((u8 *)p, file_name, flen);
Rob Clark2a920802017-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 Schuchardtcb0c2a72019-04-06 16:27:34 +0200222 exists = fs_exists(fh->path);
Heinrich Schuchardt823c2332019-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 Schuchardtcb0c2a72019-04-06 16:27:34 +0200227 if (!exists) {
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200228 if (!(open_mode & EFI_FILE_MODE_CREATE) ||
Heinrich Schuchardtcb0c2a72019-04-06 16:27:34 +0200229 efi_create_file(fh, attributes))
230 goto error;
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200231 if (set_blk_dev(fh))
232 goto error;
Heinrich Schuchardtcb0c2a72019-04-06 16:27:34 +0200233 }
234
Rob Clark2a920802017-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 Schuchardtdb12f512021-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 Clark2a920802017-09-13 18:05:34 -0400253{
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100254 struct file_handle *fh = to_fh(this);
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200255 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400256
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200257 /* Check parameters */
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100258 if (!this || !new_handle || !file_name) {
Heinrich Schuchardt143acd12018-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 Schuchardtbd665882018-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 Schuchardt143acd12018-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 Clark2a920802017-09-13 18:05:34 -0400283
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200284 /* Open file */
285 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
Heinrich Schuchardt19b2d892019-04-06 16:36:16 +0200286 if (*new_handle) {
287 EFI_PRINT("file handle %p\n", *new_handle);
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200288 ret = EFI_SUCCESS;
Heinrich Schuchardt19b2d892019-04-06 16:36:16 +0200289 } else {
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200290 ret = EFI_NOT_FOUND;
Heinrich Schuchardt19b2d892019-04-06 16:36:16 +0200291 }
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200292out:
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100293 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
361out:
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200362 return EFI_EXIT(ret);
Rob Clark2a920802017-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 Takahirod39f6a62018-09-11 15:59:16 +0900382 efi_status_t ret = EFI_SUCCESS;
383
Rob Clark2a920802017-09-13 18:05:34 -0400384 EFI_ENTRY("%p", file);
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900385
Heinrich Schuchardtfa390812019-06-17 22:00:13 +0200386 if (set_blk_dev(fh) || fs_unlink(fh->path))
387 ret = EFI_WARN_DELETE_FAILURE;
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900388
Rob Clark2a920802017-09-13 18:05:34 -0400389 file_close(fh);
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900390 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400391}
392
Heinrich Schuchardt9bb62fa2019-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
Rob Clark2a920802017-09-13 18:05:34 -0400412static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
413 void *buffer)
414{
415 loff_t actread;
Heinrich Schuchardt9bb62fa2019-09-07 23:28:04 +0200416 efi_status_t ret;
417 loff_t file_size;
Rob Clark2a920802017-09-13 18:05:34 -0400418
Stefan Sørensen1ef1cf12020-07-22 09:43:31 +0200419 if (!buffer) {
420 ret = EFI_INVALID_PARAMETER;
421 return ret;
422 }
423
Heinrich Schuchardt9bb62fa2019-09-07 23:28:04 +0200424 ret = efi_get_file_size(fh, &file_size);
425 if (ret != EFI_SUCCESS)
426 return ret;
427 if (file_size < fh->offset) {
428 ret = EFI_DEVICE_ERROR;
429 return ret;
430 }
431
432 if (set_blk_dev(fh))
433 return EFI_DEVICE_ERROR;
Simon Glass2ae843f2018-09-15 00:51:00 -0600434 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
Rob Clark2a920802017-09-13 18:05:34 -0400435 *buffer_size, &actread))
436 return EFI_DEVICE_ERROR;
437
438 *buffer_size = actread;
439 fh->offset += actread;
440
441 return EFI_SUCCESS;
442}
443
444static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
445 void *buffer)
446{
447 struct efi_file_info *info = buffer;
448 struct fs_dirent *dent;
Heinrich Schuchardt83a74ad2019-09-07 22:34:07 +0200449 u64 required_size;
Heinrich Schuchardt87c48402019-09-07 21:05:45 +0200450 u16 *dst;
Rob Clark2a920802017-09-13 18:05:34 -0400451
Heinrich Schuchardt9bb62fa2019-09-07 23:28:04 +0200452 if (set_blk_dev(fh))
453 return EFI_DEVICE_ERROR;
454
Rob Clark2a920802017-09-13 18:05:34 -0400455 if (!fh->dirs) {
456 assert(fh->offset == 0);
457 fh->dirs = fs_opendir(fh->path);
458 if (!fh->dirs)
459 return EFI_DEVICE_ERROR;
Heinrich Schuchardt83a74ad2019-09-07 22:34:07 +0200460 fh->dent = NULL;
Rob Clark2a920802017-09-13 18:05:34 -0400461 }
462
463 /*
464 * So this is a bit awkward. Since fs layer is stateful and we
465 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
466 * we might have to return without consuming the dent.. so we
467 * have to stash it for next call.
468 */
469 if (fh->dent) {
470 dent = fh->dent;
Rob Clark2a920802017-09-13 18:05:34 -0400471 } else {
472 dent = fs_readdir(fh->dirs);
473 }
474
Rob Clark2a920802017-09-13 18:05:34 -0400475 if (!dent) {
Heinrich Schuchardt83a74ad2019-09-07 22:34:07 +0200476 /* no more files in directory */
477 *buffer_size = 0;
Rob Clark2a920802017-09-13 18:05:34 -0400478 return EFI_SUCCESS;
479 }
480
481 /* check buffer size: */
Heinrich Schuchardt87c48402019-09-07 21:05:45 +0200482 required_size = sizeof(*info) +
483 2 * (utf8_utf16_strlen(dent->name) + 1);
Rob Clark2a920802017-09-13 18:05:34 -0400484 if (*buffer_size < required_size) {
485 *buffer_size = required_size;
486 fh->dent = dent;
487 return EFI_BUFFER_TOO_SMALL;
488 }
Stefan Sørensen1ef1cf12020-07-22 09:43:31 +0200489 if (!buffer)
490 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt83a74ad2019-09-07 22:34:07 +0200491 fh->dent = NULL;
Rob Clark2a920802017-09-13 18:05:34 -0400492
493 *buffer_size = required_size;
494 memset(info, 0, required_size);
495
496 info->size = required_size;
497 info->file_size = dent->size;
498 info->physical_size = dent->size;
499
500 if (dent->type == FS_DT_DIR)
501 info->attribute |= EFI_FILE_DIRECTORY;
502
Heinrich Schuchardt87c48402019-09-07 21:05:45 +0200503 dst = info->file_name;
504 utf8_utf16_strcpy(&dst, dent->name);
Rob Clark2a920802017-09-13 18:05:34 -0400505
506 fh->offset++;
507
508 return EFI_SUCCESS;
509}
510
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100511static efi_status_t efi_file_read_int(struct efi_file_handle *this,
512 efi_uintn_t *buffer_size, void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400513{
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100514 struct file_handle *fh = to_fh(this);
Rob Clark2a920802017-09-13 18:05:34 -0400515 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200516 u64 bs;
Rob Clark2a920802017-09-13 18:05:34 -0400517
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100518 if (!this || !buffer_size || !buffer)
519 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200520
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200521 bs = *buffer_size;
Rob Clark2a920802017-09-13 18:05:34 -0400522 if (fh->isdir)
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200523 ret = dir_read(fh, &bs, buffer);
Rob Clark2a920802017-09-13 18:05:34 -0400524 else
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200525 ret = file_read(fh, &bs, buffer);
526 if (bs <= SIZE_MAX)
527 *buffer_size = bs;
528 else
529 *buffer_size = SIZE_MAX;
Rob Clark2a920802017-09-13 18:05:34 -0400530
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100531 return ret;
Rob Clark2a920802017-09-13 18:05:34 -0400532}
533
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200534/**
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100535 * efi_file_read() - read file
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200536 *
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100537 * This function implements the Read() service of the EFI_FILE_PROTOCOL.
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200538 *
539 * See the Unified Extensible Firmware Interface (UEFI) specification for
540 * details.
541 *
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100542 * @this: file protocol instance
543 * @buffer_size: number of bytes to read
544 * @buffer: read buffer
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200545 * Return: status code
546 */
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100547static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *this,
548 efi_uintn_t *buffer_size, void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400549{
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100550 efi_status_t ret;
551
552 EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
553
554 ret = efi_file_read_int(this, buffer_size, buffer);
555
556 return EFI_EXIT(ret);
557}
558
559/**
560 * efi_file_read_ex() - read file asynchonously
561 *
562 * This function implements the ReadEx() service of the EFI_FILE_PROTOCOL.
563 *
564 * See the Unified Extensible Firmware Interface (UEFI) specification for
565 * details.
566 *
567 * @this: file protocol instance
568 * @token: transaction token
569 * Return: status code
570 */
571static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *this,
572 struct efi_file_io_token *token)
573{
574 efi_status_t ret;
575
576 EFI_ENTRY("%p, %p", this, token);
577
578 if (!token) {
579 ret = EFI_INVALID_PARAMETER;
580 goto out;
581 }
582
583 ret = efi_file_read_int(this, &token->buffer_size, token->buffer);
584
585 if (ret == EFI_SUCCESS && token->event) {
586 token->status = EFI_SUCCESS;
587 efi_signal_event(token->event);
588 }
589
590out:
591 return EFI_EXIT(ret);
592}
593
594static efi_status_t efi_file_write_int(struct efi_file_handle *this,
595 efi_uintn_t *buffer_size, void *buffer)
596{
597 struct file_handle *fh = to_fh(this);
Rob Clark2a920802017-09-13 18:05:34 -0400598 efi_status_t ret = EFI_SUCCESS;
599 loff_t actwrite;
600
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100601 if (!this || !buffer_size || !buffer) {
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200602 ret = EFI_INVALID_PARAMETER;
603 goto out;
604 }
605 if (fh->isdir) {
606 ret = EFI_UNSUPPORTED;
607 goto out;
608 }
609 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
610 ret = EFI_ACCESS_DENIED;
611 goto out;
Rob Clark2a920802017-09-13 18:05:34 -0400612 }
613
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200614 if (!*buffer_size)
615 goto out;
616
617 if (set_blk_dev(fh)) {
618 ret = EFI_DEVICE_ERROR;
619 goto out;
620 }
Simon Glass2ae843f2018-09-15 00:51:00 -0600621 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
Rob Clark2a920802017-09-13 18:05:34 -0400622 &actwrite)) {
623 ret = EFI_DEVICE_ERROR;
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200624 goto out;
Rob Clark2a920802017-09-13 18:05:34 -0400625 }
Rob Clark2a920802017-09-13 18:05:34 -0400626 *buffer_size = actwrite;
627 fh->offset += actwrite;
628
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200629out:
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100630 return ret;
631}
632
633/**
634 * efi_file_write() - write to file
635 *
636 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
637 *
638 * See the Unified Extensible Firmware Interface (UEFI) specification for
639 * details.
640 *
641 * @this: file protocol instance
642 * @buffer_size: number of bytes to write
643 * @buffer: buffer with the bytes to write
644 * Return: status code
645 */
646static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *this,
647 efi_uintn_t *buffer_size,
648 void *buffer)
649{
650 efi_status_t ret;
651
652 EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
653
654 ret = efi_file_write_int(this, buffer_size, buffer);
655
656 return EFI_EXIT(ret);
657}
658
659/**
660 * efi_file_write_ex() - write to file
661 *
662 * This function implements the WriteEx() service of the EFI_FILE_PROTOCOL.
663 *
664 * See the Unified Extensible Firmware Interface (UEFI) specification for
665 * details.
666 *
667 * @this: file protocol instance
668 * @token: transaction token
669 * Return: status code
670 */
671static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *this,
672 struct efi_file_io_token *token)
673{
674 efi_status_t ret;
675
676 EFI_ENTRY("%p, %p", this, token);
677
678 if (!token) {
679 ret = EFI_INVALID_PARAMETER;
680 goto out;
681 }
682
683 ret = efi_file_write_int(this, &token->buffer_size, token->buffer);
684
685 if (ret == EFI_SUCCESS && token->event) {
686 token->status = EFI_SUCCESS;
687 efi_signal_event(token->event);
688 }
689
690out:
Rob Clark2a920802017-09-13 18:05:34 -0400691 return EFI_EXIT(ret);
692}
693
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200694/**
695 * efi_file_getpos() - get current position in file
696 *
697 * This function implements the GetPosition service of the EFI file protocol.
698 * See the UEFI spec for details.
699 *
700 * @file: file handle
701 * @pos: pointer to file position
702 * Return: status code
703 */
Rob Clark2a920802017-09-13 18:05:34 -0400704static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200705 u64 *pos)
Rob Clark2a920802017-09-13 18:05:34 -0400706{
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200707 efi_status_t ret = EFI_SUCCESS;
Rob Clark2a920802017-09-13 18:05:34 -0400708 struct file_handle *fh = to_fh(file);
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200709
Rob Clark2a920802017-09-13 18:05:34 -0400710 EFI_ENTRY("%p, %p", file, pos);
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200711
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200712 if (fh->isdir) {
713 ret = EFI_UNSUPPORTED;
714 goto out;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200715 }
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200716
717 *pos = fh->offset;
718out:
719 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400720}
721
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200722/**
723 * efi_file_setpos() - set current position in file
724 *
725 * This function implements the SetPosition service of the EFI file protocol.
726 * See the UEFI spec for details.
727 *
728 * @file: file handle
729 * @pos: new file position
730 * Return: status code
731 */
Rob Clark2a920802017-09-13 18:05:34 -0400732static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200733 u64 pos)
Rob Clark2a920802017-09-13 18:05:34 -0400734{
735 struct file_handle *fh = to_fh(file);
736 efi_status_t ret = EFI_SUCCESS;
737
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200738 EFI_ENTRY("%p, %llu", file, pos);
Rob Clark2a920802017-09-13 18:05:34 -0400739
740 if (fh->isdir) {
741 if (pos != 0) {
742 ret = EFI_UNSUPPORTED;
743 goto error;
744 }
745 fs_closedir(fh->dirs);
746 fh->dirs = NULL;
747 }
748
749 if (pos == ~0ULL) {
750 loff_t file_size;
751
Heinrich Schuchardt9bb62fa2019-09-07 23:28:04 +0200752 ret = efi_get_file_size(fh, &file_size);
753 if (ret != EFI_SUCCESS)
Rob Clark2a920802017-09-13 18:05:34 -0400754 goto error;
Rob Clark2a920802017-09-13 18:05:34 -0400755 pos = file_size;
756 }
757
758 fh->offset = pos;
759
760error:
761 return EFI_EXIT(ret);
762}
763
764static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
Heinrich Schuchardt9c9021e2018-04-04 15:42:09 +0200765 const efi_guid_t *info_type,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200766 efi_uintn_t *buffer_size,
767 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400768{
769 struct file_handle *fh = to_fh(file);
770 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt87c48402019-09-07 21:05:45 +0200771 u16 *dst;
Rob Clark2a920802017-09-13 18:05:34 -0400772
Heinrich Schuchardt008fb482018-09-14 22:46:34 +0200773 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
Rob Clark2a920802017-09-13 18:05:34 -0400774
Heinrich Schuchardt11335c02019-09-08 10:45:31 +0200775 if (!file || !info_type || !buffer_size ||
776 (*buffer_size && !buffer)) {
777 ret = EFI_INVALID_PARAMETER;
778 goto error;
779 }
780
Rob Clark2a920802017-09-13 18:05:34 -0400781 if (!guidcmp(info_type, &efi_file_info_guid)) {
782 struct efi_file_info *info = buffer;
783 char *filename = basename(fh);
784 unsigned int required_size;
785 loff_t file_size;
786
787 /* check buffer size: */
Heinrich Schuchardt87c48402019-09-07 21:05:45 +0200788 required_size = sizeof(*info) +
789 2 * (utf8_utf16_strlen(filename) + 1);
Rob Clark2a920802017-09-13 18:05:34 -0400790 if (*buffer_size < required_size) {
791 *buffer_size = required_size;
792 ret = EFI_BUFFER_TOO_SMALL;
793 goto error;
794 }
795
Heinrich Schuchardt9bb62fa2019-09-07 23:28:04 +0200796 ret = efi_get_file_size(fh, &file_size);
797 if (ret != EFI_SUCCESS)
Rob Clark2a920802017-09-13 18:05:34 -0400798 goto error;
Rob Clark2a920802017-09-13 18:05:34 -0400799
800 memset(info, 0, required_size);
801
802 info->size = required_size;
803 info->file_size = file_size;
804 info->physical_size = file_size;
805
806 if (fh->isdir)
807 info->attribute |= EFI_FILE_DIRECTORY;
808
Heinrich Schuchardt87c48402019-09-07 21:05:45 +0200809 dst = info->file_name;
810 utf8_utf16_strcpy(&dst, filename);
Heinrich Schuchardt9e6835e2018-04-04 15:42:11 +0200811 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
812 struct efi_file_system_info *info = buffer;
Simon Glass05289792020-05-10 11:39:57 -0600813 struct disk_partition part;
Heinrich Schuchardt9e6835e2018-04-04 15:42:11 +0200814 efi_uintn_t required_size;
815 int r;
816
817 if (fh->fs->part >= 1)
818 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
819 else
820 r = part_get_info_whole_disk(fh->fs->desc, &part);
821 if (r < 0) {
822 ret = EFI_DEVICE_ERROR;
823 goto error;
824 }
Heinrich Schuchardt632834c2019-09-08 10:32:54 +0200825 required_size = sizeof(*info) + 2;
Heinrich Schuchardt9e6835e2018-04-04 15:42:11 +0200826 if (*buffer_size < required_size) {
827 *buffer_size = required_size;
828 ret = EFI_BUFFER_TOO_SMALL;
829 goto error;
830 }
831
832 memset(info, 0, required_size);
833
834 info->size = required_size;
Heinrich Schuchardt57014722019-12-08 10:02:37 +0100835 /*
836 * TODO: We cannot determine if the volume can be written to.
837 */
838 info->read_only = false;
Heinrich Schuchardt9e6835e2018-04-04 15:42:11 +0200839 info->volume_size = part.size * part.blksz;
Heinrich Schuchardt57014722019-12-08 10:02:37 +0100840 /*
841 * TODO: We currently have no function to determine the free
842 * space. The volume size is the best upper bound we have.
843 */
844 info->free_space = info->volume_size;
Heinrich Schuchardt9e6835e2018-04-04 15:42:11 +0200845 info->block_size = part.blksz;
846 /*
847 * TODO: The volume label is not available in U-Boot.
Heinrich Schuchardt9e6835e2018-04-04 15:42:11 +0200848 */
Heinrich Schuchardt632834c2019-09-08 10:32:54 +0200849 info->volume_label[0] = 0;
850 } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
851 if (*buffer_size < 2) {
852 *buffer_size = 2;
853 ret = EFI_BUFFER_TOO_SMALL;
854 goto error;
855 }
856 *(u16 *)buffer = 0;
Rob Clark2a920802017-09-13 18:05:34 -0400857 } else {
858 ret = EFI_UNSUPPORTED;
859 }
860
861error:
862 return EFI_EXIT(ret);
863}
864
865static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
Heinrich Schuchardt9c9021e2018-04-04 15:42:09 +0200866 const efi_guid_t *info_type,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200867 efi_uintn_t buffer_size,
868 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400869{
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200870 struct file_handle *fh = to_fh(file);
871 efi_status_t ret = EFI_UNSUPPORTED;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200872
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200873 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
874
875 if (!guidcmp(info_type, &efi_file_info_guid)) {
876 struct efi_file_info *info = (struct efi_file_info *)buffer;
877 char *filename = basename(fh);
878 char *new_file_name, *pos;
879 loff_t file_size;
880
Heinrich Schuchardt82625782019-09-08 11:37:07 +0200881 /* The buffer will always contain a file name. */
882 if (buffer_size < sizeof(struct efi_file_info) + 2 ||
883 buffer_size < info->size) {
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200884 ret = EFI_BAD_BUFFER_SIZE;
885 goto out;
886 }
887 /* We cannot change the directory attribute */
888 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
889 ret = EFI_ACCESS_DENIED;
890 goto out;
891 }
892 /* Check for renaming */
Heinrich Schuchardt792aee12020-11-10 07:24:16 +0100893 new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1);
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200894 if (!new_file_name) {
895 ret = EFI_OUT_OF_RESOURCES;
896 goto out;
897 }
898 pos = new_file_name;
899 utf16_utf8_strcpy(&pos, info->file_name);
900 if (strcmp(new_file_name, filename)) {
901 /* TODO: we do not support renaming */
902 EFI_PRINT("Renaming not supported\n");
903 free(new_file_name);
904 ret = EFI_ACCESS_DENIED;
905 goto out;
906 }
907 free(new_file_name);
908 /* Check for truncation */
Heinrich Schuchardt9bb62fa2019-09-07 23:28:04 +0200909 ret = efi_get_file_size(fh, &file_size);
910 if (ret != EFI_SUCCESS)
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200911 goto out;
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200912 if (file_size != info->file_size) {
913 /* TODO: we do not support truncation */
914 EFI_PRINT("Truncation not supported\n");
915 ret = EFI_ACCESS_DENIED;
916 goto out;
917 }
918 /*
919 * We do not care for the other attributes
920 * TODO: Support read only
921 */
922 ret = EFI_SUCCESS;
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200923 } else {
Heinrich Schuchardt82625782019-09-08 11:37:07 +0200924 /* TODO: We do not support changing the volume label */
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200925 ret = EFI_UNSUPPORTED;
926 }
927out:
928 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400929}
930
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100931/**
932 * efi_file_flush_int() - flush file
933 *
934 * This is the internal implementation of the Flush() and FlushEx() services of
935 * the EFI_FILE_PROTOCOL.
936 *
937 * @this: file protocol instance
938 * Return: status code
939 */
940static efi_status_t efi_file_flush_int(struct efi_file_handle *this)
Rob Clark2a920802017-09-13 18:05:34 -0400941{
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100942 struct file_handle *fh = to_fh(this);
943
944 if (!this)
945 return EFI_INVALID_PARAMETER;
946
947 if (!(fh->open_mode & EFI_FILE_MODE_WRITE))
948 return EFI_ACCESS_DENIED;
949
950 /* TODO: flush for file position after end of file */
951 return EFI_SUCCESS;
Rob Clark2a920802017-09-13 18:05:34 -0400952}
953
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100954/**
955 * efi_file_flush() - flush file
956 *
957 * This function implements the Flush() service of the EFI_FILE_PROTOCOL.
958 *
959 * See the Unified Extensible Firmware Interface (UEFI) specification for
960 * details.
961 *
962 * @this: file protocol instance
963 * Return: status code
964 */
965static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *this)
Heinrich Schuchardte692ed12019-09-08 09:35:32 +0200966{
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100967 efi_status_t ret;
968
969 EFI_ENTRY("%p", this);
970
971 ret = efi_file_flush_int(this);
972
973 return EFI_EXIT(ret);
Heinrich Schuchardte692ed12019-09-08 09:35:32 +0200974}
975
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100976/**
977 * efi_file_flush_ex() - flush file
978 *
979 * This function implements the FlushEx() service of the EFI_FILE_PROTOCOL.
980 *
981 * See the Unified Extensible Firmware Interface (UEFI) specification for
982 * details.
983 *
984 * @this: file protocol instance
985 * @token: transaction token
986 * Return: status code
987 */
988static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *this,
989 struct efi_file_io_token *token)
Heinrich Schuchardte692ed12019-09-08 09:35:32 +0200990{
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100991 efi_status_t ret;
Heinrich Schuchardte692ed12019-09-08 09:35:32 +0200992
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100993 EFI_ENTRY("%p, %p", this, token);
Heinrich Schuchardte692ed12019-09-08 09:35:32 +0200994
Heinrich Schuchardtdb12f512021-01-01 08:39:43 +0100995 if (!token) {
996 ret = EFI_INVALID_PARAMETER;
997 goto out;
998 }
999
1000 ret = efi_file_flush_int(this);
1001
1002 if (ret == EFI_SUCCESS && token->event) {
1003 token->status = EFI_SUCCESS;
1004 efi_signal_event(token->event);
1005 }
1006
1007out:
1008 return EFI_EXIT(ret);
Heinrich Schuchardte692ed12019-09-08 09:35:32 +02001009}
1010
Rob Clark2a920802017-09-13 18:05:34 -04001011static const struct efi_file_handle efi_file_handle_protocol = {
Heinrich Schuchardte692ed12019-09-08 09:35:32 +02001012 .rev = EFI_FILE_PROTOCOL_REVISION2,
Rob Clark2a920802017-09-13 18:05:34 -04001013 .open = efi_file_open,
1014 .close = efi_file_close,
1015 .delete = efi_file_delete,
1016 .read = efi_file_read,
1017 .write = efi_file_write,
1018 .getpos = efi_file_getpos,
1019 .setpos = efi_file_setpos,
1020 .getinfo = efi_file_getinfo,
1021 .setinfo = efi_file_setinfo,
1022 .flush = efi_file_flush,
Heinrich Schuchardte692ed12019-09-08 09:35:32 +02001023 .open_ex = efi_file_open_ex,
1024 .read_ex = efi_file_read_ex,
1025 .write_ex = efi_file_write_ex,
1026 .flush_ex = efi_file_flush_ex,
Rob Clark2a920802017-09-13 18:05:34 -04001027};
1028
Heinrich Schuchardt95288b12019-02-04 21:24:35 +01001029/**
1030 * efi_file_from_path() - open file via device path
1031 *
1032 * @fp: device path
1033 * @return: EFI_FILE_PROTOCOL for the file or NULL
1034 */
Rob Clark2a920802017-09-13 18:05:34 -04001035struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
1036{
1037 struct efi_simple_file_system_protocol *v;
1038 struct efi_file_handle *f;
1039 efi_status_t ret;
1040
1041 v = efi_fs_from_path(fp);
1042 if (!v)
1043 return NULL;
1044
1045 EFI_CALL(ret = v->open_volume(v, &f));
1046 if (ret != EFI_SUCCESS)
1047 return NULL;
1048
Heinrich Schuchardt95288b12019-02-04 21:24:35 +01001049 /* Skip over device-path nodes before the file path. */
Rob Clark2a920802017-09-13 18:05:34 -04001050 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
1051 fp = efi_dp_next(fp);
1052
Heinrich Schuchardt95288b12019-02-04 21:24:35 +01001053 /*
1054 * Step through the nodes of the directory path until the actual file
1055 * node is reached which is the final node in the device path.
1056 */
Rob Clark2a920802017-09-13 18:05:34 -04001057 while (fp) {
1058 struct efi_device_path_file_path *fdp =
1059 container_of(fp, struct efi_device_path_file_path, dp);
1060 struct efi_file_handle *f2;
Heinrich Schuchardtf62be162019-07-14 20:14:46 +02001061 u16 *filename;
Rob Clark2a920802017-09-13 18:05:34 -04001062
1063 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
1064 printf("bad file path!\n");
1065 f->close(f);
1066 return NULL;
1067 }
1068
Heinrich Schuchardtf62be162019-07-14 20:14:46 +02001069 filename = u16_strdup(fdp->str);
1070 if (!filename)
1071 return NULL;
1072 EFI_CALL(ret = f->open(f, &f2, filename,
Rob Clark2a920802017-09-13 18:05:34 -04001073 EFI_FILE_MODE_READ, 0));
Heinrich Schuchardtf62be162019-07-14 20:14:46 +02001074 free(filename);
Rob Clark2a920802017-09-13 18:05:34 -04001075 if (ret != EFI_SUCCESS)
1076 return NULL;
1077
1078 fp = efi_dp_next(fp);
1079
1080 EFI_CALL(f->close(f));
1081 f = f2;
1082 }
1083
1084 return f;
1085}
1086
1087static efi_status_t EFIAPI
1088efi_open_volume(struct efi_simple_file_system_protocol *this,
1089 struct efi_file_handle **root)
1090{
1091 struct file_system *fs = to_fs(this);
1092
1093 EFI_ENTRY("%p, %p", this, root);
1094
AKASHI Takahiro5bc84a12018-09-11 15:59:12 +09001095 *root = file_open(fs, NULL, NULL, 0, 0);
Rob Clark2a920802017-09-13 18:05:34 -04001096
1097 return EFI_EXIT(EFI_SUCCESS);
1098}
1099
1100struct efi_simple_file_system_protocol *
1101efi_simple_file_system(struct blk_desc *desc, int part,
1102 struct efi_device_path *dp)
1103{
1104 struct file_system *fs;
1105
1106 fs = calloc(1, sizeof(*fs));
1107 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
1108 fs->base.open_volume = efi_open_volume;
1109 fs->desc = desc;
1110 fs->part = part;
1111 fs->dp = dp;
1112
1113 return &fs->base;
1114}