blob: 17e4bc33f762eb72dc50bb05fdc6fa577c281166 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warreneefbc3f2012-10-22 06:43:51 +00002/*
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
Stephen Warreneefbc3f2012-10-22 06:43:51 +00004 */
5
Simon Glassed38aef2020-05-10 11:40:03 -06006#include <command.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +00007#include <config.h>
Christian Gmeiner9f9eec32014-11-12 14:35:04 +01008#include <errno.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +00009#include <common.h>
Simon Glass313112a2019-08-01 09:46:46 -060010#include <env.h>
Simon Glass655306c2020-05-10 11:39:58 -060011#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050013#include <mapmem.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000014#include <part.h>
15#include <ext4fs.h>
16#include <fat.h>
17#include <fs.h>
Simon Glass11842872012-12-26 09:53:35 +000018#include <sandboxfs.h>
Hans de Goede690c7962015-09-17 18:46:58 -040019#include <ubifs_uboot.h>
Marek BehĂșn98ec1d12017-09-03 17:00:29 +020020#include <btrfs.h>
Simon Glasscbe5d5d2012-12-26 09:53:32 +000021#include <asm/io.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050022#include <div64.h>
23#include <linux/math64.h>
Mian Yousaf Kaukab961ebe82019-06-18 15:03:44 +020024#include <efi_loader.h>
Joao Marcos Costa29da3742020-07-30 15:33:47 +020025#include <squashfs.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000026
Stephen Warren44161af2012-10-30 07:50:47 +000027DECLARE_GLOBAL_DATA_PTR;
28
Simon Glasse3394752016-02-29 15:25:34 -070029static struct blk_desc *fs_dev_desc;
Rob Clark2b7bfd92017-09-09 13:15:55 -040030static int fs_dev_part;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060031static struct disk_partition fs_partition;
Stephen Warreneefbc3f2012-10-22 06:43:51 +000032static int fs_type = FS_TYPE_ANY;
33
Simon Glasse3394752016-02-29 15:25:34 -070034static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -060035 struct disk_partition *fs_partition)
Simon Glass1aede482012-12-26 09:53:29 +000036{
37 printf("** Unrecognized filesystem type **\n");
38 return -1;
39}
40
Stephen Warreneefbc3f2012-10-22 06:43:51 +000041static inline int fs_ls_unsupported(const char *dirname)
42{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000043 return -1;
44}
45
Rob Clark60d74a92017-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 Costa2a7fb152020-07-30 15:33:51 +020062 } else if (dent->type == FS_DT_LNK) {
63 printf(" <SYM> %s\n", dent->name);
64 nfiles++;
Rob Clark60d74a92017-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 Warrend008fbb2014-02-03 13:21:00 -070078static inline int fs_exists_unsupported(const char *filename)
79{
80 return 0;
81}
82
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080083static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -060084{
85 return -1;
86}
87
Simon Glasscbe5d5d2012-12-26 09:53:32 +000088static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080089 loff_t offset, loff_t len,
90 loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +000091{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000092 return -1;
93}
94
Simon Glasseda14ea2013-04-20 08:42:50 +000095static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080096 loff_t offset, loff_t len,
97 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +000098{
99 return -1;
100}
101
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100102static inline int fs_ln_unsupported(const char *filename, const char *target)
103{
104 return -1;
105}
106
Simon Glass1aede482012-12-26 09:53:29 +0000107static inline void fs_close_unsupported(void)
108{
109}
110
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100111static inline int fs_uuid_unsupported(char *uuid_str)
112{
113 return -1;
114}
115
Rob Clark2b7bfd92017-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 Takahiro1ba42532018-09-11 15:59:13 +0900122static inline int fs_unlink_unsupported(const char *filename)
123{
124 return -1;
125}
126
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900127static inline int fs_mkdir_unsupported(const char *dirname)
128{
129 return -1;
130}
131
Simon Glass1aede482012-12-26 09:53:29 +0000132struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000133 int fstype;
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100134 char *name;
Stephen Warrenec63d4a2014-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 Schuchardt99dc17d2018-08-11 15:52:14 +0200139 * set to true. This should also be true for the dummy entry at the end
Stephen Warrenec63d4a2014-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 Glasse3394752016-02-29 15:25:34 -0700144 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600145 struct disk_partition *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +0000146 int (*ls)(const char *dirname);
Stephen Warrend008fbb2014-02-03 13:21:00 -0700147 int (*exists)(const char *filename);
Suriyan Ramasami96171fb2014-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 Glass1aede482012-12-26 09:53:29 +0000153 void (*close)(void);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100154 int (*uuid)(char *uuid_str);
Rob Clark2b7bfd92017-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 Takahiro1ba42532018-09-11 15:59:13 +0900169 int (*unlink)(const char *filename);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900170 int (*mkdir)(const char *dirname);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100171 int (*ln)(const char *filename, const char *target);
Simon Glass1aede482012-12-26 09:53:29 +0000172};
173
174static struct fstype_info fstypes[] = {
175#ifdef CONFIG_FS_FAT
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000176 {
177 .fstype = FS_TYPE_FAT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100178 .name = "fat",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700179 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000180 .probe = fat_set_blk_dev,
181 .close = fat_close,
Rob Clark60d74a92017-09-09 13:15:58 -0400182 .ls = fs_ls_generic,
Stephen Warren1d2f9a02014-02-03 13:21:10 -0700183 .exists = fat_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600184 .size = fat_size,
Simon Glass19e38582012-12-26 09:53:33 +0000185 .read = fat_read_file,
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800186#if CONFIG_IS_ENABLED(FAT_WRITE)
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800187 .write = file_fat_write,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900188 .unlink = fat_unlink,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900189 .mkdir = fat_mkdir,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800190#else
Stephen Warren9df25802014-02-03 13:20:59 -0700191 .write = fs_write_unsupported,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900192 .unlink = fs_unlink_unsupported,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900193 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800194#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100195 .uuid = fs_uuid_unsupported,
Rob Clark60d74a92017-09-09 13:15:58 -0400196 .opendir = fat_opendir,
197 .readdir = fat_readdir,
198 .closedir = fat_closedir,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100199 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000200 },
Simon Glass1aede482012-12-26 09:53:29 +0000201#endif
Tien Fong Chee64eab042019-01-23 14:20:06 +0800202
203#if CONFIG_IS_ENABLED(FS_EXT4)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000204 {
205 .fstype = FS_TYPE_EXT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100206 .name = "ext4",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700207 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000208 .probe = ext4fs_probe,
209 .close = ext4fs_close,
Simon Glass1aede482012-12-26 09:53:29 +0000210 .ls = ext4fs_ls,
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700211 .exists = ext4fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600212 .size = ext4fs_size,
Simon Glass19e38582012-12-26 09:53:33 +0000213 .read = ext4_read_file,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800214#ifdef CONFIG_CMD_EXT4_WRITE
215 .write = ext4_write_file,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100216 .ln = ext4fs_create_link,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800217#else
Stephen Warren9df25802014-02-03 13:20:59 -0700218 .write = fs_write_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100219 .ln = fs_ln_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800220#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100221 .uuid = ext4fs_uuid,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400222 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900223 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900224 .mkdir = fs_mkdir_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000225 },
226#endif
Simon Glass11842872012-12-26 09:53:35 +0000227#ifdef CONFIG_SANDBOX
228 {
229 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100230 .name = "sandbox",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700231 .null_dev_desc_ok = true,
Simon Glass11842872012-12-26 09:53:35 +0000232 .probe = sandbox_fs_set_blk_dev,
233 .close = sandbox_fs_close,
234 .ls = sandbox_fs_ls,
Stephen Warren97d66f22014-02-03 13:21:07 -0700235 .exists = sandbox_fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600236 .size = sandbox_fs_size,
Simon Glass11842872012-12-26 09:53:35 +0000237 .read = fs_read_sandbox,
Simon Glassea307e82013-04-20 08:42:51 +0000238 .write = fs_write_sandbox,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100239 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400240 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900241 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900242 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100243 .ln = fs_ln_unsupported,
Simon Glass11842872012-12-26 09:53:35 +0000244 },
245#endif
Hans de Goede690c7962015-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 Clark2b7bfd92017-09-09 13:15:55 -0400259 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900260 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900261 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100262 .ln = fs_ln_unsupported,
Hans de Goede690c7962015-09-17 18:46:58 -0400263 },
264#endif
Marek BehĂșn98ec1d12017-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Ășn90762d92017-10-06 16:56:07 +0200278 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900279 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900280 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100281 .ln = fs_ln_unsupported,
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200282 },
283#endif
Joao Marcos Costa29da3742020-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 Glass1aede482012-12-26 09:53:29 +0000298 {
299 .fstype = FS_TYPE_ANY,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100300 .name = "unsupported",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700301 .null_dev_desc_ok = true,
Simon Glass1aede482012-12-26 09:53:29 +0000302 .probe = fs_probe_unsupported,
303 .close = fs_close_unsupported,
304 .ls = fs_ls_unsupported,
Stephen Warrend008fbb2014-02-03 13:21:00 -0700305 .exists = fs_exists_unsupported,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600306 .size = fs_size_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000307 .read = fs_read_unsupported,
Simon Glasseda14ea2013-04-20 08:42:50 +0000308 .write = fs_write_unsupported,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100309 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400310 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900311 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900312 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100313 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000314 },
315};
316
Simon Glasse6aad852012-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 Kiernan3a6163a2018-05-29 15:30:50 +0000331/**
AKASHI Takahiro80ac7342019-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 Kiernan3a6163a2018-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 Warreneefbc3f2012-10-22 06:43:51 +0000357int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
358{
Simon Glass1aede482012-12-26 09:53:29 +0000359 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000360 int part, i;
Stephen Warren44161af2012-10-30 07:50:47 +0000361#ifdef CONFIG_NEEDS_MANUAL_RELOC
362 static int relocated;
363
364 if (!relocated) {
Simon Glass1aede482012-12-26 09:53:29 +0000365 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
366 i++, info++) {
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100367 info->name += gd->reloc_off;
Simon Glass1aede482012-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 Glasseda14ea2013-04-20 08:42:50 +0000372 info->write += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000373 }
Stephen Warren44161af2012-10-30 07:50:47 +0000374 relocated = 1;
375 }
376#endif
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000377
Simon Glasse76ee972016-02-29 15:25:44 -0700378 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000379 &fs_partition, 1);
380 if (part < 0)
381 return -1;
382
Simon Glass1aede482012-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 Warreneefbc3f2012-10-22 06:43:51 +0000386 continue;
387
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700388 if (!fs_dev_desc && !info->null_dev_desc_ok)
389 continue;
390
Simon Glass6a46e2f2012-12-26 09:53:31 +0000391 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000392 fs_type = info->fstype;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400393 fs_dev_part = part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000394 return 0;
395 }
396 }
397
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000398 return -1;
399}
400
Rob Clark2b7bfd92017-09-09 13:15:55 -0400401/* 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 Takahiro43efaa02018-10-17 16:32:02 +0900418 fs_dev_part = part;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400419 return 0;
420 }
421 }
422
423 return -1;
424}
425
AKASHI Takahiroca8e3772019-10-07 14:59:35 +0900426void fs_close(void)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000427{
Simon Glasse6aad852012-12-26 09:53:30 +0000428 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000429
Simon Glasse6aad852012-12-26 09:53:30 +0000430 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000431
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000432 fs_type = FS_TYPE_ANY;
433}
434
Christian Gmeiner9f9eec32014-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 Warreneefbc3f2012-10-22 06:43:51 +0000442int fs_ls(const char *dirname)
443{
444 int ret;
445
Simon Glasse6aad852012-12-26 09:53:30 +0000446 struct fstype_info *info = fs_get_info(fs_type);
447
448 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000449
450 fs_close();
451
452 return ret;
453}
454
Stephen Warrend008fbb2014-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 Ramasami96171fb2014-11-17 14:39:38 -0800468int fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600469{
470 int ret;
471
472 struct fstype_info *info = fs_get_info(fs_type);
473
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800474 ret = info->size(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600475
476 fs_close();
477
478 return ret;
479}
480
Simon Goldschmidt5b2c6872019-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 Goldschmidt8890e7d2019-01-26 22:13:04 +0100505 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
Simon Goldschmidt5b2c6872019-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 Warreneefbc3f2012-10-22 06:43:51 +0000518{
Simon Glasse6aad852012-12-26 09:53:30 +0000519 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000520 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000521 int ret;
522
Simon Goldschmidt5b2c6872019-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 Glasscbe5d5d2012-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 Ramasami96171fb2014-11-17 14:39:38 -0800536 ret = info->read(filename, buf, offset, len, actread);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000537 unmap_sysmem(buf);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000538
Simon Glasse6aad852012-12-26 09:53:30 +0000539 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher56d9cdf2015-08-05 17:16:58 +0200540 if (ret == 0 && len && *actread != len)
Heinrich Schuchardtf02c5c52018-01-01 21:15:37 +0100541 debug("** %s shorter than offset + len **\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000542 fs_close();
543
544 return ret;
545}
546
Simon Goldschmidt5b2c6872019-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 Ramasami96171fb2014-11-17 14:39:38 -0800553int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
554 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000555{
556 struct fstype_info *info = fs_get_info(fs_type);
557 void *buf;
558 int ret;
559
Simon Glasseda14ea2013-04-20 08:42:50 +0000560 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800561 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glasseda14ea2013-04-20 08:42:50 +0000562 unmap_sysmem(buf);
563
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800564 if (ret < 0 && len != *actwrite) {
Simon Glasseda14ea2013-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 Clark2b7bfd92017-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 Takahiro1ba42532018-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 Takahiro1ba42532018-09-11 15:59:13 +0900633 fs_close();
634
635 return ret;
636}
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900637
638int 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 Takahiroadc8c9f2018-09-11 15:59:08 +0900646 fs_close();
647
648 return ret;
649}
Rob Clark2b7bfd92017-09-09 13:15:55 -0400650
Jean-Jacques Hiblotbc237482019-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 Glassed38aef2020-05-10 11:40:03 -0600667int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
668 int fstype)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600669{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800670 loff_t size;
Stephen Warren3eb58f52014-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 Ramasami96171fb2014-11-17 14:39:38 -0800678 if (fs_size(argv[3], &size) < 0)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600679 return CMD_RET_FAILURE;
680
Simon Glass4d949a22017-08-03 12:22:10 -0600681 env_set_hex("filesize", size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600682
683 return 0;
684}
685
Simon Glassed38aef2020-05-10 11:40:03 -0600686int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
687 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000688{
689 unsigned long addr;
690 const char *addr_str;
691 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800692 loff_t bytes;
693 loff_t pos;
694 loff_t len_read;
695 int ret;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100696 unsigned long time;
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200697 char *ep;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000698
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000699 if (argc < 2)
700 return CMD_RET_USAGE;
701 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000702 return CMD_RET_USAGE;
703
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000704 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000705 return 1;
706
707 if (argc >= 4) {
Pavel Machek4c1a9c52014-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 Warreneefbc3f2012-10-22 06:43:51 +0000711 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600712 addr_str = env_get("loadaddr");
Stephen Warreneefbc3f2012-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 Glass64b723f2017-08-03 12:22:12 -0600721 filename = env_get("bootfile");
Stephen Warreneefbc3f2012-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 Denka10c7a52013-10-05 21:07:25 +0200728 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000729 else
730 bytes = 0;
731 if (argc >= 7)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200732 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000733 else
734 pos = 0;
735
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100736 time = get_timer(0);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100737 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100738 time = get_timer(time);
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200739 if (ret < 0) {
740 printf("Failed to load '%s'\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000741 return 1;
Heinrich Schuchardtca816ce2020-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 Warreneefbc3f2012-10-22 06:43:51 +0000747
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800748 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100749 if (time > 0) {
750 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500751 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100752 puts(")");
753 }
754 puts("\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000755
Simon Glass4d949a22017-08-03 12:22:10 -0600756 env_set_hex("fileaddr", addr);
757 env_set_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000758
759 return 0;
760}
761
Simon Glassed38aef2020-05-10 11:40:03 -0600762int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
763 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000764{
765 if (argc < 2)
766 return CMD_RET_USAGE;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000767 if (argc > 4)
768 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-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 Warrenb2d5cd62012-10-30 12:04:17 +0000773 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000774 return 1;
775
776 return 0;
777}
Simon Glasseda14ea2013-04-20 08:42:50 +0000778
Stephen Warrend008fbb2014-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 Glassed38aef2020-05-10 11:40:03 -0600788int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
789 int fstype)
Simon Glasseda14ea2013-04-20 08:42:50 +0000790{
791 unsigned long addr;
792 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800793 loff_t bytes;
794 loff_t pos;
795 loff_t len;
796 int ret;
Simon Glasseda14ea2013-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 Ramasami96171fb2014-11-17 14:39:38 -0800805 addr = simple_strtoul(argv[3], NULL, 16);
806 filename = argv[4];
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200807 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glasseda14ea2013-04-20 08:42:50 +0000808 if (argc >= 7)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200809 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glasseda14ea2013-04-20 08:42:50 +0000810 else
811 pos = 0;
812
813 time = get_timer(0);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800814 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glasseda14ea2013-04-20 08:42:50 +0000815 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800816 if (ret < 0)
Simon Glasseda14ea2013-04-20 08:42:50 +0000817 return 1;
818
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800819 printf("%llu bytes written in %lu ms", len, time);
Simon Glasseda14ea2013-04-20 08:42:50 +0000820 if (time > 0) {
821 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500822 print_size(div_u64(len, time) * 1000, "/s");
Simon Glasseda14ea2013-04-20 08:42:50 +0000823 puts(")");
824 }
825 puts("\n");
826
827 return 0;
828}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100829
Simon Glassed38aef2020-05-10 11:40:03 -0600830int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
831 int fstype)
Christian Gmeiner9f9eec32014-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 Glass6a38e412017-08-03 12:22:09 -0600848 env_set(argv[3], uuid);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100849 else
850 printf("%s\n", uuid);
851
852 return CMD_RET_SUCCESS;
853}
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100854
Simon Glassed38aef2020-05-10 11:40:03 -0600855int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Sjoerd Simons7d9faf62015-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 Glass6a38e412017-08-03 12:22:09 -0600868 env_set(argv[3], info->name);
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100869 else
870 printf("%s\n", info->name);
871
Marek Vasut08d1c422019-02-06 13:19:29 +0100872 fs_close();
873
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100874 return CMD_RET_SUCCESS;
875}
876
Simon Glassed38aef2020-05-10 11:40:03 -0600877int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiro1ba42532018-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 Glassed38aef2020-05-10 11:40:03 -0600892int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroadc8c9f2018-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 Hiblotbc237482019-02-13 12:15:26 +0100911
Simon Glassed38aef2020-05-10 11:40:03 -0600912int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotbc237482019-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 Fourie9252d702020-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}