spl: Kconfig: Replace CONFIG_SPL_EXT_SUPPORT to CONFIG_SPL_FS_EXT4
[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 #ifdef CONFIG_FS_EXT4
187 {
188 .fstype = FS_TYPE_EXT,
189 .name = "ext4",
190 .null_dev_desc_ok = false,
191 .probe = ext4fs_probe,
192 .close = ext4fs_close,
193 .ls = ext4fs_ls,
194 .exists = ext4fs_exists,
195 .size = ext4fs_size,
196 .read = ext4_read_file,
197 #ifdef CONFIG_CMD_EXT4_WRITE
198 .write = ext4_write_file,
199 #else
200 .write = fs_write_unsupported,
201 #endif
202 .uuid = ext4fs_uuid,
203 .opendir = fs_opendir_unsupported,
204 .unlink = fs_unlink_unsupported,
205 .mkdir = fs_mkdir_unsupported,
206 },
207 #endif
208 #ifdef CONFIG_SANDBOX
209 {
210 .fstype = FS_TYPE_SANDBOX,
211 .name = "sandbox",
212 .null_dev_desc_ok = true,
213 .probe = sandbox_fs_set_blk_dev,
214 .close = sandbox_fs_close,
215 .ls = sandbox_fs_ls,
216 .exists = sandbox_fs_exists,
217 .size = sandbox_fs_size,
218 .read = fs_read_sandbox,
219 .write = fs_write_sandbox,
220 .uuid = fs_uuid_unsupported,
221 .opendir = fs_opendir_unsupported,
222 .unlink = fs_unlink_unsupported,
223 .mkdir = fs_mkdir_unsupported,
224 },
225 #endif
226 #ifdef CONFIG_CMD_UBIFS
227 {
228 .fstype = FS_TYPE_UBIFS,
229 .name = "ubifs",
230 .null_dev_desc_ok = true,
231 .probe = ubifs_set_blk_dev,
232 .close = ubifs_close,
233 .ls = ubifs_ls,
234 .exists = ubifs_exists,
235 .size = ubifs_size,
236 .read = ubifs_read,
237 .write = fs_write_unsupported,
238 .uuid = fs_uuid_unsupported,
239 .opendir = fs_opendir_unsupported,
240 .unlink = fs_unlink_unsupported,
241 .mkdir = fs_mkdir_unsupported,
242 },
243 #endif
244 #ifdef CONFIG_FS_BTRFS
245 {
246 .fstype = FS_TYPE_BTRFS,
247 .name = "btrfs",
248 .null_dev_desc_ok = false,
249 .probe = btrfs_probe,
250 .close = btrfs_close,
251 .ls = btrfs_ls,
252 .exists = btrfs_exists,
253 .size = btrfs_size,
254 .read = btrfs_read,
255 .write = fs_write_unsupported,
256 .uuid = btrfs_uuid,
257 .opendir = fs_opendir_unsupported,
258 .unlink = fs_unlink_unsupported,
259 .mkdir = fs_mkdir_unsupported,
260 },
261 #endif
262 {
263 .fstype = FS_TYPE_ANY,
264 .name = "unsupported",
265 .null_dev_desc_ok = true,
266 .probe = fs_probe_unsupported,
267 .close = fs_close_unsupported,
268 .ls = fs_ls_unsupported,
269 .exists = fs_exists_unsupported,
270 .size = fs_size_unsupported,
271 .read = fs_read_unsupported,
272 .write = fs_write_unsupported,
273 .uuid = fs_uuid_unsupported,
274 .opendir = fs_opendir_unsupported,
275 .unlink = fs_unlink_unsupported,
276 .mkdir = fs_mkdir_unsupported,
277 },
278 };
279
280 static struct fstype_info *fs_get_info(int fstype)
281 {
282 struct fstype_info *info;
283 int i;
284
285 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
286 if (fstype == info->fstype)
287 return info;
288 }
289
290 /* Return the 'unsupported' sentinel */
291 return info;
292 }
293
294 /**
295 * fs_get_type_name() - Get type of current filesystem
296 *
297 * Return: Pointer to filesystem name
298 *
299 * Returns a string describing the current filesystem, or the sentinel
300 * "unsupported" for any unrecognised filesystem.
301 */
302 const char *fs_get_type_name(void)
303 {
304 return fs_get_info(fs_type)->name;
305 }
306
307 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
308 {
309 struct fstype_info *info;
310 int part, i;
311 #ifdef CONFIG_NEEDS_MANUAL_RELOC
312 static int relocated;
313
314 if (!relocated) {
315 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
316 i++, info++) {
317 info->name += gd->reloc_off;
318 info->probe += gd->reloc_off;
319 info->close += gd->reloc_off;
320 info->ls += gd->reloc_off;
321 info->read += gd->reloc_off;
322 info->write += gd->reloc_off;
323 }
324 relocated = 1;
325 }
326 #endif
327
328 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
329 &fs_partition, 1);
330 if (part < 0)
331 return -1;
332
333 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
334 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
335 fstype != info->fstype)
336 continue;
337
338 if (!fs_dev_desc && !info->null_dev_desc_ok)
339 continue;
340
341 if (!info->probe(fs_dev_desc, &fs_partition)) {
342 fs_type = info->fstype;
343 fs_dev_part = part;
344 return 0;
345 }
346 }
347
348 return -1;
349 }
350
351 /* set current blk device w/ blk_desc + partition # */
352 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
353 {
354 struct fstype_info *info;
355 int ret, i;
356
357 if (part >= 1)
358 ret = part_get_info(desc, part, &fs_partition);
359 else
360 ret = part_get_info_whole_disk(desc, &fs_partition);
361 if (ret)
362 return ret;
363 fs_dev_desc = desc;
364
365 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
366 if (!info->probe(fs_dev_desc, &fs_partition)) {
367 fs_type = info->fstype;
368 fs_dev_part = part;
369 return 0;
370 }
371 }
372
373 return -1;
374 }
375
376 static void fs_close(void)
377 {
378 struct fstype_info *info = fs_get_info(fs_type);
379
380 info->close();
381
382 fs_type = FS_TYPE_ANY;
383 }
384
385 int fs_uuid(char *uuid_str)
386 {
387 struct fstype_info *info = fs_get_info(fs_type);
388
389 return info->uuid(uuid_str);
390 }
391
392 int fs_ls(const char *dirname)
393 {
394 int ret;
395
396 struct fstype_info *info = fs_get_info(fs_type);
397
398 ret = info->ls(dirname);
399
400 fs_type = FS_TYPE_ANY;
401 fs_close();
402
403 return ret;
404 }
405
406 int fs_exists(const char *filename)
407 {
408 int ret;
409
410 struct fstype_info *info = fs_get_info(fs_type);
411
412 ret = info->exists(filename);
413
414 fs_close();
415
416 return ret;
417 }
418
419 int fs_size(const char *filename, loff_t *size)
420 {
421 int ret;
422
423 struct fstype_info *info = fs_get_info(fs_type);
424
425 ret = info->size(filename, size);
426
427 fs_close();
428
429 return ret;
430 }
431
432 #ifdef CONFIG_LMB
433 /* Check if a file may be read to the given address */
434 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
435 loff_t len, struct fstype_info *info)
436 {
437 struct lmb lmb;
438 int ret;
439 loff_t size;
440 loff_t read_len;
441
442 /* get the actual size of the file */
443 ret = info->size(filename, &size);
444 if (ret)
445 return ret;
446 if (offset >= size) {
447 /* offset >= EOF, no bytes will be written */
448 return 0;
449 }
450 read_len = size - offset;
451
452 /* limit to 'len' if it is smaller */
453 if (len && len < read_len)
454 read_len = len;
455
456 lmb_init_and_reserve(&lmb, gd->bd->bi_dram[0].start,
457 gd->bd->bi_dram[0].size, (void *)gd->fdt_blob);
458 lmb_dump_all(&lmb);
459
460 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
461 return 0;
462
463 printf("** Reading file would overwrite reserved memory **\n");
464 return -ENOSPC;
465 }
466 #endif
467
468 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
469 int do_lmb_check, loff_t *actread)
470 {
471 struct fstype_info *info = fs_get_info(fs_type);
472 void *buf;
473 int ret;
474
475 #ifdef CONFIG_LMB
476 if (do_lmb_check) {
477 ret = fs_read_lmb_check(filename, addr, offset, len, info);
478 if (ret)
479 return ret;
480 }
481 #endif
482
483 /*
484 * We don't actually know how many bytes are being read, since len==0
485 * means read the whole file.
486 */
487 buf = map_sysmem(addr, len);
488 ret = info->read(filename, buf, offset, len, actread);
489 unmap_sysmem(buf);
490
491 /* If we requested a specific number of bytes, check we got it */
492 if (ret == 0 && len && *actread != len)
493 debug("** %s shorter than offset + len **\n", filename);
494 fs_close();
495
496 return ret;
497 }
498
499 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
500 loff_t *actread)
501 {
502 return _fs_read(filename, addr, offset, len, 0, actread);
503 }
504
505 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
506 loff_t *actwrite)
507 {
508 struct fstype_info *info = fs_get_info(fs_type);
509 void *buf;
510 int ret;
511
512 buf = map_sysmem(addr, len);
513 ret = info->write(filename, buf, offset, len, actwrite);
514 unmap_sysmem(buf);
515
516 if (ret < 0 && len != *actwrite) {
517 printf("** Unable to write file %s **\n", filename);
518 ret = -1;
519 }
520 fs_close();
521
522 return ret;
523 }
524
525 struct fs_dir_stream *fs_opendir(const char *filename)
526 {
527 struct fstype_info *info = fs_get_info(fs_type);
528 struct fs_dir_stream *dirs = NULL;
529 int ret;
530
531 ret = info->opendir(filename, &dirs);
532 fs_close();
533 if (ret) {
534 errno = -ret;
535 return NULL;
536 }
537
538 dirs->desc = fs_dev_desc;
539 dirs->part = fs_dev_part;
540
541 return dirs;
542 }
543
544 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
545 {
546 struct fstype_info *info;
547 struct fs_dirent *dirent;
548 int ret;
549
550 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
551 info = fs_get_info(fs_type);
552
553 ret = info->readdir(dirs, &dirent);
554 fs_close();
555 if (ret) {
556 errno = -ret;
557 return NULL;
558 }
559
560 return dirent;
561 }
562
563 void fs_closedir(struct fs_dir_stream *dirs)
564 {
565 struct fstype_info *info;
566
567 if (!dirs)
568 return;
569
570 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
571 info = fs_get_info(fs_type);
572
573 info->closedir(dirs);
574 fs_close();
575 }
576
577 int fs_unlink(const char *filename)
578 {
579 int ret;
580
581 struct fstype_info *info = fs_get_info(fs_type);
582
583 ret = info->unlink(filename);
584
585 fs_type = FS_TYPE_ANY;
586 fs_close();
587
588 return ret;
589 }
590
591 int fs_mkdir(const char *dirname)
592 {
593 int ret;
594
595 struct fstype_info *info = fs_get_info(fs_type);
596
597 ret = info->mkdir(dirname);
598
599 fs_type = FS_TYPE_ANY;
600 fs_close();
601
602 return ret;
603 }
604
605 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
606 int fstype)
607 {
608 loff_t size;
609
610 if (argc != 4)
611 return CMD_RET_USAGE;
612
613 if (fs_set_blk_dev(argv[1], argv[2], fstype))
614 return 1;
615
616 if (fs_size(argv[3], &size) < 0)
617 return CMD_RET_FAILURE;
618
619 env_set_hex("filesize", size);
620
621 return 0;
622 }
623
624 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
625 int fstype)
626 {
627 unsigned long addr;
628 const char *addr_str;
629 const char *filename;
630 loff_t bytes;
631 loff_t pos;
632 loff_t len_read;
633 int ret;
634 unsigned long time;
635 char *ep;
636
637 if (argc < 2)
638 return CMD_RET_USAGE;
639 if (argc > 7)
640 return CMD_RET_USAGE;
641
642 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
643 return 1;
644
645 if (argc >= 4) {
646 addr = simple_strtoul(argv[3], &ep, 16);
647 if (ep == argv[3] || *ep != '\0')
648 return CMD_RET_USAGE;
649 } else {
650 addr_str = env_get("loadaddr");
651 if (addr_str != NULL)
652 addr = simple_strtoul(addr_str, NULL, 16);
653 else
654 addr = CONFIG_SYS_LOAD_ADDR;
655 }
656 if (argc >= 5) {
657 filename = argv[4];
658 } else {
659 filename = env_get("bootfile");
660 if (!filename) {
661 puts("** No boot file defined **\n");
662 return 1;
663 }
664 }
665 if (argc >= 6)
666 bytes = simple_strtoul(argv[5], NULL, 16);
667 else
668 bytes = 0;
669 if (argc >= 7)
670 pos = simple_strtoul(argv[6], NULL, 16);
671 else
672 pos = 0;
673
674 time = get_timer(0);
675 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
676 time = get_timer(time);
677 if (ret < 0)
678 return 1;
679
680 printf("%llu bytes read in %lu ms", len_read, time);
681 if (time > 0) {
682 puts(" (");
683 print_size(div_u64(len_read, time) * 1000, "/s");
684 puts(")");
685 }
686 puts("\n");
687
688 env_set_hex("fileaddr", addr);
689 env_set_hex("filesize", len_read);
690
691 return 0;
692 }
693
694 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
695 int fstype)
696 {
697 if (argc < 2)
698 return CMD_RET_USAGE;
699 if (argc > 4)
700 return CMD_RET_USAGE;
701
702 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
703 return 1;
704
705 if (fs_ls(argc >= 4 ? argv[3] : "/"))
706 return 1;
707
708 return 0;
709 }
710
711 int file_exists(const char *dev_type, const char *dev_part, const char *file,
712 int fstype)
713 {
714 if (fs_set_blk_dev(dev_type, dev_part, fstype))
715 return 0;
716
717 return fs_exists(file);
718 }
719
720 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
721 int fstype)
722 {
723 unsigned long addr;
724 const char *filename;
725 loff_t bytes;
726 loff_t pos;
727 loff_t len;
728 int ret;
729 unsigned long time;
730
731 if (argc < 6 || argc > 7)
732 return CMD_RET_USAGE;
733
734 if (fs_set_blk_dev(argv[1], argv[2], fstype))
735 return 1;
736
737 addr = simple_strtoul(argv[3], NULL, 16);
738 filename = argv[4];
739 bytes = simple_strtoul(argv[5], NULL, 16);
740 if (argc >= 7)
741 pos = simple_strtoul(argv[6], NULL, 16);
742 else
743 pos = 0;
744
745 time = get_timer(0);
746 ret = fs_write(filename, addr, pos, bytes, &len);
747 time = get_timer(time);
748 if (ret < 0)
749 return 1;
750
751 printf("%llu bytes written in %lu ms", len, time);
752 if (time > 0) {
753 puts(" (");
754 print_size(div_u64(len, time) * 1000, "/s");
755 puts(")");
756 }
757 puts("\n");
758
759 return 0;
760 }
761
762 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
763 int fstype)
764 {
765 int ret;
766 char uuid[37];
767 memset(uuid, 0, sizeof(uuid));
768
769 if (argc < 3 || argc > 4)
770 return CMD_RET_USAGE;
771
772 if (fs_set_blk_dev(argv[1], argv[2], fstype))
773 return 1;
774
775 ret = fs_uuid(uuid);
776 if (ret)
777 return CMD_RET_FAILURE;
778
779 if (argc == 4)
780 env_set(argv[3], uuid);
781 else
782 printf("%s\n", uuid);
783
784 return CMD_RET_SUCCESS;
785 }
786
787 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
788 {
789 struct fstype_info *info;
790
791 if (argc < 3 || argc > 4)
792 return CMD_RET_USAGE;
793
794 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
795 return 1;
796
797 info = fs_get_info(fs_type);
798
799 if (argc == 4)
800 env_set(argv[3], info->name);
801 else
802 printf("%s\n", info->name);
803
804 return CMD_RET_SUCCESS;
805 }
806
807 int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
808 int fstype)
809 {
810 if (argc != 4)
811 return CMD_RET_USAGE;
812
813 if (fs_set_blk_dev(argv[1], argv[2], fstype))
814 return 1;
815
816 if (fs_unlink(argv[3]))
817 return 1;
818
819 return 0;
820 }
821
822 int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
823 int fstype)
824 {
825 int ret;
826
827 if (argc != 4)
828 return CMD_RET_USAGE;
829
830 if (fs_set_blk_dev(argv[1], argv[2], fstype))
831 return 1;
832
833 ret = fs_mkdir(argv[3]);
834 if (ret) {
835 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
836 return 1;
837 }
838
839 return 0;
840 }