]> git.ipfire.org Git - people/ms/u-boot.git/blame - fs/fs.c
fs: btrfs: Add btrfs_tree.h and ctree.h from Linux (and modified)
[people/ms/u-boot.git] / fs / fs.c
CommitLineData
045fa1e1
SW
1/*
2 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
3 *
5b8031cc 4 * SPDX-License-Identifier: GPL-2.0
045fa1e1
SW
5 */
6
7#include <config.h>
59e890ef 8#include <errno.h>
045fa1e1 9#include <common.h>
0eb25b61 10#include <mapmem.h>
045fa1e1
SW
11#include <part.h>
12#include <ext4fs.h>
13#include <fat.h>
14#include <fs.h>
92ccc96b 15#include <sandboxfs.h>
251cee0d 16#include <ubifs_uboot.h>
117e0507 17#include <asm/io.h>
9e374e7b
TR
18#include <div64.h>
19#include <linux/math64.h>
045fa1e1 20
a1b231ce
SW
21DECLARE_GLOBAL_DATA_PTR;
22
4101f687 23static struct blk_desc *fs_dev_desc;
4bbcc965 24static int fs_dev_part;
045fa1e1
SW
25static disk_partition_t fs_partition;
26static int fs_type = FS_TYPE_ANY;
27
4101f687 28static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
2ded0d47 29 disk_partition_t *fs_partition)
436e2b73
SG
30{
31 printf("** Unrecognized filesystem type **\n");
32 return -1;
33}
34
045fa1e1
SW
35static inline int fs_ls_unsupported(const char *dirname)
36{
045fa1e1
SW
37 return -1;
38}
39
89191d62
RC
40/* generic implementation of ls in terms of opendir/readdir/closedir */
41__maybe_unused
42static 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
6152916a
SW
69static inline int fs_exists_unsupported(const char *filename)
70{
71 return 0;
72}
73
d455d878 74static inline int fs_size_unsupported(const char *filename, loff_t *size)
cf659819
SW
75{
76 return -1;
77}
78
117e0507 79static inline int fs_read_unsupported(const char *filename, void *buf,
d455d878
SR
80 loff_t offset, loff_t len,
81 loff_t *actread)
045fa1e1 82{
045fa1e1
SW
83 return -1;
84}
85
a8f6ab52 86static inline int fs_write_unsupported(const char *filename, void *buf,
d455d878
SR
87 loff_t offset, loff_t len,
88 loff_t *actwrite)
a8f6ab52
SG
89{
90 return -1;
91}
92
436e2b73
SG
93static inline void fs_close_unsupported(void)
94{
95}
96
59e890ef
CG
97static inline int fs_uuid_unsupported(char *uuid_str)
98{
99 return -1;
100}
101
4bbcc965
RC
102static inline int fs_opendir_unsupported(const char *filename,
103 struct fs_dir_stream **dirs)
104{
105 return -EACCES;
106}
107
436e2b73 108struct fstype_info {
045fa1e1 109 int fstype;
1a1ad8e0 110 char *name;
377202b5
SW
111 /*
112 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
113 * should be false in most cases. For "virtual" filesystems which
114 * aren't based on a U-Boot block device (e.g. sandbox), this can be
115 * set to true. This should also be true for the dumm entry at the end
116 * of fstypes[], since that is essentially a "virtual" (non-existent)
117 * filesystem.
118 */
119 bool null_dev_desc_ok;
4101f687 120 int (*probe)(struct blk_desc *fs_dev_desc,
2ded0d47 121 disk_partition_t *fs_partition);
436e2b73 122 int (*ls)(const char *dirname);
6152916a 123 int (*exists)(const char *filename);
d455d878
SR
124 int (*size)(const char *filename, loff_t *size);
125 int (*read)(const char *filename, void *buf, loff_t offset,
126 loff_t len, loff_t *actread);
127 int (*write)(const char *filename, void *buf, loff_t offset,
128 loff_t len, loff_t *actwrite);
436e2b73 129 void (*close)(void);
59e890ef 130 int (*uuid)(char *uuid_str);
4bbcc965
RC
131 /*
132 * Open a directory stream. On success return 0 and directory
133 * stream pointer via 'dirsp'. On error, return -errno. See
134 * fs_opendir().
135 */
136 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
137 /*
138 * Read next entry from directory stream. On success return 0
139 * and directory entry pointer via 'dentp'. On error return
140 * -errno. See fs_readdir().
141 */
142 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
143 /* see fs_closedir() */
144 void (*closedir)(struct fs_dir_stream *dirs);
436e2b73
SG
145};
146
147static struct fstype_info fstypes[] = {
148#ifdef CONFIG_FS_FAT
045fa1e1
SW
149 {
150 .fstype = FS_TYPE_FAT,
1a1ad8e0 151 .name = "fat",
377202b5 152 .null_dev_desc_ok = false,
e6d52415
SG
153 .probe = fat_set_blk_dev,
154 .close = fat_close,
89191d62 155 .ls = fs_ls_generic,
b7b5f319 156 .exists = fat_exists,
cf659819 157 .size = fat_size,
e6d52415 158 .read = fat_read_file,
d455d878
SR
159#ifdef CONFIG_FAT_WRITE
160 .write = file_fat_write,
161#else
bd6fb31f 162 .write = fs_write_unsupported,
d455d878 163#endif
59e890ef 164 .uuid = fs_uuid_unsupported,
89191d62
RC
165 .opendir = fat_opendir,
166 .readdir = fat_readdir,
167 .closedir = fat_closedir,
045fa1e1 168 },
436e2b73
SG
169#endif
170#ifdef CONFIG_FS_EXT4
045fa1e1
SW
171 {
172 .fstype = FS_TYPE_EXT,
1a1ad8e0 173 .name = "ext4",
377202b5 174 .null_dev_desc_ok = false,
e6d52415
SG
175 .probe = ext4fs_probe,
176 .close = ext4fs_close,
436e2b73 177 .ls = ext4fs_ls,
55af5c93 178 .exists = ext4fs_exists,
cf659819 179 .size = ext4fs_size,
e6d52415 180 .read = ext4_read_file,
d455d878
SR
181#ifdef CONFIG_CMD_EXT4_WRITE
182 .write = ext4_write_file,
183#else
bd6fb31f 184 .write = fs_write_unsupported,
d455d878 185#endif
59e890ef 186 .uuid = ext4fs_uuid,
4bbcc965 187 .opendir = fs_opendir_unsupported,
436e2b73 188 },
92ccc96b
SG
189#endif
190#ifdef CONFIG_SANDBOX
191 {
192 .fstype = FS_TYPE_SANDBOX,
1a1ad8e0 193 .name = "sandbox",
377202b5 194 .null_dev_desc_ok = true,
92ccc96b
SG
195 .probe = sandbox_fs_set_blk_dev,
196 .close = sandbox_fs_close,
197 .ls = sandbox_fs_ls,
0a30aa1e 198 .exists = sandbox_fs_exists,
cf659819 199 .size = sandbox_fs_size,
92ccc96b 200 .read = fs_read_sandbox,
7eb2c8d5 201 .write = fs_write_sandbox,
59e890ef 202 .uuid = fs_uuid_unsupported,
4bbcc965 203 .opendir = fs_opendir_unsupported,
92ccc96b 204 },
251cee0d
HG
205#endif
206#ifdef CONFIG_CMD_UBIFS
207 {
208 .fstype = FS_TYPE_UBIFS,
209 .name = "ubifs",
210 .null_dev_desc_ok = true,
211 .probe = ubifs_set_blk_dev,
212 .close = ubifs_close,
213 .ls = ubifs_ls,
214 .exists = ubifs_exists,
215 .size = ubifs_size,
216 .read = ubifs_read,
217 .write = fs_write_unsupported,
218 .uuid = fs_uuid_unsupported,
4bbcc965 219 .opendir = fs_opendir_unsupported,
251cee0d 220 },
436e2b73
SG
221#endif
222 {
223 .fstype = FS_TYPE_ANY,
1a1ad8e0 224 .name = "unsupported",
377202b5 225 .null_dev_desc_ok = true,
436e2b73
SG
226 .probe = fs_probe_unsupported,
227 .close = fs_close_unsupported,
228 .ls = fs_ls_unsupported,
6152916a 229 .exists = fs_exists_unsupported,
cf659819 230 .size = fs_size_unsupported,
436e2b73 231 .read = fs_read_unsupported,
a8f6ab52 232 .write = fs_write_unsupported,
59e890ef 233 .uuid = fs_uuid_unsupported,
4bbcc965 234 .opendir = fs_opendir_unsupported,
045fa1e1
SW
235 },
236};
237
c6f548d2
SG
238static struct fstype_info *fs_get_info(int fstype)
239{
240 struct fstype_info *info;
241 int i;
242
243 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
244 if (fstype == info->fstype)
245 return info;
246 }
247
248 /* Return the 'unsupported' sentinel */
249 return info;
250}
251
045fa1e1
SW
252int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
253{
436e2b73 254 struct fstype_info *info;
045fa1e1 255 int part, i;
a1b231ce
SW
256#ifdef CONFIG_NEEDS_MANUAL_RELOC
257 static int relocated;
258
259 if (!relocated) {
436e2b73
SG
260 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
261 i++, info++) {
1a1ad8e0 262 info->name += gd->reloc_off;
436e2b73
SG
263 info->probe += gd->reloc_off;
264 info->close += gd->reloc_off;
265 info->ls += gd->reloc_off;
266 info->read += gd->reloc_off;
a8f6ab52 267 info->write += gd->reloc_off;
436e2b73 268 }
a1b231ce
SW
269 relocated = 1;
270 }
271#endif
045fa1e1 272
e35929e4 273 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
045fa1e1
SW
274 &fs_partition, 1);
275 if (part < 0)
276 return -1;
277
436e2b73
SG
278 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
279 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
280 fstype != info->fstype)
045fa1e1
SW
281 continue;
282
377202b5
SW
283 if (!fs_dev_desc && !info->null_dev_desc_ok)
284 continue;
285
4bbcc965
RC
286 if (!info->probe(fs_dev_desc, &fs_partition)) {
287 fs_type = info->fstype;
288 fs_dev_part = part;
289 return 0;
290 }
291 }
292
293 return -1;
294}
295
296/* set current blk device w/ blk_desc + partition # */
297int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
298{
299 struct fstype_info *info;
300 int ret, i;
301
302 if (part >= 1)
303 ret = part_get_info(desc, part, &fs_partition);
304 else
305 ret = part_get_info_whole_disk(desc, &fs_partition);
306 if (ret)
307 return ret;
308 fs_dev_desc = desc;
309
310 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
2ded0d47 311 if (!info->probe(fs_dev_desc, &fs_partition)) {
436e2b73 312 fs_type = info->fstype;
045fa1e1
SW
313 return 0;
314 }
315 }
316
045fa1e1
SW
317 return -1;
318}
319
320static void fs_close(void)
321{
c6f548d2 322 struct fstype_info *info = fs_get_info(fs_type);
045fa1e1 323
c6f548d2 324 info->close();
e6d52415 325
045fa1e1
SW
326 fs_type = FS_TYPE_ANY;
327}
328
59e890ef
CG
329int fs_uuid(char *uuid_str)
330{
331 struct fstype_info *info = fs_get_info(fs_type);
332
333 return info->uuid(uuid_str);
334}
335
045fa1e1
SW
336int fs_ls(const char *dirname)
337{
338 int ret;
339
c6f548d2
SG
340 struct fstype_info *info = fs_get_info(fs_type);
341
342 ret = info->ls(dirname);
045fa1e1 343
e6d52415 344 fs_type = FS_TYPE_ANY;
045fa1e1
SW
345 fs_close();
346
347 return ret;
348}
349
6152916a
SW
350int fs_exists(const char *filename)
351{
352 int ret;
353
354 struct fstype_info *info = fs_get_info(fs_type);
355
356 ret = info->exists(filename);
357
358 fs_close();
359
360 return ret;
361}
362
d455d878 363int fs_size(const char *filename, loff_t *size)
cf659819
SW
364{
365 int ret;
366
367 struct fstype_info *info = fs_get_info(fs_type);
368
d455d878 369 ret = info->size(filename, size);
cf659819
SW
370
371 fs_close();
372
373 return ret;
374}
375
d455d878
SR
376int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
377 loff_t *actread)
045fa1e1 378{
c6f548d2 379 struct fstype_info *info = fs_get_info(fs_type);
117e0507 380 void *buf;
045fa1e1
SW
381 int ret;
382
117e0507
SG
383 /*
384 * We don't actually know how many bytes are being read, since len==0
385 * means read the whole file.
386 */
387 buf = map_sysmem(addr, len);
d455d878 388 ret = info->read(filename, buf, offset, len, actread);
117e0507 389 unmap_sysmem(buf);
045fa1e1 390
c6f548d2 391 /* If we requested a specific number of bytes, check we got it */
7a3e70cf
MK
392 if (ret == 0 && len && *actread != len)
393 printf("** %s shorter than offset + len **\n", filename);
045fa1e1
SW
394 fs_close();
395
396 return ret;
397}
398
d455d878
SR
399int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
400 loff_t *actwrite)
a8f6ab52
SG
401{
402 struct fstype_info *info = fs_get_info(fs_type);
403 void *buf;
404 int ret;
405
a8f6ab52 406 buf = map_sysmem(addr, len);
d455d878 407 ret = info->write(filename, buf, offset, len, actwrite);
a8f6ab52
SG
408 unmap_sysmem(buf);
409
d455d878 410 if (ret < 0 && len != *actwrite) {
a8f6ab52
SG
411 printf("** Unable to write file %s **\n", filename);
412 ret = -1;
413 }
414 fs_close();
415
416 return ret;
417}
418
4bbcc965
RC
419struct fs_dir_stream *fs_opendir(const char *filename)
420{
421 struct fstype_info *info = fs_get_info(fs_type);
422 struct fs_dir_stream *dirs = NULL;
423 int ret;
424
425 ret = info->opendir(filename, &dirs);
426 fs_close();
427 if (ret) {
428 errno = -ret;
429 return NULL;
430 }
431
432 dirs->desc = fs_dev_desc;
433 dirs->part = fs_dev_part;
434
435 return dirs;
436}
437
438struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
439{
440 struct fstype_info *info;
441 struct fs_dirent *dirent;
442 int ret;
443
444 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
445 info = fs_get_info(fs_type);
446
447 ret = info->readdir(dirs, &dirent);
448 fs_close();
449 if (ret) {
450 errno = -ret;
451 return NULL;
452 }
453
454 return dirent;
455}
456
457void fs_closedir(struct fs_dir_stream *dirs)
458{
459 struct fstype_info *info;
460
461 if (!dirs)
462 return;
463
464 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
465 info = fs_get_info(fs_type);
466
467 info->closedir(dirs);
468 fs_close();
469}
470
471
cf659819
SW
472int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
473 int fstype)
474{
d455d878 475 loff_t size;
cf659819
SW
476
477 if (argc != 4)
478 return CMD_RET_USAGE;
479
480 if (fs_set_blk_dev(argv[1], argv[2], fstype))
481 return 1;
482
d455d878 483 if (fs_size(argv[3], &size) < 0)
cf659819
SW
484 return CMD_RET_FAILURE;
485
018f5303 486 env_set_hex("filesize", size);
cf659819
SW
487
488 return 0;
489}
490
f9b55e22 491int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
b770e88a 492 int fstype)
045fa1e1
SW
493{
494 unsigned long addr;
495 const char *addr_str;
496 const char *filename;
d455d878
SR
497 loff_t bytes;
498 loff_t pos;
499 loff_t len_read;
500 int ret;
da1fd96c 501 unsigned long time;
949bbd7c 502 char *ep;
045fa1e1 503
e9b0f99e
SW
504 if (argc < 2)
505 return CMD_RET_USAGE;
506 if (argc > 7)
045fa1e1
SW
507 return CMD_RET_USAGE;
508
e9b0f99e 509 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
045fa1e1
SW
510 return 1;
511
512 if (argc >= 4) {
949bbd7c
PM
513 addr = simple_strtoul(argv[3], &ep, 16);
514 if (ep == argv[3] || *ep != '\0')
515 return CMD_RET_USAGE;
045fa1e1 516 } else {
00caae6d 517 addr_str = env_get("loadaddr");
045fa1e1
SW
518 if (addr_str != NULL)
519 addr = simple_strtoul(addr_str, NULL, 16);
520 else
521 addr = CONFIG_SYS_LOAD_ADDR;
522 }
523 if (argc >= 5) {
524 filename = argv[4];
525 } else {
00caae6d 526 filename = env_get("bootfile");
045fa1e1
SW
527 if (!filename) {
528 puts("** No boot file defined **\n");
529 return 1;
530 }
531 }
532 if (argc >= 6)
b770e88a 533 bytes = simple_strtoul(argv[5], NULL, 16);
045fa1e1
SW
534 else
535 bytes = 0;
536 if (argc >= 7)
b770e88a 537 pos = simple_strtoul(argv[6], NULL, 16);
045fa1e1
SW
538 else
539 pos = 0;
540
da1fd96c 541 time = get_timer(0);
d455d878 542 ret = fs_read(filename, addr, pos, bytes, &len_read);
da1fd96c 543 time = get_timer(time);
d455d878 544 if (ret < 0)
045fa1e1
SW
545 return 1;
546
d455d878 547 printf("%llu bytes read in %lu ms", len_read, time);
da1fd96c
AB
548 if (time > 0) {
549 puts(" (");
9e374e7b 550 print_size(div_u64(len_read, time) * 1000, "/s");
da1fd96c
AB
551 puts(")");
552 }
553 puts("\n");
045fa1e1 554
018f5303
SG
555 env_set_hex("fileaddr", addr);
556 env_set_hex("filesize", len_read);
045fa1e1
SW
557
558 return 0;
559}
560
561int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
562 int fstype)
563{
564 if (argc < 2)
565 return CMD_RET_USAGE;
e9b0f99e
SW
566 if (argc > 4)
567 return CMD_RET_USAGE;
045fa1e1
SW
568
569 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
570 return 1;
571
e9b0f99e 572 if (fs_ls(argc >= 4 ? argv[3] : "/"))
045fa1e1
SW
573 return 1;
574
575 return 0;
576}
a8f6ab52 577
6152916a
SW
578int file_exists(const char *dev_type, const char *dev_part, const char *file,
579 int fstype)
580{
581 if (fs_set_blk_dev(dev_type, dev_part, fstype))
582 return 0;
583
584 return fs_exists(file);
585}
586
a8f6ab52 587int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
b770e88a 588 int fstype)
a8f6ab52
SG
589{
590 unsigned long addr;
591 const char *filename;
d455d878
SR
592 loff_t bytes;
593 loff_t pos;
594 loff_t len;
595 int ret;
a8f6ab52
SG
596 unsigned long time;
597
598 if (argc < 6 || argc > 7)
599 return CMD_RET_USAGE;
600
601 if (fs_set_blk_dev(argv[1], argv[2], fstype))
602 return 1;
603
d455d878
SR
604 addr = simple_strtoul(argv[3], NULL, 16);
605 filename = argv[4];
b770e88a 606 bytes = simple_strtoul(argv[5], NULL, 16);
a8f6ab52 607 if (argc >= 7)
b770e88a 608 pos = simple_strtoul(argv[6], NULL, 16);
a8f6ab52
SG
609 else
610 pos = 0;
611
612 time = get_timer(0);
d455d878 613 ret = fs_write(filename, addr, pos, bytes, &len);
a8f6ab52 614 time = get_timer(time);
d455d878 615 if (ret < 0)
a8f6ab52
SG
616 return 1;
617
d455d878 618 printf("%llu bytes written in %lu ms", len, time);
a8f6ab52
SG
619 if (time > 0) {
620 puts(" (");
9e374e7b 621 print_size(div_u64(len, time) * 1000, "/s");
a8f6ab52
SG
622 puts(")");
623 }
624 puts("\n");
625
626 return 0;
627}
59e890ef
CG
628
629int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
630 int fstype)
631{
632 int ret;
633 char uuid[37];
634 memset(uuid, 0, sizeof(uuid));
635
636 if (argc < 3 || argc > 4)
637 return CMD_RET_USAGE;
638
639 if (fs_set_blk_dev(argv[1], argv[2], fstype))
640 return 1;
641
642 ret = fs_uuid(uuid);
643 if (ret)
644 return CMD_RET_FAILURE;
645
646 if (argc == 4)
382bee57 647 env_set(argv[3], uuid);
59e890ef
CG
648 else
649 printf("%s\n", uuid);
650
651 return CMD_RET_SUCCESS;
652}
1a1ad8e0
SS
653
654int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
655{
656 struct fstype_info *info;
657
658 if (argc < 3 || argc > 4)
659 return CMD_RET_USAGE;
660
661 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
662 return 1;
663
664 info = fs_get_info(fs_type);
665
666 if (argc == 4)
382bee57 667 env_set(argv[3], info->name);
1a1ad8e0
SS
668 else
669 printf("%s\n", info->name);
670
671 return CMD_RET_SUCCESS;
672}
673