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