blob: ad4caaeb1ec9c1f7f502df20d0dce92df6471b1a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren045fa1e2012-10-22 06:43:51 +00002/*
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
Stephen Warren045fa1e2012-10-22 06:43:51 +00004 */
5
Simon Glass09140112020-05-10 11:40:03 -06006#include <command.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +00007#include <config.h>
Christian Gmeiner59e890e2014-11-12 14:35:04 +01008#include <errno.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +00009#include <common.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060010#include <env.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060011#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050013#include <mapmem.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000014#include <part.h>
15#include <ext4fs.h>
16#include <fat.h>
17#include <fs.h>
Simon Glass92ccc962012-12-26 09:53:35 +000018#include <sandboxfs.h>
Hans de Goede251cee02015-09-17 18:46:58 -040019#include <ubifs_uboot.h>
Marek BehĂșn0c936ee2017-09-03 17:00:29 +020020#include <btrfs.h>
Simon Glass117e0502012-12-26 09:53:32 +000021#include <asm/io.h>
Tom Rini9e374e72014-11-24 11:50:46 -050022#include <div64.h>
23#include <linux/math64.h>
Mian Yousaf Kaukabee88eac2019-06-18 15:03:44 +020024#include <efi_loader.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000025
Stephen Warrena1b231c2012-10-30 07:50:47 +000026DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass4101f682016-02-29 15:25:34 -070028static struct blk_desc *fs_dev_desc;
Rob Clark4bbcc962017-09-09 13:15:55 -040029static int fs_dev_part;
Simon Glass05289792020-05-10 11:39:57 -060030static struct disk_partition fs_partition;
Stephen Warren045fa1e2012-10-22 06:43:51 +000031static int fs_type = FS_TYPE_ANY;
32
Simon Glass4101f682016-02-29 15:25:34 -070033static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glass05289792020-05-10 11:39:57 -060034 struct disk_partition *fs_partition)
Simon Glass436e2b72012-12-26 09:53:29 +000035{
36 printf("** Unrecognized filesystem type **\n");
37 return -1;
38}
39
Stephen Warren045fa1e2012-10-22 06:43:51 +000040static inline int fs_ls_unsupported(const char *dirname)
41{
Stephen Warren045fa1e2012-10-22 06:43:51 +000042 return -1;
43}
44
Rob Clark89191d62017-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 Warren61529162014-02-03 13:21:00 -070074static inline int fs_exists_unsupported(const char *filename)
75{
76 return 0;
77}
78
Suriyan Ramasamid455d872014-11-17 14:39:38 -080079static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -060080{
81 return -1;
82}
83
Simon Glass117e0502012-12-26 09:53:32 +000084static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080085 loff_t offset, loff_t len,
86 loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +000087{
Stephen Warren045fa1e2012-10-22 06:43:51 +000088 return -1;
89}
90
Simon Glassa8f6ab52013-04-20 08:42:50 +000091static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080092 loff_t offset, loff_t len,
93 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +000094{
95 return -1;
96}
97
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +010098static inline int fs_ln_unsupported(const char *filename, const char *target)
99{
100 return -1;
101}
102
Simon Glass436e2b72012-12-26 09:53:29 +0000103static inline void fs_close_unsupported(void)
104{
105}
106
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100107static inline int fs_uuid_unsupported(char *uuid_str)
108{
109 return -1;
110}
111
Rob Clark4bbcc962017-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 Takahiroe2519da2018-09-11 15:59:13 +0900118static inline int fs_unlink_unsupported(const char *filename)
119{
120 return -1;
121}
122
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900123static inline int fs_mkdir_unsupported(const char *dirname)
124{
125 return -1;
126}
127
Simon Glass436e2b72012-12-26 09:53:29 +0000128struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +0000129 int fstype;
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100130 char *name;
Stephen Warren377202b2014-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 Schuchardtca230b02018-08-11 15:52:14 +0200135 * set to true. This should also be true for the dummy entry at the end
Stephen Warren377202b2014-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 Glass4101f682016-02-29 15:25:34 -0700140 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glass05289792020-05-10 11:39:57 -0600141 struct disk_partition *fs_partition);
Simon Glass436e2b72012-12-26 09:53:29 +0000142 int (*ls)(const char *dirname);
Stephen Warren61529162014-02-03 13:21:00 -0700143 int (*exists)(const char *filename);
Suriyan Ramasamid455d872014-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 Glass436e2b72012-12-26 09:53:29 +0000149 void (*close)(void);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100150 int (*uuid)(char *uuid_str);
Rob Clark4bbcc962017-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 Takahiroe2519da2018-09-11 15:59:13 +0900165 int (*unlink)(const char *filename);
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900166 int (*mkdir)(const char *dirname);
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100167 int (*ln)(const char *filename, const char *target);
Simon Glass436e2b72012-12-26 09:53:29 +0000168};
169
170static struct fstype_info fstypes[] = {
171#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +0000172 {
173 .fstype = FS_TYPE_FAT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100174 .name = "fat",
Stephen Warren377202b2014-02-03 13:21:01 -0700175 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000176 .probe = fat_set_blk_dev,
177 .close = fat_close,
Rob Clark89191d62017-09-09 13:15:58 -0400178 .ls = fs_ls_generic,
Stephen Warrenb7b5f312014-02-03 13:21:10 -0700179 .exists = fat_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600180 .size = fat_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000181 .read = fat_read_file,
Tien Fong Cheed8c3ea92019-01-23 14:20:04 +0800182#if CONFIG_IS_ENABLED(FAT_WRITE)
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800183 .write = file_fat_write,
AKASHI Takahirof8240ce2018-09-11 15:59:14 +0900184 .unlink = fat_unlink,
AKASHI Takahiro31a18d52018-09-11 15:59:10 +0900185 .mkdir = fat_mkdir,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800186#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700187 .write = fs_write_unsupported,
AKASHI Takahirof8240ce2018-09-11 15:59:14 +0900188 .unlink = fs_unlink_unsupported,
AKASHI Takahiro31a18d52018-09-11 15:59:10 +0900189 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800190#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100191 .uuid = fs_uuid_unsupported,
Rob Clark89191d62017-09-09 13:15:58 -0400192 .opendir = fat_opendir,
193 .readdir = fat_readdir,
194 .closedir = fat_closedir,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100195 .ln = fs_ln_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000196 },
Simon Glass436e2b72012-12-26 09:53:29 +0000197#endif
Tien Fong Cheecafc4292019-01-23 14:20:06 +0800198
199#if CONFIG_IS_ENABLED(FS_EXT4)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000200 {
201 .fstype = FS_TYPE_EXT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100202 .name = "ext4",
Stephen Warren377202b2014-02-03 13:21:01 -0700203 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000204 .probe = ext4fs_probe,
205 .close = ext4fs_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000206 .ls = ext4fs_ls,
Stephen Warren55af5c92014-02-03 13:21:09 -0700207 .exists = ext4fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600208 .size = ext4fs_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000209 .read = ext4_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800210#ifdef CONFIG_CMD_EXT4_WRITE
211 .write = ext4_write_file,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100212 .ln = ext4fs_create_link,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800213#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700214 .write = fs_write_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100215 .ln = fs_ln_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800216#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100217 .uuid = ext4fs_uuid,
Rob Clark4bbcc962017-09-09 13:15:55 -0400218 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900219 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900220 .mkdir = fs_mkdir_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000221 },
222#endif
Simon Glass92ccc962012-12-26 09:53:35 +0000223#ifdef CONFIG_SANDBOX
224 {
225 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100226 .name = "sandbox",
Stephen Warren377202b2014-02-03 13:21:01 -0700227 .null_dev_desc_ok = true,
Simon Glass92ccc962012-12-26 09:53:35 +0000228 .probe = sandbox_fs_set_blk_dev,
229 .close = sandbox_fs_close,
230 .ls = sandbox_fs_ls,
Stephen Warren0a30aa12014-02-03 13:21:07 -0700231 .exists = sandbox_fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600232 .size = sandbox_fs_size,
Simon Glass92ccc962012-12-26 09:53:35 +0000233 .read = fs_read_sandbox,
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000234 .write = fs_write_sandbox,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100235 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400236 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900237 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900238 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100239 .ln = fs_ln_unsupported,
Simon Glass92ccc962012-12-26 09:53:35 +0000240 },
241#endif
Hans de Goede251cee02015-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 Clark4bbcc962017-09-09 13:15:55 -0400255 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900256 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900257 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100258 .ln = fs_ln_unsupported,
Hans de Goede251cee02015-09-17 18:46:58 -0400259 },
260#endif
Marek BehĂșn0c936ee2017-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Ășn38fc6832017-10-06 16:56:07 +0200274 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900275 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900276 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100277 .ln = fs_ln_unsupported,
Marek BehĂșn0c936ee2017-09-03 17:00:29 +0200278 },
279#endif
Simon Glass436e2b72012-12-26 09:53:29 +0000280 {
281 .fstype = FS_TYPE_ANY,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100282 .name = "unsupported",
Stephen Warren377202b2014-02-03 13:21:01 -0700283 .null_dev_desc_ok = true,
Simon Glass436e2b72012-12-26 09:53:29 +0000284 .probe = fs_probe_unsupported,
285 .close = fs_close_unsupported,
286 .ls = fs_ls_unsupported,
Stephen Warren61529162014-02-03 13:21:00 -0700287 .exists = fs_exists_unsupported,
Stephen Warrencf659812014-06-11 12:47:26 -0600288 .size = fs_size_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000289 .read = fs_read_unsupported,
Simon Glassa8f6ab52013-04-20 08:42:50 +0000290 .write = fs_write_unsupported,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100291 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400292 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900293 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900294 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100295 .ln = fs_ln_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000296 },
297};
298
Simon Glassc6f548d2012-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 Kiernan0d488e82018-05-29 15:30:50 +0000313/**
AKASHI Takahirob7cd9562019-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 Kiernan0d488e82018-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 Warren045fa1e2012-10-22 06:43:51 +0000339int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
340{
Simon Glass436e2b72012-12-26 09:53:29 +0000341 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000342 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000343#ifdef CONFIG_NEEDS_MANUAL_RELOC
344 static int relocated;
345
346 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000347 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
348 i++, info++) {
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100349 info->name += gd->reloc_off;
Simon Glass436e2b72012-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 Glassa8f6ab52013-04-20 08:42:50 +0000354 info->write += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000355 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000356 relocated = 1;
357 }
358#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000359
Simon Glasse35929e2016-02-29 15:25:44 -0700360 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000361 &fs_partition, 1);
362 if (part < 0)
363 return -1;
364
Simon Glass436e2b72012-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 Warren045fa1e2012-10-22 06:43:51 +0000368 continue;
369
Stephen Warren377202b2014-02-03 13:21:01 -0700370 if (!fs_dev_desc && !info->null_dev_desc_ok)
371 continue;
372
Simon Glass2ded0d42012-12-26 09:53:31 +0000373 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass436e2b72012-12-26 09:53:29 +0000374 fs_type = info->fstype;
Rob Clark4bbcc962017-09-09 13:15:55 -0400375 fs_dev_part = part;
376 return 0;
377 }
378 }
379
380 return -1;
381}
382
383/* 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 Takahirob0c78d82018-10-17 16:32:02 +0900400 fs_dev_part = part;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000401 return 0;
402 }
403 }
404
Stephen Warren045fa1e2012-10-22 06:43:51 +0000405 return -1;
406}
407
AKASHI Takahiro64f49eb2019-10-07 14:59:35 +0900408void fs_close(void)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000409{
Simon Glassc6f548d2012-12-26 09:53:30 +0000410 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000411
Simon Glassc6f548d2012-12-26 09:53:30 +0000412 info->close();
Simon Glasse6d52412012-12-26 09:53:33 +0000413
Stephen Warren045fa1e2012-10-22 06:43:51 +0000414 fs_type = FS_TYPE_ANY;
415}
416
Christian Gmeiner59e890e2014-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 Warren045fa1e2012-10-22 06:43:51 +0000424int fs_ls(const char *dirname)
425{
426 int ret;
427
Simon Glassc6f548d2012-12-26 09:53:30 +0000428 struct fstype_info *info = fs_get_info(fs_type);
429
430 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000431
432 fs_close();
433
434 return ret;
435}
436
Stephen Warren61529162014-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 Ramasamid455d872014-11-17 14:39:38 -0800450int fs_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -0600451{
452 int ret;
453
454 struct fstype_info *info = fs_get_info(fs_type);
455
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800456 ret = info->size(filename, size);
Stephen Warrencf659812014-06-11 12:47:26 -0600457
458 fs_close();
459
460 return ret;
461}
462
Simon Goldschmidtaa3c6092019-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 Goldschmidt9cc23232019-01-26 22:13:04 +0100487 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
Simon Goldschmidtaa3c6092019-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 Warren045fa1e2012-10-22 06:43:51 +0000500{
Simon Glassc6f548d2012-12-26 09:53:30 +0000501 struct fstype_info *info = fs_get_info(fs_type);
Simon Glass117e0502012-12-26 09:53:32 +0000502 void *buf;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000503 int ret;
504
Simon Goldschmidtaa3c6092019-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 Glass117e0502012-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 Ramasamid455d872014-11-17 14:39:38 -0800518 ret = info->read(filename, buf, offset, len, actread);
Simon Glass117e0502012-12-26 09:53:32 +0000519 unmap_sysmem(buf);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000520
Simon Glassc6f548d2012-12-26 09:53:30 +0000521 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher7a3e70c2015-08-05 17:16:58 +0200522 if (ret == 0 && len && *actread != len)
Heinrich Schuchardta327bde2018-01-01 21:15:37 +0100523 debug("** %s shorter than offset + len **\n", filename);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000524 fs_close();
525
526 return ret;
527}
528
Simon Goldschmidtaa3c6092019-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 Ramasamid455d872014-11-17 14:39:38 -0800535int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
536 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000537{
538 struct fstype_info *info = fs_get_info(fs_type);
539 void *buf;
540 int ret;
541
Simon Glassa8f6ab52013-04-20 08:42:50 +0000542 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800543 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000544 unmap_sysmem(buf);
545
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800546 if (ret < 0 && len != *actwrite) {
Simon Glassa8f6ab52013-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 Clark4bbcc962017-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 Takahiroe2519da2018-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 Takahiroe2519da2018-09-11 15:59:13 +0900615 fs_close();
616
617 return ret;
618}
Rob Clark4bbcc962017-09-09 13:15:55 -0400619
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900620int 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 Takahiroe7074cf2018-09-11 15:59:08 +0900628 fs_close();
629
630 return ret;
631}
632
Jean-Jacques Hiblotaaa12152019-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 Glass09140112020-05-10 11:40:03 -0600649int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
650 int fstype)
Stephen Warrencf659812014-06-11 12:47:26 -0600651{
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800652 loff_t size;
Stephen Warrencf659812014-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 Ramasamid455d872014-11-17 14:39:38 -0800660 if (fs_size(argv[3], &size) < 0)
Stephen Warrencf659812014-06-11 12:47:26 -0600661 return CMD_RET_FAILURE;
662
Simon Glass018f5302017-08-03 12:22:10 -0600663 env_set_hex("filesize", size);
Stephen Warrencf659812014-06-11 12:47:26 -0600664
665 return 0;
666}
667
Simon Glass09140112020-05-10 11:40:03 -0600668int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
669 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000670{
671 unsigned long addr;
672 const char *addr_str;
673 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800674 loff_t bytes;
675 loff_t pos;
676 loff_t len_read;
677 int ret;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100678 unsigned long time;
Pavel Machek949bbd72014-07-09 22:42:57 +0200679 char *ep;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000680
Stephen Warrene9b0f992012-10-30 12:04:17 +0000681 if (argc < 2)
682 return CMD_RET_USAGE;
683 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000684 return CMD_RET_USAGE;
685
Stephen Warrene9b0f992012-10-30 12:04:17 +0000686 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000687 return 1;
688
689 if (argc >= 4) {
Pavel Machek949bbd72014-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 Warren045fa1e2012-10-22 06:43:51 +0000693 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600694 addr_str = env_get("loadaddr");
Stephen Warren045fa1e2012-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 Glass00caae62017-08-03 12:22:12 -0600703 filename = env_get("bootfile");
Stephen Warren045fa1e2012-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 Denkb770e882013-10-05 21:07:25 +0200710 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000711 else
712 bytes = 0;
713 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200714 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000715 else
716 pos = 0;
717
Mian Yousaf Kaukabee88eac2019-06-18 15:03:44 +0200718#ifdef CONFIG_CMD_BOOTEFI
719 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
720 (argc > 4) ? argv[4] : "");
721#endif
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100722 time = get_timer(0);
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100723 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100724 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800725 if (ret < 0)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000726 return 1;
727
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800728 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100729 if (time > 0) {
730 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500731 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100732 puts(")");
733 }
734 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000735
Simon Glass018f5302017-08-03 12:22:10 -0600736 env_set_hex("fileaddr", addr);
737 env_set_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000738
739 return 0;
740}
741
Simon Glass09140112020-05-10 11:40:03 -0600742int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
743 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000744{
745 if (argc < 2)
746 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000747 if (argc > 4)
748 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000749
750 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
751 return 1;
752
Stephen Warrene9b0f992012-10-30 12:04:17 +0000753 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000754 return 1;
755
756 return 0;
757}
Simon Glassa8f6ab52013-04-20 08:42:50 +0000758
Stephen Warren61529162014-02-03 13:21:00 -0700759int file_exists(const char *dev_type, const char *dev_part, const char *file,
760 int fstype)
761{
762 if (fs_set_blk_dev(dev_type, dev_part, fstype))
763 return 0;
764
765 return fs_exists(file);
766}
767
Simon Glass09140112020-05-10 11:40:03 -0600768int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
769 int fstype)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000770{
771 unsigned long addr;
772 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800773 loff_t bytes;
774 loff_t pos;
775 loff_t len;
776 int ret;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000777 unsigned long time;
778
779 if (argc < 6 || argc > 7)
780 return CMD_RET_USAGE;
781
782 if (fs_set_blk_dev(argv[1], argv[2], fstype))
783 return 1;
784
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800785 addr = simple_strtoul(argv[3], NULL, 16);
786 filename = argv[4];
Wolfgang Denkb770e882013-10-05 21:07:25 +0200787 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000788 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200789 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000790 else
791 pos = 0;
792
793 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800794 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000795 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800796 if (ret < 0)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000797 return 1;
798
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800799 printf("%llu bytes written in %lu ms", len, time);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000800 if (time > 0) {
801 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500802 print_size(div_u64(len, time) * 1000, "/s");
Simon Glassa8f6ab52013-04-20 08:42:50 +0000803 puts(")");
804 }
805 puts("\n");
806
807 return 0;
808}
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100809
Simon Glass09140112020-05-10 11:40:03 -0600810int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
811 int fstype)
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100812{
813 int ret;
814 char uuid[37];
815 memset(uuid, 0, sizeof(uuid));
816
817 if (argc < 3 || argc > 4)
818 return CMD_RET_USAGE;
819
820 if (fs_set_blk_dev(argv[1], argv[2], fstype))
821 return 1;
822
823 ret = fs_uuid(uuid);
824 if (ret)
825 return CMD_RET_FAILURE;
826
827 if (argc == 4)
Simon Glass382bee52017-08-03 12:22:09 -0600828 env_set(argv[3], uuid);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100829 else
830 printf("%s\n", uuid);
831
832 return CMD_RET_SUCCESS;
833}
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100834
Simon Glass09140112020-05-10 11:40:03 -0600835int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100836{
837 struct fstype_info *info;
838
839 if (argc < 3 || argc > 4)
840 return CMD_RET_USAGE;
841
842 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
843 return 1;
844
845 info = fs_get_info(fs_type);
846
847 if (argc == 4)
Simon Glass382bee52017-08-03 12:22:09 -0600848 env_set(argv[3], info->name);
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100849 else
850 printf("%s\n", info->name);
851
Marek Vasute531c672019-02-06 13:19:29 +0100852 fs_close();
853
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100854 return CMD_RET_SUCCESS;
855}
856
Simon Glass09140112020-05-10 11:40:03 -0600857int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900858 int fstype)
859{
860 if (argc != 4)
861 return CMD_RET_USAGE;
862
863 if (fs_set_blk_dev(argv[1], argv[2], fstype))
864 return 1;
865
866 if (fs_unlink(argv[3]))
867 return 1;
868
869 return 0;
870}
871
Simon Glass09140112020-05-10 11:40:03 -0600872int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900873 int fstype)
874{
875 int ret;
876
877 if (argc != 4)
878 return CMD_RET_USAGE;
879
880 if (fs_set_blk_dev(argv[1], argv[2], fstype))
881 return 1;
882
883 ret = fs_mkdir(argv[3]);
884 if (ret) {
885 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
886 return 1;
887 }
888
889 return 0;
890}
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100891
Simon Glass09140112020-05-10 11:40:03 -0600892int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100893 int fstype)
894{
895 if (argc != 5)
896 return CMD_RET_USAGE;
897
898 if (fs_set_blk_dev(argv[1], argv[2], fstype))
899 return 1;
900
901 if (fs_ln(argv[3], argv[4]))
902 return 1;
903
904 return 0;
905}