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