be3e55f4d87bc2b6438328c631f118cbfb140bdd
[oweals/u-boot.git] / lib / efi_loader / efi_file.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI utils
4  *
5  *  Copyright (c) 2017 Rob Clark
6  */
7
8 #include <common.h>
9 #include <charset.h>
10 #include <efi_loader.h>
11 #include <malloc.h>
12 #include <mapmem.h>
13 #include <fs.h>
14
15 /* GUID for file system information */
16 const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
17
18 struct 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
26 struct 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
40 static const struct efi_file_handle efi_file_handle_protocol;
41
42 static 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
50 static 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
55 /**
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  */
63 static int is_dir(struct file_handle *fh)
64 {
65         struct fs_dir_stream *dirs;
66
67         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  */
80 static 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
137 /**
138  * file_open() - open a file handle
139  *
140  * @fs:                 file system
141  * @parent:             directory relative to which the file is to be opened
142  * @file_name:          path of the file to be opened. '\', '.', or '..' may
143  *                      be used as modifiers. A leading backslash indicates an
144  *                      absolute path.
145  * @mode:               bit mask indicating the access mode (read, write,
146  *                      create)
147  * @attributes:         attributes for newly created file
148  * Returns:             handle to the opened file or NULL
149  */
150 static struct efi_file_handle *file_open(struct file_system *fs,
151                 struct file_handle *parent, u16 *file_name, u64 mode,
152                 u64 attributes)
153 {
154         struct file_handle *fh;
155         char f0[MAX_UTF8_PER_UTF16] = {0};
156         int plen = 0;
157         int flen = 0;
158
159         if (file_name) {
160                 utf16_to_utf8((u8 *)f0, file_name, 1);
161                 flen = u16_strlen(file_name);
162         }
163
164         /* we could have a parent, but also an absolute path: */
165         if (f0[0] == '\\') {
166                 plen = 0;
167         } else if (parent) {
168                 plen = strlen(parent->path) + 1;
169         }
170
171         /* +2 is for null and '/' */
172         fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
173
174         fh->base = efi_file_handle_protocol;
175         fh->fs = fs;
176
177         if (parent) {
178                 char *p = fh->path;
179
180                 if (plen > 0) {
181                         strcpy(p, parent->path);
182                         p += plen - 1;
183                         *p++ = '/';
184                 }
185
186                 utf16_to_utf8((u8 *)p, file_name, flen);
187
188                 if (sanitize_path(fh->path))
189                         goto error;
190
191                 /* check if file exists: */
192                 if (set_blk_dev(fh))
193                         goto error;
194
195                 if ((mode & EFI_FILE_MODE_CREATE) &&
196                     (attributes & EFI_FILE_DIRECTORY)) {
197                         if (fs_mkdir(fh->path))
198                                 goto error;
199                 } else if (!((mode & EFI_FILE_MODE_CREATE) ||
200                              fs_exists(fh->path)))
201                         goto error;
202
203                 /* fs_exists() calls fs_close(), so open file system again */
204                 if (set_blk_dev(fh))
205                         goto error;
206
207                 /* figure out if file is a directory: */
208                 fh->isdir = is_dir(fh);
209         } else {
210                 fh->isdir = 1;
211                 strcpy(fh->path, "");
212         }
213
214         return &fh->base;
215
216 error:
217         free(fh);
218         return NULL;
219 }
220
221 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
222                 struct efi_file_handle **new_handle,
223                 u16 *file_name, u64 open_mode, u64 attributes)
224 {
225         struct file_handle *fh = to_fh(file);
226         efi_status_t ret;
227
228         EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
229                   file_name, open_mode, attributes);
230
231         /* Check parameters */
232         if (!file || !new_handle || !file_name) {
233                 ret = EFI_INVALID_PARAMETER;
234                 goto out;
235         }
236         if (open_mode != EFI_FILE_MODE_READ &&
237             open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
238             open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
239                          EFI_FILE_MODE_CREATE)) {
240                 ret = EFI_INVALID_PARAMETER;
241                 goto out;
242         }
243         /*
244          * The UEFI spec requires that attributes are only set in create mode.
245          * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
246          * read mode. EDK2 does not check that attributes are zero if not in
247          * create mode.
248          *
249          * So here we only check attributes in create mode and do not check
250          * that they are zero otherwise.
251          */
252         if ((open_mode & EFI_FILE_MODE_CREATE) &&
253             (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
254                 ret = EFI_INVALID_PARAMETER;
255                 goto out;
256         }
257
258         /* Open file */
259         *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
260         if (*new_handle) {
261                 EFI_PRINT("file handle %p\n", *new_handle);
262                 ret = EFI_SUCCESS;
263         } else {
264                 ret = EFI_NOT_FOUND;
265         }
266 out:
267         return EFI_EXIT(ret);
268 }
269
270 static efi_status_t file_close(struct file_handle *fh)
271 {
272         fs_closedir(fh->dirs);
273         free(fh);
274         return EFI_SUCCESS;
275 }
276
277 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
278 {
279         struct file_handle *fh = to_fh(file);
280         EFI_ENTRY("%p", file);
281         return EFI_EXIT(file_close(fh));
282 }
283
284 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
285 {
286         struct file_handle *fh = to_fh(file);
287         efi_status_t ret = EFI_SUCCESS;
288
289         EFI_ENTRY("%p", file);
290
291         if (set_blk_dev(fh)) {
292                 ret = EFI_DEVICE_ERROR;
293                 goto error;
294         }
295
296         if (fs_unlink(fh->path))
297                 ret = EFI_DEVICE_ERROR;
298         file_close(fh);
299
300 error:
301         return EFI_EXIT(ret);
302 }
303
304 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
305                 void *buffer)
306 {
307         loff_t actread;
308
309         if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
310                     *buffer_size, &actread))
311                 return EFI_DEVICE_ERROR;
312
313         *buffer_size = actread;
314         fh->offset += actread;
315
316         return EFI_SUCCESS;
317 }
318
319 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
320                 void *buffer)
321 {
322         struct efi_file_info *info = buffer;
323         struct fs_dirent *dent;
324         unsigned int required_size;
325
326         if (!fh->dirs) {
327                 assert(fh->offset == 0);
328                 fh->dirs = fs_opendir(fh->path);
329                 if (!fh->dirs)
330                         return EFI_DEVICE_ERROR;
331         }
332
333         /*
334          * So this is a bit awkward.  Since fs layer is stateful and we
335          * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
336          * we might have to return without consuming the dent.. so we
337          * have to stash it for next call.
338          */
339         if (fh->dent) {
340                 dent = fh->dent;
341                 fh->dent = NULL;
342         } else {
343                 dent = fs_readdir(fh->dirs);
344         }
345
346
347         if (!dent) {
348                 /* no more files in directory: */
349                 /* workaround shim.efi bug/quirk.. as find_boot_csv()
350                  * loops through directory contents, it initially calls
351                  * read w/ zero length buffer to find out how much mem
352                  * to allocate for the EFI_FILE_INFO, then allocates,
353                  * and then calls a 2nd time.  If we return size of
354                  * zero the first time, it happily passes that to
355                  * AllocateZeroPool(), and when that returns NULL it
356                  * thinks it is EFI_OUT_OF_RESOURCES.  So on first
357                  * call return a non-zero size:
358                  */
359                 if (*buffer_size == 0)
360                         *buffer_size = sizeof(*info);
361                 else
362                         *buffer_size = 0;
363                 return EFI_SUCCESS;
364         }
365
366         /* check buffer size: */
367         required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
368         if (*buffer_size < required_size) {
369                 *buffer_size = required_size;
370                 fh->dent = dent;
371                 return EFI_BUFFER_TOO_SMALL;
372         }
373
374         *buffer_size = required_size;
375         memset(info, 0, required_size);
376
377         info->size = required_size;
378         info->file_size = dent->size;
379         info->physical_size = dent->size;
380
381         if (dent->type == FS_DT_DIR)
382                 info->attribute |= EFI_FILE_DIRECTORY;
383
384         ascii2unicode(info->file_name, dent->name);
385
386         fh->offset++;
387
388         return EFI_SUCCESS;
389 }
390
391 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
392                                          efi_uintn_t *buffer_size, void *buffer)
393 {
394         struct file_handle *fh = to_fh(file);
395         efi_status_t ret = EFI_SUCCESS;
396         u64 bs;
397
398         EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
399
400         if (!buffer_size || !buffer) {
401                 ret = EFI_INVALID_PARAMETER;
402                 goto error;
403         }
404
405         if (set_blk_dev(fh)) {
406                 ret = EFI_DEVICE_ERROR;
407                 goto error;
408         }
409
410         bs = *buffer_size;
411         if (fh->isdir)
412                 ret = dir_read(fh, &bs, buffer);
413         else
414                 ret = file_read(fh, &bs, buffer);
415         if (bs <= SIZE_MAX)
416                 *buffer_size = bs;
417         else
418                 *buffer_size = SIZE_MAX;
419
420 error:
421         return EFI_EXIT(ret);
422 }
423
424 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
425                                           efi_uintn_t *buffer_size,
426                                           void *buffer)
427 {
428         struct file_handle *fh = to_fh(file);
429         efi_status_t ret = EFI_SUCCESS;
430         loff_t actwrite;
431
432         EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
433
434         if (set_blk_dev(fh)) {
435                 ret = EFI_DEVICE_ERROR;
436                 goto error;
437         }
438
439         if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
440                      &actwrite)) {
441                 ret = EFI_DEVICE_ERROR;
442                 goto error;
443         }
444
445         *buffer_size = actwrite;
446         fh->offset += actwrite;
447
448 error:
449         return EFI_EXIT(ret);
450 }
451
452 /**
453  * efi_file_getpos() - get current position in file
454  *
455  * This function implements the GetPosition service of the EFI file protocol.
456  * See the UEFI spec for details.
457  *
458  * @file:       file handle
459  * @pos:        pointer to file position
460  * Return:      status code
461  */
462 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
463                                            u64 *pos)
464 {
465         efi_status_t ret = EFI_SUCCESS;
466         struct file_handle *fh = to_fh(file);
467
468         EFI_ENTRY("%p, %p", file, pos);
469
470         if (fh->isdir) {
471                 ret = EFI_UNSUPPORTED;
472                 goto out;
473         }
474
475         *pos = fh->offset;
476 out:
477         return EFI_EXIT(ret);
478 }
479
480 /**
481  * efi_file_setpos() - set current position in file
482  *
483  * This function implements the SetPosition service of the EFI file protocol.
484  * See the UEFI spec for details.
485  *
486  * @file:       file handle
487  * @pos:        new file position
488  * Return:      status code
489  */
490 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
491                                            u64 pos)
492 {
493         struct file_handle *fh = to_fh(file);
494         efi_status_t ret = EFI_SUCCESS;
495
496         EFI_ENTRY("%p, %llu", file, pos);
497
498         if (fh->isdir) {
499                 if (pos != 0) {
500                         ret = EFI_UNSUPPORTED;
501                         goto error;
502                 }
503                 fs_closedir(fh->dirs);
504                 fh->dirs = NULL;
505         }
506
507         if (pos == ~0ULL) {
508                 loff_t file_size;
509
510                 if (set_blk_dev(fh)) {
511                         ret = EFI_DEVICE_ERROR;
512                         goto error;
513                 }
514
515                 if (fs_size(fh->path, &file_size)) {
516                         ret = EFI_DEVICE_ERROR;
517                         goto error;
518                 }
519
520                 pos = file_size;
521         }
522
523         fh->offset = pos;
524
525 error:
526         return EFI_EXIT(ret);
527 }
528
529 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
530                                             const efi_guid_t *info_type,
531                                             efi_uintn_t *buffer_size,
532                                             void *buffer)
533 {
534         struct file_handle *fh = to_fh(file);
535         efi_status_t ret = EFI_SUCCESS;
536
537         EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
538
539         if (!guidcmp(info_type, &efi_file_info_guid)) {
540                 struct efi_file_info *info = buffer;
541                 char *filename = basename(fh);
542                 unsigned int required_size;
543                 loff_t file_size;
544
545                 /* check buffer size: */
546                 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
547                 if (*buffer_size < required_size) {
548                         *buffer_size = required_size;
549                         ret = EFI_BUFFER_TOO_SMALL;
550                         goto error;
551                 }
552
553                 if (set_blk_dev(fh)) {
554                         ret = EFI_DEVICE_ERROR;
555                         goto error;
556                 }
557
558                 if (fs_size(fh->path, &file_size)) {
559                         ret = EFI_DEVICE_ERROR;
560                         goto error;
561                 }
562
563                 memset(info, 0, required_size);
564
565                 info->size = required_size;
566                 info->file_size = file_size;
567                 info->physical_size = file_size;
568
569                 if (fh->isdir)
570                         info->attribute |= EFI_FILE_DIRECTORY;
571
572                 ascii2unicode(info->file_name, filename);
573         } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
574                 struct efi_file_system_info *info = buffer;
575                 disk_partition_t part;
576                 efi_uintn_t required_size;
577                 int r;
578
579                 if (fh->fs->part >= 1)
580                         r = part_get_info(fh->fs->desc, fh->fs->part, &part);
581                 else
582                         r = part_get_info_whole_disk(fh->fs->desc, &part);
583                 if (r < 0) {
584                         ret = EFI_DEVICE_ERROR;
585                         goto error;
586                 }
587                 required_size = sizeof(info) + 2 *
588                                 (strlen((const char *)part.name) + 1);
589                 if (*buffer_size < required_size) {
590                         *buffer_size = required_size;
591                         ret = EFI_BUFFER_TOO_SMALL;
592                         goto error;
593                 }
594
595                 memset(info, 0, required_size);
596
597                 info->size = required_size;
598                 info->read_only = true;
599                 info->volume_size = part.size * part.blksz;
600                 info->free_space = 0;
601                 info->block_size = part.blksz;
602                 /*
603                  * TODO: The volume label is not available in U-Boot.
604                  * Use the partition name as substitute.
605                  */
606                 ascii2unicode((u16 *)info->volume_label,
607                               (const char *)part.name);
608         } else {
609                 ret = EFI_UNSUPPORTED;
610         }
611
612 error:
613         return EFI_EXIT(ret);
614 }
615
616 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
617                                             const efi_guid_t *info_type,
618                                             efi_uintn_t buffer_size,
619                                             void *buffer)
620 {
621         EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
622
623         return EFI_EXIT(EFI_UNSUPPORTED);
624 }
625
626 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
627 {
628         EFI_ENTRY("%p", file);
629         return EFI_EXIT(EFI_SUCCESS);
630 }
631
632 static const struct efi_file_handle efi_file_handle_protocol = {
633         /*
634          * TODO: We currently only support EFI file protocol revision 0x00010000
635          *       while UEFI specs 2.4 - 2.7 prescribe revision 0x00020000.
636          */
637         .rev = EFI_FILE_PROTOCOL_REVISION,
638         .open = efi_file_open,
639         .close = efi_file_close,
640         .delete = efi_file_delete,
641         .read = efi_file_read,
642         .write = efi_file_write,
643         .getpos = efi_file_getpos,
644         .setpos = efi_file_setpos,
645         .getinfo = efi_file_getinfo,
646         .setinfo = efi_file_setinfo,
647         .flush = efi_file_flush,
648 };
649
650 /**
651  * efi_file_from_path() - open file via device path
652  *
653  * @fp:         device path
654  * @return:     EFI_FILE_PROTOCOL for the file or NULL
655  */
656 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
657 {
658         struct efi_simple_file_system_protocol *v;
659         struct efi_file_handle *f;
660         efi_status_t ret;
661
662         v = efi_fs_from_path(fp);
663         if (!v)
664                 return NULL;
665
666         EFI_CALL(ret = v->open_volume(v, &f));
667         if (ret != EFI_SUCCESS)
668                 return NULL;
669
670         /* Skip over device-path nodes before the file path. */
671         while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
672                 fp = efi_dp_next(fp);
673
674         /*
675          * Step through the nodes of the directory path until the actual file
676          * node is reached which is the final node in the device path.
677          */
678         while (fp) {
679                 struct efi_device_path_file_path *fdp =
680                         container_of(fp, struct efi_device_path_file_path, dp);
681                 struct efi_file_handle *f2;
682
683                 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
684                         printf("bad file path!\n");
685                         f->close(f);
686                         return NULL;
687                 }
688
689                 EFI_CALL(ret = f->open(f, &f2, fdp->str,
690                                        EFI_FILE_MODE_READ, 0));
691                 if (ret != EFI_SUCCESS)
692                         return NULL;
693
694                 fp = efi_dp_next(fp);
695
696                 EFI_CALL(f->close(f));
697                 f = f2;
698         }
699
700         return f;
701 }
702
703 static efi_status_t EFIAPI
704 efi_open_volume(struct efi_simple_file_system_protocol *this,
705                 struct efi_file_handle **root)
706 {
707         struct file_system *fs = to_fs(this);
708
709         EFI_ENTRY("%p, %p", this, root);
710
711         *root = file_open(fs, NULL, NULL, 0, 0);
712
713         return EFI_EXIT(EFI_SUCCESS);
714 }
715
716 struct efi_simple_file_system_protocol *
717 efi_simple_file_system(struct blk_desc *desc, int part,
718                        struct efi_device_path *dp)
719 {
720         struct file_system *fs;
721
722         fs = calloc(1, sizeof(*fs));
723         fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
724         fs->base.open_volume = efi_open_volume;
725         fs->desc = desc;
726         fs->part = part;
727         fs->dp = dp;
728
729         return &fs->base;
730 }