blob: f4ca5694eee3a688ca2a4331096f42ab7f30a122 [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/*
3 * EFI utils
4 *
5 * 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;
31
32 /* for reading a directory: */
33 struct fs_dir_stream *dirs;
34 struct fs_dirent *dent;
35
36 char path[0];
37};
38#define to_fh(x) container_of(x, struct file_handle, base)
39
40static const struct efi_file_handle efi_file_handle_protocol;
41
42static char *basename(struct file_handle *fh)
43{
44 char *s = strrchr(fh->path, '/');
45 if (s)
46 return s + 1;
47 return fh->path;
48}
49
50static int set_blk_dev(struct file_handle *fh)
51{
52 return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
53}
54
Heinrich Schuchardt2c61e0c2018-10-02 05:57:32 +020055/**
56 * is_dir() - check if file handle points to directory
57 *
58 * We assume that set_blk_dev(fh) has been called already.
59 *
60 * @fh: file handle
61 * Return: true if file handle points to a directory
62 */
Rob Clark2a920802017-09-13 18:05:34 -040063static int is_dir(struct file_handle *fh)
64{
65 struct fs_dir_stream *dirs;
66
Rob Clark2a920802017-09-13 18:05:34 -040067 dirs = fs_opendir(fh->path);
68 if (!dirs)
69 return 0;
70
71 fs_closedir(dirs);
72
73 return 1;
74}
75
76/*
77 * Normalize a path which may include either back or fwd slashes,
78 * double slashes, . or .. entries in the path, etc.
79 */
80static int sanitize_path(char *path)
81{
82 char *p;
83
84 /* backslash to slash: */
85 p = path;
86 while ((p = strchr(p, '\\')))
87 *p++ = '/';
88
89 /* handle double-slashes: */
90 p = path;
91 while ((p = strstr(p, "//"))) {
92 char *src = p + 1;
93 memmove(p, src, strlen(src) + 1);
94 }
95
96 /* handle extra /.'s */
97 p = path;
98 while ((p = strstr(p, "/."))) {
99 /*
100 * You'd be tempted to do this *after* handling ".."s
101 * below to avoid having to check if "/." is start of
102 * a "/..", but that won't have the correct results..
103 * for example, "/foo/./../bar" would get resolved to
104 * "/foo/bar" if you did these two passes in the other
105 * order
106 */
107 if (p[2] == '.') {
108 p += 2;
109 continue;
110 }
111 char *src = p + 2;
112 memmove(p, src, strlen(src) + 1);
113 }
114
115 /* handle extra /..'s: */
116 p = path;
117 while ((p = strstr(p, "/.."))) {
118 char *src = p + 3;
119
120 p--;
121
122 /* find beginning of previous path entry: */
123 while (true) {
124 if (p < path)
125 return -1;
126 if (*p == '/')
127 break;
128 p--;
129 }
130
131 memmove(p, src, strlen(src) + 1);
132 }
133
134 return 0;
135}
136
Heinrich Schuchardt050cea72018-09-12 19:00:02 +0200137/**
Heinrich Schuchardtcb0c2a72019-04-06 16:27:34 +0200138 * efi_create_file() - create file or directory
139 *
140 * @fh: file handle
141 * @attributes: attributes for newly created file
142 * Returns: 0 for success
143 */
144static int efi_create_file(struct file_handle *fh, u64 attributes)
145{
146 loff_t actwrite;
147 void *buffer = &actwrite;
148
149 if (attributes & EFI_FILE_DIRECTORY)
150 return fs_mkdir(fh->path);
151 else
152 return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
153 &actwrite);
154}
155
156/**
Heinrich Schuchardt050cea72018-09-12 19:00:02 +0200157 * file_open() - open a file handle
158 *
159 * @fs: file system
160 * @parent: directory relative to which the file is to be opened
161 * @file_name: path of the file to be opened. '\', '.', or '..' may
162 * be used as modifiers. A leading backslash indicates an
163 * absolute path.
164 * @mode: bit mask indicating the access mode (read, write,
165 * create)
166 * @attributes: attributes for newly created file
167 * Returns: handle to the opened file or NULL
Rob Clark2a920802017-09-13 18:05:34 -0400168 */
169static struct efi_file_handle *file_open(struct file_system *fs,
Heinrich Schuchardtc82f8f62019-01-12 12:02:33 +0100170 struct file_handle *parent, u16 *file_name, u64 mode,
AKASHI Takahiro5bc84a12018-09-11 15:59:12 +0900171 u64 attributes)
Rob Clark2a920802017-09-13 18:05:34 -0400172{
173 struct file_handle *fh;
174 char f0[MAX_UTF8_PER_UTF16] = {0};
175 int plen = 0;
176 int flen = 0;
177
178 if (file_name) {
Heinrich Schuchardtc82f8f62019-01-12 12:02:33 +0100179 utf16_to_utf8((u8 *)f0, file_name, 1);
180 flen = u16_strlen(file_name);
Rob Clark2a920802017-09-13 18:05:34 -0400181 }
182
183 /* we could have a parent, but also an absolute path: */
184 if (f0[0] == '\\') {
185 plen = 0;
186 } else if (parent) {
187 plen = strlen(parent->path) + 1;
188 }
189
190 /* +2 is for null and '/' */
191 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
192
193 fh->base = efi_file_handle_protocol;
194 fh->fs = fs;
195
196 if (parent) {
197 char *p = fh->path;
Heinrich Schuchardtcb0c2a72019-04-06 16:27:34 +0200198 int exists;
Rob Clark2a920802017-09-13 18:05:34 -0400199
200 if (plen > 0) {
201 strcpy(p, parent->path);
202 p += plen - 1;
203 *p++ = '/';
204 }
205
Heinrich Schuchardtc82f8f62019-01-12 12:02:33 +0100206 utf16_to_utf8((u8 *)p, file_name, flen);
Rob Clark2a920802017-09-13 18:05:34 -0400207
208 if (sanitize_path(fh->path))
209 goto error;
210
211 /* check if file exists: */
212 if (set_blk_dev(fh))
213 goto error;
214
Heinrich Schuchardtcb0c2a72019-04-06 16:27:34 +0200215 exists = fs_exists(fh->path);
Heinrich Schuchardt823c2332019-02-09 22:23:48 +0100216 /* fs_exists() calls fs_close(), so open file system again */
217 if (set_blk_dev(fh))
218 goto error;
219
Heinrich Schuchardtcb0c2a72019-04-06 16:27:34 +0200220 if (!exists) {
221 if (!(mode & EFI_FILE_MODE_CREATE) ||
222 efi_create_file(fh, attributes))
223 goto error;
224 }
225
Rob Clark2a920802017-09-13 18:05:34 -0400226 /* figure out if file is a directory: */
227 fh->isdir = is_dir(fh);
228 } else {
229 fh->isdir = 1;
230 strcpy(fh->path, "");
231 }
232
233 return &fh->base;
234
235error:
236 free(fh);
237 return NULL;
238}
239
240static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
241 struct efi_file_handle **new_handle,
Heinrich Schuchardtc82f8f62019-01-12 12:02:33 +0100242 u16 *file_name, u64 open_mode, u64 attributes)
Rob Clark2a920802017-09-13 18:05:34 -0400243{
244 struct file_handle *fh = to_fh(file);
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200245 efi_status_t ret;
Rob Clark2a920802017-09-13 18:05:34 -0400246
Simon Glass0c89a312019-01-07 16:44:18 -0700247 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
Heinrich Schuchardt1646e092019-03-19 19:16:23 +0100248 file_name, open_mode, attributes);
Rob Clark2a920802017-09-13 18:05:34 -0400249
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200250 /* Check parameters */
Heinrich Schuchardtd3dce352018-09-15 20:43:46 +0200251 if (!file || !new_handle || !file_name) {
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200252 ret = EFI_INVALID_PARAMETER;
253 goto out;
254 }
255 if (open_mode != EFI_FILE_MODE_READ &&
256 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
257 open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
258 EFI_FILE_MODE_CREATE)) {
259 ret = EFI_INVALID_PARAMETER;
260 goto out;
261 }
Heinrich Schuchardtbd665882018-09-13 21:31:49 +0200262 /*
263 * The UEFI spec requires that attributes are only set in create mode.
264 * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
265 * read mode. EDK2 does not check that attributes are zero if not in
266 * create mode.
267 *
268 * So here we only check attributes in create mode and do not check
269 * that they are zero otherwise.
270 */
271 if ((open_mode & EFI_FILE_MODE_CREATE) &&
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200272 (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
273 ret = EFI_INVALID_PARAMETER;
274 goto out;
275 }
Rob Clark2a920802017-09-13 18:05:34 -0400276
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200277 /* Open file */
278 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
Heinrich Schuchardt19b2d892019-04-06 16:36:16 +0200279 if (*new_handle) {
280 EFI_PRINT("file handle %p\n", *new_handle);
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200281 ret = EFI_SUCCESS;
Heinrich Schuchardt19b2d892019-04-06 16:36:16 +0200282 } else {
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200283 ret = EFI_NOT_FOUND;
Heinrich Schuchardt19b2d892019-04-06 16:36:16 +0200284 }
Heinrich Schuchardt143acd12018-09-12 18:43:58 +0200285out:
286 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400287}
288
289static efi_status_t file_close(struct file_handle *fh)
290{
291 fs_closedir(fh->dirs);
292 free(fh);
293 return EFI_SUCCESS;
294}
295
296static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
297{
298 struct file_handle *fh = to_fh(file);
299 EFI_ENTRY("%p", file);
300 return EFI_EXIT(file_close(fh));
301}
302
303static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
304{
305 struct file_handle *fh = to_fh(file);
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900306 efi_status_t ret = EFI_SUCCESS;
307
Rob Clark2a920802017-09-13 18:05:34 -0400308 EFI_ENTRY("%p", file);
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900309
Heinrich Schuchardtfa390812019-06-17 22:00:13 +0200310 if (set_blk_dev(fh) || fs_unlink(fh->path))
311 ret = EFI_WARN_DELETE_FAILURE;
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900312
Rob Clark2a920802017-09-13 18:05:34 -0400313 file_close(fh);
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900314 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400315}
316
317static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
318 void *buffer)
319{
320 loff_t actread;
321
Simon Glass2ae843f2018-09-15 00:51:00 -0600322 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
Rob Clark2a920802017-09-13 18:05:34 -0400323 *buffer_size, &actread))
324 return EFI_DEVICE_ERROR;
325
326 *buffer_size = actread;
327 fh->offset += actread;
328
329 return EFI_SUCCESS;
330}
331
332static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
333 void *buffer)
334{
335 struct efi_file_info *info = buffer;
336 struct fs_dirent *dent;
337 unsigned int required_size;
338
339 if (!fh->dirs) {
340 assert(fh->offset == 0);
341 fh->dirs = fs_opendir(fh->path);
342 if (!fh->dirs)
343 return EFI_DEVICE_ERROR;
344 }
345
346 /*
347 * So this is a bit awkward. Since fs layer is stateful and we
348 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
349 * we might have to return without consuming the dent.. so we
350 * have to stash it for next call.
351 */
352 if (fh->dent) {
353 dent = fh->dent;
354 fh->dent = NULL;
355 } else {
356 dent = fs_readdir(fh->dirs);
357 }
358
359
360 if (!dent) {
361 /* no more files in directory: */
362 /* workaround shim.efi bug/quirk.. as find_boot_csv()
363 * loops through directory contents, it initially calls
364 * read w/ zero length buffer to find out how much mem
365 * to allocate for the EFI_FILE_INFO, then allocates,
366 * and then calls a 2nd time. If we return size of
367 * zero the first time, it happily passes that to
368 * AllocateZeroPool(), and when that returns NULL it
369 * thinks it is EFI_OUT_OF_RESOURCES. So on first
370 * call return a non-zero size:
371 */
372 if (*buffer_size == 0)
373 *buffer_size = sizeof(*info);
374 else
375 *buffer_size = 0;
376 return EFI_SUCCESS;
377 }
378
379 /* check buffer size: */
380 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
381 if (*buffer_size < required_size) {
382 *buffer_size = required_size;
383 fh->dent = dent;
384 return EFI_BUFFER_TOO_SMALL;
385 }
386
387 *buffer_size = required_size;
388 memset(info, 0, required_size);
389
390 info->size = required_size;
391 info->file_size = dent->size;
392 info->physical_size = dent->size;
393
394 if (dent->type == FS_DT_DIR)
395 info->attribute |= EFI_FILE_DIRECTORY;
396
Heinrich Schuchardtc82f8f62019-01-12 12:02:33 +0100397 ascii2unicode(info->file_name, dent->name);
Rob Clark2a920802017-09-13 18:05:34 -0400398
399 fh->offset++;
400
401 return EFI_SUCCESS;
402}
403
404static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200405 efi_uintn_t *buffer_size, void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400406{
407 struct file_handle *fh = to_fh(file);
408 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200409 u64 bs;
Rob Clark2a920802017-09-13 18:05:34 -0400410
411 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
412
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200413 if (!buffer_size || !buffer) {
414 ret = EFI_INVALID_PARAMETER;
415 goto error;
416 }
417
Rob Clark2a920802017-09-13 18:05:34 -0400418 if (set_blk_dev(fh)) {
419 ret = EFI_DEVICE_ERROR;
420 goto error;
421 }
422
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200423 bs = *buffer_size;
Rob Clark2a920802017-09-13 18:05:34 -0400424 if (fh->isdir)
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200425 ret = dir_read(fh, &bs, buffer);
Rob Clark2a920802017-09-13 18:05:34 -0400426 else
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200427 ret = file_read(fh, &bs, buffer);
428 if (bs <= SIZE_MAX)
429 *buffer_size = bs;
430 else
431 *buffer_size = SIZE_MAX;
Rob Clark2a920802017-09-13 18:05:34 -0400432
433error:
434 return EFI_EXIT(ret);
435}
436
437static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200438 efi_uintn_t *buffer_size,
439 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400440{
441 struct file_handle *fh = to_fh(file);
442 efi_status_t ret = EFI_SUCCESS;
443 loff_t actwrite;
444
445 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
446
447 if (set_blk_dev(fh)) {
448 ret = EFI_DEVICE_ERROR;
449 goto error;
450 }
451
Simon Glass2ae843f2018-09-15 00:51:00 -0600452 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
Rob Clark2a920802017-09-13 18:05:34 -0400453 &actwrite)) {
454 ret = EFI_DEVICE_ERROR;
455 goto error;
456 }
457
458 *buffer_size = actwrite;
459 fh->offset += actwrite;
460
461error:
462 return EFI_EXIT(ret);
463}
464
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200465/**
466 * efi_file_getpos() - get current position in file
467 *
468 * This function implements the GetPosition service of the EFI file protocol.
469 * See the UEFI spec for details.
470 *
471 * @file: file handle
472 * @pos: pointer to file position
473 * Return: status code
474 */
Rob Clark2a920802017-09-13 18:05:34 -0400475static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200476 u64 *pos)
Rob Clark2a920802017-09-13 18:05:34 -0400477{
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200478 efi_status_t ret = EFI_SUCCESS;
Rob Clark2a920802017-09-13 18:05:34 -0400479 struct file_handle *fh = to_fh(file);
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200480
Rob Clark2a920802017-09-13 18:05:34 -0400481 EFI_ENTRY("%p, %p", file, pos);
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200482
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200483 if (fh->isdir) {
484 ret = EFI_UNSUPPORTED;
485 goto out;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200486 }
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200487
488 *pos = fh->offset;
489out:
490 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400491}
492
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200493/**
494 * efi_file_setpos() - set current position in file
495 *
496 * This function implements the SetPosition service of the EFI file protocol.
497 * See the UEFI spec for details.
498 *
499 * @file: file handle
500 * @pos: new file position
501 * Return: status code
502 */
Rob Clark2a920802017-09-13 18:05:34 -0400503static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200504 u64 pos)
Rob Clark2a920802017-09-13 18:05:34 -0400505{
506 struct file_handle *fh = to_fh(file);
507 efi_status_t ret = EFI_SUCCESS;
508
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200509 EFI_ENTRY("%p, %llu", file, pos);
Rob Clark2a920802017-09-13 18:05:34 -0400510
511 if (fh->isdir) {
512 if (pos != 0) {
513 ret = EFI_UNSUPPORTED;
514 goto error;
515 }
516 fs_closedir(fh->dirs);
517 fh->dirs = NULL;
518 }
519
520 if (pos == ~0ULL) {
521 loff_t file_size;
522
523 if (set_blk_dev(fh)) {
524 ret = EFI_DEVICE_ERROR;
525 goto error;
526 }
527
528 if (fs_size(fh->path, &file_size)) {
529 ret = EFI_DEVICE_ERROR;
530 goto error;
531 }
532
533 pos = file_size;
534 }
535
536 fh->offset = pos;
537
538error:
539 return EFI_EXIT(ret);
540}
541
542static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
Heinrich Schuchardt9c9021e2018-04-04 15:42:09 +0200543 const efi_guid_t *info_type,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200544 efi_uintn_t *buffer_size,
545 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400546{
547 struct file_handle *fh = to_fh(file);
548 efi_status_t ret = EFI_SUCCESS;
549
Heinrich Schuchardt008fb482018-09-14 22:46:34 +0200550 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
Rob Clark2a920802017-09-13 18:05:34 -0400551
552 if (!guidcmp(info_type, &efi_file_info_guid)) {
553 struct efi_file_info *info = buffer;
554 char *filename = basename(fh);
555 unsigned int required_size;
556 loff_t file_size;
557
558 /* check buffer size: */
559 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
560 if (*buffer_size < required_size) {
561 *buffer_size = required_size;
562 ret = EFI_BUFFER_TOO_SMALL;
563 goto error;
564 }
565
566 if (set_blk_dev(fh)) {
567 ret = EFI_DEVICE_ERROR;
568 goto error;
569 }
570
571 if (fs_size(fh->path, &file_size)) {
572 ret = EFI_DEVICE_ERROR;
573 goto error;
574 }
575
576 memset(info, 0, required_size);
577
578 info->size = required_size;
579 info->file_size = file_size;
580 info->physical_size = file_size;
581
582 if (fh->isdir)
583 info->attribute |= EFI_FILE_DIRECTORY;
584
Heinrich Schuchardt02c2f022018-11-05 20:22:12 +0100585 ascii2unicode(info->file_name, filename);
Heinrich Schuchardt9e6835e2018-04-04 15:42:11 +0200586 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
587 struct efi_file_system_info *info = buffer;
588 disk_partition_t part;
589 efi_uintn_t required_size;
590 int r;
591
592 if (fh->fs->part >= 1)
593 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
594 else
595 r = part_get_info_whole_disk(fh->fs->desc, &part);
596 if (r < 0) {
597 ret = EFI_DEVICE_ERROR;
598 goto error;
599 }
600 required_size = sizeof(info) + 2 *
601 (strlen((const char *)part.name) + 1);
602 if (*buffer_size < required_size) {
603 *buffer_size = required_size;
604 ret = EFI_BUFFER_TOO_SMALL;
605 goto error;
606 }
607
608 memset(info, 0, required_size);
609
610 info->size = required_size;
611 info->read_only = true;
612 info->volume_size = part.size * part.blksz;
613 info->free_space = 0;
614 info->block_size = part.blksz;
615 /*
616 * TODO: The volume label is not available in U-Boot.
617 * Use the partition name as substitute.
618 */
619 ascii2unicode((u16 *)info->volume_label,
620 (const char *)part.name);
Rob Clark2a920802017-09-13 18:05:34 -0400621 } else {
622 ret = EFI_UNSUPPORTED;
623 }
624
625error:
626 return EFI_EXIT(ret);
627}
628
629static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
Heinrich Schuchardt9c9021e2018-04-04 15:42:09 +0200630 const efi_guid_t *info_type,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200631 efi_uintn_t buffer_size,
632 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400633{
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200634 struct file_handle *fh = to_fh(file);
635 efi_status_t ret = EFI_UNSUPPORTED;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200636
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200637 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
638
639 if (!guidcmp(info_type, &efi_file_info_guid)) {
640 struct efi_file_info *info = (struct efi_file_info *)buffer;
641 char *filename = basename(fh);
642 char *new_file_name, *pos;
643 loff_t file_size;
644
645 if (buffer_size < sizeof(struct efi_file_info)) {
646 ret = EFI_BAD_BUFFER_SIZE;
647 goto out;
648 }
649 /* We cannot change the directory attribute */
650 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
651 ret = EFI_ACCESS_DENIED;
652 goto out;
653 }
654 /* Check for renaming */
655 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
656 if (!new_file_name) {
657 ret = EFI_OUT_OF_RESOURCES;
658 goto out;
659 }
660 pos = new_file_name;
661 utf16_utf8_strcpy(&pos, info->file_name);
662 if (strcmp(new_file_name, filename)) {
663 /* TODO: we do not support renaming */
664 EFI_PRINT("Renaming not supported\n");
665 free(new_file_name);
666 ret = EFI_ACCESS_DENIED;
667 goto out;
668 }
669 free(new_file_name);
670 /* Check for truncation */
671 if (set_blk_dev(fh)) {
672 ret = EFI_DEVICE_ERROR;
673 goto out;
674 }
675 if (fs_size(fh->path, &file_size)) {
676 ret = EFI_DEVICE_ERROR;
677 goto out;
678 }
679 if (file_size != info->file_size) {
680 /* TODO: we do not support truncation */
681 EFI_PRINT("Truncation not supported\n");
682 ret = EFI_ACCESS_DENIED;
683 goto out;
684 }
685 /*
686 * We do not care for the other attributes
687 * TODO: Support read only
688 */
689 ret = EFI_SUCCESS;
690 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
691 if (buffer_size < sizeof(struct efi_file_system_info)) {
692 ret = EFI_BAD_BUFFER_SIZE;
693 goto out;
694 }
695 } else {
696 ret = EFI_UNSUPPORTED;
697 }
698out:
699 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400700}
701
702static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
703{
704 EFI_ENTRY("%p", file);
705 return EFI_EXIT(EFI_SUCCESS);
706}
707
708static const struct efi_file_handle efi_file_handle_protocol = {
Heinrich Schuchardt17394f92019-03-27 21:41:04 +0100709 /*
710 * TODO: We currently only support EFI file protocol revision 0x00010000
711 * while UEFI specs 2.4 - 2.7 prescribe revision 0x00020000.
712 */
Rob Clark2a920802017-09-13 18:05:34 -0400713 .rev = EFI_FILE_PROTOCOL_REVISION,
714 .open = efi_file_open,
715 .close = efi_file_close,
716 .delete = efi_file_delete,
717 .read = efi_file_read,
718 .write = efi_file_write,
719 .getpos = efi_file_getpos,
720 .setpos = efi_file_setpos,
721 .getinfo = efi_file_getinfo,
722 .setinfo = efi_file_setinfo,
723 .flush = efi_file_flush,
724};
725
Heinrich Schuchardt95288b12019-02-04 21:24:35 +0100726/**
727 * efi_file_from_path() - open file via device path
728 *
729 * @fp: device path
730 * @return: EFI_FILE_PROTOCOL for the file or NULL
731 */
Rob Clark2a920802017-09-13 18:05:34 -0400732struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
733{
734 struct efi_simple_file_system_protocol *v;
735 struct efi_file_handle *f;
736 efi_status_t ret;
737
738 v = efi_fs_from_path(fp);
739 if (!v)
740 return NULL;
741
742 EFI_CALL(ret = v->open_volume(v, &f));
743 if (ret != EFI_SUCCESS)
744 return NULL;
745
Heinrich Schuchardt95288b12019-02-04 21:24:35 +0100746 /* Skip over device-path nodes before the file path. */
Rob Clark2a920802017-09-13 18:05:34 -0400747 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
748 fp = efi_dp_next(fp);
749
Heinrich Schuchardt95288b12019-02-04 21:24:35 +0100750 /*
751 * Step through the nodes of the directory path until the actual file
752 * node is reached which is the final node in the device path.
753 */
Rob Clark2a920802017-09-13 18:05:34 -0400754 while (fp) {
755 struct efi_device_path_file_path *fdp =
756 container_of(fp, struct efi_device_path_file_path, dp);
757 struct efi_file_handle *f2;
Heinrich Schuchardtf62be162019-07-14 20:14:46 +0200758 u16 *filename;
Rob Clark2a920802017-09-13 18:05:34 -0400759
760 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
761 printf("bad file path!\n");
762 f->close(f);
763 return NULL;
764 }
765
Heinrich Schuchardtf62be162019-07-14 20:14:46 +0200766 filename = u16_strdup(fdp->str);
767 if (!filename)
768 return NULL;
769 EFI_CALL(ret = f->open(f, &f2, filename,
Rob Clark2a920802017-09-13 18:05:34 -0400770 EFI_FILE_MODE_READ, 0));
Heinrich Schuchardtf62be162019-07-14 20:14:46 +0200771 free(filename);
Rob Clark2a920802017-09-13 18:05:34 -0400772 if (ret != EFI_SUCCESS)
773 return NULL;
774
775 fp = efi_dp_next(fp);
776
777 EFI_CALL(f->close(f));
778 f = f2;
779 }
780
781 return f;
782}
783
784static efi_status_t EFIAPI
785efi_open_volume(struct efi_simple_file_system_protocol *this,
786 struct efi_file_handle **root)
787{
788 struct file_system *fs = to_fs(this);
789
790 EFI_ENTRY("%p, %p", this, root);
791
AKASHI Takahiro5bc84a12018-09-11 15:59:12 +0900792 *root = file_open(fs, NULL, NULL, 0, 0);
Rob Clark2a920802017-09-13 18:05:34 -0400793
794 return EFI_EXIT(EFI_SUCCESS);
795}
796
797struct efi_simple_file_system_protocol *
798efi_simple_file_system(struct blk_desc *desc, int part,
799 struct efi_device_path *dp)
800{
801 struct file_system *fs;
802
803 fs = calloc(1, sizeof(*fs));
804 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
805 fs->base.open_volume = efi_open_volume;
806 fs->desc = desc;
807 fs->part = part;
808 fs->dp = dp;
809
810 return &fs->base;
811}