blob: 89d65694da3c8e8de891c196c51ca315d32c94c1 [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/*
3 * EFI utils
4 *
5 * 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;
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
55static int is_dir(struct file_handle *fh)
56{
57 struct fs_dir_stream *dirs;
58
59 set_blk_dev(fh);
60 dirs = fs_opendir(fh->path);
61 if (!dirs)
62 return 0;
63
64 fs_closedir(dirs);
65
66 return 1;
67}
68
69/*
70 * Normalize a path which may include either back or fwd slashes,
71 * double slashes, . or .. entries in the path, etc.
72 */
73static int sanitize_path(char *path)
74{
75 char *p;
76
77 /* backslash to slash: */
78 p = path;
79 while ((p = strchr(p, '\\')))
80 *p++ = '/';
81
82 /* handle double-slashes: */
83 p = path;
84 while ((p = strstr(p, "//"))) {
85 char *src = p + 1;
86 memmove(p, src, strlen(src) + 1);
87 }
88
89 /* handle extra /.'s */
90 p = path;
91 while ((p = strstr(p, "/."))) {
92 /*
93 * You'd be tempted to do this *after* handling ".."s
94 * below to avoid having to check if "/." is start of
95 * a "/..", but that won't have the correct results..
96 * for example, "/foo/./../bar" would get resolved to
97 * "/foo/bar" if you did these two passes in the other
98 * order
99 */
100 if (p[2] == '.') {
101 p += 2;
102 continue;
103 }
104 char *src = p + 2;
105 memmove(p, src, strlen(src) + 1);
106 }
107
108 /* handle extra /..'s: */
109 p = path;
110 while ((p = strstr(p, "/.."))) {
111 char *src = p + 3;
112
113 p--;
114
115 /* find beginning of previous path entry: */
116 while (true) {
117 if (p < path)
118 return -1;
119 if (*p == '/')
120 break;
121 p--;
122 }
123
124 memmove(p, src, strlen(src) + 1);
125 }
126
127 return 0;
128}
129
130/* NOTE: despite what you would expect, 'file_name' is actually a path.
131 * With windoze style backlashes, ofc.
132 */
133static struct efi_file_handle *file_open(struct file_system *fs,
AKASHI Takahiro0c672b02018-09-11 15:59:12 +0900134 struct file_handle *parent, s16 *file_name, u64 mode,
135 u64 attributes)
Rob Clark53142032017-09-13 18:05:34 -0400136{
137 struct file_handle *fh;
138 char f0[MAX_UTF8_PER_UTF16] = {0};
139 int plen = 0;
140 int flen = 0;
141
142 if (file_name) {
143 utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1);
Heinrich Schuchardt597f8f72018-08-31 21:31:26 +0200144 flen = u16_strlen((u16 *)file_name);
Rob Clark53142032017-09-13 18:05:34 -0400145 }
146
147 /* we could have a parent, but also an absolute path: */
148 if (f0[0] == '\\') {
149 plen = 0;
150 } else if (parent) {
151 plen = strlen(parent->path) + 1;
152 }
153
154 /* +2 is for null and '/' */
155 fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
156
157 fh->base = efi_file_handle_protocol;
158 fh->fs = fs;
159
160 if (parent) {
161 char *p = fh->path;
162
163 if (plen > 0) {
164 strcpy(p, parent->path);
165 p += plen - 1;
166 *p++ = '/';
167 }
168
169 utf16_to_utf8((u8 *)p, (u16 *)file_name, flen);
170
171 if (sanitize_path(fh->path))
172 goto error;
173
174 /* check if file exists: */
175 if (set_blk_dev(fh))
176 goto error;
177
AKASHI Takahiro0c672b02018-09-11 15:59:12 +0900178 if ((mode & EFI_FILE_MODE_CREATE) &&
179 (attributes & EFI_FILE_DIRECTORY)) {
180 if (fs_mkdir(fh->path))
181 goto error;
182 } else if (!((mode & EFI_FILE_MODE_CREATE) ||
183 fs_exists(fh->path)))
Rob Clark53142032017-09-13 18:05:34 -0400184 goto error;
185
186 /* figure out if file is a directory: */
187 fh->isdir = is_dir(fh);
188 } else {
189 fh->isdir = 1;
190 strcpy(fh->path, "");
191 }
192
193 return &fh->base;
194
195error:
196 free(fh);
197 return NULL;
198}
199
200static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
201 struct efi_file_handle **new_handle,
202 s16 *file_name, u64 open_mode, u64 attributes)
203{
204 struct file_handle *fh = to_fh(file);
205
206 EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name,
207 open_mode, attributes);
208
AKASHI Takahiro0c672b02018-09-11 15:59:12 +0900209 *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
Rob Clark53142032017-09-13 18:05:34 -0400210 if (!*new_handle)
211 return EFI_EXIT(EFI_NOT_FOUND);
212
213 return EFI_EXIT(EFI_SUCCESS);
214}
215
216static efi_status_t file_close(struct file_handle *fh)
217{
218 fs_closedir(fh->dirs);
219 free(fh);
220 return EFI_SUCCESS;
221}
222
223static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
224{
225 struct file_handle *fh = to_fh(file);
226 EFI_ENTRY("%p", file);
227 return EFI_EXIT(file_close(fh));
228}
229
230static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
231{
232 struct file_handle *fh = to_fh(file);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900233 efi_status_t ret = EFI_SUCCESS;
234
Rob Clark53142032017-09-13 18:05:34 -0400235 EFI_ENTRY("%p", file);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900236
237 if (set_blk_dev(fh)) {
238 ret = EFI_DEVICE_ERROR;
239 goto error;
240 }
241
242 if (fs_unlink(fh->path))
243 ret = EFI_DEVICE_ERROR;
Rob Clark53142032017-09-13 18:05:34 -0400244 file_close(fh);
AKASHI Takahiro4dd5b9c2018-09-11 15:59:16 +0900245
246error:
247 return EFI_EXIT(ret);
Rob Clark53142032017-09-13 18:05:34 -0400248}
249
250static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
251 void *buffer)
252{
253 loff_t actread;
Alexander Grafab133c72018-08-08 03:54:32 -0600254 /* fs_read expects buffer address, not pointer */
255 uintptr_t buffer_addr = (uintptr_t)map_to_sysmem(buffer);
Rob Clark53142032017-09-13 18:05:34 -0400256
Alexander Grafab133c72018-08-08 03:54:32 -0600257 if (fs_read(fh->path, buffer_addr, fh->offset,
Rob Clark53142032017-09-13 18:05:34 -0400258 *buffer_size, &actread))
259 return EFI_DEVICE_ERROR;
260
261 *buffer_size = actread;
262 fh->offset += actread;
263
264 return EFI_SUCCESS;
265}
266
267static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
268 void *buffer)
269{
270 struct efi_file_info *info = buffer;
271 struct fs_dirent *dent;
272 unsigned int required_size;
273
274 if (!fh->dirs) {
275 assert(fh->offset == 0);
276 fh->dirs = fs_opendir(fh->path);
277 if (!fh->dirs)
278 return EFI_DEVICE_ERROR;
279 }
280
281 /*
282 * So this is a bit awkward. Since fs layer is stateful and we
283 * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
284 * we might have to return without consuming the dent.. so we
285 * have to stash it for next call.
286 */
287 if (fh->dent) {
288 dent = fh->dent;
289 fh->dent = NULL;
290 } else {
291 dent = fs_readdir(fh->dirs);
292 }
293
294
295 if (!dent) {
296 /* no more files in directory: */
297 /* workaround shim.efi bug/quirk.. as find_boot_csv()
298 * loops through directory contents, it initially calls
299 * read w/ zero length buffer to find out how much mem
300 * to allocate for the EFI_FILE_INFO, then allocates,
301 * and then calls a 2nd time. If we return size of
302 * zero the first time, it happily passes that to
303 * AllocateZeroPool(), and when that returns NULL it
304 * thinks it is EFI_OUT_OF_RESOURCES. So on first
305 * call return a non-zero size:
306 */
307 if (*buffer_size == 0)
308 *buffer_size = sizeof(*info);
309 else
310 *buffer_size = 0;
311 return EFI_SUCCESS;
312 }
313
314 /* check buffer size: */
315 required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
316 if (*buffer_size < required_size) {
317 *buffer_size = required_size;
318 fh->dent = dent;
319 return EFI_BUFFER_TOO_SMALL;
320 }
321
322 *buffer_size = required_size;
323 memset(info, 0, required_size);
324
325 info->size = required_size;
326 info->file_size = dent->size;
327 info->physical_size = dent->size;
328
329 if (dent->type == FS_DT_DIR)
330 info->attribute |= EFI_FILE_DIRECTORY;
331
332 ascii2unicode((u16 *)info->file_name, dent->name);
333
334 fh->offset++;
335
336 return EFI_SUCCESS;
337}
338
339static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200340 efi_uintn_t *buffer_size, void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400341{
342 struct file_handle *fh = to_fh(file);
343 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200344 u64 bs;
Rob Clark53142032017-09-13 18:05:34 -0400345
346 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
347
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200348 if (!buffer_size || !buffer) {
349 ret = EFI_INVALID_PARAMETER;
350 goto error;
351 }
352
Rob Clark53142032017-09-13 18:05:34 -0400353 if (set_blk_dev(fh)) {
354 ret = EFI_DEVICE_ERROR;
355 goto error;
356 }
357
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200358 bs = *buffer_size;
Rob Clark53142032017-09-13 18:05:34 -0400359 if (fh->isdir)
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200360 ret = dir_read(fh, &bs, buffer);
Rob Clark53142032017-09-13 18:05:34 -0400361 else
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200362 ret = file_read(fh, &bs, buffer);
363 if (bs <= SIZE_MAX)
364 *buffer_size = bs;
365 else
366 *buffer_size = SIZE_MAX;
Rob Clark53142032017-09-13 18:05:34 -0400367
368error:
369 return EFI_EXIT(ret);
370}
371
372static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200373 efi_uintn_t *buffer_size,
374 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400375{
376 struct file_handle *fh = to_fh(file);
377 efi_status_t ret = EFI_SUCCESS;
378 loff_t actwrite;
379
380 EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
381
382 if (set_blk_dev(fh)) {
383 ret = EFI_DEVICE_ERROR;
384 goto error;
385 }
386
387 if (fs_write(fh->path, (ulong)buffer, fh->offset, *buffer_size,
388 &actwrite)) {
389 ret = EFI_DEVICE_ERROR;
390 goto error;
391 }
392
393 *buffer_size = actwrite;
394 fh->offset += actwrite;
395
396error:
397 return EFI_EXIT(ret);
398}
399
400static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200401 efi_uintn_t *pos)
Rob Clark53142032017-09-13 18:05:34 -0400402{
403 struct file_handle *fh = to_fh(file);
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200404
Rob Clark53142032017-09-13 18:05:34 -0400405 EFI_ENTRY("%p, %p", file, pos);
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200406
407 if (fh->offset <= SIZE_MAX) {
408 *pos = fh->offset;
409 return EFI_EXIT(EFI_SUCCESS);
410 } else {
411 return EFI_EXIT(EFI_DEVICE_ERROR);
412 }
Rob Clark53142032017-09-13 18:05:34 -0400413}
414
415static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200416 efi_uintn_t pos)
Rob Clark53142032017-09-13 18:05:34 -0400417{
418 struct file_handle *fh = to_fh(file);
419 efi_status_t ret = EFI_SUCCESS;
420
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200421 EFI_ENTRY("%p, %zu", file, pos);
Rob Clark53142032017-09-13 18:05:34 -0400422
423 if (fh->isdir) {
424 if (pos != 0) {
425 ret = EFI_UNSUPPORTED;
426 goto error;
427 }
428 fs_closedir(fh->dirs);
429 fh->dirs = NULL;
430 }
431
432 if (pos == ~0ULL) {
433 loff_t file_size;
434
435 if (set_blk_dev(fh)) {
436 ret = EFI_DEVICE_ERROR;
437 goto error;
438 }
439
440 if (fs_size(fh->path, &file_size)) {
441 ret = EFI_DEVICE_ERROR;
442 goto error;
443 }
444
445 pos = file_size;
446 }
447
448 fh->offset = pos;
449
450error:
451 return EFI_EXIT(ret);
452}
453
454static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
Heinrich Schuchardte86380d2018-04-04 15:42:09 +0200455 const efi_guid_t *info_type,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200456 efi_uintn_t *buffer_size,
457 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400458{
459 struct file_handle *fh = to_fh(file);
460 efi_status_t ret = EFI_SUCCESS;
461
462 EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer);
463
464 if (!guidcmp(info_type, &efi_file_info_guid)) {
465 struct efi_file_info *info = buffer;
466 char *filename = basename(fh);
467 unsigned int required_size;
468 loff_t file_size;
469
470 /* check buffer size: */
471 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
472 if (*buffer_size < required_size) {
473 *buffer_size = required_size;
474 ret = EFI_BUFFER_TOO_SMALL;
475 goto error;
476 }
477
478 if (set_blk_dev(fh)) {
479 ret = EFI_DEVICE_ERROR;
480 goto error;
481 }
482
483 if (fs_size(fh->path, &file_size)) {
484 ret = EFI_DEVICE_ERROR;
485 goto error;
486 }
487
488 memset(info, 0, required_size);
489
490 info->size = required_size;
491 info->file_size = file_size;
492 info->physical_size = file_size;
493
494 if (fh->isdir)
495 info->attribute |= EFI_FILE_DIRECTORY;
496
497 ascii2unicode((u16 *)info->file_name, filename);
Heinrich Schuchardt37fb4b92018-04-04 15:42:11 +0200498 } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
499 struct efi_file_system_info *info = buffer;
500 disk_partition_t part;
501 efi_uintn_t required_size;
502 int r;
503
504 if (fh->fs->part >= 1)
505 r = part_get_info(fh->fs->desc, fh->fs->part, &part);
506 else
507 r = part_get_info_whole_disk(fh->fs->desc, &part);
508 if (r < 0) {
509 ret = EFI_DEVICE_ERROR;
510 goto error;
511 }
512 required_size = sizeof(info) + 2 *
513 (strlen((const char *)part.name) + 1);
514 if (*buffer_size < required_size) {
515 *buffer_size = required_size;
516 ret = EFI_BUFFER_TOO_SMALL;
517 goto error;
518 }
519
520 memset(info, 0, required_size);
521
522 info->size = required_size;
523 info->read_only = true;
524 info->volume_size = part.size * part.blksz;
525 info->free_space = 0;
526 info->block_size = part.blksz;
527 /*
528 * TODO: The volume label is not available in U-Boot.
529 * Use the partition name as substitute.
530 */
531 ascii2unicode((u16 *)info->volume_label,
532 (const char *)part.name);
Rob Clark53142032017-09-13 18:05:34 -0400533 } else {
534 ret = EFI_UNSUPPORTED;
535 }
536
537error:
538 return EFI_EXIT(ret);
539}
540
541static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
Heinrich Schuchardte86380d2018-04-04 15:42:09 +0200542 const efi_guid_t *info_type,
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200543 efi_uintn_t buffer_size,
544 void *buffer)
Rob Clark53142032017-09-13 18:05:34 -0400545{
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +0200546 EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
547
Rob Clark53142032017-09-13 18:05:34 -0400548 return EFI_EXIT(EFI_UNSUPPORTED);
549}
550
551static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
552{
553 EFI_ENTRY("%p", file);
554 return EFI_EXIT(EFI_SUCCESS);
555}
556
557static const struct efi_file_handle efi_file_handle_protocol = {
558 .rev = EFI_FILE_PROTOCOL_REVISION,
559 .open = efi_file_open,
560 .close = efi_file_close,
561 .delete = efi_file_delete,
562 .read = efi_file_read,
563 .write = efi_file_write,
564 .getpos = efi_file_getpos,
565 .setpos = efi_file_setpos,
566 .getinfo = efi_file_getinfo,
567 .setinfo = efi_file_setinfo,
568 .flush = efi_file_flush,
569};
570
571struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
572{
573 struct efi_simple_file_system_protocol *v;
574 struct efi_file_handle *f;
575 efi_status_t ret;
576
577 v = efi_fs_from_path(fp);
578 if (!v)
579 return NULL;
580
581 EFI_CALL(ret = v->open_volume(v, &f));
582 if (ret != EFI_SUCCESS)
583 return NULL;
584
585 /* skip over device-path nodes before the file path: */
586 while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
587 fp = efi_dp_next(fp);
588
589 while (fp) {
590 struct efi_device_path_file_path *fdp =
591 container_of(fp, struct efi_device_path_file_path, dp);
592 struct efi_file_handle *f2;
593
594 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
595 printf("bad file path!\n");
596 f->close(f);
597 return NULL;
598 }
599
600 EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
601 EFI_FILE_MODE_READ, 0));
602 if (ret != EFI_SUCCESS)
603 return NULL;
604
605 fp = efi_dp_next(fp);
606
607 EFI_CALL(f->close(f));
608 f = f2;
609 }
610
611 return f;
612}
613
614static efi_status_t EFIAPI
615efi_open_volume(struct efi_simple_file_system_protocol *this,
616 struct efi_file_handle **root)
617{
618 struct file_system *fs = to_fs(this);
619
620 EFI_ENTRY("%p, %p", this, root);
621
AKASHI Takahiro0c672b02018-09-11 15:59:12 +0900622 *root = file_open(fs, NULL, NULL, 0, 0);
Rob Clark53142032017-09-13 18:05:34 -0400623
624 return EFI_EXIT(EFI_SUCCESS);
625}
626
627struct efi_simple_file_system_protocol *
628efi_simple_file_system(struct blk_desc *desc, int part,
629 struct efi_device_path *dp)
630{
631 struct file_system *fs;
632
633 fs = calloc(1, sizeof(*fs));
634 fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
635 fs->base.open_volume = efi_open_volume;
636 fs->desc = desc;
637 fs->part = part;
638 fs->dp = dp;
639
640 return &fs->base;
641}