efi_loader: setting boot device
[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 = fat_uuid,
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 = sqfs_exists,
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] : "", map_sysmem(addr, 0),
756                                 len_read);
757
758         printf("%llu bytes read in %lu ms", len_read, time);
759         if (time > 0) {
760                 puts(" (");
761                 print_size(div_u64(len_read, time) * 1000, "/s");
762                 puts(")");
763         }
764         puts("\n");
765
766         env_set_hex("fileaddr", addr);
767         env_set_hex("filesize", len_read);
768
769         return 0;
770 }
771
772 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
773           int fstype)
774 {
775         if (argc < 2)
776                 return CMD_RET_USAGE;
777         if (argc > 4)
778                 return CMD_RET_USAGE;
779
780         if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
781                 return 1;
782
783         if (fs_ls(argc >= 4 ? argv[3] : "/"))
784                 return 1;
785
786         return 0;
787 }
788
789 int file_exists(const char *dev_type, const char *dev_part, const char *file,
790                 int fstype)
791 {
792         if (fs_set_blk_dev(dev_type, dev_part, fstype))
793                 return 0;
794
795         return fs_exists(file);
796 }
797
798 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
799             int fstype)
800 {
801         unsigned long addr;
802         const char *filename;
803         loff_t bytes;
804         loff_t pos;
805         loff_t len;
806         int ret;
807         unsigned long time;
808
809         if (argc < 6 || argc > 7)
810                 return CMD_RET_USAGE;
811
812         if (fs_set_blk_dev(argv[1], argv[2], fstype))
813                 return 1;
814
815         addr = simple_strtoul(argv[3], NULL, 16);
816         filename = argv[4];
817         bytes = simple_strtoul(argv[5], NULL, 16);
818         if (argc >= 7)
819                 pos = simple_strtoul(argv[6], NULL, 16);
820         else
821                 pos = 0;
822
823         time = get_timer(0);
824         ret = fs_write(filename, addr, pos, bytes, &len);
825         time = get_timer(time);
826         if (ret < 0)
827                 return 1;
828
829         printf("%llu bytes written in %lu ms", len, time);
830         if (time > 0) {
831                 puts(" (");
832                 print_size(div_u64(len, time) * 1000, "/s");
833                 puts(")");
834         }
835         puts("\n");
836
837         return 0;
838 }
839
840 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
841                int fstype)
842 {
843         int ret;
844         char uuid[37];
845         memset(uuid, 0, sizeof(uuid));
846
847         if (argc < 3 || argc > 4)
848                 return CMD_RET_USAGE;
849
850         if (fs_set_blk_dev(argv[1], argv[2], fstype))
851                 return 1;
852
853         ret = fs_uuid(uuid);
854         if (ret)
855                 return CMD_RET_FAILURE;
856
857         if (argc == 4)
858                 env_set(argv[3], uuid);
859         else
860                 printf("%s\n", uuid);
861
862         return CMD_RET_SUCCESS;
863 }
864
865 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
866 {
867         struct fstype_info *info;
868
869         if (argc < 3 || argc > 4)
870                 return CMD_RET_USAGE;
871
872         if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
873                 return 1;
874
875         info = fs_get_info(fs_type);
876
877         if (argc == 4)
878                 env_set(argv[3], info->name);
879         else
880                 printf("%s\n", info->name);
881
882         fs_close();
883
884         return CMD_RET_SUCCESS;
885 }
886
887 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
888           int fstype)
889 {
890         if (argc != 4)
891                 return CMD_RET_USAGE;
892
893         if (fs_set_blk_dev(argv[1], argv[2], fstype))
894                 return 1;
895
896         if (fs_unlink(argv[3]))
897                 return 1;
898
899         return 0;
900 }
901
902 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
903              int fstype)
904 {
905         int ret;
906
907         if (argc != 4)
908                 return CMD_RET_USAGE;
909
910         if (fs_set_blk_dev(argv[1], argv[2], fstype))
911                 return 1;
912
913         ret = fs_mkdir(argv[3]);
914         if (ret) {
915                 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
916                 return 1;
917         }
918
919         return 0;
920 }
921
922 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
923           int fstype)
924 {
925         if (argc != 5)
926                 return CMD_RET_USAGE;
927
928         if (fs_set_blk_dev(argv[1], argv[2], fstype))
929                 return 1;
930
931         if (fs_ln(argv[3], argv[4]))
932                 return 1;
933
934         return 0;
935 }
936
937 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
938 {
939         struct fstype_info *drv = fstypes;
940         const int n_ents = ARRAY_SIZE(fstypes);
941         struct fstype_info *entry;
942         int i = 0;
943
944         puts("Supported filesystems");
945         for (entry = drv; entry != drv + n_ents; entry++) {
946                 if (entry->fstype != FS_TYPE_ANY) {
947                         printf("%c %s", i ? ',' : ':', entry->name);
948                         i++;
949                 }
950         }
951         if (!i)
952                 puts(": <none>");
953         puts("\n");
954         return CMD_RET_SUCCESS;
955 }