Merge https://gitlab.denx.de/u-boot/custodians/u-boot-mpc85xx
[platform/kernel/u-boot.git] / fs / fs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
4  */
5
6 #define LOG_CATEGORY LOGC_CORE
7
8 #include <command.h>
9 #include <config.h>
10 #include <errno.h>
11 #include <common.h>
12 #include <env.h>
13 #include <lmb.h>
14 #include <log.h>
15 #include <mapmem.h>
16 #include <part.h>
17 #include <ext4fs.h>
18 #include <fat.h>
19 #include <fs.h>
20 #include <sandboxfs.h>
21 #include <semihostingfs.h>
22 #include <ubifs_uboot.h>
23 #include <btrfs.h>
24 #include <asm/global_data.h>
25 #include <asm/io.h>
26 #include <div64.h>
27 #include <linux/math64.h>
28 #include <efi_loader.h>
29 #include <squashfs.h>
30 #include <erofs.h>
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 static struct blk_desc *fs_dev_desc;
35 static int fs_dev_part;
36 static struct disk_partition fs_partition;
37 static int fs_type = FS_TYPE_ANY;
38
39 void fs_set_type(int type)
40 {
41         fs_type = type;
42 }
43
44 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
45                                       struct disk_partition *fs_partition)
46 {
47         log_debug("Unrecognized filesystem type\n");
48         return -1;
49 }
50
51 static inline int fs_ls_unsupported(const char *dirname)
52 {
53         return -1;
54 }
55
56 /* generic implementation of ls in terms of opendir/readdir/closedir */
57 __maybe_unused
58 static int fs_ls_generic(const char *dirname)
59 {
60         struct fs_dir_stream *dirs;
61         struct fs_dirent *dent;
62         int nfiles = 0, ndirs = 0;
63
64         dirs = fs_opendir(dirname);
65         if (!dirs)
66                 return -errno;
67
68         while ((dent = fs_readdir(dirs))) {
69                 if (dent->type == FS_DT_DIR) {
70                         printf("            %s/\n", dent->name);
71                         ndirs++;
72                 } else if (dent->type == FS_DT_LNK) {
73                         printf("    <SYM>   %s\n", dent->name);
74                         nfiles++;
75                 } else {
76                         printf(" %8lld   %s\n", dent->size, dent->name);
77                         nfiles++;
78                 }
79         }
80
81         fs_closedir(dirs);
82
83         printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
84
85         return 0;
86 }
87
88 static inline int fs_exists_unsupported(const char *filename)
89 {
90         return 0;
91 }
92
93 static inline int fs_size_unsupported(const char *filename, loff_t *size)
94 {
95         return -1;
96 }
97
98 static inline int fs_read_unsupported(const char *filename, void *buf,
99                                       loff_t offset, loff_t len,
100                                       loff_t *actread)
101 {
102         return -1;
103 }
104
105 static inline int fs_write_unsupported(const char *filename, void *buf,
106                                       loff_t offset, loff_t len,
107                                       loff_t *actwrite)
108 {
109         return -1;
110 }
111
112 static inline int fs_ln_unsupported(const char *filename, const char *target)
113 {
114         return -1;
115 }
116
117 static inline void fs_close_unsupported(void)
118 {
119 }
120
121 static inline int fs_uuid_unsupported(char *uuid_str)
122 {
123         return -1;
124 }
125
126 static inline int fs_opendir_unsupported(const char *filename,
127                                          struct fs_dir_stream **dirs)
128 {
129         return -EACCES;
130 }
131
132 static inline int fs_unlink_unsupported(const char *filename)
133 {
134         return -1;
135 }
136
137 static inline int fs_mkdir_unsupported(const char *dirname)
138 {
139         return -1;
140 }
141
142 struct fstype_info {
143         int fstype;
144         char *name;
145         /*
146          * Is it legal to pass NULL as .probe()'s  fs_dev_desc parameter? This
147          * should be false in most cases. For "virtual" filesystems which
148          * aren't based on a U-Boot block device (e.g. sandbox), this can be
149          * set to true. This should also be true for the dummy entry at the end
150          * of fstypes[], since that is essentially a "virtual" (non-existent)
151          * filesystem.
152          */
153         bool null_dev_desc_ok;
154         int (*probe)(struct blk_desc *fs_dev_desc,
155                      struct disk_partition *fs_partition);
156         int (*ls)(const char *dirname);
157         int (*exists)(const char *filename);
158         int (*size)(const char *filename, loff_t *size);
159         int (*read)(const char *filename, void *buf, loff_t offset,
160                     loff_t len, loff_t *actread);
161         int (*write)(const char *filename, void *buf, loff_t offset,
162                      loff_t len, loff_t *actwrite);
163         void (*close)(void);
164         int (*uuid)(char *uuid_str);
165         /*
166          * Open a directory stream.  On success return 0 and directory
167          * stream pointer via 'dirsp'.  On error, return -errno.  See
168          * fs_opendir().
169          */
170         int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
171         /*
172          * Read next entry from directory stream.  On success return 0
173          * and directory entry pointer via 'dentp'.  On error return
174          * -errno.  See fs_readdir().
175          */
176         int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
177         /* see fs_closedir() */
178         void (*closedir)(struct fs_dir_stream *dirs);
179         int (*unlink)(const char *filename);
180         int (*mkdir)(const char *dirname);
181         int (*ln)(const char *filename, const char *target);
182 };
183
184 static struct fstype_info fstypes[] = {
185 #if CONFIG_IS_ENABLED(FS_FAT)
186         {
187                 .fstype = FS_TYPE_FAT,
188                 .name = "fat",
189                 .null_dev_desc_ok = false,
190                 .probe = fat_set_blk_dev,
191                 .close = fat_close,
192                 .ls = fs_ls_generic,
193                 .exists = fat_exists,
194                 .size = fat_size,
195                 .read = fat_read_file,
196 #if CONFIG_IS_ENABLED(FAT_WRITE)
197                 .write = file_fat_write,
198                 .unlink = fat_unlink,
199                 .mkdir = fat_mkdir,
200 #else
201                 .write = fs_write_unsupported,
202                 .unlink = fs_unlink_unsupported,
203                 .mkdir = fs_mkdir_unsupported,
204 #endif
205                 .uuid = fat_uuid,
206                 .opendir = fat_opendir,
207                 .readdir = fat_readdir,
208                 .closedir = fat_closedir,
209                 .ln = fs_ln_unsupported,
210         },
211 #endif
212
213 #if CONFIG_IS_ENABLED(FS_EXT4)
214         {
215                 .fstype = FS_TYPE_EXT,
216                 .name = "ext4",
217                 .null_dev_desc_ok = false,
218                 .probe = ext4fs_probe,
219                 .close = ext4fs_close,
220                 .ls = ext4fs_ls,
221                 .exists = ext4fs_exists,
222                 .size = ext4fs_size,
223                 .read = ext4_read_file,
224 #ifdef CONFIG_CMD_EXT4_WRITE
225                 .write = ext4_write_file,
226                 .ln = ext4fs_create_link,
227 #else
228                 .write = fs_write_unsupported,
229                 .ln = fs_ln_unsupported,
230 #endif
231                 .uuid = ext4fs_uuid,
232                 .opendir = fs_opendir_unsupported,
233                 .unlink = fs_unlink_unsupported,
234                 .mkdir = fs_mkdir_unsupported,
235         },
236 #endif
237 #ifdef CONFIG_SANDBOX
238         {
239                 .fstype = FS_TYPE_SANDBOX,
240                 .name = "sandbox",
241                 .null_dev_desc_ok = true,
242                 .probe = sandbox_fs_set_blk_dev,
243                 .close = sandbox_fs_close,
244                 .ls = sandbox_fs_ls,
245                 .exists = sandbox_fs_exists,
246                 .size = sandbox_fs_size,
247                 .read = fs_read_sandbox,
248                 .write = fs_write_sandbox,
249                 .uuid = fs_uuid_unsupported,
250                 .opendir = fs_opendir_unsupported,
251                 .unlink = fs_unlink_unsupported,
252                 .mkdir = fs_mkdir_unsupported,
253                 .ln = fs_ln_unsupported,
254         },
255 #endif
256 #ifdef CONFIG_SEMIHOSTING
257         {
258                 .fstype = FS_TYPE_SEMIHOSTING,
259                 .name = "semihosting",
260                 .null_dev_desc_ok = true,
261                 .probe = smh_fs_set_blk_dev,
262                 .close = fs_close_unsupported,
263                 .ls = fs_ls_unsupported,
264                 .exists = fs_exists_unsupported,
265                 .size = smh_fs_size,
266                 .read = smh_fs_read,
267                 .write = smh_fs_write,
268                 .uuid = fs_uuid_unsupported,
269                 .opendir = fs_opendir_unsupported,
270                 .unlink = fs_unlink_unsupported,
271                 .mkdir = fs_mkdir_unsupported,
272                 .ln = fs_ln_unsupported,
273         },
274 #endif
275 #ifndef CONFIG_SPL_BUILD
276 #ifdef CONFIG_CMD_UBIFS
277         {
278                 .fstype = FS_TYPE_UBIFS,
279                 .name = "ubifs",
280                 .null_dev_desc_ok = true,
281                 .probe = ubifs_set_blk_dev,
282                 .close = ubifs_close,
283                 .ls = ubifs_ls,
284                 .exists = ubifs_exists,
285                 .size = ubifs_size,
286                 .read = ubifs_read,
287                 .write = fs_write_unsupported,
288                 .uuid = fs_uuid_unsupported,
289                 .opendir = fs_opendir_unsupported,
290                 .unlink = fs_unlink_unsupported,
291                 .mkdir = fs_mkdir_unsupported,
292                 .ln = fs_ln_unsupported,
293         },
294 #endif
295 #endif
296 #ifndef CONFIG_SPL_BUILD
297 #ifdef CONFIG_FS_BTRFS
298         {
299                 .fstype = FS_TYPE_BTRFS,
300                 .name = "btrfs",
301                 .null_dev_desc_ok = false,
302                 .probe = btrfs_probe,
303                 .close = btrfs_close,
304                 .ls = btrfs_ls,
305                 .exists = btrfs_exists,
306                 .size = btrfs_size,
307                 .read = btrfs_read,
308                 .write = fs_write_unsupported,
309                 .uuid = btrfs_uuid,
310                 .opendir = fs_opendir_unsupported,
311                 .unlink = fs_unlink_unsupported,
312                 .mkdir = fs_mkdir_unsupported,
313                 .ln = fs_ln_unsupported,
314         },
315 #endif
316 #endif
317 #if CONFIG_IS_ENABLED(FS_SQUASHFS)
318         {
319                 .fstype = FS_TYPE_SQUASHFS,
320                 .name = "squashfs",
321                 .null_dev_desc_ok = false,
322                 .probe = sqfs_probe,
323                 .opendir = sqfs_opendir,
324                 .readdir = sqfs_readdir,
325                 .ls = fs_ls_generic,
326                 .read = sqfs_read,
327                 .size = sqfs_size,
328                 .close = sqfs_close,
329                 .closedir = sqfs_closedir,
330                 .exists = sqfs_exists,
331                 .uuid = fs_uuid_unsupported,
332                 .write = fs_write_unsupported,
333                 .ln = fs_ln_unsupported,
334                 .unlink = fs_unlink_unsupported,
335                 .mkdir = fs_mkdir_unsupported,
336         },
337 #endif
338 #if IS_ENABLED(CONFIG_FS_EROFS)
339         {
340                 .fstype = FS_TYPE_EROFS,
341                 .name = "erofs",
342                 .null_dev_desc_ok = false,
343                 .probe = erofs_probe,
344                 .opendir = erofs_opendir,
345                 .readdir = erofs_readdir,
346                 .ls = fs_ls_generic,
347                 .read = erofs_read,
348                 .size = erofs_size,
349                 .close = erofs_close,
350                 .closedir = erofs_closedir,
351                 .exists = erofs_exists,
352                 .uuid = fs_uuid_unsupported,
353                 .write = fs_write_unsupported,
354                 .ln = fs_ln_unsupported,
355                 .unlink = fs_unlink_unsupported,
356                 .mkdir = fs_mkdir_unsupported,
357         },
358 #endif
359         {
360                 .fstype = FS_TYPE_ANY,
361                 .name = "unsupported",
362                 .null_dev_desc_ok = true,
363                 .probe = fs_probe_unsupported,
364                 .close = fs_close_unsupported,
365                 .ls = fs_ls_unsupported,
366                 .exists = fs_exists_unsupported,
367                 .size = fs_size_unsupported,
368                 .read = fs_read_unsupported,
369                 .write = fs_write_unsupported,
370                 .uuid = fs_uuid_unsupported,
371                 .opendir = fs_opendir_unsupported,
372                 .unlink = fs_unlink_unsupported,
373                 .mkdir = fs_mkdir_unsupported,
374                 .ln = fs_ln_unsupported,
375         },
376 };
377
378 static struct fstype_info *fs_get_info(int fstype)
379 {
380         struct fstype_info *info;
381         int i;
382
383         for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
384                 if (fstype == info->fstype)
385                         return info;
386         }
387
388         /* Return the 'unsupported' sentinel */
389         return info;
390 }
391
392 /**
393  * fs_get_type() - Get type of current filesystem
394  *
395  * Return: filesystem type
396  *
397  * Returns filesystem type representing the current filesystem, or
398  * FS_TYPE_ANY for any unrecognised filesystem.
399  */
400 int fs_get_type(void)
401 {
402         return fs_type;
403 }
404
405 /**
406  * fs_get_type_name() - Get type of current filesystem
407  *
408  * Return: Pointer to filesystem name
409  *
410  * Returns a string describing the current filesystem, or the sentinel
411  * "unsupported" for any unrecognised filesystem.
412  */
413 const char *fs_get_type_name(void)
414 {
415         return fs_get_info(fs_type)->name;
416 }
417
418 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
419 {
420         struct fstype_info *info;
421         int part, i;
422 #ifdef CONFIG_NEEDS_MANUAL_RELOC
423         static int relocated;
424
425         if (!relocated) {
426                 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
427                                 i++, info++) {
428                         info->name += gd->reloc_off;
429                         info->probe += gd->reloc_off;
430                         info->close += gd->reloc_off;
431                         info->ls += gd->reloc_off;
432                         info->read += gd->reloc_off;
433                         info->write += gd->reloc_off;
434                 }
435                 relocated = 1;
436         }
437 #endif
438
439         part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
440                                                     &fs_partition, 1);
441         if (part < 0)
442                 return -1;
443
444         for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
445                 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
446                                 fstype != info->fstype)
447                         continue;
448
449                 if (!fs_dev_desc && !info->null_dev_desc_ok)
450                         continue;
451
452                 if (!info->probe(fs_dev_desc, &fs_partition)) {
453                         fs_type = info->fstype;
454                         fs_dev_part = part;
455                         return 0;
456                 }
457         }
458
459         return -1;
460 }
461
462 /* set current blk device w/ blk_desc + partition # */
463 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
464 {
465         struct fstype_info *info;
466         int ret, i;
467
468         if (part >= 1)
469                 ret = part_get_info(desc, part, &fs_partition);
470         else
471                 ret = part_get_info_whole_disk(desc, &fs_partition);
472         if (ret)
473                 return ret;
474         fs_dev_desc = desc;
475
476         for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
477                 if (!info->probe(fs_dev_desc, &fs_partition)) {
478                         fs_type = info->fstype;
479                         fs_dev_part = part;
480                         return 0;
481                 }
482         }
483
484         return -1;
485 }
486
487 void fs_close(void)
488 {
489         struct fstype_info *info = fs_get_info(fs_type);
490
491         info->close();
492
493         fs_type = FS_TYPE_ANY;
494 }
495
496 int fs_uuid(char *uuid_str)
497 {
498         struct fstype_info *info = fs_get_info(fs_type);
499
500         return info->uuid(uuid_str);
501 }
502
503 int fs_ls(const char *dirname)
504 {
505         int ret;
506
507         struct fstype_info *info = fs_get_info(fs_type);
508
509         ret = info->ls(dirname);
510
511         fs_close();
512
513         return ret;
514 }
515
516 int fs_exists(const char *filename)
517 {
518         int ret;
519
520         struct fstype_info *info = fs_get_info(fs_type);
521
522         ret = info->exists(filename);
523
524         fs_close();
525
526         return ret;
527 }
528
529 int fs_size(const char *filename, loff_t *size)
530 {
531         int ret;
532
533         struct fstype_info *info = fs_get_info(fs_type);
534
535         ret = info->size(filename, size);
536
537         fs_close();
538
539         return ret;
540 }
541
542 #ifdef CONFIG_LMB
543 /* Check if a file may be read to the given address */
544 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
545                              loff_t len, struct fstype_info *info)
546 {
547         struct lmb lmb;
548         int ret;
549         loff_t size;
550         loff_t read_len;
551
552         /* get the actual size of the file */
553         ret = info->size(filename, &size);
554         if (ret)
555                 return ret;
556         if (offset >= size) {
557                 /* offset >= EOF, no bytes will be written */
558                 return 0;
559         }
560         read_len = size - offset;
561
562         /* limit to 'len' if it is smaller */
563         if (len && len < read_len)
564                 read_len = len;
565
566         lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
567         lmb_dump_all(&lmb);
568
569         if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
570                 return 0;
571
572         log_err("** Reading file would overwrite reserved memory **\n");
573         return -ENOSPC;
574 }
575 #endif
576
577 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
578                     int do_lmb_check, loff_t *actread)
579 {
580         struct fstype_info *info = fs_get_info(fs_type);
581         void *buf;
582         int ret;
583
584 #ifdef CONFIG_LMB
585         if (do_lmb_check) {
586                 ret = fs_read_lmb_check(filename, addr, offset, len, info);
587                 if (ret)
588                         return ret;
589         }
590 #endif
591
592         /*
593          * We don't actually know how many bytes are being read, since len==0
594          * means read the whole file.
595          */
596         buf = map_sysmem(addr, len);
597         ret = info->read(filename, buf, offset, len, actread);
598         unmap_sysmem(buf);
599
600         /* If we requested a specific number of bytes, check we got it */
601         if (ret == 0 && len && *actread != len)
602                 log_debug("** %s shorter than offset + len **\n", filename);
603         fs_close();
604
605         return ret;
606 }
607
608 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
609             loff_t *actread)
610 {
611         return _fs_read(filename, addr, offset, len, 0, actread);
612 }
613
614 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
615              loff_t *actwrite)
616 {
617         struct fstype_info *info = fs_get_info(fs_type);
618         void *buf;
619         int ret;
620
621         buf = map_sysmem(addr, len);
622         ret = info->write(filename, buf, offset, len, actwrite);
623         unmap_sysmem(buf);
624
625         if (ret < 0 && len != *actwrite) {
626                 log_err("** Unable to write file %s **\n", filename);
627                 ret = -1;
628         }
629         fs_close();
630
631         return ret;
632 }
633
634 struct fs_dir_stream *fs_opendir(const char *filename)
635 {
636         struct fstype_info *info = fs_get_info(fs_type);
637         struct fs_dir_stream *dirs = NULL;
638         int ret;
639
640         ret = info->opendir(filename, &dirs);
641         fs_close();
642         if (ret) {
643                 errno = -ret;
644                 return NULL;
645         }
646
647         dirs->desc = fs_dev_desc;
648         dirs->part = fs_dev_part;
649
650         return dirs;
651 }
652
653 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
654 {
655         struct fstype_info *info;
656         struct fs_dirent *dirent;
657         int ret;
658
659         fs_set_blk_dev_with_part(dirs->desc, dirs->part);
660         info = fs_get_info(fs_type);
661
662         ret = info->readdir(dirs, &dirent);
663         fs_close();
664         if (ret) {
665                 errno = -ret;
666                 return NULL;
667         }
668
669         return dirent;
670 }
671
672 void fs_closedir(struct fs_dir_stream *dirs)
673 {
674         struct fstype_info *info;
675
676         if (!dirs)
677                 return;
678
679         fs_set_blk_dev_with_part(dirs->desc, dirs->part);
680         info = fs_get_info(fs_type);
681
682         info->closedir(dirs);
683         fs_close();
684 }
685
686 int fs_unlink(const char *filename)
687 {
688         int ret;
689
690         struct fstype_info *info = fs_get_info(fs_type);
691
692         ret = info->unlink(filename);
693
694         fs_close();
695
696         return ret;
697 }
698
699 int fs_mkdir(const char *dirname)
700 {
701         int ret;
702
703         struct fstype_info *info = fs_get_info(fs_type);
704
705         ret = info->mkdir(dirname);
706
707         fs_close();
708
709         return ret;
710 }
711
712 int fs_ln(const char *fname, const char *target)
713 {
714         struct fstype_info *info = fs_get_info(fs_type);
715         int ret;
716
717         ret = info->ln(fname, target);
718
719         if (ret < 0) {
720                 log_err("** Unable to create link %s -> %s **\n", fname, target);
721                 ret = -1;
722         }
723         fs_close();
724
725         return ret;
726 }
727
728 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
729             int fstype)
730 {
731         loff_t size;
732
733         if (argc != 4)
734                 return CMD_RET_USAGE;
735
736         if (fs_set_blk_dev(argv[1], argv[2], fstype))
737                 return 1;
738
739         if (fs_size(argv[3], &size) < 0)
740                 return CMD_RET_FAILURE;
741
742         env_set_hex("filesize", size);
743
744         return 0;
745 }
746
747 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
748             int fstype)
749 {
750         unsigned long addr;
751         const char *addr_str;
752         const char *filename;
753         loff_t bytes;
754         loff_t pos;
755         loff_t len_read;
756         int ret;
757         unsigned long time;
758         char *ep;
759
760         if (argc < 2)
761                 return CMD_RET_USAGE;
762         if (argc > 7)
763                 return CMD_RET_USAGE;
764
765         if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) {
766                 log_err("Can't set block device\n");
767                 return 1;
768         }
769
770         if (argc >= 4) {
771                 addr = hextoul(argv[3], &ep);
772                 if (ep == argv[3] || *ep != '\0')
773                         return CMD_RET_USAGE;
774         } else {
775                 addr_str = env_get("loadaddr");
776                 if (addr_str != NULL)
777                         addr = hextoul(addr_str, NULL);
778                 else
779                         addr = CONFIG_SYS_LOAD_ADDR;
780         }
781         if (argc >= 5) {
782                 filename = argv[4];
783         } else {
784                 filename = env_get("bootfile");
785                 if (!filename) {
786                         puts("** No boot file defined **\n");
787                         return 1;
788                 }
789         }
790         if (argc >= 6)
791                 bytes = hextoul(argv[5], NULL);
792         else
793                 bytes = 0;
794         if (argc >= 7)
795                 pos = hextoul(argv[6], NULL);
796         else
797                 pos = 0;
798
799         time = get_timer(0);
800         ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
801         time = get_timer(time);
802         if (ret < 0) {
803                 log_err("Failed to load '%s'\n", filename);
804                 return 1;
805         }
806
807         if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
808                 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
809                                 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
810                                 len_read);
811
812         printf("%llu bytes read in %lu ms", len_read, time);
813         if (time > 0) {
814                 puts(" (");
815                 print_size(div_u64(len_read, time) * 1000, "/s");
816                 puts(")");
817         }
818         puts("\n");
819
820         env_set_hex("fileaddr", addr);
821         env_set_hex("filesize", len_read);
822
823         return 0;
824 }
825
826 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
827           int fstype)
828 {
829         if (argc < 2)
830                 return CMD_RET_USAGE;
831         if (argc > 4)
832                 return CMD_RET_USAGE;
833
834         if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
835                 return 1;
836
837         if (fs_ls(argc >= 4 ? argv[3] : "/"))
838                 return 1;
839
840         return 0;
841 }
842
843 int file_exists(const char *dev_type, const char *dev_part, const char *file,
844                 int fstype)
845 {
846         if (fs_set_blk_dev(dev_type, dev_part, fstype))
847                 return 0;
848
849         return fs_exists(file);
850 }
851
852 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
853             int fstype)
854 {
855         unsigned long addr;
856         const char *filename;
857         loff_t bytes;
858         loff_t pos;
859         loff_t len;
860         int ret;
861         unsigned long time;
862
863         if (argc < 6 || argc > 7)
864                 return CMD_RET_USAGE;
865
866         if (fs_set_blk_dev(argv[1], argv[2], fstype))
867                 return 1;
868
869         addr = hextoul(argv[3], NULL);
870         filename = argv[4];
871         bytes = hextoul(argv[5], NULL);
872         if (argc >= 7)
873                 pos = hextoul(argv[6], NULL);
874         else
875                 pos = 0;
876
877         time = get_timer(0);
878         ret = fs_write(filename, addr, pos, bytes, &len);
879         time = get_timer(time);
880         if (ret < 0)
881                 return 1;
882
883         printf("%llu bytes written in %lu ms", len, time);
884         if (time > 0) {
885                 puts(" (");
886                 print_size(div_u64(len, time) * 1000, "/s");
887                 puts(")");
888         }
889         puts("\n");
890
891         return 0;
892 }
893
894 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
895                int fstype)
896 {
897         int ret;
898         char uuid[37];
899         memset(uuid, 0, sizeof(uuid));
900
901         if (argc < 3 || argc > 4)
902                 return CMD_RET_USAGE;
903
904         if (fs_set_blk_dev(argv[1], argv[2], fstype))
905                 return 1;
906
907         ret = fs_uuid(uuid);
908         if (ret)
909                 return CMD_RET_FAILURE;
910
911         if (argc == 4)
912                 env_set(argv[3], uuid);
913         else
914                 printf("%s\n", uuid);
915
916         return CMD_RET_SUCCESS;
917 }
918
919 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
920 {
921         struct fstype_info *info;
922
923         if (argc < 3 || argc > 4)
924                 return CMD_RET_USAGE;
925
926         if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
927                 return 1;
928
929         info = fs_get_info(fs_type);
930
931         if (argc == 4)
932                 env_set(argv[3], info->name);
933         else
934                 printf("%s\n", info->name);
935
936         fs_close();
937
938         return CMD_RET_SUCCESS;
939 }
940
941 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
942           int fstype)
943 {
944         if (argc != 4)
945                 return CMD_RET_USAGE;
946
947         if (fs_set_blk_dev(argv[1], argv[2], fstype))
948                 return 1;
949
950         if (fs_unlink(argv[3]))
951                 return 1;
952
953         return 0;
954 }
955
956 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
957              int fstype)
958 {
959         int ret;
960
961         if (argc != 4)
962                 return CMD_RET_USAGE;
963
964         if (fs_set_blk_dev(argv[1], argv[2], fstype))
965                 return 1;
966
967         ret = fs_mkdir(argv[3]);
968         if (ret) {
969                 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
970                 return 1;
971         }
972
973         return 0;
974 }
975
976 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
977           int fstype)
978 {
979         if (argc != 5)
980                 return CMD_RET_USAGE;
981
982         if (fs_set_blk_dev(argv[1], argv[2], fstype))
983                 return 1;
984
985         if (fs_ln(argv[3], argv[4]))
986                 return 1;
987
988         return 0;
989 }
990
991 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
992 {
993         struct fstype_info *drv = fstypes;
994         const int n_ents = ARRAY_SIZE(fstypes);
995         struct fstype_info *entry;
996         int i = 0;
997
998         puts("Supported filesystems");
999         for (entry = drv; entry != drv + n_ents; entry++) {
1000                 if (entry->fstype != FS_TYPE_ANY) {
1001                         printf("%c %s", i ? ',' : ':', entry->name);
1002                         i++;
1003                 }
1004         }
1005         if (!i)
1006                 puts(": <none>");
1007         puts("\n");
1008         return CMD_RET_SUCCESS;
1009 }