blob: 7d81da8f2d813a0d7aec2d8583caf7074f78042e [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
Heinrich Schuchardt955a3212025-01-16 20:26:59 +01008#define LOG_CATEGORY LOGC_EFI
9
Rob Clark53142032017-09-13 18:05:34 -040010#include <charset.h>
11#include <efi_loader.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Rob Clark53142032017-09-13 18:05:34 -040013#include <malloc.h>
Alexander Grafab133c72018-08-08 03:54:32 -060014#include <mapmem.h>
Rob Clark53142032017-09-13 18:05:34 -040015#include <fs.h>
Simon Glass655306c2020-05-10 11:39:58 -060016#include <part.h>
Rob Clark53142032017-09-13 18:05:34 -040017
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +020018/* GUID for file system information */
19const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
20
Heinrich Schuchardt0c236c42019-09-08 10:32:54 +020021/* GUID to obtain the volume label */
22const efi_guid_t efi_system_volume_label_id = EFI_FILE_SYSTEM_VOLUME_LABEL_ID;
23
Rob Clark53142032017-09-13 18:05:34 -040024struct file_system {
25 struct efi_simple_file_system_protocol base;
26 struct efi_device_path *dp;
27 struct blk_desc *desc;
28 int part;
29};
30#define to_fs(x) container_of(x, struct file_system, base)
31
32struct file_handle {
33 struct efi_file_handle base;
34 struct file_system *fs;
35 loff_t offset; /* current file position/cursor */
36 int isdir;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +020037 u64 open_mode;
Rob Clark53142032017-09-13 18:05:34 -040038
39 /* for reading a directory: */
40 struct fs_dir_stream *dirs;
41 struct fs_dirent *dent;
42
Gabriel Dalimonte7823d752025-02-17 13:26:46 -050043 char *path;
Rob Clark53142032017-09-13 18:05:34 -040044};
45#define to_fh(x) container_of(x, struct file_handle, base)
46
47static const struct efi_file_handle efi_file_handle_protocol;
48
49static char *basename(struct file_handle *fh)
50{
51 char *s = strrchr(fh->path, '/');
52 if (s)
53 return s + 1;
54 return fh->path;
55}
56
57static int set_blk_dev(struct file_handle *fh)
58{
59 return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
60}
61
Heinrich Schuchardt80f834d2018-10-02 05:57:32 +020062/**
63 * is_dir() - check if file handle points to directory
64 *
65 * We assume that set_blk_dev(fh) has been called already.
66 *
67 * @fh: file handle
68 * Return: true if file handle points to a directory
69 */
Rob Clark53142032017-09-13 18:05:34 -040070static int is_dir(struct file_handle *fh)
71{
72 struct fs_dir_stream *dirs;
73
Rob Clark53142032017-09-13 18:05:34 -040074 dirs = fs_opendir(fh->path);
75 if (!dirs)
76 return 0;
77
78 fs_closedir(dirs);
79
80 return 1;
81}
82
83/*
84 * Normalize a path which may include either back or fwd slashes,
85 * double slashes, . or .. entries in the path, etc.
86 */
87static int sanitize_path(char *path)
88{
89 char *p;
90
91 /* backslash to slash: */
92 p = path;
93 while ((p = strchr(p, '\\')))
94 *p++ = '/';
95
96 /* handle double-slashes: */
97 p = path;
98 while ((p = strstr(p, "//"))) {
99 char *src = p + 1;
100 memmove(p, src, strlen(src) + 1);
101 }
102
103 /* handle extra /.'s */
104 p = path;
105 while ((p = strstr(p, "/."))) {
106 /*
107 * You'd be tempted to do this *after* handling ".."s
108 * below to avoid having to check if "/." is start of
109 * a "/..", but that won't have the correct results..
110 * for example, "/foo/./../bar" would get resolved to
111 * "/foo/bar" if you did these two passes in the other
112 * order
113 */
114 if (p[2] == '.') {
115 p += 2;
116 continue;
117 }
118 char *src = p + 2;
119 memmove(p, src, strlen(src) + 1);
120 }
121
122 /* handle extra /..'s: */
123 p = path;
124 while ((p = strstr(p, "/.."))) {
125 char *src = p + 3;
126
127 p--;
128
129 /* find beginning of previous path entry: */
130 while (true) {
131 if (p < path)
132 return -1;
133 if (*p == '/')
134 break;
135 p--;
136 }
137
138 memmove(p, src, strlen(src) + 1);
139 }
140
141 return 0;
142}
143
Heinrich Schuchardt1af423b2018-09-12 19:00:02 +0200144/**
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200145 * efi_create_file() - create file or directory
146 *
147 * @fh: file handle
148 * @attributes: attributes for newly created file
149 * Returns: 0 for success
150 */
151static int efi_create_file(struct file_handle *fh, u64 attributes)
152{
153 loff_t actwrite;
154 void *buffer = &actwrite;
155
156 if (attributes & EFI_FILE_DIRECTORY)
157 return fs_mkdir(fh->path);
158 else
159 return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
160 &actwrite);
161}
162
163/**
Heinrich Schuchardt1af423b2018-09-12 19:00:02 +0200164 * file_open() - open a file handle
165 *
166 * @fs: file system
167 * @parent: directory relative to which the file is to be opened
168 * @file_name: path of the file to be opened. '\', '.', or '..' may
169 * be used as modifiers. A leading backslash indicates an
170 * absolute path.
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200171 * @open_mode: bit mask indicating the access mode (read, write,
Heinrich Schuchardt1af423b2018-09-12 19:00:02 +0200172 * create)
173 * @attributes: attributes for newly created file
174 * Returns: handle to the opened file or NULL
Rob Clark53142032017-09-13 18:05:34 -0400175 */
176static struct efi_file_handle *file_open(struct file_system *fs,
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200177 struct file_handle *parent, u16 *file_name, u64 open_mode,
AKASHI Takahiro0c672b02018-09-11 15:59:12 +0900178 u64 attributes)
Rob Clark53142032017-09-13 18:05:34 -0400179{
180 struct file_handle *fh;
Gabriel Dalimonte7823d752025-02-17 13:26:46 -0500181 char *path;
Rob Clark53142032017-09-13 18:05:34 -0400182 char f0[MAX_UTF8_PER_UTF16] = {0};
183 int plen = 0;
184 int flen = 0;
185
186 if (file_name) {
Heinrich Schuchardtdb47c342019-01-12 12:02:33 +0100187 utf16_to_utf8((u8 *)f0, file_name, 1);
188 flen = u16_strlen(file_name);
Rob Clark53142032017-09-13 18:05:34 -0400189 }
190
191 /* we could have a parent, but also an absolute path: */
192 if (f0[0] == '\\') {
193 plen = 0;
194 } else if (parent) {
195 plen = strlen(parent->path) + 1;
196 }
197
Gabriel Dalimonte7823d752025-02-17 13:26:46 -0500198 fh = calloc(1, sizeof(*fh));
Rob Clark53142032017-09-13 18:05:34 -0400199 /* +2 is for null and '/' */
Gabriel Dalimonte7823d752025-02-17 13:26:46 -0500200 path = calloc(1, plen + (flen * MAX_UTF8_PER_UTF16) + 2);
201 if (!fh || !path)
202 goto error;
Rob Clark53142032017-09-13 18:05:34 -0400203
Gabriel Dalimonte7823d752025-02-17 13:26:46 -0500204 fh->path = path;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200205 fh->open_mode = open_mode;
Rob Clark53142032017-09-13 18:05:34 -0400206 fh->base = efi_file_handle_protocol;
207 fh->fs = fs;
208
209 if (parent) {
210 char *p = fh->path;
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200211 int exists;
Rob Clark53142032017-09-13 18:05:34 -0400212
213 if (plen > 0) {
214 strcpy(p, parent->path);
215 p += plen - 1;
216 *p++ = '/';
217 }
218
Heinrich Schuchardtdb47c342019-01-12 12:02:33 +0100219 utf16_to_utf8((u8 *)p, file_name, flen);
Rob Clark53142032017-09-13 18:05:34 -0400220
221 if (sanitize_path(fh->path))
222 goto error;
223
224 /* check if file exists: */
225 if (set_blk_dev(fh))
226 goto error;
227
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200228 exists = fs_exists(fh->path);
Heinrich Schuchardt3ab87ac2019-02-09 22:23:48 +0100229 /* fs_exists() calls fs_close(), so open file system again */
230 if (set_blk_dev(fh))
231 goto error;
232
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200233 if (!exists) {
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200234 if (!(open_mode & EFI_FILE_MODE_CREATE) ||
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200235 efi_create_file(fh, attributes))
236 goto error;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200237 if (set_blk_dev(fh))
238 goto error;
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200239 }
240
Rob Clark53142032017-09-13 18:05:34 -0400241 /* figure out if file is a directory: */
242 fh->isdir = is_dir(fh);
243 } else {
244 fh->isdir = 1;
245 strcpy(fh->path, "");
246 }
247
248 return &fh->base;
249
250error:
Gabriel Dalimonte7823d752025-02-17 13:26:46 -0500251 free(fh->path);
Rob Clark53142032017-09-13 18:05:34 -0400252 free(fh);
253 return NULL;
254}
255
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900256efi_status_t efi_file_open_int(struct efi_file_handle *this,
257 struct efi_file_handle **new_handle,
258 u16 *file_name, u64 open_mode,
259 u64 attributes)
Rob Clark53142032017-09-13 18:05:34 -0400260{
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100261 struct file_handle *fh = to_fh(this);
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200262 efi_status_t ret;
Rob Clark53142032017-09-13 18:05:34 -0400263
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200264 /* Check parameters */
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100265 if (!this || !new_handle || !file_name) {
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200266 ret = EFI_INVALID_PARAMETER;
267 goto out;
268 }
269 if (open_mode != EFI_FILE_MODE_READ &&
270 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
271 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
272 EFI_FILE_MODE_CREATE)) {
273 ret = EFI_INVALID_PARAMETER;
274 goto out;
275 }
Heinrich Schuchardt662d0be2018-09-13 21:31:49 +0200276 /*
277 * The UEFI spec requires that attributes are only set in create mode.
278 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
279 * read mode. EDK2 does not check that attributes are zero if not in
280 * create mode.
281 *
282 * So here we only check attributes in create mode and do not check
283 * that they are zero otherwise.
284 */
285 if ((open_mode & EFI_FILE_MODE_CREATE) &&
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200286 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
287 ret = EFI_INVALID_PARAMETER;
288 goto out;
289 }
Rob Clark53142032017-09-13 18:05:34 -0400290
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200291 /* Open file */
292 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200293 if (*new_handle) {
294 EFI_PRINT("file handle %p\n", *new_handle);
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200295 ret = EFI_SUCCESS;
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200296 } else {
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200297 ret = EFI_NOT_FOUND;
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200298 }
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100299out:
300 return ret;
301}
302
303/**
Heinrich Schuchardtb0102502024-09-18 23:37:28 +0200304 * efi_file_open() - open file synchronously
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100305 *
306 * This function implements the Open service of the File Protocol.
307 * See the UEFI spec for details.
308 *
309 * @this: EFI_FILE_PROTOCOL instance
310 * @new_handle: on return pointer to file handle
311 * @file_name: file name
312 * @open_mode: mode to open the file (read, read/write, create/read/write)
313 * @attributes: attributes for newly created file
314 */
315static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *this,
316 struct efi_file_handle **new_handle,
317 u16 *file_name, u64 open_mode,
318 u64 attributes)
319{
320 efi_status_t ret;
321
322 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", this, new_handle,
323 file_name, open_mode, attributes);
324
325 ret = efi_file_open_int(this, new_handle, file_name, open_mode,
326 attributes);
327
328 return EFI_EXIT(ret);
329}
330
331/**
332 * efi_file_open_ex() - open file asynchronously
333 *
334 * This function implements the OpenEx service of the File Protocol.
335 * See the UEFI spec for details.
336 *
337 * @this: EFI_FILE_PROTOCOL instance
338 * @new_handle: on return pointer to file handle
339 * @file_name: file name
340 * @open_mode: mode to open the file (read, read/write, create/read/write)
341 * @attributes: attributes for newly created file
342 * @token: transaction token
343 */
344static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *this,
345 struct efi_file_handle **new_handle,
346 u16 *file_name, u64 open_mode,
347 u64 attributes,
348 struct efi_file_io_token *token)
349{
350 efi_status_t ret;
351
352 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu, %p", this, new_handle,
353 file_name, open_mode, attributes, token);
354
355 if (!token) {
356 ret = EFI_INVALID_PARAMETER;
357 goto out;
358 }
359
360 ret = efi_file_open_int(this, new_handle, file_name, open_mode,
361 attributes);
362
363 if (ret == EFI_SUCCESS && token->event) {
364 token->status = EFI_SUCCESS;
365 efi_signal_event(token->event);
366 }
367
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200368out:
369 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400370}
371
372static efi_status_t file_close(struct file_handle *fh)
373{
374 fs_closedir(fh->dirs);
Gabriel Dalimonte7823d752025-02-17 13:26:46 -0500375 free(fh->path);
Rob Clark53142032017-09-13 18:05:34 -0400376 free(fh);
377 return EFI_SUCCESS;
378}
379
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900380efi_status_t efi_file_close_int(struct efi_file_handle *file)
Rob Clark53142032017-09-13 18:05:34 -0400381{
382 struct file_handle *fh = to_fh(file);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900383
384 return file_close(fh);
385}
386
387static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
388{
Rob Clark53142032017-09-13 18:05:34 -0400389 EFI_ENTRY("%p", file);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900390 return EFI_EXIT(efi_file_close_int(file));
Rob Clark53142032017-09-13 18:05:34 -0400391}
392
393static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
394{
395 struct file_handle *fh = to_fh(file);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900396 efi_status_t ret = EFI_SUCCESS;
397
Rob Clark53142032017-09-13 18:05:34 -0400398 EFI_ENTRY("%p", file);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900399
Heinrich Schuchardt646faf32019-06-17 22:00:13 +0200400 if (set_blk_dev(fh) || fs_unlink(fh->path))
401 ret = EFI_WARN_DELETE_FAILURE;
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900402
Rob Clark53142032017-09-13 18:05:34 -0400403 file_close(fh);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900404 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400405}
406
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200407/**
408 * efi_get_file_size() - determine the size of a file
409 *
410 * @fh: file handle
411 * @file_size: pointer to receive file size
412 * Return: status code
413 */
414static efi_status_t efi_get_file_size(struct file_handle *fh,
415 loff_t *file_size)
416{
417 if (set_blk_dev(fh))
418 return EFI_DEVICE_ERROR;
419
420 if (fs_size(fh->path, file_size))
421 return EFI_DEVICE_ERROR;
422
423 return EFI_SUCCESS;
424}
425
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +0200426/**
427 * efi_file_size() - Get the size of a file using an EFI file handle
428 *
429 * @fh: EFI file handle
430 * @size: buffer to fill in the discovered size
431 *
432 * Return: size of the file
433 */
434efi_status_t efi_file_size(struct efi_file_handle *fh, efi_uintn_t *size)
435{
436 struct efi_file_info *info = NULL;
437 efi_uintn_t bs = 0;
438 efi_status_t ret;
439
440 *size = 0;
441 ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
442 info));
443 if (ret != EFI_BUFFER_TOO_SMALL) {
444 ret = EFI_DEVICE_ERROR;
445 goto out;
446 }
447
448 info = malloc(bs);
449 if (!info) {
450 ret = EFI_OUT_OF_RESOURCES;
451 goto out;
452 }
453 ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
454 info));
455 if (ret != EFI_SUCCESS)
456 goto out;
457
458 *size = info->file_size;
459
460out:
461 free(info);
462 return ret;
463}
464
Rob Clark53142032017-09-13 18:05:34 -0400465static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
466 void *buffer)
467{
468 loff_t actread;
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200469 efi_status_t ret;
470 loff_t file_size;
Rob Clark53142032017-09-13 18:05:34 -0400471
Stefan Sørensen6b398d02020-07-22 09:43:31 +0200472 if (!buffer) {
473 ret = EFI_INVALID_PARAMETER;
474 return ret;
475 }
476
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200477 ret = efi_get_file_size(fh, &file_size);
478 if (ret != EFI_SUCCESS)
479 return ret;
480 if (file_size < fh->offset) {
481 ret = EFI_DEVICE_ERROR;
482 return ret;
483 }
484
485 if (set_blk_dev(fh))
486 return EFI_DEVICE_ERROR;
Simon Glass849688f2018-09-15 00:51:00 -0600487 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
Rob Clark53142032017-09-13 18:05:34 -0400488 *buffer_size, &actread))
489 return EFI_DEVICE_ERROR;
490
491 *buffer_size = actread;
492 fh->offset += actread;
493
494 return EFI_SUCCESS;
495}
496
Heinrich Schuchardt69f722b2021-05-15 22:41:26 +0200497static void rtc2efi(struct efi_time *time, struct rtc_time *tm)
498{
499 memset(time, 0, sizeof(struct efi_time));
500 time->year = tm->tm_year;
501 time->month = tm->tm_mon;
502 time->day = tm->tm_mday;
503 time->hour = tm->tm_hour;
504 time->minute = tm->tm_min;
505 time->second = tm->tm_sec;
506}
507
Rob Clark53142032017-09-13 18:05:34 -0400508static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
509 void *buffer)
510{
511 struct efi_file_info *info = buffer;
512 struct fs_dirent *dent;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200513 u64 required_size;
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200514 u16 *dst;
Rob Clark53142032017-09-13 18:05:34 -0400515
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200516 if (set_blk_dev(fh))
517 return EFI_DEVICE_ERROR;
518
Rob Clark53142032017-09-13 18:05:34 -0400519 if (!fh->dirs) {
520 assert(fh->offset == 0);
521 fh->dirs = fs_opendir(fh->path);
522 if (!fh->dirs)
523 return EFI_DEVICE_ERROR;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200524 fh->dent = NULL;
Rob Clark53142032017-09-13 18:05:34 -0400525 }
526
527 /*
528 * So this is a bit awkward. Since fs layer is stateful and we
529 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
530 * we might have to return without consuming the dent.. so we
531 * have to stash it for next call.
532 */
533 if (fh->dent) {
534 dent = fh->dent;
Rob Clark53142032017-09-13 18:05:34 -0400535 } else {
536 dent = fs_readdir(fh->dirs);
537 }
538
Rob Clark53142032017-09-13 18:05:34 -0400539 if (!dent) {
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200540 /* no more files in directory */
541 *buffer_size = 0;
Rob Clark53142032017-09-13 18:05:34 -0400542 return EFI_SUCCESS;
543 }
544
545 /* check buffer size: */
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200546 required_size = sizeof(*info) +
547 2 * (utf8_utf16_strlen(dent->name) + 1);
Rob Clark53142032017-09-13 18:05:34 -0400548 if (*buffer_size < required_size) {
549 *buffer_size = required_size;
550 fh->dent = dent;
551 return EFI_BUFFER_TOO_SMALL;
552 }
Stefan Sørensen6b398d02020-07-22 09:43:31 +0200553 if (!buffer)
554 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200555 fh->dent = NULL;
Rob Clark53142032017-09-13 18:05:34 -0400556
557 *buffer_size = required_size;
558 memset(info, 0, required_size);
559
560 info->size = required_size;
561 info->file_size = dent->size;
562 info->physical_size = dent->size;
Heinrich Schuchardt69f722b2021-05-15 22:41:26 +0200563 info->attribute = dent->attr;
564 rtc2efi(&info->create_time, &dent->create_time);
565 rtc2efi(&info->modification_time, &dent->change_time);
566 rtc2efi(&info->last_access_time, &dent->access_time);
Rob Clark53142032017-09-13 18:05:34 -0400567
568 if (dent->type == FS_DT_DIR)
569 info->attribute |= EFI_FILE_DIRECTORY;
570
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200571 dst = info->file_name;
572 utf8_utf16_strcpy(&dst, dent->name);
Rob Clark53142032017-09-13 18:05:34 -0400573
574 fh->offset++;
575
576 return EFI_SUCCESS;
577}
578
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900579efi_status_t efi_file_read_int(struct efi_file_handle *this,
580 efi_uintn_t *buffer_size, void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400581{
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100582 struct file_handle *fh = to_fh(this);
Rob Clark53142032017-09-13 18:05:34 -0400583 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200584 u64 bs;
Rob Clark53142032017-09-13 18:05:34 -0400585
Peng Fanca02db22021-04-28 21:54:01 +0800586 if (!this || !buffer_size)
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100587 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200588
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200589 bs = *buffer_size;
Rob Clark53142032017-09-13 18:05:34 -0400590 if (fh->isdir)
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200591 ret = dir_read(fh, &bs, buffer);
Rob Clark53142032017-09-13 18:05:34 -0400592 else
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200593 ret = file_read(fh, &bs, buffer);
594 if (bs <= SIZE_MAX)
595 *buffer_size = bs;
596 else
597 *buffer_size = SIZE_MAX;
Rob Clark53142032017-09-13 18:05:34 -0400598
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100599 return ret;
600}
601
602/**
603 * efi_file_read() - read file
604 *
605 * This function implements the Read() service of the EFI_FILE_PROTOCOL.
606 *
607 * See the Unified Extensible Firmware Interface (UEFI) specification for
608 * details.
609 *
610 * @this: file protocol instance
611 * @buffer_size: number of bytes to read
612 * @buffer: read buffer
613 * Return: status code
614 */
615static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *this,
616 efi_uintn_t *buffer_size, void *buffer)
617{
618 efi_status_t ret;
619
620 EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
621
622 ret = efi_file_read_int(this, buffer_size, buffer);
623
Rob Clark53142032017-09-13 18:05:34 -0400624 return EFI_EXIT(ret);
625}
626
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200627/**
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100628 * efi_file_read_ex() - read file asynchonously
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200629 *
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100630 * This function implements the ReadEx() service of the EFI_FILE_PROTOCOL.
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200631 *
632 * See the Unified Extensible Firmware Interface (UEFI) specification for
633 * details.
634 *
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100635 * @this: file protocol instance
636 * @token: transaction token
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200637 * Return: status code
638 */
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100639static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *this,
640 struct efi_file_io_token *token)
Rob Clark53142032017-09-13 18:05:34 -0400641{
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100642 efi_status_t ret;
643
644 EFI_ENTRY("%p, %p", this, token);
645
646 if (!token) {
647 ret = EFI_INVALID_PARAMETER;
648 goto out;
649 }
650
651 ret = efi_file_read_int(this, &token->buffer_size, token->buffer);
652
653 if (ret == EFI_SUCCESS && token->event) {
654 token->status = EFI_SUCCESS;
655 efi_signal_event(token->event);
656 }
657
658out:
659 return EFI_EXIT(ret);
660}
661
662static efi_status_t efi_file_write_int(struct efi_file_handle *this,
663 efi_uintn_t *buffer_size, void *buffer)
664{
665 struct file_handle *fh = to_fh(this);
Rob Clark53142032017-09-13 18:05:34 -0400666 efi_status_t ret = EFI_SUCCESS;
667 loff_t actwrite;
668
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100669 if (!this || !buffer_size || !buffer) {
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200670 ret = EFI_INVALID_PARAMETER;
671 goto out;
672 }
673 if (fh->isdir) {
674 ret = EFI_UNSUPPORTED;
675 goto out;
676 }
677 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
678 ret = EFI_ACCESS_DENIED;
679 goto out;
680 }
681
682 if (!*buffer_size)
683 goto out;
684
Rob Clark53142032017-09-13 18:05:34 -0400685 if (set_blk_dev(fh)) {
686 ret = EFI_DEVICE_ERROR;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200687 goto out;
Rob Clark53142032017-09-13 18:05:34 -0400688 }
Simon Glass849688f2018-09-15 00:51:00 -0600689 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
Rob Clark53142032017-09-13 18:05:34 -0400690 &actwrite)) {
691 ret = EFI_DEVICE_ERROR;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200692 goto out;
Rob Clark53142032017-09-13 18:05:34 -0400693 }
Rob Clark53142032017-09-13 18:05:34 -0400694 *buffer_size = actwrite;
695 fh->offset += actwrite;
696
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200697out:
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100698 return ret;
699}
700
701/**
702 * efi_file_write() - write to file
703 *
704 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
705 *
706 * See the Unified Extensible Firmware Interface (UEFI) specification for
707 * details.
708 *
709 * @this: file protocol instance
710 * @buffer_size: number of bytes to write
711 * @buffer: buffer with the bytes to write
712 * Return: status code
713 */
714static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *this,
715 efi_uintn_t *buffer_size,
716 void *buffer)
717{
718 efi_status_t ret;
719
720 EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
721
722 ret = efi_file_write_int(this, buffer_size, buffer);
723
Rob Clark53142032017-09-13 18:05:34 -0400724 return EFI_EXIT(ret);
725}
726
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200727/**
Heinrich Schuchardte759b572021-01-01 08:39:43 +0100728 * efi_file_write_ex() - write to file
729 *
730 * This function implements the WriteEx() service of the EFI_FILE_PROTOCOL.
731 *
732 * See the Unified Extensible Firmware Interface (UEFI) specification for
733 * details.
734 *
735 * @this: file protocol instance
736 * @token: transaction token
737 * Return: status code
738 */
739static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *this,
740 struct efi_file_io_token *token)
741{
742 efi_status_t ret;
743
744 EFI_ENTRY("%p, %p", this, token);
745
746 if (!token) {
747 ret = EFI_INVALID_PARAMETER;
748 goto out;
749 }
750
751 ret = efi_file_write_int(this, &token->buffer_size, token->buffer);
752
753 if (ret == EFI_SUCCESS && token->event) {
754 token->status = EFI_SUCCESS;
755 efi_signal_event(token->event);
756 }
757
758out:
759 return EFI_EXIT(ret);
760}
761
762/**
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200763 * efi_file_getpos() - get current position in file
764 *
765 * This function implements the GetPosition service of the EFI file protocol.
766 * See the UEFI spec for details.
767 *
768 * @file: file handle
769 * @pos: pointer to file position
770 * Return: status code
771 */
Rob Clark53142032017-09-13 18:05:34 -0400772static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200773 u64 *pos)
Rob Clark53142032017-09-13 18:05:34 -0400774{
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200775 efi_status_t ret = EFI_SUCCESS;
Rob Clark53142032017-09-13 18:05:34 -0400776 struct file_handle *fh = to_fh(file);
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200777
Rob Clark53142032017-09-13 18:05:34 -0400778 EFI_ENTRY("%p, %p", file, pos);
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200779
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200780 if (fh->isdir) {
781 ret = EFI_UNSUPPORTED;
782 goto out;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200783 }
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200784
785 *pos = fh->offset;
786out:
787 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400788}
789
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900790efi_status_t efi_file_setpos_int(struct efi_file_handle *file, u64 pos)
Rob Clark53142032017-09-13 18:05:34 -0400791{
792 struct file_handle *fh = to_fh(file);
793 efi_status_t ret = EFI_SUCCESS;
794
Rob Clark53142032017-09-13 18:05:34 -0400795 if (fh->isdir) {
796 if (pos != 0) {
797 ret = EFI_UNSUPPORTED;
798 goto error;
799 }
800 fs_closedir(fh->dirs);
801 fh->dirs = NULL;
802 }
803
804 if (pos == ~0ULL) {
805 loff_t file_size;
806
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200807 ret = efi_get_file_size(fh, &file_size);
808 if (ret != EFI_SUCCESS)
Rob Clark53142032017-09-13 18:05:34 -0400809 goto error;
Rob Clark53142032017-09-13 18:05:34 -0400810 pos = file_size;
811 }
812
813 fh->offset = pos;
814
815error:
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900816 return ret;
817}
818
819/**
820 * efi_file_setpos() - set current position in file
821 *
822 * This function implements the SetPosition service of the EFI file protocol.
823 * See the UEFI spec for details.
824 *
825 * @file: file handle
826 * @pos: new file position
827 * Return: status code
828 */
829static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
830 u64 pos)
831{
832 efi_status_t ret = EFI_SUCCESS;
833
834 EFI_ENTRY("%p, %llu", file, pos);
835
836 ret = efi_file_setpos_int(file, pos);
837
Rob Clark53142032017-09-13 18:05:34 -0400838 return EFI_EXIT(ret);
839}
840
841static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
Heinrich Schuchardte86380d2018-04-04 15:42:09 +0200842 const efi_guid_t *info_type,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200843 efi_uintn_t *buffer_size,
844 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400845{
846 struct file_handle *fh = to_fh(file);
847 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200848 u16 *dst;
Rob Clark53142032017-09-13 18:05:34 -0400849
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100850 EFI_ENTRY("%p, %pUs, %p, %p", file, info_type, buffer_size, buffer);
Rob Clark53142032017-09-13 18:05:34 -0400851
Heinrich Schuchardtc540a0e2019-09-08 10:45:31 +0200852 if (!file || !info_type || !buffer_size ||
853 (*buffer_size && !buffer)) {
854 ret = EFI_INVALID_PARAMETER;
855 goto error;
856 }
857
Rob Clark53142032017-09-13 18:05:34 -0400858 if (!guidcmp(info_type, &efi_file_info_guid)) {
859 struct efi_file_info *info = buffer;
860 char *filename = basename(fh);
861 unsigned int required_size;
862 loff_t file_size;
863
864 /* check buffer size: */
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200865 required_size = sizeof(*info) +
866 2 * (utf8_utf16_strlen(filename) + 1);
Rob Clark53142032017-09-13 18:05:34 -0400867 if (*buffer_size < required_size) {
868 *buffer_size = required_size;
869 ret = EFI_BUFFER_TOO_SMALL;
870 goto error;
871 }
872
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200873 ret = efi_get_file_size(fh, &file_size);
Heinrich Schuchardt6e3f70b2024-10-26 08:40:47 +0200874 if (ret != EFI_SUCCESS) {
875 if (!fh->isdir)
876 goto error;
877 /*
878 * Some file drivers don't implement fs_size() for
879 * directories. Use a dummy non-zero value.
880 */
881 file_size = 4096;
882 ret = EFI_SUCCESS;
883 }
Rob Clark53142032017-09-13 18:05:34 -0400884
885 memset(info, 0, required_size);
886
887 info->size = required_size;
888 info->file_size = file_size;
889 info->physical_size = file_size;
890
891 if (fh->isdir)
892 info->attribute |= EFI_FILE_DIRECTORY;
893
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200894 dst = info->file_name;
895 utf8_utf16_strcpy(&dst, filename);
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200896 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
897 struct efi_file_system_info *info = buffer;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600898 struct disk_partition part;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200899 efi_uintn_t required_size;
900 int r;
901
902 if (fh->fs->part >= 1)
903 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
904 else
905 r = part_get_info_whole_disk(fh->fs->desc, &part);
906 if (r < 0) {
907 ret = EFI_DEVICE_ERROR;
908 goto error;
909 }
Heinrich Schuchardt0c236c42019-09-08 10:32:54 +0200910 required_size = sizeof(*info) + 2;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200911 if (*buffer_size < required_size) {
912 *buffer_size = required_size;
913 ret = EFI_BUFFER_TOO_SMALL;
914 goto error;
915 }
916
917 memset(info, 0, required_size);
918
919 info->size = required_size;
Heinrich Schuchardta821b0c2019-12-08 10:02:37 +0100920 /*
921 * TODO: We cannot determine if the volume can be written to.
922 */
923 info->read_only = false;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200924 info->volume_size = part.size * part.blksz;
Heinrich Schuchardta821b0c2019-12-08 10:02:37 +0100925 /*
926 * TODO: We currently have no function to determine the free
927 * space. The volume size is the best upper bound we have.
928 */
929 info->free_space = info->volume_size;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200930 info->block_size = part.blksz;
931 /*
932 * TODO: The volume label is not available in U-Boot.
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200933 */
Heinrich Schuchardt0c236c42019-09-08 10:32:54 +0200934 info->volume_label[0] = 0;
935 } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
936 if (*buffer_size < 2) {
937 *buffer_size = 2;
938 ret = EFI_BUFFER_TOO_SMALL;
939 goto error;
940 }
941 *(u16 *)buffer = 0;
Rob Clark53142032017-09-13 18:05:34 -0400942 } else {
943 ret = EFI_UNSUPPORTED;
944 }
945
946error:
947 return EFI_EXIT(ret);
948}
949
950static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
Heinrich Schuchardte86380d2018-04-04 15:42:09 +0200951 const efi_guid_t *info_type,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200952 efi_uintn_t buffer_size,
953 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400954{
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200955 struct file_handle *fh = to_fh(file);
956 efi_status_t ret = EFI_UNSUPPORTED;
Gabriel Dalimonte7f771752025-02-17 13:26:47 -0500957 char *new_file_name = NULL, *new_path = NULL;
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200958
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100959 EFI_ENTRY("%p, %pUs, %zu, %p", file, info_type, buffer_size, buffer);
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200960
961 if (!guidcmp(info_type, &efi_file_info_guid)) {
962 struct efi_file_info *info = (struct efi_file_info *)buffer;
963 char *filename = basename(fh);
964 char *new_file_name, *pos;
965 loff_t file_size;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200966
Heinrich Schuchardtdd106d02019-09-08 11:37:07 +0200967 /* The buffer will always contain a file name. */
968 if (buffer_size < sizeof(struct efi_file_info) + 2 ||
969 buffer_size < info->size) {
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200970 ret = EFI_BAD_BUFFER_SIZE;
971 goto out;
972 }
973 /* We cannot change the directory attribute */
974 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
975 ret = EFI_ACCESS_DENIED;
976 goto out;
977 }
978 /* Check for renaming */
Heinrich Schuchardt344bc072020-11-10 07:24:16 +0100979 new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1);
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200980 if (!new_file_name) {
981 ret = EFI_OUT_OF_RESOURCES;
982 goto out;
983 }
984 pos = new_file_name;
985 utf16_utf8_strcpy(&pos, info->file_name);
986 if (strcmp(new_file_name, filename)) {
Gabriel Dalimonte7f771752025-02-17 13:26:47 -0500987 int dlen;
988 int rv;
989
990 if (set_blk_dev(fh)) {
991 ret = EFI_DEVICE_ERROR;
992 goto out;
993 }
994 dlen = filename - fh->path;
995 new_path = calloc(1, dlen + strlen(new_file_name) + 1);
996 if (!new_path) {
997 ret = EFI_OUT_OF_RESOURCES;
998 goto out;
999 }
1000 memcpy(new_path, fh->path, dlen);
1001 strcpy(new_path + dlen, new_file_name);
1002 sanitize_path(new_path);
1003 rv = fs_exists(new_path);
1004 if (rv) {
1005 ret = EFI_ACCESS_DENIED;
1006 goto out;
1007 }
1008 /* fs_exists() calls fs_close(), so open file system again */
1009 if (set_blk_dev(fh)) {
1010 ret = EFI_DEVICE_ERROR;
1011 goto out;
1012 }
1013 rv = fs_rename(fh->path, new_path);
1014 if (rv) {
1015 ret = EFI_ACCESS_DENIED;
1016 goto out;
1017 }
1018 free(fh->path);
1019 fh->path = new_path;
1020 /* Prevent new_path from being freed on out */
1021 new_path = NULL;
1022 ret = EFI_SUCCESS;
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +02001023 }
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +02001024 /* Check for truncation */
Heinrich Schuchardt6e3f70b2024-10-26 08:40:47 +02001025 if (!fh->isdir) {
1026 ret = efi_get_file_size(fh, &file_size);
1027 if (ret != EFI_SUCCESS)
1028 goto out;
1029 if (file_size != info->file_size) {
1030 /* TODO: we do not support truncation */
1031 EFI_PRINT("Truncation not supported\n");
1032 ret = EFI_ACCESS_DENIED;
1033 goto out;
1034 }
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +02001035 }
1036 /*
1037 * We do not care for the other attributes
1038 * TODO: Support read only
1039 */
1040 ret = EFI_SUCCESS;
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +02001041 } else {
Heinrich Schuchardtdd106d02019-09-08 11:37:07 +02001042 /* TODO: We do not support changing the volume label */
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +02001043 ret = EFI_UNSUPPORTED;
1044 }
1045out:
Gabriel Dalimonte7f771752025-02-17 13:26:47 -05001046 free(new_path);
1047 free(new_file_name);
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +02001048 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -04001049}
1050
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001051/**
1052 * efi_file_flush_int() - flush file
1053 *
1054 * This is the internal implementation of the Flush() and FlushEx() services of
1055 * the EFI_FILE_PROTOCOL.
1056 *
1057 * @this: file protocol instance
1058 * Return: status code
1059 */
1060static efi_status_t efi_file_flush_int(struct efi_file_handle *this)
Rob Clark53142032017-09-13 18:05:34 -04001061{
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001062 struct file_handle *fh = to_fh(this);
Rob Clark53142032017-09-13 18:05:34 -04001063
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001064 if (!this)
1065 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001066
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001067 if (!(fh->open_mode & EFI_FILE_MODE_WRITE))
1068 return EFI_ACCESS_DENIED;
1069
1070 /* TODO: flush for file position after end of file */
1071 return EFI_SUCCESS;
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001072}
1073
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001074/**
1075 * efi_file_flush() - flush file
1076 *
1077 * This function implements the Flush() service of the EFI_FILE_PROTOCOL.
1078 *
1079 * See the Unified Extensible Firmware Interface (UEFI) specification for
1080 * details.
1081 *
1082 * @this: file protocol instance
1083 * Return: status code
1084 */
1085static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *this)
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001086{
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001087 efi_status_t ret;
1088
1089 EFI_ENTRY("%p", this);
1090
1091 ret = efi_file_flush_int(this);
1092
1093 return EFI_EXIT(ret);
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001094}
1095
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001096/**
1097 * efi_file_flush_ex() - flush file
1098 *
1099 * This function implements the FlushEx() service of the EFI_FILE_PROTOCOL.
1100 *
1101 * See the Unified Extensible Firmware Interface (UEFI) specification for
1102 * details.
1103 *
1104 * @this: file protocol instance
1105 * @token: transaction token
1106 * Return: status code
1107 */
1108static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *this,
1109 struct efi_file_io_token *token)
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001110{
Heinrich Schuchardte759b572021-01-01 08:39:43 +01001111 efi_status_t ret;
1112
1113 EFI_ENTRY("%p, %p", this, token);
1114
1115 if (!token) {
1116 ret = EFI_INVALID_PARAMETER;
1117 goto out;
1118 }
1119
1120 ret = efi_file_flush_int(this);
1121
1122 if (ret == EFI_SUCCESS && token->event) {
1123 token->status = EFI_SUCCESS;
1124 efi_signal_event(token->event);
1125 }
1126
1127out:
1128 return EFI_EXIT(ret);
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001129}
1130
Rob Clark53142032017-09-13 18:05:34 -04001131static const struct efi_file_handle efi_file_handle_protocol = {
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001132 .rev = EFI_FILE_PROTOCOL_REVISION2,
Rob Clark53142032017-09-13 18:05:34 -04001133 .open = efi_file_open,
1134 .close = efi_file_close,
1135 .delete = efi_file_delete,
1136 .read = efi_file_read,
1137 .write = efi_file_write,
1138 .getpos = efi_file_getpos,
1139 .setpos = efi_file_setpos,
1140 .getinfo = efi_file_getinfo,
1141 .setinfo = efi_file_setinfo,
1142 .flush = efi_file_flush,
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +02001143 .open_ex = efi_file_open_ex,
1144 .read_ex = efi_file_read_ex,
1145 .write_ex = efi_file_write_ex,
1146 .flush_ex = efi_file_flush_ex,
Rob Clark53142032017-09-13 18:05:34 -04001147};
1148
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001149/**
1150 * efi_file_from_path() - open file via device path
1151 *
Heinrich Schuchardt4395adb2022-11-10 14:14:06 +01001152 * The device path @fp consists of the device path of the handle with the
1153 * simple file system protocol and one or more file path device path nodes.
1154 * The concatenation of all file path names provides the total file path.
1155 *
1156 * The code starts at the first file path node and tries to open that file or
1157 * directory. If there is a succeding file path node, the code opens it relative
1158 * to this directory and continues iterating until reaching the last file path
1159 * node.
1160 *
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001161 * @fp: device path
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +01001162 * Return: EFI_FILE_PROTOCOL for the file or NULL
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001163 */
Rob Clark53142032017-09-13 18:05:34 -04001164struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
1165{
1166 struct efi_simple_file_system_protocol *v;
1167 struct efi_file_handle *f;
1168 efi_status_t ret;
1169
1170 v = efi_fs_from_path(fp);
1171 if (!v)
1172 return NULL;
1173
1174 EFI_CALL(ret = v->open_volume(v, &f));
1175 if (ret != EFI_SUCCESS)
1176 return NULL;
1177
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001178 /* Skip over device-path nodes before the file path. */
Rob Clark53142032017-09-13 18:05:34 -04001179 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
1180 fp = efi_dp_next(fp);
1181
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +01001182 /*
1183 * Step through the nodes of the directory path until the actual file
1184 * node is reached which is the final node in the device path.
1185 */
Rob Clark53142032017-09-13 18:05:34 -04001186 while (fp) {
1187 struct efi_device_path_file_path *fdp =
1188 container_of(fp, struct efi_device_path_file_path, dp);
1189 struct efi_file_handle *f2;
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +02001190 u16 *filename;
Ilias Apalodimas50279dc2022-11-11 20:04:31 +02001191 size_t filename_sz;
Rob Clark53142032017-09-13 18:05:34 -04001192
1193 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
1194 printf("bad file path!\n");
Ilias Apalodimas08d1af22022-11-11 18:20:37 +02001195 EFI_CALL(f->close(f));
Rob Clark53142032017-09-13 18:05:34 -04001196 return NULL;
1197 }
1198
Ilias Apalodimas9124bc62022-11-10 15:31:30 +02001199 /*
1200 * UEFI specification requires pointers that are passed to
1201 * protocol member functions to be aligned. So memcpy it
1202 * unconditionally
1203 */
Ilias Apalodimas50279dc2022-11-11 20:04:31 +02001204 if (fdp->dp.length <= offsetof(struct efi_device_path_file_path, str))
1205 return NULL;
1206 filename_sz = fdp->dp.length -
1207 offsetof(struct efi_device_path_file_path, str);
1208 filename = malloc(filename_sz);
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +02001209 if (!filename)
1210 return NULL;
Ilias Apalodimas50279dc2022-11-11 20:04:31 +02001211 memcpy(filename, fdp->str, filename_sz);
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +02001212 EFI_CALL(ret = f->open(f, &f2, filename,
Rob Clark53142032017-09-13 18:05:34 -04001213 EFI_FILE_MODE_READ, 0));
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +02001214 free(filename);
Rob Clark53142032017-09-13 18:05:34 -04001215 if (ret != EFI_SUCCESS)
1216 return NULL;
1217
1218 fp = efi_dp_next(fp);
1219
1220 EFI_CALL(f->close(f));
1221 f = f2;
1222 }
1223
1224 return f;
1225}
1226
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001227efi_status_t efi_open_volume_int(struct efi_simple_file_system_protocol *this,
1228 struct efi_file_handle **root)
1229{
1230 struct file_system *fs = to_fs(this);
1231
1232 *root = file_open(fs, NULL, NULL, 0, 0);
1233
1234 return EFI_SUCCESS;
1235}
1236
Rob Clark53142032017-09-13 18:05:34 -04001237static efi_status_t EFIAPI
1238efi_open_volume(struct efi_simple_file_system_protocol *this,
1239 struct efi_file_handle **root)
1240{
Rob Clark53142032017-09-13 18:05:34 -04001241 EFI_ENTRY("%p, %p", this, root);
1242
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001243 return EFI_EXIT(efi_open_volume_int(this, root));
Rob Clark53142032017-09-13 18:05:34 -04001244}
1245
Heinrich Schuchardtf9e8ada2023-07-30 14:03:53 +02001246efi_status_t
1247efi_create_simple_file_system(struct blk_desc *desc, int part,
1248 struct efi_device_path *dp,
1249 struct efi_simple_file_system_protocol **fsp)
Rob Clark53142032017-09-13 18:05:34 -04001250{
1251 struct file_system *fs;
1252
1253 fs = calloc(1, sizeof(*fs));
Heinrich Schuchardtf9e8ada2023-07-30 14:03:53 +02001254 if (!fs)
1255 return EFI_OUT_OF_RESOURCES;
Rob Clark53142032017-09-13 18:05:34 -04001256 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
1257 fs->base.open_volume = efi_open_volume;
1258 fs->desc = desc;
1259 fs->part = part;
1260 fs->dp = dp;
Heinrich Schuchardtf9e8ada2023-07-30 14:03:53 +02001261 *fsp = &fs->base;
Rob Clark53142032017-09-13 18:05:34 -04001262
Heinrich Schuchardtf9e8ada2023-07-30 14:03:53 +02001263 return EFI_SUCCESS;
Rob Clark53142032017-09-13 18:05:34 -04001264}