blob: 736ebef4a93917fc84590d2739a73cc9f2793138 [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
6#include <config.h>
Christian Gmeiner9f9eec32014-11-12 14:35:04 +01007#include <errno.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +00008#include <common.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -05009#include <mapmem.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000010#include <part.h>
11#include <ext4fs.h>
12#include <fat.h>
13#include <fs.h>
Simon Glass11842872012-12-26 09:53:35 +000014#include <sandboxfs.h>
Hans de Goede690c7962015-09-17 18:46:58 -040015#include <ubifs_uboot.h>
Marek BehĂșn98ec1d12017-09-03 17:00:29 +020016#include <btrfs.h>
Simon Glasscbe5d5d2012-12-26 09:53:32 +000017#include <asm/io.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050018#include <div64.h>
19#include <linux/math64.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000020
Stephen Warren44161af2012-10-30 07:50:47 +000021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glasse3394752016-02-29 15:25:34 -070023static struct blk_desc *fs_dev_desc;
Rob Clark2b7bfd92017-09-09 13:15:55 -040024static int fs_dev_part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +000025static disk_partition_t fs_partition;
26static int fs_type = FS_TYPE_ANY;
27
Simon Glasse3394752016-02-29 15:25:34 -070028static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glass6a46e2f2012-12-26 09:53:31 +000029 disk_partition_t *fs_partition)
Simon Glass1aede482012-12-26 09:53:29 +000030{
31 printf("** Unrecognized filesystem type **\n");
32 return -1;
33}
34
Stephen Warreneefbc3f2012-10-22 06:43:51 +000035static inline int fs_ls_unsupported(const char *dirname)
36{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000037 return -1;
38}
39
Rob Clark60d74a92017-09-09 13:15:58 -040040/* generic implementation of ls in terms of opendir/readdir/closedir */
41__maybe_unused
42static int fs_ls_generic(const char *dirname)
43{
44 struct fs_dir_stream *dirs;
45 struct fs_dirent *dent;
46 int nfiles = 0, ndirs = 0;
47
48 dirs = fs_opendir(dirname);
49 if (!dirs)
50 return -errno;
51
52 while ((dent = fs_readdir(dirs))) {
53 if (dent->type == FS_DT_DIR) {
54 printf(" %s/\n", dent->name);
55 ndirs++;
56 } else {
57 printf(" %8lld %s\n", dent->size, dent->name);
58 nfiles++;
59 }
60 }
61
62 fs_closedir(dirs);
63
64 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
65
66 return 0;
67}
68
Stephen Warrend008fbb2014-02-03 13:21:00 -070069static inline int fs_exists_unsupported(const char *filename)
70{
71 return 0;
72}
73
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080074static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -060075{
76 return -1;
77}
78
Simon Glasscbe5d5d2012-12-26 09:53:32 +000079static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080080 loff_t offset, loff_t len,
81 loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +000082{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000083 return -1;
84}
85
Simon Glasseda14ea2013-04-20 08:42:50 +000086static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080087 loff_t offset, loff_t len,
88 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +000089{
90 return -1;
91}
92
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +010093static inline int fs_ln_unsupported(const char *filename, const char *target)
94{
95 return -1;
96}
97
Simon Glass1aede482012-12-26 09:53:29 +000098static inline void fs_close_unsupported(void)
99{
100}
101
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100102static inline int fs_uuid_unsupported(char *uuid_str)
103{
104 return -1;
105}
106
Rob Clark2b7bfd92017-09-09 13:15:55 -0400107static inline int fs_opendir_unsupported(const char *filename,
108 struct fs_dir_stream **dirs)
109{
110 return -EACCES;
111}
112
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900113static inline int fs_unlink_unsupported(const char *filename)
114{
115 return -1;
116}
117
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900118static inline int fs_mkdir_unsupported(const char *dirname)
119{
120 return -1;
121}
122
Simon Glass1aede482012-12-26 09:53:29 +0000123struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000124 int fstype;
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100125 char *name;
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700126 /*
127 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
128 * should be false in most cases. For "virtual" filesystems which
129 * aren't based on a U-Boot block device (e.g. sandbox), this can be
Heinrich Schuchardt99dc17d2018-08-11 15:52:14 +0200130 * set to true. This should also be true for the dummy entry at the end
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700131 * of fstypes[], since that is essentially a "virtual" (non-existent)
132 * filesystem.
133 */
134 bool null_dev_desc_ok;
Simon Glasse3394752016-02-29 15:25:34 -0700135 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glass6a46e2f2012-12-26 09:53:31 +0000136 disk_partition_t *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +0000137 int (*ls)(const char *dirname);
Stephen Warrend008fbb2014-02-03 13:21:00 -0700138 int (*exists)(const char *filename);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800139 int (*size)(const char *filename, loff_t *size);
140 int (*read)(const char *filename, void *buf, loff_t offset,
141 loff_t len, loff_t *actread);
142 int (*write)(const char *filename, void *buf, loff_t offset,
143 loff_t len, loff_t *actwrite);
Simon Glass1aede482012-12-26 09:53:29 +0000144 void (*close)(void);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100145 int (*uuid)(char *uuid_str);
Rob Clark2b7bfd92017-09-09 13:15:55 -0400146 /*
147 * Open a directory stream. On success return 0 and directory
148 * stream pointer via 'dirsp'. On error, return -errno. See
149 * fs_opendir().
150 */
151 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
152 /*
153 * Read next entry from directory stream. On success return 0
154 * and directory entry pointer via 'dentp'. On error return
155 * -errno. See fs_readdir().
156 */
157 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
158 /* see fs_closedir() */
159 void (*closedir)(struct fs_dir_stream *dirs);
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900160 int (*unlink)(const char *filename);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900161 int (*mkdir)(const char *dirname);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100162 int (*ln)(const char *filename, const char *target);
Simon Glass1aede482012-12-26 09:53:29 +0000163};
164
165static struct fstype_info fstypes[] = {
166#ifdef CONFIG_FS_FAT
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000167 {
168 .fstype = FS_TYPE_FAT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100169 .name = "fat",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700170 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000171 .probe = fat_set_blk_dev,
172 .close = fat_close,
Rob Clark60d74a92017-09-09 13:15:58 -0400173 .ls = fs_ls_generic,
Stephen Warren1d2f9a02014-02-03 13:21:10 -0700174 .exists = fat_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600175 .size = fat_size,
Simon Glass19e38582012-12-26 09:53:33 +0000176 .read = fat_read_file,
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800177#if CONFIG_IS_ENABLED(FAT_WRITE)
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800178 .write = file_fat_write,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900179 .unlink = fat_unlink,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900180 .mkdir = fat_mkdir,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800181#else
Stephen Warren9df25802014-02-03 13:20:59 -0700182 .write = fs_write_unsupported,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900183 .unlink = fs_unlink_unsupported,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900184 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800185#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100186 .uuid = fs_uuid_unsupported,
Rob Clark60d74a92017-09-09 13:15:58 -0400187 .opendir = fat_opendir,
188 .readdir = fat_readdir,
189 .closedir = fat_closedir,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100190 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000191 },
Simon Glass1aede482012-12-26 09:53:29 +0000192#endif
Tien Fong Chee64eab042019-01-23 14:20:06 +0800193
194#if CONFIG_IS_ENABLED(FS_EXT4)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000195 {
196 .fstype = FS_TYPE_EXT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100197 .name = "ext4",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700198 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000199 .probe = ext4fs_probe,
200 .close = ext4fs_close,
Simon Glass1aede482012-12-26 09:53:29 +0000201 .ls = ext4fs_ls,
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700202 .exists = ext4fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600203 .size = ext4fs_size,
Simon Glass19e38582012-12-26 09:53:33 +0000204 .read = ext4_read_file,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800205#ifdef CONFIG_CMD_EXT4_WRITE
206 .write = ext4_write_file,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100207 .ln = ext4fs_create_link,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800208#else
Stephen Warren9df25802014-02-03 13:20:59 -0700209 .write = fs_write_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100210 .ln = fs_ln_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800211#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100212 .uuid = ext4fs_uuid,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400213 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900214 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900215 .mkdir = fs_mkdir_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000216 },
217#endif
Simon Glass11842872012-12-26 09:53:35 +0000218#ifdef CONFIG_SANDBOX
219 {
220 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100221 .name = "sandbox",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700222 .null_dev_desc_ok = true,
Simon Glass11842872012-12-26 09:53:35 +0000223 .probe = sandbox_fs_set_blk_dev,
224 .close = sandbox_fs_close,
225 .ls = sandbox_fs_ls,
Stephen Warren97d66f22014-02-03 13:21:07 -0700226 .exists = sandbox_fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600227 .size = sandbox_fs_size,
Simon Glass11842872012-12-26 09:53:35 +0000228 .read = fs_read_sandbox,
Simon Glassea307e82013-04-20 08:42:51 +0000229 .write = fs_write_sandbox,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100230 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400231 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900232 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900233 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100234 .ln = fs_ln_unsupported,
Simon Glass11842872012-12-26 09:53:35 +0000235 },
236#endif
Hans de Goede690c7962015-09-17 18:46:58 -0400237#ifdef CONFIG_CMD_UBIFS
238 {
239 .fstype = FS_TYPE_UBIFS,
240 .name = "ubifs",
241 .null_dev_desc_ok = true,
242 .probe = ubifs_set_blk_dev,
243 .close = ubifs_close,
244 .ls = ubifs_ls,
245 .exists = ubifs_exists,
246 .size = ubifs_size,
247 .read = ubifs_read,
248 .write = fs_write_unsupported,
249 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400250 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900251 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900252 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100253 .ln = fs_ln_unsupported,
Hans de Goede690c7962015-09-17 18:46:58 -0400254 },
255#endif
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200256#ifdef CONFIG_FS_BTRFS
257 {
258 .fstype = FS_TYPE_BTRFS,
259 .name = "btrfs",
260 .null_dev_desc_ok = false,
261 .probe = btrfs_probe,
262 .close = btrfs_close,
263 .ls = btrfs_ls,
264 .exists = btrfs_exists,
265 .size = btrfs_size,
266 .read = btrfs_read,
267 .write = fs_write_unsupported,
268 .uuid = btrfs_uuid,
Marek BehĂșn90762d92017-10-06 16:56:07 +0200269 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900270 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900271 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100272 .ln = fs_ln_unsupported,
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200273 },
274#endif
Simon Glass1aede482012-12-26 09:53:29 +0000275 {
276 .fstype = FS_TYPE_ANY,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100277 .name = "unsupported",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700278 .null_dev_desc_ok = true,
Simon Glass1aede482012-12-26 09:53:29 +0000279 .probe = fs_probe_unsupported,
280 .close = fs_close_unsupported,
281 .ls = fs_ls_unsupported,
Stephen Warrend008fbb2014-02-03 13:21:00 -0700282 .exists = fs_exists_unsupported,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600283 .size = fs_size_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000284 .read = fs_read_unsupported,
Simon Glasseda14ea2013-04-20 08:42:50 +0000285 .write = fs_write_unsupported,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100286 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400287 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900288 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900289 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100290 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000291 },
292};
293
Simon Glasse6aad852012-12-26 09:53:30 +0000294static struct fstype_info *fs_get_info(int fstype)
295{
296 struct fstype_info *info;
297 int i;
298
299 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
300 if (fstype == info->fstype)
301 return info;
302 }
303
304 /* Return the 'unsupported' sentinel */
305 return info;
306}
307
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000308/**
309 * fs_get_type_name() - Get type of current filesystem
310 *
311 * Return: Pointer to filesystem name
312 *
313 * Returns a string describing the current filesystem, or the sentinel
314 * "unsupported" for any unrecognised filesystem.
315 */
316const char *fs_get_type_name(void)
317{
318 return fs_get_info(fs_type)->name;
319}
320
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000321int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
322{
Simon Glass1aede482012-12-26 09:53:29 +0000323 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000324 int part, i;
Stephen Warren44161af2012-10-30 07:50:47 +0000325#ifdef CONFIG_NEEDS_MANUAL_RELOC
326 static int relocated;
327
328 if (!relocated) {
Simon Glass1aede482012-12-26 09:53:29 +0000329 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
330 i++, info++) {
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100331 info->name += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000332 info->probe += gd->reloc_off;
333 info->close += gd->reloc_off;
334 info->ls += gd->reloc_off;
335 info->read += gd->reloc_off;
Simon Glasseda14ea2013-04-20 08:42:50 +0000336 info->write += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000337 }
Stephen Warren44161af2012-10-30 07:50:47 +0000338 relocated = 1;
339 }
340#endif
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000341
Simon Glasse76ee972016-02-29 15:25:44 -0700342 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000343 &fs_partition, 1);
344 if (part < 0)
345 return -1;
346
Simon Glass1aede482012-12-26 09:53:29 +0000347 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
348 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
349 fstype != info->fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000350 continue;
351
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700352 if (!fs_dev_desc && !info->null_dev_desc_ok)
353 continue;
354
Simon Glass6a46e2f2012-12-26 09:53:31 +0000355 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000356 fs_type = info->fstype;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400357 fs_dev_part = part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000358 return 0;
359 }
360 }
361
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000362 return -1;
363}
364
Rob Clark2b7bfd92017-09-09 13:15:55 -0400365/* set current blk device w/ blk_desc + partition # */
366int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
367{
368 struct fstype_info *info;
369 int ret, i;
370
371 if (part >= 1)
372 ret = part_get_info(desc, part, &fs_partition);
373 else
374 ret = part_get_info_whole_disk(desc, &fs_partition);
375 if (ret)
376 return ret;
377 fs_dev_desc = desc;
378
379 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
380 if (!info->probe(fs_dev_desc, &fs_partition)) {
381 fs_type = info->fstype;
AKASHI Takahiro43efaa02018-10-17 16:32:02 +0900382 fs_dev_part = part;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400383 return 0;
384 }
385 }
386
387 return -1;
388}
389
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000390static void fs_close(void)
391{
Simon Glasse6aad852012-12-26 09:53:30 +0000392 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000393
Simon Glasse6aad852012-12-26 09:53:30 +0000394 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000395
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000396 fs_type = FS_TYPE_ANY;
397}
398
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100399int fs_uuid(char *uuid_str)
400{
401 struct fstype_info *info = fs_get_info(fs_type);
402
403 return info->uuid(uuid_str);
404}
405
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000406int fs_ls(const char *dirname)
407{
408 int ret;
409
Simon Glasse6aad852012-12-26 09:53:30 +0000410 struct fstype_info *info = fs_get_info(fs_type);
411
412 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000413
Simon Glass19e38582012-12-26 09:53:33 +0000414 fs_type = FS_TYPE_ANY;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000415 fs_close();
416
417 return ret;
418}
419
Stephen Warrend008fbb2014-02-03 13:21:00 -0700420int fs_exists(const char *filename)
421{
422 int ret;
423
424 struct fstype_info *info = fs_get_info(fs_type);
425
426 ret = info->exists(filename);
427
428 fs_close();
429
430 return ret;
431}
432
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800433int fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600434{
435 int ret;
436
437 struct fstype_info *info = fs_get_info(fs_type);
438
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800439 ret = info->size(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600440
441 fs_close();
442
443 return ret;
444}
445
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100446#ifdef CONFIG_LMB
447/* Check if a file may be read to the given address */
448static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
449 loff_t len, struct fstype_info *info)
450{
451 struct lmb lmb;
452 int ret;
453 loff_t size;
454 loff_t read_len;
455
456 /* get the actual size of the file */
457 ret = info->size(filename, &size);
458 if (ret)
459 return ret;
460 if (offset >= size) {
461 /* offset >= EOF, no bytes will be written */
462 return 0;
463 }
464 read_len = size - offset;
465
466 /* limit to 'len' if it is smaller */
467 if (len && len < read_len)
468 read_len = len;
469
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100470 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100471 lmb_dump_all(&lmb);
472
473 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
474 return 0;
475
476 printf("** Reading file would overwrite reserved memory **\n");
477 return -ENOSPC;
478}
479#endif
480
481static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
482 int do_lmb_check, loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000483{
Simon Glasse6aad852012-12-26 09:53:30 +0000484 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000485 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000486 int ret;
487
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100488#ifdef CONFIG_LMB
489 if (do_lmb_check) {
490 ret = fs_read_lmb_check(filename, addr, offset, len, info);
491 if (ret)
492 return ret;
493 }
494#endif
495
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000496 /*
497 * We don't actually know how many bytes are being read, since len==0
498 * means read the whole file.
499 */
500 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800501 ret = info->read(filename, buf, offset, len, actread);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000502 unmap_sysmem(buf);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000503
Simon Glasse6aad852012-12-26 09:53:30 +0000504 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher56d9cdf2015-08-05 17:16:58 +0200505 if (ret == 0 && len && *actread != len)
Heinrich Schuchardtf02c5c52018-01-01 21:15:37 +0100506 debug("** %s shorter than offset + len **\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000507 fs_close();
508
509 return ret;
510}
511
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100512int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
513 loff_t *actread)
514{
515 return _fs_read(filename, addr, offset, len, 0, actread);
516}
517
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800518int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
519 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000520{
521 struct fstype_info *info = fs_get_info(fs_type);
522 void *buf;
523 int ret;
524
Simon Glasseda14ea2013-04-20 08:42:50 +0000525 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800526 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glasseda14ea2013-04-20 08:42:50 +0000527 unmap_sysmem(buf);
528
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800529 if (ret < 0 && len != *actwrite) {
Simon Glasseda14ea2013-04-20 08:42:50 +0000530 printf("** Unable to write file %s **\n", filename);
531 ret = -1;
532 }
533 fs_close();
534
535 return ret;
536}
537
Rob Clark2b7bfd92017-09-09 13:15:55 -0400538struct fs_dir_stream *fs_opendir(const char *filename)
539{
540 struct fstype_info *info = fs_get_info(fs_type);
541 struct fs_dir_stream *dirs = NULL;
542 int ret;
543
544 ret = info->opendir(filename, &dirs);
545 fs_close();
546 if (ret) {
547 errno = -ret;
548 return NULL;
549 }
550
551 dirs->desc = fs_dev_desc;
552 dirs->part = fs_dev_part;
553
554 return dirs;
555}
556
557struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
558{
559 struct fstype_info *info;
560 struct fs_dirent *dirent;
561 int ret;
562
563 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
564 info = fs_get_info(fs_type);
565
566 ret = info->readdir(dirs, &dirent);
567 fs_close();
568 if (ret) {
569 errno = -ret;
570 return NULL;
571 }
572
573 return dirent;
574}
575
576void fs_closedir(struct fs_dir_stream *dirs)
577{
578 struct fstype_info *info;
579
580 if (!dirs)
581 return;
582
583 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
584 info = fs_get_info(fs_type);
585
586 info->closedir(dirs);
587 fs_close();
588}
589
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900590int fs_unlink(const char *filename)
591{
592 int ret;
593
594 struct fstype_info *info = fs_get_info(fs_type);
595
596 ret = info->unlink(filename);
597
598 fs_type = FS_TYPE_ANY;
599 fs_close();
600
601 return ret;
602}
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900603
604int fs_mkdir(const char *dirname)
605{
606 int ret;
607
608 struct fstype_info *info = fs_get_info(fs_type);
609
610 ret = info->mkdir(dirname);
611
612 fs_type = FS_TYPE_ANY;
613 fs_close();
614
615 return ret;
616}
Rob Clark2b7bfd92017-09-09 13:15:55 -0400617
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100618int fs_ln(const char *fname, const char *target)
619{
620 struct fstype_info *info = fs_get_info(fs_type);
621 int ret;
622
623 ret = info->ln(fname, target);
624
625 if (ret < 0) {
626 printf("** Unable to create link %s -> %s **\n", fname, target);
627 ret = -1;
628 }
629 fs_close();
630
631 return ret;
632}
633
Stephen Warren3eb58f52014-06-11 12:47:26 -0600634int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
635 int fstype)
636{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800637 loff_t size;
Stephen Warren3eb58f52014-06-11 12:47:26 -0600638
639 if (argc != 4)
640 return CMD_RET_USAGE;
641
642 if (fs_set_blk_dev(argv[1], argv[2], fstype))
643 return 1;
644
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800645 if (fs_size(argv[3], &size) < 0)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600646 return CMD_RET_FAILURE;
647
Simon Glass4d949a22017-08-03 12:22:10 -0600648 env_set_hex("filesize", size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600649
650 return 0;
651}
652
Stephen Warren128d3d92012-10-31 11:05:07 +0000653int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200654 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000655{
656 unsigned long addr;
657 const char *addr_str;
658 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800659 loff_t bytes;
660 loff_t pos;
661 loff_t len_read;
662 int ret;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100663 unsigned long time;
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200664 char *ep;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000665
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000666 if (argc < 2)
667 return CMD_RET_USAGE;
668 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000669 return CMD_RET_USAGE;
670
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000671 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000672 return 1;
673
674 if (argc >= 4) {
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200675 addr = simple_strtoul(argv[3], &ep, 16);
676 if (ep == argv[3] || *ep != '\0')
677 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000678 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600679 addr_str = env_get("loadaddr");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000680 if (addr_str != NULL)
681 addr = simple_strtoul(addr_str, NULL, 16);
682 else
683 addr = CONFIG_SYS_LOAD_ADDR;
684 }
685 if (argc >= 5) {
686 filename = argv[4];
687 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600688 filename = env_get("bootfile");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000689 if (!filename) {
690 puts("** No boot file defined **\n");
691 return 1;
692 }
693 }
694 if (argc >= 6)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200695 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000696 else
697 bytes = 0;
698 if (argc >= 7)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200699 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000700 else
701 pos = 0;
702
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100703 time = get_timer(0);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100704 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100705 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800706 if (ret < 0)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000707 return 1;
708
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800709 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100710 if (time > 0) {
711 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500712 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100713 puts(")");
714 }
715 puts("\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000716
Simon Glass4d949a22017-08-03 12:22:10 -0600717 env_set_hex("fileaddr", addr);
718 env_set_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000719
720 return 0;
721}
722
723int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
724 int fstype)
725{
726 if (argc < 2)
727 return CMD_RET_USAGE;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000728 if (argc > 4)
729 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000730
731 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
732 return 1;
733
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000734 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000735 return 1;
736
737 return 0;
738}
Simon Glasseda14ea2013-04-20 08:42:50 +0000739
Stephen Warrend008fbb2014-02-03 13:21:00 -0700740int file_exists(const char *dev_type, const char *dev_part, const char *file,
741 int fstype)
742{
743 if (fs_set_blk_dev(dev_type, dev_part, fstype))
744 return 0;
745
746 return fs_exists(file);
747}
748
Simon Glasseda14ea2013-04-20 08:42:50 +0000749int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200750 int fstype)
Simon Glasseda14ea2013-04-20 08:42:50 +0000751{
752 unsigned long addr;
753 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800754 loff_t bytes;
755 loff_t pos;
756 loff_t len;
757 int ret;
Simon Glasseda14ea2013-04-20 08:42:50 +0000758 unsigned long time;
759
760 if (argc < 6 || argc > 7)
761 return CMD_RET_USAGE;
762
763 if (fs_set_blk_dev(argv[1], argv[2], fstype))
764 return 1;
765
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800766 addr = simple_strtoul(argv[3], NULL, 16);
767 filename = argv[4];
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200768 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glasseda14ea2013-04-20 08:42:50 +0000769 if (argc >= 7)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200770 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glasseda14ea2013-04-20 08:42:50 +0000771 else
772 pos = 0;
773
774 time = get_timer(0);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800775 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glasseda14ea2013-04-20 08:42:50 +0000776 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800777 if (ret < 0)
Simon Glasseda14ea2013-04-20 08:42:50 +0000778 return 1;
779
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800780 printf("%llu bytes written in %lu ms", len, time);
Simon Glasseda14ea2013-04-20 08:42:50 +0000781 if (time > 0) {
782 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500783 print_size(div_u64(len, time) * 1000, "/s");
Simon Glasseda14ea2013-04-20 08:42:50 +0000784 puts(")");
785 }
786 puts("\n");
787
788 return 0;
789}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100790
791int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
792 int fstype)
793{
794 int ret;
795 char uuid[37];
796 memset(uuid, 0, sizeof(uuid));
797
798 if (argc < 3 || argc > 4)
799 return CMD_RET_USAGE;
800
801 if (fs_set_blk_dev(argv[1], argv[2], fstype))
802 return 1;
803
804 ret = fs_uuid(uuid);
805 if (ret)
806 return CMD_RET_FAILURE;
807
808 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600809 env_set(argv[3], uuid);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100810 else
811 printf("%s\n", uuid);
812
813 return CMD_RET_SUCCESS;
814}
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100815
816int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
817{
818 struct fstype_info *info;
819
820 if (argc < 3 || argc > 4)
821 return CMD_RET_USAGE;
822
823 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
824 return 1;
825
826 info = fs_get_info(fs_type);
827
828 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600829 env_set(argv[3], info->name);
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100830 else
831 printf("%s\n", info->name);
832
Marek Vasut08d1c422019-02-06 13:19:29 +0100833 fs_close();
834
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100835 return CMD_RET_SUCCESS;
836}
837
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900838int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
839 int fstype)
840{
841 if (argc != 4)
842 return CMD_RET_USAGE;
843
844 if (fs_set_blk_dev(argv[1], argv[2], fstype))
845 return 1;
846
847 if (fs_unlink(argv[3]))
848 return 1;
849
850 return 0;
851}
852
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900853int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
854 int fstype)
855{
856 int ret;
857
858 if (argc != 4)
859 return CMD_RET_USAGE;
860
861 if (fs_set_blk_dev(argv[1], argv[2], fstype))
862 return 1;
863
864 ret = fs_mkdir(argv[3]);
865 if (ret) {
866 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
867 return 1;
868 }
869
870 return 0;
871}
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100872
873int do_ln(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
874 int fstype)
875{
876 if (argc != 5)
877 return CMD_RET_USAGE;
878
879 if (fs_set_blk_dev(argv[1], argv[2], fstype))
880 return 1;
881
882 if (fs_ln(argv[3], argv[4]))
883 return 1;
884
885 return 0;
886}