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