]> git.ipfire.org Git - thirdparty/u-boot.git/blob - fs/fs.c
Merge tag 'i2cfixes-for-v2024-01-rc2' of https://source.denx.de/u-boot/custodians...
[thirdparty/u-boot.git] / fs / fs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
4 */
5
6 #define LOG_CATEGORY LOGC_CORE
7
8 #include <command.h>
9 #include <config.h>
10 #include <display_options.h>
11 #include <errno.h>
12 #include <common.h>
13 #include <env.h>
14 #include <lmb.h>
15 #include <log.h>
16 #include <malloc.h>
17 #include <mapmem.h>
18 #include <part.h>
19 #include <ext4fs.h>
20 #include <fat.h>
21 #include <fs.h>
22 #include <sandboxfs.h>
23 #include <semihostingfs.h>
24 #include <ubifs_uboot.h>
25 #include <btrfs.h>
26 #include <asm/global_data.h>
27 #include <asm/io.h>
28 #include <div64.h>
29 #include <linux/math64.h>
30 #include <linux/sizes.h>
31 #include <efi_loader.h>
32 #include <squashfs.h>
33 #include <erofs.h>
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 static struct blk_desc *fs_dev_desc;
38 static int fs_dev_part;
39 static struct disk_partition fs_partition;
40 static int fs_type = FS_TYPE_ANY;
41
42 void fs_set_type(int type)
43 {
44 fs_type = type;
45 }
46
47 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
48 struct disk_partition *fs_partition)
49 {
50 log_debug("Unrecognized filesystem type\n");
51 return -1;
52 }
53
54 static inline int fs_ls_unsupported(const char *dirname)
55 {
56 return -1;
57 }
58
59 /* generic implementation of ls in terms of opendir/readdir/closedir */
60 __maybe_unused
61 static 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++;
75 } else if (dent->type == FS_DT_LNK) {
76 printf(" <SYM> %s\n", dent->name);
77 nfiles++;
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
91 static inline int fs_exists_unsupported(const char *filename)
92 {
93 return 0;
94 }
95
96 static inline int fs_size_unsupported(const char *filename, loff_t *size)
97 {
98 return -1;
99 }
100
101 static inline int fs_read_unsupported(const char *filename, void *buf,
102 loff_t offset, loff_t len,
103 loff_t *actread)
104 {
105 return -1;
106 }
107
108 static inline int fs_write_unsupported(const char *filename, void *buf,
109 loff_t offset, loff_t len,
110 loff_t *actwrite)
111 {
112 return -1;
113 }
114
115 static inline int fs_ln_unsupported(const char *filename, const char *target)
116 {
117 return -1;
118 }
119
120 static inline void fs_close_unsupported(void)
121 {
122 }
123
124 static inline int fs_uuid_unsupported(char *uuid_str)
125 {
126 return -1;
127 }
128
129 static inline int fs_opendir_unsupported(const char *filename,
130 struct fs_dir_stream **dirs)
131 {
132 return -EACCES;
133 }
134
135 static inline int fs_unlink_unsupported(const char *filename)
136 {
137 return -1;
138 }
139
140 static inline int fs_mkdir_unsupported(const char *dirname)
141 {
142 return -1;
143 }
144
145 struct fstype_info {
146 int fstype;
147 char *name;
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
152 * set to true. This should also be true for the dummy entry at the end
153 * of fstypes[], since that is essentially a "virtual" (non-existent)
154 * filesystem.
155 */
156 bool null_dev_desc_ok;
157 int (*probe)(struct blk_desc *fs_dev_desc,
158 struct disk_partition *fs_partition);
159 int (*ls)(const char *dirname);
160 int (*exists)(const char *filename);
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);
166 void (*close)(void);
167 int (*uuid)(char *uuid_str);
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);
182 int (*unlink)(const char *filename);
183 int (*mkdir)(const char *dirname);
184 int (*ln)(const char *filename, const char *target);
185 };
186
187 static struct fstype_info fstypes[] = {
188 #if CONFIG_IS_ENABLED(FS_FAT)
189 {
190 .fstype = FS_TYPE_FAT,
191 .name = "fat",
192 .null_dev_desc_ok = false,
193 .probe = fat_set_blk_dev,
194 .close = fat_close,
195 .ls = fs_ls_generic,
196 .exists = fat_exists,
197 .size = fat_size,
198 .read = fat_read_file,
199 #if CONFIG_IS_ENABLED(FAT_WRITE)
200 .write = file_fat_write,
201 .unlink = fat_unlink,
202 .mkdir = fat_mkdir,
203 #else
204 .write = fs_write_unsupported,
205 .unlink = fs_unlink_unsupported,
206 .mkdir = fs_mkdir_unsupported,
207 #endif
208 .uuid = fat_uuid,
209 .opendir = fat_opendir,
210 .readdir = fat_readdir,
211 .closedir = fat_closedir,
212 .ln = fs_ln_unsupported,
213 },
214 #endif
215
216 #if CONFIG_IS_ENABLED(FS_EXT4)
217 {
218 .fstype = FS_TYPE_EXT,
219 .name = "ext4",
220 .null_dev_desc_ok = false,
221 .probe = ext4fs_probe,
222 .close = ext4fs_close,
223 .ls = ext4fs_ls,
224 .exists = ext4fs_exists,
225 .size = ext4fs_size,
226 .read = ext4_read_file,
227 #ifdef CONFIG_CMD_EXT4_WRITE
228 .write = ext4_write_file,
229 .ln = ext4fs_create_link,
230 #else
231 .write = fs_write_unsupported,
232 .ln = fs_ln_unsupported,
233 #endif
234 .uuid = ext4fs_uuid,
235 .opendir = fs_opendir_unsupported,
236 .unlink = fs_unlink_unsupported,
237 .mkdir = fs_mkdir_unsupported,
238 },
239 #endif
240 #if IS_ENABLED(CONFIG_SANDBOX) && !IS_ENABLED(CONFIG_SPL_BUILD)
241 {
242 .fstype = FS_TYPE_SANDBOX,
243 .name = "sandbox",
244 .null_dev_desc_ok = true,
245 .probe = sandbox_fs_set_blk_dev,
246 .close = sandbox_fs_close,
247 .ls = sandbox_fs_ls,
248 .exists = sandbox_fs_exists,
249 .size = sandbox_fs_size,
250 .read = fs_read_sandbox,
251 .write = fs_write_sandbox,
252 .uuid = fs_uuid_unsupported,
253 .opendir = fs_opendir_unsupported,
254 .unlink = fs_unlink_unsupported,
255 .mkdir = fs_mkdir_unsupported,
256 .ln = fs_ln_unsupported,
257 },
258 #endif
259 #ifdef CONFIG_SEMIHOSTING
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
278 #ifndef CONFIG_SPL_BUILD
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,
292 .opendir = fs_opendir_unsupported,
293 .unlink = fs_unlink_unsupported,
294 .mkdir = fs_mkdir_unsupported,
295 .ln = fs_ln_unsupported,
296 },
297 #endif
298 #endif
299 #ifndef CONFIG_SPL_BUILD
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,
313 .opendir = fs_opendir_unsupported,
314 .unlink = fs_unlink_unsupported,
315 .mkdir = fs_mkdir_unsupported,
316 .ln = fs_ln_unsupported,
317 },
318 #endif
319 #endif
320 #if CONFIG_IS_ENABLED(FS_SQUASHFS)
321 {
322 .fstype = FS_TYPE_SQUASHFS,
323 .name = "squashfs",
324 .null_dev_desc_ok = false,
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,
333 .exists = sqfs_exists,
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,
339 },
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 },
361 #endif
362 {
363 .fstype = FS_TYPE_ANY,
364 .name = "unsupported",
365 .null_dev_desc_ok = true,
366 .probe = fs_probe_unsupported,
367 .close = fs_close_unsupported,
368 .ls = fs_ls_unsupported,
369 .exists = fs_exists_unsupported,
370 .size = fs_size_unsupported,
371 .read = fs_read_unsupported,
372 .write = fs_write_unsupported,
373 .uuid = fs_uuid_unsupported,
374 .opendir = fs_opendir_unsupported,
375 .unlink = fs_unlink_unsupported,
376 .mkdir = fs_mkdir_unsupported,
377 .ln = fs_ln_unsupported,
378 },
379 };
380
381 static 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
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 */
403 int fs_get_type(void)
404 {
405 return fs_type;
406 }
407
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 */
416 const char *fs_get_type_name(void)
417 {
418 return fs_get_info(fs_type)->name;
419 }
420
421 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
422 {
423 struct fstype_info *info;
424 int part, i;
425
426 part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
427 &fs_partition, 1);
428 if (part < 0)
429 return -1;
430
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)
434 continue;
435
436 if (!fs_dev_desc && !info->null_dev_desc_ok)
437 continue;
438
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 # */
450 int 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++) {
464 if (!info->probe(fs_dev_desc, &fs_partition)) {
465 fs_type = info->fstype;
466 fs_dev_part = part;
467 return 0;
468 }
469 }
470
471 return -1;
472 }
473
474 void fs_close(void)
475 {
476 struct fstype_info *info = fs_get_info(fs_type);
477
478 info->close();
479
480 fs_type = FS_TYPE_ANY;
481 }
482
483 int 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
490 int fs_ls(const char *dirname)
491 {
492 int ret;
493
494 struct fstype_info *info = fs_get_info(fs_type);
495
496 ret = info->ls(dirname);
497
498 fs_close();
499
500 return ret;
501 }
502
503 int 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
516 int fs_size(const char *filename, loff_t *size)
517 {
518 int ret;
519
520 struct fstype_info *info = fs_get_info(fs_type);
521
522 ret = info->size(filename, size);
523
524 fs_close();
525
526 return ret;
527 }
528
529 #ifdef CONFIG_LMB
530 /* Check if a file may be read to the given address */
531 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
532 loff_t len, struct fstype_info *info)
533 {
534 struct lmb lmb;
535 int ret;
536 loff_t size;
537 loff_t read_len;
538
539 /* get the actual size of the file */
540 ret = info->size(filename, &size);
541 if (ret)
542 return ret;
543 if (offset >= size) {
544 /* offset >= EOF, no bytes will be written */
545 return 0;
546 }
547 read_len = size - offset;
548
549 /* limit to 'len' if it is smaller */
550 if (len && len < read_len)
551 read_len = len;
552
553 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
554 lmb_dump_all(&lmb);
555
556 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
557 return 0;
558
559 log_err("** Reading file would overwrite reserved memory **\n");
560 return -ENOSPC;
561 }
562 #endif
563
564 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
565 int do_lmb_check, loff_t *actread)
566 {
567 struct fstype_info *info = fs_get_info(fs_type);
568 void *buf;
569 int ret;
570
571 #ifdef CONFIG_LMB
572 if (do_lmb_check) {
573 ret = fs_read_lmb_check(filename, addr, offset, len, info);
574 if (ret)
575 return ret;
576 }
577 #endif
578
579 /*
580 * We don't actually know how many bytes are being read, since len==0
581 * means read the whole file.
582 */
583 buf = map_sysmem(addr, len);
584 ret = info->read(filename, buf, offset, len, actread);
585 unmap_sysmem(buf);
586
587 /* If we requested a specific number of bytes, check we got it */
588 if (ret == 0 && len && *actread != len)
589 log_debug("** %s shorter than offset + len **\n", filename);
590 fs_close();
591
592 return ret;
593 }
594
595 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
596 loff_t *actread)
597 {
598 return _fs_read(filename, addr, offset, len, 0, actread);
599 }
600
601 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
602 loff_t *actwrite)
603 {
604 struct fstype_info *info = fs_get_info(fs_type);
605 void *buf;
606 int ret;
607
608 buf = map_sysmem(addr, len);
609 ret = info->write(filename, buf, offset, len, actwrite);
610 unmap_sysmem(buf);
611
612 if (ret < 0 && len != *actwrite) {
613 log_err("** Unable to write file %s **\n", filename);
614 ret = -1;
615 }
616 fs_close();
617
618 return ret;
619 }
620
621 struct fs_dir_stream *fs_opendir(const char *filename)
622 {
623 struct fstype_info *info = fs_get_info(fs_type);
624 struct fs_dir_stream *dirs = NULL;
625 int ret;
626
627 ret = info->opendir(filename, &dirs);
628 fs_close();
629 if (ret) {
630 errno = -ret;
631 return NULL;
632 }
633
634 dirs->desc = fs_dev_desc;
635 dirs->part = fs_dev_part;
636
637 return dirs;
638 }
639
640 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
641 {
642 struct fstype_info *info;
643 struct fs_dirent *dirent;
644 int ret;
645
646 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
647 info = fs_get_info(fs_type);
648
649 ret = info->readdir(dirs, &dirent);
650 fs_close();
651 if (ret) {
652 errno = -ret;
653 return NULL;
654 }
655
656 return dirent;
657 }
658
659 void fs_closedir(struct fs_dir_stream *dirs)
660 {
661 struct fstype_info *info;
662
663 if (!dirs)
664 return;
665
666 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
667 info = fs_get_info(fs_type);
668
669 info->closedir(dirs);
670 fs_close();
671 }
672
673 int fs_unlink(const char *filename)
674 {
675 int ret;
676
677 struct fstype_info *info = fs_get_info(fs_type);
678
679 ret = info->unlink(filename);
680
681 fs_close();
682
683 return ret;
684 }
685
686 int fs_mkdir(const char *dirname)
687 {
688 int ret;
689
690 struct fstype_info *info = fs_get_info(fs_type);
691
692 ret = info->mkdir(dirname);
693
694 fs_close();
695
696 return ret;
697 }
698
699 int fs_ln(const char *fname, const char *target)
700 {
701 struct fstype_info *info = fs_get_info(fs_type);
702 int ret;
703
704 ret = info->ln(fname, target);
705
706 if (ret < 0) {
707 log_err("** Unable to create link %s -> %s **\n", fname, target);
708 ret = -1;
709 }
710 fs_close();
711
712 return ret;
713 }
714
715 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
716 int fstype)
717 {
718 loff_t size;
719
720 if (argc != 4)
721 return CMD_RET_USAGE;
722
723 if (fs_set_blk_dev(argv[1], argv[2], fstype))
724 return 1;
725
726 if (fs_size(argv[3], &size) < 0)
727 return CMD_RET_FAILURE;
728
729 env_set_hex("filesize", size);
730
731 return 0;
732 }
733
734 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
735 int fstype)
736 {
737 unsigned long addr;
738 const char *addr_str;
739 const char *filename;
740 loff_t bytes;
741 loff_t pos;
742 loff_t len_read;
743 int ret;
744 unsigned long time;
745 char *ep;
746
747 if (argc < 2)
748 return CMD_RET_USAGE;
749 if (argc > 7)
750 return CMD_RET_USAGE;
751
752 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) {
753 log_err("Can't set block device\n");
754 return 1;
755 }
756
757 if (argc >= 4) {
758 addr = hextoul(argv[3], &ep);
759 if (ep == argv[3] || *ep != '\0')
760 return CMD_RET_USAGE;
761 } else {
762 addr_str = env_get("loadaddr");
763 if (addr_str != NULL)
764 addr = hextoul(addr_str, NULL);
765 else
766 addr = CONFIG_SYS_LOAD_ADDR;
767 }
768 if (argc >= 5) {
769 filename = argv[4];
770 } else {
771 filename = env_get("bootfile");
772 if (!filename) {
773 puts("** No boot file defined **\n");
774 return 1;
775 }
776 }
777 if (argc >= 6)
778 bytes = hextoul(argv[5], NULL);
779 else
780 bytes = 0;
781 if (argc >= 7)
782 pos = hextoul(argv[6], NULL);
783 else
784 pos = 0;
785
786 time = get_timer(0);
787 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
788 time = get_timer(time);
789 if (ret < 0) {
790 log_err("Failed to load '%s'\n", filename);
791 return 1;
792 }
793
794 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
795 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
796 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
797 len_read);
798
799 printf("%llu bytes read in %lu ms", len_read, time);
800 if (time > 0) {
801 puts(" (");
802 print_size(div_u64(len_read, time) * 1000, "/s");
803 puts(")");
804 }
805 puts("\n");
806
807 env_set_hex("fileaddr", addr);
808 env_set_hex("filesize", len_read);
809
810 return 0;
811 }
812
813 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
814 int fstype)
815 {
816 if (argc < 2)
817 return CMD_RET_USAGE;
818 if (argc > 4)
819 return CMD_RET_USAGE;
820
821 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
822 return 1;
823
824 if (fs_ls(argc >= 4 ? argv[3] : "/"))
825 return 1;
826
827 return 0;
828 }
829
830 int file_exists(const char *dev_type, const char *dev_part, const char *file,
831 int fstype)
832 {
833 if (fs_set_blk_dev(dev_type, dev_part, fstype))
834 return 0;
835
836 return fs_exists(file);
837 }
838
839 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
840 int fstype)
841 {
842 unsigned long addr;
843 const char *filename;
844 loff_t bytes;
845 loff_t pos;
846 loff_t len;
847 int ret;
848 unsigned long time;
849
850 if (argc < 6 || argc > 7)
851 return CMD_RET_USAGE;
852
853 if (fs_set_blk_dev(argv[1], argv[2], fstype))
854 return 1;
855
856 addr = hextoul(argv[3], NULL);
857 filename = argv[4];
858 bytes = hextoul(argv[5], NULL);
859 if (argc >= 7)
860 pos = hextoul(argv[6], NULL);
861 else
862 pos = 0;
863
864 time = get_timer(0);
865 ret = fs_write(filename, addr, pos, bytes, &len);
866 time = get_timer(time);
867 if (ret < 0)
868 return 1;
869
870 printf("%llu bytes written in %lu ms", len, time);
871 if (time > 0) {
872 puts(" (");
873 print_size(div_u64(len, time) * 1000, "/s");
874 puts(")");
875 }
876 puts("\n");
877
878 return 0;
879 }
880
881 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
882 int fstype)
883 {
884 int ret;
885 char uuid[37];
886 memset(uuid, 0, sizeof(uuid));
887
888 if (argc < 3 || argc > 4)
889 return CMD_RET_USAGE;
890
891 if (fs_set_blk_dev(argv[1], argv[2], fstype))
892 return 1;
893
894 ret = fs_uuid(uuid);
895 if (ret)
896 return CMD_RET_FAILURE;
897
898 if (argc == 4)
899 env_set(argv[3], uuid);
900 else
901 printf("%s\n", uuid);
902
903 return CMD_RET_SUCCESS;
904 }
905
906 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
907 {
908 struct fstype_info *info;
909
910 if (argc < 3 || argc > 4)
911 return CMD_RET_USAGE;
912
913 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
914 return 1;
915
916 info = fs_get_info(fs_type);
917
918 if (argc == 4)
919 env_set(argv[3], info->name);
920 else
921 printf("%s\n", info->name);
922
923 fs_close();
924
925 return CMD_RET_SUCCESS;
926 }
927
928 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
929 int fstype)
930 {
931 if (argc != 4)
932 return CMD_RET_USAGE;
933
934 if (fs_set_blk_dev(argv[1], argv[2], fstype))
935 return 1;
936
937 if (fs_unlink(argv[3]))
938 return 1;
939
940 return 0;
941 }
942
943 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
944 int fstype)
945 {
946 int ret;
947
948 if (argc != 4)
949 return CMD_RET_USAGE;
950
951 if (fs_set_blk_dev(argv[1], argv[2], fstype))
952 return 1;
953
954 ret = fs_mkdir(argv[3]);
955 if (ret) {
956 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
957 return 1;
958 }
959
960 return 0;
961 }
962
963 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
964 int fstype)
965 {
966 if (argc != 5)
967 return CMD_RET_USAGE;
968
969 if (fs_set_blk_dev(argv[1], argv[2], fstype))
970 return 1;
971
972 if (fs_ln(argv[3], argv[4]))
973 return 1;
974
975 return 0;
976 }
977
978 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
979 {
980 struct fstype_info *drv = fstypes;
981 const int n_ents = ARRAY_SIZE(fstypes);
982 struct fstype_info *entry;
983 int i = 0;
984
985 puts("Supported filesystems");
986 for (entry = drv; entry != drv + n_ents; entry++) {
987 if (entry->fstype != FS_TYPE_ANY) {
988 printf("%c %s", i ? ',' : ':', entry->name);
989 i++;
990 }
991 }
992 if (!i)
993 puts(": <none>");
994 puts("\n");
995 return CMD_RET_SUCCESS;
996 }
997
998 int fs_read_alloc(const char *fname, ulong size, uint align, void **bufp)
999 {
1000 loff_t bytes_read;
1001 ulong addr;
1002 char *buf;
1003 int ret;
1004
1005 buf = memalign(align, size + 1);
1006 if (!buf)
1007 return log_msg_ret("buf", -ENOMEM);
1008 addr = map_to_sysmem(buf);
1009
1010 ret = fs_read(fname, addr, 0, size, &bytes_read);
1011 if (ret) {
1012 free(buf);
1013 return log_msg_ret("read", ret);
1014 }
1015 if (size != bytes_read)
1016 return log_msg_ret("bread", -EIO);
1017 buf[size] = '\0';
1018
1019 *bufp = buf;
1020
1021 return 0;
1022 }
1023
1024 int fs_load_alloc(const char *ifname, const char *dev_part_str,
1025 const char *fname, ulong max_size, ulong align, void **bufp,
1026 ulong *sizep)
1027 {
1028 loff_t size;
1029 void *buf;
1030 int ret;
1031
1032 if (fs_set_blk_dev(ifname, dev_part_str, FS_TYPE_ANY))
1033 return log_msg_ret("set", -ENOMEDIUM);
1034
1035 ret = fs_size(fname, &size);
1036 if (ret)
1037 return log_msg_ret("sz", -ENOENT);
1038
1039 if (size >= (max_size ?: SZ_1G))
1040 return log_msg_ret("sz", -E2BIG);
1041
1042 if (fs_set_blk_dev(ifname, dev_part_str, FS_TYPE_ANY))
1043 return log_msg_ret("set", -ENOMEDIUM);
1044
1045 ret = fs_read_alloc(fname, size, align, &buf);
1046 if (ret)
1047 return log_msg_ret("al", ret);
1048 *sizep = size;
1049 *bufp = buf;
1050
1051 return 0;
1052 }