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