blob: 21a23efd932d730fa6893d331992a3aff54ac6c1 [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>
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>
Simon Glasse6c49fc2023-06-01 10:22:38 -060015#include <malloc.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>
Tom Rinidec7ea02024-05-20 13:35:03 -060023#include <time.h>
Hans de Goede690c7962015-09-17 18:46:58 -040024#include <ubifs_uboot.h>
Marek BehĂșn98ec1d12017-09-03 17:00:29 +020025#include <btrfs.h>
Nam Cao15bbb832024-11-07 16:01:06 +010026#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060027#include <asm/global_data.h>
Simon Glasscbe5d5d2012-12-26 09:53:32 +000028#include <asm/io.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050029#include <div64.h>
30#include <linux/math64.h>
Simon Glasse6c49fc2023-06-01 10:22:38 -060031#include <linux/sizes.h>
Mian Yousaf Kaukab961ebe82019-06-18 15:03:44 +020032#include <efi_loader.h>
Joao Marcos Costa29da3742020-07-30 15:33:47 +020033#include <squashfs.h>
Huang Jianan024fb2f2022-02-26 15:05:47 +080034#include <erofs.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000035
Stephen Warren44161af2012-10-30 07:50:47 +000036DECLARE_GLOBAL_DATA_PTR;
37
Simon Glasse3394752016-02-29 15:25:34 -070038static struct blk_desc *fs_dev_desc;
Rob Clark2b7bfd92017-09-09 13:15:55 -040039static int fs_dev_part;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060040static struct disk_partition fs_partition;
Stephen Warreneefbc3f2012-10-22 06:43:51 +000041static int fs_type = FS_TYPE_ANY;
42
Simon Glass896ed4b2022-04-24 23:31:04 -060043void fs_set_type(int type)
44{
45 fs_type = type;
46}
47
Simon Glasse3394752016-02-29 15:25:34 -070048static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -060049 struct disk_partition *fs_partition)
Simon Glass1aede482012-12-26 09:53:29 +000050{
Heinrich Schuchardt3f006d52021-09-20 14:15:03 +020051 log_debug("Unrecognized filesystem type\n");
Simon Glass1aede482012-12-26 09:53:29 +000052 return -1;
53}
54
Stephen Warreneefbc3f2012-10-22 06:43:51 +000055static inline int fs_ls_unsupported(const char *dirname)
56{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000057 return -1;
58}
59
Rob Clark60d74a92017-09-09 13:15:58 -040060/* generic implementation of ls in terms of opendir/readdir/closedir */
61__maybe_unused
62static int fs_ls_generic(const char *dirname)
63{
64 struct fs_dir_stream *dirs;
65 struct fs_dirent *dent;
66 int nfiles = 0, ndirs = 0;
67
68 dirs = fs_opendir(dirname);
69 if (!dirs)
70 return -errno;
71
72 while ((dent = fs_readdir(dirs))) {
73 if (dent->type == FS_DT_DIR) {
74 printf(" %s/\n", dent->name);
75 ndirs++;
Joao Marcos Costa2a7fb152020-07-30 15:33:51 +020076 } else if (dent->type == FS_DT_LNK) {
77 printf(" <SYM> %s\n", dent->name);
78 nfiles++;
Rob Clark60d74a92017-09-09 13:15:58 -040079 } else {
80 printf(" %8lld %s\n", dent->size, dent->name);
81 nfiles++;
82 }
83 }
84
85 fs_closedir(dirs);
86
87 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
88
89 return 0;
90}
91
Stephen Warrend008fbb2014-02-03 13:21:00 -070092static inline int fs_exists_unsupported(const char *filename)
93{
94 return 0;
95}
96
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080097static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -060098{
99 return -1;
100}
101
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000102static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800103 loff_t offset, loff_t len,
104 loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000105{
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000106 return -1;
107}
108
Simon Glasseda14ea2013-04-20 08:42:50 +0000109static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800110 loff_t offset, loff_t len,
111 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000112{
113 return -1;
114}
115
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100116static inline int fs_ln_unsupported(const char *filename, const char *target)
117{
118 return -1;
119}
120
Simon Glass1aede482012-12-26 09:53:29 +0000121static inline void fs_close_unsupported(void)
122{
123}
124
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100125static inline int fs_uuid_unsupported(char *uuid_str)
126{
127 return -1;
128}
129
Rob Clark2b7bfd92017-09-09 13:15:55 -0400130static inline int fs_opendir_unsupported(const char *filename,
131 struct fs_dir_stream **dirs)
132{
133 return -EACCES;
134}
135
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900136static inline int fs_unlink_unsupported(const char *filename)
137{
138 return -1;
139}
140
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900141static inline int fs_mkdir_unsupported(const char *dirname)
142{
143 return -1;
144}
145
Simon Glass1aede482012-12-26 09:53:29 +0000146struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000147 int fstype;
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100148 char *name;
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700149 /*
150 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
151 * should be false in most cases. For "virtual" filesystems which
152 * aren't based on a U-Boot block device (e.g. sandbox), this can be
Heinrich Schuchardt99dc17d2018-08-11 15:52:14 +0200153 * set to true. This should also be true for the dummy entry at the end
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700154 * of fstypes[], since that is essentially a "virtual" (non-existent)
155 * filesystem.
156 */
157 bool null_dev_desc_ok;
Simon Glasse3394752016-02-29 15:25:34 -0700158 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600159 struct disk_partition *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +0000160 int (*ls)(const char *dirname);
Stephen Warrend008fbb2014-02-03 13:21:00 -0700161 int (*exists)(const char *filename);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800162 int (*size)(const char *filename, loff_t *size);
163 int (*read)(const char *filename, void *buf, loff_t offset,
164 loff_t len, loff_t *actread);
165 int (*write)(const char *filename, void *buf, loff_t offset,
166 loff_t len, loff_t *actwrite);
Simon Glass1aede482012-12-26 09:53:29 +0000167 void (*close)(void);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100168 int (*uuid)(char *uuid_str);
Rob Clark2b7bfd92017-09-09 13:15:55 -0400169 /*
170 * Open a directory stream. On success return 0 and directory
171 * stream pointer via 'dirsp'. On error, return -errno. See
172 * fs_opendir().
173 */
174 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
175 /*
176 * Read next entry from directory stream. On success return 0
177 * and directory entry pointer via 'dentp'. On error return
178 * -errno. See fs_readdir().
179 */
180 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
181 /* see fs_closedir() */
182 void (*closedir)(struct fs_dir_stream *dirs);
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900183 int (*unlink)(const char *filename);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900184 int (*mkdir)(const char *dirname);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100185 int (*ln)(const char *filename, const char *target);
Simon Glass1aede482012-12-26 09:53:29 +0000186};
187
188static struct fstype_info fstypes[] = {
Pali RohĂĄrd291f892022-04-05 15:48:32 +0200189#if CONFIG_IS_ENABLED(FS_FAT)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000190 {
191 .fstype = FS_TYPE_FAT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100192 .name = "fat",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700193 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000194 .probe = fat_set_blk_dev,
195 .close = fat_close,
Rob Clark60d74a92017-09-09 13:15:58 -0400196 .ls = fs_ls_generic,
Stephen Warren1d2f9a02014-02-03 13:21:10 -0700197 .exists = fat_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600198 .size = fat_size,
Simon Glass19e38582012-12-26 09:53:33 +0000199 .read = fat_read_file,
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800200#if CONFIG_IS_ENABLED(FAT_WRITE)
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800201 .write = file_fat_write,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900202 .unlink = fat_unlink,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900203 .mkdir = fat_mkdir,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800204#else
Stephen Warren9df25802014-02-03 13:20:59 -0700205 .write = fs_write_unsupported,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900206 .unlink = fs_unlink_unsupported,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900207 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800208#endif
Heinrich Schuchardtfac91832020-12-31 00:38:13 +0100209 .uuid = fat_uuid,
Rob Clark60d74a92017-09-09 13:15:58 -0400210 .opendir = fat_opendir,
211 .readdir = fat_readdir,
212 .closedir = fat_closedir,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100213 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000214 },
Simon Glass1aede482012-12-26 09:53:29 +0000215#endif
Tien Fong Chee64eab042019-01-23 14:20:06 +0800216
217#if CONFIG_IS_ENABLED(FS_EXT4)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000218 {
219 .fstype = FS_TYPE_EXT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100220 .name = "ext4",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700221 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000222 .probe = ext4fs_probe,
223 .close = ext4fs_close,
Heinrich Schuchardt19c781d2024-10-26 08:40:48 +0200224 .ls = fs_ls_generic,
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700225 .exists = ext4fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600226 .size = ext4fs_size,
Simon Glass19e38582012-12-26 09:53:33 +0000227 .read = ext4_read_file,
Baruch Siachaacc5c52024-05-15 07:37:16 +0300228#ifdef CONFIG_EXT4_WRITE
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800229 .write = ext4_write_file,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100230 .ln = ext4fs_create_link,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800231#else
Stephen Warren9df25802014-02-03 13:20:59 -0700232 .write = fs_write_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100233 .ln = fs_ln_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800234#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100235 .uuid = ext4fs_uuid,
Heinrich Schuchardte415b992024-10-26 08:40:46 +0200236 .opendir = ext4fs_opendir,
237 .readdir = ext4fs_readdir,
238 .closedir = ext4fs_closedir,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900239 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900240 .mkdir = fs_mkdir_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000241 },
242#endif
Simon Glass0e84d962024-09-29 19:49:50 -0600243#if IS_ENABLED(CONFIG_SANDBOX) && !IS_ENABLED(CONFIG_XPL_BUILD)
Simon Glass11842872012-12-26 09:53:35 +0000244 {
245 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100246 .name = "sandbox",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700247 .null_dev_desc_ok = true,
Simon Glass11842872012-12-26 09:53:35 +0000248 .probe = sandbox_fs_set_blk_dev,
249 .close = sandbox_fs_close,
250 .ls = sandbox_fs_ls,
Stephen Warren97d66f22014-02-03 13:21:07 -0700251 .exists = sandbox_fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600252 .size = sandbox_fs_size,
Simon Glass11842872012-12-26 09:53:35 +0000253 .read = fs_read_sandbox,
Simon Glassea307e82013-04-20 08:42:51 +0000254 .write = fs_write_sandbox,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100255 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400256 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900257 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900258 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100259 .ln = fs_ln_unsupported,
Simon Glass11842872012-12-26 09:53:35 +0000260 },
261#endif
Mayuresh Chitale8fd51842023-11-16 22:46:12 +0530262#if CONFIG_IS_ENABLED(SEMIHOSTING)
Sean Andersonc678b322022-03-22 16:59:20 -0400263 {
264 .fstype = FS_TYPE_SEMIHOSTING,
265 .name = "semihosting",
266 .null_dev_desc_ok = true,
267 .probe = smh_fs_set_blk_dev,
268 .close = fs_close_unsupported,
269 .ls = fs_ls_unsupported,
270 .exists = fs_exists_unsupported,
271 .size = smh_fs_size,
272 .read = smh_fs_read,
273 .write = smh_fs_write,
274 .uuid = fs_uuid_unsupported,
275 .opendir = fs_opendir_unsupported,
276 .unlink = fs_unlink_unsupported,
277 .mkdir = fs_mkdir_unsupported,
278 .ln = fs_ln_unsupported,
279 },
280#endif
Simon Glass0e84d962024-09-29 19:49:50 -0600281#ifndef CONFIG_XPL_BUILD
Hans de Goede690c7962015-09-17 18:46:58 -0400282#ifdef CONFIG_CMD_UBIFS
283 {
284 .fstype = FS_TYPE_UBIFS,
285 .name = "ubifs",
286 .null_dev_desc_ok = true,
287 .probe = ubifs_set_blk_dev,
288 .close = ubifs_close,
289 .ls = ubifs_ls,
290 .exists = ubifs_exists,
291 .size = ubifs_size,
292 .read = ubifs_read,
293 .write = fs_write_unsupported,
294 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400295 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900296 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900297 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100298 .ln = fs_ln_unsupported,
Hans de Goede690c7962015-09-17 18:46:58 -0400299 },
300#endif
Pali RohĂĄrd3a3c122022-04-05 15:49:24 +0200301#endif
Simon Glass0e84d962024-09-29 19:49:50 -0600302#ifndef CONFIG_XPL_BUILD
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200303#ifdef CONFIG_FS_BTRFS
304 {
305 .fstype = FS_TYPE_BTRFS,
306 .name = "btrfs",
307 .null_dev_desc_ok = false,
308 .probe = btrfs_probe,
309 .close = btrfs_close,
310 .ls = btrfs_ls,
311 .exists = btrfs_exists,
312 .size = btrfs_size,
313 .read = btrfs_read,
314 .write = fs_write_unsupported,
315 .uuid = btrfs_uuid,
Marek BehĂșn90762d92017-10-06 16:56:07 +0200316 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900317 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900318 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100319 .ln = fs_ln_unsupported,
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200320 },
321#endif
Pali RohĂĄr849a61a2022-04-07 14:53:25 +0200322#endif
Pali RohĂĄr1660e962022-04-06 23:34:00 +0200323#if CONFIG_IS_ENABLED(FS_SQUASHFS)
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200324 {
325 .fstype = FS_TYPE_SQUASHFS,
326 .name = "squashfs",
Richard Genoud654b2482020-11-03 12:10:59 +0100327 .null_dev_desc_ok = false,
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200328 .probe = sqfs_probe,
329 .opendir = sqfs_opendir,
330 .readdir = sqfs_readdir,
331 .ls = fs_ls_generic,
332 .read = sqfs_read,
333 .size = sqfs_size,
334 .close = sqfs_close,
335 .closedir = sqfs_closedir,
Richard Genoudcfca20c2020-11-03 12:11:26 +0100336 .exists = sqfs_exists,
Richard Genoud654b2482020-11-03 12:10:59 +0100337 .uuid = fs_uuid_unsupported,
338 .write = fs_write_unsupported,
339 .ln = fs_ln_unsupported,
340 .unlink = fs_unlink_unsupported,
341 .mkdir = fs_mkdir_unsupported,
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200342 },
343#endif
Huang Jianan024fb2f2022-02-26 15:05:47 +0800344#if IS_ENABLED(CONFIG_FS_EROFS)
345 {
346 .fstype = FS_TYPE_EROFS,
347 .name = "erofs",
348 .null_dev_desc_ok = false,
349 .probe = erofs_probe,
350 .opendir = erofs_opendir,
351 .readdir = erofs_readdir,
352 .ls = fs_ls_generic,
353 .read = erofs_read,
354 .size = erofs_size,
355 .close = erofs_close,
356 .closedir = erofs_closedir,
357 .exists = erofs_exists,
358 .uuid = fs_uuid_unsupported,
359 .write = fs_write_unsupported,
360 .ln = fs_ln_unsupported,
361 .unlink = fs_unlink_unsupported,
362 .mkdir = fs_mkdir_unsupported,
363 },
364#endif
Simon Glass1aede482012-12-26 09:53:29 +0000365 {
366 .fstype = FS_TYPE_ANY,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100367 .name = "unsupported",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700368 .null_dev_desc_ok = true,
Simon Glass1aede482012-12-26 09:53:29 +0000369 .probe = fs_probe_unsupported,
370 .close = fs_close_unsupported,
371 .ls = fs_ls_unsupported,
Stephen Warrend008fbb2014-02-03 13:21:00 -0700372 .exists = fs_exists_unsupported,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600373 .size = fs_size_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000374 .read = fs_read_unsupported,
Simon Glasseda14ea2013-04-20 08:42:50 +0000375 .write = fs_write_unsupported,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100376 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400377 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900378 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900379 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100380 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000381 },
382};
383
Simon Glasse6aad852012-12-26 09:53:30 +0000384static struct fstype_info *fs_get_info(int fstype)
385{
386 struct fstype_info *info;
387 int i;
388
389 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
390 if (fstype == info->fstype)
391 return info;
392 }
393
394 /* Return the 'unsupported' sentinel */
395 return info;
396}
397
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000398/**
AKASHI Takahiro80ac7342019-10-07 14:59:37 +0900399 * fs_get_type() - Get type of current filesystem
400 *
401 * Return: filesystem type
402 *
403 * Returns filesystem type representing the current filesystem, or
404 * FS_TYPE_ANY for any unrecognised filesystem.
405 */
406int fs_get_type(void)
407{
408 return fs_type;
409}
410
411/**
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000412 * fs_get_type_name() - Get type of current filesystem
413 *
414 * Return: Pointer to filesystem name
415 *
416 * Returns a string describing the current filesystem, or the sentinel
417 * "unsupported" for any unrecognised filesystem.
418 */
419const char *fs_get_type_name(void)
420{
421 return fs_get_info(fs_type)->name;
422}
423
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000424int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
425{
Simon Glass1aede482012-12-26 09:53:29 +0000426 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000427 int part, i;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000428
Sean Andersonf476b4a2021-04-12 18:53:05 -0400429 part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
430 &fs_partition, 1);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000431 if (part < 0)
432 return -1;
433
Simon Glass1aede482012-12-26 09:53:29 +0000434 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
435 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
436 fstype != info->fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000437 continue;
438
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700439 if (!fs_dev_desc && !info->null_dev_desc_ok)
440 continue;
441
Simon Glass6a46e2f2012-12-26 09:53:31 +0000442 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000443 fs_type = info->fstype;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400444 fs_dev_part = part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000445 return 0;
446 }
447 }
448
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000449 return -1;
450}
451
Rob Clark2b7bfd92017-09-09 13:15:55 -0400452/* set current blk device w/ blk_desc + partition # */
453int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
454{
455 struct fstype_info *info;
456 int ret, i;
457
458 if (part >= 1)
459 ret = part_get_info(desc, part, &fs_partition);
460 else
461 ret = part_get_info_whole_disk(desc, &fs_partition);
462 if (ret)
463 return ret;
464 fs_dev_desc = desc;
465
466 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
467 if (!info->probe(fs_dev_desc, &fs_partition)) {
468 fs_type = info->fstype;
AKASHI Takahiro43efaa02018-10-17 16:32:02 +0900469 fs_dev_part = part;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400470 return 0;
471 }
472 }
473
474 return -1;
475}
476
AKASHI Takahiroca8e3772019-10-07 14:59:35 +0900477void fs_close(void)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000478{
Simon Glasse6aad852012-12-26 09:53:30 +0000479 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000480
Simon Glasse6aad852012-12-26 09:53:30 +0000481 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000482
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000483 fs_type = FS_TYPE_ANY;
484}
485
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100486int fs_uuid(char *uuid_str)
487{
488 struct fstype_info *info = fs_get_info(fs_type);
489
490 return info->uuid(uuid_str);
491}
492
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000493int fs_ls(const char *dirname)
494{
495 int ret;
496
Simon Glasse6aad852012-12-26 09:53:30 +0000497 struct fstype_info *info = fs_get_info(fs_type);
498
499 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000500
501 fs_close();
502
503 return ret;
504}
505
Stephen Warrend008fbb2014-02-03 13:21:00 -0700506int fs_exists(const char *filename)
507{
508 int ret;
509
510 struct fstype_info *info = fs_get_info(fs_type);
511
512 ret = info->exists(filename);
513
514 fs_close();
515
516 return ret;
517}
518
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800519int fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600520{
521 int ret;
522
523 struct fstype_info *info = fs_get_info(fs_type);
524
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800525 ret = info->size(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600526
527 fs_close();
528
529 return ret;
530}
531
Sughosh Ganu0d733f02024-08-26 17:29:22 +0530532#if CONFIG_IS_ENABLED(LMB)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100533/* Check if a file may be read to the given address */
534static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
535 loff_t len, struct fstype_info *info)
536{
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100537 int ret;
538 loff_t size;
539 loff_t read_len;
540
541 /* get the actual size of the file */
542 ret = info->size(filename, &size);
543 if (ret)
544 return ret;
545 if (offset >= size) {
546 /* offset >= EOF, no bytes will be written */
547 return 0;
548 }
549 read_len = size - offset;
550
551 /* limit to 'len' if it is smaller */
552 if (len && len < read_len)
553 read_len = len;
554
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530555 lmb_dump_all();
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100556
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530557 if (lmb_alloc_addr(addr, read_len) == addr)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100558 return 0;
559
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000560 log_err("** Reading file would overwrite reserved memory **\n");
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100561 return -ENOSPC;
562}
563#endif
564
565static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
566 int do_lmb_check, loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000567{
Simon Glasse6aad852012-12-26 09:53:30 +0000568 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000569 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000570 int ret;
571
Sughosh Ganu0d733f02024-08-26 17:29:22 +0530572#if CONFIG_IS_ENABLED(LMB)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100573 if (do_lmb_check) {
574 ret = fs_read_lmb_check(filename, addr, offset, len, info);
575 if (ret)
576 return ret;
577 }
578#endif
579
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000580 /*
581 * We don't actually know how many bytes are being read, since len==0
582 * means read the whole file.
583 */
584 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800585 ret = info->read(filename, buf, offset, len, actread);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000586 unmap_sysmem(buf);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000587
Simon Glasse6aad852012-12-26 09:53:30 +0000588 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher56d9cdf2015-08-05 17:16:58 +0200589 if (ret == 0 && len && *actread != len)
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000590 log_debug("** %s shorter than offset + len **\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000591 fs_close();
592
593 return ret;
594}
595
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100596int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
597 loff_t *actread)
598{
599 return _fs_read(filename, addr, offset, len, 0, actread);
600}
601
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800602int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
603 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000604{
605 struct fstype_info *info = fs_get_info(fs_type);
606 void *buf;
607 int ret;
608
Simon Glasseda14ea2013-04-20 08:42:50 +0000609 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800610 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glasseda14ea2013-04-20 08:42:50 +0000611 unmap_sysmem(buf);
612
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800613 if (ret < 0 && len != *actwrite) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000614 log_err("** Unable to write file %s **\n", filename);
Simon Glasseda14ea2013-04-20 08:42:50 +0000615 ret = -1;
616 }
617 fs_close();
618
619 return ret;
620}
621
Rob Clark2b7bfd92017-09-09 13:15:55 -0400622struct fs_dir_stream *fs_opendir(const char *filename)
623{
624 struct fstype_info *info = fs_get_info(fs_type);
625 struct fs_dir_stream *dirs = NULL;
626 int ret;
627
628 ret = info->opendir(filename, &dirs);
629 fs_close();
630 if (ret) {
631 errno = -ret;
632 return NULL;
633 }
634
635 dirs->desc = fs_dev_desc;
636 dirs->part = fs_dev_part;
637
638 return dirs;
639}
640
641struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
642{
643 struct fstype_info *info;
644 struct fs_dirent *dirent;
645 int ret;
646
647 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
648 info = fs_get_info(fs_type);
649
650 ret = info->readdir(dirs, &dirent);
651 fs_close();
652 if (ret) {
653 errno = -ret;
654 return NULL;
655 }
656
657 return dirent;
658}
659
660void fs_closedir(struct fs_dir_stream *dirs)
661{
662 struct fstype_info *info;
663
664 if (!dirs)
665 return;
666
667 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
668 info = fs_get_info(fs_type);
669
670 info->closedir(dirs);
671 fs_close();
672}
673
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900674int fs_unlink(const char *filename)
675{
676 int ret;
677
678 struct fstype_info *info = fs_get_info(fs_type);
679
680 ret = info->unlink(filename);
681
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900682 fs_close();
683
684 return ret;
685}
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900686
687int fs_mkdir(const char *dirname)
688{
689 int ret;
690
691 struct fstype_info *info = fs_get_info(fs_type);
692
693 ret = info->mkdir(dirname);
694
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900695 fs_close();
696
697 return ret;
698}
Rob Clark2b7bfd92017-09-09 13:15:55 -0400699
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100700int fs_ln(const char *fname, const char *target)
701{
702 struct fstype_info *info = fs_get_info(fs_type);
703 int ret;
704
705 ret = info->ln(fname, target);
706
707 if (ret < 0) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000708 log_err("** Unable to create link %s -> %s **\n", fname, target);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100709 ret = -1;
710 }
711 fs_close();
712
713 return ret;
714}
715
Simon Glassed38aef2020-05-10 11:40:03 -0600716int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
717 int fstype)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600718{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800719 loff_t size;
Stephen Warren3eb58f52014-06-11 12:47:26 -0600720
721 if (argc != 4)
722 return CMD_RET_USAGE;
723
724 if (fs_set_blk_dev(argv[1], argv[2], fstype))
725 return 1;
726
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800727 if (fs_size(argv[3], &size) < 0)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600728 return CMD_RET_FAILURE;
729
Simon Glass4d949a22017-08-03 12:22:10 -0600730 env_set_hex("filesize", size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600731
732 return 0;
733}
734
Simon Glassed38aef2020-05-10 11:40:03 -0600735int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
736 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000737{
738 unsigned long addr;
739 const char *addr_str;
740 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800741 loff_t bytes;
742 loff_t pos;
743 loff_t len_read;
744 int ret;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100745 unsigned long time;
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200746 char *ep;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000747
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000748 if (argc < 2)
749 return CMD_RET_USAGE;
750 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000751 return CMD_RET_USAGE;
752
Simon Glass793a98e2023-11-18 14:05:20 -0700753 if (fs_set_blk_dev(argv[1], cmd_arg2(argc, argv), fstype)) {
Heinrich Schuchardtc556a452021-01-27 21:26:43 +0100754 log_err("Can't set block device\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000755 return 1;
Heinrich Schuchardtc556a452021-01-27 21:26:43 +0100756 }
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000757
758 if (argc >= 4) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600759 addr = hextoul(argv[3], &ep);
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200760 if (ep == argv[3] || *ep != '\0')
761 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000762 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600763 addr_str = env_get("loadaddr");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000764 if (addr_str != NULL)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600765 addr = hextoul(addr_str, NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000766 else
767 addr = CONFIG_SYS_LOAD_ADDR;
768 }
769 if (argc >= 5) {
770 filename = argv[4];
771 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600772 filename = env_get("bootfile");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000773 if (!filename) {
774 puts("** No boot file defined **\n");
775 return 1;
776 }
777 }
778 if (argc >= 6)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600779 bytes = hextoul(argv[5], NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000780 else
781 bytes = 0;
782 if (argc >= 7)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600783 pos = hextoul(argv[6], NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000784 else
785 pos = 0;
786
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100787 time = get_timer(0);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100788 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100789 time = get_timer(time);
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200790 if (ret < 0) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000791 log_err("Failed to load '%s'\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000792 return 1;
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200793 }
794
AKASHI Takahirod4c99462024-01-17 13:39:44 +0900795 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
796 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
797 len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000798
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800799 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100800 if (time > 0) {
801 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500802 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100803 puts(")");
804 }
805 puts("\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000806
Simon Glass4d949a22017-08-03 12:22:10 -0600807 env_set_hex("fileaddr", addr);
808 env_set_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000809
810 return 0;
811}
812
Simon Glassed38aef2020-05-10 11:40:03 -0600813int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
814 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000815{
816 if (argc < 2)
817 return CMD_RET_USAGE;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000818 if (argc > 4)
819 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000820
Simon Glass793a98e2023-11-18 14:05:20 -0700821 if (fs_set_blk_dev(argv[1], cmd_arg2(argc, argv), fstype))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000822 return 1;
823
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000824 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000825 return 1;
826
827 return 0;
828}
Simon Glasseda14ea2013-04-20 08:42:50 +0000829
Stephen Warrend008fbb2014-02-03 13:21:00 -0700830int file_exists(const char *dev_type, const char *dev_part, const char *file,
831 int fstype)
832{
833 if (fs_set_blk_dev(dev_type, dev_part, fstype))
834 return 0;
835
836 return fs_exists(file);
837}
838
Simon Glassed38aef2020-05-10 11:40:03 -0600839int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
840 int fstype)
Simon Glasseda14ea2013-04-20 08:42:50 +0000841{
842 unsigned long addr;
843 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800844 loff_t bytes;
845 loff_t pos;
846 loff_t len;
847 int ret;
Simon Glasseda14ea2013-04-20 08:42:50 +0000848 unsigned long time;
849
850 if (argc < 6 || argc > 7)
851 return CMD_RET_USAGE;
852
853 if (fs_set_blk_dev(argv[1], argv[2], fstype))
854 return 1;
855
Simon Glass3ff49ec2021-07-24 09:03:29 -0600856 addr = hextoul(argv[3], NULL);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800857 filename = argv[4];
Simon Glass3ff49ec2021-07-24 09:03:29 -0600858 bytes = hextoul(argv[5], NULL);
Simon Glasseda14ea2013-04-20 08:42:50 +0000859 if (argc >= 7)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600860 pos = hextoul(argv[6], NULL);
Simon Glasseda14ea2013-04-20 08:42:50 +0000861 else
862 pos = 0;
863
864 time = get_timer(0);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800865 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glasseda14ea2013-04-20 08:42:50 +0000866 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800867 if (ret < 0)
Simon Glasseda14ea2013-04-20 08:42:50 +0000868 return 1;
869
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800870 printf("%llu bytes written in %lu ms", len, time);
Simon Glasseda14ea2013-04-20 08:42:50 +0000871 if (time > 0) {
872 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500873 print_size(div_u64(len, time) * 1000, "/s");
Simon Glasseda14ea2013-04-20 08:42:50 +0000874 puts(")");
875 }
876 puts("\n");
877
878 return 0;
879}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100880
Simon Glassed38aef2020-05-10 11:40:03 -0600881int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
882 int fstype)
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100883{
884 int ret;
885 char uuid[37];
886 memset(uuid, 0, sizeof(uuid));
887
888 if (argc < 3 || argc > 4)
889 return CMD_RET_USAGE;
890
891 if (fs_set_blk_dev(argv[1], argv[2], fstype))
892 return 1;
893
894 ret = fs_uuid(uuid);
895 if (ret)
896 return CMD_RET_FAILURE;
897
898 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600899 env_set(argv[3], uuid);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100900 else
901 printf("%s\n", uuid);
902
903 return CMD_RET_SUCCESS;
904}
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100905
Simon Glassed38aef2020-05-10 11:40:03 -0600906int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100907{
908 struct fstype_info *info;
909
910 if (argc < 3 || argc > 4)
911 return CMD_RET_USAGE;
912
913 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
914 return 1;
915
916 info = fs_get_info(fs_type);
917
918 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600919 env_set(argv[3], info->name);
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100920 else
921 printf("%s\n", info->name);
922
Marek Vasut08d1c422019-02-06 13:19:29 +0100923 fs_close();
924
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100925 return CMD_RET_SUCCESS;
926}
927
Simon Glassed38aef2020-05-10 11:40:03 -0600928int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900929 int fstype)
930{
931 if (argc != 4)
932 return CMD_RET_USAGE;
933
934 if (fs_set_blk_dev(argv[1], argv[2], fstype))
935 return 1;
936
937 if (fs_unlink(argv[3]))
938 return 1;
939
940 return 0;
941}
942
Simon Glassed38aef2020-05-10 11:40:03 -0600943int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900944 int fstype)
945{
946 int ret;
947
948 if (argc != 4)
949 return CMD_RET_USAGE;
950
951 if (fs_set_blk_dev(argv[1], argv[2], fstype))
952 return 1;
953
954 ret = fs_mkdir(argv[3]);
955 if (ret) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000956 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900957 return 1;
958 }
959
960 return 0;
961}
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100962
Simon Glassed38aef2020-05-10 11:40:03 -0600963int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100964 int fstype)
965{
966 if (argc != 5)
967 return CMD_RET_USAGE;
968
969 if (fs_set_blk_dev(argv[1], argv[2], fstype))
970 return 1;
971
972 if (fs_ln(argv[3], argv[4]))
973 return 1;
974
975 return 0;
976}
Niel Fourie9252d702020-03-24 16:17:04 +0100977
978int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
979{
980 struct fstype_info *drv = fstypes;
981 const int n_ents = ARRAY_SIZE(fstypes);
982 struct fstype_info *entry;
983 int i = 0;
984
985 puts("Supported filesystems");
986 for (entry = drv; entry != drv + n_ents; entry++) {
987 if (entry->fstype != FS_TYPE_ANY) {
988 printf("%c %s", i ? ',' : ':', entry->name);
989 i++;
990 }
991 }
992 if (!i)
993 puts(": <none>");
994 puts("\n");
995 return CMD_RET_SUCCESS;
996}
Simon Glasse6c49fc2023-06-01 10:22:38 -0600997
998int fs_read_alloc(const char *fname, ulong size, uint align, void **bufp)
999{
1000 loff_t bytes_read;
1001 ulong addr;
1002 char *buf;
1003 int ret;
1004
Nam Cao15bbb832024-11-07 16:01:06 +01001005 if (!align)
1006 align = ARCH_DMA_MINALIGN;
1007
Simon Glasse6c49fc2023-06-01 10:22:38 -06001008 buf = memalign(align, size + 1);
1009 if (!buf)
1010 return log_msg_ret("buf", -ENOMEM);
1011 addr = map_to_sysmem(buf);
1012
1013 ret = fs_read(fname, addr, 0, size, &bytes_read);
1014 if (ret) {
1015 free(buf);
1016 return log_msg_ret("read", ret);
1017 }
1018 if (size != bytes_read)
1019 return log_msg_ret("bread", -EIO);
1020 buf[size] = '\0';
1021
1022 *bufp = buf;
1023
1024 return 0;
1025}
1026
1027int fs_load_alloc(const char *ifname, const char *dev_part_str,
1028 const char *fname, ulong max_size, ulong align, void **bufp,
1029 ulong *sizep)
1030{
1031 loff_t size;
1032 void *buf;
1033 int ret;
1034
1035 if (fs_set_blk_dev(ifname, dev_part_str, FS_TYPE_ANY))
1036 return log_msg_ret("set", -ENOMEDIUM);
1037
1038 ret = fs_size(fname, &size);
1039 if (ret)
1040 return log_msg_ret("sz", -ENOENT);
1041
1042 if (size >= (max_size ?: SZ_1G))
1043 return log_msg_ret("sz", -E2BIG);
1044
1045 if (fs_set_blk_dev(ifname, dev_part_str, FS_TYPE_ANY))
1046 return log_msg_ret("set", -ENOMEDIUM);
1047
1048 ret = fs_read_alloc(fname, size, align, &buf);
1049 if (ret)
1050 return log_msg_ret("al", ret);
1051 *sizep = size;
1052 *bufp = buf;
1053
1054 return 0;
1055}