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