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