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