Merge branch 'master' of https://source.denx.de/u-boot/custodians/u-boot-sunxi
[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 #ifdef CONFIG_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 #ifdef CONFIG_CMD_UBIFS
271         {
272                 .fstype = FS_TYPE_UBIFS,
273                 .name = "ubifs",
274                 .null_dev_desc_ok = true,
275                 .probe = ubifs_set_blk_dev,
276                 .close = ubifs_close,
277                 .ls = ubifs_ls,
278                 .exists = ubifs_exists,
279                 .size = ubifs_size,
280                 .read = ubifs_read,
281                 .write = fs_write_unsupported,
282                 .uuid = fs_uuid_unsupported,
283                 .opendir = fs_opendir_unsupported,
284                 .unlink = fs_unlink_unsupported,
285                 .mkdir = fs_mkdir_unsupported,
286                 .ln = fs_ln_unsupported,
287         },
288 #endif
289 #ifdef CONFIG_FS_BTRFS
290         {
291                 .fstype = FS_TYPE_BTRFS,
292                 .name = "btrfs",
293                 .null_dev_desc_ok = false,
294                 .probe = btrfs_probe,
295                 .close = btrfs_close,
296                 .ls = btrfs_ls,
297                 .exists = btrfs_exists,
298                 .size = btrfs_size,
299                 .read = btrfs_read,
300                 .write = fs_write_unsupported,
301                 .uuid = btrfs_uuid,
302                 .opendir = fs_opendir_unsupported,
303                 .unlink = fs_unlink_unsupported,
304                 .mkdir = fs_mkdir_unsupported,
305                 .ln = fs_ln_unsupported,
306         },
307 #endif
308 #if IS_ENABLED(CONFIG_FS_SQUASHFS)
309         {
310                 .fstype = FS_TYPE_SQUASHFS,
311                 .name = "squashfs",
312                 .null_dev_desc_ok = false,
313                 .probe = sqfs_probe,
314                 .opendir = sqfs_opendir,
315                 .readdir = sqfs_readdir,
316                 .ls = fs_ls_generic,
317                 .read = sqfs_read,
318                 .size = sqfs_size,
319                 .close = sqfs_close,
320                 .closedir = sqfs_closedir,
321                 .exists = sqfs_exists,
322                 .uuid = fs_uuid_unsupported,
323                 .write = fs_write_unsupported,
324                 .ln = fs_ln_unsupported,
325                 .unlink = fs_unlink_unsupported,
326                 .mkdir = fs_mkdir_unsupported,
327         },
328 #endif
329 #if IS_ENABLED(CONFIG_FS_EROFS)
330         {
331                 .fstype = FS_TYPE_EROFS,
332                 .name = "erofs",
333                 .null_dev_desc_ok = false,
334                 .probe = erofs_probe,
335                 .opendir = erofs_opendir,
336                 .readdir = erofs_readdir,
337                 .ls = fs_ls_generic,
338                 .read = erofs_read,
339                 .size = erofs_size,
340                 .close = erofs_close,
341                 .closedir = erofs_closedir,
342                 .exists = erofs_exists,
343                 .uuid = fs_uuid_unsupported,
344                 .write = fs_write_unsupported,
345                 .ln = fs_ln_unsupported,
346                 .unlink = fs_unlink_unsupported,
347                 .mkdir = fs_mkdir_unsupported,
348         },
349 #endif
350         {
351                 .fstype = FS_TYPE_ANY,
352                 .name = "unsupported",
353                 .null_dev_desc_ok = true,
354                 .probe = fs_probe_unsupported,
355                 .close = fs_close_unsupported,
356                 .ls = fs_ls_unsupported,
357                 .exists = fs_exists_unsupported,
358                 .size = fs_size_unsupported,
359                 .read = fs_read_unsupported,
360                 .write = fs_write_unsupported,
361                 .uuid = fs_uuid_unsupported,
362                 .opendir = fs_opendir_unsupported,
363                 .unlink = fs_unlink_unsupported,
364                 .mkdir = fs_mkdir_unsupported,
365                 .ln = fs_ln_unsupported,
366         },
367 };
368
369 static struct fstype_info *fs_get_info(int fstype)
370 {
371         struct fstype_info *info;
372         int i;
373
374         for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
375                 if (fstype == info->fstype)
376                         return info;
377         }
378
379         /* Return the 'unsupported' sentinel */
380         return info;
381 }
382
383 /**
384  * fs_get_type() - Get type of current filesystem
385  *
386  * Return: filesystem type
387  *
388  * Returns filesystem type representing the current filesystem, or
389  * FS_TYPE_ANY for any unrecognised filesystem.
390  */
391 int fs_get_type(void)
392 {
393         return fs_type;
394 }
395
396 /**
397  * fs_get_type_name() - Get type of current filesystem
398  *
399  * Return: Pointer to filesystem name
400  *
401  * Returns a string describing the current filesystem, or the sentinel
402  * "unsupported" for any unrecognised filesystem.
403  */
404 const char *fs_get_type_name(void)
405 {
406         return fs_get_info(fs_type)->name;
407 }
408
409 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
410 {
411         struct fstype_info *info;
412         int part, i;
413 #ifdef CONFIG_NEEDS_MANUAL_RELOC
414         static int relocated;
415
416         if (!relocated) {
417                 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
418                                 i++, info++) {
419                         info->name += gd->reloc_off;
420                         info->probe += gd->reloc_off;
421                         info->close += gd->reloc_off;
422                         info->ls += gd->reloc_off;
423                         info->read += gd->reloc_off;
424                         info->write += gd->reloc_off;
425                 }
426                 relocated = 1;
427         }
428 #endif
429
430         part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
431                                                     &fs_partition, 1);
432         if (part < 0)
433                 return -1;
434
435         for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
436                 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
437                                 fstype != info->fstype)
438                         continue;
439
440                 if (!fs_dev_desc && !info->null_dev_desc_ok)
441                         continue;
442
443                 if (!info->probe(fs_dev_desc, &fs_partition)) {
444                         fs_type = info->fstype;
445                         fs_dev_part = part;
446                         return 0;
447                 }
448         }
449
450         return -1;
451 }
452
453 /* set current blk device w/ blk_desc + partition # */
454 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
455 {
456         struct fstype_info *info;
457         int ret, i;
458
459         if (part >= 1)
460                 ret = part_get_info(desc, part, &fs_partition);
461         else
462                 ret = part_get_info_whole_disk(desc, &fs_partition);
463         if (ret)
464                 return ret;
465         fs_dev_desc = desc;
466
467         for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
468                 if (!info->probe(fs_dev_desc, &fs_partition)) {
469                         fs_type = info->fstype;
470                         fs_dev_part = part;
471                         return 0;
472                 }
473         }
474
475         return -1;
476 }
477
478 void fs_close(void)
479 {
480         struct fstype_info *info = fs_get_info(fs_type);
481
482         info->close();
483
484         fs_type = FS_TYPE_ANY;
485 }
486
487 int fs_uuid(char *uuid_str)
488 {
489         struct fstype_info *info = fs_get_info(fs_type);
490
491         return info->uuid(uuid_str);
492 }
493
494 int fs_ls(const char *dirname)
495 {
496         int ret;
497
498         struct fstype_info *info = fs_get_info(fs_type);
499
500         ret = info->ls(dirname);
501
502         fs_close();
503
504         return ret;
505 }
506
507 int fs_exists(const char *filename)
508 {
509         int ret;
510
511         struct fstype_info *info = fs_get_info(fs_type);
512
513         ret = info->exists(filename);
514
515         fs_close();
516
517         return ret;
518 }
519
520 int fs_size(const char *filename, loff_t *size)
521 {
522         int ret;
523
524         struct fstype_info *info = fs_get_info(fs_type);
525
526         ret = info->size(filename, size);
527
528         fs_close();
529
530         return ret;
531 }
532
533 #ifdef CONFIG_LMB
534 /* Check if a file may be read to the given address */
535 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
536                              loff_t len, struct fstype_info *info)
537 {
538         struct lmb lmb;
539         int ret;
540         loff_t size;
541         loff_t read_len;
542
543         /* get the actual size of the file */
544         ret = info->size(filename, &size);
545         if (ret)
546                 return ret;
547         if (offset >= size) {
548                 /* offset >= EOF, no bytes will be written */
549                 return 0;
550         }
551         read_len = size - offset;
552
553         /* limit to 'len' if it is smaller */
554         if (len && len < read_len)
555                 read_len = len;
556
557         lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
558         lmb_dump_all(&lmb);
559
560         if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
561                 return 0;
562
563         log_err("** Reading file would overwrite reserved memory **\n");
564         return -ENOSPC;
565 }
566 #endif
567
568 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
569                     int do_lmb_check, loff_t *actread)
570 {
571         struct fstype_info *info = fs_get_info(fs_type);
572         void *buf;
573         int ret;
574
575 #ifdef CONFIG_LMB
576         if (do_lmb_check) {
577                 ret = fs_read_lmb_check(filename, addr, offset, len, info);
578                 if (ret)
579                         return ret;
580         }
581 #endif
582
583         /*
584          * We don't actually know how many bytes are being read, since len==0
585          * means read the whole file.
586          */
587         buf = map_sysmem(addr, len);
588         ret = info->read(filename, buf, offset, len, actread);
589         unmap_sysmem(buf);
590
591         /* If we requested a specific number of bytes, check we got it */
592         if (ret == 0 && len && *actread != len)
593                 log_debug("** %s shorter than offset + len **\n", filename);
594         fs_close();
595
596         return ret;
597 }
598
599 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
600             loff_t *actread)
601 {
602         return _fs_read(filename, addr, offset, len, 0, actread);
603 }
604
605 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
606              loff_t *actwrite)
607 {
608         struct fstype_info *info = fs_get_info(fs_type);
609         void *buf;
610         int ret;
611
612         buf = map_sysmem(addr, len);
613         ret = info->write(filename, buf, offset, len, actwrite);
614         unmap_sysmem(buf);
615
616         if (ret < 0 && len != *actwrite) {
617                 log_err("** Unable to write file %s **\n", filename);
618                 ret = -1;
619         }
620         fs_close();
621
622         return ret;
623 }
624
625 struct fs_dir_stream *fs_opendir(const char *filename)
626 {
627         struct fstype_info *info = fs_get_info(fs_type);
628         struct fs_dir_stream *dirs = NULL;
629         int ret;
630
631         ret = info->opendir(filename, &dirs);
632         fs_close();
633         if (ret) {
634                 errno = -ret;
635                 return NULL;
636         }
637
638         dirs->desc = fs_dev_desc;
639         dirs->part = fs_dev_part;
640
641         return dirs;
642 }
643
644 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
645 {
646         struct fstype_info *info;
647         struct fs_dirent *dirent;
648         int ret;
649
650         fs_set_blk_dev_with_part(dirs->desc, dirs->part);
651         info = fs_get_info(fs_type);
652
653         ret = info->readdir(dirs, &dirent);
654         fs_close();
655         if (ret) {
656                 errno = -ret;
657                 return NULL;
658         }
659
660         return dirent;
661 }
662
663 void fs_closedir(struct fs_dir_stream *dirs)
664 {
665         struct fstype_info *info;
666
667         if (!dirs)
668                 return;
669
670         fs_set_blk_dev_with_part(dirs->desc, dirs->part);
671         info = fs_get_info(fs_type);
672
673         info->closedir(dirs);
674         fs_close();
675 }
676
677 int fs_unlink(const char *filename)
678 {
679         int ret;
680
681         struct fstype_info *info = fs_get_info(fs_type);
682
683         ret = info->unlink(filename);
684
685         fs_close();
686
687         return ret;
688 }
689
690 int fs_mkdir(const char *dirname)
691 {
692         int ret;
693
694         struct fstype_info *info = fs_get_info(fs_type);
695
696         ret = info->mkdir(dirname);
697
698         fs_close();
699
700         return ret;
701 }
702
703 int fs_ln(const char *fname, const char *target)
704 {
705         struct fstype_info *info = fs_get_info(fs_type);
706         int ret;
707
708         ret = info->ln(fname, target);
709
710         if (ret < 0) {
711                 log_err("** Unable to create link %s -> %s **\n", fname, target);
712                 ret = -1;
713         }
714         fs_close();
715
716         return ret;
717 }
718
719 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
720             int fstype)
721 {
722         loff_t size;
723
724         if (argc != 4)
725                 return CMD_RET_USAGE;
726
727         if (fs_set_blk_dev(argv[1], argv[2], fstype))
728                 return 1;
729
730         if (fs_size(argv[3], &size) < 0)
731                 return CMD_RET_FAILURE;
732
733         env_set_hex("filesize", size);
734
735         return 0;
736 }
737
738 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
739             int fstype)
740 {
741         unsigned long addr;
742         const char *addr_str;
743         const char *filename;
744         loff_t bytes;
745         loff_t pos;
746         loff_t len_read;
747         int ret;
748         unsigned long time;
749         char *ep;
750
751         if (argc < 2)
752                 return CMD_RET_USAGE;
753         if (argc > 7)
754                 return CMD_RET_USAGE;
755
756         if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) {
757                 log_err("Can't set block device\n");
758                 return 1;
759         }
760
761         if (argc >= 4) {
762                 addr = hextoul(argv[3], &ep);
763                 if (ep == argv[3] || *ep != '\0')
764                         return CMD_RET_USAGE;
765         } else {
766                 addr_str = env_get("loadaddr");
767                 if (addr_str != NULL)
768                         addr = hextoul(addr_str, NULL);
769                 else
770                         addr = CONFIG_SYS_LOAD_ADDR;
771         }
772         if (argc >= 5) {
773                 filename = argv[4];
774         } else {
775                 filename = env_get("bootfile");
776                 if (!filename) {
777                         puts("** No boot file defined **\n");
778                         return 1;
779                 }
780         }
781         if (argc >= 6)
782                 bytes = hextoul(argv[5], NULL);
783         else
784                 bytes = 0;
785         if (argc >= 7)
786                 pos = hextoul(argv[6], NULL);
787         else
788                 pos = 0;
789
790         time = get_timer(0);
791         ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
792         time = get_timer(time);
793         if (ret < 0) {
794                 log_err("Failed to load '%s'\n", filename);
795                 return 1;
796         }
797
798         if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
799                 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
800                                 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
801                                 len_read);
802
803         printf("%llu bytes read in %lu ms", len_read, time);
804         if (time > 0) {
805                 puts(" (");
806                 print_size(div_u64(len_read, time) * 1000, "/s");
807                 puts(")");
808         }
809         puts("\n");
810
811         env_set_hex("fileaddr", addr);
812         env_set_hex("filesize", len_read);
813
814         return 0;
815 }
816
817 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
818           int fstype)
819 {
820         if (argc < 2)
821                 return CMD_RET_USAGE;
822         if (argc > 4)
823                 return CMD_RET_USAGE;
824
825         if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
826                 return 1;
827
828         if (fs_ls(argc >= 4 ? argv[3] : "/"))
829                 return 1;
830
831         return 0;
832 }
833
834 int file_exists(const char *dev_type, const char *dev_part, const char *file,
835                 int fstype)
836 {
837         if (fs_set_blk_dev(dev_type, dev_part, fstype))
838                 return 0;
839
840         return fs_exists(file);
841 }
842
843 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
844             int fstype)
845 {
846         unsigned long addr;
847         const char *filename;
848         loff_t bytes;
849         loff_t pos;
850         loff_t len;
851         int ret;
852         unsigned long time;
853
854         if (argc < 6 || argc > 7)
855                 return CMD_RET_USAGE;
856
857         if (fs_set_blk_dev(argv[1], argv[2], fstype))
858                 return 1;
859
860         addr = hextoul(argv[3], NULL);
861         filename = argv[4];
862         bytes = hextoul(argv[5], NULL);
863         if (argc >= 7)
864                 pos = hextoul(argv[6], NULL);
865         else
866                 pos = 0;
867
868         time = get_timer(0);
869         ret = fs_write(filename, addr, pos, bytes, &len);
870         time = get_timer(time);
871         if (ret < 0)
872                 return 1;
873
874         printf("%llu bytes written in %lu ms", len, time);
875         if (time > 0) {
876                 puts(" (");
877                 print_size(div_u64(len, time) * 1000, "/s");
878                 puts(")");
879         }
880         puts("\n");
881
882         return 0;
883 }
884
885 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
886                int fstype)
887 {
888         int ret;
889         char uuid[37];
890         memset(uuid, 0, sizeof(uuid));
891
892         if (argc < 3 || argc > 4)
893                 return CMD_RET_USAGE;
894
895         if (fs_set_blk_dev(argv[1], argv[2], fstype))
896                 return 1;
897
898         ret = fs_uuid(uuid);
899         if (ret)
900                 return CMD_RET_FAILURE;
901
902         if (argc == 4)
903                 env_set(argv[3], uuid);
904         else
905                 printf("%s\n", uuid);
906
907         return CMD_RET_SUCCESS;
908 }
909
910 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
911 {
912         struct fstype_info *info;
913
914         if (argc < 3 || argc > 4)
915                 return CMD_RET_USAGE;
916
917         if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
918                 return 1;
919
920         info = fs_get_info(fs_type);
921
922         if (argc == 4)
923                 env_set(argv[3], info->name);
924         else
925                 printf("%s\n", info->name);
926
927         fs_close();
928
929         return CMD_RET_SUCCESS;
930 }
931
932 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
933           int fstype)
934 {
935         if (argc != 4)
936                 return CMD_RET_USAGE;
937
938         if (fs_set_blk_dev(argv[1], argv[2], fstype))
939                 return 1;
940
941         if (fs_unlink(argv[3]))
942                 return 1;
943
944         return 0;
945 }
946
947 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
948              int fstype)
949 {
950         int ret;
951
952         if (argc != 4)
953                 return CMD_RET_USAGE;
954
955         if (fs_set_blk_dev(argv[1], argv[2], fstype))
956                 return 1;
957
958         ret = fs_mkdir(argv[3]);
959         if (ret) {
960                 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
961                 return 1;
962         }
963
964         return 0;
965 }
966
967 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
968           int fstype)
969 {
970         if (argc != 5)
971                 return CMD_RET_USAGE;
972
973         if (fs_set_blk_dev(argv[1], argv[2], fstype))
974                 return 1;
975
976         if (fs_ln(argv[3], argv[4]))
977                 return 1;
978
979         return 0;
980 }
981
982 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
983 {
984         struct fstype_info *drv = fstypes;
985         const int n_ents = ARRAY_SIZE(fstypes);
986         struct fstype_info *entry;
987         int i = 0;
988
989         puts("Supported filesystems");
990         for (entry = drv; entry != drv + n_ents; entry++) {
991                 if (entry->fstype != FS_TYPE_ANY) {
992                         printf("%c %s", i ? ',' : ':', entry->name);
993                         i++;
994                 }
995         }
996         if (!i)
997                 puts(": <none>");
998         puts("\n");
999         return CMD_RET_SUCCESS;
1000 }