blob: 1afa0fbeaed9bebb5001c6f0587f94b7719ae57c [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060026#include <asm/global_data.h>
Simon Glasscbe5d5d2012-12-26 09:53:32 +000027#include <asm/io.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050028#include <div64.h>
29#include <linux/math64.h>
Simon Glasse6c49fc2023-06-01 10:22:38 -060030#include <linux/sizes.h>
Mian Yousaf Kaukab961ebe82019-06-18 15:03:44 +020031#include <efi_loader.h>
Joao Marcos Costa29da3742020-07-30 15:33:47 +020032#include <squashfs.h>
Huang Jianan024fb2f2022-02-26 15:05:47 +080033#include <erofs.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000034
Stephen Warren44161af2012-10-30 07:50:47 +000035DECLARE_GLOBAL_DATA_PTR;
36
Simon Glasse3394752016-02-29 15:25:34 -070037static struct blk_desc *fs_dev_desc;
Rob Clark2b7bfd92017-09-09 13:15:55 -040038static int fs_dev_part;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060039static struct disk_partition fs_partition;
Stephen Warreneefbc3f2012-10-22 06:43:51 +000040static int fs_type = FS_TYPE_ANY;
41
Simon Glass896ed4b2022-04-24 23:31:04 -060042void fs_set_type(int type)
43{
44 fs_type = type;
45}
46
Simon Glasse3394752016-02-29 15:25:34 -070047static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -060048 struct disk_partition *fs_partition)
Simon Glass1aede482012-12-26 09:53:29 +000049{
Heinrich Schuchardt3f006d52021-09-20 14:15:03 +020050 log_debug("Unrecognized filesystem type\n");
Simon Glass1aede482012-12-26 09:53:29 +000051 return -1;
52}
53
Stephen Warreneefbc3f2012-10-22 06:43:51 +000054static inline int fs_ls_unsupported(const char *dirname)
55{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000056 return -1;
57}
58
Rob Clark60d74a92017-09-09 13:15:58 -040059/* generic implementation of ls in terms of opendir/readdir/closedir */
60__maybe_unused
61static int fs_ls_generic(const char *dirname)
62{
63 struct fs_dir_stream *dirs;
64 struct fs_dirent *dent;
65 int nfiles = 0, ndirs = 0;
66
67 dirs = fs_opendir(dirname);
68 if (!dirs)
69 return -errno;
70
71 while ((dent = fs_readdir(dirs))) {
72 if (dent->type == FS_DT_DIR) {
73 printf(" %s/\n", dent->name);
74 ndirs++;
Joao Marcos Costa2a7fb152020-07-30 15:33:51 +020075 } else if (dent->type == FS_DT_LNK) {
76 printf(" <SYM> %s\n", dent->name);
77 nfiles++;
Rob Clark60d74a92017-09-09 13:15:58 -040078 } else {
79 printf(" %8lld %s\n", dent->size, dent->name);
80 nfiles++;
81 }
82 }
83
84 fs_closedir(dirs);
85
86 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
87
88 return 0;
89}
90
Stephen Warrend008fbb2014-02-03 13:21:00 -070091static inline int fs_exists_unsupported(const char *filename)
92{
93 return 0;
94}
95
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080096static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -060097{
98 return -1;
99}
100
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000101static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800102 loff_t offset, loff_t len,
103 loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000104{
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000105 return -1;
106}
107
Simon Glasseda14ea2013-04-20 08:42:50 +0000108static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800109 loff_t offset, loff_t len,
110 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000111{
112 return -1;
113}
114
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100115static inline int fs_ln_unsupported(const char *filename, const char *target)
116{
117 return -1;
118}
119
Simon Glass1aede482012-12-26 09:53:29 +0000120static inline void fs_close_unsupported(void)
121{
122}
123
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100124static inline int fs_uuid_unsupported(char *uuid_str)
125{
126 return -1;
127}
128
Rob Clark2b7bfd92017-09-09 13:15:55 -0400129static inline int fs_opendir_unsupported(const char *filename,
130 struct fs_dir_stream **dirs)
131{
132 return -EACCES;
133}
134
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900135static inline int fs_unlink_unsupported(const char *filename)
136{
137 return -1;
138}
139
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900140static inline int fs_mkdir_unsupported(const char *dirname)
141{
142 return -1;
143}
144
Simon Glass1aede482012-12-26 09:53:29 +0000145struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000146 int fstype;
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100147 char *name;
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700148 /*
149 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
150 * should be false in most cases. For "virtual" filesystems which
151 * aren't based on a U-Boot block device (e.g. sandbox), this can be
Heinrich Schuchardt99dc17d2018-08-11 15:52:14 +0200152 * set to true. This should also be true for the dummy entry at the end
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700153 * of fstypes[], since that is essentially a "virtual" (non-existent)
154 * filesystem.
155 */
156 bool null_dev_desc_ok;
Simon Glasse3394752016-02-29 15:25:34 -0700157 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600158 struct disk_partition *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +0000159 int (*ls)(const char *dirname);
Stephen Warrend008fbb2014-02-03 13:21:00 -0700160 int (*exists)(const char *filename);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800161 int (*size)(const char *filename, loff_t *size);
162 int (*read)(const char *filename, void *buf, loff_t offset,
163 loff_t len, loff_t *actread);
164 int (*write)(const char *filename, void *buf, loff_t offset,
165 loff_t len, loff_t *actwrite);
Simon Glass1aede482012-12-26 09:53:29 +0000166 void (*close)(void);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100167 int (*uuid)(char *uuid_str);
Rob Clark2b7bfd92017-09-09 13:15:55 -0400168 /*
169 * Open a directory stream. On success return 0 and directory
170 * stream pointer via 'dirsp'. On error, return -errno. See
171 * fs_opendir().
172 */
173 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
174 /*
175 * Read next entry from directory stream. On success return 0
176 * and directory entry pointer via 'dentp'. On error return
177 * -errno. See fs_readdir().
178 */
179 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
180 /* see fs_closedir() */
181 void (*closedir)(struct fs_dir_stream *dirs);
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900182 int (*unlink)(const char *filename);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900183 int (*mkdir)(const char *dirname);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100184 int (*ln)(const char *filename, const char *target);
Simon Glass1aede482012-12-26 09:53:29 +0000185};
186
187static struct fstype_info fstypes[] = {
Pali RohĂĄrd291f892022-04-05 15:48:32 +0200188#if CONFIG_IS_ENABLED(FS_FAT)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000189 {
190 .fstype = FS_TYPE_FAT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100191 .name = "fat",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700192 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000193 .probe = fat_set_blk_dev,
194 .close = fat_close,
Rob Clark60d74a92017-09-09 13:15:58 -0400195 .ls = fs_ls_generic,
Stephen Warren1d2f9a02014-02-03 13:21:10 -0700196 .exists = fat_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600197 .size = fat_size,
Simon Glass19e38582012-12-26 09:53:33 +0000198 .read = fat_read_file,
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800199#if CONFIG_IS_ENABLED(FAT_WRITE)
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800200 .write = file_fat_write,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900201 .unlink = fat_unlink,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900202 .mkdir = fat_mkdir,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800203#else
Stephen Warren9df25802014-02-03 13:20:59 -0700204 .write = fs_write_unsupported,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900205 .unlink = fs_unlink_unsupported,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900206 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800207#endif
Heinrich Schuchardtfac91832020-12-31 00:38:13 +0100208 .uuid = fat_uuid,
Rob Clark60d74a92017-09-09 13:15:58 -0400209 .opendir = fat_opendir,
210 .readdir = fat_readdir,
211 .closedir = fat_closedir,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100212 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000213 },
Simon Glass1aede482012-12-26 09:53:29 +0000214#endif
Tien Fong Chee64eab042019-01-23 14:20:06 +0800215
216#if CONFIG_IS_ENABLED(FS_EXT4)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000217 {
218 .fstype = FS_TYPE_EXT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100219 .name = "ext4",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700220 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000221 .probe = ext4fs_probe,
222 .close = ext4fs_close,
Heinrich Schuchardt19c781d2024-10-26 08:40:48 +0200223 .ls = fs_ls_generic,
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700224 .exists = ext4fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600225 .size = ext4fs_size,
Simon Glass19e38582012-12-26 09:53:33 +0000226 .read = ext4_read_file,
Baruch Siachaacc5c52024-05-15 07:37:16 +0300227#ifdef CONFIG_EXT4_WRITE
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800228 .write = ext4_write_file,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100229 .ln = ext4fs_create_link,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800230#else
Stephen Warren9df25802014-02-03 13:20:59 -0700231 .write = fs_write_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100232 .ln = fs_ln_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800233#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100234 .uuid = ext4fs_uuid,
Heinrich Schuchardte415b992024-10-26 08:40:46 +0200235 .opendir = ext4fs_opendir,
236 .readdir = ext4fs_readdir,
237 .closedir = ext4fs_closedir,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900238 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900239 .mkdir = fs_mkdir_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000240 },
241#endif
Simon Glass0e84d962024-09-29 19:49:50 -0600242#if IS_ENABLED(CONFIG_SANDBOX) && !IS_ENABLED(CONFIG_XPL_BUILD)
Simon Glass11842872012-12-26 09:53:35 +0000243 {
244 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100245 .name = "sandbox",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700246 .null_dev_desc_ok = true,
Simon Glass11842872012-12-26 09:53:35 +0000247 .probe = sandbox_fs_set_blk_dev,
248 .close = sandbox_fs_close,
249 .ls = sandbox_fs_ls,
Stephen Warren97d66f22014-02-03 13:21:07 -0700250 .exists = sandbox_fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600251 .size = sandbox_fs_size,
Simon Glass11842872012-12-26 09:53:35 +0000252 .read = fs_read_sandbox,
Simon Glassea307e82013-04-20 08:42:51 +0000253 .write = fs_write_sandbox,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100254 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400255 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900256 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900257 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100258 .ln = fs_ln_unsupported,
Simon Glass11842872012-12-26 09:53:35 +0000259 },
260#endif
Mayuresh Chitale8fd51842023-11-16 22:46:12 +0530261#if CONFIG_IS_ENABLED(SEMIHOSTING)
Sean Andersonc678b322022-03-22 16:59:20 -0400262 {
263 .fstype = FS_TYPE_SEMIHOSTING,
264 .name = "semihosting",
265 .null_dev_desc_ok = true,
266 .probe = smh_fs_set_blk_dev,
267 .close = fs_close_unsupported,
268 .ls = fs_ls_unsupported,
269 .exists = fs_exists_unsupported,
270 .size = smh_fs_size,
271 .read = smh_fs_read,
272 .write = smh_fs_write,
273 .uuid = fs_uuid_unsupported,
274 .opendir = fs_opendir_unsupported,
275 .unlink = fs_unlink_unsupported,
276 .mkdir = fs_mkdir_unsupported,
277 .ln = fs_ln_unsupported,
278 },
279#endif
Simon Glass0e84d962024-09-29 19:49:50 -0600280#ifndef CONFIG_XPL_BUILD
Hans de Goede690c7962015-09-17 18:46:58 -0400281#ifdef CONFIG_CMD_UBIFS
282 {
283 .fstype = FS_TYPE_UBIFS,
284 .name = "ubifs",
285 .null_dev_desc_ok = true,
286 .probe = ubifs_set_blk_dev,
287 .close = ubifs_close,
288 .ls = ubifs_ls,
289 .exists = ubifs_exists,
290 .size = ubifs_size,
291 .read = ubifs_read,
292 .write = fs_write_unsupported,
293 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400294 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900295 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900296 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100297 .ln = fs_ln_unsupported,
Hans de Goede690c7962015-09-17 18:46:58 -0400298 },
299#endif
Pali RohĂĄrd3a3c122022-04-05 15:49:24 +0200300#endif
Simon Glass0e84d962024-09-29 19:49:50 -0600301#ifndef CONFIG_XPL_BUILD
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200302#ifdef CONFIG_FS_BTRFS
303 {
304 .fstype = FS_TYPE_BTRFS,
305 .name = "btrfs",
306 .null_dev_desc_ok = false,
307 .probe = btrfs_probe,
308 .close = btrfs_close,
309 .ls = btrfs_ls,
310 .exists = btrfs_exists,
311 .size = btrfs_size,
312 .read = btrfs_read,
313 .write = fs_write_unsupported,
314 .uuid = btrfs_uuid,
Marek BehĂșn90762d92017-10-06 16:56:07 +0200315 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900316 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900317 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100318 .ln = fs_ln_unsupported,
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200319 },
320#endif
Pali RohĂĄr849a61a2022-04-07 14:53:25 +0200321#endif
Pali RohĂĄr1660e962022-04-06 23:34:00 +0200322#if CONFIG_IS_ENABLED(FS_SQUASHFS)
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200323 {
324 .fstype = FS_TYPE_SQUASHFS,
325 .name = "squashfs",
Richard Genoud654b2482020-11-03 12:10:59 +0100326 .null_dev_desc_ok = false,
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200327 .probe = sqfs_probe,
328 .opendir = sqfs_opendir,
329 .readdir = sqfs_readdir,
330 .ls = fs_ls_generic,
331 .read = sqfs_read,
332 .size = sqfs_size,
333 .close = sqfs_close,
334 .closedir = sqfs_closedir,
Richard Genoudcfca20c2020-11-03 12:11:26 +0100335 .exists = sqfs_exists,
Richard Genoud654b2482020-11-03 12:10:59 +0100336 .uuid = fs_uuid_unsupported,
337 .write = fs_write_unsupported,
338 .ln = fs_ln_unsupported,
339 .unlink = fs_unlink_unsupported,
340 .mkdir = fs_mkdir_unsupported,
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200341 },
342#endif
Huang Jianan024fb2f2022-02-26 15:05:47 +0800343#if IS_ENABLED(CONFIG_FS_EROFS)
344 {
345 .fstype = FS_TYPE_EROFS,
346 .name = "erofs",
347 .null_dev_desc_ok = false,
348 .probe = erofs_probe,
349 .opendir = erofs_opendir,
350 .readdir = erofs_readdir,
351 .ls = fs_ls_generic,
352 .read = erofs_read,
353 .size = erofs_size,
354 .close = erofs_close,
355 .closedir = erofs_closedir,
356 .exists = erofs_exists,
357 .uuid = fs_uuid_unsupported,
358 .write = fs_write_unsupported,
359 .ln = fs_ln_unsupported,
360 .unlink = fs_unlink_unsupported,
361 .mkdir = fs_mkdir_unsupported,
362 },
363#endif
Simon Glass1aede482012-12-26 09:53:29 +0000364 {
365 .fstype = FS_TYPE_ANY,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100366 .name = "unsupported",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700367 .null_dev_desc_ok = true,
Simon Glass1aede482012-12-26 09:53:29 +0000368 .probe = fs_probe_unsupported,
369 .close = fs_close_unsupported,
370 .ls = fs_ls_unsupported,
Stephen Warrend008fbb2014-02-03 13:21:00 -0700371 .exists = fs_exists_unsupported,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600372 .size = fs_size_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000373 .read = fs_read_unsupported,
Simon Glasseda14ea2013-04-20 08:42:50 +0000374 .write = fs_write_unsupported,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100375 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400376 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900377 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900378 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100379 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000380 },
381};
382
Simon Glasse6aad852012-12-26 09:53:30 +0000383static struct fstype_info *fs_get_info(int fstype)
384{
385 struct fstype_info *info;
386 int i;
387
388 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
389 if (fstype == info->fstype)
390 return info;
391 }
392
393 /* Return the 'unsupported' sentinel */
394 return info;
395}
396
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000397/**
AKASHI Takahiro80ac7342019-10-07 14:59:37 +0900398 * fs_get_type() - Get type of current filesystem
399 *
400 * Return: filesystem type
401 *
402 * Returns filesystem type representing the current filesystem, or
403 * FS_TYPE_ANY for any unrecognised filesystem.
404 */
405int fs_get_type(void)
406{
407 return fs_type;
408}
409
410/**
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000411 * fs_get_type_name() - Get type of current filesystem
412 *
413 * Return: Pointer to filesystem name
414 *
415 * Returns a string describing the current filesystem, or the sentinel
416 * "unsupported" for any unrecognised filesystem.
417 */
418const char *fs_get_type_name(void)
419{
420 return fs_get_info(fs_type)->name;
421}
422
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000423int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
424{
Simon Glass1aede482012-12-26 09:53:29 +0000425 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000426 int part, i;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000427
Sean Andersonf476b4a2021-04-12 18:53:05 -0400428 part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
429 &fs_partition, 1);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000430 if (part < 0)
431 return -1;
432
Simon Glass1aede482012-12-26 09:53:29 +0000433 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
434 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
435 fstype != info->fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000436 continue;
437
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700438 if (!fs_dev_desc && !info->null_dev_desc_ok)
439 continue;
440
Simon Glass6a46e2f2012-12-26 09:53:31 +0000441 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000442 fs_type = info->fstype;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400443 fs_dev_part = part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000444 return 0;
445 }
446 }
447
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000448 return -1;
449}
450
Rob Clark2b7bfd92017-09-09 13:15:55 -0400451/* set current blk device w/ blk_desc + partition # */
452int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
453{
454 struct fstype_info *info;
455 int ret, i;
456
457 if (part >= 1)
458 ret = part_get_info(desc, part, &fs_partition);
459 else
460 ret = part_get_info_whole_disk(desc, &fs_partition);
461 if (ret)
462 return ret;
463 fs_dev_desc = desc;
464
465 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
466 if (!info->probe(fs_dev_desc, &fs_partition)) {
467 fs_type = info->fstype;
AKASHI Takahiro43efaa02018-10-17 16:32:02 +0900468 fs_dev_part = part;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400469 return 0;
470 }
471 }
472
473 return -1;
474}
475
AKASHI Takahiroca8e3772019-10-07 14:59:35 +0900476void fs_close(void)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000477{
Simon Glasse6aad852012-12-26 09:53:30 +0000478 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000479
Simon Glasse6aad852012-12-26 09:53:30 +0000480 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000481
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000482 fs_type = FS_TYPE_ANY;
483}
484
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100485int fs_uuid(char *uuid_str)
486{
487 struct fstype_info *info = fs_get_info(fs_type);
488
489 return info->uuid(uuid_str);
490}
491
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000492int fs_ls(const char *dirname)
493{
494 int ret;
495
Simon Glasse6aad852012-12-26 09:53:30 +0000496 struct fstype_info *info = fs_get_info(fs_type);
497
498 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000499
500 fs_close();
501
502 return ret;
503}
504
Stephen Warrend008fbb2014-02-03 13:21:00 -0700505int fs_exists(const char *filename)
506{
507 int ret;
508
509 struct fstype_info *info = fs_get_info(fs_type);
510
511 ret = info->exists(filename);
512
513 fs_close();
514
515 return ret;
516}
517
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800518int fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600519{
520 int ret;
521
522 struct fstype_info *info = fs_get_info(fs_type);
523
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800524 ret = info->size(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600525
526 fs_close();
527
528 return ret;
529}
530
Sughosh Ganu0d733f02024-08-26 17:29:22 +0530531#if CONFIG_IS_ENABLED(LMB)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100532/* Check if a file may be read to the given address */
533static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
534 loff_t len, struct fstype_info *info)
535{
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100536 int ret;
537 loff_t size;
538 loff_t read_len;
539
540 /* get the actual size of the file */
541 ret = info->size(filename, &size);
542 if (ret)
543 return ret;
544 if (offset >= size) {
545 /* offset >= EOF, no bytes will be written */
546 return 0;
547 }
548 read_len = size - offset;
549
550 /* limit to 'len' if it is smaller */
551 if (len && len < read_len)
552 read_len = len;
553
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530554 lmb_dump_all();
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100555
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530556 if (lmb_alloc_addr(addr, read_len) == addr)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100557 return 0;
558
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000559 log_err("** Reading file would overwrite reserved memory **\n");
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100560 return -ENOSPC;
561}
562#endif
563
564static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
565 int do_lmb_check, loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000566{
Simon Glasse6aad852012-12-26 09:53:30 +0000567 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000568 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000569 int ret;
570
Sughosh Ganu0d733f02024-08-26 17:29:22 +0530571#if CONFIG_IS_ENABLED(LMB)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100572 if (do_lmb_check) {
573 ret = fs_read_lmb_check(filename, addr, offset, len, info);
574 if (ret)
575 return ret;
576 }
577#endif
578
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000579 /*
580 * We don't actually know how many bytes are being read, since len==0
581 * means read the whole file.
582 */
583 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800584 ret = info->read(filename, buf, offset, len, actread);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000585 unmap_sysmem(buf);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000586
Simon Glasse6aad852012-12-26 09:53:30 +0000587 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher56d9cdf2015-08-05 17:16:58 +0200588 if (ret == 0 && len && *actread != len)
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000589 log_debug("** %s shorter than offset + len **\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000590 fs_close();
591
592 return ret;
593}
594
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100595int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
596 loff_t *actread)
597{
598 return _fs_read(filename, addr, offset, len, 0, actread);
599}
600
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800601int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
602 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000603{
604 struct fstype_info *info = fs_get_info(fs_type);
605 void *buf;
606 int ret;
607
Simon Glasseda14ea2013-04-20 08:42:50 +0000608 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800609 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glasseda14ea2013-04-20 08:42:50 +0000610 unmap_sysmem(buf);
611
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800612 if (ret < 0 && len != *actwrite) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000613 log_err("** Unable to write file %s **\n", filename);
Simon Glasseda14ea2013-04-20 08:42:50 +0000614 ret = -1;
615 }
616 fs_close();
617
618 return ret;
619}
620
Rob Clark2b7bfd92017-09-09 13:15:55 -0400621struct fs_dir_stream *fs_opendir(const char *filename)
622{
623 struct fstype_info *info = fs_get_info(fs_type);
624 struct fs_dir_stream *dirs = NULL;
625 int ret;
626
627 ret = info->opendir(filename, &dirs);
628 fs_close();
629 if (ret) {
630 errno = -ret;
631 return NULL;
632 }
633
634 dirs->desc = fs_dev_desc;
635 dirs->part = fs_dev_part;
636
637 return dirs;
638}
639
640struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
641{
642 struct fstype_info *info;
643 struct fs_dirent *dirent;
644 int ret;
645
646 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
647 info = fs_get_info(fs_type);
648
649 ret = info->readdir(dirs, &dirent);
650 fs_close();
651 if (ret) {
652 errno = -ret;
653 return NULL;
654 }
655
656 return dirent;
657}
658
659void fs_closedir(struct fs_dir_stream *dirs)
660{
661 struct fstype_info *info;
662
663 if (!dirs)
664 return;
665
666 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
667 info = fs_get_info(fs_type);
668
669 info->closedir(dirs);
670 fs_close();
671}
672
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900673int fs_unlink(const char *filename)
674{
675 int ret;
676
677 struct fstype_info *info = fs_get_info(fs_type);
678
679 ret = info->unlink(filename);
680
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900681 fs_close();
682
683 return ret;
684}
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900685
686int fs_mkdir(const char *dirname)
687{
688 int ret;
689
690 struct fstype_info *info = fs_get_info(fs_type);
691
692 ret = info->mkdir(dirname);
693
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900694 fs_close();
695
696 return ret;
697}
Rob Clark2b7bfd92017-09-09 13:15:55 -0400698
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100699int fs_ln(const char *fname, const char *target)
700{
701 struct fstype_info *info = fs_get_info(fs_type);
702 int ret;
703
704 ret = info->ln(fname, target);
705
706 if (ret < 0) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000707 log_err("** Unable to create link %s -> %s **\n", fname, target);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100708 ret = -1;
709 }
710 fs_close();
711
712 return ret;
713}
714
Simon Glassed38aef2020-05-10 11:40:03 -0600715int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
716 int fstype)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600717{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800718 loff_t size;
Stephen Warren3eb58f52014-06-11 12:47:26 -0600719
720 if (argc != 4)
721 return CMD_RET_USAGE;
722
723 if (fs_set_blk_dev(argv[1], argv[2], fstype))
724 return 1;
725
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800726 if (fs_size(argv[3], &size) < 0)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600727 return CMD_RET_FAILURE;
728
Simon Glass4d949a22017-08-03 12:22:10 -0600729 env_set_hex("filesize", size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600730
731 return 0;
732}
733
Simon Glassed38aef2020-05-10 11:40:03 -0600734int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
735 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000736{
737 unsigned long addr;
738 const char *addr_str;
739 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800740 loff_t bytes;
741 loff_t pos;
742 loff_t len_read;
743 int ret;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100744 unsigned long time;
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200745 char *ep;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000746
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000747 if (argc < 2)
748 return CMD_RET_USAGE;
749 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000750 return CMD_RET_USAGE;
751
Simon Glass793a98e2023-11-18 14:05:20 -0700752 if (fs_set_blk_dev(argv[1], cmd_arg2(argc, argv), fstype)) {
Heinrich Schuchardtc556a452021-01-27 21:26:43 +0100753 log_err("Can't set block device\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000754 return 1;
Heinrich Schuchardtc556a452021-01-27 21:26:43 +0100755 }
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000756
757 if (argc >= 4) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600758 addr = hextoul(argv[3], &ep);
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200759 if (ep == argv[3] || *ep != '\0')
760 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000761 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600762 addr_str = env_get("loadaddr");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000763 if (addr_str != NULL)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600764 addr = hextoul(addr_str, NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000765 else
766 addr = CONFIG_SYS_LOAD_ADDR;
767 }
768 if (argc >= 5) {
769 filename = argv[4];
770 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600771 filename = env_get("bootfile");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000772 if (!filename) {
773 puts("** No boot file defined **\n");
774 return 1;
775 }
776 }
777 if (argc >= 6)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600778 bytes = hextoul(argv[5], NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000779 else
780 bytes = 0;
781 if (argc >= 7)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600782 pos = hextoul(argv[6], NULL);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000783 else
784 pos = 0;
785
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100786 time = get_timer(0);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100787 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100788 time = get_timer(time);
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200789 if (ret < 0) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000790 log_err("Failed to load '%s'\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000791 return 1;
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200792 }
793
AKASHI Takahirod4c99462024-01-17 13:39:44 +0900794 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
795 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
796 len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000797
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800798 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100799 if (time > 0) {
800 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500801 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100802 puts(")");
803 }
804 puts("\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000805
Simon Glass4d949a22017-08-03 12:22:10 -0600806 env_set_hex("fileaddr", addr);
807 env_set_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000808
809 return 0;
810}
811
Simon Glassed38aef2020-05-10 11:40:03 -0600812int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
813 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000814{
815 if (argc < 2)
816 return CMD_RET_USAGE;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000817 if (argc > 4)
818 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000819
Simon Glass793a98e2023-11-18 14:05:20 -0700820 if (fs_set_blk_dev(argv[1], cmd_arg2(argc, argv), fstype))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000821 return 1;
822
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000823 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000824 return 1;
825
826 return 0;
827}
Simon Glasseda14ea2013-04-20 08:42:50 +0000828
Stephen Warrend008fbb2014-02-03 13:21:00 -0700829int file_exists(const char *dev_type, const char *dev_part, const char *file,
830 int fstype)
831{
832 if (fs_set_blk_dev(dev_type, dev_part, fstype))
833 return 0;
834
835 return fs_exists(file);
836}
837
Simon Glassed38aef2020-05-10 11:40:03 -0600838int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
839 int fstype)
Simon Glasseda14ea2013-04-20 08:42:50 +0000840{
841 unsigned long addr;
842 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800843 loff_t bytes;
844 loff_t pos;
845 loff_t len;
846 int ret;
Simon Glasseda14ea2013-04-20 08:42:50 +0000847 unsigned long time;
848
849 if (argc < 6 || argc > 7)
850 return CMD_RET_USAGE;
851
852 if (fs_set_blk_dev(argv[1], argv[2], fstype))
853 return 1;
854
Simon Glass3ff49ec2021-07-24 09:03:29 -0600855 addr = hextoul(argv[3], NULL);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800856 filename = argv[4];
Simon Glass3ff49ec2021-07-24 09:03:29 -0600857 bytes = hextoul(argv[5], NULL);
Simon Glasseda14ea2013-04-20 08:42:50 +0000858 if (argc >= 7)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600859 pos = hextoul(argv[6], NULL);
Simon Glasseda14ea2013-04-20 08:42:50 +0000860 else
861 pos = 0;
862
863 time = get_timer(0);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800864 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glasseda14ea2013-04-20 08:42:50 +0000865 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800866 if (ret < 0)
Simon Glasseda14ea2013-04-20 08:42:50 +0000867 return 1;
868
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800869 printf("%llu bytes written in %lu ms", len, time);
Simon Glasseda14ea2013-04-20 08:42:50 +0000870 if (time > 0) {
871 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500872 print_size(div_u64(len, time) * 1000, "/s");
Simon Glasseda14ea2013-04-20 08:42:50 +0000873 puts(")");
874 }
875 puts("\n");
876
877 return 0;
878}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100879
Simon Glassed38aef2020-05-10 11:40:03 -0600880int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
881 int fstype)
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100882{
883 int ret;
884 char uuid[37];
885 memset(uuid, 0, sizeof(uuid));
886
887 if (argc < 3 || argc > 4)
888 return CMD_RET_USAGE;
889
890 if (fs_set_blk_dev(argv[1], argv[2], fstype))
891 return 1;
892
893 ret = fs_uuid(uuid);
894 if (ret)
895 return CMD_RET_FAILURE;
896
897 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600898 env_set(argv[3], uuid);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100899 else
900 printf("%s\n", uuid);
901
902 return CMD_RET_SUCCESS;
903}
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100904
Simon Glassed38aef2020-05-10 11:40:03 -0600905int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100906{
907 struct fstype_info *info;
908
909 if (argc < 3 || argc > 4)
910 return CMD_RET_USAGE;
911
912 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
913 return 1;
914
915 info = fs_get_info(fs_type);
916
917 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600918 env_set(argv[3], info->name);
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100919 else
920 printf("%s\n", info->name);
921
Marek Vasut08d1c422019-02-06 13:19:29 +0100922 fs_close();
923
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100924 return CMD_RET_SUCCESS;
925}
926
Simon Glassed38aef2020-05-10 11:40:03 -0600927int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900928 int fstype)
929{
930 if (argc != 4)
931 return CMD_RET_USAGE;
932
933 if (fs_set_blk_dev(argv[1], argv[2], fstype))
934 return 1;
935
936 if (fs_unlink(argv[3]))
937 return 1;
938
939 return 0;
940}
941
Simon Glassed38aef2020-05-10 11:40:03 -0600942int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900943 int fstype)
944{
945 int ret;
946
947 if (argc != 4)
948 return CMD_RET_USAGE;
949
950 if (fs_set_blk_dev(argv[1], argv[2], fstype))
951 return 1;
952
953 ret = fs_mkdir(argv[3]);
954 if (ret) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +0000955 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900956 return 1;
957 }
958
959 return 0;
960}
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100961
Simon Glassed38aef2020-05-10 11:40:03 -0600962int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100963 int fstype)
964{
965 if (argc != 5)
966 return CMD_RET_USAGE;
967
968 if (fs_set_blk_dev(argv[1], argv[2], fstype))
969 return 1;
970
971 if (fs_ln(argv[3], argv[4]))
972 return 1;
973
974 return 0;
975}
Niel Fourie9252d702020-03-24 16:17:04 +0100976
977int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
978{
979 struct fstype_info *drv = fstypes;
980 const int n_ents = ARRAY_SIZE(fstypes);
981 struct fstype_info *entry;
982 int i = 0;
983
984 puts("Supported filesystems");
985 for (entry = drv; entry != drv + n_ents; entry++) {
986 if (entry->fstype != FS_TYPE_ANY) {
987 printf("%c %s", i ? ',' : ':', entry->name);
988 i++;
989 }
990 }
991 if (!i)
992 puts(": <none>");
993 puts("\n");
994 return CMD_RET_SUCCESS;
995}
Simon Glasse6c49fc2023-06-01 10:22:38 -0600996
997int fs_read_alloc(const char *fname, ulong size, uint align, void **bufp)
998{
999 loff_t bytes_read;
1000 ulong addr;
1001 char *buf;
1002 int ret;
1003
1004 buf = memalign(align, size + 1);
1005 if (!buf)
1006 return log_msg_ret("buf", -ENOMEM);
1007 addr = map_to_sysmem(buf);
1008
1009 ret = fs_read(fname, addr, 0, size, &bytes_read);
1010 if (ret) {
1011 free(buf);
1012 return log_msg_ret("read", ret);
1013 }
1014 if (size != bytes_read)
1015 return log_msg_ret("bread", -EIO);
1016 buf[size] = '\0';
1017
1018 *bufp = buf;
1019
1020 return 0;
1021}
1022
1023int fs_load_alloc(const char *ifname, const char *dev_part_str,
1024 const char *fname, ulong max_size, ulong align, void **bufp,
1025 ulong *sizep)
1026{
1027 loff_t size;
1028 void *buf;
1029 int ret;
1030
1031 if (fs_set_blk_dev(ifname, dev_part_str, FS_TYPE_ANY))
1032 return log_msg_ret("set", -ENOMEDIUM);
1033
1034 ret = fs_size(fname, &size);
1035 if (ret)
1036 return log_msg_ret("sz", -ENOENT);
1037
1038 if (size >= (max_size ?: SZ_1G))
1039 return log_msg_ret("sz", -E2BIG);
1040
1041 if (fs_set_blk_dev(ifname, dev_part_str, FS_TYPE_ANY))
1042 return log_msg_ret("set", -ENOMEDIUM);
1043
1044 ret = fs_read_alloc(fname, size, align, &buf);
1045 if (ret)
1046 return log_msg_ret("al", ret);
1047 *sizep = size;
1048 *bufp = buf;
1049
1050 return 0;
1051}