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