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