blob: 74ad878217afeba020650c8b665274508f2e19ec [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>
11#include <malloc.h>
Alexander Grafab133c72018-08-08 03:54:32 -060012#include <mapmem.h>
Rob Clark53142032017-09-13 18:05:34 -040013#include <fs.h>
14
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +020015/* GUID for file system information */
16const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
17
Rob Clark53142032017-09-13 18:05:34 -040018struct file_system {
19 struct efi_simple_file_system_protocol base;
20 struct efi_device_path *dp;
21 struct blk_desc *desc;
22 int part;
23};
24#define to_fs(x) container_of(x, struct file_system, base)
25
26struct file_handle {
27 struct efi_file_handle base;
28 struct file_system *fs;
29 loff_t offset; /* current file position/cursor */
30 int isdir;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +020031 u64 open_mode;
Rob Clark53142032017-09-13 18:05:34 -040032
33 /* for reading a directory: */
34 struct fs_dir_stream *dirs;
35 struct fs_dirent *dent;
36
37 char path[0];
38};
39#define to_fh(x) container_of(x, struct file_handle, base)
40
41static const struct efi_file_handle efi_file_handle_protocol;
42
43static char *basename(struct file_handle *fh)
44{
45 char *s = strrchr(fh->path, '/');
46 if (s)
47 return s + 1;
48 return fh->path;
49}
50
51static int set_blk_dev(struct file_handle *fh)
52{
53 return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
54}
55
Heinrich Schuchardt80f834d2018-10-02 05:57:32 +020056/**
57 * is_dir() - check if file handle points to directory
58 *
59 * We assume that set_blk_dev(fh) has been called already.
60 *
61 * @fh: file handle
62 * Return: true if file handle points to a directory
63 */
Rob Clark53142032017-09-13 18:05:34 -040064static int is_dir(struct file_handle *fh)
65{
66 struct fs_dir_stream *dirs;
67
Rob Clark53142032017-09-13 18:05:34 -040068 dirs = fs_opendir(fh->path);
69 if (!dirs)
70 return 0;
71
72 fs_closedir(dirs);
73
74 return 1;
75}
76
77/*
78 * Normalize a path which may include either back or fwd slashes,
79 * double slashes, . or .. entries in the path, etc.
80 */
81static int sanitize_path(char *path)
82{
83 char *p;
84
85 /* backslash to slash: */
86 p = path;
87 while ((p = strchr(p, '\\')))
88 *p++ = '/';
89
90 /* handle double-slashes: */
91 p = path;
92 while ((p = strstr(p, "//"))) {
93 char *src = p + 1;
94 memmove(p, src, strlen(src) + 1);
95 }
96
97 /* handle extra /.'s */
98 p = path;
99 while ((p = strstr(p, "/."))) {
100 /*
101 * You'd be tempted to do this *after* handling ".."s
102 * below to avoid having to check if "/." is start of
103 * a "/..", but that won't have the correct results..
104 * for example, "/foo/./../bar" would get resolved to
105 * "/foo/bar" if you did these two passes in the other
106 * order
107 */
108 if (p[2] == '.') {
109 p += 2;
110 continue;
111 }
112 char *src = p + 2;
113 memmove(p, src, strlen(src) + 1);
114 }
115
116 /* handle extra /..'s: */
117 p = path;
118 while ((p = strstr(p, "/.."))) {
119 char *src = p + 3;
120
121 p--;
122
123 /* find beginning of previous path entry: */
124 while (true) {
125 if (p < path)
126 return -1;
127 if (*p == '/')
128 break;
129 p--;
130 }
131
132 memmove(p, src, strlen(src) + 1);
133 }
134
135 return 0;
136}
137
Heinrich Schuchardt1af423b2018-09-12 19:00:02 +0200138/**
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200139 * efi_create_file() - create file or directory
140 *
141 * @fh: file handle
142 * @attributes: attributes for newly created file
143 * Returns: 0 for success
144 */
145static int efi_create_file(struct file_handle *fh, u64 attributes)
146{
147 loff_t actwrite;
148 void *buffer = &actwrite;
149
150 if (attributes & EFI_FILE_DIRECTORY)
151 return fs_mkdir(fh->path);
152 else
153 return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
154 &actwrite);
155}
156
157/**
Heinrich Schuchardt1af423b2018-09-12 19:00:02 +0200158 * file_open() - open a file handle
159 *
160 * @fs: file system
161 * @parent: directory relative to which the file is to be opened
162 * @file_name: path of the file to be opened. '\', '.', or '..' may
163 * be used as modifiers. A leading backslash indicates an
164 * absolute path.
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200165 * @open_mode: bit mask indicating the access mode (read, write,
Heinrich Schuchardt1af423b2018-09-12 19:00:02 +0200166 * create)
167 * @attributes: attributes for newly created file
168 * Returns: handle to the opened file or NULL
Rob Clark53142032017-09-13 18:05:34 -0400169 */
170static struct efi_file_handle *file_open(struct file_system *fs,
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200171 struct file_handle *parent, u16 *file_name, u64 open_mode,
AKASHI Takahiro0c672b02018-09-11 15:59:12 +0900172 u64 attributes)
Rob Clark53142032017-09-13 18:05:34 -0400173{
174 struct file_handle *fh;
175 char f0[MAX_UTF8_PER_UTF16] = {0};
176 int plen = 0;
177 int flen = 0;
178
179 if (file_name) {
Heinrich Schuchardtdb47c342019-01-12 12:02:33 +0100180 utf16_to_utf8((u8 *)f0, file_name, 1);
181 flen = u16_strlen(file_name);
Rob Clark53142032017-09-13 18:05:34 -0400182 }
183
184 /* we could have a parent, but also an absolute path: */
185 if (f0[0] == '\\') {
186 plen = 0;
187 } else if (parent) {
188 plen = strlen(parent->path) + 1;
189 }
190
191 /* +2 is for null and '/' */
192 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
193
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200194 fh->open_mode = open_mode;
Rob Clark53142032017-09-13 18:05:34 -0400195 fh->base = efi_file_handle_protocol;
196 fh->fs = fs;
197
198 if (parent) {
199 char *p = fh->path;
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200200 int exists;
Rob Clark53142032017-09-13 18:05:34 -0400201
202 if (plen > 0) {
203 strcpy(p, parent->path);
204 p += plen - 1;
205 *p++ = '/';
206 }
207
Heinrich Schuchardtdb47c342019-01-12 12:02:33 +0100208 utf16_to_utf8((u8 *)p, file_name, flen);
Rob Clark53142032017-09-13 18:05:34 -0400209
210 if (sanitize_path(fh->path))
211 goto error;
212
213 /* check if file exists: */
214 if (set_blk_dev(fh))
215 goto error;
216
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200217 exists = fs_exists(fh->path);
Heinrich Schuchardt3ab87ac2019-02-09 22:23:48 +0100218 /* fs_exists() calls fs_close(), so open file system again */
219 if (set_blk_dev(fh))
220 goto error;
221
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200222 if (!exists) {
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200223 if (!(open_mode & EFI_FILE_MODE_CREATE) ||
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200224 efi_create_file(fh, attributes))
225 goto error;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200226 if (set_blk_dev(fh))
227 goto error;
Heinrich Schuchardt34c303b2019-04-06 16:27:34 +0200228 }
229
Rob Clark53142032017-09-13 18:05:34 -0400230 /* figure out if file is a directory: */
231 fh->isdir = is_dir(fh);
232 } else {
233 fh->isdir = 1;
234 strcpy(fh->path, "");
235 }
236
237 return &fh->base;
238
239error:
240 free(fh);
241 return NULL;
242}
243
244static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
245 struct efi_file_handle **new_handle,
Heinrich Schuchardtdb47c342019-01-12 12:02:33 +0100246 u16 *file_name, u64 open_mode, u64 attributes)
Rob Clark53142032017-09-13 18:05:34 -0400247{
248 struct file_handle *fh = to_fh(file);
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200249 efi_status_t ret;
Rob Clark53142032017-09-13 18:05:34 -0400250
Simon Glass60209e62019-01-07 16:44:18 -0700251 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
Heinrich Schuchardt9de5c4c2019-03-19 19:16:23 +0100252 file_name, open_mode, attributes);
Rob Clark53142032017-09-13 18:05:34 -0400253
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200254 /* Check parameters */
Heinrich Schuchardtb63f9fc2018-09-15 20:43:46 +0200255 if (!file || !new_handle || !file_name) {
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200256 ret = EFI_INVALID_PARAMETER;
257 goto out;
258 }
259 if (open_mode != EFI_FILE_MODE_READ &&
260 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
261 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
262 EFI_FILE_MODE_CREATE)) {
263 ret = EFI_INVALID_PARAMETER;
264 goto out;
265 }
Heinrich Schuchardt662d0be2018-09-13 21:31:49 +0200266 /*
267 * The UEFI spec requires that attributes are only set in create mode.
268 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
269 * read mode. EDK2 does not check that attributes are zero if not in
270 * create mode.
271 *
272 * So here we only check attributes in create mode and do not check
273 * that they are zero otherwise.
274 */
275 if ((open_mode & EFI_FILE_MODE_CREATE) &&
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200276 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
277 ret = EFI_INVALID_PARAMETER;
278 goto out;
279 }
Rob Clark53142032017-09-13 18:05:34 -0400280
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200281 /* Open file */
282 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200283 if (*new_handle) {
284 EFI_PRINT("file handle %p\n", *new_handle);
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200285 ret = EFI_SUCCESS;
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200286 } else {
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200287 ret = EFI_NOT_FOUND;
Heinrich Schuchardtd4fe5282019-04-06 16:36:16 +0200288 }
Heinrich Schuchardtd6932c02018-09-12 18:43:58 +0200289out:
290 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400291}
292
293static efi_status_t file_close(struct file_handle *fh)
294{
295 fs_closedir(fh->dirs);
296 free(fh);
297 return EFI_SUCCESS;
298}
299
300static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
301{
302 struct file_handle *fh = to_fh(file);
303 EFI_ENTRY("%p", file);
304 return EFI_EXIT(file_close(fh));
305}
306
307static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
308{
309 struct file_handle *fh = to_fh(file);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900310 efi_status_t ret = EFI_SUCCESS;
311
Rob Clark53142032017-09-13 18:05:34 -0400312 EFI_ENTRY("%p", file);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900313
Heinrich Schuchardt646faf32019-06-17 22:00:13 +0200314 if (set_blk_dev(fh) || fs_unlink(fh->path))
315 ret = EFI_WARN_DELETE_FAILURE;
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900316
Rob Clark53142032017-09-13 18:05:34 -0400317 file_close(fh);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900318 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400319}
320
321static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
322 void *buffer)
323{
324 loff_t actread;
325
Simon Glass849688f2018-09-15 00:51:00 -0600326 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
Rob Clark53142032017-09-13 18:05:34 -0400327 *buffer_size, &actread))
328 return EFI_DEVICE_ERROR;
329
330 *buffer_size = actread;
331 fh->offset += actread;
332
333 return EFI_SUCCESS;
334}
335
336static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
337 void *buffer)
338{
339 struct efi_file_info *info = buffer;
340 struct fs_dirent *dent;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200341 u64 required_size;
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200342 u16 *dst;
Rob Clark53142032017-09-13 18:05:34 -0400343
344 if (!fh->dirs) {
345 assert(fh->offset == 0);
346 fh->dirs = fs_opendir(fh->path);
347 if (!fh->dirs)
348 return EFI_DEVICE_ERROR;
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200349 fh->dent = NULL;
Rob Clark53142032017-09-13 18:05:34 -0400350 }
351
352 /*
353 * So this is a bit awkward. Since fs layer is stateful and we
354 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
355 * we might have to return without consuming the dent.. so we
356 * have to stash it for next call.
357 */
358 if (fh->dent) {
359 dent = fh->dent;
Rob Clark53142032017-09-13 18:05:34 -0400360 } else {
361 dent = fs_readdir(fh->dirs);
362 }
363
Rob Clark53142032017-09-13 18:05:34 -0400364 if (!dent) {
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200365 /* no more files in directory */
366 *buffer_size = 0;
Rob Clark53142032017-09-13 18:05:34 -0400367 return EFI_SUCCESS;
368 }
369
370 /* check buffer size: */
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200371 required_size = sizeof(*info) +
372 2 * (utf8_utf16_strlen(dent->name) + 1);
Rob Clark53142032017-09-13 18:05:34 -0400373 if (*buffer_size < required_size) {
374 *buffer_size = required_size;
375 fh->dent = dent;
376 return EFI_BUFFER_TOO_SMALL;
377 }
Heinrich Schuchardtda062602019-09-07 22:34:07 +0200378 fh->dent = NULL;
Rob Clark53142032017-09-13 18:05:34 -0400379
380 *buffer_size = required_size;
381 memset(info, 0, required_size);
382
383 info->size = required_size;
384 info->file_size = dent->size;
385 info->physical_size = dent->size;
386
387 if (dent->type == FS_DT_DIR)
388 info->attribute |= EFI_FILE_DIRECTORY;
389
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200390 dst = info->file_name;
391 utf8_utf16_strcpy(&dst, dent->name);
Rob Clark53142032017-09-13 18:05:34 -0400392
393 fh->offset++;
394
395 return EFI_SUCCESS;
396}
397
398static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200399 efi_uintn_t *buffer_size, void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400400{
401 struct file_handle *fh = to_fh(file);
402 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200403 u64 bs;
Rob Clark53142032017-09-13 18:05:34 -0400404
405 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
406
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200407 if (!buffer_size || !buffer) {
408 ret = EFI_INVALID_PARAMETER;
409 goto error;
410 }
411
Rob Clark53142032017-09-13 18:05:34 -0400412 if (set_blk_dev(fh)) {
413 ret = EFI_DEVICE_ERROR;
414 goto error;
415 }
416
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200417 bs = *buffer_size;
Rob Clark53142032017-09-13 18:05:34 -0400418 if (fh->isdir)
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200419 ret = dir_read(fh, &bs, buffer);
Rob Clark53142032017-09-13 18:05:34 -0400420 else
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200421 ret = file_read(fh, &bs, buffer);
422 if (bs <= SIZE_MAX)
423 *buffer_size = bs;
424 else
425 *buffer_size = SIZE_MAX;
Rob Clark53142032017-09-13 18:05:34 -0400426
427error:
428 return EFI_EXIT(ret);
429}
430
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200431/**
432 * efi_file_write() - write to file
433 *
434 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
435 *
436 * See the Unified Extensible Firmware Interface (UEFI) specification for
437 * details.
438 *
439 * @file: file handle
440 * @buffer_size: number of bytes to write
441 * @buffer: buffer with the bytes to write
442 * Return: status code
443 */
Rob Clark53142032017-09-13 18:05:34 -0400444static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200445 efi_uintn_t *buffer_size,
446 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400447{
448 struct file_handle *fh = to_fh(file);
449 efi_status_t ret = EFI_SUCCESS;
450 loff_t actwrite;
451
452 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
453
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200454 if (!file || !buffer_size || !buffer) {
455 ret = EFI_INVALID_PARAMETER;
456 goto out;
457 }
458 if (fh->isdir) {
459 ret = EFI_UNSUPPORTED;
460 goto out;
461 }
462 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
463 ret = EFI_ACCESS_DENIED;
464 goto out;
465 }
466
467 if (!*buffer_size)
468 goto out;
469
Rob Clark53142032017-09-13 18:05:34 -0400470 if (set_blk_dev(fh)) {
471 ret = EFI_DEVICE_ERROR;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200472 goto out;
Rob Clark53142032017-09-13 18:05:34 -0400473 }
Simon Glass849688f2018-09-15 00:51:00 -0600474 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
Rob Clark53142032017-09-13 18:05:34 -0400475 &actwrite)) {
476 ret = EFI_DEVICE_ERROR;
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200477 goto out;
Rob Clark53142032017-09-13 18:05:34 -0400478 }
Rob Clark53142032017-09-13 18:05:34 -0400479 *buffer_size = actwrite;
480 fh->offset += actwrite;
481
Heinrich Schuchardtb7e87a22019-09-06 21:37:21 +0200482out:
Rob Clark53142032017-09-13 18:05:34 -0400483 return EFI_EXIT(ret);
484}
485
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200486/**
487 * efi_file_getpos() - get current position in file
488 *
489 * This function implements the GetPosition service of the EFI file protocol.
490 * See the UEFI spec for details.
491 *
492 * @file: file handle
493 * @pos: pointer to file position
494 * Return: status code
495 */
Rob Clark53142032017-09-13 18:05:34 -0400496static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200497 u64 *pos)
Rob Clark53142032017-09-13 18:05:34 -0400498{
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200499 efi_status_t ret = EFI_SUCCESS;
Rob Clark53142032017-09-13 18:05:34 -0400500 struct file_handle *fh = to_fh(file);
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200501
Rob Clark53142032017-09-13 18:05:34 -0400502 EFI_ENTRY("%p, %p", file, pos);
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200503
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200504 if (fh->isdir) {
505 ret = EFI_UNSUPPORTED;
506 goto out;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200507 }
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200508
509 *pos = fh->offset;
510out:
511 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400512}
513
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200514/**
515 * efi_file_setpos() - set current position in file
516 *
517 * This function implements the SetPosition service of the EFI file protocol.
518 * See the UEFI spec for details.
519 *
520 * @file: file handle
521 * @pos: new file position
522 * Return: status code
523 */
Rob Clark53142032017-09-13 18:05:34 -0400524static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200525 u64 pos)
Rob Clark53142032017-09-13 18:05:34 -0400526{
527 struct file_handle *fh = to_fh(file);
528 efi_status_t ret = EFI_SUCCESS;
529
Heinrich Schuchardt21ae0572018-10-07 05:26:26 +0200530 EFI_ENTRY("%p, %llu", file, pos);
Rob Clark53142032017-09-13 18:05:34 -0400531
532 if (fh->isdir) {
533 if (pos != 0) {
534 ret = EFI_UNSUPPORTED;
535 goto error;
536 }
537 fs_closedir(fh->dirs);
538 fh->dirs = NULL;
539 }
540
541 if (pos == ~0ULL) {
542 loff_t file_size;
543
544 if (set_blk_dev(fh)) {
545 ret = EFI_DEVICE_ERROR;
546 goto error;
547 }
548
549 if (fs_size(fh->path, &file_size)) {
550 ret = EFI_DEVICE_ERROR;
551 goto error;
552 }
553
554 pos = file_size;
555 }
556
557 fh->offset = pos;
558
559error:
560 return EFI_EXIT(ret);
561}
562
563static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
Heinrich Schuchardte86380d2018-04-04 15:42:09 +0200564 const efi_guid_t *info_type,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200565 efi_uintn_t *buffer_size,
566 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400567{
568 struct file_handle *fh = to_fh(file);
569 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200570 u16 *dst;
Rob Clark53142032017-09-13 18:05:34 -0400571
Heinrich Schuchardte8c99b02018-09-14 22:46:34 +0200572 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
Rob Clark53142032017-09-13 18:05:34 -0400573
574 if (!guidcmp(info_type, &efi_file_info_guid)) {
575 struct efi_file_info *info = buffer;
576 char *filename = basename(fh);
577 unsigned int required_size;
578 loff_t file_size;
579
580 /* check buffer size: */
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200581 required_size = sizeof(*info) +
582 2 * (utf8_utf16_strlen(filename) + 1);
Rob Clark53142032017-09-13 18:05:34 -0400583 if (*buffer_size < required_size) {
584 *buffer_size = required_size;
585 ret = EFI_BUFFER_TOO_SMALL;
586 goto error;
587 }
588
589 if (set_blk_dev(fh)) {
590 ret = EFI_DEVICE_ERROR;
591 goto error;
592 }
593
594 if (fs_size(fh->path, &file_size)) {
595 ret = EFI_DEVICE_ERROR;
596 goto error;
597 }
598
599 memset(info, 0, required_size);
600
601 info->size = required_size;
602 info->file_size = file_size;
603 info->physical_size = file_size;
604
605 if (fh->isdir)
606 info->attribute |= EFI_FILE_DIRECTORY;
607
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200608 dst = info->file_name;
609 utf8_utf16_strcpy(&dst, filename);
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200610 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
611 struct efi_file_system_info *info = buffer;
612 disk_partition_t part;
613 efi_uintn_t required_size;
614 int r;
615
616 if (fh->fs->part >= 1)
617 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
618 else
619 r = part_get_info_whole_disk(fh->fs->desc, &part);
620 if (r < 0) {
621 ret = EFI_DEVICE_ERROR;
622 goto error;
623 }
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200624 required_size = sizeof(*info) + 2 *
625 (utf8_utf16_strlen((const char *)part.name) +
626 1);
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200627 if (*buffer_size < required_size) {
628 *buffer_size = required_size;
629 ret = EFI_BUFFER_TOO_SMALL;
630 goto error;
631 }
632
633 memset(info, 0, required_size);
634
635 info->size = required_size;
636 info->read_only = true;
637 info->volume_size = part.size * part.blksz;
638 info->free_space = 0;
639 info->block_size = part.blksz;
640 /*
641 * TODO: The volume label is not available in U-Boot.
642 * Use the partition name as substitute.
643 */
Heinrich Schuchardt2ad17f92019-09-07 21:05:45 +0200644 dst = info->volume_label;
645 utf8_utf16_strcpy(&dst, (const char *)part.name);
Rob Clark53142032017-09-13 18:05:34 -0400646 } else {
647 ret = EFI_UNSUPPORTED;
648 }
649
650error:
651 return EFI_EXIT(ret);
652}
653
654static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
Heinrich Schuchardte86380d2018-04-04 15:42:09 +0200655 const efi_guid_t *info_type,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200656 efi_uintn_t buffer_size,
657 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400658{
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200659 struct file_handle *fh = to_fh(file);
660 efi_status_t ret = EFI_UNSUPPORTED;
661
662 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
663
664 if (!guidcmp(info_type, &efi_file_info_guid)) {
665 struct efi_file_info *info = (struct efi_file_info *)buffer;
666 char *filename = basename(fh);
667 char *new_file_name, *pos;
668 loff_t file_size;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200669
Heinrich Schuchardtf1fe17e2019-04-06 18:17:39 +0200670 if (buffer_size < sizeof(struct efi_file_info)) {
671 ret = EFI_BAD_BUFFER_SIZE;
672 goto out;
673 }
674 /* We cannot change the directory attribute */
675 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
676 ret = EFI_ACCESS_DENIED;
677 goto out;
678 }
679 /* Check for renaming */
680 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
681 if (!new_file_name) {
682 ret = EFI_OUT_OF_RESOURCES;
683 goto out;
684 }
685 pos = new_file_name;
686 utf16_utf8_strcpy(&pos, info->file_name);
687 if (strcmp(new_file_name, filename)) {
688 /* TODO: we do not support renaming */
689 EFI_PRINT("Renaming not supported\n");
690 free(new_file_name);
691 ret = EFI_ACCESS_DENIED;
692 goto out;
693 }
694 free(new_file_name);
695 /* Check for truncation */
696 if (set_blk_dev(fh)) {
697 ret = EFI_DEVICE_ERROR;
698 goto out;
699 }
700 if (fs_size(fh->path, &file_size)) {
701 ret = EFI_DEVICE_ERROR;
702 goto out;
703 }
704 if (file_size != info->file_size) {
705 /* TODO: we do not support truncation */
706 EFI_PRINT("Truncation not supported\n");
707 ret = EFI_ACCESS_DENIED;
708 goto out;
709 }
710 /*
711 * We do not care for the other attributes
712 * TODO: Support read only
713 */
714 ret = EFI_SUCCESS;
715 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
716 if (buffer_size < sizeof(struct efi_file_system_info)) {
717 ret = EFI_BAD_BUFFER_SIZE;
718 goto out;
719 }
720 } else {
721 ret = EFI_UNSUPPORTED;
722 }
723out:
724 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400725}
726
727static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
728{
729 EFI_ENTRY("%p", file);
730 return EFI_EXIT(EFI_SUCCESS);
731}
732
733static const struct efi_file_handle efi_file_handle_protocol = {
Heinrich Schuchardt79730de2019-03-27 21:41:04 +0100734 /*
735 * TODO: We currently only support EFI file protocol revision 0x00010000
736 * while UEFI specs 2.4 - 2.7 prescribe revision 0x00020000.
737 */
Rob Clark53142032017-09-13 18:05:34 -0400738 .rev = EFI_FILE_PROTOCOL_REVISION,
739 .open = efi_file_open,
740 .close = efi_file_close,
741 .delete = efi_file_delete,
742 .read = efi_file_read,
743 .write = efi_file_write,
744 .getpos = efi_file_getpos,
745 .setpos = efi_file_setpos,
746 .getinfo = efi_file_getinfo,
747 .setinfo = efi_file_setinfo,
748 .flush = efi_file_flush,
749};
750
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +0100751/**
752 * efi_file_from_path() - open file via device path
753 *
754 * @fp: device path
755 * @return: EFI_FILE_PROTOCOL for the file or NULL
756 */
Rob Clark53142032017-09-13 18:05:34 -0400757struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
758{
759 struct efi_simple_file_system_protocol *v;
760 struct efi_file_handle *f;
761 efi_status_t ret;
762
763 v = efi_fs_from_path(fp);
764 if (!v)
765 return NULL;
766
767 EFI_CALL(ret = v->open_volume(v, &f));
768 if (ret != EFI_SUCCESS)
769 return NULL;
770
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +0100771 /* Skip over device-path nodes before the file path. */
Rob Clark53142032017-09-13 18:05:34 -0400772 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
773 fp = efi_dp_next(fp);
774
Heinrich Schuchardtc1476c22019-02-04 21:24:35 +0100775 /*
776 * Step through the nodes of the directory path until the actual file
777 * node is reached which is the final node in the device path.
778 */
Rob Clark53142032017-09-13 18:05:34 -0400779 while (fp) {
780 struct efi_device_path_file_path *fdp =
781 container_of(fp, struct efi_device_path_file_path, dp);
782 struct efi_file_handle *f2;
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +0200783 u16 *filename;
Rob Clark53142032017-09-13 18:05:34 -0400784
785 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
786 printf("bad file path!\n");
787 f->close(f);
788 return NULL;
789 }
790
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +0200791 filename = u16_strdup(fdp->str);
792 if (!filename)
793 return NULL;
794 EFI_CALL(ret = f->open(f, &f2, filename,
Rob Clark53142032017-09-13 18:05:34 -0400795 EFI_FILE_MODE_READ, 0));
Heinrich Schuchardt2cfcd992019-07-14 20:14:46 +0200796 free(filename);
Rob Clark53142032017-09-13 18:05:34 -0400797 if (ret != EFI_SUCCESS)
798 return NULL;
799
800 fp = efi_dp_next(fp);
801
802 EFI_CALL(f->close(f));
803 f = f2;
804 }
805
806 return f;
807}
808
809static efi_status_t EFIAPI
810efi_open_volume(struct efi_simple_file_system_protocol *this,
811 struct efi_file_handle **root)
812{
813 struct file_system *fs = to_fs(this);
814
815 EFI_ENTRY("%p, %p", this, root);
816
AKASHI Takahiro0c672b02018-09-11 15:59:12 +0900817 *root = file_open(fs, NULL, NULL, 0, 0);
Rob Clark53142032017-09-13 18:05:34 -0400818
819 return EFI_EXIT(EFI_SUCCESS);
820}
821
822struct efi_simple_file_system_protocol *
823efi_simple_file_system(struct blk_desc *desc, int part,
824 struct efi_device_path *dp)
825{
826 struct file_system *fs;
827
828 fs = calloc(1, sizeof(*fs));
829 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
830 fs->base.open_volume = efi_open_volume;
831 fs->desc = desc;
832 fs->part = part;
833 fs->dp = dp;
834
835 return &fs->base;
836}