]> Git Repo - J-u-boot.git/blame - fs/fs.c
fs: fat: structure for name and extension
[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
c0029e4e 197 .uuid = fat_uuid,
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",
1b1e0c01 290 .null_dev_desc_ok = false,
c5100613
JMC
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,
dd4866b4 299 .exists = sqfs_exists,
1b1e0c01
RG
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,
c5100613 305 },
436e2b73
SG
306#endif
307 {
308 .fstype = FS_TYPE_ANY,
1a1ad8e0 309 .name = "unsupported",
377202b5 310 .null_dev_desc_ok = true,
436e2b73
SG
311 .probe = fs_probe_unsupported,
312 .close = fs_close_unsupported,
313 .ls = fs_ls_unsupported,
6152916a 314 .exists = fs_exists_unsupported,
cf659819 315 .size = fs_size_unsupported,
436e2b73 316 .read = fs_read_unsupported,
a8f6ab52 317 .write = fs_write_unsupported,
59e890ef 318 .uuid = fs_uuid_unsupported,
4bbcc965 319 .opendir = fs_opendir_unsupported,
e2519daf 320 .unlink = fs_unlink_unsupported,
e7074cff 321 .mkdir = fs_mkdir_unsupported,
aaa12157 322 .ln = fs_ln_unsupported,
045fa1e1
SW
323 },
324};
325
c6f548d2
SG
326static 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
b7cd9562
AT
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 */
348int fs_get_type(void)
349{
350 return fs_type;
351}
352
0d488e8f
AK
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 */
361const char *fs_get_type_name(void)
362{
363 return fs_get_info(fs_type)->name;
364}
365
045fa1e1
SW
366int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
367{
436e2b73 368 struct fstype_info *info;
045fa1e1 369 int part, i;
a1b231ce
SW
370#ifdef CONFIG_NEEDS_MANUAL_RELOC
371 static int relocated;
372
373 if (!relocated) {
436e2b73
SG
374 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
375 i++, info++) {
1a1ad8e0 376 info->name += gd->reloc_off;
436e2b73
SG
377 info->probe += gd->reloc_off;
378 info->close += gd->reloc_off;
379 info->ls += gd->reloc_off;
380 info->read += gd->reloc_off;
a8f6ab52 381 info->write += gd->reloc_off;
436e2b73 382 }
a1b231ce
SW
383 relocated = 1;
384 }
385#endif
045fa1e1 386
e35929e4 387 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
045fa1e1
SW
388 &fs_partition, 1);
389 if (part < 0)
390 return -1;
391
436e2b73
SG
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)
045fa1e1
SW
395 continue;
396
377202b5
SW
397 if (!fs_dev_desc && !info->null_dev_desc_ok)
398 continue;
399
4bbcc965
RC
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 # */
411int 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++) {
2ded0d47 425 if (!info->probe(fs_dev_desc, &fs_partition)) {
436e2b73 426 fs_type = info->fstype;
b0c78d8f 427 fs_dev_part = part;
045fa1e1
SW
428 return 0;
429 }
430 }
431
045fa1e1
SW
432 return -1;
433}
434
64f49eb7 435void fs_close(void)
045fa1e1 436{
c6f548d2 437 struct fstype_info *info = fs_get_info(fs_type);
045fa1e1 438
c6f548d2 439 info->close();
e6d52415 440
045fa1e1
SW
441 fs_type = FS_TYPE_ANY;
442}
443
59e890ef
CG
444int 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
045fa1e1
SW
451int fs_ls(const char *dirname)
452{
453 int ret;
454
c6f548d2
SG
455 struct fstype_info *info = fs_get_info(fs_type);
456
457 ret = info->ls(dirname);
045fa1e1
SW
458
459 fs_close();
460
461 return ret;
462}
463
6152916a
SW
464int 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
d455d878 477int fs_size(const char *filename, loff_t *size)
cf659819
SW
478{
479 int ret;
480
481 struct fstype_info *info = fs_get_info(fs_type);
482
d455d878 483 ret = info->size(filename, size);
cf659819
SW
484
485 fs_close();
486
487 return ret;
488}
489
aa3c609e
SG
490#ifdef CONFIG_LMB
491/* Check if a file may be read to the given address */
492static 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
9cc2323f 514 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
aa3c609e
SG
515 lmb_dump_all(&lmb);
516
517 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
518 return 0;
519
0d32d8cf 520 log_err("** Reading file would overwrite reserved memory **\n");
aa3c609e
SG
521 return -ENOSPC;
522}
523#endif
524
525static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
526 int do_lmb_check, loff_t *actread)
045fa1e1 527{
c6f548d2 528 struct fstype_info *info = fs_get_info(fs_type);
117e0507 529 void *buf;
045fa1e1
SW
530 int ret;
531
aa3c609e
SG
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
117e0507
SG
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);
d455d878 545 ret = info->read(filename, buf, offset, len, actread);
117e0507 546 unmap_sysmem(buf);
045fa1e1 547
c6f548d2 548 /* If we requested a specific number of bytes, check we got it */
7a3e70cf 549 if (ret == 0 && len && *actread != len)
0d32d8cf 550 log_debug("** %s shorter than offset + len **\n", filename);
045fa1e1
SW
551 fs_close();
552
553 return ret;
554}
555
aa3c609e
SG
556int 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
d455d878
SR
562int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
563 loff_t *actwrite)
a8f6ab52
SG
564{
565 struct fstype_info *info = fs_get_info(fs_type);
566 void *buf;
567 int ret;
568
a8f6ab52 569 buf = map_sysmem(addr, len);
d455d878 570 ret = info->write(filename, buf, offset, len, actwrite);
a8f6ab52
SG
571 unmap_sysmem(buf);
572
d455d878 573 if (ret < 0 && len != *actwrite) {
0d32d8cf 574 log_err("** Unable to write file %s **\n", filename);
a8f6ab52
SG
575 ret = -1;
576 }
577 fs_close();
578
579 return ret;
580}
581
4bbcc965
RC
582struct 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
601struct 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
620void 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
e2519daf
AT
634int 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
e2519daf
AT
642 fs_close();
643
644 return ret;
645}
4bbcc965 646
e7074cff
AT
647int 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
e7074cff
AT
655 fs_close();
656
657 return ret;
658}
659
aaa12157
JJH
660int 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) {
0d32d8cf 668 log_err("** Unable to create link %s -> %s **\n", fname, target);
aaa12157
JJH
669 ret = -1;
670 }
671 fs_close();
672
673 return ret;
674}
675
09140113
SG
676int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
677 int fstype)
cf659819 678{
d455d878 679 loff_t size;
cf659819
SW
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
d455d878 687 if (fs_size(argv[3], &size) < 0)
cf659819
SW
688 return CMD_RET_FAILURE;
689
018f5303 690 env_set_hex("filesize", size);
cf659819
SW
691
692 return 0;
693}
694
09140113
SG
695int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
696 int fstype)
045fa1e1
SW
697{
698 unsigned long addr;
699 const char *addr_str;
700 const char *filename;
d455d878
SR
701 loff_t bytes;
702 loff_t pos;
703 loff_t len_read;
704 int ret;
da1fd96c 705 unsigned long time;
949bbd7c 706 char *ep;
045fa1e1 707
e9b0f99e
SW
708 if (argc < 2)
709 return CMD_RET_USAGE;
710 if (argc > 7)
045fa1e1
SW
711 return CMD_RET_USAGE;
712
e9b0f99e 713 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
045fa1e1
SW
714 return 1;
715
716 if (argc >= 4) {
949bbd7c
PM
717 addr = simple_strtoul(argv[3], &ep, 16);
718 if (ep == argv[3] || *ep != '\0')
719 return CMD_RET_USAGE;
045fa1e1 720 } else {
00caae6d 721 addr_str = env_get("loadaddr");
045fa1e1
SW
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 {
00caae6d 730 filename = env_get("bootfile");
045fa1e1
SW
731 if (!filename) {
732 puts("** No boot file defined **\n");
733 return 1;
734 }
735 }
736 if (argc >= 6)
b770e88a 737 bytes = simple_strtoul(argv[5], NULL, 16);
045fa1e1
SW
738 else
739 bytes = 0;
740 if (argc >= 7)
b770e88a 741 pos = simple_strtoul(argv[6], NULL, 16);
045fa1e1
SW
742 else
743 pos = 0;
744
da1fd96c 745 time = get_timer(0);
aa3c609e 746 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
da1fd96c 747 time = get_timer(time);
1244f369 748 if (ret < 0) {
0d32d8cf 749 log_err("Failed to load '%s'\n", filename);
045fa1e1 750 return 1;
1244f369
HS
751 }
752
753 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
754 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
5f59518a
HS
755 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
756 len_read);
045fa1e1 757
d455d878 758 printf("%llu bytes read in %lu ms", len_read, time);
da1fd96c
AB
759 if (time > 0) {
760 puts(" (");
9e374e7b 761 print_size(div_u64(len_read, time) * 1000, "/s");
da1fd96c
AB
762 puts(")");
763 }
764 puts("\n");
045fa1e1 765
018f5303
SG
766 env_set_hex("fileaddr", addr);
767 env_set_hex("filesize", len_read);
045fa1e1
SW
768
769 return 0;
770}
771
09140113
SG
772int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
773 int fstype)
045fa1e1
SW
774{
775 if (argc < 2)
776 return CMD_RET_USAGE;
e9b0f99e
SW
777 if (argc > 4)
778 return CMD_RET_USAGE;
045fa1e1
SW
779
780 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
781 return 1;
782
e9b0f99e 783 if (fs_ls(argc >= 4 ? argv[3] : "/"))
045fa1e1
SW
784 return 1;
785
786 return 0;
787}
a8f6ab52 788
6152916a
SW
789int 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
09140113
SG
798int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
799 int fstype)
a8f6ab52
SG
800{
801 unsigned long addr;
802 const char *filename;
d455d878
SR
803 loff_t bytes;
804 loff_t pos;
805 loff_t len;
806 int ret;
a8f6ab52
SG
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
d455d878
SR
815 addr = simple_strtoul(argv[3], NULL, 16);
816 filename = argv[4];
b770e88a 817 bytes = simple_strtoul(argv[5], NULL, 16);
a8f6ab52 818 if (argc >= 7)
b770e88a 819 pos = simple_strtoul(argv[6], NULL, 16);
a8f6ab52
SG
820 else
821 pos = 0;
822
823 time = get_timer(0);
d455d878 824 ret = fs_write(filename, addr, pos, bytes, &len);
a8f6ab52 825 time = get_timer(time);
d455d878 826 if (ret < 0)
a8f6ab52
SG
827 return 1;
828
d455d878 829 printf("%llu bytes written in %lu ms", len, time);
a8f6ab52
SG
830 if (time > 0) {
831 puts(" (");
9e374e7b 832 print_size(div_u64(len, time) * 1000, "/s");
a8f6ab52
SG
833 puts(")");
834 }
835 puts("\n");
836
837 return 0;
838}
59e890ef 839
09140113
SG
840int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
841 int fstype)
59e890ef
CG
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)
382bee57 858 env_set(argv[3], uuid);
59e890ef
CG
859 else
860 printf("%s\n", uuid);
861
862 return CMD_RET_SUCCESS;
863}
1a1ad8e0 864
09140113 865int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1a1ad8e0
SS
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)
382bee57 878 env_set(argv[3], info->name);
1a1ad8e0
SS
879 else
880 printf("%s\n", info->name);
881
e531c673
MV
882 fs_close();
883
1a1ad8e0
SS
884 return CMD_RET_SUCCESS;
885}
886
09140113 887int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
e2519daf
AT
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
09140113 902int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
e7074cff
AT
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) {
0d32d8cf 915 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
e7074cff
AT
916 return 1;
917 }
918
919 return 0;
920}
aaa12157 921
09140113 922int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
aaa12157
JJH
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}
2280fa56
NF
936
937int 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}
This page took 0.489015 seconds and 4 git commands to generate.