blob: c67a1c7876a64f00ab5e837035e09ba9bc34081f [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
Heinrich Schuchardtb4559782020-08-25 17:49:16 +00006#define LOG_CATEGORY LOGC_CORE
7
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +00009#include <config.h>
Christian Gmeiner9f9eec32014-11-12 14:35:04 +010010#include <errno.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000011#include <common.h>
Simon Glass313112a2019-08-01 09:46:46 -060012#include <env.h>
Simon Glass655306c2020-05-10 11:39:58 -060013#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050015#include <mapmem.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000016#include <part.h>
17#include <ext4fs.h>
18#include <fat.h>
19#include <fs.h>
Simon Glass11842872012-12-26 09:53:35 +000020#include <sandboxfs.h>
Sean Andersonc678b322022-03-22 16:59:20 -040021#include <semihostingfs.h>
Hans de Goede690c7962015-09-17 18:46:58 -040022#include <ubifs_uboot.h>
Marek BehĂșn98ec1d12017-09-03 17:00:29 +020023#include <btrfs.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060024#include <asm/global_data.h>
Simon Glasscbe5d5d2012-12-26 09:53:32 +000025#include <asm/io.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050026#include <div64.h>
27#include <linux/math64.h>
Mian Yousaf Kaukab961ebe82019-06-18 15:03:44 +020028#include <efi_loader.h>
Joao Marcos Costa29da3742020-07-30 15:33:47 +020029#include <squashfs.h>
Huang Jianan024fb2f2022-02-26 15:05:47 +080030#include <erofs.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000031
Stephen Warren44161af2012-10-30 07:50:47 +000032DECLARE_GLOBAL_DATA_PTR;
33
Simon Glasse3394752016-02-29 15:25:34 -070034static struct blk_desc *fs_dev_desc;
Rob Clark2b7bfd92017-09-09 13:15:55 -040035static int fs_dev_part;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060036static struct disk_partition fs_partition;
Stephen Warreneefbc3f2012-10-22 06:43:51 +000037static int fs_type = FS_TYPE_ANY;
38
Simon Glasse3394752016-02-29 15:25:34 -070039static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -060040 struct disk_partition *fs_partition)
Simon Glass1aede482012-12-26 09:53:29 +000041{
Heinrich Schuchardt3f006d52021-09-20 14:15:03 +020042 log_debug("Unrecognized filesystem type\n");
Simon Glass1aede482012-12-26 09:53:29 +000043 return -1;
44}
45
Stephen Warreneefbc3f2012-10-22 06:43:51 +000046static inline int fs_ls_unsupported(const char *dirname)
47{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000048 return -1;
49}
50
Rob Clark60d74a92017-09-09 13:15:58 -040051/* generic implementation of ls in terms of opendir/readdir/closedir */
52__maybe_unused
53static int fs_ls_generic(const char *dirname)
54{
55 struct fs_dir_stream *dirs;
56 struct fs_dirent *dent;
57 int nfiles = 0, ndirs = 0;
58
59 dirs = fs_opendir(dirname);
60 if (!dirs)
61 return -errno;
62
63 while ((dent = fs_readdir(dirs))) {
64 if (dent->type == FS_DT_DIR) {
65 printf(" %s/\n", dent->name);
66 ndirs++;
Joao Marcos Costa2a7fb152020-07-30 15:33:51 +020067 } else if (dent->type == FS_DT_LNK) {
68 printf(" <SYM> %s\n", dent->name);
69 nfiles++;
Rob Clark60d74a92017-09-09 13:15:58 -040070 } else {
71 printf(" %8lld %s\n", dent->size, dent->name);
72 nfiles++;
73 }
74 }
75
76 fs_closedir(dirs);
77
78 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
79
80 return 0;
81}
82
Stephen Warrend008fbb2014-02-03 13:21:00 -070083static inline int fs_exists_unsupported(const char *filename)
84{
85 return 0;
86}
87
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080088static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -060089{
90 return -1;
91}
92
Simon Glasscbe5d5d2012-12-26 09:53:32 +000093static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080094 loff_t offset, loff_t len,
95 loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +000096{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000097 return -1;
98}
99
Simon Glasseda14ea2013-04-20 08:42:50 +0000100static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800101 loff_t offset, loff_t len,
102 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000103{
104 return -1;
105}
106
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100107static inline int fs_ln_unsupported(const char *filename, const char *target)
108{
109 return -1;
110}
111
Simon Glass1aede482012-12-26 09:53:29 +0000112static inline void fs_close_unsupported(void)
113{
114}
115
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100116static inline int fs_uuid_unsupported(char *uuid_str)
117{
118 return -1;
119}
120
Rob Clark2b7bfd92017-09-09 13:15:55 -0400121static inline int fs_opendir_unsupported(const char *filename,
122 struct fs_dir_stream **dirs)
123{
124 return -EACCES;
125}
126
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900127static inline int fs_unlink_unsupported(const char *filename)
128{
129 return -1;
130}
131
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900132static inline int fs_mkdir_unsupported(const char *dirname)
133{
134 return -1;
135}
136
Simon Glass1aede482012-12-26 09:53:29 +0000137struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000138 int fstype;
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100139 char *name;
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700140 /*
141 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
142 * should be false in most cases. For "virtual" filesystems which
143 * aren't based on a U-Boot block device (e.g. sandbox), this can be
Heinrich Schuchardt99dc17d2018-08-11 15:52:14 +0200144 * set to true. This should also be true for the dummy entry at the end
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700145 * of fstypes[], since that is essentially a "virtual" (non-existent)
146 * filesystem.
147 */
148 bool null_dev_desc_ok;
Simon Glasse3394752016-02-29 15:25:34 -0700149 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600150 struct disk_partition *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +0000151 int (*ls)(const char *dirname);
Stephen Warrend008fbb2014-02-03 13:21:00 -0700152 int (*exists)(const char *filename);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800153 int (*size)(const char *filename, loff_t *size);
154 int (*read)(const char *filename, void *buf, loff_t offset,
155 loff_t len, loff_t *actread);
156 int (*write)(const char *filename, void *buf, loff_t offset,
157 loff_t len, loff_t *actwrite);
Simon Glass1aede482012-12-26 09:53:29 +0000158 void (*close)(void);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100159 int (*uuid)(char *uuid_str);
Rob Clark2b7bfd92017-09-09 13:15:55 -0400160 /*
161 * Open a directory stream. On success return 0 and directory
162 * stream pointer via 'dirsp'. On error, return -errno. See
163 * fs_opendir().
164 */
165 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
166 /*
167 * Read next entry from directory stream. On success return 0
168 * and directory entry pointer via 'dentp'. On error return
169 * -errno. See fs_readdir().
170 */
171 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
172 /* see fs_closedir() */
173 void (*closedir)(struct fs_dir_stream *dirs);
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900174 int (*unlink)(const char *filename);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900175 int (*mkdir)(const char *dirname);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100176 int (*ln)(const char *filename, const char *target);
Simon Glass1aede482012-12-26 09:53:29 +0000177};
178
179static struct fstype_info fstypes[] = {
Pali RohĂĄrd291f892022-04-05 15:48:32 +0200180#if CONFIG_IS_ENABLED(FS_FAT)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000181 {
182 .fstype = FS_TYPE_FAT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100183 .name = "fat",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700184 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000185 .probe = fat_set_blk_dev,
186 .close = fat_close,
Rob Clark60d74a92017-09-09 13:15:58 -0400187 .ls = fs_ls_generic,
Stephen Warren1d2f9a02014-02-03 13:21:10 -0700188 .exists = fat_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600189 .size = fat_size,
Simon Glass19e38582012-12-26 09:53:33 +0000190 .read = fat_read_file,
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800191#if CONFIG_IS_ENABLED(FAT_WRITE)
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800192 .write = file_fat_write,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900193 .unlink = fat_unlink,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900194 .mkdir = fat_mkdir,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800195#else
Stephen Warren9df25802014-02-03 13:20:59 -0700196 .write = fs_write_unsupported,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900197 .unlink = fs_unlink_unsupported,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900198 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800199#endif
Heinrich Schuchardtfac91832020-12-31 00:38:13 +0100200 .uuid = fat_uuid,
Rob Clark60d74a92017-09-09 13:15:58 -0400201 .opendir = fat_opendir,
202 .readdir = fat_readdir,
203 .closedir = fat_closedir,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100204 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000205 },
Simon Glass1aede482012-12-26 09:53:29 +0000206#endif
Tien Fong Chee64eab042019-01-23 14:20:06 +0800207
208#if CONFIG_IS_ENABLED(FS_EXT4)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000209 {
210 .fstype = FS_TYPE_EXT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100211 .name = "ext4",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700212 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000213 .probe = ext4fs_probe,
214 .close = ext4fs_close,
Simon Glass1aede482012-12-26 09:53:29 +0000215 .ls = ext4fs_ls,
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700216 .exists = ext4fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600217 .size = ext4fs_size,
Simon Glass19e38582012-12-26 09:53:33 +0000218 .read = ext4_read_file,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800219#ifdef CONFIG_CMD_EXT4_WRITE
220 .write = ext4_write_file,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100221 .ln = ext4fs_create_link,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800222#else
Stephen Warren9df25802014-02-03 13:20:59 -0700223 .write = fs_write_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100224 .ln = fs_ln_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800225#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100226 .uuid = ext4fs_uuid,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400227 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900228 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900229 .mkdir = fs_mkdir_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000230 },
231#endif
Simon Glass11842872012-12-26 09:53:35 +0000232#ifdef CONFIG_SANDBOX
233 {
234 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100235 .name = "sandbox",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700236 .null_dev_desc_ok = true,
Simon Glass11842872012-12-26 09:53:35 +0000237 .probe = sandbox_fs_set_blk_dev,
238 .close = sandbox_fs_close,
239 .ls = sandbox_fs_ls,
Stephen Warren97d66f22014-02-03 13:21:07 -0700240 .exists = sandbox_fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600241 .size = sandbox_fs_size,
Simon Glass11842872012-12-26 09:53:35 +0000242 .read = fs_read_sandbox,
Simon Glassea307e82013-04-20 08:42:51 +0000243 .write = fs_write_sandbox,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100244 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400245 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900246 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900247 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100248 .ln = fs_ln_unsupported,
Simon Glass11842872012-12-26 09:53:35 +0000249 },
250#endif
Sean Andersonc678b322022-03-22 16:59:20 -0400251#ifdef CONFIG_SEMIHOSTING
252 {
253 .fstype = FS_TYPE_SEMIHOSTING,
254 .name = "semihosting",
255 .null_dev_desc_ok = true,
256 .probe = smh_fs_set_blk_dev,
257 .close = fs_close_unsupported,
258 .ls = fs_ls_unsupported,
259 .exists = fs_exists_unsupported,
260 .size = smh_fs_size,
261 .read = smh_fs_read,
262 .write = smh_fs_write,
263 .uuid = fs_uuid_unsupported,
264 .opendir = fs_opendir_unsupported,
265 .unlink = fs_unlink_unsupported,
266 .mkdir = fs_mkdir_unsupported,
267 .ln = fs_ln_unsupported,
268 },
269#endif
Pali RohĂĄrd3a3c122022-04-05 15:49:24 +0200270#ifndef CONFIG_SPL_BUILD
Hans de Goede690c7962015-09-17 18:46:58 -0400271#ifdef CONFIG_CMD_UBIFS
272 {
273 .fstype = FS_TYPE_UBIFS,
274 .name = "ubifs",
275 .null_dev_desc_ok = true,
276 .probe = ubifs_set_blk_dev,
277 .close = ubifs_close,
278 .ls = ubifs_ls,
279 .exists = ubifs_exists,
280 .size = ubifs_size,
281 .read = ubifs_read,
282 .write = fs_write_unsupported,
283 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400284 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900285 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900286 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100287 .ln = fs_ln_unsupported,
Hans de Goede690c7962015-09-17 18:46:58 -0400288 },
289#endif
Pali RohĂĄrd3a3c122022-04-05 15:49:24 +0200290#endif
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200291#ifdef CONFIG_FS_BTRFS
292 {
293 .fstype = FS_TYPE_BTRFS,
294 .name = "btrfs",
295 .null_dev_desc_ok = false,
296 .probe = btrfs_probe,
297 .close = btrfs_close,
298 .ls = btrfs_ls,
299 .exists = btrfs_exists,
300 .size = btrfs_size,
301 .read = btrfs_read,
302 .write = fs_write_unsupported,
303 .uuid = btrfs_uuid,
Marek BehĂșn90762d92017-10-06 16:56:07 +0200304 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900305 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900306 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100307 .ln = fs_ln_unsupported,
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200308 },
309#endif
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200310#if IS_ENABLED(CONFIG_FS_SQUASHFS)
311 {
312 .fstype = FS_TYPE_SQUASHFS,
313 .name = "squashfs",
Richard Genoud654b2482020-11-03 12:10:59 +0100314 .null_dev_desc_ok = false,
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200315 .probe = sqfs_probe,
316 .opendir = sqfs_opendir,
317 .readdir = sqfs_readdir,
318 .ls = fs_ls_generic,
319 .read = sqfs_read,
320 .size = sqfs_size,
321 .close = sqfs_close,
322 .closedir = sqfs_closedir,
Richard Genoudcfca20c2020-11-03 12:11:26 +0100323 .exists = sqfs_exists,
Richard Genoud654b2482020-11-03 12:10:59 +0100324 .uuid = fs_uuid_unsupported,
325 .write = fs_write_unsupported,
326 .ln = fs_ln_unsupported,
327 .unlink = fs_unlink_unsupported,
328 .mkdir = fs_mkdir_unsupported,
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200329 },
330#endif
Huang Jianan024fb2f2022-02-26 15:05:47 +0800331#if IS_ENABLED(CONFIG_FS_EROFS)
332 {
333 .fstype = FS_TYPE_EROFS,
334 .name = "erofs",
335 .null_dev_desc_ok = false,
336 .probe = erofs_probe,
337 .opendir = erofs_opendir,
338 .readdir = erofs_readdir,
339 .ls = fs_ls_generic,
340 .read = erofs_read,
341 .size = erofs_size,
342 .close = erofs_close,
343 .closedir = erofs_closedir,
344 .exists = erofs_exists,
345 .uuid = fs_uuid_unsupported,
346 .write = fs_write_unsupported,
347 .ln = fs_ln_unsupported,
348 .unlink = fs_unlink_unsupported,
349 .mkdir = fs_mkdir_unsupported,
350 },
351#endif
Simon Glass1aede482012-12-26 09:53:29 +0000352 {
353 .fstype = FS_TYPE_ANY,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100354 .name = "unsupported",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700355 .null_dev_desc_ok = true,
Simon Glass1aede482012-12-26 09:53:29 +0000356 .probe = fs_probe_unsupported,
357 .close = fs_close_unsupported,
358 .ls = fs_ls_unsupported,
Stephen Warrend008fbb2014-02-03 13:21:00 -0700359 .exists = fs_exists_unsupported,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600360 .size = fs_size_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000361 .read = fs_read_unsupported,
Simon Glasseda14ea2013-04-20 08:42:50 +0000362 .write = fs_write_unsupported,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100363 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400364 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900365 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900366 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100367 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000368 },
369};
370
Simon Glasse6aad852012-12-26 09:53:30 +0000371static struct fstype_info *fs_get_info(int fstype)
372{
373 struct fstype_info *info;
374 int i;
375
376 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
377 if (fstype == info->fstype)
378 return info;
379 }
380
381 /* Return the 'unsupported' sentinel */
382 return info;
383}
384
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000385/**
AKASHI Takahiro80ac7342019-10-07 14:59:37 +0900386 * fs_get_type() - Get type of current filesystem
387 *
388 * Return: filesystem type
389 *
390 * Returns filesystem type representing the current filesystem, or
391 * FS_TYPE_ANY for any unrecognised filesystem.
392 */
393int fs_get_type(void)
394{
395 return fs_type;
396}
397
398/**
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000399 * fs_get_type_name() - Get type of current filesystem
400 *
401 * Return: Pointer to filesystem name
402 *
403 * Returns a string describing the current filesystem, or the sentinel
404 * "unsupported" for any unrecognised filesystem.
405 */
406const char *fs_get_type_name(void)
407{
408 return fs_get_info(fs_type)->name;
409}
410
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000411int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
412{
Simon Glass1aede482012-12-26 09:53:29 +0000413 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000414 int part, i;
Stephen Warren44161af2012-10-30 07:50:47 +0000415#ifdef CONFIG_NEEDS_MANUAL_RELOC
416 static int relocated;
417
418 if (!relocated) {
Simon Glass1aede482012-12-26 09:53:29 +0000419 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
420 i++, info++) {
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100421 info->name += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000422 info->probe += gd->reloc_off;
423 info->close += gd->reloc_off;
424 info->ls += gd->reloc_off;
425 info->read += gd->reloc_off;
Simon Glasseda14ea2013-04-20 08:42:50 +0000426 info->write += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000427 }
Stephen Warren44161af2012-10-30 07:50:47 +0000428 relocated = 1;
429 }
430#endif
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000431
Sean Andersonf476b4a2021-04-12 18:53:05 -0400432 part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
433 &fs_partition, 1);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000434 if (part < 0)
435 return -1;
436
Simon Glass1aede482012-12-26 09:53:29 +0000437 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
438 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
439 fstype != info->fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000440 continue;
441
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700442 if (!fs_dev_desc && !info->null_dev_desc_ok)
443 continue;
444
Simon Glass6a46e2f2012-12-26 09:53:31 +0000445 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000446 fs_type = info->fstype;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400447 fs_dev_part = part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000448 return 0;
449 }
450 }
451
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000452 return -1;
453}
454
Rob Clark2b7bfd92017-09-09 13:15:55 -0400455/* set current blk device w/ blk_desc + partition # */
456int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
457{
458 struct fstype_info *info;
459 int ret, i;
460
461 if (part >= 1)
462 ret = part_get_info(desc, part, &fs_partition);
463 else
464 ret = part_get_info_whole_disk(desc, &fs_partition);
465 if (ret)
466 return ret;
467 fs_dev_desc = desc;
468
469 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
470 if (!info->probe(fs_dev_desc, &fs_partition)) {
471 fs_type = info->fstype;
AKASHI Takahiro43efaa02018-10-17 16:32:02 +0900472 fs_dev_part = part;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400473 return 0;
474 }
475 }
476
477 return -1;
478}
479
AKASHI Takahiroca8e3772019-10-07 14:59:35 +0900480void fs_close(void)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000481{
Simon Glasse6aad852012-12-26 09:53:30 +0000482 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000483
Simon Glasse6aad852012-12-26 09:53:30 +0000484 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000485
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000486 fs_type = FS_TYPE_ANY;
487}
488
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100489int fs_uuid(char *uuid_str)
490{
491 struct fstype_info *info = fs_get_info(fs_type);
492
493 return info->uuid(uuid_str);
494}
495
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000496int fs_ls(const char *dirname)
497{
498 int ret;
499
Simon Glasse6aad852012-12-26 09:53:30 +0000500 struct fstype_info *info = fs_get_info(fs_type);
501
502 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000503
504 fs_close();
505
506 return ret;
507}
508
Stephen Warrend008fbb2014-02-03 13:21:00 -0700509int fs_exists(const char *filename)
510{
511 int ret;
512
513 struct fstype_info *info = fs_get_info(fs_type);
514
515 ret = info->exists(filename);
516
517 fs_close();
518
519 return ret;
520}
521
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800522int fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600523{
524 int ret;
525
526 struct fstype_info *info = fs_get_info(fs_type);
527
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800528 ret = info->size(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600529
530 fs_close();
531
532 return ret;
533}
534
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100535#ifdef CONFIG_LMB
536/* Check if a file may be read to the given address */
537static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
538 loff_t len, struct fstype_info *info)
539{
540 struct lmb lmb;
541 int ret;
542 loff_t size;
543 loff_t read_len;
544
545 /* get the actual size of the file */
546 ret = info->size(filename, &size);
547 if (ret)
548 return ret;
549 if (offset >= size) {
550 /* offset >= EOF, no bytes will be written */
551 return 0;
552 }
553 read_len = size - offset;
554
555 /* limit to 'len' if it is smaller */
556 if (len && len < read_len)
557 read_len = len;
558
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100559 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100560 lmb_dump_all(&lmb);
561
562 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
563 return 0;
564
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000565 log_err("** Reading file would overwrite reserved memory **\n");
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100566 return -ENOSPC;
567}
568#endif
569
570static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
571 int do_lmb_check, loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000572{
Simon Glasse6aad852012-12-26 09:53:30 +0000573 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000574 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000575 int ret;
576
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100577#ifdef CONFIG_LMB
578 if (do_lmb_check) {
579 ret = fs_read_lmb_check(filename, addr, offset, len, info);
580 if (ret)
581 return ret;
582 }
583#endif
584
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000585 /*
586 * We don't actually know how many bytes are being read, since len==0
587 * means read the whole file.
588 */
589 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800590 ret = info->read(filename, buf, offset, len, actread);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000591 unmap_sysmem(buf);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000592
Simon Glasse6aad852012-12-26 09:53:30 +0000593 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher56d9cdf2015-08-05 17:16:58 +0200594 if (ret == 0 && len && *actread != len)
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000595 log_debug("** %s shorter than offset + len **\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000596 fs_close();
597
598 return ret;
599}
600
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100601int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
602 loff_t *actread)
603{
604 return _fs_read(filename, addr, offset, len, 0, actread);
605}
606
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800607int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
608 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000609{
610 struct fstype_info *info = fs_get_info(fs_type);
611 void *buf;
612 int ret;
613
Simon Glasseda14ea2013-04-20 08:42:50 +0000614 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800615 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glasseda14ea2013-04-20 08:42:50 +0000616 unmap_sysmem(buf);
617
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800618 if (ret < 0 && len != *actwrite) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000619 log_err("** Unable to write file %s **\n", filename);
Simon Glasseda14ea2013-04-20 08:42:50 +0000620 ret = -1;
621 }
622 fs_close();
623
624 return ret;
625}
626
Rob Clark2b7bfd92017-09-09 13:15:55 -0400627struct fs_dir_stream *fs_opendir(const char *filename)
628{
629 struct fstype_info *info = fs_get_info(fs_type);
630 struct fs_dir_stream *dirs = NULL;
631 int ret;
632
633 ret = info->opendir(filename, &dirs);
634 fs_close();
635 if (ret) {
636 errno = -ret;
637 return NULL;
638 }
639
640 dirs->desc = fs_dev_desc;
641 dirs->part = fs_dev_part;
642
643 return dirs;
644}
645
646struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
647{
648 struct fstype_info *info;
649 struct fs_dirent *dirent;
650 int ret;
651
652 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
653 info = fs_get_info(fs_type);
654
655 ret = info->readdir(dirs, &dirent);
656 fs_close();
657 if (ret) {
658 errno = -ret;
659 return NULL;
660 }
661
662 return dirent;
663}
664
665void fs_closedir(struct fs_dir_stream *dirs)
666{
667 struct fstype_info *info;
668
669 if (!dirs)
670 return;
671
672 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
673 info = fs_get_info(fs_type);
674
675 info->closedir(dirs);
676 fs_close();
677}
678
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900679int fs_unlink(const char *filename)
680{
681 int ret;
682
683 struct fstype_info *info = fs_get_info(fs_type);
684
685 ret = info->unlink(filename);
686
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900687 fs_close();
688
689 return ret;
690}
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900691
692int fs_mkdir(const char *dirname)
693{
694 int ret;
695
696 struct fstype_info *info = fs_get_info(fs_type);
697
698 ret = info->mkdir(dirname);
699
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900700 fs_close();
701
702 return ret;
703}
Rob Clark2b7bfd92017-09-09 13:15:55 -0400704
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100705int fs_ln(const char *fname, const char *target)
706{
707 struct fstype_info *info = fs_get_info(fs_type);
708 int ret;
709
710 ret = info->ln(fname, target);
711
712 if (ret < 0) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000713 log_err("** Unable to create link %s -> %s **\n", fname, target);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100714 ret = -1;
715 }
716 fs_close();
717
718 return ret;
719}
720
Simon Glassed38aef2020-05-10 11:40:03 -0600721int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
722 int fstype)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600723{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800724 loff_t size;
Stephen Warren3eb58f52014-06-11 12:47:26 -0600725
726 if (argc != 4)
727 return CMD_RET_USAGE;
728
729 if (fs_set_blk_dev(argv[1], argv[2], fstype))
730 return 1;
731
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800732 if (fs_size(argv[3], &size) < 0)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600733 return CMD_RET_FAILURE;
734
Simon Glass4d949a22017-08-03 12:22:10 -0600735 env_set_hex("filesize", size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600736
737 return 0;
738}
739
Simon Glassed38aef2020-05-10 11:40:03 -0600740int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
741 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000742{
743 unsigned long addr;
744 const char *addr_str;
745 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800746 loff_t bytes;
747 loff_t pos;
748 loff_t len_read;
749 int ret;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100750 unsigned long time;
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200751 char *ep;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000752
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000753 if (argc < 2)
754 return CMD_RET_USAGE;
755 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000756 return CMD_RET_USAGE;
757
Heinrich Schuchardtc556a452021-01-27 21:26:43 +0100758 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) {
759 log_err("Can't set block device\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000760 return 1;
Heinrich Schuchardtc556a452021-01-27 21:26:43 +0100761 }
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000762
763 if (argc >= 4) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600764 addr = hextoul(argv[3], &ep);
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200765 if (ep == argv[3] || *ep != '\0')
766 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000767 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600768 addr_str = env_get("loadaddr");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000769 if (addr_str != NULL)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600770 addr = hextoul(addr_str, NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000771 else
772 addr = CONFIG_SYS_LOAD_ADDR;
773 }
774 if (argc >= 5) {
775 filename = argv[4];
776 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600777 filename = env_get("bootfile");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000778 if (!filename) {
779 puts("** No boot file defined **\n");
780 return 1;
781 }
782 }
783 if (argc >= 6)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600784 bytes = hextoul(argv[5], NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000785 else
786 bytes = 0;
787 if (argc >= 7)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600788 pos = hextoul(argv[6], NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000789 else
790 pos = 0;
791
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100792 time = get_timer(0);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100793 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100794 time = get_timer(time);
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200795 if (ret < 0) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000796 log_err("Failed to load '%s'\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000797 return 1;
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200798 }
799
800 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
801 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
Heinrich Schuchardt6b821592021-01-12 12:46:24 +0100802 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
803 len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000804
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800805 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100806 if (time > 0) {
807 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500808 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100809 puts(")");
810 }
811 puts("\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000812
Simon Glass4d949a22017-08-03 12:22:10 -0600813 env_set_hex("fileaddr", addr);
814 env_set_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000815
816 return 0;
817}
818
Simon Glassed38aef2020-05-10 11:40:03 -0600819int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
820 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000821{
822 if (argc < 2)
823 return CMD_RET_USAGE;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000824 if (argc > 4)
825 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000826
827 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
828 return 1;
829
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000830 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000831 return 1;
832
833 return 0;
834}
Simon Glasseda14ea2013-04-20 08:42:50 +0000835
Stephen Warrend008fbb2014-02-03 13:21:00 -0700836int file_exists(const char *dev_type, const char *dev_part, const char *file,
837 int fstype)
838{
839 if (fs_set_blk_dev(dev_type, dev_part, fstype))
840 return 0;
841
842 return fs_exists(file);
843}
844
Simon Glassed38aef2020-05-10 11:40:03 -0600845int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
846 int fstype)
Simon Glasseda14ea2013-04-20 08:42:50 +0000847{
848 unsigned long addr;
849 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800850 loff_t bytes;
851 loff_t pos;
852 loff_t len;
853 int ret;
Simon Glasseda14ea2013-04-20 08:42:50 +0000854 unsigned long time;
855
856 if (argc < 6 || argc > 7)
857 return CMD_RET_USAGE;
858
859 if (fs_set_blk_dev(argv[1], argv[2], fstype))
860 return 1;
861
Simon Glass3ff49ec2021-07-24 09:03:29 -0600862 addr = hextoul(argv[3], NULL);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800863 filename = argv[4];
Simon Glass3ff49ec2021-07-24 09:03:29 -0600864 bytes = hextoul(argv[5], NULL);
Simon Glasseda14ea2013-04-20 08:42:50 +0000865 if (argc >= 7)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600866 pos = hextoul(argv[6], NULL);
Simon Glasseda14ea2013-04-20 08:42:50 +0000867 else
868 pos = 0;
869
870 time = get_timer(0);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800871 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glasseda14ea2013-04-20 08:42:50 +0000872 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800873 if (ret < 0)
Simon Glasseda14ea2013-04-20 08:42:50 +0000874 return 1;
875
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800876 printf("%llu bytes written in %lu ms", len, time);
Simon Glasseda14ea2013-04-20 08:42:50 +0000877 if (time > 0) {
878 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500879 print_size(div_u64(len, time) * 1000, "/s");
Simon Glasseda14ea2013-04-20 08:42:50 +0000880 puts(")");
881 }
882 puts("\n");
883
884 return 0;
885}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100886
Simon Glassed38aef2020-05-10 11:40:03 -0600887int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
888 int fstype)
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100889{
890 int ret;
891 char uuid[37];
892 memset(uuid, 0, sizeof(uuid));
893
894 if (argc < 3 || argc > 4)
895 return CMD_RET_USAGE;
896
897 if (fs_set_blk_dev(argv[1], argv[2], fstype))
898 return 1;
899
900 ret = fs_uuid(uuid);
901 if (ret)
902 return CMD_RET_FAILURE;
903
904 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600905 env_set(argv[3], uuid);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100906 else
907 printf("%s\n", uuid);
908
909 return CMD_RET_SUCCESS;
910}
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100911
Simon Glassed38aef2020-05-10 11:40:03 -0600912int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100913{
914 struct fstype_info *info;
915
916 if (argc < 3 || argc > 4)
917 return CMD_RET_USAGE;
918
919 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
920 return 1;
921
922 info = fs_get_info(fs_type);
923
924 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600925 env_set(argv[3], info->name);
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100926 else
927 printf("%s\n", info->name);
928
Marek Vasut08d1c422019-02-06 13:19:29 +0100929 fs_close();
930
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100931 return CMD_RET_SUCCESS;
932}
933
Simon Glassed38aef2020-05-10 11:40:03 -0600934int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900935 int fstype)
936{
937 if (argc != 4)
938 return CMD_RET_USAGE;
939
940 if (fs_set_blk_dev(argv[1], argv[2], fstype))
941 return 1;
942
943 if (fs_unlink(argv[3]))
944 return 1;
945
946 return 0;
947}
948
Simon Glassed38aef2020-05-10 11:40:03 -0600949int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900950 int fstype)
951{
952 int ret;
953
954 if (argc != 4)
955 return CMD_RET_USAGE;
956
957 if (fs_set_blk_dev(argv[1], argv[2], fstype))
958 return 1;
959
960 ret = fs_mkdir(argv[3]);
961 if (ret) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000962 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900963 return 1;
964 }
965
966 return 0;
967}
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100968
Simon Glassed38aef2020-05-10 11:40:03 -0600969int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100970 int fstype)
971{
972 if (argc != 5)
973 return CMD_RET_USAGE;
974
975 if (fs_set_blk_dev(argv[1], argv[2], fstype))
976 return 1;
977
978 if (fs_ln(argv[3], argv[4]))
979 return 1;
980
981 return 0;
982}
Niel Fourie9252d702020-03-24 16:17:04 +0100983
984int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
985{
986 struct fstype_info *drv = fstypes;
987 const int n_ents = ARRAY_SIZE(fstypes);
988 struct fstype_info *entry;
989 int i = 0;
990
991 puts("Supported filesystems");
992 for (entry = drv; entry != drv + n_ents; entry++) {
993 if (entry->fstype != FS_TYPE_ANY) {
994 printf("%c %s", i ? ',' : ':', entry->name);
995 i++;
996 }
997 }
998 if (!i)
999 puts(": <none>");
1000 puts("\n");
1001 return CMD_RET_SUCCESS;
1002}