blob: a13754a558fde10138a4123f133aea1911a6b180 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clark2a920802017-09-13 18:05:34 -04002/*
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +02003 * EFI_FILE_PROTOCOL
Rob Clark2a920802017-09-13 18:05:34 -04004 *
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +02005 * Copyright (c) 2017 Rob Clark
Rob Clark2a920802017-09-13 18:05:34 -04006 */
7
8#include <common.h>
9#include <charset.h>
10#include <efi_loader.h>
11#include <malloc.h>
Alexander Grafd5a5a5a2018-08-08 03:54:32 -060012#include <mapmem.h>
Rob Clark2a920802017-09-13 18:05:34 -040013#include <fs.h>
14
Heinrich Schuchardt9e6835e2018-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 Clark2a920802017-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 Schuchardtb0f1c722019-09-06 21:37:21 +020031 u64 open_mode;
Rob Clark2a920802017-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 Schuchardt2c61e0c2018-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 Clark2a920802017-09-13 18:05:34 -040064static int is_dir(struct file_handle *fh)
65{
66 struct fs_dir_stream *dirs;
67
Rob Clark2a920802017-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 Schuchardt050cea72018-09-12 19:00:02 +0200138/**
Heinrich Schuchardtcb0c2a72019-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 Schuchardt050cea72018-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 Schuchardtb0f1c722019-09-06 21:37:21 +0200165 * @open_mode: bit mask indicating the access mode (read, write,
Heinrich Schuchardt050cea72018-09-12 19:00:02 +0200166 * create)
167 * @attributes: attributes for newly created file
168 * Returns: handle to the opened file or NULL
Rob Clark2a920802017-09-13 18:05:34 -0400169 */
170static struct efi_file_handle *file_open(struct file_system *fs,
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200171 struct file_handle *parent, u16 *file_name, u64 open_mode,
AKASHI Takahiro5bc84a12018-09-11 15:59:12 +0900172 u64 attributes)
Rob Clark2a920802017-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 Schuchardtc82f8f62019-01-12 12:02:33 +0100180 utf16_to_utf8((u8 *)f0, file_name, 1);
181 flen = u16_strlen(file_name);
Rob Clark2a920802017-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 Schuchardtb0f1c722019-09-06 21:37:21 +0200194 fh->open_mode = open_mode;
Rob Clark2a920802017-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 Schuchardtcb0c2a72019-04-06 16:27:34 +0200200 int exists;
Rob Clark2a920802017-09-13 18:05:34 -0400201
202 if (plen > 0) {
203 strcpy(p, parent->path);
204 p += plen - 1;
205 *p++ = '/';
206 }
207
Heinrich Schuchardtc82f8f62019-01-12 12:02:33 +0100208 utf16_to_utf8((u8 *)p, file_name, flen);
Rob Clark2a920802017-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 Schuchardtcb0c2a72019-04-06 16:27:34 +0200217 exists = fs_exists(fh->path);
Heinrich Schuchardt823c2332019-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 Schuchardtcb0c2a72019-04-06 16:27:34 +0200222 if (!exists) {
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200223 if (!(open_mode & EFI_FILE_MODE_CREATE) ||
Heinrich Schuchardtcb0c2a72019-04-06 16:27:34 +0200224 efi_create_file(fh, attributes))
225 goto error;
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200226 if (set_blk_dev(fh))
227 goto error;
Heinrich Schuchardtcb0c2a72019-04-06 16:27:34 +0200228 }
229
Rob Clark2a920802017-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 Schuchardtc82f8f62019-01-12 12:02:33 +0100246 u16 *file_name, u64 open_mode, u64 attributes)
Rob Clark2a920802017-09-13 18:05:34 -0400247{
248 struct file_handle *fh = to_fh(file);
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200249 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400250
Simon Glass0c89a312019-01-07 16:44:18 -0700251 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
Heinrich Schuchardt1646e092019-03-19 19:16:23 +0100252 file_name, open_mode, attributes);
Rob Clark2a920802017-09-13 18:05:34 -0400253
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200254 /* Check parameters */
Heinrich Schuchardtd3dce352018-09-15 20:43:46 +0200255 if (!file || !new_handle || !file_name) {
Heinrich Schuchardt143acd12018-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 Schuchardtbd665882018-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 Schuchardt143acd12018-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 Clark2a920802017-09-13 18:05:34 -0400280
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200281 /* Open file */
282 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
Heinrich Schuchardt19b2d892019-04-06 16:36:16 +0200283 if (*new_handle) {
284 EFI_PRINT("file handle %p\n", *new_handle);
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200285 ret = EFI_SUCCESS;
Heinrich Schuchardt19b2d892019-04-06 16:36:16 +0200286 } else {
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200287 ret = EFI_NOT_FOUND;
Heinrich Schuchardt19b2d892019-04-06 16:36:16 +0200288 }
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200289out:
290 return EFI_EXIT(ret);
Rob Clark2a920802017-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 Takahirod39f6a62018-09-11 15:59:16 +0900310 efi_status_t ret = EFI_SUCCESS;
311
Rob Clark2a920802017-09-13 18:05:34 -0400312 EFI_ENTRY("%p", file);
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900313
Heinrich Schuchardtfa390812019-06-17 22:00:13 +0200314 if (set_blk_dev(fh) || fs_unlink(fh->path))
315 ret = EFI_WARN_DELETE_FAILURE;
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900316
Rob Clark2a920802017-09-13 18:05:34 -0400317 file_close(fh);
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900318 return EFI_EXIT(ret);
Rob Clark2a920802017-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 Glass2ae843f2018-09-15 00:51:00 -0600326 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
Rob Clark2a920802017-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;
341 unsigned int required_size;
342
343 if (!fh->dirs) {
344 assert(fh->offset == 0);
345 fh->dirs = fs_opendir(fh->path);
346 if (!fh->dirs)
347 return EFI_DEVICE_ERROR;
348 }
349
350 /*
351 * So this is a bit awkward. Since fs layer is stateful and we
352 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
353 * we might have to return without consuming the dent.. so we
354 * have to stash it for next call.
355 */
356 if (fh->dent) {
357 dent = fh->dent;
358 fh->dent = NULL;
359 } else {
360 dent = fs_readdir(fh->dirs);
361 }
362
363
364 if (!dent) {
365 /* no more files in directory: */
366 /* workaround shim.efi bug/quirk.. as find_boot_csv()
367 * loops through directory contents, it initially calls
368 * read w/ zero length buffer to find out how much mem
369 * to allocate for the EFI_FILE_INFO, then allocates,
370 * and then calls a 2nd time. If we return size of
371 * zero the first time, it happily passes that to
372 * AllocateZeroPool(), and when that returns NULL it
373 * thinks it is EFI_OUT_OF_RESOURCES. So on first
374 * call return a non-zero size:
375 */
376 if (*buffer_size == 0)
377 *buffer_size = sizeof(*info);
378 else
379 *buffer_size = 0;
380 return EFI_SUCCESS;
381 }
382
383 /* check buffer size: */
384 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
385 if (*buffer_size < required_size) {
386 *buffer_size = required_size;
387 fh->dent = dent;
388 return EFI_BUFFER_TOO_SMALL;
389 }
390
391 *buffer_size = required_size;
392 memset(info, 0, required_size);
393
394 info->size = required_size;
395 info->file_size = dent->size;
396 info->physical_size = dent->size;
397
398 if (dent->type == FS_DT_DIR)
399 info->attribute |= EFI_FILE_DIRECTORY;
400
Heinrich Schuchardtc82f8f62019-01-12 12:02:33 +0100401 ascii2unicode(info->file_name, dent->name);
Rob Clark2a920802017-09-13 18:05:34 -0400402
403 fh->offset++;
404
405 return EFI_SUCCESS;
406}
407
408static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200409 efi_uintn_t *buffer_size, void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400410{
411 struct file_handle *fh = to_fh(file);
412 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200413 u64 bs;
Rob Clark2a920802017-09-13 18:05:34 -0400414
415 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
416
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200417 if (!buffer_size || !buffer) {
418 ret = EFI_INVALID_PARAMETER;
419 goto error;
420 }
421
Rob Clark2a920802017-09-13 18:05:34 -0400422 if (set_blk_dev(fh)) {
423 ret = EFI_DEVICE_ERROR;
424 goto error;
425 }
426
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200427 bs = *buffer_size;
Rob Clark2a920802017-09-13 18:05:34 -0400428 if (fh->isdir)
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200429 ret = dir_read(fh, &bs, buffer);
Rob Clark2a920802017-09-13 18:05:34 -0400430 else
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200431 ret = file_read(fh, &bs, buffer);
432 if (bs <= SIZE_MAX)
433 *buffer_size = bs;
434 else
435 *buffer_size = SIZE_MAX;
Rob Clark2a920802017-09-13 18:05:34 -0400436
437error:
438 return EFI_EXIT(ret);
439}
440
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200441/**
442 * efi_file_write() - write to file
443 *
444 * This function implements the Write() service of the EFI_FILE_PROTOCOL.
445 *
446 * See the Unified Extensible Firmware Interface (UEFI) specification for
447 * details.
448 *
449 * @file: file handle
450 * @buffer_size: number of bytes to write
451 * @buffer: buffer with the bytes to write
452 * Return: status code
453 */
Rob Clark2a920802017-09-13 18:05:34 -0400454static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200455 efi_uintn_t *buffer_size,
456 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400457{
458 struct file_handle *fh = to_fh(file);
459 efi_status_t ret = EFI_SUCCESS;
460 loff_t actwrite;
461
462 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
463
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200464 if (!file || !buffer_size || !buffer) {
465 ret = EFI_INVALID_PARAMETER;
466 goto out;
467 }
468 if (fh->isdir) {
469 ret = EFI_UNSUPPORTED;
470 goto out;
471 }
472 if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
473 ret = EFI_ACCESS_DENIED;
474 goto out;
Rob Clark2a920802017-09-13 18:05:34 -0400475 }
476
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200477 if (!*buffer_size)
478 goto out;
479
480 if (set_blk_dev(fh)) {
481 ret = EFI_DEVICE_ERROR;
482 goto out;
483 }
Simon Glass2ae843f2018-09-15 00:51:00 -0600484 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
Rob Clark2a920802017-09-13 18:05:34 -0400485 &actwrite)) {
486 ret = EFI_DEVICE_ERROR;
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200487 goto out;
Rob Clark2a920802017-09-13 18:05:34 -0400488 }
Rob Clark2a920802017-09-13 18:05:34 -0400489 *buffer_size = actwrite;
490 fh->offset += actwrite;
491
Heinrich Schuchardtb0f1c722019-09-06 21:37:21 +0200492out:
Rob Clark2a920802017-09-13 18:05:34 -0400493 return EFI_EXIT(ret);
494}
495
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200496/**
497 * efi_file_getpos() - get current position in file
498 *
499 * This function implements the GetPosition service of the EFI file protocol.
500 * See the UEFI spec for details.
501 *
502 * @file: file handle
503 * @pos: pointer to file position
504 * Return: status code
505 */
Rob Clark2a920802017-09-13 18:05:34 -0400506static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200507 u64 *pos)
Rob Clark2a920802017-09-13 18:05:34 -0400508{
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200509 efi_status_t ret = EFI_SUCCESS;
Rob Clark2a920802017-09-13 18:05:34 -0400510 struct file_handle *fh = to_fh(file);
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200511
Rob Clark2a920802017-09-13 18:05:34 -0400512 EFI_ENTRY("%p, %p", file, pos);
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200513
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200514 if (fh->isdir) {
515 ret = EFI_UNSUPPORTED;
516 goto out;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200517 }
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200518
519 *pos = fh->offset;
520out:
521 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400522}
523
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200524/**
525 * efi_file_setpos() - set current position in file
526 *
527 * This function implements the SetPosition service of the EFI file protocol.
528 * See the UEFI spec for details.
529 *
530 * @file: file handle
531 * @pos: new file position
532 * Return: status code
533 */
Rob Clark2a920802017-09-13 18:05:34 -0400534static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200535 u64 pos)
Rob Clark2a920802017-09-13 18:05:34 -0400536{
537 struct file_handle *fh = to_fh(file);
538 efi_status_t ret = EFI_SUCCESS;
539
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200540 EFI_ENTRY("%p, %llu", file, pos);
Rob Clark2a920802017-09-13 18:05:34 -0400541
542 if (fh->isdir) {
543 if (pos != 0) {
544 ret = EFI_UNSUPPORTED;
545 goto error;
546 }
547 fs_closedir(fh->dirs);
548 fh->dirs = NULL;
549 }
550
551 if (pos == ~0ULL) {
552 loff_t file_size;
553
554 if (set_blk_dev(fh)) {
555 ret = EFI_DEVICE_ERROR;
556 goto error;
557 }
558
559 if (fs_size(fh->path, &file_size)) {
560 ret = EFI_DEVICE_ERROR;
561 goto error;
562 }
563
564 pos = file_size;
565 }
566
567 fh->offset = pos;
568
569error:
570 return EFI_EXIT(ret);
571}
572
573static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
Heinrich Schuchardt9c9021e2018-04-04 15:42:09 +0200574 const efi_guid_t *info_type,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200575 efi_uintn_t *buffer_size,
576 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400577{
578 struct file_handle *fh = to_fh(file);
579 efi_status_t ret = EFI_SUCCESS;
580
Heinrich Schuchardt008fb482018-09-14 22:46:34 +0200581 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
Rob Clark2a920802017-09-13 18:05:34 -0400582
583 if (!guidcmp(info_type, &efi_file_info_guid)) {
584 struct efi_file_info *info = buffer;
585 char *filename = basename(fh);
586 unsigned int required_size;
587 loff_t file_size;
588
589 /* check buffer size: */
590 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
591 if (*buffer_size < required_size) {
592 *buffer_size = required_size;
593 ret = EFI_BUFFER_TOO_SMALL;
594 goto error;
595 }
596
597 if (set_blk_dev(fh)) {
598 ret = EFI_DEVICE_ERROR;
599 goto error;
600 }
601
602 if (fs_size(fh->path, &file_size)) {
603 ret = EFI_DEVICE_ERROR;
604 goto error;
605 }
606
607 memset(info, 0, required_size);
608
609 info->size = required_size;
610 info->file_size = file_size;
611 info->physical_size = file_size;
612
613 if (fh->isdir)
614 info->attribute |= EFI_FILE_DIRECTORY;
615
Heinrich Schuchardt02c2f022018-11-05 20:22:12 +0100616 ascii2unicode(info->file_name, filename);
Heinrich Schuchardt9e6835e2018-04-04 15:42:11 +0200617 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
618 struct efi_file_system_info *info = buffer;
619 disk_partition_t part;
620 efi_uintn_t required_size;
621 int r;
622
623 if (fh->fs->part >= 1)
624 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
625 else
626 r = part_get_info_whole_disk(fh->fs->desc, &part);
627 if (r < 0) {
628 ret = EFI_DEVICE_ERROR;
629 goto error;
630 }
631 required_size = sizeof(info) + 2 *
632 (strlen((const char *)part.name) + 1);
633 if (*buffer_size < required_size) {
634 *buffer_size = required_size;
635 ret = EFI_BUFFER_TOO_SMALL;
636 goto error;
637 }
638
639 memset(info, 0, required_size);
640
641 info->size = required_size;
642 info->read_only = true;
643 info->volume_size = part.size * part.blksz;
644 info->free_space = 0;
645 info->block_size = part.blksz;
646 /*
647 * TODO: The volume label is not available in U-Boot.
648 * Use the partition name as substitute.
649 */
650 ascii2unicode((u16 *)info->volume_label,
651 (const char *)part.name);
Rob Clark2a920802017-09-13 18:05:34 -0400652 } else {
653 ret = EFI_UNSUPPORTED;
654 }
655
656error:
657 return EFI_EXIT(ret);
658}
659
660static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
Heinrich Schuchardt9c9021e2018-04-04 15:42:09 +0200661 const efi_guid_t *info_type,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200662 efi_uintn_t buffer_size,
663 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400664{
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200665 struct file_handle *fh = to_fh(file);
666 efi_status_t ret = EFI_UNSUPPORTED;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200667
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200668 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
669
670 if (!guidcmp(info_type, &efi_file_info_guid)) {
671 struct efi_file_info *info = (struct efi_file_info *)buffer;
672 char *filename = basename(fh);
673 char *new_file_name, *pos;
674 loff_t file_size;
675
676 if (buffer_size < sizeof(struct efi_file_info)) {
677 ret = EFI_BAD_BUFFER_SIZE;
678 goto out;
679 }
680 /* We cannot change the directory attribute */
681 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
682 ret = EFI_ACCESS_DENIED;
683 goto out;
684 }
685 /* Check for renaming */
686 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
687 if (!new_file_name) {
688 ret = EFI_OUT_OF_RESOURCES;
689 goto out;
690 }
691 pos = new_file_name;
692 utf16_utf8_strcpy(&pos, info->file_name);
693 if (strcmp(new_file_name, filename)) {
694 /* TODO: we do not support renaming */
695 EFI_PRINT("Renaming not supported\n");
696 free(new_file_name);
697 ret = EFI_ACCESS_DENIED;
698 goto out;
699 }
700 free(new_file_name);
701 /* Check for truncation */
702 if (set_blk_dev(fh)) {
703 ret = EFI_DEVICE_ERROR;
704 goto out;
705 }
706 if (fs_size(fh->path, &file_size)) {
707 ret = EFI_DEVICE_ERROR;
708 goto out;
709 }
710 if (file_size != info->file_size) {
711 /* TODO: we do not support truncation */
712 EFI_PRINT("Truncation not supported\n");
713 ret = EFI_ACCESS_DENIED;
714 goto out;
715 }
716 /*
717 * We do not care for the other attributes
718 * TODO: Support read only
719 */
720 ret = EFI_SUCCESS;
721 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
722 if (buffer_size < sizeof(struct efi_file_system_info)) {
723 ret = EFI_BAD_BUFFER_SIZE;
724 goto out;
725 }
726 } else {
727 ret = EFI_UNSUPPORTED;
728 }
729out:
730 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400731}
732
733static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
734{
735 EFI_ENTRY("%p", file);
736 return EFI_EXIT(EFI_SUCCESS);
737}
738
739static const struct efi_file_handle efi_file_handle_protocol = {
Heinrich Schuchardt17394f92019-03-27 21:41:04 +0100740 /*
741 * TODO: We currently only support EFI file protocol revision 0x00010000
742 * while UEFI specs 2.4 - 2.7 prescribe revision 0x00020000.
743 */
Rob Clark2a920802017-09-13 18:05:34 -0400744 .rev = EFI_FILE_PROTOCOL_REVISION,
745 .open = efi_file_open,
746 .close = efi_file_close,
747 .delete = efi_file_delete,
748 .read = efi_file_read,
749 .write = efi_file_write,
750 .getpos = efi_file_getpos,
751 .setpos = efi_file_setpos,
752 .getinfo = efi_file_getinfo,
753 .setinfo = efi_file_setinfo,
754 .flush = efi_file_flush,
755};
756
Heinrich Schuchardt95288b12019-02-04 21:24:35 +0100757/**
758 * efi_file_from_path() - open file via device path
759 *
760 * @fp: device path
761 * @return: EFI_FILE_PROTOCOL for the file or NULL
762 */
Rob Clark2a920802017-09-13 18:05:34 -0400763struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
764{
765 struct efi_simple_file_system_protocol *v;
766 struct efi_file_handle *f;
767 efi_status_t ret;
768
769 v = efi_fs_from_path(fp);
770 if (!v)
771 return NULL;
772
773 EFI_CALL(ret = v->open_volume(v, &f));
774 if (ret != EFI_SUCCESS)
775 return NULL;
776
Heinrich Schuchardt95288b12019-02-04 21:24:35 +0100777 /* Skip over device-path nodes before the file path. */
Rob Clark2a920802017-09-13 18:05:34 -0400778 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
779 fp = efi_dp_next(fp);
780
Heinrich Schuchardt95288b12019-02-04 21:24:35 +0100781 /*
782 * Step through the nodes of the directory path until the actual file
783 * node is reached which is the final node in the device path.
784 */
Rob Clark2a920802017-09-13 18:05:34 -0400785 while (fp) {
786 struct efi_device_path_file_path *fdp =
787 container_of(fp, struct efi_device_path_file_path, dp);
788 struct efi_file_handle *f2;
Heinrich Schuchardtf62be162019-07-14 20:14:46 +0200789 u16 *filename;
Rob Clark2a920802017-09-13 18:05:34 -0400790
791 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
792 printf("bad file path!\n");
793 f->close(f);
794 return NULL;
795 }
796
Heinrich Schuchardtf62be162019-07-14 20:14:46 +0200797 filename = u16_strdup(fdp->str);
798 if (!filename)
799 return NULL;
800 EFI_CALL(ret = f->open(f, &f2, filename,
Rob Clark2a920802017-09-13 18:05:34 -0400801 EFI_FILE_MODE_READ, 0));
Heinrich Schuchardtf62be162019-07-14 20:14:46 +0200802 free(filename);
Rob Clark2a920802017-09-13 18:05:34 -0400803 if (ret != EFI_SUCCESS)
804 return NULL;
805
806 fp = efi_dp_next(fp);
807
808 EFI_CALL(f->close(f));
809 f = f2;
810 }
811
812 return f;
813}
814
815static efi_status_t EFIAPI
816efi_open_volume(struct efi_simple_file_system_protocol *this,
817 struct efi_file_handle **root)
818{
819 struct file_system *fs = to_fs(this);
820
821 EFI_ENTRY("%p, %p", this, root);
822
AKASHI Takahiro5bc84a12018-09-11 15:59:12 +0900823 *root = file_open(fs, NULL, NULL, 0, 0);
Rob Clark2a920802017-09-13 18:05:34 -0400824
825 return EFI_EXIT(EFI_SUCCESS);
826}
827
828struct efi_simple_file_system_protocol *
829efi_simple_file_system(struct blk_desc *desc, int part,
830 struct efi_device_path *dp)
831{
832 struct file_system *fs;
833
834 fs = calloc(1, sizeof(*fs));
835 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
836 fs->base.open_volume = efi_open_volume;
837 fs->desc = desc;
838 fs->part = part;
839 fs->dp = dp;
840
841 return &fs->base;
842}