blob: 182d735e6bc28cadf6a83823cdb5d344020480e0 [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
310 if (set_blk_dev(fh)) {
311 ret = EFI_DEVICE_ERROR;
312 goto error;
313 }
314
315 if (fs_unlink(fh->path))
316 ret = EFI_DEVICE_ERROR;
Rob Clark2a920802017-09-13 18:05:34 -0400317 file_close(fh);
AKASHI Takahirod39f6a62018-09-11 15:59:16 +0900318
319error:
320 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400321}
322
323static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
324 void *buffer)
325{
326 loff_t actread;
327
Simon Glass2ae843f2018-09-15 00:51:00 -0600328 if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
Rob Clark2a920802017-09-13 18:05:34 -0400329 *buffer_size, &actread))
330 return EFI_DEVICE_ERROR;
331
332 *buffer_size = actread;
333 fh->offset += actread;
334
335 return EFI_SUCCESS;
336}
337
338static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
339 void *buffer)
340{
341 struct efi_file_info *info = buffer;
342 struct fs_dirent *dent;
343 unsigned int required_size;
344
345 if (!fh->dirs) {
346 assert(fh->offset == 0);
347 fh->dirs = fs_opendir(fh->path);
348 if (!fh->dirs)
349 return EFI_DEVICE_ERROR;
350 }
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;
360 fh->dent = NULL;
361 } else {
362 dent = fs_readdir(fh->dirs);
363 }
364
365
366 if (!dent) {
367 /* no more files in directory: */
368 /* workaround shim.efi bug/quirk.. as find_boot_csv()
369 * loops through directory contents, it initially calls
370 * read w/ zero length buffer to find out how much mem
371 * to allocate for the EFI_FILE_INFO, then allocates,
372 * and then calls a 2nd time. If we return size of
373 * zero the first time, it happily passes that to
374 * AllocateZeroPool(), and when that returns NULL it
375 * thinks it is EFI_OUT_OF_RESOURCES. So on first
376 * call return a non-zero size:
377 */
378 if (*buffer_size == 0)
379 *buffer_size = sizeof(*info);
380 else
381 *buffer_size = 0;
382 return EFI_SUCCESS;
383 }
384
385 /* check buffer size: */
386 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
387 if (*buffer_size < required_size) {
388 *buffer_size = required_size;
389 fh->dent = dent;
390 return EFI_BUFFER_TOO_SMALL;
391 }
392
393 *buffer_size = required_size;
394 memset(info, 0, required_size);
395
396 info->size = required_size;
397 info->file_size = dent->size;
398 info->physical_size = dent->size;
399
400 if (dent->type == FS_DT_DIR)
401 info->attribute |= EFI_FILE_DIRECTORY;
402
Heinrich Schuchardtc82f8f62019-01-12 12:02:33 +0100403 ascii2unicode(info->file_name, dent->name);
Rob Clark2a920802017-09-13 18:05:34 -0400404
405 fh->offset++;
406
407 return EFI_SUCCESS;
408}
409
410static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200411 efi_uintn_t *buffer_size, void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400412{
413 struct file_handle *fh = to_fh(file);
414 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200415 u64 bs;
Rob Clark2a920802017-09-13 18:05:34 -0400416
417 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
418
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200419 if (!buffer_size || !buffer) {
420 ret = EFI_INVALID_PARAMETER;
421 goto error;
422 }
423
Rob Clark2a920802017-09-13 18:05:34 -0400424 if (set_blk_dev(fh)) {
425 ret = EFI_DEVICE_ERROR;
426 goto error;
427 }
428
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200429 bs = *buffer_size;
Rob Clark2a920802017-09-13 18:05:34 -0400430 if (fh->isdir)
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200431 ret = dir_read(fh, &bs, buffer);
Rob Clark2a920802017-09-13 18:05:34 -0400432 else
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200433 ret = file_read(fh, &bs, buffer);
434 if (bs <= SIZE_MAX)
435 *buffer_size = bs;
436 else
437 *buffer_size = SIZE_MAX;
Rob Clark2a920802017-09-13 18:05:34 -0400438
439error:
440 return EFI_EXIT(ret);
441}
442
443static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200444 efi_uintn_t *buffer_size,
445 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400446{
447 struct file_handle *fh = to_fh(file);
448 efi_status_t ret = EFI_SUCCESS;
449 loff_t actwrite;
450
451 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
452
453 if (set_blk_dev(fh)) {
454 ret = EFI_DEVICE_ERROR;
455 goto error;
456 }
457
Simon Glass2ae843f2018-09-15 00:51:00 -0600458 if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
Rob Clark2a920802017-09-13 18:05:34 -0400459 &actwrite)) {
460 ret = EFI_DEVICE_ERROR;
461 goto error;
462 }
463
464 *buffer_size = actwrite;
465 fh->offset += actwrite;
466
467error:
468 return EFI_EXIT(ret);
469}
470
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200471/**
472 * efi_file_getpos() - get current position in file
473 *
474 * This function implements the GetPosition service of the EFI file protocol.
475 * See the UEFI spec for details.
476 *
477 * @file: file handle
478 * @pos: pointer to file position
479 * Return: status code
480 */
Rob Clark2a920802017-09-13 18:05:34 -0400481static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200482 u64 *pos)
Rob Clark2a920802017-09-13 18:05:34 -0400483{
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200484 efi_status_t ret = EFI_SUCCESS;
Rob Clark2a920802017-09-13 18:05:34 -0400485 struct file_handle *fh = to_fh(file);
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200486
Rob Clark2a920802017-09-13 18:05:34 -0400487 EFI_ENTRY("%p, %p", file, pos);
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200488
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200489 if (fh->isdir) {
490 ret = EFI_UNSUPPORTED;
491 goto out;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200492 }
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200493
494 *pos = fh->offset;
495out:
496 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400497}
498
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200499/**
500 * efi_file_setpos() - set current position in file
501 *
502 * This function implements the SetPosition service of the EFI file protocol.
503 * See the UEFI spec for details.
504 *
505 * @file: file handle
506 * @pos: new file position
507 * Return: status code
508 */
Rob Clark2a920802017-09-13 18:05:34 -0400509static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200510 u64 pos)
Rob Clark2a920802017-09-13 18:05:34 -0400511{
512 struct file_handle *fh = to_fh(file);
513 efi_status_t ret = EFI_SUCCESS;
514
Heinrich Schuchardt0801d4d2018-10-07 05:26:26 +0200515 EFI_ENTRY("%p, %llu", file, pos);
Rob Clark2a920802017-09-13 18:05:34 -0400516
517 if (fh->isdir) {
518 if (pos != 0) {
519 ret = EFI_UNSUPPORTED;
520 goto error;
521 }
522 fs_closedir(fh->dirs);
523 fh->dirs = NULL;
524 }
525
526 if (pos == ~0ULL) {
527 loff_t file_size;
528
529 if (set_blk_dev(fh)) {
530 ret = EFI_DEVICE_ERROR;
531 goto error;
532 }
533
534 if (fs_size(fh->path, &file_size)) {
535 ret = EFI_DEVICE_ERROR;
536 goto error;
537 }
538
539 pos = file_size;
540 }
541
542 fh->offset = pos;
543
544error:
545 return EFI_EXIT(ret);
546}
547
548static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
Heinrich Schuchardt9c9021e2018-04-04 15:42:09 +0200549 const efi_guid_t *info_type,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200550 efi_uintn_t *buffer_size,
551 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400552{
553 struct file_handle *fh = to_fh(file);
554 efi_status_t ret = EFI_SUCCESS;
555
Heinrich Schuchardt008fb482018-09-14 22:46:34 +0200556 EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
Rob Clark2a920802017-09-13 18:05:34 -0400557
558 if (!guidcmp(info_type, &efi_file_info_guid)) {
559 struct efi_file_info *info = buffer;
560 char *filename = basename(fh);
561 unsigned int required_size;
562 loff_t file_size;
563
564 /* check buffer size: */
565 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
566 if (*buffer_size < required_size) {
567 *buffer_size = required_size;
568 ret = EFI_BUFFER_TOO_SMALL;
569 goto error;
570 }
571
572 if (set_blk_dev(fh)) {
573 ret = EFI_DEVICE_ERROR;
574 goto error;
575 }
576
577 if (fs_size(fh->path, &file_size)) {
578 ret = EFI_DEVICE_ERROR;
579 goto error;
580 }
581
582 memset(info, 0, required_size);
583
584 info->size = required_size;
585 info->file_size = file_size;
586 info->physical_size = file_size;
587
588 if (fh->isdir)
589 info->attribute |= EFI_FILE_DIRECTORY;
590
Heinrich Schuchardt02c2f022018-11-05 20:22:12 +0100591 ascii2unicode(info->file_name, filename);
Heinrich Schuchardt9e6835e2018-04-04 15:42:11 +0200592 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
593 struct efi_file_system_info *info = buffer;
594 disk_partition_t part;
595 efi_uintn_t required_size;
596 int r;
597
598 if (fh->fs->part >= 1)
599 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
600 else
601 r = part_get_info_whole_disk(fh->fs->desc, &part);
602 if (r < 0) {
603 ret = EFI_DEVICE_ERROR;
604 goto error;
605 }
606 required_size = sizeof(info) + 2 *
607 (strlen((const char *)part.name) + 1);
608 if (*buffer_size < required_size) {
609 *buffer_size = required_size;
610 ret = EFI_BUFFER_TOO_SMALL;
611 goto error;
612 }
613
614 memset(info, 0, required_size);
615
616 info->size = required_size;
617 info->read_only = true;
618 info->volume_size = part.size * part.blksz;
619 info->free_space = 0;
620 info->block_size = part.blksz;
621 /*
622 * TODO: The volume label is not available in U-Boot.
623 * Use the partition name as substitute.
624 */
625 ascii2unicode((u16 *)info->volume_label,
626 (const char *)part.name);
Rob Clark2a920802017-09-13 18:05:34 -0400627 } else {
628 ret = EFI_UNSUPPORTED;
629 }
630
631error:
632 return EFI_EXIT(ret);
633}
634
635static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
Heinrich Schuchardt9c9021e2018-04-04 15:42:09 +0200636 const efi_guid_t *info_type,
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200637 efi_uintn_t buffer_size,
638 void *buffer)
Rob Clark2a920802017-09-13 18:05:34 -0400639{
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200640 struct file_handle *fh = to_fh(file);
641 efi_status_t ret = EFI_UNSUPPORTED;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +0200642
Heinrich Schuchardtfbe4c7d2019-04-06 18:17:39 +0200643 EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
644
645 if (!guidcmp(info_type, &efi_file_info_guid)) {
646 struct efi_file_info *info = (struct efi_file_info *)buffer;
647 char *filename = basename(fh);
648 char *new_file_name, *pos;
649 loff_t file_size;
650
651 if (buffer_size < sizeof(struct efi_file_info)) {
652 ret = EFI_BAD_BUFFER_SIZE;
653 goto out;
654 }
655 /* We cannot change the directory attribute */
656 if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
657 ret = EFI_ACCESS_DENIED;
658 goto out;
659 }
660 /* Check for renaming */
661 new_file_name = malloc(utf16_utf8_strlen(info->file_name));
662 if (!new_file_name) {
663 ret = EFI_OUT_OF_RESOURCES;
664 goto out;
665 }
666 pos = new_file_name;
667 utf16_utf8_strcpy(&pos, info->file_name);
668 if (strcmp(new_file_name, filename)) {
669 /* TODO: we do not support renaming */
670 EFI_PRINT("Renaming not supported\n");
671 free(new_file_name);
672 ret = EFI_ACCESS_DENIED;
673 goto out;
674 }
675 free(new_file_name);
676 /* Check for truncation */
677 if (set_blk_dev(fh)) {
678 ret = EFI_DEVICE_ERROR;
679 goto out;
680 }
681 if (fs_size(fh->path, &file_size)) {
682 ret = EFI_DEVICE_ERROR;
683 goto out;
684 }
685 if (file_size != info->file_size) {
686 /* TODO: we do not support truncation */
687 EFI_PRINT("Truncation not supported\n");
688 ret = EFI_ACCESS_DENIED;
689 goto out;
690 }
691 /*
692 * We do not care for the other attributes
693 * TODO: Support read only
694 */
695 ret = EFI_SUCCESS;
696 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
697 if (buffer_size < sizeof(struct efi_file_system_info)) {
698 ret = EFI_BAD_BUFFER_SIZE;
699 goto out;
700 }
701 } else {
702 ret = EFI_UNSUPPORTED;
703 }
704out:
705 return EFI_EXIT(ret);
Rob Clark2a920802017-09-13 18:05:34 -0400706}
707
708static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
709{
710 EFI_ENTRY("%p", file);
711 return EFI_EXIT(EFI_SUCCESS);
712}
713
714static const struct efi_file_handle efi_file_handle_protocol = {
Heinrich Schuchardt17394f92019-03-27 21:41:04 +0100715 /*
716 * TODO: We currently only support EFI file protocol revision 0x00010000
717 * while UEFI specs 2.4 - 2.7 prescribe revision 0x00020000.
718 */
Rob Clark2a920802017-09-13 18:05:34 -0400719 .rev = EFI_FILE_PROTOCOL_REVISION,
720 .open = efi_file_open,
721 .close = efi_file_close,
722 .delete = efi_file_delete,
723 .read = efi_file_read,
724 .write = efi_file_write,
725 .getpos = efi_file_getpos,
726 .setpos = efi_file_setpos,
727 .getinfo = efi_file_getinfo,
728 .setinfo = efi_file_setinfo,
729 .flush = efi_file_flush,
730};
731
Heinrich Schuchardt95288b12019-02-04 21:24:35 +0100732/**
733 * efi_file_from_path() - open file via device path
734 *
735 * @fp: device path
736 * @return: EFI_FILE_PROTOCOL for the file or NULL
737 */
Rob Clark2a920802017-09-13 18:05:34 -0400738struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
739{
740 struct efi_simple_file_system_protocol *v;
741 struct efi_file_handle *f;
742 efi_status_t ret;
743
744 v = efi_fs_from_path(fp);
745 if (!v)
746 return NULL;
747
748 EFI_CALL(ret = v->open_volume(v, &f));
749 if (ret != EFI_SUCCESS)
750 return NULL;
751
Heinrich Schuchardt95288b12019-02-04 21:24:35 +0100752 /* Skip over device-path nodes before the file path. */
Rob Clark2a920802017-09-13 18:05:34 -0400753 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
754 fp = efi_dp_next(fp);
755
Heinrich Schuchardt95288b12019-02-04 21:24:35 +0100756 /*
757 * Step through the nodes of the directory path until the actual file
758 * node is reached which is the final node in the device path.
759 */
Rob Clark2a920802017-09-13 18:05:34 -0400760 while (fp) {
761 struct efi_device_path_file_path *fdp =
762 container_of(fp, struct efi_device_path_file_path, dp);
763 struct efi_file_handle *f2;
764
765 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
766 printf("bad file path!\n");
767 f->close(f);
768 return NULL;
769 }
770
Heinrich Schuchardtc82f8f62019-01-12 12:02:33 +0100771 EFI_CALL(ret = f->open(f, &f2, fdp->str,
Rob Clark2a920802017-09-13 18:05:34 -0400772 EFI_FILE_MODE_READ, 0));
773 if (ret != EFI_SUCCESS)
774 return NULL;
775
776 fp = efi_dp_next(fp);
777
778 EFI_CALL(f->close(f));
779 f = f2;
780 }
781
782 return f;
783}
784
785static efi_status_t EFIAPI
786efi_open_volume(struct efi_simple_file_system_protocol *this,
787 struct efi_file_handle **root)
788{
789 struct file_system *fs = to_fs(this);
790
791 EFI_ENTRY("%p, %p", this, root);
792
AKASHI Takahiro5bc84a12018-09-11 15:59:12 +0900793 *root = file_open(fs, NULL, NULL, 0, 0);
Rob Clark2a920802017-09-13 18:05:34 -0400794
795 return EFI_EXIT(EFI_SUCCESS);
796}
797
798struct efi_simple_file_system_protocol *
799efi_simple_file_system(struct blk_desc *desc, int part,
800 struct efi_device_path *dp)
801{
802 struct file_system *fs;
803
804 fs = calloc(1, sizeof(*fs));
805 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
806 fs->base.open_volume = efi_open_volume;
807 fs->desc = desc;
808 fs->part = part;
809 fs->dp = dp;
810
811 return &fs->base;
812}