blob: edd8adc21b97870d0236a95dbb339fbdafd3d387 [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
Simon Glassed38aef2020-05-10 11:40:03 -06006#include <command.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +00007#include <config.h>
Christian Gmeiner9f9eec32014-11-12 14:35:04 +01008#include <errno.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +00009#include <common.h>
Simon Glass313112a2019-08-01 09:46:46 -060010#include <env.h>
Simon Glass655306c2020-05-10 11:39:58 -060011#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050013#include <mapmem.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000014#include <part.h>
15#include <ext4fs.h>
16#include <fat.h>
17#include <fs.h>
Simon Glass11842872012-12-26 09:53:35 +000018#include <sandboxfs.h>
Hans de Goede690c7962015-09-17 18:46:58 -040019#include <ubifs_uboot.h>
Marek BehĂșn98ec1d12017-09-03 17:00:29 +020020#include <btrfs.h>
Simon Glasscbe5d5d2012-12-26 09:53:32 +000021#include <asm/io.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050022#include <div64.h>
23#include <linux/math64.h>
Mian Yousaf Kaukab961ebe82019-06-18 15:03:44 +020024#include <efi_loader.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000025
Stephen Warren44161af2012-10-30 07:50:47 +000026DECLARE_GLOBAL_DATA_PTR;
27
Simon Glasse3394752016-02-29 15:25:34 -070028static struct blk_desc *fs_dev_desc;
Rob Clark2b7bfd92017-09-09 13:15:55 -040029static int fs_dev_part;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060030static struct disk_partition fs_partition;
Stephen Warreneefbc3f2012-10-22 06:43:51 +000031static int fs_type = FS_TYPE_ANY;
32
Simon Glasse3394752016-02-29 15:25:34 -070033static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -060034 struct disk_partition *fs_partition)
Simon Glass1aede482012-12-26 09:53:29 +000035{
36 printf("** Unrecognized filesystem type **\n");
37 return -1;
38}
39
Stephen Warreneefbc3f2012-10-22 06:43:51 +000040static inline int fs_ls_unsupported(const char *dirname)
41{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000042 return -1;
43}
44
Rob Clark60d74a92017-09-09 13:15:58 -040045/* generic implementation of ls in terms of opendir/readdir/closedir */
46__maybe_unused
47static int fs_ls_generic(const char *dirname)
48{
49 struct fs_dir_stream *dirs;
50 struct fs_dirent *dent;
51 int nfiles = 0, ndirs = 0;
52
53 dirs = fs_opendir(dirname);
54 if (!dirs)
55 return -errno;
56
57 while ((dent = fs_readdir(dirs))) {
58 if (dent->type == FS_DT_DIR) {
59 printf(" %s/\n", dent->name);
60 ndirs++;
61 } else {
62 printf(" %8lld %s\n", dent->size, dent->name);
63 nfiles++;
64 }
65 }
66
67 fs_closedir(dirs);
68
69 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
70
71 return 0;
72}
73
Stephen Warrend008fbb2014-02-03 13:21:00 -070074static inline int fs_exists_unsupported(const char *filename)
75{
76 return 0;
77}
78
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080079static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -060080{
81 return -1;
82}
83
Simon Glasscbe5d5d2012-12-26 09:53:32 +000084static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080085 loff_t offset, loff_t len,
86 loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +000087{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000088 return -1;
89}
90
Simon Glasseda14ea2013-04-20 08:42:50 +000091static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080092 loff_t offset, loff_t len,
93 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +000094{
95 return -1;
96}
97
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +010098static inline int fs_ln_unsupported(const char *filename, const char *target)
99{
100 return -1;
101}
102
Simon Glass1aede482012-12-26 09:53:29 +0000103static inline void fs_close_unsupported(void)
104{
105}
106
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100107static inline int fs_uuid_unsupported(char *uuid_str)
108{
109 return -1;
110}
111
Rob Clark2b7bfd92017-09-09 13:15:55 -0400112static inline int fs_opendir_unsupported(const char *filename,
113 struct fs_dir_stream **dirs)
114{
115 return -EACCES;
116}
117
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900118static inline int fs_unlink_unsupported(const char *filename)
119{
120 return -1;
121}
122
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900123static inline int fs_mkdir_unsupported(const char *dirname)
124{
125 return -1;
126}
127
Simon Glass1aede482012-12-26 09:53:29 +0000128struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000129 int fstype;
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100130 char *name;
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700131 /*
132 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
133 * should be false in most cases. For "virtual" filesystems which
134 * aren't based on a U-Boot block device (e.g. sandbox), this can be
Heinrich Schuchardt99dc17d2018-08-11 15:52:14 +0200135 * set to true. This should also be true for the dummy entry at the end
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700136 * of fstypes[], since that is essentially a "virtual" (non-existent)
137 * filesystem.
138 */
139 bool null_dev_desc_ok;
Simon Glasse3394752016-02-29 15:25:34 -0700140 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600141 struct disk_partition *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +0000142 int (*ls)(const char *dirname);
Stephen Warrend008fbb2014-02-03 13:21:00 -0700143 int (*exists)(const char *filename);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800144 int (*size)(const char *filename, loff_t *size);
145 int (*read)(const char *filename, void *buf, loff_t offset,
146 loff_t len, loff_t *actread);
147 int (*write)(const char *filename, void *buf, loff_t offset,
148 loff_t len, loff_t *actwrite);
Simon Glass1aede482012-12-26 09:53:29 +0000149 void (*close)(void);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100150 int (*uuid)(char *uuid_str);
Rob Clark2b7bfd92017-09-09 13:15:55 -0400151 /*
152 * Open a directory stream. On success return 0 and directory
153 * stream pointer via 'dirsp'. On error, return -errno. See
154 * fs_opendir().
155 */
156 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
157 /*
158 * Read next entry from directory stream. On success return 0
159 * and directory entry pointer via 'dentp'. On error return
160 * -errno. See fs_readdir().
161 */
162 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
163 /* see fs_closedir() */
164 void (*closedir)(struct fs_dir_stream *dirs);
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900165 int (*unlink)(const char *filename);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900166 int (*mkdir)(const char *dirname);
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100167 int (*ln)(const char *filename, const char *target);
Simon Glass1aede482012-12-26 09:53:29 +0000168};
169
170static struct fstype_info fstypes[] = {
171#ifdef CONFIG_FS_FAT
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000172 {
173 .fstype = FS_TYPE_FAT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100174 .name = "fat",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700175 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000176 .probe = fat_set_blk_dev,
177 .close = fat_close,
Rob Clark60d74a92017-09-09 13:15:58 -0400178 .ls = fs_ls_generic,
Stephen Warren1d2f9a02014-02-03 13:21:10 -0700179 .exists = fat_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600180 .size = fat_size,
Simon Glass19e38582012-12-26 09:53:33 +0000181 .read = fat_read_file,
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800182#if CONFIG_IS_ENABLED(FAT_WRITE)
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800183 .write = file_fat_write,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900184 .unlink = fat_unlink,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900185 .mkdir = fat_mkdir,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800186#else
Stephen Warren9df25802014-02-03 13:20:59 -0700187 .write = fs_write_unsupported,
AKASHI Takahiro73b34972018-09-11 15:59:14 +0900188 .unlink = fs_unlink_unsupported,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900189 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800190#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100191 .uuid = fs_uuid_unsupported,
Rob Clark60d74a92017-09-09 13:15:58 -0400192 .opendir = fat_opendir,
193 .readdir = fat_readdir,
194 .closedir = fat_closedir,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100195 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000196 },
Simon Glass1aede482012-12-26 09:53:29 +0000197#endif
Tien Fong Chee64eab042019-01-23 14:20:06 +0800198
199#if CONFIG_IS_ENABLED(FS_EXT4)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000200 {
201 .fstype = FS_TYPE_EXT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100202 .name = "ext4",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700203 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000204 .probe = ext4fs_probe,
205 .close = ext4fs_close,
Simon Glass1aede482012-12-26 09:53:29 +0000206 .ls = ext4fs_ls,
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700207 .exists = ext4fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600208 .size = ext4fs_size,
Simon Glass19e38582012-12-26 09:53:33 +0000209 .read = ext4_read_file,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800210#ifdef CONFIG_CMD_EXT4_WRITE
211 .write = ext4_write_file,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100212 .ln = ext4fs_create_link,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800213#else
Stephen Warren9df25802014-02-03 13:20:59 -0700214 .write = fs_write_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100215 .ln = fs_ln_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800216#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100217 .uuid = ext4fs_uuid,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400218 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900219 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900220 .mkdir = fs_mkdir_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000221 },
222#endif
Simon Glass11842872012-12-26 09:53:35 +0000223#ifdef CONFIG_SANDBOX
224 {
225 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100226 .name = "sandbox",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700227 .null_dev_desc_ok = true,
Simon Glass11842872012-12-26 09:53:35 +0000228 .probe = sandbox_fs_set_blk_dev,
229 .close = sandbox_fs_close,
230 .ls = sandbox_fs_ls,
Stephen Warren97d66f22014-02-03 13:21:07 -0700231 .exists = sandbox_fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600232 .size = sandbox_fs_size,
Simon Glass11842872012-12-26 09:53:35 +0000233 .read = fs_read_sandbox,
Simon Glassea307e82013-04-20 08:42:51 +0000234 .write = fs_write_sandbox,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100235 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400236 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900237 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900238 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100239 .ln = fs_ln_unsupported,
Simon Glass11842872012-12-26 09:53:35 +0000240 },
241#endif
Hans de Goede690c7962015-09-17 18:46:58 -0400242#ifdef CONFIG_CMD_UBIFS
243 {
244 .fstype = FS_TYPE_UBIFS,
245 .name = "ubifs",
246 .null_dev_desc_ok = true,
247 .probe = ubifs_set_blk_dev,
248 .close = ubifs_close,
249 .ls = ubifs_ls,
250 .exists = ubifs_exists,
251 .size = ubifs_size,
252 .read = ubifs_read,
253 .write = fs_write_unsupported,
254 .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,
Hans de Goede690c7962015-09-17 18:46:58 -0400259 },
260#endif
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200261#ifdef CONFIG_FS_BTRFS
262 {
263 .fstype = FS_TYPE_BTRFS,
264 .name = "btrfs",
265 .null_dev_desc_ok = false,
266 .probe = btrfs_probe,
267 .close = btrfs_close,
268 .ls = btrfs_ls,
269 .exists = btrfs_exists,
270 .size = btrfs_size,
271 .read = btrfs_read,
272 .write = fs_write_unsupported,
273 .uuid = btrfs_uuid,
Marek BehĂșn90762d92017-10-06 16:56:07 +0200274 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900275 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900276 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100277 .ln = fs_ln_unsupported,
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200278 },
279#endif
Simon Glass1aede482012-12-26 09:53:29 +0000280 {
281 .fstype = FS_TYPE_ANY,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100282 .name = "unsupported",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700283 .null_dev_desc_ok = true,
Simon Glass1aede482012-12-26 09:53:29 +0000284 .probe = fs_probe_unsupported,
285 .close = fs_close_unsupported,
286 .ls = fs_ls_unsupported,
Stephen Warrend008fbb2014-02-03 13:21:00 -0700287 .exists = fs_exists_unsupported,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600288 .size = fs_size_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000289 .read = fs_read_unsupported,
Simon Glasseda14ea2013-04-20 08:42:50 +0000290 .write = fs_write_unsupported,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100291 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400292 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900293 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900294 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100295 .ln = fs_ln_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000296 },
297};
298
Simon Glasse6aad852012-12-26 09:53:30 +0000299static struct fstype_info *fs_get_info(int fstype)
300{
301 struct fstype_info *info;
302 int i;
303
304 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
305 if (fstype == info->fstype)
306 return info;
307 }
308
309 /* Return the 'unsupported' sentinel */
310 return info;
311}
312
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000313/**
AKASHI Takahiro80ac7342019-10-07 14:59:37 +0900314 * fs_get_type() - Get type of current filesystem
315 *
316 * Return: filesystem type
317 *
318 * Returns filesystem type representing the current filesystem, or
319 * FS_TYPE_ANY for any unrecognised filesystem.
320 */
321int fs_get_type(void)
322{
323 return fs_type;
324}
325
326/**
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000327 * fs_get_type_name() - Get type of current filesystem
328 *
329 * Return: Pointer to filesystem name
330 *
331 * Returns a string describing the current filesystem, or the sentinel
332 * "unsupported" for any unrecognised filesystem.
333 */
334const char *fs_get_type_name(void)
335{
336 return fs_get_info(fs_type)->name;
337}
338
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000339int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
340{
Simon Glass1aede482012-12-26 09:53:29 +0000341 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000342 int part, i;
Stephen Warren44161af2012-10-30 07:50:47 +0000343#ifdef CONFIG_NEEDS_MANUAL_RELOC
344 static int relocated;
345
346 if (!relocated) {
Simon Glass1aede482012-12-26 09:53:29 +0000347 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
348 i++, info++) {
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100349 info->name += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000350 info->probe += gd->reloc_off;
351 info->close += gd->reloc_off;
352 info->ls += gd->reloc_off;
353 info->read += gd->reloc_off;
Simon Glasseda14ea2013-04-20 08:42:50 +0000354 info->write += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000355 }
Stephen Warren44161af2012-10-30 07:50:47 +0000356 relocated = 1;
357 }
358#endif
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000359
Simon Glasse76ee972016-02-29 15:25:44 -0700360 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000361 &fs_partition, 1);
362 if (part < 0)
363 return -1;
364
Simon Glass1aede482012-12-26 09:53:29 +0000365 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
366 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
367 fstype != info->fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000368 continue;
369
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700370 if (!fs_dev_desc && !info->null_dev_desc_ok)
371 continue;
372
Simon Glass6a46e2f2012-12-26 09:53:31 +0000373 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000374 fs_type = info->fstype;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400375 fs_dev_part = part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000376 return 0;
377 }
378 }
379
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000380 return -1;
381}
382
Rob Clark2b7bfd92017-09-09 13:15:55 -0400383/* set current blk device w/ blk_desc + partition # */
384int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
385{
386 struct fstype_info *info;
387 int ret, i;
388
389 if (part >= 1)
390 ret = part_get_info(desc, part, &fs_partition);
391 else
392 ret = part_get_info_whole_disk(desc, &fs_partition);
393 if (ret)
394 return ret;
395 fs_dev_desc = desc;
396
397 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
398 if (!info->probe(fs_dev_desc, &fs_partition)) {
399 fs_type = info->fstype;
AKASHI Takahiro43efaa02018-10-17 16:32:02 +0900400 fs_dev_part = part;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400401 return 0;
402 }
403 }
404
405 return -1;
406}
407
AKASHI Takahiroca8e3772019-10-07 14:59:35 +0900408void fs_close(void)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000409{
Simon Glasse6aad852012-12-26 09:53:30 +0000410 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000411
Simon Glasse6aad852012-12-26 09:53:30 +0000412 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000413
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000414 fs_type = FS_TYPE_ANY;
415}
416
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100417int fs_uuid(char *uuid_str)
418{
419 struct fstype_info *info = fs_get_info(fs_type);
420
421 return info->uuid(uuid_str);
422}
423
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000424int fs_ls(const char *dirname)
425{
426 int ret;
427
Simon Glasse6aad852012-12-26 09:53:30 +0000428 struct fstype_info *info = fs_get_info(fs_type);
429
430 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000431
432 fs_close();
433
434 return ret;
435}
436
Stephen Warrend008fbb2014-02-03 13:21:00 -0700437int fs_exists(const char *filename)
438{
439 int ret;
440
441 struct fstype_info *info = fs_get_info(fs_type);
442
443 ret = info->exists(filename);
444
445 fs_close();
446
447 return ret;
448}
449
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800450int fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600451{
452 int ret;
453
454 struct fstype_info *info = fs_get_info(fs_type);
455
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800456 ret = info->size(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600457
458 fs_close();
459
460 return ret;
461}
462
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100463#ifdef CONFIG_LMB
464/* Check if a file may be read to the given address */
465static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
466 loff_t len, struct fstype_info *info)
467{
468 struct lmb lmb;
469 int ret;
470 loff_t size;
471 loff_t read_len;
472
473 /* get the actual size of the file */
474 ret = info->size(filename, &size);
475 if (ret)
476 return ret;
477 if (offset >= size) {
478 /* offset >= EOF, no bytes will be written */
479 return 0;
480 }
481 read_len = size - offset;
482
483 /* limit to 'len' if it is smaller */
484 if (len && len < read_len)
485 read_len = len;
486
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100487 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100488 lmb_dump_all(&lmb);
489
490 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
491 return 0;
492
493 printf("** Reading file would overwrite reserved memory **\n");
494 return -ENOSPC;
495}
496#endif
497
498static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
499 int do_lmb_check, loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000500{
Simon Glasse6aad852012-12-26 09:53:30 +0000501 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000502 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000503 int ret;
504
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100505#ifdef CONFIG_LMB
506 if (do_lmb_check) {
507 ret = fs_read_lmb_check(filename, addr, offset, len, info);
508 if (ret)
509 return ret;
510 }
511#endif
512
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000513 /*
514 * We don't actually know how many bytes are being read, since len==0
515 * means read the whole file.
516 */
517 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800518 ret = info->read(filename, buf, offset, len, actread);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000519 unmap_sysmem(buf);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000520
Simon Glasse6aad852012-12-26 09:53:30 +0000521 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher56d9cdf2015-08-05 17:16:58 +0200522 if (ret == 0 && len && *actread != len)
Heinrich Schuchardtf02c5c52018-01-01 21:15:37 +0100523 debug("** %s shorter than offset + len **\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000524 fs_close();
525
526 return ret;
527}
528
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100529int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
530 loff_t *actread)
531{
532 return _fs_read(filename, addr, offset, len, 0, actread);
533}
534
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800535int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
536 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000537{
538 struct fstype_info *info = fs_get_info(fs_type);
539 void *buf;
540 int ret;
541
Simon Glasseda14ea2013-04-20 08:42:50 +0000542 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800543 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glasseda14ea2013-04-20 08:42:50 +0000544 unmap_sysmem(buf);
545
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800546 if (ret < 0 && len != *actwrite) {
Simon Glasseda14ea2013-04-20 08:42:50 +0000547 printf("** Unable to write file %s **\n", filename);
548 ret = -1;
549 }
550 fs_close();
551
552 return ret;
553}
554
Rob Clark2b7bfd92017-09-09 13:15:55 -0400555struct fs_dir_stream *fs_opendir(const char *filename)
556{
557 struct fstype_info *info = fs_get_info(fs_type);
558 struct fs_dir_stream *dirs = NULL;
559 int ret;
560
561 ret = info->opendir(filename, &dirs);
562 fs_close();
563 if (ret) {
564 errno = -ret;
565 return NULL;
566 }
567
568 dirs->desc = fs_dev_desc;
569 dirs->part = fs_dev_part;
570
571 return dirs;
572}
573
574struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
575{
576 struct fstype_info *info;
577 struct fs_dirent *dirent;
578 int ret;
579
580 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
581 info = fs_get_info(fs_type);
582
583 ret = info->readdir(dirs, &dirent);
584 fs_close();
585 if (ret) {
586 errno = -ret;
587 return NULL;
588 }
589
590 return dirent;
591}
592
593void fs_closedir(struct fs_dir_stream *dirs)
594{
595 struct fstype_info *info;
596
597 if (!dirs)
598 return;
599
600 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
601 info = fs_get_info(fs_type);
602
603 info->closedir(dirs);
604 fs_close();
605}
606
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900607int fs_unlink(const char *filename)
608{
609 int ret;
610
611 struct fstype_info *info = fs_get_info(fs_type);
612
613 ret = info->unlink(filename);
614
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900615 fs_close();
616
617 return ret;
618}
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900619
620int fs_mkdir(const char *dirname)
621{
622 int ret;
623
624 struct fstype_info *info = fs_get_info(fs_type);
625
626 ret = info->mkdir(dirname);
627
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900628 fs_close();
629
630 return ret;
631}
Rob Clark2b7bfd92017-09-09 13:15:55 -0400632
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100633int fs_ln(const char *fname, const char *target)
634{
635 struct fstype_info *info = fs_get_info(fs_type);
636 int ret;
637
638 ret = info->ln(fname, target);
639
640 if (ret < 0) {
641 printf("** Unable to create link %s -> %s **\n", fname, target);
642 ret = -1;
643 }
644 fs_close();
645
646 return ret;
647}
648
Simon Glassed38aef2020-05-10 11:40:03 -0600649int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
650 int fstype)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600651{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800652 loff_t size;
Stephen Warren3eb58f52014-06-11 12:47:26 -0600653
654 if (argc != 4)
655 return CMD_RET_USAGE;
656
657 if (fs_set_blk_dev(argv[1], argv[2], fstype))
658 return 1;
659
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800660 if (fs_size(argv[3], &size) < 0)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600661 return CMD_RET_FAILURE;
662
Simon Glass4d949a22017-08-03 12:22:10 -0600663 env_set_hex("filesize", size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600664
665 return 0;
666}
667
Simon Glassed38aef2020-05-10 11:40:03 -0600668int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
669 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000670{
671 unsigned long addr;
672 const char *addr_str;
673 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800674 loff_t bytes;
675 loff_t pos;
676 loff_t len_read;
677 int ret;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100678 unsigned long time;
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200679 char *ep;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000680
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000681 if (argc < 2)
682 return CMD_RET_USAGE;
683 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000684 return CMD_RET_USAGE;
685
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000686 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000687 return 1;
688
689 if (argc >= 4) {
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200690 addr = simple_strtoul(argv[3], &ep, 16);
691 if (ep == argv[3] || *ep != '\0')
692 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000693 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600694 addr_str = env_get("loadaddr");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000695 if (addr_str != NULL)
696 addr = simple_strtoul(addr_str, NULL, 16);
697 else
698 addr = CONFIG_SYS_LOAD_ADDR;
699 }
700 if (argc >= 5) {
701 filename = argv[4];
702 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600703 filename = env_get("bootfile");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000704 if (!filename) {
705 puts("** No boot file defined **\n");
706 return 1;
707 }
708 }
709 if (argc >= 6)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200710 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000711 else
712 bytes = 0;
713 if (argc >= 7)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200714 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000715 else
716 pos = 0;
717
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100718 time = get_timer(0);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100719 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100720 time = get_timer(time);
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200721 if (ret < 0) {
722 printf("Failed to load '%s'\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000723 return 1;
Heinrich Schuchardtca816ce2020-06-29 20:08:49 +0200724 }
725
726 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
727 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
728 (argc > 4) ? argv[4] : "");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000729
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800730 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100731 if (time > 0) {
732 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500733 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100734 puts(")");
735 }
736 puts("\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000737
Simon Glass4d949a22017-08-03 12:22:10 -0600738 env_set_hex("fileaddr", addr);
739 env_set_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000740
741 return 0;
742}
743
Simon Glassed38aef2020-05-10 11:40:03 -0600744int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
745 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000746{
747 if (argc < 2)
748 return CMD_RET_USAGE;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000749 if (argc > 4)
750 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000751
752 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
753 return 1;
754
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000755 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000756 return 1;
757
758 return 0;
759}
Simon Glasseda14ea2013-04-20 08:42:50 +0000760
Stephen Warrend008fbb2014-02-03 13:21:00 -0700761int file_exists(const char *dev_type, const char *dev_part, const char *file,
762 int fstype)
763{
764 if (fs_set_blk_dev(dev_type, dev_part, fstype))
765 return 0;
766
767 return fs_exists(file);
768}
769
Simon Glassed38aef2020-05-10 11:40:03 -0600770int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
771 int fstype)
Simon Glasseda14ea2013-04-20 08:42:50 +0000772{
773 unsigned long addr;
774 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800775 loff_t bytes;
776 loff_t pos;
777 loff_t len;
778 int ret;
Simon Glasseda14ea2013-04-20 08:42:50 +0000779 unsigned long time;
780
781 if (argc < 6 || argc > 7)
782 return CMD_RET_USAGE;
783
784 if (fs_set_blk_dev(argv[1], argv[2], fstype))
785 return 1;
786
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800787 addr = simple_strtoul(argv[3], NULL, 16);
788 filename = argv[4];
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200789 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glasseda14ea2013-04-20 08:42:50 +0000790 if (argc >= 7)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200791 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glasseda14ea2013-04-20 08:42:50 +0000792 else
793 pos = 0;
794
795 time = get_timer(0);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800796 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glasseda14ea2013-04-20 08:42:50 +0000797 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800798 if (ret < 0)
Simon Glasseda14ea2013-04-20 08:42:50 +0000799 return 1;
800
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800801 printf("%llu bytes written in %lu ms", len, time);
Simon Glasseda14ea2013-04-20 08:42:50 +0000802 if (time > 0) {
803 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500804 print_size(div_u64(len, time) * 1000, "/s");
Simon Glasseda14ea2013-04-20 08:42:50 +0000805 puts(")");
806 }
807 puts("\n");
808
809 return 0;
810}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100811
Simon Glassed38aef2020-05-10 11:40:03 -0600812int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
813 int fstype)
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100814{
815 int ret;
816 char uuid[37];
817 memset(uuid, 0, sizeof(uuid));
818
819 if (argc < 3 || argc > 4)
820 return CMD_RET_USAGE;
821
822 if (fs_set_blk_dev(argv[1], argv[2], fstype))
823 return 1;
824
825 ret = fs_uuid(uuid);
826 if (ret)
827 return CMD_RET_FAILURE;
828
829 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600830 env_set(argv[3], uuid);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100831 else
832 printf("%s\n", uuid);
833
834 return CMD_RET_SUCCESS;
835}
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100836
Simon Glassed38aef2020-05-10 11:40:03 -0600837int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100838{
839 struct fstype_info *info;
840
841 if (argc < 3 || argc > 4)
842 return CMD_RET_USAGE;
843
844 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
845 return 1;
846
847 info = fs_get_info(fs_type);
848
849 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600850 env_set(argv[3], info->name);
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100851 else
852 printf("%s\n", info->name);
853
Marek Vasut08d1c422019-02-06 13:19:29 +0100854 fs_close();
855
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100856 return CMD_RET_SUCCESS;
857}
858
Simon Glassed38aef2020-05-10 11:40:03 -0600859int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900860 int fstype)
861{
862 if (argc != 4)
863 return CMD_RET_USAGE;
864
865 if (fs_set_blk_dev(argv[1], argv[2], fstype))
866 return 1;
867
868 if (fs_unlink(argv[3]))
869 return 1;
870
871 return 0;
872}
873
Simon Glassed38aef2020-05-10 11:40:03 -0600874int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900875 int fstype)
876{
877 int ret;
878
879 if (argc != 4)
880 return CMD_RET_USAGE;
881
882 if (fs_set_blk_dev(argv[1], argv[2], fstype))
883 return 1;
884
885 ret = fs_mkdir(argv[3]);
886 if (ret) {
887 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
888 return 1;
889 }
890
891 return 0;
892}
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100893
Simon Glassed38aef2020-05-10 11:40:03 -0600894int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100895 int fstype)
896{
897 if (argc != 5)
898 return CMD_RET_USAGE;
899
900 if (fs_set_blk_dev(argv[1], argv[2], fstype))
901 return 1;
902
903 if (fs_ln(argv[3], argv[4]))
904 return 1;
905
906 return 0;
907}
Niel Fourie9252d702020-03-24 16:17:04 +0100908
909int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
910{
911 struct fstype_info *drv = fstypes;
912 const int n_ents = ARRAY_SIZE(fstypes);
913 struct fstype_info *entry;
914 int i = 0;
915
916 puts("Supported filesystems");
917 for (entry = drv; entry != drv + n_ents; entry++) {
918 if (entry->fstype != FS_TYPE_ANY) {
919 printf("%c %s", i ? ',' : ':', entry->name);
920 i++;
921 }
922 }
923 if (!i)
924 puts(": <none>");
925 puts("\n");
926 return CMD_RET_SUCCESS;
927}