blob: 8324b4a22f20661a71de9a34451076659353e170 [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>
Simon Glass1ab16922022-07-31 12:28:48 -060010#include <display_options.h>
Christian Gmeiner9f9eec32014-11-12 14:35:04 +010011#include <errno.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000012#include <common.h>
Simon Glass313112a2019-08-01 09:46:46 -060013#include <env.h>
Simon Glass655306c2020-05-10 11:39:58 -060014#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050016#include <mapmem.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000017#include <part.h>
18#include <ext4fs.h>
19#include <fat.h>
20#include <fs.h>
Simon Glass11842872012-12-26 09:53:35 +000021#include <sandboxfs.h>
Sean Andersonc678b322022-03-22 16:59:20 -040022#include <semihostingfs.h>
Hans de Goede690c7962015-09-17 18:46:58 -040023#include <ubifs_uboot.h>
Marek BehĂșn98ec1d12017-09-03 17:00:29 +020024#include <btrfs.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060025#include <asm/global_data.h>
Simon Glasscbe5d5d2012-12-26 09:53:32 +000026#include <asm/io.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050027#include <div64.h>
28#include <linux/math64.h>
Mian Yousaf Kaukab961ebe82019-06-18 15:03:44 +020029#include <efi_loader.h>
Joao Marcos Costa29da3742020-07-30 15:33:47 +020030#include <squashfs.h>
Huang Jianan024fb2f2022-02-26 15:05:47 +080031#include <erofs.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000032
Stephen Warren44161af2012-10-30 07:50:47 +000033DECLARE_GLOBAL_DATA_PTR;
34
Simon Glasse3394752016-02-29 15:25:34 -070035static struct blk_desc *fs_dev_desc;
Rob Clark2b7bfd92017-09-09 13:15:55 -040036static int fs_dev_part;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060037static struct disk_partition fs_partition;
Stephen Warreneefbc3f2012-10-22 06:43:51 +000038static int fs_type = FS_TYPE_ANY;
39
Simon Glass896ed4b2022-04-24 23:31:04 -060040void fs_set_type(int type)
41{
42 fs_type = type;
43}
44
Simon Glasse3394752016-02-29 15:25:34 -070045static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -060046 struct disk_partition *fs_partition)
Simon Glass1aede482012-12-26 09:53:29 +000047{
Heinrich Schuchardt3f006d52021-09-20 14:15:03 +020048 log_debug("Unrecognized filesystem type\n");
Simon Glass1aede482012-12-26 09:53:29 +000049 return -1;
50}
51
Stephen Warreneefbc3f2012-10-22 06:43:51 +000052static inline int fs_ls_unsupported(const char *dirname)
53{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000054 return -1;
55}
56
Rob Clark60d74a92017-09-09 13:15:58 -040057/* generic implementation of ls in terms of opendir/readdir/closedir */
58__maybe_unused
59static int fs_ls_generic(const char *dirname)
60{
61 struct fs_dir_stream *dirs;
62 struct fs_dirent *dent;
63 int nfiles = 0, ndirs = 0;
64
65 dirs = fs_opendir(dirname);
66 if (!dirs)
67 return -errno;
68
69 while ((dent = fs_readdir(dirs))) {
70 if (dent->type == FS_DT_DIR) {
71 printf(" %s/\n", dent->name);
72 ndirs++;
Joao Marcos Costa2a7fb152020-07-30 15:33:51 +020073 } else if (dent->type == FS_DT_LNK) {
74 printf(" <SYM> %s\n", dent->name);
75 nfiles++;
Rob Clark60d74a92017-09-09 13:15:58 -040076 } else {
77 printf(" %8lld %s\n", dent->size, dent->name);
78 nfiles++;
79 }
80 }
81
82 fs_closedir(dirs);
83
84 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
85
86 return 0;
87}
88
Stephen Warrend008fbb2014-02-03 13:21:00 -070089static inline int fs_exists_unsupported(const char *filename)
90{
91 return 0;
92}
93
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080094static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -060095{
96 return -1;
97}
98
Simon Glasscbe5d5d2012-12-26 09:53:32 +000099static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800100 loff_t offset, loff_t len,
101 loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000102{
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000103 return -1;
104}
105
Simon Glasseda14ea2013-04-20 08:42:50 +0000106static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800107 loff_t offset, loff_t len,
108 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000109{
110 return -1;
111}
112
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100113static inline int fs_ln_unsupported(const char *filename, const char *target)
114{
115 return -1;
116}
117
Simon Glass1aede482012-12-26 09:53:29 +0000118static inline void fs_close_unsupported(void)
119{
120}
121
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100122static inline int fs_uuid_unsupported(char *uuid_str)
123{
124 return -1;
125}
126
Rob Clark2b7bfd92017-09-09 13:15:55 -0400127static inline int fs_opendir_unsupported(const char *filename,
128 struct fs_dir_stream **dirs)
129{
130 return -EACCES;
131}
132
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900133static inline int fs_unlink_unsupported(const char *filename)
134{
135 return -1;
136}
137
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900138static inline int fs_mkdir_unsupported(const char *dirname)
139{
140 return -1;
141}
142
Simon Glass1aede482012-12-26 09:53:29 +0000143struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000144 int fstype;
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100145 char *name;
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700146 /*
147 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
148 * should be false in most cases. For "virtual" filesystems which
149 * aren't based on a U-Boot block device (e.g. sandbox), this can be
Heinrich Schuchardt99dc17d2018-08-11 15:52:14 +0200150 * set to true. This should also be true for the dummy entry at the end
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700151 * of fstypes[], since that is essentially a "virtual" (non-existent)
152 * filesystem.
153 */
154 bool null_dev_desc_ok;
Simon Glasse3394752016-02-29 15:25:34 -0700155 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600156 struct disk_partition *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +0000157 int (*ls)(const char *dirname);
Stephen Warrend008fbb2014-02-03 13:21:00 -0700158 int (*exists)(const char *filename);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800159 int (*size)(const char *filename, loff_t *size);
160 int (*read)(const char *filename, void *buf, loff_t offset,
161 loff_t len, loff_t *actread);
162 int (*write)(const char *filename, void *buf, loff_t offset,
163 loff_t len, loff_t *actwrite);
Simon Glass1aede482012-12-26 09:53:29 +0000164 void (*close)(void);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100165 int (*uuid)(char *uuid_str);
Rob Clark2b7bfd92017-09-09 13:15:55 -0400166 /*
167 * Open a directory stream. On success return 0 and directory
168 * stream pointer via 'dirsp'. On error, return -errno. See
169 * fs_opendir().
170 */
171 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
172 /*
173 * Read next entry from directory stream. On success return 0
174 * and directory entry pointer via 'dentp'. On error return
175 * -errno. See fs_readdir().
176 */
177 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
178 /* see fs_closedir() */
179 void (*closedir)(struct fs_dir_stream *dirs);
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900180 int (*unlink)(const char *filename);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900181 int (*mkdir)(const char *dirname);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100182 int (*ln)(const char *filename, const char *target);
Simon Glass1aede482012-12-26 09:53:29 +0000183};
184
185static struct fstype_info fstypes[] = {
Pali RohĂĄrd291f892022-04-05 15:48:32 +0200186#if CONFIG_IS_ENABLED(FS_FAT)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000187 {
188 .fstype = FS_TYPE_FAT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100189 .name = "fat",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700190 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000191 .probe = fat_set_blk_dev,
192 .close = fat_close,
Rob Clark60d74a92017-09-09 13:15:58 -0400193 .ls = fs_ls_generic,
Stephen Warren1d2f9a02014-02-03 13:21:10 -0700194 .exists = fat_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600195 .size = fat_size,
Simon Glass19e38582012-12-26 09:53:33 +0000196 .read = fat_read_file,
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800197#if CONFIG_IS_ENABLED(FAT_WRITE)
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800198 .write = file_fat_write,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900199 .unlink = fat_unlink,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900200 .mkdir = fat_mkdir,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800201#else
Stephen Warren9df25802014-02-03 13:20:59 -0700202 .write = fs_write_unsupported,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900203 .unlink = fs_unlink_unsupported,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900204 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800205#endif
Heinrich Schuchardtfac91832020-12-31 00:38:13 +0100206 .uuid = fat_uuid,
Rob Clark60d74a92017-09-09 13:15:58 -0400207 .opendir = fat_opendir,
208 .readdir = fat_readdir,
209 .closedir = fat_closedir,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100210 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000211 },
Simon Glass1aede482012-12-26 09:53:29 +0000212#endif
Tien Fong Chee64eab042019-01-23 14:20:06 +0800213
214#if CONFIG_IS_ENABLED(FS_EXT4)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000215 {
216 .fstype = FS_TYPE_EXT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100217 .name = "ext4",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700218 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000219 .probe = ext4fs_probe,
220 .close = ext4fs_close,
Simon Glass1aede482012-12-26 09:53:29 +0000221 .ls = ext4fs_ls,
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700222 .exists = ext4fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600223 .size = ext4fs_size,
Simon Glass19e38582012-12-26 09:53:33 +0000224 .read = ext4_read_file,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800225#ifdef CONFIG_CMD_EXT4_WRITE
226 .write = ext4_write_file,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100227 .ln = ext4fs_create_link,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800228#else
Stephen Warren9df25802014-02-03 13:20:59 -0700229 .write = fs_write_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100230 .ln = fs_ln_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800231#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100232 .uuid = ext4fs_uuid,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400233 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900234 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900235 .mkdir = fs_mkdir_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000236 },
237#endif
Simon Glass11842872012-12-26 09:53:35 +0000238#ifdef CONFIG_SANDBOX
239 {
240 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100241 .name = "sandbox",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700242 .null_dev_desc_ok = true,
Simon Glass11842872012-12-26 09:53:35 +0000243 .probe = sandbox_fs_set_blk_dev,
244 .close = sandbox_fs_close,
245 .ls = sandbox_fs_ls,
Stephen Warren97d66f22014-02-03 13:21:07 -0700246 .exists = sandbox_fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600247 .size = sandbox_fs_size,
Simon Glass11842872012-12-26 09:53:35 +0000248 .read = fs_read_sandbox,
Simon Glassea307e82013-04-20 08:42:51 +0000249 .write = fs_write_sandbox,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100250 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400251 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900252 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900253 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100254 .ln = fs_ln_unsupported,
Simon Glass11842872012-12-26 09:53:35 +0000255 },
256#endif
Sean Andersonc678b322022-03-22 16:59:20 -0400257#ifdef CONFIG_SEMIHOSTING
258 {
259 .fstype = FS_TYPE_SEMIHOSTING,
260 .name = "semihosting",
261 .null_dev_desc_ok = true,
262 .probe = smh_fs_set_blk_dev,
263 .close = fs_close_unsupported,
264 .ls = fs_ls_unsupported,
265 .exists = fs_exists_unsupported,
266 .size = smh_fs_size,
267 .read = smh_fs_read,
268 .write = smh_fs_write,
269 .uuid = fs_uuid_unsupported,
270 .opendir = fs_opendir_unsupported,
271 .unlink = fs_unlink_unsupported,
272 .mkdir = fs_mkdir_unsupported,
273 .ln = fs_ln_unsupported,
274 },
275#endif
Pali RohĂĄrd3a3c122022-04-05 15:49:24 +0200276#ifndef CONFIG_SPL_BUILD
Hans de Goede690c7962015-09-17 18:46:58 -0400277#ifdef CONFIG_CMD_UBIFS
278 {
279 .fstype = FS_TYPE_UBIFS,
280 .name = "ubifs",
281 .null_dev_desc_ok = true,
282 .probe = ubifs_set_blk_dev,
283 .close = ubifs_close,
284 .ls = ubifs_ls,
285 .exists = ubifs_exists,
286 .size = ubifs_size,
287 .read = ubifs_read,
288 .write = fs_write_unsupported,
289 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400290 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900291 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900292 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100293 .ln = fs_ln_unsupported,
Hans de Goede690c7962015-09-17 18:46:58 -0400294 },
295#endif
Pali RohĂĄrd3a3c122022-04-05 15:49:24 +0200296#endif
Pali RohĂĄr849a61a2022-04-07 14:53:25 +0200297#ifndef CONFIG_SPL_BUILD
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200298#ifdef CONFIG_FS_BTRFS
299 {
300 .fstype = FS_TYPE_BTRFS,
301 .name = "btrfs",
302 .null_dev_desc_ok = false,
303 .probe = btrfs_probe,
304 .close = btrfs_close,
305 .ls = btrfs_ls,
306 .exists = btrfs_exists,
307 .size = btrfs_size,
308 .read = btrfs_read,
309 .write = fs_write_unsupported,
310 .uuid = btrfs_uuid,
Marek BehĂșn90762d92017-10-06 16:56:07 +0200311 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900312 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900313 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100314 .ln = fs_ln_unsupported,
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200315 },
316#endif
Pali RohĂĄr849a61a2022-04-07 14:53:25 +0200317#endif
Pali RohĂĄr1660e962022-04-06 23:34:00 +0200318#if CONFIG_IS_ENABLED(FS_SQUASHFS)
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200319 {
320 .fstype = FS_TYPE_SQUASHFS,
321 .name = "squashfs",
Richard Genoud654b2482020-11-03 12:10:59 +0100322 .null_dev_desc_ok = false,
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200323 .probe = sqfs_probe,
324 .opendir = sqfs_opendir,
325 .readdir = sqfs_readdir,
326 .ls = fs_ls_generic,
327 .read = sqfs_read,
328 .size = sqfs_size,
329 .close = sqfs_close,
330 .closedir = sqfs_closedir,
Richard Genoudcfca20c2020-11-03 12:11:26 +0100331 .exists = sqfs_exists,
Richard Genoud654b2482020-11-03 12:10:59 +0100332 .uuid = fs_uuid_unsupported,
333 .write = fs_write_unsupported,
334 .ln = fs_ln_unsupported,
335 .unlink = fs_unlink_unsupported,
336 .mkdir = fs_mkdir_unsupported,
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200337 },
338#endif
Huang Jianan024fb2f2022-02-26 15:05:47 +0800339#if IS_ENABLED(CONFIG_FS_EROFS)
340 {
341 .fstype = FS_TYPE_EROFS,
342 .name = "erofs",
343 .null_dev_desc_ok = false,
344 .probe = erofs_probe,
345 .opendir = erofs_opendir,
346 .readdir = erofs_readdir,
347 .ls = fs_ls_generic,
348 .read = erofs_read,
349 .size = erofs_size,
350 .close = erofs_close,
351 .closedir = erofs_closedir,
352 .exists = erofs_exists,
353 .uuid = fs_uuid_unsupported,
354 .write = fs_write_unsupported,
355 .ln = fs_ln_unsupported,
356 .unlink = fs_unlink_unsupported,
357 .mkdir = fs_mkdir_unsupported,
358 },
359#endif
Simon Glass1aede482012-12-26 09:53:29 +0000360 {
361 .fstype = FS_TYPE_ANY,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100362 .name = "unsupported",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700363 .null_dev_desc_ok = true,
Simon Glass1aede482012-12-26 09:53:29 +0000364 .probe = fs_probe_unsupported,
365 .close = fs_close_unsupported,
366 .ls = fs_ls_unsupported,
Stephen Warrend008fbb2014-02-03 13:21:00 -0700367 .exists = fs_exists_unsupported,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600368 .size = fs_size_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000369 .read = fs_read_unsupported,
Simon Glasseda14ea2013-04-20 08:42:50 +0000370 .write = fs_write_unsupported,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100371 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400372 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900373 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900374 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100375 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000376 },
377};
378
Simon Glasse6aad852012-12-26 09:53:30 +0000379static struct fstype_info *fs_get_info(int fstype)
380{
381 struct fstype_info *info;
382 int i;
383
384 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
385 if (fstype == info->fstype)
386 return info;
387 }
388
389 /* Return the 'unsupported' sentinel */
390 return info;
391}
392
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000393/**
AKASHI Takahiro80ac7342019-10-07 14:59:37 +0900394 * fs_get_type() - Get type of current filesystem
395 *
396 * Return: filesystem type
397 *
398 * Returns filesystem type representing the current filesystem, or
399 * FS_TYPE_ANY for any unrecognised filesystem.
400 */
401int fs_get_type(void)
402{
403 return fs_type;
404}
405
406/**
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000407 * fs_get_type_name() - Get type of current filesystem
408 *
409 * Return: Pointer to filesystem name
410 *
411 * Returns a string describing the current filesystem, or the sentinel
412 * "unsupported" for any unrecognised filesystem.
413 */
414const char *fs_get_type_name(void)
415{
416 return fs_get_info(fs_type)->name;
417}
418
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000419int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
420{
Simon Glass1aede482012-12-26 09:53:29 +0000421 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000422 int part, i;
Stephen Warren44161af2012-10-30 07:50:47 +0000423#ifdef CONFIG_NEEDS_MANUAL_RELOC
424 static int relocated;
425
426 if (!relocated) {
Simon Glass1aede482012-12-26 09:53:29 +0000427 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
428 i++, info++) {
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100429 info->name += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000430 info->probe += gd->reloc_off;
431 info->close += gd->reloc_off;
432 info->ls += gd->reloc_off;
433 info->read += gd->reloc_off;
Simon Glasseda14ea2013-04-20 08:42:50 +0000434 info->write += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000435 }
Stephen Warren44161af2012-10-30 07:50:47 +0000436 relocated = 1;
437 }
438#endif
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000439
Sean Andersonf476b4a2021-04-12 18:53:05 -0400440 part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
441 &fs_partition, 1);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000442 if (part < 0)
443 return -1;
444
Simon Glass1aede482012-12-26 09:53:29 +0000445 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
446 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
447 fstype != info->fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000448 continue;
449
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700450 if (!fs_dev_desc && !info->null_dev_desc_ok)
451 continue;
452
Simon Glass6a46e2f2012-12-26 09:53:31 +0000453 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000454 fs_type = info->fstype;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400455 fs_dev_part = part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000456 return 0;
457 }
458 }
459
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000460 return -1;
461}
462
Rob Clark2b7bfd92017-09-09 13:15:55 -0400463/* set current blk device w/ blk_desc + partition # */
464int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
465{
466 struct fstype_info *info;
467 int ret, i;
468
469 if (part >= 1)
470 ret = part_get_info(desc, part, &fs_partition);
471 else
472 ret = part_get_info_whole_disk(desc, &fs_partition);
473 if (ret)
474 return ret;
475 fs_dev_desc = desc;
476
477 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
478 if (!info->probe(fs_dev_desc, &fs_partition)) {
479 fs_type = info->fstype;
AKASHI Takahiro43efaa02018-10-17 16:32:02 +0900480 fs_dev_part = part;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400481 return 0;
482 }
483 }
484
485 return -1;
486}
487
AKASHI Takahiroca8e3772019-10-07 14:59:35 +0900488void fs_close(void)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000489{
Simon Glasse6aad852012-12-26 09:53:30 +0000490 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000491
Simon Glasse6aad852012-12-26 09:53:30 +0000492 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000493
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000494 fs_type = FS_TYPE_ANY;
495}
496
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100497int fs_uuid(char *uuid_str)
498{
499 struct fstype_info *info = fs_get_info(fs_type);
500
501 return info->uuid(uuid_str);
502}
503
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000504int fs_ls(const char *dirname)
505{
506 int ret;
507
Simon Glasse6aad852012-12-26 09:53:30 +0000508 struct fstype_info *info = fs_get_info(fs_type);
509
510 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000511
512 fs_close();
513
514 return ret;
515}
516
Stephen Warrend008fbb2014-02-03 13:21:00 -0700517int fs_exists(const char *filename)
518{
519 int ret;
520
521 struct fstype_info *info = fs_get_info(fs_type);
522
523 ret = info->exists(filename);
524
525 fs_close();
526
527 return ret;
528}
529
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800530int fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600531{
532 int ret;
533
534 struct fstype_info *info = fs_get_info(fs_type);
535
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800536 ret = info->size(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600537
538 fs_close();
539
540 return ret;
541}
542
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100543#ifdef CONFIG_LMB
544/* Check if a file may be read to the given address */
545static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
546 loff_t len, struct fstype_info *info)
547{
548 struct lmb lmb;
549 int ret;
550 loff_t size;
551 loff_t read_len;
552
553 /* get the actual size of the file */
554 ret = info->size(filename, &size);
555 if (ret)
556 return ret;
557 if (offset >= size) {
558 /* offset >= EOF, no bytes will be written */
559 return 0;
560 }
561 read_len = size - offset;
562
563 /* limit to 'len' if it is smaller */
564 if (len && len < read_len)
565 read_len = len;
566
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100567 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100568 lmb_dump_all(&lmb);
569
570 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
571 return 0;
572
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000573 log_err("** Reading file would overwrite reserved memory **\n");
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100574 return -ENOSPC;
575}
576#endif
577
578static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
579 int do_lmb_check, loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000580{
Simon Glasse6aad852012-12-26 09:53:30 +0000581 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000582 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000583 int ret;
584
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100585#ifdef CONFIG_LMB
586 if (do_lmb_check) {
587 ret = fs_read_lmb_check(filename, addr, offset, len, info);
588 if (ret)
589 return ret;
590 }
591#endif
592
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000593 /*
594 * We don't actually know how many bytes are being read, since len==0
595 * means read the whole file.
596 */
597 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800598 ret = info->read(filename, buf, offset, len, actread);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000599 unmap_sysmem(buf);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000600
Simon Glasse6aad852012-12-26 09:53:30 +0000601 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher56d9cdf2015-08-05 17:16:58 +0200602 if (ret == 0 && len && *actread != len)
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000603 log_debug("** %s shorter than offset + len **\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000604 fs_close();
605
606 return ret;
607}
608
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100609int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
610 loff_t *actread)
611{
612 return _fs_read(filename, addr, offset, len, 0, actread);
613}
614
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800615int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
616 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000617{
618 struct fstype_info *info = fs_get_info(fs_type);
619 void *buf;
620 int ret;
621
Simon Glasseda14ea2013-04-20 08:42:50 +0000622 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800623 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glasseda14ea2013-04-20 08:42:50 +0000624 unmap_sysmem(buf);
625
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800626 if (ret < 0 && len != *actwrite) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000627 log_err("** Unable to write file %s **\n", filename);
Simon Glasseda14ea2013-04-20 08:42:50 +0000628 ret = -1;
629 }
630 fs_close();
631
632 return ret;
633}
634
Rob Clark2b7bfd92017-09-09 13:15:55 -0400635struct fs_dir_stream *fs_opendir(const char *filename)
636{
637 struct fstype_info *info = fs_get_info(fs_type);
638 struct fs_dir_stream *dirs = NULL;
639 int ret;
640
641 ret = info->opendir(filename, &dirs);
642 fs_close();
643 if (ret) {
644 errno = -ret;
645 return NULL;
646 }
647
648 dirs->desc = fs_dev_desc;
649 dirs->part = fs_dev_part;
650
651 return dirs;
652}
653
654struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
655{
656 struct fstype_info *info;
657 struct fs_dirent *dirent;
658 int ret;
659
660 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
661 info = fs_get_info(fs_type);
662
663 ret = info->readdir(dirs, &dirent);
664 fs_close();
665 if (ret) {
666 errno = -ret;
667 return NULL;
668 }
669
670 return dirent;
671}
672
673void fs_closedir(struct fs_dir_stream *dirs)
674{
675 struct fstype_info *info;
676
677 if (!dirs)
678 return;
679
680 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
681 info = fs_get_info(fs_type);
682
683 info->closedir(dirs);
684 fs_close();
685}
686
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900687int fs_unlink(const char *filename)
688{
689 int ret;
690
691 struct fstype_info *info = fs_get_info(fs_type);
692
693 ret = info->unlink(filename);
694
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900695 fs_close();
696
697 return ret;
698}
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900699
700int fs_mkdir(const char *dirname)
701{
702 int ret;
703
704 struct fstype_info *info = fs_get_info(fs_type);
705
706 ret = info->mkdir(dirname);
707
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900708 fs_close();
709
710 return ret;
711}
Rob Clark2b7bfd92017-09-09 13:15:55 -0400712
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100713int fs_ln(const char *fname, const char *target)
714{
715 struct fstype_info *info = fs_get_info(fs_type);
716 int ret;
717
718 ret = info->ln(fname, target);
719
720 if (ret < 0) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000721 log_err("** Unable to create link %s -> %s **\n", fname, target);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100722 ret = -1;
723 }
724 fs_close();
725
726 return ret;
727}
728
Simon Glassed38aef2020-05-10 11:40:03 -0600729int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
730 int fstype)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600731{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800732 loff_t size;
Stephen Warren3eb58f52014-06-11 12:47:26 -0600733
734 if (argc != 4)
735 return CMD_RET_USAGE;
736
737 if (fs_set_blk_dev(argv[1], argv[2], fstype))
738 return 1;
739
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800740 if (fs_size(argv[3], &size) < 0)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600741 return CMD_RET_FAILURE;
742
Simon Glass4d949a22017-08-03 12:22:10 -0600743 env_set_hex("filesize", size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600744
745 return 0;
746}
747
Simon Glassed38aef2020-05-10 11:40:03 -0600748int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
749 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000750{
751 unsigned long addr;
752 const char *addr_str;
753 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800754 loff_t bytes;
755 loff_t pos;
756 loff_t len_read;
757 int ret;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100758 unsigned long time;
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200759 char *ep;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000760
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000761 if (argc < 2)
762 return CMD_RET_USAGE;
763 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000764 return CMD_RET_USAGE;
765
Heinrich Schuchardtc556a452021-01-27 21:26:43 +0100766 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) {
767 log_err("Can't set block device\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000768 return 1;
Heinrich Schuchardtc556a452021-01-27 21:26:43 +0100769 }
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000770
771 if (argc >= 4) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600772 addr = hextoul(argv[3], &ep);
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200773 if (ep == argv[3] || *ep != '\0')
774 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000775 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600776 addr_str = env_get("loadaddr");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000777 if (addr_str != NULL)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600778 addr = hextoul(addr_str, NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000779 else
780 addr = CONFIG_SYS_LOAD_ADDR;
781 }
782 if (argc >= 5) {
783 filename = argv[4];
784 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600785 filename = env_get("bootfile");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000786 if (!filename) {
787 puts("** No boot file defined **\n");
788 return 1;
789 }
790 }
791 if (argc >= 6)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600792 bytes = hextoul(argv[5], NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000793 else
794 bytes = 0;
795 if (argc >= 7)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600796 pos = hextoul(argv[6], NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000797 else
798 pos = 0;
799
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100800 time = get_timer(0);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100801 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100802 time = get_timer(time);
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200803 if (ret < 0) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000804 log_err("Failed to load '%s'\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000805 return 1;
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200806 }
807
808 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
809 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
Heinrich Schuchardt6b821592021-01-12 12:46:24 +0100810 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
811 len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000812
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800813 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100814 if (time > 0) {
815 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500816 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100817 puts(")");
818 }
819 puts("\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000820
Simon Glass4d949a22017-08-03 12:22:10 -0600821 env_set_hex("fileaddr", addr);
822 env_set_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000823
824 return 0;
825}
826
Simon Glassed38aef2020-05-10 11:40:03 -0600827int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
828 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000829{
830 if (argc < 2)
831 return CMD_RET_USAGE;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000832 if (argc > 4)
833 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000834
835 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
836 return 1;
837
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000838 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000839 return 1;
840
841 return 0;
842}
Simon Glasseda14ea2013-04-20 08:42:50 +0000843
Stephen Warrend008fbb2014-02-03 13:21:00 -0700844int file_exists(const char *dev_type, const char *dev_part, const char *file,
845 int fstype)
846{
847 if (fs_set_blk_dev(dev_type, dev_part, fstype))
848 return 0;
849
850 return fs_exists(file);
851}
852
Simon Glassed38aef2020-05-10 11:40:03 -0600853int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
854 int fstype)
Simon Glasseda14ea2013-04-20 08:42:50 +0000855{
856 unsigned long addr;
857 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800858 loff_t bytes;
859 loff_t pos;
860 loff_t len;
861 int ret;
Simon Glasseda14ea2013-04-20 08:42:50 +0000862 unsigned long time;
863
864 if (argc < 6 || argc > 7)
865 return CMD_RET_USAGE;
866
867 if (fs_set_blk_dev(argv[1], argv[2], fstype))
868 return 1;
869
Simon Glass3ff49ec2021-07-24 09:03:29 -0600870 addr = hextoul(argv[3], NULL);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800871 filename = argv[4];
Simon Glass3ff49ec2021-07-24 09:03:29 -0600872 bytes = hextoul(argv[5], NULL);
Simon Glasseda14ea2013-04-20 08:42:50 +0000873 if (argc >= 7)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600874 pos = hextoul(argv[6], NULL);
Simon Glasseda14ea2013-04-20 08:42:50 +0000875 else
876 pos = 0;
877
878 time = get_timer(0);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800879 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glasseda14ea2013-04-20 08:42:50 +0000880 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800881 if (ret < 0)
Simon Glasseda14ea2013-04-20 08:42:50 +0000882 return 1;
883
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800884 printf("%llu bytes written in %lu ms", len, time);
Simon Glasseda14ea2013-04-20 08:42:50 +0000885 if (time > 0) {
886 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500887 print_size(div_u64(len, time) * 1000, "/s");
Simon Glasseda14ea2013-04-20 08:42:50 +0000888 puts(")");
889 }
890 puts("\n");
891
892 return 0;
893}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100894
Simon Glassed38aef2020-05-10 11:40:03 -0600895int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
896 int fstype)
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100897{
898 int ret;
899 char uuid[37];
900 memset(uuid, 0, sizeof(uuid));
901
902 if (argc < 3 || argc > 4)
903 return CMD_RET_USAGE;
904
905 if (fs_set_blk_dev(argv[1], argv[2], fstype))
906 return 1;
907
908 ret = fs_uuid(uuid);
909 if (ret)
910 return CMD_RET_FAILURE;
911
912 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600913 env_set(argv[3], uuid);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100914 else
915 printf("%s\n", uuid);
916
917 return CMD_RET_SUCCESS;
918}
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100919
Simon Glassed38aef2020-05-10 11:40:03 -0600920int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100921{
922 struct fstype_info *info;
923
924 if (argc < 3 || argc > 4)
925 return CMD_RET_USAGE;
926
927 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
928 return 1;
929
930 info = fs_get_info(fs_type);
931
932 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600933 env_set(argv[3], info->name);
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100934 else
935 printf("%s\n", info->name);
936
Marek Vasut08d1c422019-02-06 13:19:29 +0100937 fs_close();
938
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100939 return CMD_RET_SUCCESS;
940}
941
Simon Glassed38aef2020-05-10 11:40:03 -0600942int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900943 int fstype)
944{
945 if (argc != 4)
946 return CMD_RET_USAGE;
947
948 if (fs_set_blk_dev(argv[1], argv[2], fstype))
949 return 1;
950
951 if (fs_unlink(argv[3]))
952 return 1;
953
954 return 0;
955}
956
Simon Glassed38aef2020-05-10 11:40:03 -0600957int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900958 int fstype)
959{
960 int ret;
961
962 if (argc != 4)
963 return CMD_RET_USAGE;
964
965 if (fs_set_blk_dev(argv[1], argv[2], fstype))
966 return 1;
967
968 ret = fs_mkdir(argv[3]);
969 if (ret) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000970 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900971 return 1;
972 }
973
974 return 0;
975}
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100976
Simon Glassed38aef2020-05-10 11:40:03 -0600977int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100978 int fstype)
979{
980 if (argc != 5)
981 return CMD_RET_USAGE;
982
983 if (fs_set_blk_dev(argv[1], argv[2], fstype))
984 return 1;
985
986 if (fs_ln(argv[3], argv[4]))
987 return 1;
988
989 return 0;
990}
Niel Fourie9252d702020-03-24 16:17:04 +0100991
992int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
993{
994 struct fstype_info *drv = fstypes;
995 const int n_ents = ARRAY_SIZE(fstypes);
996 struct fstype_info *entry;
997 int i = 0;
998
999 puts("Supported filesystems");
1000 for (entry = drv; entry != drv + n_ents; entry++) {
1001 if (entry->fstype != FS_TYPE_ANY) {
1002 printf("%c %s", i ? ',' : ':', entry->name);
1003 i++;
1004 }
1005 }
1006 if (!i)
1007 puts(": <none>");
1008 puts("\n");
1009 return CMD_RET_SUCCESS;
1010}