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