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