spl: fat/fs: Add control to build FS EXT4 in SPL
[project/bcm63xx/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 #include <config.h>
7 #include <errno.h>
8 #include <common.h>
9 #include <mapmem.h>
10 #include <part.h>
11 #include <ext4fs.h>
12 #include <fat.h>
13 #include <fs.h>
14 #include <sandboxfs.h>
15 #include <ubifs_uboot.h>
16 #include <btrfs.h>
17 #include <asm/io.h>
18 #include <div64.h>
19 #include <linux/math64.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 static struct blk_desc *fs_dev_desc;
24 static int fs_dev_part;
25 static disk_partition_t fs_partition;
26 static int fs_type = FS_TYPE_ANY;
27
28 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
29 disk_partition_t *fs_partition)
30 {
31 printf("** Unrecognized filesystem type **\n");
32 return -1;
33 }
34
35 static inline int fs_ls_unsupported(const char *dirname)
36 {
37 return -1;
38 }
39
40 /* generic implementation of ls in terms of opendir/readdir/closedir */
41 __maybe_unused
42 static int fs_ls_generic(const char *dirname)
43 {
44 struct fs_dir_stream *dirs;
45 struct fs_dirent *dent;
46 int nfiles = 0, ndirs = 0;
47
48 dirs = fs_opendir(dirname);
49 if (!dirs)
50 return -errno;
51
52 while ((dent = fs_readdir(dirs))) {
53 if (dent->type == FS_DT_DIR) {
54 printf(" %s/\n", dent->name);
55 ndirs++;
56 } else {
57 printf(" %8lld %s\n", dent->size, dent->name);
58 nfiles++;
59 }
60 }
61
62 fs_closedir(dirs);
63
64 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
65
66 return 0;
67 }
68
69 static inline int fs_exists_unsupported(const char *filename)
70 {
71 return 0;
72 }
73
74 static inline int fs_size_unsupported(const char *filename, loff_t *size)
75 {
76 return -1;
77 }
78
79 static inline int fs_read_unsupported(const char *filename, void *buf,
80 loff_t offset, loff_t len,
81 loff_t *actread)
82 {
83 return -1;
84 }
85
86 static inline int fs_write_unsupported(const char *filename, void *buf,
87 loff_t offset, loff_t len,
88 loff_t *actwrite)
89 {
90 return -1;
91 }
92
93 static inline void fs_close_unsupported(void)
94 {
95 }
96
97 static inline int fs_uuid_unsupported(char *uuid_str)
98 {
99 return -1;
100 }
101
102 static inline int fs_opendir_unsupported(const char *filename,
103 struct fs_dir_stream **dirs)
104 {
105 return -EACCES;
106 }
107
108 static inline int fs_unlink_unsupported(const char *filename)
109 {
110 return -1;
111 }
112
113 static inline int fs_mkdir_unsupported(const char *dirname)
114 {
115 return -1;
116 }
117
118 struct fstype_info {
119 int fstype;
120 char *name;
121 /*
122 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
123 * should be false in most cases. For "virtual" filesystems which
124 * aren't based on a U-Boot block device (e.g. sandbox), this can be
125 * set to true. This should also be true for the dummy entry at the end
126 * of fstypes[], since that is essentially a "virtual" (non-existent)
127 * filesystem.
128 */
129 bool null_dev_desc_ok;
130 int (*probe)(struct blk_desc *fs_dev_desc,
131 disk_partition_t *fs_partition);
132 int (*ls)(const char *dirname);
133 int (*exists)(const char *filename);
134 int (*size)(const char *filename, loff_t *size);
135 int (*read)(const char *filename, void *buf, loff_t offset,
136 loff_t len, loff_t *actread);
137 int (*write)(const char *filename, void *buf, loff_t offset,
138 loff_t len, loff_t *actwrite);
139 void (*close)(void);
140 int (*uuid)(char *uuid_str);
141 /*
142 * Open a directory stream. On success return 0 and directory
143 * stream pointer via 'dirsp'. On error, return -errno. See
144 * fs_opendir().
145 */
146 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
147 /*
148 * Read next entry from directory stream. On success return 0
149 * and directory entry pointer via 'dentp'. On error return
150 * -errno. See fs_readdir().
151 */
152 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
153 /* see fs_closedir() */
154 void (*closedir)(struct fs_dir_stream *dirs);
155 int (*unlink)(const char *filename);
156 int (*mkdir)(const char *dirname);
157 };
158
159 static struct fstype_info fstypes[] = {
160 #ifdef CONFIG_FS_FAT
161 {
162 .fstype = FS_TYPE_FAT,
163 .name = "fat",
164 .null_dev_desc_ok = false,
165 .probe = fat_set_blk_dev,
166 .close = fat_close,
167 .ls = fs_ls_generic,
168 .exists = fat_exists,
169 .size = fat_size,
170 .read = fat_read_file,
171 #if CONFIG_IS_ENABLED(FAT_WRITE)
172 .write = file_fat_write,
173 .unlink = fat_unlink,
174 .mkdir = fat_mkdir,
175 #else
176 .write = fs_write_unsupported,
177 .unlink = fs_unlink_unsupported,
178 .mkdir = fs_mkdir_unsupported,
179 #endif
180 .uuid = fs_uuid_unsupported,
181 .opendir = fat_opendir,
182 .readdir = fat_readdir,
183 .closedir = fat_closedir,
184 },
185 #endif
186
187 #if CONFIG_IS_ENABLED(FS_EXT4)
188 {
189 .fstype = FS_TYPE_EXT,
190 .name = "ext4",
191 .null_dev_desc_ok = false,
192 .probe = ext4fs_probe,
193 .close = ext4fs_close,
194 .ls = ext4fs_ls,
195 .exists = ext4fs_exists,
196 .size = ext4fs_size,
197 .read = ext4_read_file,
198 #ifdef CONFIG_CMD_EXT4_WRITE
199 .write = ext4_write_file,
200 #else
201 .write = fs_write_unsupported,
202 #endif
203 .uuid = ext4fs_uuid,
204 .opendir = fs_opendir_unsupported,
205 .unlink = fs_unlink_unsupported,
206 .mkdir = fs_mkdir_unsupported,
207 },
208 #endif
209 #ifdef CONFIG_SANDBOX
210 {
211 .fstype = FS_TYPE_SANDBOX,
212 .name = "sandbox",
213 .null_dev_desc_ok = true,
214 .probe = sandbox_fs_set_blk_dev,
215 .close = sandbox_fs_close,
216 .ls = sandbox_fs_ls,
217 .exists = sandbox_fs_exists,
218 .size = sandbox_fs_size,
219 .read = fs_read_sandbox,
220 .write = fs_write_sandbox,
221 .uuid = fs_uuid_unsupported,
222 .opendir = fs_opendir_unsupported,
223 .unlink = fs_unlink_unsupported,
224 .mkdir = fs_mkdir_unsupported,
225 },
226 #endif
227 #ifdef CONFIG_CMD_UBIFS
228 {
229 .fstype = FS_TYPE_UBIFS,
230 .name = "ubifs",
231 .null_dev_desc_ok = true,
232 .probe = ubifs_set_blk_dev,
233 .close = ubifs_close,
234 .ls = ubifs_ls,
235 .exists = ubifs_exists,
236 .size = ubifs_size,
237 .read = ubifs_read,
238 .write = fs_write_unsupported,
239 .uuid = fs_uuid_unsupported,
240 .opendir = fs_opendir_unsupported,
241 .unlink = fs_unlink_unsupported,
242 .mkdir = fs_mkdir_unsupported,
243 },
244 #endif
245 #ifdef CONFIG_FS_BTRFS
246 {
247 .fstype = FS_TYPE_BTRFS,
248 .name = "btrfs",
249 .null_dev_desc_ok = false,
250 .probe = btrfs_probe,
251 .close = btrfs_close,
252 .ls = btrfs_ls,
253 .exists = btrfs_exists,
254 .size = btrfs_size,
255 .read = btrfs_read,
256 .write = fs_write_unsupported,
257 .uuid = btrfs_uuid,
258 .opendir = fs_opendir_unsupported,
259 .unlink = fs_unlink_unsupported,
260 .mkdir = fs_mkdir_unsupported,
261 },
262 #endif
263 {
264 .fstype = FS_TYPE_ANY,
265 .name = "unsupported",
266 .null_dev_desc_ok = true,
267 .probe = fs_probe_unsupported,
268 .close = fs_close_unsupported,
269 .ls = fs_ls_unsupported,
270 .exists = fs_exists_unsupported,
271 .size = fs_size_unsupported,
272 .read = fs_read_unsupported,
273 .write = fs_write_unsupported,
274 .uuid = fs_uuid_unsupported,
275 .opendir = fs_opendir_unsupported,
276 .unlink = fs_unlink_unsupported,
277 .mkdir = fs_mkdir_unsupported,
278 },
279 };
280
281 static struct fstype_info *fs_get_info(int fstype)
282 {
283 struct fstype_info *info;
284 int i;
285
286 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
287 if (fstype == info->fstype)
288 return info;
289 }
290
291 /* Return the 'unsupported' sentinel */
292 return info;
293 }
294
295 /**
296 * fs_get_type_name() - Get type of current filesystem
297 *
298 * Return: Pointer to filesystem name
299 *
300 * Returns a string describing the current filesystem, or the sentinel
301 * "unsupported" for any unrecognised filesystem.
302 */
303 const char *fs_get_type_name(void)
304 {
305 return fs_get_info(fs_type)->name;
306 }
307
308 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
309 {
310 struct fstype_info *info;
311 int part, i;
312 #ifdef CONFIG_NEEDS_MANUAL_RELOC
313 static int relocated;
314
315 if (!relocated) {
316 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
317 i++, info++) {
318 info->name += gd->reloc_off;
319 info->probe += gd->reloc_off;
320 info->close += gd->reloc_off;
321 info->ls += gd->reloc_off;
322 info->read += gd->reloc_off;
323 info->write += gd->reloc_off;
324 }
325 relocated = 1;
326 }
327 #endif
328
329 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
330 &fs_partition, 1);
331 if (part < 0)
332 return -1;
333
334 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
335 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
336 fstype != info->fstype)
337 continue;
338
339 if (!fs_dev_desc && !info->null_dev_desc_ok)
340 continue;
341
342 if (!info->probe(fs_dev_desc, &fs_partition)) {
343 fs_type = info->fstype;
344 fs_dev_part = part;
345 return 0;
346 }
347 }
348
349 return -1;
350 }
351
352 /* set current blk device w/ blk_desc + partition # */
353 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
354 {
355 struct fstype_info *info;
356 int ret, i;
357
358 if (part >= 1)
359 ret = part_get_info(desc, part, &fs_partition);
360 else
361 ret = part_get_info_whole_disk(desc, &fs_partition);
362 if (ret)
363 return ret;
364 fs_dev_desc = desc;
365
366 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
367 if (!info->probe(fs_dev_desc, &fs_partition)) {
368 fs_type = info->fstype;
369 fs_dev_part = part;
370 return 0;
371 }
372 }
373
374 return -1;
375 }
376
377 static void fs_close(void)
378 {
379 struct fstype_info *info = fs_get_info(fs_type);
380
381 info->close();
382
383 fs_type = FS_TYPE_ANY;
384 }
385
386 int fs_uuid(char *uuid_str)
387 {
388 struct fstype_info *info = fs_get_info(fs_type);
389
390 return info->uuid(uuid_str);
391 }
392
393 int fs_ls(const char *dirname)
394 {
395 int ret;
396
397 struct fstype_info *info = fs_get_info(fs_type);
398
399 ret = info->ls(dirname);
400
401 fs_type = FS_TYPE_ANY;
402 fs_close();
403
404 return ret;
405 }
406
407 int fs_exists(const char *filename)
408 {
409 int ret;
410
411 struct fstype_info *info = fs_get_info(fs_type);
412
413 ret = info->exists(filename);
414
415 fs_close();
416
417 return ret;
418 }
419
420 int fs_size(const char *filename, loff_t *size)
421 {
422 int ret;
423
424 struct fstype_info *info = fs_get_info(fs_type);
425
426 ret = info->size(filename, size);
427
428 fs_close();
429
430 return ret;
431 }
432
433 #ifdef CONFIG_LMB
434 /* Check if a file may be read to the given address */
435 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
436 loff_t len, struct fstype_info *info)
437 {
438 struct lmb lmb;
439 int ret;
440 loff_t size;
441 loff_t read_len;
442
443 /* get the actual size of the file */
444 ret = info->size(filename, &size);
445 if (ret)
446 return ret;
447 if (offset >= size) {
448 /* offset >= EOF, no bytes will be written */
449 return 0;
450 }
451 read_len = size - offset;
452
453 /* limit to 'len' if it is smaller */
454 if (len && len < read_len)
455 read_len = len;
456
457 lmb_init_and_reserve(&lmb, gd->bd->bi_dram[0].start,
458 gd->bd->bi_dram[0].size, (void *)gd->fdt_blob);
459 lmb_dump_all(&lmb);
460
461 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
462 return 0;
463
464 printf("** Reading file would overwrite reserved memory **\n");
465 return -ENOSPC;
466 }
467 #endif
468
469 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
470 int do_lmb_check, loff_t *actread)
471 {
472 struct fstype_info *info = fs_get_info(fs_type);
473 void *buf;
474 int ret;
475
476 #ifdef CONFIG_LMB
477 if (do_lmb_check) {
478 ret = fs_read_lmb_check(filename, addr, offset, len, info);
479 if (ret)
480 return ret;
481 }
482 #endif
483
484 /*
485 * We don't actually know how many bytes are being read, since len==0
486 * means read the whole file.
487 */
488 buf = map_sysmem(addr, len);
489 ret = info->read(filename, buf, offset, len, actread);
490 unmap_sysmem(buf);
491
492 /* If we requested a specific number of bytes, check we got it */
493 if (ret == 0 && len && *actread != len)
494 debug("** %s shorter than offset + len **\n", filename);
495 fs_close();
496
497 return ret;
498 }
499
500 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
501 loff_t *actread)
502 {
503 return _fs_read(filename, addr, offset, len, 0, actread);
504 }
505
506 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
507 loff_t *actwrite)
508 {
509 struct fstype_info *info = fs_get_info(fs_type);
510 void *buf;
511 int ret;
512
513 buf = map_sysmem(addr, len);
514 ret = info->write(filename, buf, offset, len, actwrite);
515 unmap_sysmem(buf);
516
517 if (ret < 0 && len != *actwrite) {
518 printf("** Unable to write file %s **\n", filename);
519 ret = -1;
520 }
521 fs_close();
522
523 return ret;
524 }
525
526 struct fs_dir_stream *fs_opendir(const char *filename)
527 {
528 struct fstype_info *info = fs_get_info(fs_type);
529 struct fs_dir_stream *dirs = NULL;
530 int ret;
531
532 ret = info->opendir(filename, &dirs);
533 fs_close();
534 if (ret) {
535 errno = -ret;
536 return NULL;
537 }
538
539 dirs->desc = fs_dev_desc;
540 dirs->part = fs_dev_part;
541
542 return dirs;
543 }
544
545 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
546 {
547 struct fstype_info *info;
548 struct fs_dirent *dirent;
549 int ret;
550
551 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
552 info = fs_get_info(fs_type);
553
554 ret = info->readdir(dirs, &dirent);
555 fs_close();
556 if (ret) {
557 errno = -ret;
558 return NULL;
559 }
560
561 return dirent;
562 }
563
564 void fs_closedir(struct fs_dir_stream *dirs)
565 {
566 struct fstype_info *info;
567
568 if (!dirs)
569 return;
570
571 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
572 info = fs_get_info(fs_type);
573
574 info->closedir(dirs);
575 fs_close();
576 }
577
578 int fs_unlink(const char *filename)
579 {
580 int ret;
581
582 struct fstype_info *info = fs_get_info(fs_type);
583
584 ret = info->unlink(filename);
585
586 fs_type = FS_TYPE_ANY;
587 fs_close();
588
589 return ret;
590 }
591
592 int fs_mkdir(const char *dirname)
593 {
594 int ret;
595
596 struct fstype_info *info = fs_get_info(fs_type);
597
598 ret = info->mkdir(dirname);
599
600 fs_type = FS_TYPE_ANY;
601 fs_close();
602
603 return ret;
604 }
605
606 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
607 int fstype)
608 {
609 loff_t size;
610
611 if (argc != 4)
612 return CMD_RET_USAGE;
613
614 if (fs_set_blk_dev(argv[1], argv[2], fstype))
615 return 1;
616
617 if (fs_size(argv[3], &size) < 0)
618 return CMD_RET_FAILURE;
619
620 env_set_hex("filesize", size);
621
622 return 0;
623 }
624
625 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
626 int fstype)
627 {
628 unsigned long addr;
629 const char *addr_str;
630 const char *filename;
631 loff_t bytes;
632 loff_t pos;
633 loff_t len_read;
634 int ret;
635 unsigned long time;
636 char *ep;
637
638 if (argc < 2)
639 return CMD_RET_USAGE;
640 if (argc > 7)
641 return CMD_RET_USAGE;
642
643 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
644 return 1;
645
646 if (argc >= 4) {
647 addr = simple_strtoul(argv[3], &ep, 16);
648 if (ep == argv[3] || *ep != '\0')
649 return CMD_RET_USAGE;
650 } else {
651 addr_str = env_get("loadaddr");
652 if (addr_str != NULL)
653 addr = simple_strtoul(addr_str, NULL, 16);
654 else
655 addr = CONFIG_SYS_LOAD_ADDR;
656 }
657 if (argc >= 5) {
658 filename = argv[4];
659 } else {
660 filename = env_get("bootfile");
661 if (!filename) {
662 puts("** No boot file defined **\n");
663 return 1;
664 }
665 }
666 if (argc >= 6)
667 bytes = simple_strtoul(argv[5], NULL, 16);
668 else
669 bytes = 0;
670 if (argc >= 7)
671 pos = simple_strtoul(argv[6], NULL, 16);
672 else
673 pos = 0;
674
675 time = get_timer(0);
676 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
677 time = get_timer(time);
678 if (ret < 0)
679 return 1;
680
681 printf("%llu bytes read in %lu ms", len_read, time);
682 if (time > 0) {
683 puts(" (");
684 print_size(div_u64(len_read, time) * 1000, "/s");
685 puts(")");
686 }
687 puts("\n");
688
689 env_set_hex("fileaddr", addr);
690 env_set_hex("filesize", len_read);
691
692 return 0;
693 }
694
695 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
696 int fstype)
697 {
698 if (argc < 2)
699 return CMD_RET_USAGE;
700 if (argc > 4)
701 return CMD_RET_USAGE;
702
703 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
704 return 1;
705
706 if (fs_ls(argc >= 4 ? argv[3] : "/"))
707 return 1;
708
709 return 0;
710 }
711
712 int file_exists(const char *dev_type, const char *dev_part, const char *file,
713 int fstype)
714 {
715 if (fs_set_blk_dev(dev_type, dev_part, fstype))
716 return 0;
717
718 return fs_exists(file);
719 }
720
721 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
722 int fstype)
723 {
724 unsigned long addr;
725 const char *filename;
726 loff_t bytes;
727 loff_t pos;
728 loff_t len;
729 int ret;
730 unsigned long time;
731
732 if (argc < 6 || argc > 7)
733 return CMD_RET_USAGE;
734
735 if (fs_set_blk_dev(argv[1], argv[2], fstype))
736 return 1;
737
738 addr = simple_strtoul(argv[3], NULL, 16);
739 filename = argv[4];
740 bytes = simple_strtoul(argv[5], NULL, 16);
741 if (argc >= 7)
742 pos = simple_strtoul(argv[6], NULL, 16);
743 else
744 pos = 0;
745
746 time = get_timer(0);
747 ret = fs_write(filename, addr, pos, bytes, &len);
748 time = get_timer(time);
749 if (ret < 0)
750 return 1;
751
752 printf("%llu bytes written in %lu ms", len, time);
753 if (time > 0) {
754 puts(" (");
755 print_size(div_u64(len, time) * 1000, "/s");
756 puts(")");
757 }
758 puts("\n");
759
760 return 0;
761 }
762
763 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
764 int fstype)
765 {
766 int ret;
767 char uuid[37];
768 memset(uuid, 0, sizeof(uuid));
769
770 if (argc < 3 || argc > 4)
771 return CMD_RET_USAGE;
772
773 if (fs_set_blk_dev(argv[1], argv[2], fstype))
774 return 1;
775
776 ret = fs_uuid(uuid);
777 if (ret)
778 return CMD_RET_FAILURE;
779
780 if (argc == 4)
781 env_set(argv[3], uuid);
782 else
783 printf("%s\n", uuid);
784
785 return CMD_RET_SUCCESS;
786 }
787
788 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
789 {
790 struct fstype_info *info;
791
792 if (argc < 3 || argc > 4)
793 return CMD_RET_USAGE;
794
795 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
796 return 1;
797
798 info = fs_get_info(fs_type);
799
800 if (argc == 4)
801 env_set(argv[3], info->name);
802 else
803 printf("%s\n", info->name);
804
805 return CMD_RET_SUCCESS;
806 }
807
808 int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
809 int fstype)
810 {
811 if (argc != 4)
812 return CMD_RET_USAGE;
813
814 if (fs_set_blk_dev(argv[1], argv[2], fstype))
815 return 1;
816
817 if (fs_unlink(argv[3]))
818 return 1;
819
820 return 0;
821 }
822
823 int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
824 int fstype)
825 {
826 int ret;
827
828 if (argc != 4)
829 return CMD_RET_USAGE;
830
831 if (fs_set_blk_dev(argv[1], argv[2], fstype))
832 return 1;
833
834 ret = fs_mkdir(argv[3]);
835 if (ret) {
836 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
837 return 1;
838 }
839
840 return 0;
841 }