blob: 6de1a3eb6d5dd46ce54237be8ac4b58297b69cf5 [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 Glass896ed4b2022-04-24 23:31:04 -060039void fs_set_type(int type)
40{
41 fs_type = type;
42}
43
Simon Glasse3394752016-02-29 15:25:34 -070044static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -060045 struct disk_partition *fs_partition)
Simon Glass1aede482012-12-26 09:53:29 +000046{
Heinrich Schuchardt3f006d52021-09-20 14:15:03 +020047 log_debug("Unrecognized filesystem type\n");
Simon Glass1aede482012-12-26 09:53:29 +000048 return -1;
49}
50
Stephen Warreneefbc3f2012-10-22 06:43:51 +000051static inline int fs_ls_unsupported(const char *dirname)
52{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000053 return -1;
54}
55
Rob Clark60d74a92017-09-09 13:15:58 -040056/* generic implementation of ls in terms of opendir/readdir/closedir */
57__maybe_unused
58static int fs_ls_generic(const char *dirname)
59{
60 struct fs_dir_stream *dirs;
61 struct fs_dirent *dent;
62 int nfiles = 0, ndirs = 0;
63
64 dirs = fs_opendir(dirname);
65 if (!dirs)
66 return -errno;
67
68 while ((dent = fs_readdir(dirs))) {
69 if (dent->type == FS_DT_DIR) {
70 printf(" %s/\n", dent->name);
71 ndirs++;
Joao Marcos Costa2a7fb152020-07-30 15:33:51 +020072 } else if (dent->type == FS_DT_LNK) {
73 printf(" <SYM> %s\n", dent->name);
74 nfiles++;
Rob Clark60d74a92017-09-09 13:15:58 -040075 } else {
76 printf(" %8lld %s\n", dent->size, dent->name);
77 nfiles++;
78 }
79 }
80
81 fs_closedir(dirs);
82
83 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
84
85 return 0;
86}
87
Stephen Warrend008fbb2014-02-03 13:21:00 -070088static inline int fs_exists_unsupported(const char *filename)
89{
90 return 0;
91}
92
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080093static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -060094{
95 return -1;
96}
97
Simon Glasscbe5d5d2012-12-26 09:53:32 +000098static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080099 loff_t offset, loff_t len,
100 loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000101{
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000102 return -1;
103}
104
Simon Glasseda14ea2013-04-20 08:42:50 +0000105static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800106 loff_t offset, loff_t len,
107 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000108{
109 return -1;
110}
111
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100112static inline int fs_ln_unsupported(const char *filename, const char *target)
113{
114 return -1;
115}
116
Simon Glass1aede482012-12-26 09:53:29 +0000117static inline void fs_close_unsupported(void)
118{
119}
120
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100121static inline int fs_uuid_unsupported(char *uuid_str)
122{
123 return -1;
124}
125
Rob Clark2b7bfd92017-09-09 13:15:55 -0400126static inline int fs_opendir_unsupported(const char *filename,
127 struct fs_dir_stream **dirs)
128{
129 return -EACCES;
130}
131
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900132static inline int fs_unlink_unsupported(const char *filename)
133{
134 return -1;
135}
136
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900137static inline int fs_mkdir_unsupported(const char *dirname)
138{
139 return -1;
140}
141
Simon Glass1aede482012-12-26 09:53:29 +0000142struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000143 int fstype;
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100144 char *name;
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700145 /*
146 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
147 * should be false in most cases. For "virtual" filesystems which
148 * aren't based on a U-Boot block device (e.g. sandbox), this can be
Heinrich Schuchardt99dc17d2018-08-11 15:52:14 +0200149 * set to true. This should also be true for the dummy entry at the end
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700150 * of fstypes[], since that is essentially a "virtual" (non-existent)
151 * filesystem.
152 */
153 bool null_dev_desc_ok;
Simon Glasse3394752016-02-29 15:25:34 -0700154 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600155 struct disk_partition *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +0000156 int (*ls)(const char *dirname);
Stephen Warrend008fbb2014-02-03 13:21:00 -0700157 int (*exists)(const char *filename);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800158 int (*size)(const char *filename, loff_t *size);
159 int (*read)(const char *filename, void *buf, loff_t offset,
160 loff_t len, loff_t *actread);
161 int (*write)(const char *filename, void *buf, loff_t offset,
162 loff_t len, loff_t *actwrite);
Simon Glass1aede482012-12-26 09:53:29 +0000163 void (*close)(void);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100164 int (*uuid)(char *uuid_str);
Rob Clark2b7bfd92017-09-09 13:15:55 -0400165 /*
166 * Open a directory stream. On success return 0 and directory
167 * stream pointer via 'dirsp'. On error, return -errno. See
168 * fs_opendir().
169 */
170 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
171 /*
172 * Read next entry from directory stream. On success return 0
173 * and directory entry pointer via 'dentp'. On error return
174 * -errno. See fs_readdir().
175 */
176 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
177 /* see fs_closedir() */
178 void (*closedir)(struct fs_dir_stream *dirs);
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900179 int (*unlink)(const char *filename);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900180 int (*mkdir)(const char *dirname);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100181 int (*ln)(const char *filename, const char *target);
Simon Glass1aede482012-12-26 09:53:29 +0000182};
183
184static struct fstype_info fstypes[] = {
Pali RohĂĄrd291f892022-04-05 15:48:32 +0200185#if CONFIG_IS_ENABLED(FS_FAT)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000186 {
187 .fstype = FS_TYPE_FAT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100188 .name = "fat",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700189 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000190 .probe = fat_set_blk_dev,
191 .close = fat_close,
Rob Clark60d74a92017-09-09 13:15:58 -0400192 .ls = fs_ls_generic,
Stephen Warren1d2f9a02014-02-03 13:21:10 -0700193 .exists = fat_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600194 .size = fat_size,
Simon Glass19e38582012-12-26 09:53:33 +0000195 .read = fat_read_file,
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800196#if CONFIG_IS_ENABLED(FAT_WRITE)
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800197 .write = file_fat_write,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900198 .unlink = fat_unlink,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900199 .mkdir = fat_mkdir,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800200#else
Stephen Warren9df25802014-02-03 13:20:59 -0700201 .write = fs_write_unsupported,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900202 .unlink = fs_unlink_unsupported,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900203 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800204#endif
Heinrich Schuchardtfac91832020-12-31 00:38:13 +0100205 .uuid = fat_uuid,
Rob Clark60d74a92017-09-09 13:15:58 -0400206 .opendir = fat_opendir,
207 .readdir = fat_readdir,
208 .closedir = fat_closedir,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100209 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000210 },
Simon Glass1aede482012-12-26 09:53:29 +0000211#endif
Tien Fong Chee64eab042019-01-23 14:20:06 +0800212
213#if CONFIG_IS_ENABLED(FS_EXT4)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000214 {
215 .fstype = FS_TYPE_EXT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100216 .name = "ext4",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700217 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000218 .probe = ext4fs_probe,
219 .close = ext4fs_close,
Simon Glass1aede482012-12-26 09:53:29 +0000220 .ls = ext4fs_ls,
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700221 .exists = ext4fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600222 .size = ext4fs_size,
Simon Glass19e38582012-12-26 09:53:33 +0000223 .read = ext4_read_file,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800224#ifdef CONFIG_CMD_EXT4_WRITE
225 .write = ext4_write_file,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100226 .ln = ext4fs_create_link,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800227#else
Stephen Warren9df25802014-02-03 13:20:59 -0700228 .write = fs_write_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100229 .ln = fs_ln_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800230#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100231 .uuid = ext4fs_uuid,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400232 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900233 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900234 .mkdir = fs_mkdir_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000235 },
236#endif
Simon Glass11842872012-12-26 09:53:35 +0000237#ifdef CONFIG_SANDBOX
238 {
239 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100240 .name = "sandbox",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700241 .null_dev_desc_ok = true,
Simon Glass11842872012-12-26 09:53:35 +0000242 .probe = sandbox_fs_set_blk_dev,
243 .close = sandbox_fs_close,
244 .ls = sandbox_fs_ls,
Stephen Warren97d66f22014-02-03 13:21:07 -0700245 .exists = sandbox_fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600246 .size = sandbox_fs_size,
Simon Glass11842872012-12-26 09:53:35 +0000247 .read = fs_read_sandbox,
Simon Glassea307e82013-04-20 08:42:51 +0000248 .write = fs_write_sandbox,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100249 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400250 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900251 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900252 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100253 .ln = fs_ln_unsupported,
Simon Glass11842872012-12-26 09:53:35 +0000254 },
255#endif
Sean Andersonc678b322022-03-22 16:59:20 -0400256#ifdef CONFIG_SEMIHOSTING
257 {
258 .fstype = FS_TYPE_SEMIHOSTING,
259 .name = "semihosting",
260 .null_dev_desc_ok = true,
261 .probe = smh_fs_set_blk_dev,
262 .close = fs_close_unsupported,
263 .ls = fs_ls_unsupported,
264 .exists = fs_exists_unsupported,
265 .size = smh_fs_size,
266 .read = smh_fs_read,
267 .write = smh_fs_write,
268 .uuid = fs_uuid_unsupported,
269 .opendir = fs_opendir_unsupported,
270 .unlink = fs_unlink_unsupported,
271 .mkdir = fs_mkdir_unsupported,
272 .ln = fs_ln_unsupported,
273 },
274#endif
Pali RohĂĄrd3a3c122022-04-05 15:49:24 +0200275#ifndef CONFIG_SPL_BUILD
Hans de Goede690c7962015-09-17 18:46:58 -0400276#ifdef CONFIG_CMD_UBIFS
277 {
278 .fstype = FS_TYPE_UBIFS,
279 .name = "ubifs",
280 .null_dev_desc_ok = true,
281 .probe = ubifs_set_blk_dev,
282 .close = ubifs_close,
283 .ls = ubifs_ls,
284 .exists = ubifs_exists,
285 .size = ubifs_size,
286 .read = ubifs_read,
287 .write = fs_write_unsupported,
288 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400289 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900290 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900291 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100292 .ln = fs_ln_unsupported,
Hans de Goede690c7962015-09-17 18:46:58 -0400293 },
294#endif
Pali RohĂĄrd3a3c122022-04-05 15:49:24 +0200295#endif
Pali RohĂĄr849a61a2022-04-07 14:53:25 +0200296#ifndef CONFIG_SPL_BUILD
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200297#ifdef CONFIG_FS_BTRFS
298 {
299 .fstype = FS_TYPE_BTRFS,
300 .name = "btrfs",
301 .null_dev_desc_ok = false,
302 .probe = btrfs_probe,
303 .close = btrfs_close,
304 .ls = btrfs_ls,
305 .exists = btrfs_exists,
306 .size = btrfs_size,
307 .read = btrfs_read,
308 .write = fs_write_unsupported,
309 .uuid = btrfs_uuid,
Marek BehĂșn90762d92017-10-06 16:56:07 +0200310 .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,
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200314 },
315#endif
Pali RohĂĄr849a61a2022-04-07 14:53:25 +0200316#endif
Pali RohĂĄr1660e962022-04-06 23:34:00 +0200317#if CONFIG_IS_ENABLED(FS_SQUASHFS)
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200318 {
319 .fstype = FS_TYPE_SQUASHFS,
320 .name = "squashfs",
Richard Genoud654b2482020-11-03 12:10:59 +0100321 .null_dev_desc_ok = false,
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200322 .probe = sqfs_probe,
323 .opendir = sqfs_opendir,
324 .readdir = sqfs_readdir,
325 .ls = fs_ls_generic,
326 .read = sqfs_read,
327 .size = sqfs_size,
328 .close = sqfs_close,
329 .closedir = sqfs_closedir,
Richard Genoudcfca20c2020-11-03 12:11:26 +0100330 .exists = sqfs_exists,
Richard Genoud654b2482020-11-03 12:10:59 +0100331 .uuid = fs_uuid_unsupported,
332 .write = fs_write_unsupported,
333 .ln = fs_ln_unsupported,
334 .unlink = fs_unlink_unsupported,
335 .mkdir = fs_mkdir_unsupported,
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200336 },
337#endif
Huang Jianan024fb2f2022-02-26 15:05:47 +0800338#if IS_ENABLED(CONFIG_FS_EROFS)
339 {
340 .fstype = FS_TYPE_EROFS,
341 .name = "erofs",
342 .null_dev_desc_ok = false,
343 .probe = erofs_probe,
344 .opendir = erofs_opendir,
345 .readdir = erofs_readdir,
346 .ls = fs_ls_generic,
347 .read = erofs_read,
348 .size = erofs_size,
349 .close = erofs_close,
350 .closedir = erofs_closedir,
351 .exists = erofs_exists,
352 .uuid = fs_uuid_unsupported,
353 .write = fs_write_unsupported,
354 .ln = fs_ln_unsupported,
355 .unlink = fs_unlink_unsupported,
356 .mkdir = fs_mkdir_unsupported,
357 },
358#endif
Simon Glass1aede482012-12-26 09:53:29 +0000359 {
360 .fstype = FS_TYPE_ANY,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100361 .name = "unsupported",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700362 .null_dev_desc_ok = true,
Simon Glass1aede482012-12-26 09:53:29 +0000363 .probe = fs_probe_unsupported,
364 .close = fs_close_unsupported,
365 .ls = fs_ls_unsupported,
Stephen Warrend008fbb2014-02-03 13:21:00 -0700366 .exists = fs_exists_unsupported,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600367 .size = fs_size_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000368 .read = fs_read_unsupported,
Simon Glasseda14ea2013-04-20 08:42:50 +0000369 .write = fs_write_unsupported,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100370 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400371 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900372 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900373 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100374 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000375 },
376};
377
Simon Glasse6aad852012-12-26 09:53:30 +0000378static struct fstype_info *fs_get_info(int fstype)
379{
380 struct fstype_info *info;
381 int i;
382
383 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
384 if (fstype == info->fstype)
385 return info;
386 }
387
388 /* Return the 'unsupported' sentinel */
389 return info;
390}
391
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000392/**
AKASHI Takahiro80ac7342019-10-07 14:59:37 +0900393 * fs_get_type() - Get type of current filesystem
394 *
395 * Return: filesystem type
396 *
397 * Returns filesystem type representing the current filesystem, or
398 * FS_TYPE_ANY for any unrecognised filesystem.
399 */
400int fs_get_type(void)
401{
402 return fs_type;
403}
404
405/**
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000406 * fs_get_type_name() - Get type of current filesystem
407 *
408 * Return: Pointer to filesystem name
409 *
410 * Returns a string describing the current filesystem, or the sentinel
411 * "unsupported" for any unrecognised filesystem.
412 */
413const char *fs_get_type_name(void)
414{
415 return fs_get_info(fs_type)->name;
416}
417
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000418int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
419{
Simon Glass1aede482012-12-26 09:53:29 +0000420 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000421 int part, i;
Stephen Warren44161af2012-10-30 07:50:47 +0000422#ifdef CONFIG_NEEDS_MANUAL_RELOC
423 static int relocated;
424
425 if (!relocated) {
Simon Glass1aede482012-12-26 09:53:29 +0000426 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
427 i++, info++) {
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100428 info->name += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000429 info->probe += gd->reloc_off;
430 info->close += gd->reloc_off;
431 info->ls += gd->reloc_off;
432 info->read += gd->reloc_off;
Simon Glasseda14ea2013-04-20 08:42:50 +0000433 info->write += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000434 }
Stephen Warren44161af2012-10-30 07:50:47 +0000435 relocated = 1;
436 }
437#endif
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000438
Sean Andersonf476b4a2021-04-12 18:53:05 -0400439 part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
440 &fs_partition, 1);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000441 if (part < 0)
442 return -1;
443
Simon Glass1aede482012-12-26 09:53:29 +0000444 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
445 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
446 fstype != info->fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000447 continue;
448
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700449 if (!fs_dev_desc && !info->null_dev_desc_ok)
450 continue;
451
Simon Glass6a46e2f2012-12-26 09:53:31 +0000452 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000453 fs_type = info->fstype;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400454 fs_dev_part = part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000455 return 0;
456 }
457 }
458
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000459 return -1;
460}
461
Rob Clark2b7bfd92017-09-09 13:15:55 -0400462/* set current blk device w/ blk_desc + partition # */
463int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
464{
465 struct fstype_info *info;
466 int ret, i;
467
468 if (part >= 1)
469 ret = part_get_info(desc, part, &fs_partition);
470 else
471 ret = part_get_info_whole_disk(desc, &fs_partition);
472 if (ret)
473 return ret;
474 fs_dev_desc = desc;
475
476 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
477 if (!info->probe(fs_dev_desc, &fs_partition)) {
478 fs_type = info->fstype;
AKASHI Takahiro43efaa02018-10-17 16:32:02 +0900479 fs_dev_part = part;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400480 return 0;
481 }
482 }
483
484 return -1;
485}
486
AKASHI Takahiroca8e3772019-10-07 14:59:35 +0900487void fs_close(void)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000488{
Simon Glasse6aad852012-12-26 09:53:30 +0000489 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000490
Simon Glasse6aad852012-12-26 09:53:30 +0000491 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000492
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000493 fs_type = FS_TYPE_ANY;
494}
495
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100496int fs_uuid(char *uuid_str)
497{
498 struct fstype_info *info = fs_get_info(fs_type);
499
500 return info->uuid(uuid_str);
501}
502
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000503int fs_ls(const char *dirname)
504{
505 int ret;
506
Simon Glasse6aad852012-12-26 09:53:30 +0000507 struct fstype_info *info = fs_get_info(fs_type);
508
509 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000510
511 fs_close();
512
513 return ret;
514}
515
Stephen Warrend008fbb2014-02-03 13:21:00 -0700516int fs_exists(const char *filename)
517{
518 int ret;
519
520 struct fstype_info *info = fs_get_info(fs_type);
521
522 ret = info->exists(filename);
523
524 fs_close();
525
526 return ret;
527}
528
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800529int fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600530{
531 int ret;
532
533 struct fstype_info *info = fs_get_info(fs_type);
534
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800535 ret = info->size(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600536
537 fs_close();
538
539 return ret;
540}
541
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100542#ifdef CONFIG_LMB
543/* Check if a file may be read to the given address */
544static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
545 loff_t len, struct fstype_info *info)
546{
547 struct lmb lmb;
548 int ret;
549 loff_t size;
550 loff_t read_len;
551
552 /* get the actual size of the file */
553 ret = info->size(filename, &size);
554 if (ret)
555 return ret;
556 if (offset >= size) {
557 /* offset >= EOF, no bytes will be written */
558 return 0;
559 }
560 read_len = size - offset;
561
562 /* limit to 'len' if it is smaller */
563 if (len && len < read_len)
564 read_len = len;
565
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100566 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100567 lmb_dump_all(&lmb);
568
569 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
570 return 0;
571
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000572 log_err("** Reading file would overwrite reserved memory **\n");
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100573 return -ENOSPC;
574}
575#endif
576
577static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
578 int do_lmb_check, loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000579{
Simon Glasse6aad852012-12-26 09:53:30 +0000580 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000581 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000582 int ret;
583
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100584#ifdef CONFIG_LMB
585 if (do_lmb_check) {
586 ret = fs_read_lmb_check(filename, addr, offset, len, info);
587 if (ret)
588 return ret;
589 }
590#endif
591
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000592 /*
593 * We don't actually know how many bytes are being read, since len==0
594 * means read the whole file.
595 */
596 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800597 ret = info->read(filename, buf, offset, len, actread);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000598 unmap_sysmem(buf);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000599
Simon Glasse6aad852012-12-26 09:53:30 +0000600 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher56d9cdf2015-08-05 17:16:58 +0200601 if (ret == 0 && len && *actread != len)
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000602 log_debug("** %s shorter than offset + len **\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000603 fs_close();
604
605 return ret;
606}
607
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100608int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
609 loff_t *actread)
610{
611 return _fs_read(filename, addr, offset, len, 0, actread);
612}
613
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800614int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
615 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000616{
617 struct fstype_info *info = fs_get_info(fs_type);
618 void *buf;
619 int ret;
620
Simon Glasseda14ea2013-04-20 08:42:50 +0000621 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800622 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glasseda14ea2013-04-20 08:42:50 +0000623 unmap_sysmem(buf);
624
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800625 if (ret < 0 && len != *actwrite) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000626 log_err("** Unable to write file %s **\n", filename);
Simon Glasseda14ea2013-04-20 08:42:50 +0000627 ret = -1;
628 }
629 fs_close();
630
631 return ret;
632}
633
Rob Clark2b7bfd92017-09-09 13:15:55 -0400634struct fs_dir_stream *fs_opendir(const char *filename)
635{
636 struct fstype_info *info = fs_get_info(fs_type);
637 struct fs_dir_stream *dirs = NULL;
638 int ret;
639
640 ret = info->opendir(filename, &dirs);
641 fs_close();
642 if (ret) {
643 errno = -ret;
644 return NULL;
645 }
646
647 dirs->desc = fs_dev_desc;
648 dirs->part = fs_dev_part;
649
650 return dirs;
651}
652
653struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
654{
655 struct fstype_info *info;
656 struct fs_dirent *dirent;
657 int ret;
658
659 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
660 info = fs_get_info(fs_type);
661
662 ret = info->readdir(dirs, &dirent);
663 fs_close();
664 if (ret) {
665 errno = -ret;
666 return NULL;
667 }
668
669 return dirent;
670}
671
672void fs_closedir(struct fs_dir_stream *dirs)
673{
674 struct fstype_info *info;
675
676 if (!dirs)
677 return;
678
679 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
680 info = fs_get_info(fs_type);
681
682 info->closedir(dirs);
683 fs_close();
684}
685
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900686int fs_unlink(const char *filename)
687{
688 int ret;
689
690 struct fstype_info *info = fs_get_info(fs_type);
691
692 ret = info->unlink(filename);
693
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900694 fs_close();
695
696 return ret;
697}
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900698
699int fs_mkdir(const char *dirname)
700{
701 int ret;
702
703 struct fstype_info *info = fs_get_info(fs_type);
704
705 ret = info->mkdir(dirname);
706
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900707 fs_close();
708
709 return ret;
710}
Rob Clark2b7bfd92017-09-09 13:15:55 -0400711
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100712int fs_ln(const char *fname, const char *target)
713{
714 struct fstype_info *info = fs_get_info(fs_type);
715 int ret;
716
717 ret = info->ln(fname, target);
718
719 if (ret < 0) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000720 log_err("** Unable to create link %s -> %s **\n", fname, target);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100721 ret = -1;
722 }
723 fs_close();
724
725 return ret;
726}
727
Simon Glassed38aef2020-05-10 11:40:03 -0600728int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
729 int fstype)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600730{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800731 loff_t size;
Stephen Warren3eb58f52014-06-11 12:47:26 -0600732
733 if (argc != 4)
734 return CMD_RET_USAGE;
735
736 if (fs_set_blk_dev(argv[1], argv[2], fstype))
737 return 1;
738
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800739 if (fs_size(argv[3], &size) < 0)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600740 return CMD_RET_FAILURE;
741
Simon Glass4d949a22017-08-03 12:22:10 -0600742 env_set_hex("filesize", size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600743
744 return 0;
745}
746
Simon Glassed38aef2020-05-10 11:40:03 -0600747int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
748 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000749{
750 unsigned long addr;
751 const char *addr_str;
752 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800753 loff_t bytes;
754 loff_t pos;
755 loff_t len_read;
756 int ret;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100757 unsigned long time;
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200758 char *ep;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000759
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000760 if (argc < 2)
761 return CMD_RET_USAGE;
762 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000763 return CMD_RET_USAGE;
764
Heinrich Schuchardtc556a452021-01-27 21:26:43 +0100765 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) {
766 log_err("Can't set block device\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000767 return 1;
Heinrich Schuchardtc556a452021-01-27 21:26:43 +0100768 }
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000769
770 if (argc >= 4) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600771 addr = hextoul(argv[3], &ep);
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200772 if (ep == argv[3] || *ep != '\0')
773 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000774 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600775 addr_str = env_get("loadaddr");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000776 if (addr_str != NULL)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600777 addr = hextoul(addr_str, NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000778 else
779 addr = CONFIG_SYS_LOAD_ADDR;
780 }
781 if (argc >= 5) {
782 filename = argv[4];
783 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600784 filename = env_get("bootfile");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000785 if (!filename) {
786 puts("** No boot file defined **\n");
787 return 1;
788 }
789 }
790 if (argc >= 6)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600791 bytes = hextoul(argv[5], NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000792 else
793 bytes = 0;
794 if (argc >= 7)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600795 pos = hextoul(argv[6], NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000796 else
797 pos = 0;
798
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100799 time = get_timer(0);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100800 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100801 time = get_timer(time);
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200802 if (ret < 0) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000803 log_err("Failed to load '%s'\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000804 return 1;
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200805 }
806
807 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
808 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
Heinrich Schuchardt6b821592021-01-12 12:46:24 +0100809 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
810 len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000811
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800812 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100813 if (time > 0) {
814 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500815 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100816 puts(")");
817 }
818 puts("\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000819
Simon Glass4d949a22017-08-03 12:22:10 -0600820 env_set_hex("fileaddr", addr);
821 env_set_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000822
823 return 0;
824}
825
Simon Glassed38aef2020-05-10 11:40:03 -0600826int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
827 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000828{
829 if (argc < 2)
830 return CMD_RET_USAGE;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000831 if (argc > 4)
832 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000833
834 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
835 return 1;
836
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000837 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000838 return 1;
839
840 return 0;
841}
Simon Glasseda14ea2013-04-20 08:42:50 +0000842
Stephen Warrend008fbb2014-02-03 13:21:00 -0700843int file_exists(const char *dev_type, const char *dev_part, const char *file,
844 int fstype)
845{
846 if (fs_set_blk_dev(dev_type, dev_part, fstype))
847 return 0;
848
849 return fs_exists(file);
850}
851
Simon Glassed38aef2020-05-10 11:40:03 -0600852int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
853 int fstype)
Simon Glasseda14ea2013-04-20 08:42:50 +0000854{
855 unsigned long addr;
856 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800857 loff_t bytes;
858 loff_t pos;
859 loff_t len;
860 int ret;
Simon Glasseda14ea2013-04-20 08:42:50 +0000861 unsigned long time;
862
863 if (argc < 6 || argc > 7)
864 return CMD_RET_USAGE;
865
866 if (fs_set_blk_dev(argv[1], argv[2], fstype))
867 return 1;
868
Simon Glass3ff49ec2021-07-24 09:03:29 -0600869 addr = hextoul(argv[3], NULL);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800870 filename = argv[4];
Simon Glass3ff49ec2021-07-24 09:03:29 -0600871 bytes = hextoul(argv[5], NULL);
Simon Glasseda14ea2013-04-20 08:42:50 +0000872 if (argc >= 7)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600873 pos = hextoul(argv[6], NULL);
Simon Glasseda14ea2013-04-20 08:42:50 +0000874 else
875 pos = 0;
876
877 time = get_timer(0);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800878 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glasseda14ea2013-04-20 08:42:50 +0000879 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800880 if (ret < 0)
Simon Glasseda14ea2013-04-20 08:42:50 +0000881 return 1;
882
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800883 printf("%llu bytes written in %lu ms", len, time);
Simon Glasseda14ea2013-04-20 08:42:50 +0000884 if (time > 0) {
885 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500886 print_size(div_u64(len, time) * 1000, "/s");
Simon Glasseda14ea2013-04-20 08:42:50 +0000887 puts(")");
888 }
889 puts("\n");
890
891 return 0;
892}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100893
Simon Glassed38aef2020-05-10 11:40:03 -0600894int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
895 int fstype)
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100896{
897 int ret;
898 char uuid[37];
899 memset(uuid, 0, sizeof(uuid));
900
901 if (argc < 3 || argc > 4)
902 return CMD_RET_USAGE;
903
904 if (fs_set_blk_dev(argv[1], argv[2], fstype))
905 return 1;
906
907 ret = fs_uuid(uuid);
908 if (ret)
909 return CMD_RET_FAILURE;
910
911 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600912 env_set(argv[3], uuid);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100913 else
914 printf("%s\n", uuid);
915
916 return CMD_RET_SUCCESS;
917}
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100918
Simon Glassed38aef2020-05-10 11:40:03 -0600919int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100920{
921 struct fstype_info *info;
922
923 if (argc < 3 || argc > 4)
924 return CMD_RET_USAGE;
925
926 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
927 return 1;
928
929 info = fs_get_info(fs_type);
930
931 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600932 env_set(argv[3], info->name);
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100933 else
934 printf("%s\n", info->name);
935
Marek Vasut08d1c422019-02-06 13:19:29 +0100936 fs_close();
937
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100938 return CMD_RET_SUCCESS;
939}
940
Simon Glassed38aef2020-05-10 11:40:03 -0600941int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900942 int fstype)
943{
944 if (argc != 4)
945 return CMD_RET_USAGE;
946
947 if (fs_set_blk_dev(argv[1], argv[2], fstype))
948 return 1;
949
950 if (fs_unlink(argv[3]))
951 return 1;
952
953 return 0;
954}
955
Simon Glassed38aef2020-05-10 11:40:03 -0600956int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900957 int fstype)
958{
959 int ret;
960
961 if (argc != 4)
962 return CMD_RET_USAGE;
963
964 if (fs_set_blk_dev(argv[1], argv[2], fstype))
965 return 1;
966
967 ret = fs_mkdir(argv[3]);
968 if (ret) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000969 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900970 return 1;
971 }
972
973 return 0;
974}
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100975
Simon Glassed38aef2020-05-10 11:40:03 -0600976int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100977 int fstype)
978{
979 if (argc != 5)
980 return CMD_RET_USAGE;
981
982 if (fs_set_blk_dev(argv[1], argv[2], fstype))
983 return 1;
984
985 if (fs_ln(argv[3], argv[4]))
986 return 1;
987
988 return 0;
989}
Niel Fourie9252d702020-03-24 16:17:04 +0100990
991int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
992{
993 struct fstype_info *drv = fstypes;
994 const int n_ents = ARRAY_SIZE(fstypes);
995 struct fstype_info *entry;
996 int i = 0;
997
998 puts("Supported filesystems");
999 for (entry = drv; entry != drv + n_ents; entry++) {
1000 if (entry->fstype != FS_TYPE_ANY) {
1001 printf("%c %s", i ? ',' : ':', entry->name);
1002 i++;
1003 }
1004 }
1005 if (!i)
1006 puts(": <none>");
1007 puts("\n");
1008 return CMD_RET_SUCCESS;
1009}