blob: 17e4bc33f762eb72dc50bb05fdc6fa577c281166 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren045fa1e2012-10-22 06:43:51 +00002/*
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
Stephen Warren045fa1e2012-10-22 06:43:51 +00004 */
5
Simon Glass09140112020-05-10 11:40:03 -06006#include <command.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +00007#include <config.h>
Christian Gmeiner59e890e2014-11-12 14:35:04 +01008#include <errno.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +00009#include <common.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060010#include <env.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060011#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050013#include <mapmem.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000014#include <part.h>
15#include <ext4fs.h>
16#include <fat.h>
17#include <fs.h>
Simon Glass92ccc962012-12-26 09:53:35 +000018#include <sandboxfs.h>
Hans de Goede251cee02015-09-17 18:46:58 -040019#include <ubifs_uboot.h>
Marek BehĂșn0c936ee2017-09-03 17:00:29 +020020#include <btrfs.h>
Simon Glass117e0502012-12-26 09:53:32 +000021#include <asm/io.h>
Tom Rini9e374e72014-11-24 11:50:46 -050022#include <div64.h>
23#include <linux/math64.h>
Mian Yousaf Kaukabee88eac2019-06-18 15:03:44 +020024#include <efi_loader.h>
Joao Marcos Costac5100612020-07-30 15:33:47 +020025#include <squashfs.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000026
Stephen Warrena1b231c2012-10-30 07:50:47 +000027DECLARE_GLOBAL_DATA_PTR;
28
Simon Glass4101f682016-02-29 15:25:34 -070029static struct blk_desc *fs_dev_desc;
Rob Clark4bbcc962017-09-09 13:15:55 -040030static int fs_dev_part;
Simon Glass05289792020-05-10 11:39:57 -060031static struct disk_partition fs_partition;
Stephen Warren045fa1e2012-10-22 06:43:51 +000032static int fs_type = FS_TYPE_ANY;
33
Simon Glass4101f682016-02-29 15:25:34 -070034static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glass05289792020-05-10 11:39:57 -060035 struct disk_partition *fs_partition)
Simon Glass436e2b72012-12-26 09:53:29 +000036{
37 printf("** Unrecognized filesystem type **\n");
38 return -1;
39}
40
Stephen Warren045fa1e2012-10-22 06:43:51 +000041static inline int fs_ls_unsupported(const char *dirname)
42{
Stephen Warren045fa1e2012-10-22 06:43:51 +000043 return -1;
44}
45
Rob Clark89191d62017-09-09 13:15:58 -040046/* generic implementation of ls in terms of opendir/readdir/closedir */
47__maybe_unused
48static int fs_ls_generic(const char *dirname)
49{
50 struct fs_dir_stream *dirs;
51 struct fs_dirent *dent;
52 int nfiles = 0, ndirs = 0;
53
54 dirs = fs_opendir(dirname);
55 if (!dirs)
56 return -errno;
57
58 while ((dent = fs_readdir(dirs))) {
59 if (dent->type == FS_DT_DIR) {
60 printf(" %s/\n", dent->name);
61 ndirs++;
Joao Marcos Costa02c366b2020-07-30 15:33:51 +020062 } else if (dent->type == FS_DT_LNK) {
63 printf(" <SYM> %s\n", dent->name);
64 nfiles++;
Rob Clark89191d62017-09-09 13:15:58 -040065 } else {
66 printf(" %8lld %s\n", dent->size, dent->name);
67 nfiles++;
68 }
69 }
70
71 fs_closedir(dirs);
72
73 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
74
75 return 0;
76}
77
Stephen Warren61529162014-02-03 13:21:00 -070078static inline int fs_exists_unsupported(const char *filename)
79{
80 return 0;
81}
82
Suriyan Ramasamid455d872014-11-17 14:39:38 -080083static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -060084{
85 return -1;
86}
87
Simon Glass117e0502012-12-26 09:53:32 +000088static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080089 loff_t offset, loff_t len,
90 loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +000091{
Stephen Warren045fa1e2012-10-22 06:43:51 +000092 return -1;
93}
94
Simon Glassa8f6ab52013-04-20 08:42:50 +000095static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080096 loff_t offset, loff_t len,
97 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +000098{
99 return -1;
100}
101
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100102static inline int fs_ln_unsupported(const char *filename, const char *target)
103{
104 return -1;
105}
106
Simon Glass436e2b72012-12-26 09:53:29 +0000107static inline void fs_close_unsupported(void)
108{
109}
110
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100111static inline int fs_uuid_unsupported(char *uuid_str)
112{
113 return -1;
114}
115
Rob Clark4bbcc962017-09-09 13:15:55 -0400116static inline int fs_opendir_unsupported(const char *filename,
117 struct fs_dir_stream **dirs)
118{
119 return -EACCES;
120}
121
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900122static inline int fs_unlink_unsupported(const char *filename)
123{
124 return -1;
125}
126
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900127static inline int fs_mkdir_unsupported(const char *dirname)
128{
129 return -1;
130}
131
Simon Glass436e2b72012-12-26 09:53:29 +0000132struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +0000133 int fstype;
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100134 char *name;
Stephen Warren377202b2014-02-03 13:21:01 -0700135 /*
136 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
137 * should be false in most cases. For "virtual" filesystems which
138 * aren't based on a U-Boot block device (e.g. sandbox), this can be
Heinrich Schuchardtca230b02018-08-11 15:52:14 +0200139 * set to true. This should also be true for the dummy entry at the end
Stephen Warren377202b2014-02-03 13:21:01 -0700140 * of fstypes[], since that is essentially a "virtual" (non-existent)
141 * filesystem.
142 */
143 bool null_dev_desc_ok;
Simon Glass4101f682016-02-29 15:25:34 -0700144 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glass05289792020-05-10 11:39:57 -0600145 struct disk_partition *fs_partition);
Simon Glass436e2b72012-12-26 09:53:29 +0000146 int (*ls)(const char *dirname);
Stephen Warren61529162014-02-03 13:21:00 -0700147 int (*exists)(const char *filename);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800148 int (*size)(const char *filename, loff_t *size);
149 int (*read)(const char *filename, void *buf, loff_t offset,
150 loff_t len, loff_t *actread);
151 int (*write)(const char *filename, void *buf, loff_t offset,
152 loff_t len, loff_t *actwrite);
Simon Glass436e2b72012-12-26 09:53:29 +0000153 void (*close)(void);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100154 int (*uuid)(char *uuid_str);
Rob Clark4bbcc962017-09-09 13:15:55 -0400155 /*
156 * Open a directory stream. On success return 0 and directory
157 * stream pointer via 'dirsp'. On error, return -errno. See
158 * fs_opendir().
159 */
160 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
161 /*
162 * Read next entry from directory stream. On success return 0
163 * and directory entry pointer via 'dentp'. On error return
164 * -errno. See fs_readdir().
165 */
166 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
167 /* see fs_closedir() */
168 void (*closedir)(struct fs_dir_stream *dirs);
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900169 int (*unlink)(const char *filename);
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900170 int (*mkdir)(const char *dirname);
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100171 int (*ln)(const char *filename, const char *target);
Simon Glass436e2b72012-12-26 09:53:29 +0000172};
173
174static struct fstype_info fstypes[] = {
175#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +0000176 {
177 .fstype = FS_TYPE_FAT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100178 .name = "fat",
Stephen Warren377202b2014-02-03 13:21:01 -0700179 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000180 .probe = fat_set_blk_dev,
181 .close = fat_close,
Rob Clark89191d62017-09-09 13:15:58 -0400182 .ls = fs_ls_generic,
Stephen Warrenb7b5f312014-02-03 13:21:10 -0700183 .exists = fat_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600184 .size = fat_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000185 .read = fat_read_file,
Tien Fong Cheed8c3ea92019-01-23 14:20:04 +0800186#if CONFIG_IS_ENABLED(FAT_WRITE)
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800187 .write = file_fat_write,
AKASHI Takahirof8240ce2018-09-11 15:59:14 +0900188 .unlink = fat_unlink,
AKASHI Takahiro31a18d52018-09-11 15:59:10 +0900189 .mkdir = fat_mkdir,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800190#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700191 .write = fs_write_unsupported,
AKASHI Takahirof8240ce2018-09-11 15:59:14 +0900192 .unlink = fs_unlink_unsupported,
AKASHI Takahiro31a18d52018-09-11 15:59:10 +0900193 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800194#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100195 .uuid = fs_uuid_unsupported,
Rob Clark89191d62017-09-09 13:15:58 -0400196 .opendir = fat_opendir,
197 .readdir = fat_readdir,
198 .closedir = fat_closedir,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100199 .ln = fs_ln_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000200 },
Simon Glass436e2b72012-12-26 09:53:29 +0000201#endif
Tien Fong Cheecafc4292019-01-23 14:20:06 +0800202
203#if CONFIG_IS_ENABLED(FS_EXT4)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000204 {
205 .fstype = FS_TYPE_EXT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100206 .name = "ext4",
Stephen Warren377202b2014-02-03 13:21:01 -0700207 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000208 .probe = ext4fs_probe,
209 .close = ext4fs_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000210 .ls = ext4fs_ls,
Stephen Warren55af5c92014-02-03 13:21:09 -0700211 .exists = ext4fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600212 .size = ext4fs_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000213 .read = ext4_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800214#ifdef CONFIG_CMD_EXT4_WRITE
215 .write = ext4_write_file,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100216 .ln = ext4fs_create_link,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800217#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700218 .write = fs_write_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100219 .ln = fs_ln_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800220#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100221 .uuid = ext4fs_uuid,
Rob Clark4bbcc962017-09-09 13:15:55 -0400222 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900223 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900224 .mkdir = fs_mkdir_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000225 },
226#endif
Simon Glass92ccc962012-12-26 09:53:35 +0000227#ifdef CONFIG_SANDBOX
228 {
229 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100230 .name = "sandbox",
Stephen Warren377202b2014-02-03 13:21:01 -0700231 .null_dev_desc_ok = true,
Simon Glass92ccc962012-12-26 09:53:35 +0000232 .probe = sandbox_fs_set_blk_dev,
233 .close = sandbox_fs_close,
234 .ls = sandbox_fs_ls,
Stephen Warren0a30aa12014-02-03 13:21:07 -0700235 .exists = sandbox_fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600236 .size = sandbox_fs_size,
Simon Glass92ccc962012-12-26 09:53:35 +0000237 .read = fs_read_sandbox,
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000238 .write = fs_write_sandbox,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100239 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400240 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900241 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900242 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100243 .ln = fs_ln_unsupported,
Simon Glass92ccc962012-12-26 09:53:35 +0000244 },
245#endif
Hans de Goede251cee02015-09-17 18:46:58 -0400246#ifdef CONFIG_CMD_UBIFS
247 {
248 .fstype = FS_TYPE_UBIFS,
249 .name = "ubifs",
250 .null_dev_desc_ok = true,
251 .probe = ubifs_set_blk_dev,
252 .close = ubifs_close,
253 .ls = ubifs_ls,
254 .exists = ubifs_exists,
255 .size = ubifs_size,
256 .read = ubifs_read,
257 .write = fs_write_unsupported,
258 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400259 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900260 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900261 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100262 .ln = fs_ln_unsupported,
Hans de Goede251cee02015-09-17 18:46:58 -0400263 },
264#endif
Marek BehĂșn0c936ee2017-09-03 17:00:29 +0200265#ifdef CONFIG_FS_BTRFS
266 {
267 .fstype = FS_TYPE_BTRFS,
268 .name = "btrfs",
269 .null_dev_desc_ok = false,
270 .probe = btrfs_probe,
271 .close = btrfs_close,
272 .ls = btrfs_ls,
273 .exists = btrfs_exists,
274 .size = btrfs_size,
275 .read = btrfs_read,
276 .write = fs_write_unsupported,
277 .uuid = btrfs_uuid,
Marek BehĂșn38fc6832017-10-06 16:56:07 +0200278 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900279 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900280 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100281 .ln = fs_ln_unsupported,
Marek BehĂșn0c936ee2017-09-03 17:00:29 +0200282 },
283#endif
Joao Marcos Costac5100612020-07-30 15:33:47 +0200284#if IS_ENABLED(CONFIG_FS_SQUASHFS)
285 {
286 .fstype = FS_TYPE_SQUASHFS,
287 .name = "squashfs",
288 .probe = sqfs_probe,
289 .opendir = sqfs_opendir,
290 .readdir = sqfs_readdir,
291 .ls = fs_ls_generic,
292 .read = sqfs_read,
293 .size = sqfs_size,
294 .close = sqfs_close,
295 .closedir = sqfs_closedir,
296 },
297#endif
Simon Glass436e2b72012-12-26 09:53:29 +0000298 {
299 .fstype = FS_TYPE_ANY,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100300 .name = "unsupported",
Stephen Warren377202b2014-02-03 13:21:01 -0700301 .null_dev_desc_ok = true,
Simon Glass436e2b72012-12-26 09:53:29 +0000302 .probe = fs_probe_unsupported,
303 .close = fs_close_unsupported,
304 .ls = fs_ls_unsupported,
Stephen Warren61529162014-02-03 13:21:00 -0700305 .exists = fs_exists_unsupported,
Stephen Warrencf659812014-06-11 12:47:26 -0600306 .size = fs_size_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000307 .read = fs_read_unsupported,
Simon Glassa8f6ab52013-04-20 08:42:50 +0000308 .write = fs_write_unsupported,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100309 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400310 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900311 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900312 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100313 .ln = fs_ln_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000314 },
315};
316
Simon Glassc6f548d2012-12-26 09:53:30 +0000317static struct fstype_info *fs_get_info(int fstype)
318{
319 struct fstype_info *info;
320 int i;
321
322 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
323 if (fstype == info->fstype)
324 return info;
325 }
326
327 /* Return the 'unsupported' sentinel */
328 return info;
329}
330
Alex Kiernan0d488e82018-05-29 15:30:50 +0000331/**
AKASHI Takahirob7cd9562019-10-07 14:59:37 +0900332 * fs_get_type() - Get type of current filesystem
333 *
334 * Return: filesystem type
335 *
336 * Returns filesystem type representing the current filesystem, or
337 * FS_TYPE_ANY for any unrecognised filesystem.
338 */
339int fs_get_type(void)
340{
341 return fs_type;
342}
343
344/**
Alex Kiernan0d488e82018-05-29 15:30:50 +0000345 * fs_get_type_name() - Get type of current filesystem
346 *
347 * Return: Pointer to filesystem name
348 *
349 * Returns a string describing the current filesystem, or the sentinel
350 * "unsupported" for any unrecognised filesystem.
351 */
352const char *fs_get_type_name(void)
353{
354 return fs_get_info(fs_type)->name;
355}
356
Stephen Warren045fa1e2012-10-22 06:43:51 +0000357int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
358{
Simon Glass436e2b72012-12-26 09:53:29 +0000359 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000360 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000361#ifdef CONFIG_NEEDS_MANUAL_RELOC
362 static int relocated;
363
364 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000365 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
366 i++, info++) {
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100367 info->name += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000368 info->probe += gd->reloc_off;
369 info->close += gd->reloc_off;
370 info->ls += gd->reloc_off;
371 info->read += gd->reloc_off;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000372 info->write += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000373 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000374 relocated = 1;
375 }
376#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000377
Simon Glasse35929e2016-02-29 15:25:44 -0700378 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000379 &fs_partition, 1);
380 if (part < 0)
381 return -1;
382
Simon Glass436e2b72012-12-26 09:53:29 +0000383 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
384 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
385 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000386 continue;
387
Stephen Warren377202b2014-02-03 13:21:01 -0700388 if (!fs_dev_desc && !info->null_dev_desc_ok)
389 continue;
390
Simon Glass2ded0d42012-12-26 09:53:31 +0000391 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass436e2b72012-12-26 09:53:29 +0000392 fs_type = info->fstype;
Rob Clark4bbcc962017-09-09 13:15:55 -0400393 fs_dev_part = part;
394 return 0;
395 }
396 }
397
398 return -1;
399}
400
401/* set current blk device w/ blk_desc + partition # */
402int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
403{
404 struct fstype_info *info;
405 int ret, i;
406
407 if (part >= 1)
408 ret = part_get_info(desc, part, &fs_partition);
409 else
410 ret = part_get_info_whole_disk(desc, &fs_partition);
411 if (ret)
412 return ret;
413 fs_dev_desc = desc;
414
415 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
416 if (!info->probe(fs_dev_desc, &fs_partition)) {
417 fs_type = info->fstype;
AKASHI Takahirob0c78d82018-10-17 16:32:02 +0900418 fs_dev_part = part;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000419 return 0;
420 }
421 }
422
Stephen Warren045fa1e2012-10-22 06:43:51 +0000423 return -1;
424}
425
AKASHI Takahiro64f49eb2019-10-07 14:59:35 +0900426void fs_close(void)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000427{
Simon Glassc6f548d2012-12-26 09:53:30 +0000428 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000429
Simon Glassc6f548d2012-12-26 09:53:30 +0000430 info->close();
Simon Glasse6d52412012-12-26 09:53:33 +0000431
Stephen Warren045fa1e2012-10-22 06:43:51 +0000432 fs_type = FS_TYPE_ANY;
433}
434
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100435int fs_uuid(char *uuid_str)
436{
437 struct fstype_info *info = fs_get_info(fs_type);
438
439 return info->uuid(uuid_str);
440}
441
Stephen Warren045fa1e2012-10-22 06:43:51 +0000442int fs_ls(const char *dirname)
443{
444 int ret;
445
Simon Glassc6f548d2012-12-26 09:53:30 +0000446 struct fstype_info *info = fs_get_info(fs_type);
447
448 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000449
450 fs_close();
451
452 return ret;
453}
454
Stephen Warren61529162014-02-03 13:21:00 -0700455int fs_exists(const char *filename)
456{
457 int ret;
458
459 struct fstype_info *info = fs_get_info(fs_type);
460
461 ret = info->exists(filename);
462
463 fs_close();
464
465 return ret;
466}
467
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800468int fs_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -0600469{
470 int ret;
471
472 struct fstype_info *info = fs_get_info(fs_type);
473
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800474 ret = info->size(filename, size);
Stephen Warrencf659812014-06-11 12:47:26 -0600475
476 fs_close();
477
478 return ret;
479}
480
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100481#ifdef CONFIG_LMB
482/* Check if a file may be read to the given address */
483static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
484 loff_t len, struct fstype_info *info)
485{
486 struct lmb lmb;
487 int ret;
488 loff_t size;
489 loff_t read_len;
490
491 /* get the actual size of the file */
492 ret = info->size(filename, &size);
493 if (ret)
494 return ret;
495 if (offset >= size) {
496 /* offset >= EOF, no bytes will be written */
497 return 0;
498 }
499 read_len = size - offset;
500
501 /* limit to 'len' if it is smaller */
502 if (len && len < read_len)
503 read_len = len;
504
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100505 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100506 lmb_dump_all(&lmb);
507
508 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
509 return 0;
510
511 printf("** Reading file would overwrite reserved memory **\n");
512 return -ENOSPC;
513}
514#endif
515
516static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
517 int do_lmb_check, loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000518{
Simon Glassc6f548d2012-12-26 09:53:30 +0000519 struct fstype_info *info = fs_get_info(fs_type);
Simon Glass117e0502012-12-26 09:53:32 +0000520 void *buf;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000521 int ret;
522
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100523#ifdef CONFIG_LMB
524 if (do_lmb_check) {
525 ret = fs_read_lmb_check(filename, addr, offset, len, info);
526 if (ret)
527 return ret;
528 }
529#endif
530
Simon Glass117e0502012-12-26 09:53:32 +0000531 /*
532 * We don't actually know how many bytes are being read, since len==0
533 * means read the whole file.
534 */
535 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800536 ret = info->read(filename, buf, offset, len, actread);
Simon Glass117e0502012-12-26 09:53:32 +0000537 unmap_sysmem(buf);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000538
Simon Glassc6f548d2012-12-26 09:53:30 +0000539 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher7a3e70c2015-08-05 17:16:58 +0200540 if (ret == 0 && len && *actread != len)
Heinrich Schuchardta327bde2018-01-01 21:15:37 +0100541 debug("** %s shorter than offset + len **\n", filename);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000542 fs_close();
543
544 return ret;
545}
546
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100547int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
548 loff_t *actread)
549{
550 return _fs_read(filename, addr, offset, len, 0, actread);
551}
552
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800553int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
554 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000555{
556 struct fstype_info *info = fs_get_info(fs_type);
557 void *buf;
558 int ret;
559
Simon Glassa8f6ab52013-04-20 08:42:50 +0000560 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800561 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000562 unmap_sysmem(buf);
563
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800564 if (ret < 0 && len != *actwrite) {
Simon Glassa8f6ab52013-04-20 08:42:50 +0000565 printf("** Unable to write file %s **\n", filename);
566 ret = -1;
567 }
568 fs_close();
569
570 return ret;
571}
572
Rob Clark4bbcc962017-09-09 13:15:55 -0400573struct fs_dir_stream *fs_opendir(const char *filename)
574{
575 struct fstype_info *info = fs_get_info(fs_type);
576 struct fs_dir_stream *dirs = NULL;
577 int ret;
578
579 ret = info->opendir(filename, &dirs);
580 fs_close();
581 if (ret) {
582 errno = -ret;
583 return NULL;
584 }
585
586 dirs->desc = fs_dev_desc;
587 dirs->part = fs_dev_part;
588
589 return dirs;
590}
591
592struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
593{
594 struct fstype_info *info;
595 struct fs_dirent *dirent;
596 int ret;
597
598 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
599 info = fs_get_info(fs_type);
600
601 ret = info->readdir(dirs, &dirent);
602 fs_close();
603 if (ret) {
604 errno = -ret;
605 return NULL;
606 }
607
608 return dirent;
609}
610
611void fs_closedir(struct fs_dir_stream *dirs)
612{
613 struct fstype_info *info;
614
615 if (!dirs)
616 return;
617
618 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
619 info = fs_get_info(fs_type);
620
621 info->closedir(dirs);
622 fs_close();
623}
624
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900625int fs_unlink(const char *filename)
626{
627 int ret;
628
629 struct fstype_info *info = fs_get_info(fs_type);
630
631 ret = info->unlink(filename);
632
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900633 fs_close();
634
635 return ret;
636}
Rob Clark4bbcc962017-09-09 13:15:55 -0400637
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900638int fs_mkdir(const char *dirname)
639{
640 int ret;
641
642 struct fstype_info *info = fs_get_info(fs_type);
643
644 ret = info->mkdir(dirname);
645
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900646 fs_close();
647
648 return ret;
649}
650
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100651int fs_ln(const char *fname, const char *target)
652{
653 struct fstype_info *info = fs_get_info(fs_type);
654 int ret;
655
656 ret = info->ln(fname, target);
657
658 if (ret < 0) {
659 printf("** Unable to create link %s -> %s **\n", fname, target);
660 ret = -1;
661 }
662 fs_close();
663
664 return ret;
665}
666
Simon Glass09140112020-05-10 11:40:03 -0600667int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
668 int fstype)
Stephen Warrencf659812014-06-11 12:47:26 -0600669{
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800670 loff_t size;
Stephen Warrencf659812014-06-11 12:47:26 -0600671
672 if (argc != 4)
673 return CMD_RET_USAGE;
674
675 if (fs_set_blk_dev(argv[1], argv[2], fstype))
676 return 1;
677
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800678 if (fs_size(argv[3], &size) < 0)
Stephen Warrencf659812014-06-11 12:47:26 -0600679 return CMD_RET_FAILURE;
680
Simon Glass018f5302017-08-03 12:22:10 -0600681 env_set_hex("filesize", size);
Stephen Warrencf659812014-06-11 12:47:26 -0600682
683 return 0;
684}
685
Simon Glass09140112020-05-10 11:40:03 -0600686int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
687 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000688{
689 unsigned long addr;
690 const char *addr_str;
691 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800692 loff_t bytes;
693 loff_t pos;
694 loff_t len_read;
695 int ret;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100696 unsigned long time;
Pavel Machek949bbd72014-07-09 22:42:57 +0200697 char *ep;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000698
Stephen Warrene9b0f992012-10-30 12:04:17 +0000699 if (argc < 2)
700 return CMD_RET_USAGE;
701 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000702 return CMD_RET_USAGE;
703
Stephen Warrene9b0f992012-10-30 12:04:17 +0000704 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000705 return 1;
706
707 if (argc >= 4) {
Pavel Machek949bbd72014-07-09 22:42:57 +0200708 addr = simple_strtoul(argv[3], &ep, 16);
709 if (ep == argv[3] || *ep != '\0')
710 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000711 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600712 addr_str = env_get("loadaddr");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000713 if (addr_str != NULL)
714 addr = simple_strtoul(addr_str, NULL, 16);
715 else
716 addr = CONFIG_SYS_LOAD_ADDR;
717 }
718 if (argc >= 5) {
719 filename = argv[4];
720 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600721 filename = env_get("bootfile");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000722 if (!filename) {
723 puts("** No boot file defined **\n");
724 return 1;
725 }
726 }
727 if (argc >= 6)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200728 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000729 else
730 bytes = 0;
731 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200732 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000733 else
734 pos = 0;
735
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100736 time = get_timer(0);
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100737 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100738 time = get_timer(time);
Heinrich Schuchardt1244f362020-06-29 20:08:49 +0200739 if (ret < 0) {
740 printf("Failed to load '%s'\n", filename);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000741 return 1;
Heinrich Schuchardt1244f362020-06-29 20:08:49 +0200742 }
743
744 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
745 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
746 (argc > 4) ? argv[4] : "");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000747
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800748 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100749 if (time > 0) {
750 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500751 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100752 puts(")");
753 }
754 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000755
Simon Glass018f5302017-08-03 12:22:10 -0600756 env_set_hex("fileaddr", addr);
757 env_set_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000758
759 return 0;
760}
761
Simon Glass09140112020-05-10 11:40:03 -0600762int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
763 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000764{
765 if (argc < 2)
766 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000767 if (argc > 4)
768 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000769
770 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
771 return 1;
772
Stephen Warrene9b0f992012-10-30 12:04:17 +0000773 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000774 return 1;
775
776 return 0;
777}
Simon Glassa8f6ab52013-04-20 08:42:50 +0000778
Stephen Warren61529162014-02-03 13:21:00 -0700779int file_exists(const char *dev_type, const char *dev_part, const char *file,
780 int fstype)
781{
782 if (fs_set_blk_dev(dev_type, dev_part, fstype))
783 return 0;
784
785 return fs_exists(file);
786}
787
Simon Glass09140112020-05-10 11:40:03 -0600788int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
789 int fstype)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000790{
791 unsigned long addr;
792 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800793 loff_t bytes;
794 loff_t pos;
795 loff_t len;
796 int ret;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000797 unsigned long time;
798
799 if (argc < 6 || argc > 7)
800 return CMD_RET_USAGE;
801
802 if (fs_set_blk_dev(argv[1], argv[2], fstype))
803 return 1;
804
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800805 addr = simple_strtoul(argv[3], NULL, 16);
806 filename = argv[4];
Wolfgang Denkb770e882013-10-05 21:07:25 +0200807 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000808 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200809 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000810 else
811 pos = 0;
812
813 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800814 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000815 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800816 if (ret < 0)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000817 return 1;
818
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800819 printf("%llu bytes written in %lu ms", len, time);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000820 if (time > 0) {
821 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500822 print_size(div_u64(len, time) * 1000, "/s");
Simon Glassa8f6ab52013-04-20 08:42:50 +0000823 puts(")");
824 }
825 puts("\n");
826
827 return 0;
828}
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100829
Simon Glass09140112020-05-10 11:40:03 -0600830int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
831 int fstype)
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100832{
833 int ret;
834 char uuid[37];
835 memset(uuid, 0, sizeof(uuid));
836
837 if (argc < 3 || argc > 4)
838 return CMD_RET_USAGE;
839
840 if (fs_set_blk_dev(argv[1], argv[2], fstype))
841 return 1;
842
843 ret = fs_uuid(uuid);
844 if (ret)
845 return CMD_RET_FAILURE;
846
847 if (argc == 4)
Simon Glass382bee52017-08-03 12:22:09 -0600848 env_set(argv[3], uuid);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100849 else
850 printf("%s\n", uuid);
851
852 return CMD_RET_SUCCESS;
853}
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100854
Simon Glass09140112020-05-10 11:40:03 -0600855int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100856{
857 struct fstype_info *info;
858
859 if (argc < 3 || argc > 4)
860 return CMD_RET_USAGE;
861
862 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
863 return 1;
864
865 info = fs_get_info(fs_type);
866
867 if (argc == 4)
Simon Glass382bee52017-08-03 12:22:09 -0600868 env_set(argv[3], info->name);
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100869 else
870 printf("%s\n", info->name);
871
Marek Vasute531c672019-02-06 13:19:29 +0100872 fs_close();
873
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100874 return CMD_RET_SUCCESS;
875}
876
Simon Glass09140112020-05-10 11:40:03 -0600877int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900878 int fstype)
879{
880 if (argc != 4)
881 return CMD_RET_USAGE;
882
883 if (fs_set_blk_dev(argv[1], argv[2], fstype))
884 return 1;
885
886 if (fs_unlink(argv[3]))
887 return 1;
888
889 return 0;
890}
891
Simon Glass09140112020-05-10 11:40:03 -0600892int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900893 int fstype)
894{
895 int ret;
896
897 if (argc != 4)
898 return CMD_RET_USAGE;
899
900 if (fs_set_blk_dev(argv[1], argv[2], fstype))
901 return 1;
902
903 ret = fs_mkdir(argv[3]);
904 if (ret) {
905 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
906 return 1;
907 }
908
909 return 0;
910}
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100911
Simon Glass09140112020-05-10 11:40:03 -0600912int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100913 int fstype)
914{
915 if (argc != 5)
916 return CMD_RET_USAGE;
917
918 if (fs_set_blk_dev(argv[1], argv[2], fstype))
919 return 1;
920
921 if (fs_ln(argv[3], argv[4]))
922 return 1;
923
924 return 0;
925}
Niel Fourie2280fa52020-03-24 16:17:04 +0100926
927int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
928{
929 struct fstype_info *drv = fstypes;
930 const int n_ents = ARRAY_SIZE(fstypes);
931 struct fstype_info *entry;
932 int i = 0;
933
934 puts("Supported filesystems");
935 for (entry = drv; entry != drv + n_ents; entry++) {
936 if (entry->fstype != FS_TYPE_ANY) {
937 printf("%c %s", i ? ',' : ':', entry->name);
938 i++;
939 }
940 }
941 if (!i)
942 puts(": <none>");
943 puts("\n");
944 return CMD_RET_SUCCESS;
945}