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