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