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