blob: d8a4ced4698ec97b2b6c5e7d11959b94f6545dee [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>
Simon Glass313112a2019-08-01 09:46:46 -06009#include <env.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050010#include <mapmem.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000011#include <part.h>
12#include <ext4fs.h>
13#include <fat.h>
14#include <fs.h>
Simon Glass11842872012-12-26 09:53:35 +000015#include <sandboxfs.h>
Hans de Goede690c7962015-09-17 18:46:58 -040016#include <ubifs_uboot.h>
Marek BehĂșn98ec1d12017-09-03 17:00:29 +020017#include <btrfs.h>
Simon Glasscbe5d5d2012-12-26 09:53:32 +000018#include <asm/io.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050019#include <div64.h>
20#include <linux/math64.h>
Mian Yousaf Kaukab961ebe82019-06-18 15:03:44 +020021#include <efi_loader.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000022
Stephen Warren44161af2012-10-30 07:50:47 +000023DECLARE_GLOBAL_DATA_PTR;
24
Simon Glasse3394752016-02-29 15:25:34 -070025static struct blk_desc *fs_dev_desc;
Rob Clark2b7bfd92017-09-09 13:15:55 -040026static int fs_dev_part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +000027static disk_partition_t fs_partition;
28static int fs_type = FS_TYPE_ANY;
29
Simon Glasse3394752016-02-29 15:25:34 -070030static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glass6a46e2f2012-12-26 09:53:31 +000031 disk_partition_t *fs_partition)
Simon Glass1aede482012-12-26 09:53:29 +000032{
33 printf("** Unrecognized filesystem type **\n");
34 return -1;
35}
36
Stephen Warreneefbc3f2012-10-22 06:43:51 +000037static inline int fs_ls_unsupported(const char *dirname)
38{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000039 return -1;
40}
41
Rob Clark60d74a92017-09-09 13:15:58 -040042/* generic implementation of ls in terms of opendir/readdir/closedir */
43__maybe_unused
44static int fs_ls_generic(const char *dirname)
45{
46 struct fs_dir_stream *dirs;
47 struct fs_dirent *dent;
48 int nfiles = 0, ndirs = 0;
49
50 dirs = fs_opendir(dirname);
51 if (!dirs)
52 return -errno;
53
54 while ((dent = fs_readdir(dirs))) {
55 if (dent->type == FS_DT_DIR) {
56 printf(" %s/\n", dent->name);
57 ndirs++;
58 } else {
59 printf(" %8lld %s\n", dent->size, dent->name);
60 nfiles++;
61 }
62 }
63
64 fs_closedir(dirs);
65
66 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
67
68 return 0;
69}
70
Stephen Warrend008fbb2014-02-03 13:21:00 -070071static inline int fs_exists_unsupported(const char *filename)
72{
73 return 0;
74}
75
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080076static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -060077{
78 return -1;
79}
80
Simon Glasscbe5d5d2012-12-26 09:53:32 +000081static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080082 loff_t offset, loff_t len,
83 loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +000084{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000085 return -1;
86}
87
Simon Glasseda14ea2013-04-20 08:42:50 +000088static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080089 loff_t offset, loff_t len,
90 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +000091{
92 return -1;
93}
94
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +010095static inline int fs_ln_unsupported(const char *filename, const char *target)
96{
97 return -1;
98}
99
Simon Glass1aede482012-12-26 09:53:29 +0000100static inline void fs_close_unsupported(void)
101{
102}
103
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100104static inline int fs_uuid_unsupported(char *uuid_str)
105{
106 return -1;
107}
108
Rob Clark2b7bfd92017-09-09 13:15:55 -0400109static inline int fs_opendir_unsupported(const char *filename,
110 struct fs_dir_stream **dirs)
111{
112 return -EACCES;
113}
114
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900115static inline int fs_unlink_unsupported(const char *filename)
116{
117 return -1;
118}
119
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900120static inline int fs_mkdir_unsupported(const char *dirname)
121{
122 return -1;
123}
124
Simon Glass1aede482012-12-26 09:53:29 +0000125struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000126 int fstype;
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100127 char *name;
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700128 /*
129 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
130 * should be false in most cases. For "virtual" filesystems which
131 * aren't based on a U-Boot block device (e.g. sandbox), this can be
Heinrich Schuchardt99dc17d2018-08-11 15:52:14 +0200132 * set to true. This should also be true for the dummy entry at the end
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700133 * of fstypes[], since that is essentially a "virtual" (non-existent)
134 * filesystem.
135 */
136 bool null_dev_desc_ok;
Simon Glasse3394752016-02-29 15:25:34 -0700137 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glass6a46e2f2012-12-26 09:53:31 +0000138 disk_partition_t *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +0000139 int (*ls)(const char *dirname);
Stephen Warrend008fbb2014-02-03 13:21:00 -0700140 int (*exists)(const char *filename);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800141 int (*size)(const char *filename, loff_t *size);
142 int (*read)(const char *filename, void *buf, loff_t offset,
143 loff_t len, loff_t *actread);
144 int (*write)(const char *filename, void *buf, loff_t offset,
145 loff_t len, loff_t *actwrite);
Simon Glass1aede482012-12-26 09:53:29 +0000146 void (*close)(void);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100147 int (*uuid)(char *uuid_str);
Rob Clark2b7bfd92017-09-09 13:15:55 -0400148 /*
149 * Open a directory stream. On success return 0 and directory
150 * stream pointer via 'dirsp'. On error, return -errno. See
151 * fs_opendir().
152 */
153 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
154 /*
155 * Read next entry from directory stream. On success return 0
156 * and directory entry pointer via 'dentp'. On error return
157 * -errno. See fs_readdir().
158 */
159 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
160 /* see fs_closedir() */
161 void (*closedir)(struct fs_dir_stream *dirs);
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900162 int (*unlink)(const char *filename);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900163 int (*mkdir)(const char *dirname);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100164 int (*ln)(const char *filename, const char *target);
Simon Glass1aede482012-12-26 09:53:29 +0000165};
166
167static struct fstype_info fstypes[] = {
168#ifdef CONFIG_FS_FAT
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000169 {
170 .fstype = FS_TYPE_FAT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100171 .name = "fat",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700172 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000173 .probe = fat_set_blk_dev,
174 .close = fat_close,
Rob Clark60d74a92017-09-09 13:15:58 -0400175 .ls = fs_ls_generic,
Stephen Warren1d2f9a02014-02-03 13:21:10 -0700176 .exists = fat_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600177 .size = fat_size,
Simon Glass19e38582012-12-26 09:53:33 +0000178 .read = fat_read_file,
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800179#if CONFIG_IS_ENABLED(FAT_WRITE)
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800180 .write = file_fat_write,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900181 .unlink = fat_unlink,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900182 .mkdir = fat_mkdir,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800183#else
Stephen Warren9df25802014-02-03 13:20:59 -0700184 .write = fs_write_unsupported,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900185 .unlink = fs_unlink_unsupported,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900186 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800187#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100188 .uuid = fs_uuid_unsupported,
Rob Clark60d74a92017-09-09 13:15:58 -0400189 .opendir = fat_opendir,
190 .readdir = fat_readdir,
191 .closedir = fat_closedir,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100192 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000193 },
Simon Glass1aede482012-12-26 09:53:29 +0000194#endif
Tien Fong Chee64eab042019-01-23 14:20:06 +0800195
196#if CONFIG_IS_ENABLED(FS_EXT4)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000197 {
198 .fstype = FS_TYPE_EXT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100199 .name = "ext4",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700200 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000201 .probe = ext4fs_probe,
202 .close = ext4fs_close,
Simon Glass1aede482012-12-26 09:53:29 +0000203 .ls = ext4fs_ls,
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700204 .exists = ext4fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600205 .size = ext4fs_size,
Simon Glass19e38582012-12-26 09:53:33 +0000206 .read = ext4_read_file,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800207#ifdef CONFIG_CMD_EXT4_WRITE
208 .write = ext4_write_file,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100209 .ln = ext4fs_create_link,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800210#else
Stephen Warren9df25802014-02-03 13:20:59 -0700211 .write = fs_write_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100212 .ln = fs_ln_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800213#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100214 .uuid = ext4fs_uuid,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400215 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900216 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900217 .mkdir = fs_mkdir_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000218 },
219#endif
Simon Glass11842872012-12-26 09:53:35 +0000220#ifdef CONFIG_SANDBOX
221 {
222 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100223 .name = "sandbox",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700224 .null_dev_desc_ok = true,
Simon Glass11842872012-12-26 09:53:35 +0000225 .probe = sandbox_fs_set_blk_dev,
226 .close = sandbox_fs_close,
227 .ls = sandbox_fs_ls,
Stephen Warren97d66f22014-02-03 13:21:07 -0700228 .exists = sandbox_fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600229 .size = sandbox_fs_size,
Simon Glass11842872012-12-26 09:53:35 +0000230 .read = fs_read_sandbox,
Simon Glassea307e82013-04-20 08:42:51 +0000231 .write = fs_write_sandbox,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100232 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400233 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900234 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900235 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100236 .ln = fs_ln_unsupported,
Simon Glass11842872012-12-26 09:53:35 +0000237 },
238#endif
Hans de Goede690c7962015-09-17 18:46:58 -0400239#ifdef CONFIG_CMD_UBIFS
240 {
241 .fstype = FS_TYPE_UBIFS,
242 .name = "ubifs",
243 .null_dev_desc_ok = true,
244 .probe = ubifs_set_blk_dev,
245 .close = ubifs_close,
246 .ls = ubifs_ls,
247 .exists = ubifs_exists,
248 .size = ubifs_size,
249 .read = ubifs_read,
250 .write = fs_write_unsupported,
251 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400252 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900253 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900254 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100255 .ln = fs_ln_unsupported,
Hans de Goede690c7962015-09-17 18:46:58 -0400256 },
257#endif
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200258#ifdef CONFIG_FS_BTRFS
259 {
260 .fstype = FS_TYPE_BTRFS,
261 .name = "btrfs",
262 .null_dev_desc_ok = false,
263 .probe = btrfs_probe,
264 .close = btrfs_close,
265 .ls = btrfs_ls,
266 .exists = btrfs_exists,
267 .size = btrfs_size,
268 .read = btrfs_read,
269 .write = fs_write_unsupported,
270 .uuid = btrfs_uuid,
Marek BehĂșn90762d92017-10-06 16:56:07 +0200271 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900272 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900273 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100274 .ln = fs_ln_unsupported,
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200275 },
276#endif
Simon Glass1aede482012-12-26 09:53:29 +0000277 {
278 .fstype = FS_TYPE_ANY,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100279 .name = "unsupported",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700280 .null_dev_desc_ok = true,
Simon Glass1aede482012-12-26 09:53:29 +0000281 .probe = fs_probe_unsupported,
282 .close = fs_close_unsupported,
283 .ls = fs_ls_unsupported,
Stephen Warrend008fbb2014-02-03 13:21:00 -0700284 .exists = fs_exists_unsupported,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600285 .size = fs_size_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000286 .read = fs_read_unsupported,
Simon Glasseda14ea2013-04-20 08:42:50 +0000287 .write = fs_write_unsupported,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100288 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400289 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900290 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900291 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100292 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000293 },
294};
295
Simon Glasse6aad852012-12-26 09:53:30 +0000296static struct fstype_info *fs_get_info(int fstype)
297{
298 struct fstype_info *info;
299 int i;
300
301 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
302 if (fstype == info->fstype)
303 return info;
304 }
305
306 /* Return the 'unsupported' sentinel */
307 return info;
308}
309
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000310/**
311 * fs_get_type_name() - Get type of current filesystem
312 *
313 * Return: Pointer to filesystem name
314 *
315 * Returns a string describing the current filesystem, or the sentinel
316 * "unsupported" for any unrecognised filesystem.
317 */
318const char *fs_get_type_name(void)
319{
320 return fs_get_info(fs_type)->name;
321}
322
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000323int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
324{
Simon Glass1aede482012-12-26 09:53:29 +0000325 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000326 int part, i;
Stephen Warren44161af2012-10-30 07:50:47 +0000327#ifdef CONFIG_NEEDS_MANUAL_RELOC
328 static int relocated;
329
330 if (!relocated) {
Simon Glass1aede482012-12-26 09:53:29 +0000331 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
332 i++, info++) {
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100333 info->name += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000334 info->probe += gd->reloc_off;
335 info->close += gd->reloc_off;
336 info->ls += gd->reloc_off;
337 info->read += gd->reloc_off;
Simon Glasseda14ea2013-04-20 08:42:50 +0000338 info->write += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000339 }
Stephen Warren44161af2012-10-30 07:50:47 +0000340 relocated = 1;
341 }
342#endif
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000343
Simon Glasse76ee972016-02-29 15:25:44 -0700344 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000345 &fs_partition, 1);
346 if (part < 0)
347 return -1;
348
Simon Glass1aede482012-12-26 09:53:29 +0000349 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
350 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
351 fstype != info->fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000352 continue;
353
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700354 if (!fs_dev_desc && !info->null_dev_desc_ok)
355 continue;
356
Simon Glass6a46e2f2012-12-26 09:53:31 +0000357 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000358 fs_type = info->fstype;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400359 fs_dev_part = part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000360 return 0;
361 }
362 }
363
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000364 return -1;
365}
366
Rob Clark2b7bfd92017-09-09 13:15:55 -0400367/* set current blk device w/ blk_desc + partition # */
368int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
369{
370 struct fstype_info *info;
371 int ret, i;
372
373 if (part >= 1)
374 ret = part_get_info(desc, part, &fs_partition);
375 else
376 ret = part_get_info_whole_disk(desc, &fs_partition);
377 if (ret)
378 return ret;
379 fs_dev_desc = desc;
380
381 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
382 if (!info->probe(fs_dev_desc, &fs_partition)) {
383 fs_type = info->fstype;
AKASHI Takahiro43efaa02018-10-17 16:32:02 +0900384 fs_dev_part = part;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400385 return 0;
386 }
387 }
388
389 return -1;
390}
391
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000392static void fs_close(void)
393{
Simon Glasse6aad852012-12-26 09:53:30 +0000394 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000395
Simon Glasse6aad852012-12-26 09:53:30 +0000396 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000397
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000398 fs_type = FS_TYPE_ANY;
399}
400
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100401int fs_uuid(char *uuid_str)
402{
403 struct fstype_info *info = fs_get_info(fs_type);
404
405 return info->uuid(uuid_str);
406}
407
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000408int fs_ls(const char *dirname)
409{
410 int ret;
411
Simon Glasse6aad852012-12-26 09:53:30 +0000412 struct fstype_info *info = fs_get_info(fs_type);
413
414 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000415
Simon Glass19e38582012-12-26 09:53:33 +0000416 fs_type = FS_TYPE_ANY;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000417 fs_close();
418
419 return ret;
420}
421
Stephen Warrend008fbb2014-02-03 13:21:00 -0700422int fs_exists(const char *filename)
423{
424 int ret;
425
426 struct fstype_info *info = fs_get_info(fs_type);
427
428 ret = info->exists(filename);
429
430 fs_close();
431
432 return ret;
433}
434
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800435int fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600436{
437 int ret;
438
439 struct fstype_info *info = fs_get_info(fs_type);
440
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800441 ret = info->size(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600442
443 fs_close();
444
445 return ret;
446}
447
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100448#ifdef CONFIG_LMB
449/* Check if a file may be read to the given address */
450static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
451 loff_t len, struct fstype_info *info)
452{
453 struct lmb lmb;
454 int ret;
455 loff_t size;
456 loff_t read_len;
457
458 /* get the actual size of the file */
459 ret = info->size(filename, &size);
460 if (ret)
461 return ret;
462 if (offset >= size) {
463 /* offset >= EOF, no bytes will be written */
464 return 0;
465 }
466 read_len = size - offset;
467
468 /* limit to 'len' if it is smaller */
469 if (len && len < read_len)
470 read_len = len;
471
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100472 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100473 lmb_dump_all(&lmb);
474
475 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
476 return 0;
477
478 printf("** Reading file would overwrite reserved memory **\n");
479 return -ENOSPC;
480}
481#endif
482
483static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
484 int do_lmb_check, loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000485{
Simon Glasse6aad852012-12-26 09:53:30 +0000486 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000487 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000488 int ret;
489
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100490#ifdef CONFIG_LMB
491 if (do_lmb_check) {
492 ret = fs_read_lmb_check(filename, addr, offset, len, info);
493 if (ret)
494 return ret;
495 }
496#endif
497
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000498 /*
499 * We don't actually know how many bytes are being read, since len==0
500 * means read the whole file.
501 */
502 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800503 ret = info->read(filename, buf, offset, len, actread);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000504 unmap_sysmem(buf);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000505
Simon Glasse6aad852012-12-26 09:53:30 +0000506 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher56d9cdf2015-08-05 17:16:58 +0200507 if (ret == 0 && len && *actread != len)
Heinrich Schuchardtf02c5c52018-01-01 21:15:37 +0100508 debug("** %s shorter than offset + len **\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000509 fs_close();
510
511 return ret;
512}
513
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100514int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
515 loff_t *actread)
516{
517 return _fs_read(filename, addr, offset, len, 0, actread);
518}
519
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800520int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
521 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000522{
523 struct fstype_info *info = fs_get_info(fs_type);
524 void *buf;
525 int ret;
526
Simon Glasseda14ea2013-04-20 08:42:50 +0000527 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800528 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glasseda14ea2013-04-20 08:42:50 +0000529 unmap_sysmem(buf);
530
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800531 if (ret < 0 && len != *actwrite) {
Simon Glasseda14ea2013-04-20 08:42:50 +0000532 printf("** Unable to write file %s **\n", filename);
533 ret = -1;
534 }
535 fs_close();
536
537 return ret;
538}
539
Rob Clark2b7bfd92017-09-09 13:15:55 -0400540struct fs_dir_stream *fs_opendir(const char *filename)
541{
542 struct fstype_info *info = fs_get_info(fs_type);
543 struct fs_dir_stream *dirs = NULL;
544 int ret;
545
546 ret = info->opendir(filename, &dirs);
547 fs_close();
548 if (ret) {
549 errno = -ret;
550 return NULL;
551 }
552
553 dirs->desc = fs_dev_desc;
554 dirs->part = fs_dev_part;
555
556 return dirs;
557}
558
559struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
560{
561 struct fstype_info *info;
562 struct fs_dirent *dirent;
563 int ret;
564
565 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
566 info = fs_get_info(fs_type);
567
568 ret = info->readdir(dirs, &dirent);
569 fs_close();
570 if (ret) {
571 errno = -ret;
572 return NULL;
573 }
574
575 return dirent;
576}
577
578void fs_closedir(struct fs_dir_stream *dirs)
579{
580 struct fstype_info *info;
581
582 if (!dirs)
583 return;
584
585 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
586 info = fs_get_info(fs_type);
587
588 info->closedir(dirs);
589 fs_close();
590}
591
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900592int fs_unlink(const char *filename)
593{
594 int ret;
595
596 struct fstype_info *info = fs_get_info(fs_type);
597
598 ret = info->unlink(filename);
599
600 fs_type = FS_TYPE_ANY;
601 fs_close();
602
603 return ret;
604}
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900605
606int fs_mkdir(const char *dirname)
607{
608 int ret;
609
610 struct fstype_info *info = fs_get_info(fs_type);
611
612 ret = info->mkdir(dirname);
613
614 fs_type = FS_TYPE_ANY;
615 fs_close();
616
617 return ret;
618}
Rob Clark2b7bfd92017-09-09 13:15:55 -0400619
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100620int fs_ln(const char *fname, const char *target)
621{
622 struct fstype_info *info = fs_get_info(fs_type);
623 int ret;
624
625 ret = info->ln(fname, target);
626
627 if (ret < 0) {
628 printf("** Unable to create link %s -> %s **\n", fname, target);
629 ret = -1;
630 }
631 fs_close();
632
633 return ret;
634}
635
Stephen Warren3eb58f52014-06-11 12:47:26 -0600636int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
637 int fstype)
638{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800639 loff_t size;
Stephen Warren3eb58f52014-06-11 12:47:26 -0600640
641 if (argc != 4)
642 return CMD_RET_USAGE;
643
644 if (fs_set_blk_dev(argv[1], argv[2], fstype))
645 return 1;
646
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800647 if (fs_size(argv[3], &size) < 0)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600648 return CMD_RET_FAILURE;
649
Simon Glass4d949a22017-08-03 12:22:10 -0600650 env_set_hex("filesize", size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600651
652 return 0;
653}
654
Stephen Warren128d3d92012-10-31 11:05:07 +0000655int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200656 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000657{
658 unsigned long addr;
659 const char *addr_str;
660 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800661 loff_t bytes;
662 loff_t pos;
663 loff_t len_read;
664 int ret;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100665 unsigned long time;
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200666 char *ep;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000667
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000668 if (argc < 2)
669 return CMD_RET_USAGE;
670 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000671 return CMD_RET_USAGE;
672
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000673 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000674 return 1;
675
676 if (argc >= 4) {
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200677 addr = simple_strtoul(argv[3], &ep, 16);
678 if (ep == argv[3] || *ep != '\0')
679 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000680 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600681 addr_str = env_get("loadaddr");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000682 if (addr_str != NULL)
683 addr = simple_strtoul(addr_str, NULL, 16);
684 else
685 addr = CONFIG_SYS_LOAD_ADDR;
686 }
687 if (argc >= 5) {
688 filename = argv[4];
689 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600690 filename = env_get("bootfile");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000691 if (!filename) {
692 puts("** No boot file defined **\n");
693 return 1;
694 }
695 }
696 if (argc >= 6)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200697 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000698 else
699 bytes = 0;
700 if (argc >= 7)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200701 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000702 else
703 pos = 0;
704
Mian Yousaf Kaukab961ebe82019-06-18 15:03:44 +0200705#ifdef CONFIG_CMD_BOOTEFI
706 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
707 (argc > 4) ? argv[4] : "");
708#endif
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100709 time = get_timer(0);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100710 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100711 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800712 if (ret < 0)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000713 return 1;
714
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800715 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100716 if (time > 0) {
717 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500718 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100719 puts(")");
720 }
721 puts("\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000722
Simon Glass4d949a22017-08-03 12:22:10 -0600723 env_set_hex("fileaddr", addr);
724 env_set_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000725
726 return 0;
727}
728
729int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
730 int fstype)
731{
732 if (argc < 2)
733 return CMD_RET_USAGE;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000734 if (argc > 4)
735 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000736
737 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
738 return 1;
739
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000740 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000741 return 1;
742
743 return 0;
744}
Simon Glasseda14ea2013-04-20 08:42:50 +0000745
Stephen Warrend008fbb2014-02-03 13:21:00 -0700746int file_exists(const char *dev_type, const char *dev_part, const char *file,
747 int fstype)
748{
749 if (fs_set_blk_dev(dev_type, dev_part, fstype))
750 return 0;
751
752 return fs_exists(file);
753}
754
Simon Glasseda14ea2013-04-20 08:42:50 +0000755int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200756 int fstype)
Simon Glasseda14ea2013-04-20 08:42:50 +0000757{
758 unsigned long addr;
759 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800760 loff_t bytes;
761 loff_t pos;
762 loff_t len;
763 int ret;
Simon Glasseda14ea2013-04-20 08:42:50 +0000764 unsigned long time;
765
766 if (argc < 6 || argc > 7)
767 return CMD_RET_USAGE;
768
769 if (fs_set_blk_dev(argv[1], argv[2], fstype))
770 return 1;
771
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800772 addr = simple_strtoul(argv[3], NULL, 16);
773 filename = argv[4];
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200774 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glasseda14ea2013-04-20 08:42:50 +0000775 if (argc >= 7)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200776 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glasseda14ea2013-04-20 08:42:50 +0000777 else
778 pos = 0;
779
780 time = get_timer(0);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800781 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glasseda14ea2013-04-20 08:42:50 +0000782 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800783 if (ret < 0)
Simon Glasseda14ea2013-04-20 08:42:50 +0000784 return 1;
785
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800786 printf("%llu bytes written in %lu ms", len, time);
Simon Glasseda14ea2013-04-20 08:42:50 +0000787 if (time > 0) {
788 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500789 print_size(div_u64(len, time) * 1000, "/s");
Simon Glasseda14ea2013-04-20 08:42:50 +0000790 puts(")");
791 }
792 puts("\n");
793
794 return 0;
795}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100796
797int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
798 int fstype)
799{
800 int ret;
801 char uuid[37];
802 memset(uuid, 0, sizeof(uuid));
803
804 if (argc < 3 || argc > 4)
805 return CMD_RET_USAGE;
806
807 if (fs_set_blk_dev(argv[1], argv[2], fstype))
808 return 1;
809
810 ret = fs_uuid(uuid);
811 if (ret)
812 return CMD_RET_FAILURE;
813
814 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600815 env_set(argv[3], uuid);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100816 else
817 printf("%s\n", uuid);
818
819 return CMD_RET_SUCCESS;
820}
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100821
822int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
823{
824 struct fstype_info *info;
825
826 if (argc < 3 || argc > 4)
827 return CMD_RET_USAGE;
828
829 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
830 return 1;
831
832 info = fs_get_info(fs_type);
833
834 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600835 env_set(argv[3], info->name);
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100836 else
837 printf("%s\n", info->name);
838
Marek Vasut08d1c422019-02-06 13:19:29 +0100839 fs_close();
840
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100841 return CMD_RET_SUCCESS;
842}
843
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900844int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
845 int fstype)
846{
847 if (argc != 4)
848 return CMD_RET_USAGE;
849
850 if (fs_set_blk_dev(argv[1], argv[2], fstype))
851 return 1;
852
853 if (fs_unlink(argv[3]))
854 return 1;
855
856 return 0;
857}
858
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900859int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
860 int fstype)
861{
862 int ret;
863
864 if (argc != 4)
865 return CMD_RET_USAGE;
866
867 if (fs_set_blk_dev(argv[1], argv[2], fstype))
868 return 1;
869
870 ret = fs_mkdir(argv[3]);
871 if (ret) {
872 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
873 return 1;
874 }
875
876 return 0;
877}
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100878
879int do_ln(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
880 int fstype)
881{
882 if (argc != 5)
883 return CMD_RET_USAGE;
884
885 if (fs_set_blk_dev(argv[1], argv[2], fstype))
886 return 1;
887
888 if (fs_ln(argv[3], argv[4]))
889 return 1;
890
891 return 0;
892}