blob: 19afa69f530bafeed8651bd5991727d138cd10a0 [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
249static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
250 struct efi_file_handle **new_handle,
Heinrich Schuchardtdb47c342019-01-12 12:02:33 +0100251 u16 *file_name, u64 open_mode, u64 attributes)
Rob Clark53142032017-09-13 18:05:34 -0400252{
253 struct file_handle *fh = to_fh(file);
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200254 efi_status_t ret;
Rob Clark53142032017-09-13 18:05:34 -0400255
Simon Glass60209e62019-01-07 16:44:18 -0700256 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
Heinrich Schuchardt9de5c4c2019-03-19 19:16:23 +0100257 file_name, open_mode, attributes);
Rob Clark53142032017-09-13 18:05:34 -0400258
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200259 /* Check parameters */
Heinrich Schuchardtb63f9fc2018-09-15 20:43:46 +0200260 if (!file || !new_handle || !file_name) {
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200261 ret = EFI_INVALID_PARAMETER;
262 goto out;
263 }
264 if (open_mode != EFI_FILE_MODE_READ &&
265 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
266 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
267 EFI_FILE_MODE_CREATE)) {
268 ret = EFI_INVALID_PARAMETER;
269 goto out;
270 }
Heinrich Schuchardt662d0be2018-09-13 21:31:49 +0200271 /*
272 * The UEFI spec requires that attributes are only set in create mode.
273 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
274 * read mode. EDK2 does not check that attributes are zero if not in
275 * create mode.
276 *
277 * So here we only check attributes in create mode and do not check
278 * that they are zero otherwise.
279 */
280 if ((open_mode & EFI_FILE_MODE_CREATE) &&
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200281 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
282 ret = EFI_INVALID_PARAMETER;
283 goto out;
284 }
Rob Clark53142032017-09-13 18:05:34 -0400285
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200286 /* Open file */
287 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200288 if (*new_handle) {
289 EFI_PRINT("file handle %p\n", *new_handle);
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200290 ret = EFI_SUCCESS;
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200291 } else {
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200292 ret = EFI_NOT_FOUND;
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200293 }
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200294out:
295 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400296}
297
298static efi_status_t file_close(struct file_handle *fh)
299{
300 fs_closedir(fh->dirs);
301 free(fh);
302 return EFI_SUCCESS;
303}
304
305static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
306{
307 struct file_handle *fh = to_fh(file);
308 EFI_ENTRY("%p", file);
309 return EFI_EXIT(file_close(fh));
310}
311
312static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
313{
314 struct file_handle *fh = to_fh(file);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900315 efi_status_t ret = EFI_SUCCESS;
316
Rob Clark53142032017-09-13 18:05:34 -0400317 EFI_ENTRY("%p", file);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900318
Heinrich Schuchardt646faf32019-06-17 22:00:13 +0200319 if (set_blk_dev(fh) || fs_unlink(fh->path))
320 ret = EFI_WARN_DELETE_FAILURE;
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900321
Rob Clark53142032017-09-13 18:05:34 -0400322 file_close(fh);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900323 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400324}
325
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200326/**
327 * efi_get_file_size() - determine the size of a file
328 *
329 * @fh: file handle
330 * @file_size: pointer to receive file size
331 * Return: status code
332 */
333static efi_status_t efi_get_file_size(struct file_handle *fh,
334 loff_t *file_size)
335{
336 if (set_blk_dev(fh))
337 return EFI_DEVICE_ERROR;
338
339 if (fs_size(fh->path, file_size))
340 return EFI_DEVICE_ERROR;
341
342 return EFI_SUCCESS;
343}
344
Rob Clark53142032017-09-13 18:05:34 -0400345static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
346 void *buffer)
347{
348 loff_t actread;
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200349 efi_status_t ret;
350 loff_t file_size;
Rob Clark53142032017-09-13 18:05:34 -0400351
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200352 ret = efi_get_file_size(fh, &file_size);
353 if (ret != EFI_SUCCESS)
354 return ret;
355 if (file_size < fh->offset) {
356 ret = EFI_DEVICE_ERROR;
357 return ret;
358 }
359
360 if (set_blk_dev(fh))
361 return EFI_DEVICE_ERROR;
Simon Glass849688f2018-09-15 00:51:00 -0600362 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
Rob Clark53142032017-09-13 18:05:34 -0400363 *buffer_size, &actread))
364 return EFI_DEVICE_ERROR;
365
366 *buffer_size = actread;
367 fh->offset += actread;
368
369 return EFI_SUCCESS;
370}
371
372static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
373 void *buffer)
374{
375 struct efi_file_info *info = buffer;
376 struct fs_dirent *dent;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200377 u64 required_size;
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200378 u16 *dst;
Rob Clark53142032017-09-13 18:05:34 -0400379
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200380 if (set_blk_dev(fh))
381 return EFI_DEVICE_ERROR;
382
Rob Clark53142032017-09-13 18:05:34 -0400383 if (!fh->dirs) {
384 assert(fh->offset == 0);
385 fh->dirs = fs_opendir(fh->path);
386 if (!fh->dirs)
387 return EFI_DEVICE_ERROR;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200388 fh->dent = NULL;
Rob Clark53142032017-09-13 18:05:34 -0400389 }
390
391 /*
392 * So this is a bit awkward. Since fs layer is stateful and we
393 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
394 * we might have to return without consuming the dent.. so we
395 * have to stash it for next call.
396 */
397 if (fh->dent) {
398 dent = fh->dent;
Rob Clark53142032017-09-13 18:05:34 -0400399 } else {
400 dent = fs_readdir(fh->dirs);
401 }
402
Rob Clark53142032017-09-13 18:05:34 -0400403 if (!dent) {
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200404 /* no more files in directory */
405 *buffer_size = 0;
Rob Clark53142032017-09-13 18:05:34 -0400406 return EFI_SUCCESS;
407 }
408
409 /* check buffer size: */
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200410 required_size = sizeof(*info) +
411 2 * (utf8_utf16_strlen(dent->name) + 1);
Rob Clark53142032017-09-13 18:05:34 -0400412 if (*buffer_size < required_size) {
413 *buffer_size = required_size;
414 fh->dent = dent;
415 return EFI_BUFFER_TOO_SMALL;
416 }
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200417 fh->dent = NULL;
Rob Clark53142032017-09-13 18:05:34 -0400418
419 *buffer_size = required_size;
420 memset(info, 0, required_size);
421
422 info->size = required_size;
423 info->file_size = dent->size;
424 info->physical_size = dent->size;
425
426 if (dent->type == FS_DT_DIR)
427 info->attribute |= EFI_FILE_DIRECTORY;
428
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200429 dst = info->file_name;
430 utf8_utf16_strcpy(&dst, dent->name);
Rob Clark53142032017-09-13 18:05:34 -0400431
432 fh->offset++;
433
434 return EFI_SUCCESS;
435}
436
437static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200438 efi_uintn_t *buffer_size, void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400439{
440 struct file_handle *fh = to_fh(file);
441 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200442 u64 bs;
Rob Clark53142032017-09-13 18:05:34 -0400443
444 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
445
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200446 if (!buffer_size || !buffer) {
447 ret = EFI_INVALID_PARAMETER;
448 goto error;
449 }
450
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200451 bs = *buffer_size;
Rob Clark53142032017-09-13 18:05:34 -0400452 if (fh->isdir)
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200453 ret = dir_read(fh, &bs, buffer);
Rob Clark53142032017-09-13 18:05:34 -0400454 else
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200455 ret = file_read(fh, &bs, buffer);
456 if (bs <= SIZE_MAX)
457 *buffer_size = bs;
458 else
459 *buffer_size = SIZE_MAX;
Rob Clark53142032017-09-13 18:05:34 -0400460
461error:
462 return EFI_EXIT(ret);
463}
464
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200465/**
466 * efi_file_write() - write to file
467 *
468 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
469 *
470 * See the Unified Extensible Firmware Interface (UEFI) specification for
471 * details.
472 *
473 * @file: file handle
474 * @buffer_size: number of bytes to write
475 * @buffer: buffer with the bytes to write
476 * Return: status code
477 */
Rob Clark53142032017-09-13 18:05:34 -0400478static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200479 efi_uintn_t *buffer_size,
480 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400481{
482 struct file_handle *fh = to_fh(file);
483 efi_status_t ret = EFI_SUCCESS;
484 loff_t actwrite;
485
486 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
487
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200488 if (!file || !buffer_size || !buffer) {
489 ret = EFI_INVALID_PARAMETER;
490 goto out;
491 }
492 if (fh->isdir) {
493 ret = EFI_UNSUPPORTED;
494 goto out;
495 }
496 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
497 ret = EFI_ACCESS_DENIED;
498 goto out;
499 }
500
501 if (!*buffer_size)
502 goto out;
503
Rob Clark53142032017-09-13 18:05:34 -0400504 if (set_blk_dev(fh)) {
505 ret = EFI_DEVICE_ERROR;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200506 goto out;
Rob Clark53142032017-09-13 18:05:34 -0400507 }
Simon Glass849688f2018-09-15 00:51:00 -0600508 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
Rob Clark53142032017-09-13 18:05:34 -0400509 &actwrite)) {
510 ret = EFI_DEVICE_ERROR;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200511 goto out;
Rob Clark53142032017-09-13 18:05:34 -0400512 }
Rob Clark53142032017-09-13 18:05:34 -0400513 *buffer_size = actwrite;
514 fh->offset += actwrite;
515
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200516out:
Rob Clark53142032017-09-13 18:05:34 -0400517 return EFI_EXIT(ret);
518}
519
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200520/**
521 * efi_file_getpos() - get current position in file
522 *
523 * This function implements the GetPosition service of the EFI file protocol.
524 * See the UEFI spec for details.
525 *
526 * @file: file handle
527 * @pos: pointer to file position
528 * Return: status code
529 */
Rob Clark53142032017-09-13 18:05:34 -0400530static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200531 u64 *pos)
Rob Clark53142032017-09-13 18:05:34 -0400532{
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200533 efi_status_t ret = EFI_SUCCESS;
Rob Clark53142032017-09-13 18:05:34 -0400534 struct file_handle *fh = to_fh(file);
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200535
Rob Clark53142032017-09-13 18:05:34 -0400536 EFI_ENTRY("%p, %p", file, pos);
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200537
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200538 if (fh->isdir) {
539 ret = EFI_UNSUPPORTED;
540 goto out;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200541 }
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200542
543 *pos = fh->offset;
544out:
545 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400546}
547
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200548/**
549 * efi_file_setpos() - set current position in file
550 *
551 * This function implements the SetPosition service of the EFI file protocol.
552 * See the UEFI spec for details.
553 *
554 * @file: file handle
555 * @pos: new file position
556 * Return: status code
557 */
Rob Clark53142032017-09-13 18:05:34 -0400558static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200559 u64 pos)
Rob Clark53142032017-09-13 18:05:34 -0400560{
561 struct file_handle *fh = to_fh(file);
562 efi_status_t ret = EFI_SUCCESS;
563
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200564 EFI_ENTRY("%p, %llu", file, pos);
Rob Clark53142032017-09-13 18:05:34 -0400565
566 if (fh->isdir) {
567 if (pos != 0) {
568 ret = EFI_UNSUPPORTED;
569 goto error;
570 }
571 fs_closedir(fh->dirs);
572 fh->dirs = NULL;
573 }
574
575 if (pos == ~0ULL) {
576 loff_t file_size;
577
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200578 ret = efi_get_file_size(fh, &file_size);
579 if (ret != EFI_SUCCESS)
Rob Clark53142032017-09-13 18:05:34 -0400580 goto error;
Rob Clark53142032017-09-13 18:05:34 -0400581 pos = file_size;
582 }
583
584 fh->offset = pos;
585
586error:
587 return EFI_EXIT(ret);
588}
589
590static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
Heinrich Schuchardte86380d2018-04-04 15:42:09 +0200591 const efi_guid_t *info_type,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200592 efi_uintn_t *buffer_size,
593 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400594{
595 struct file_handle *fh = to_fh(file);
596 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200597 u16 *dst;
Rob Clark53142032017-09-13 18:05:34 -0400598
Heinrich Schuchardte8c99b02018-09-14 22:46:34 +0200599 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
Rob Clark53142032017-09-13 18:05:34 -0400600
Heinrich Schuchardtc540a0e2019-09-08 10:45:31 +0200601 if (!file || !info_type || !buffer_size ||
602 (*buffer_size && !buffer)) {
603 ret = EFI_INVALID_PARAMETER;
604 goto error;
605 }
606
Rob Clark53142032017-09-13 18:05:34 -0400607 if (!guidcmp(info_type, &efi_file_info_guid)) {
608 struct efi_file_info *info = buffer;
609 char *filename = basename(fh);
610 unsigned int required_size;
611 loff_t file_size;
612
613 /* check buffer size: */
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200614 required_size = sizeof(*info) +
615 2 * (utf8_utf16_strlen(filename) + 1);
Rob Clark53142032017-09-13 18:05:34 -0400616 if (*buffer_size < required_size) {
617 *buffer_size = required_size;
618 ret = EFI_BUFFER_TOO_SMALL;
619 goto error;
620 }
621
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200622 ret = efi_get_file_size(fh, &file_size);
623 if (ret != EFI_SUCCESS)
Rob Clark53142032017-09-13 18:05:34 -0400624 goto error;
Rob Clark53142032017-09-13 18:05:34 -0400625
626 memset(info, 0, required_size);
627
628 info->size = required_size;
629 info->file_size = file_size;
630 info->physical_size = file_size;
631
632 if (fh->isdir)
633 info->attribute |= EFI_FILE_DIRECTORY;
634
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200635 dst = info->file_name;
636 utf8_utf16_strcpy(&dst, filename);
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200637 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
638 struct efi_file_system_info *info = buffer;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600639 struct disk_partition part;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200640 efi_uintn_t required_size;
641 int r;
642
643 if (fh->fs->part >= 1)
644 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
645 else
646 r = part_get_info_whole_disk(fh->fs->desc, &part);
647 if (r < 0) {
648 ret = EFI_DEVICE_ERROR;
649 goto error;
650 }
Heinrich Schuchardt0c236c42019-09-08 10:32:54 +0200651 required_size = sizeof(*info) + 2;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200652 if (*buffer_size < required_size) {
653 *buffer_size = required_size;
654 ret = EFI_BUFFER_TOO_SMALL;
655 goto error;
656 }
657
658 memset(info, 0, required_size);
659
660 info->size = required_size;
Heinrich Schuchardta821b0c2019-12-08 10:02:37 +0100661 /*
662 * TODO: We cannot determine if the volume can be written to.
663 */
664 info->read_only = false;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200665 info->volume_size = part.size * part.blksz;
Heinrich Schuchardta821b0c2019-12-08 10:02:37 +0100666 /*
667 * TODO: We currently have no function to determine the free
668 * space. The volume size is the best upper bound we have.
669 */
670 info->free_space = info->volume_size;
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200671 info->block_size = part.blksz;
672 /*
673 * TODO: The volume label is not available in U-Boot.
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200674 */
Heinrich Schuchardt0c236c42019-09-08 10:32:54 +0200675 info->volume_label[0] = 0;
676 } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
677 if (*buffer_size < 2) {
678 *buffer_size = 2;
679 ret = EFI_BUFFER_TOO_SMALL;
680 goto error;
681 }
682 *(u16 *)buffer = 0;
Rob Clark53142032017-09-13 18:05:34 -0400683 } else {
684 ret = EFI_UNSUPPORTED;
685 }
686
687error:
688 return EFI_EXIT(ret);
689}
690
691static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
Heinrich Schuchardte86380d2018-04-04 15:42:09 +0200692 const efi_guid_t *info_type,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200693 efi_uintn_t buffer_size,
694 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400695{
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200696 struct file_handle *fh = to_fh(file);
697 efi_status_t ret = EFI_UNSUPPORTED;
698
699 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
700
701 if (!guidcmp(info_type, &efi_file_info_guid)) {
702 struct efi_file_info *info = (struct efi_file_info *)buffer;
703 char *filename = basename(fh);
704 char *new_file_name, *pos;
705 loff_t file_size;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200706
Heinrich Schuchardtdd106d02019-09-08 11:37:07 +0200707 /* The buffer will always contain a file name. */
708 if (buffer_size < sizeof(struct efi_file_info) + 2 ||
709 buffer_size < info->size) {
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200710 ret = EFI_BAD_BUFFER_SIZE;
711 goto out;
712 }
713 /* We cannot change the directory attribute */
714 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
715 ret = EFI_ACCESS_DENIED;
716 goto out;
717 }
718 /* Check for renaming */
719 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
720 if (!new_file_name) {
721 ret = EFI_OUT_OF_RESOURCES;
722 goto out;
723 }
724 pos = new_file_name;
725 utf16_utf8_strcpy(&pos, info->file_name);
726 if (strcmp(new_file_name, filename)) {
727 /* TODO: we do not support renaming */
728 EFI_PRINT("Renaming not supported\n");
729 free(new_file_name);
730 ret = EFI_ACCESS_DENIED;
731 goto out;
732 }
733 free(new_file_name);
734 /* Check for truncation */
Heinrich Schuchardtdf862792019-09-07 23:28:04 +0200735 ret = efi_get_file_size(fh, &file_size);
736 if (ret != EFI_SUCCESS)
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200737 goto out;
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200738 if (file_size != info->file_size) {
739 /* TODO: we do not support truncation */
740 EFI_PRINT("Truncation not supported\n");
741 ret = EFI_ACCESS_DENIED;
742 goto out;
743 }
744 /*
745 * We do not care for the other attributes
746 * TODO: Support read only
747 */
748 ret = EFI_SUCCESS;
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200749 } else {
Heinrich Schuchardtdd106d02019-09-08 11:37:07 +0200750 /* TODO: We do not support changing the volume label */
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200751 ret = EFI_UNSUPPORTED;
752 }
753out:
754 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400755}
756
757static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
758{
759 EFI_ENTRY("%p", file);
760 return EFI_EXIT(EFI_SUCCESS);
761}
762
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +0200763static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *file,
764 struct efi_file_handle **new_handle,
765 u16 *file_name, u64 open_mode, u64 attributes,
766 struct efi_file_io_token *token)
767{
768 return EFI_UNSUPPORTED;
769}
770
771static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *file,
772 struct efi_file_io_token *token)
773{
774 return EFI_UNSUPPORTED;
775}
776
777static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *file,
778 struct efi_file_io_token *token)
779{
780 return EFI_UNSUPPORTED;
781}
782
783static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *file,
784 struct efi_file_io_token *token)
785{
786 return EFI_UNSUPPORTED;
787}
788
Rob Clark53142032017-09-13 18:05:34 -0400789static const struct efi_file_handle efi_file_handle_protocol = {
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +0200790 .rev = EFI_FILE_PROTOCOL_REVISION2,
Rob Clark53142032017-09-13 18:05:34 -0400791 .open = efi_file_open,
792 .close = efi_file_close,
793 .delete = efi_file_delete,
794 .read = efi_file_read,
795 .write = efi_file_write,
796 .getpos = efi_file_getpos,
797 .setpos = efi_file_setpos,
798 .getinfo = efi_file_getinfo,
799 .setinfo = efi_file_setinfo,
800 .flush = efi_file_flush,
Heinrich Schuchardtf2dcbac2019-09-08 09:35:32 +0200801 .open_ex = efi_file_open_ex,
802 .read_ex = efi_file_read_ex,
803 .write_ex = efi_file_write_ex,
804 .flush_ex = efi_file_flush_ex,
Rob Clark53142032017-09-13 18:05:34 -0400805};
806
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +0100807/**
808 * efi_file_from_path() - open file via device path
809 *
810 * @fp: device path
811 * @return: EFI_FILE_PROTOCOL for the file or NULL
812 */
Rob Clark53142032017-09-13 18:05:34 -0400813struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
814{
815 struct efi_simple_file_system_protocol *v;
816 struct efi_file_handle *f;
817 efi_status_t ret;
818
819 v = efi_fs_from_path(fp);
820 if (!v)
821 return NULL;
822
823 EFI_CALL(ret = v->open_volume(v, &f));
824 if (ret != EFI_SUCCESS)
825 return NULL;
826
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +0100827 /* Skip over device-path nodes before the file path. */
Rob Clark53142032017-09-13 18:05:34 -0400828 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
829 fp = efi_dp_next(fp);
830
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +0100831 /*
832 * Step through the nodes of the directory path until the actual file
833 * node is reached which is the final node in the device path.
834 */
Rob Clark53142032017-09-13 18:05:34 -0400835 while (fp) {
836 struct efi_device_path_file_path *fdp =
837 container_of(fp, struct efi_device_path_file_path, dp);
838 struct efi_file_handle *f2;
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +0200839 u16 *filename;
Rob Clark53142032017-09-13 18:05:34 -0400840
841 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
842 printf("bad file path!\n");
843 f->close(f);
844 return NULL;
845 }
846
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +0200847 filename = u16_strdup(fdp->str);
848 if (!filename)
849 return NULL;
850 EFI_CALL(ret = f->open(f, &f2, filename,
Rob Clark53142032017-09-13 18:05:34 -0400851 EFI_FILE_MODE_READ, 0));
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +0200852 free(filename);
Rob Clark53142032017-09-13 18:05:34 -0400853 if (ret != EFI_SUCCESS)
854 return NULL;
855
856 fp = efi_dp_next(fp);
857
858 EFI_CALL(f->close(f));
859 f = f2;
860 }
861
862 return f;
863}
864
865static efi_status_t EFIAPI
866efi_open_volume(struct efi_simple_file_system_protocol *this,
867 struct efi_file_handle **root)
868{
869 struct file_system *fs = to_fs(this);
870
871 EFI_ENTRY("%p, %p", this, root);
872
AKASHI Takahiro0c672b02018-09-11 15:59:12 +0900873 *root = file_open(fs, NULL, NULL, 0, 0);
Rob Clark53142032017-09-13 18:05:34 -0400874
875 return EFI_EXIT(EFI_SUCCESS);
876}
877
878struct efi_simple_file_system_protocol *
879efi_simple_file_system(struct blk_desc *desc, int part,
880 struct efi_device_path *dp)
881{
882 struct file_system *fs;
883
884 fs = calloc(1, sizeof(*fs));
885 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
886 fs->base.open_volume = efi_open_volume;
887 fs->desc = desc;
888 fs->part = part;
889 fs->dp = dp;
890
891 return &fs->base;
892}