]> git.sur5r.net Git - u-boot/blob - lib/efi_loader/efi_file.c
efi_loader: use correct types in EFI_FILE_PROTOCOL
[u-boot] / lib / efi_loader / efi_file.c
1 /*
2  *  EFI utils
3  *
4  *  Copyright (c) 2017 Rob Clark
5  *
6  *  SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <common.h>
10 #include <charset.h>
11 #include <efi_loader.h>
12 #include <malloc.h>
13 #include <fs.h>
14
15 struct file_system {
16         struct efi_simple_file_system_protocol base;
17         struct efi_device_path *dp;
18         struct blk_desc *desc;
19         int part;
20 };
21 #define to_fs(x) container_of(x, struct file_system, base)
22
23 struct file_handle {
24         struct efi_file_handle base;
25         struct file_system *fs;
26         loff_t offset;       /* current file position/cursor */
27         int isdir;
28
29         /* for reading a directory: */
30         struct fs_dir_stream *dirs;
31         struct fs_dirent *dent;
32
33         char path[0];
34 };
35 #define to_fh(x) container_of(x, struct file_handle, base)
36
37 static const struct efi_file_handle efi_file_handle_protocol;
38
39 static char *basename(struct file_handle *fh)
40 {
41         char *s = strrchr(fh->path, '/');
42         if (s)
43                 return s + 1;
44         return fh->path;
45 }
46
47 static int set_blk_dev(struct file_handle *fh)
48 {
49         return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
50 }
51
52 static int is_dir(struct file_handle *fh)
53 {
54         struct fs_dir_stream *dirs;
55
56         set_blk_dev(fh);
57         dirs = fs_opendir(fh->path);
58         if (!dirs)
59                 return 0;
60
61         fs_closedir(dirs);
62
63         return 1;
64 }
65
66 /*
67  * Normalize a path which may include either back or fwd slashes,
68  * double slashes, . or .. entries in the path, etc.
69  */
70 static int sanitize_path(char *path)
71 {
72         char *p;
73
74         /* backslash to slash: */
75         p = path;
76         while ((p = strchr(p, '\\')))
77                 *p++ = '/';
78
79         /* handle double-slashes: */
80         p = path;
81         while ((p = strstr(p, "//"))) {
82                 char *src = p + 1;
83                 memmove(p, src, strlen(src) + 1);
84         }
85
86         /* handle extra /.'s */
87         p = path;
88         while ((p = strstr(p, "/."))) {
89                 /*
90                  * You'd be tempted to do this *after* handling ".."s
91                  * below to avoid having to check if "/." is start of
92                  * a "/..", but that won't have the correct results..
93                  * for example, "/foo/./../bar" would get resolved to
94                  * "/foo/bar" if you did these two passes in the other
95                  * order
96                  */
97                 if (p[2] == '.') {
98                         p += 2;
99                         continue;
100                 }
101                 char *src = p + 2;
102                 memmove(p, src, strlen(src) + 1);
103         }
104
105         /* handle extra /..'s: */
106         p = path;
107         while ((p = strstr(p, "/.."))) {
108                 char *src = p + 3;
109
110                 p--;
111
112                 /* find beginning of previous path entry: */
113                 while (true) {
114                         if (p < path)
115                                 return -1;
116                         if (*p == '/')
117                                 break;
118                         p--;
119                 }
120
121                 memmove(p, src, strlen(src) + 1);
122         }
123
124         return 0;
125 }
126
127 /* NOTE: despite what you would expect, 'file_name' is actually a path.
128  * With windoze style backlashes, ofc.
129  */
130 static struct efi_file_handle *file_open(struct file_system *fs,
131                 struct file_handle *parent, s16 *file_name, u64 mode)
132 {
133         struct file_handle *fh;
134         char f0[MAX_UTF8_PER_UTF16] = {0};
135         int plen = 0;
136         int flen = 0;
137
138         if (file_name) {
139                 utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1);
140                 flen = utf16_strlen((u16 *)file_name);
141         }
142
143         /* we could have a parent, but also an absolute path: */
144         if (f0[0] == '\\') {
145                 plen = 0;
146         } else if (parent) {
147                 plen = strlen(parent->path) + 1;
148         }
149
150         /* +2 is for null and '/' */
151         fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
152
153         fh->base = efi_file_handle_protocol;
154         fh->fs = fs;
155
156         if (parent) {
157                 char *p = fh->path;
158
159                 if (plen > 0) {
160                         strcpy(p, parent->path);
161                         p += plen - 1;
162                         *p++ = '/';
163                 }
164
165                 utf16_to_utf8((u8 *)p, (u16 *)file_name, flen);
166
167                 if (sanitize_path(fh->path))
168                         goto error;
169
170                 /* check if file exists: */
171                 if (set_blk_dev(fh))
172                         goto error;
173
174                 if (!((mode & EFI_FILE_MODE_CREATE) || fs_exists(fh->path)))
175                         goto error;
176
177                 /* figure out if file is a directory: */
178                 fh->isdir = is_dir(fh);
179         } else {
180                 fh->isdir = 1;
181                 strcpy(fh->path, "");
182         }
183
184         return &fh->base;
185
186 error:
187         free(fh);
188         return NULL;
189 }
190
191 static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
192                 struct efi_file_handle **new_handle,
193                 s16 *file_name, u64 open_mode, u64 attributes)
194 {
195         struct file_handle *fh = to_fh(file);
196
197         EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name,
198                   open_mode, attributes);
199
200         *new_handle = file_open(fh->fs, fh, file_name, open_mode);
201         if (!*new_handle)
202                 return EFI_EXIT(EFI_NOT_FOUND);
203
204         return EFI_EXIT(EFI_SUCCESS);
205 }
206
207 static efi_status_t file_close(struct file_handle *fh)
208 {
209         fs_closedir(fh->dirs);
210         free(fh);
211         return EFI_SUCCESS;
212 }
213
214 static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
215 {
216         struct file_handle *fh = to_fh(file);
217         EFI_ENTRY("%p", file);
218         return EFI_EXIT(file_close(fh));
219 }
220
221 static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
222 {
223         struct file_handle *fh = to_fh(file);
224         EFI_ENTRY("%p", file);
225         file_close(fh);
226         return EFI_EXIT(EFI_WARN_DELETE_FAILURE);
227 }
228
229 static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
230                 void *buffer)
231 {
232         loff_t actread;
233
234         if (fs_read(fh->path, (ulong)buffer, fh->offset,
235                     *buffer_size, &actread))
236                 return EFI_DEVICE_ERROR;
237
238         *buffer_size = actread;
239         fh->offset += actread;
240
241         return EFI_SUCCESS;
242 }
243
244 static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
245                 void *buffer)
246 {
247         struct efi_file_info *info = buffer;
248         struct fs_dirent *dent;
249         unsigned int required_size;
250
251         if (!fh->dirs) {
252                 assert(fh->offset == 0);
253                 fh->dirs = fs_opendir(fh->path);
254                 if (!fh->dirs)
255                         return EFI_DEVICE_ERROR;
256         }
257
258         /*
259          * So this is a bit awkward.  Since fs layer is stateful and we
260          * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
261          * we might have to return without consuming the dent.. so we
262          * have to stash it for next call.
263          */
264         if (fh->dent) {
265                 dent = fh->dent;
266                 fh->dent = NULL;
267         } else {
268                 dent = fs_readdir(fh->dirs);
269         }
270
271
272         if (!dent) {
273                 /* no more files in directory: */
274                 /* workaround shim.efi bug/quirk.. as find_boot_csv()
275                  * loops through directory contents, it initially calls
276                  * read w/ zero length buffer to find out how much mem
277                  * to allocate for the EFI_FILE_INFO, then allocates,
278                  * and then calls a 2nd time.  If we return size of
279                  * zero the first time, it happily passes that to
280                  * AllocateZeroPool(), and when that returns NULL it
281                  * thinks it is EFI_OUT_OF_RESOURCES.  So on first
282                  * call return a non-zero size:
283                  */
284                 if (*buffer_size == 0)
285                         *buffer_size = sizeof(*info);
286                 else
287                         *buffer_size = 0;
288                 return EFI_SUCCESS;
289         }
290
291         /* check buffer size: */
292         required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
293         if (*buffer_size < required_size) {
294                 *buffer_size = required_size;
295                 fh->dent = dent;
296                 return EFI_BUFFER_TOO_SMALL;
297         }
298
299         *buffer_size = required_size;
300         memset(info, 0, required_size);
301
302         info->size = required_size;
303         info->file_size = dent->size;
304         info->physical_size = dent->size;
305
306         if (dent->type == FS_DT_DIR)
307                 info->attribute |= EFI_FILE_DIRECTORY;
308
309         ascii2unicode((u16 *)info->file_name, dent->name);
310
311         fh->offset++;
312
313         return EFI_SUCCESS;
314 }
315
316 static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
317                                          efi_uintn_t *buffer_size, void *buffer)
318 {
319         struct file_handle *fh = to_fh(file);
320         efi_status_t ret = EFI_SUCCESS;
321         u64 bs;
322
323         EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
324
325         if (!buffer_size || !buffer) {
326                 ret = EFI_INVALID_PARAMETER;
327                 goto error;
328         }
329
330         if (set_blk_dev(fh)) {
331                 ret = EFI_DEVICE_ERROR;
332                 goto error;
333         }
334
335         bs = *buffer_size;
336         if (fh->isdir)
337                 ret = dir_read(fh, &bs, buffer);
338         else
339                 ret = file_read(fh, &bs, buffer);
340         if (bs <= SIZE_MAX)
341                 *buffer_size = bs;
342         else
343                 *buffer_size = SIZE_MAX;
344
345 error:
346         return EFI_EXIT(ret);
347 }
348
349 static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
350                                           efi_uintn_t *buffer_size,
351                                           void *buffer)
352 {
353         struct file_handle *fh = to_fh(file);
354         efi_status_t ret = EFI_SUCCESS;
355         loff_t actwrite;
356
357         EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
358
359         if (set_blk_dev(fh)) {
360                 ret = EFI_DEVICE_ERROR;
361                 goto error;
362         }
363
364         if (fs_write(fh->path, (ulong)buffer, fh->offset, *buffer_size,
365                      &actwrite)) {
366                 ret = EFI_DEVICE_ERROR;
367                 goto error;
368         }
369
370         *buffer_size = actwrite;
371         fh->offset += actwrite;
372
373 error:
374         return EFI_EXIT(ret);
375 }
376
377 static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
378                                            efi_uintn_t *pos)
379 {
380         struct file_handle *fh = to_fh(file);
381
382         EFI_ENTRY("%p, %p", file, pos);
383
384         if (fh->offset <= SIZE_MAX) {
385                 *pos = fh->offset;
386                 return EFI_EXIT(EFI_SUCCESS);
387         } else {
388                 return EFI_EXIT(EFI_DEVICE_ERROR);
389         }
390 }
391
392 static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
393                 efi_uintn_t pos)
394 {
395         struct file_handle *fh = to_fh(file);
396         efi_status_t ret = EFI_SUCCESS;
397
398         EFI_ENTRY("%p, %zu", file, pos);
399
400         if (fh->isdir) {
401                 if (pos != 0) {
402                         ret = EFI_UNSUPPORTED;
403                         goto error;
404                 }
405                 fs_closedir(fh->dirs);
406                 fh->dirs = NULL;
407         }
408
409         if (pos == ~0ULL) {
410                 loff_t file_size;
411
412                 if (set_blk_dev(fh)) {
413                         ret = EFI_DEVICE_ERROR;
414                         goto error;
415                 }
416
417                 if (fs_size(fh->path, &file_size)) {
418                         ret = EFI_DEVICE_ERROR;
419                         goto error;
420                 }
421
422                 pos = file_size;
423         }
424
425         fh->offset = pos;
426
427 error:
428         return EFI_EXIT(ret);
429 }
430
431 static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
432                                             efi_guid_t *info_type,
433                                             efi_uintn_t *buffer_size,
434                                             void *buffer)
435 {
436         struct file_handle *fh = to_fh(file);
437         efi_status_t ret = EFI_SUCCESS;
438
439         EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer);
440
441         if (!guidcmp(info_type, &efi_file_info_guid)) {
442                 struct efi_file_info *info = buffer;
443                 char *filename = basename(fh);
444                 unsigned int required_size;
445                 loff_t file_size;
446
447                 /* check buffer size: */
448                 required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
449                 if (*buffer_size < required_size) {
450                         *buffer_size = required_size;
451                         ret = EFI_BUFFER_TOO_SMALL;
452                         goto error;
453                 }
454
455                 if (set_blk_dev(fh)) {
456                         ret = EFI_DEVICE_ERROR;
457                         goto error;
458                 }
459
460                 if (fs_size(fh->path, &file_size)) {
461                         ret = EFI_DEVICE_ERROR;
462                         goto error;
463                 }
464
465                 memset(info, 0, required_size);
466
467                 info->size = required_size;
468                 info->file_size = file_size;
469                 info->physical_size = file_size;
470
471                 if (fh->isdir)
472                         info->attribute |= EFI_FILE_DIRECTORY;
473
474                 ascii2unicode((u16 *)info->file_name, filename);
475         } else {
476                 ret = EFI_UNSUPPORTED;
477         }
478
479 error:
480         return EFI_EXIT(ret);
481 }
482
483 static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
484                                             efi_guid_t *info_type,
485                                             efi_uintn_t buffer_size,
486                                             void *buffer)
487 {
488         EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
489
490         return EFI_EXIT(EFI_UNSUPPORTED);
491 }
492
493 static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
494 {
495         EFI_ENTRY("%p", file);
496         return EFI_EXIT(EFI_SUCCESS);
497 }
498
499 static const struct efi_file_handle efi_file_handle_protocol = {
500         .rev = EFI_FILE_PROTOCOL_REVISION,
501         .open = efi_file_open,
502         .close = efi_file_close,
503         .delete = efi_file_delete,
504         .read = efi_file_read,
505         .write = efi_file_write,
506         .getpos = efi_file_getpos,
507         .setpos = efi_file_setpos,
508         .getinfo = efi_file_getinfo,
509         .setinfo = efi_file_setinfo,
510         .flush = efi_file_flush,
511 };
512
513 struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
514 {
515         struct efi_simple_file_system_protocol *v;
516         struct efi_file_handle *f;
517         efi_status_t ret;
518
519         v = efi_fs_from_path(fp);
520         if (!v)
521                 return NULL;
522
523         EFI_CALL(ret = v->open_volume(v, &f));
524         if (ret != EFI_SUCCESS)
525                 return NULL;
526
527         /* skip over device-path nodes before the file path: */
528         while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
529                 fp = efi_dp_next(fp);
530
531         while (fp) {
532                 struct efi_device_path_file_path *fdp =
533                         container_of(fp, struct efi_device_path_file_path, dp);
534                 struct efi_file_handle *f2;
535
536                 if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
537                         printf("bad file path!\n");
538                         f->close(f);
539                         return NULL;
540                 }
541
542                 EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
543                                        EFI_FILE_MODE_READ, 0));
544                 if (ret != EFI_SUCCESS)
545                         return NULL;
546
547                 fp = efi_dp_next(fp);
548
549                 EFI_CALL(f->close(f));
550                 f = f2;
551         }
552
553         return f;
554 }
555
556 static efi_status_t EFIAPI
557 efi_open_volume(struct efi_simple_file_system_protocol *this,
558                 struct efi_file_handle **root)
559 {
560         struct file_system *fs = to_fs(this);
561
562         EFI_ENTRY("%p, %p", this, root);
563
564         *root = file_open(fs, NULL, NULL, 0);
565
566         return EFI_EXIT(EFI_SUCCESS);
567 }
568
569 struct efi_simple_file_system_protocol *
570 efi_simple_file_system(struct blk_desc *desc, int part,
571                        struct efi_device_path *dp)
572 {
573         struct file_system *fs;
574
575         fs = calloc(1, sizeof(*fs));
576         fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
577         fs->base.open_volume = efi_open_volume;
578         fs->desc = desc;
579         fs->part = part;
580         fs->dp = dp;
581
582         return &fs->base;
583 }