blob: 3481229aa623bf80e0d26caf9cfc20a9d179ae0e [file] [log] [blame]
Stephen Warren045fa1e2012-10-22 06:43:51 +00001/*
2 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
3 *
Tom Rini5b8031c2016-01-14 22:05:13 -05004 * SPDX-License-Identifier: GPL-2.0
Stephen Warren045fa1e2012-10-22 06:43:51 +00005 */
6
7#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>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050010#include <mapmem.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000011#include <part.h>
12#include <ext4fs.h>
13#include <fat.h>
14#include <fs.h>
Simon Glass92ccc962012-12-26 09:53:35 +000015#include <sandboxfs.h>
Hans de Goede251cee02015-09-17 18:46:58 -040016#include <ubifs_uboot.h>
Simon Glass117e0502012-12-26 09:53:32 +000017#include <asm/io.h>
Tom Rini9e374e72014-11-24 11:50:46 -050018#include <div64.h>
19#include <linux/math64.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000020
Stephen Warrena1b231c2012-10-30 07:50:47 +000021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass4101f682016-02-29 15:25:34 -070023static struct blk_desc *fs_dev_desc;
Rob Clark4bbcc962017-09-09 13:15:55 -040024static int fs_dev_part;
Stephen Warren045fa1e2012-10-22 06:43:51 +000025static disk_partition_t fs_partition;
26static int fs_type = FS_TYPE_ANY;
27
Simon Glass4101f682016-02-29 15:25:34 -070028static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glass2ded0d42012-12-26 09:53:31 +000029 disk_partition_t *fs_partition)
Simon Glass436e2b72012-12-26 09:53:29 +000030{
31 printf("** Unrecognized filesystem type **\n");
32 return -1;
33}
34
Stephen Warren045fa1e2012-10-22 06:43:51 +000035static inline int fs_ls_unsupported(const char *dirname)
36{
Stephen Warren045fa1e2012-10-22 06:43:51 +000037 return -1;
38}
39
Rob Clark89191d62017-09-09 13:15:58 -040040/* generic implementation of ls in terms of opendir/readdir/closedir */
41__maybe_unused
42static int fs_ls_generic(const char *dirname)
43{
44 struct fs_dir_stream *dirs;
45 struct fs_dirent *dent;
46 int nfiles = 0, ndirs = 0;
47
48 dirs = fs_opendir(dirname);
49 if (!dirs)
50 return -errno;
51
52 while ((dent = fs_readdir(dirs))) {
53 if (dent->type == FS_DT_DIR) {
54 printf(" %s/\n", dent->name);
55 ndirs++;
56 } else {
57 printf(" %8lld %s\n", dent->size, dent->name);
58 nfiles++;
59 }
60 }
61
62 fs_closedir(dirs);
63
64 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
65
66 return 0;
67}
68
Stephen Warren61529162014-02-03 13:21:00 -070069static inline int fs_exists_unsupported(const char *filename)
70{
71 return 0;
72}
73
Suriyan Ramasamid455d872014-11-17 14:39:38 -080074static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -060075{
76 return -1;
77}
78
Simon Glass117e0502012-12-26 09:53:32 +000079static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080080 loff_t offset, loff_t len,
81 loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +000082{
Stephen Warren045fa1e2012-10-22 06:43:51 +000083 return -1;
84}
85
Simon Glassa8f6ab52013-04-20 08:42:50 +000086static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080087 loff_t offset, loff_t len,
88 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +000089{
90 return -1;
91}
92
Simon Glass436e2b72012-12-26 09:53:29 +000093static inline void fs_close_unsupported(void)
94{
95}
96
Christian Gmeiner59e890e2014-11-12 14:35:04 +010097static inline int fs_uuid_unsupported(char *uuid_str)
98{
99 return -1;
100}
101
Rob Clark4bbcc962017-09-09 13:15:55 -0400102static inline int fs_opendir_unsupported(const char *filename,
103 struct fs_dir_stream **dirs)
104{
105 return -EACCES;
106}
107
Simon Glass436e2b72012-12-26 09:53:29 +0000108struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +0000109 int fstype;
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100110 char *name;
Stephen Warren377202b2014-02-03 13:21:01 -0700111 /*
112 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
113 * should be false in most cases. For "virtual" filesystems which
114 * aren't based on a U-Boot block device (e.g. sandbox), this can be
115 * set to true. This should also be true for the dumm entry at the end
116 * of fstypes[], since that is essentially a "virtual" (non-existent)
117 * filesystem.
118 */
119 bool null_dev_desc_ok;
Simon Glass4101f682016-02-29 15:25:34 -0700120 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glass2ded0d42012-12-26 09:53:31 +0000121 disk_partition_t *fs_partition);
Simon Glass436e2b72012-12-26 09:53:29 +0000122 int (*ls)(const char *dirname);
Stephen Warren61529162014-02-03 13:21:00 -0700123 int (*exists)(const char *filename);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800124 int (*size)(const char *filename, loff_t *size);
125 int (*read)(const char *filename, void *buf, loff_t offset,
126 loff_t len, loff_t *actread);
127 int (*write)(const char *filename, void *buf, loff_t offset,
128 loff_t len, loff_t *actwrite);
Simon Glass436e2b72012-12-26 09:53:29 +0000129 void (*close)(void);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100130 int (*uuid)(char *uuid_str);
Rob Clark4bbcc962017-09-09 13:15:55 -0400131 /*
132 * Open a directory stream. On success return 0 and directory
133 * stream pointer via 'dirsp'. On error, return -errno. See
134 * fs_opendir().
135 */
136 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
137 /*
138 * Read next entry from directory stream. On success return 0
139 * and directory entry pointer via 'dentp'. On error return
140 * -errno. See fs_readdir().
141 */
142 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
143 /* see fs_closedir() */
144 void (*closedir)(struct fs_dir_stream *dirs);
Simon Glass436e2b72012-12-26 09:53:29 +0000145};
146
147static struct fstype_info fstypes[] = {
148#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +0000149 {
150 .fstype = FS_TYPE_FAT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100151 .name = "fat",
Stephen Warren377202b2014-02-03 13:21:01 -0700152 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000153 .probe = fat_set_blk_dev,
154 .close = fat_close,
Rob Clark89191d62017-09-09 13:15:58 -0400155 .ls = fs_ls_generic,
Stephen Warrenb7b5f312014-02-03 13:21:10 -0700156 .exists = fat_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600157 .size = fat_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000158 .read = fat_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800159#ifdef CONFIG_FAT_WRITE
160 .write = file_fat_write,
161#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700162 .write = fs_write_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800163#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100164 .uuid = fs_uuid_unsupported,
Rob Clark89191d62017-09-09 13:15:58 -0400165 .opendir = fat_opendir,
166 .readdir = fat_readdir,
167 .closedir = fat_closedir,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000168 },
Simon Glass436e2b72012-12-26 09:53:29 +0000169#endif
170#ifdef CONFIG_FS_EXT4
Stephen Warren045fa1e2012-10-22 06:43:51 +0000171 {
172 .fstype = FS_TYPE_EXT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100173 .name = "ext4",
Stephen Warren377202b2014-02-03 13:21:01 -0700174 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000175 .probe = ext4fs_probe,
176 .close = ext4fs_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000177 .ls = ext4fs_ls,
Stephen Warren55af5c92014-02-03 13:21:09 -0700178 .exists = ext4fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600179 .size = ext4fs_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000180 .read = ext4_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800181#ifdef CONFIG_CMD_EXT4_WRITE
182 .write = ext4_write_file,
183#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700184 .write = fs_write_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800185#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100186 .uuid = ext4fs_uuid,
Rob Clark4bbcc962017-09-09 13:15:55 -0400187 .opendir = fs_opendir_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000188 },
189#endif
Simon Glass92ccc962012-12-26 09:53:35 +0000190#ifdef CONFIG_SANDBOX
191 {
192 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100193 .name = "sandbox",
Stephen Warren377202b2014-02-03 13:21:01 -0700194 .null_dev_desc_ok = true,
Simon Glass92ccc962012-12-26 09:53:35 +0000195 .probe = sandbox_fs_set_blk_dev,
196 .close = sandbox_fs_close,
197 .ls = sandbox_fs_ls,
Stephen Warren0a30aa12014-02-03 13:21:07 -0700198 .exists = sandbox_fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600199 .size = sandbox_fs_size,
Simon Glass92ccc962012-12-26 09:53:35 +0000200 .read = fs_read_sandbox,
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000201 .write = fs_write_sandbox,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100202 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400203 .opendir = fs_opendir_unsupported,
Simon Glass92ccc962012-12-26 09:53:35 +0000204 },
205#endif
Hans de Goede251cee02015-09-17 18:46:58 -0400206#ifdef CONFIG_CMD_UBIFS
207 {
208 .fstype = FS_TYPE_UBIFS,
209 .name = "ubifs",
210 .null_dev_desc_ok = true,
211 .probe = ubifs_set_blk_dev,
212 .close = ubifs_close,
213 .ls = ubifs_ls,
214 .exists = ubifs_exists,
215 .size = ubifs_size,
216 .read = ubifs_read,
217 .write = fs_write_unsupported,
218 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400219 .opendir = fs_opendir_unsupported,
Hans de Goede251cee02015-09-17 18:46:58 -0400220 },
221#endif
Simon Glass436e2b72012-12-26 09:53:29 +0000222 {
223 .fstype = FS_TYPE_ANY,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100224 .name = "unsupported",
Stephen Warren377202b2014-02-03 13:21:01 -0700225 .null_dev_desc_ok = true,
Simon Glass436e2b72012-12-26 09:53:29 +0000226 .probe = fs_probe_unsupported,
227 .close = fs_close_unsupported,
228 .ls = fs_ls_unsupported,
Stephen Warren61529162014-02-03 13:21:00 -0700229 .exists = fs_exists_unsupported,
Stephen Warrencf659812014-06-11 12:47:26 -0600230 .size = fs_size_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000231 .read = fs_read_unsupported,
Simon Glassa8f6ab52013-04-20 08:42:50 +0000232 .write = fs_write_unsupported,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100233 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400234 .opendir = fs_opendir_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000235 },
236};
237
Simon Glassc6f548d2012-12-26 09:53:30 +0000238static struct fstype_info *fs_get_info(int fstype)
239{
240 struct fstype_info *info;
241 int i;
242
243 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
244 if (fstype == info->fstype)
245 return info;
246 }
247
248 /* Return the 'unsupported' sentinel */
249 return info;
250}
251
Stephen Warren045fa1e2012-10-22 06:43:51 +0000252int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
253{
Simon Glass436e2b72012-12-26 09:53:29 +0000254 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000255 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000256#ifdef CONFIG_NEEDS_MANUAL_RELOC
257 static int relocated;
258
259 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000260 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
261 i++, info++) {
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100262 info->name += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000263 info->probe += gd->reloc_off;
264 info->close += gd->reloc_off;
265 info->ls += gd->reloc_off;
266 info->read += gd->reloc_off;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000267 info->write += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000268 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000269 relocated = 1;
270 }
271#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000272
Simon Glasse35929e2016-02-29 15:25:44 -0700273 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000274 &fs_partition, 1);
275 if (part < 0)
276 return -1;
277
Simon Glass436e2b72012-12-26 09:53:29 +0000278 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
279 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
280 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000281 continue;
282
Stephen Warren377202b2014-02-03 13:21:01 -0700283 if (!fs_dev_desc && !info->null_dev_desc_ok)
284 continue;
285
Simon Glass2ded0d42012-12-26 09:53:31 +0000286 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass436e2b72012-12-26 09:53:29 +0000287 fs_type = info->fstype;
Rob Clark4bbcc962017-09-09 13:15:55 -0400288 fs_dev_part = part;
289 return 0;
290 }
291 }
292
293 return -1;
294}
295
296/* set current blk device w/ blk_desc + partition # */
297int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
298{
299 struct fstype_info *info;
300 int ret, i;
301
302 if (part >= 1)
303 ret = part_get_info(desc, part, &fs_partition);
304 else
305 ret = part_get_info_whole_disk(desc, &fs_partition);
306 if (ret)
307 return ret;
308 fs_dev_desc = desc;
309
310 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
311 if (!info->probe(fs_dev_desc, &fs_partition)) {
312 fs_type = info->fstype;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000313 return 0;
314 }
315 }
316
Stephen Warren045fa1e2012-10-22 06:43:51 +0000317 return -1;
318}
319
320static void fs_close(void)
321{
Simon Glassc6f548d2012-12-26 09:53:30 +0000322 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000323
Simon Glassc6f548d2012-12-26 09:53:30 +0000324 info->close();
Simon Glasse6d52412012-12-26 09:53:33 +0000325
Stephen Warren045fa1e2012-10-22 06:43:51 +0000326 fs_type = FS_TYPE_ANY;
327}
328
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100329int fs_uuid(char *uuid_str)
330{
331 struct fstype_info *info = fs_get_info(fs_type);
332
333 return info->uuid(uuid_str);
334}
335
Stephen Warren045fa1e2012-10-22 06:43:51 +0000336int fs_ls(const char *dirname)
337{
338 int ret;
339
Simon Glassc6f548d2012-12-26 09:53:30 +0000340 struct fstype_info *info = fs_get_info(fs_type);
341
342 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000343
Simon Glasse6d52412012-12-26 09:53:33 +0000344 fs_type = FS_TYPE_ANY;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000345 fs_close();
346
347 return ret;
348}
349
Stephen Warren61529162014-02-03 13:21:00 -0700350int fs_exists(const char *filename)
351{
352 int ret;
353
354 struct fstype_info *info = fs_get_info(fs_type);
355
356 ret = info->exists(filename);
357
358 fs_close();
359
360 return ret;
361}
362
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800363int fs_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -0600364{
365 int ret;
366
367 struct fstype_info *info = fs_get_info(fs_type);
368
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800369 ret = info->size(filename, size);
Stephen Warrencf659812014-06-11 12:47:26 -0600370
371 fs_close();
372
373 return ret;
374}
375
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800376int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
377 loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000378{
Simon Glassc6f548d2012-12-26 09:53:30 +0000379 struct fstype_info *info = fs_get_info(fs_type);
Simon Glass117e0502012-12-26 09:53:32 +0000380 void *buf;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000381 int ret;
382
Simon Glass117e0502012-12-26 09:53:32 +0000383 /*
384 * We don't actually know how many bytes are being read, since len==0
385 * means read the whole file.
386 */
387 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800388 ret = info->read(filename, buf, offset, len, actread);
Simon Glass117e0502012-12-26 09:53:32 +0000389 unmap_sysmem(buf);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000390
Simon Glassc6f548d2012-12-26 09:53:30 +0000391 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher7a3e70c2015-08-05 17:16:58 +0200392 if (ret == 0 && len && *actread != len)
393 printf("** %s shorter than offset + len **\n", filename);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000394 fs_close();
395
396 return ret;
397}
398
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800399int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
400 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000401{
402 struct fstype_info *info = fs_get_info(fs_type);
403 void *buf;
404 int ret;
405
Simon Glassa8f6ab52013-04-20 08:42:50 +0000406 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800407 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000408 unmap_sysmem(buf);
409
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800410 if (ret < 0 && len != *actwrite) {
Simon Glassa8f6ab52013-04-20 08:42:50 +0000411 printf("** Unable to write file %s **\n", filename);
412 ret = -1;
413 }
414 fs_close();
415
416 return ret;
417}
418
Rob Clark4bbcc962017-09-09 13:15:55 -0400419struct fs_dir_stream *fs_opendir(const char *filename)
420{
421 struct fstype_info *info = fs_get_info(fs_type);
422 struct fs_dir_stream *dirs = NULL;
423 int ret;
424
425 ret = info->opendir(filename, &dirs);
426 fs_close();
427 if (ret) {
428 errno = -ret;
429 return NULL;
430 }
431
432 dirs->desc = fs_dev_desc;
433 dirs->part = fs_dev_part;
434
435 return dirs;
436}
437
438struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
439{
440 struct fstype_info *info;
441 struct fs_dirent *dirent;
442 int ret;
443
444 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
445 info = fs_get_info(fs_type);
446
447 ret = info->readdir(dirs, &dirent);
448 fs_close();
449 if (ret) {
450 errno = -ret;
451 return NULL;
452 }
453
454 return dirent;
455}
456
457void fs_closedir(struct fs_dir_stream *dirs)
458{
459 struct fstype_info *info;
460
461 if (!dirs)
462 return;
463
464 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
465 info = fs_get_info(fs_type);
466
467 info->closedir(dirs);
468 fs_close();
469}
470
471
Stephen Warrencf659812014-06-11 12:47:26 -0600472int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
473 int fstype)
474{
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800475 loff_t size;
Stephen Warrencf659812014-06-11 12:47:26 -0600476
477 if (argc != 4)
478 return CMD_RET_USAGE;
479
480 if (fs_set_blk_dev(argv[1], argv[2], fstype))
481 return 1;
482
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800483 if (fs_size(argv[3], &size) < 0)
Stephen Warrencf659812014-06-11 12:47:26 -0600484 return CMD_RET_FAILURE;
485
Simon Glass018f5302017-08-03 12:22:10 -0600486 env_set_hex("filesize", size);
Stephen Warrencf659812014-06-11 12:47:26 -0600487
488 return 0;
489}
490
Stephen Warrenf9b55e22012-10-31 11:05:07 +0000491int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200492 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000493{
494 unsigned long addr;
495 const char *addr_str;
496 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800497 loff_t bytes;
498 loff_t pos;
499 loff_t len_read;
500 int ret;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100501 unsigned long time;
Pavel Machek949bbd72014-07-09 22:42:57 +0200502 char *ep;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000503
Stephen Warrene9b0f992012-10-30 12:04:17 +0000504 if (argc < 2)
505 return CMD_RET_USAGE;
506 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000507 return CMD_RET_USAGE;
508
Stephen Warrene9b0f992012-10-30 12:04:17 +0000509 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000510 return 1;
511
512 if (argc >= 4) {
Pavel Machek949bbd72014-07-09 22:42:57 +0200513 addr = simple_strtoul(argv[3], &ep, 16);
514 if (ep == argv[3] || *ep != '\0')
515 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000516 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600517 addr_str = env_get("loadaddr");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000518 if (addr_str != NULL)
519 addr = simple_strtoul(addr_str, NULL, 16);
520 else
521 addr = CONFIG_SYS_LOAD_ADDR;
522 }
523 if (argc >= 5) {
524 filename = argv[4];
525 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600526 filename = env_get("bootfile");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000527 if (!filename) {
528 puts("** No boot file defined **\n");
529 return 1;
530 }
531 }
532 if (argc >= 6)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200533 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000534 else
535 bytes = 0;
536 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200537 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000538 else
539 pos = 0;
540
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100541 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800542 ret = fs_read(filename, addr, pos, bytes, &len_read);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100543 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800544 if (ret < 0)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000545 return 1;
546
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800547 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100548 if (time > 0) {
549 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500550 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100551 puts(")");
552 }
553 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000554
Simon Glass018f5302017-08-03 12:22:10 -0600555 env_set_hex("fileaddr", addr);
556 env_set_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000557
558 return 0;
559}
560
561int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
562 int fstype)
563{
564 if (argc < 2)
565 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000566 if (argc > 4)
567 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000568
569 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
570 return 1;
571
Stephen Warrene9b0f992012-10-30 12:04:17 +0000572 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000573 return 1;
574
575 return 0;
576}
Simon Glassa8f6ab52013-04-20 08:42:50 +0000577
Stephen Warren61529162014-02-03 13:21:00 -0700578int file_exists(const char *dev_type, const char *dev_part, const char *file,
579 int fstype)
580{
581 if (fs_set_blk_dev(dev_type, dev_part, fstype))
582 return 0;
583
584 return fs_exists(file);
585}
586
Simon Glassa8f6ab52013-04-20 08:42:50 +0000587int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200588 int fstype)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000589{
590 unsigned long addr;
591 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800592 loff_t bytes;
593 loff_t pos;
594 loff_t len;
595 int ret;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000596 unsigned long time;
597
598 if (argc < 6 || argc > 7)
599 return CMD_RET_USAGE;
600
601 if (fs_set_blk_dev(argv[1], argv[2], fstype))
602 return 1;
603
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800604 addr = simple_strtoul(argv[3], NULL, 16);
605 filename = argv[4];
Wolfgang Denkb770e882013-10-05 21:07:25 +0200606 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000607 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200608 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000609 else
610 pos = 0;
611
612 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800613 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000614 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800615 if (ret < 0)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000616 return 1;
617
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800618 printf("%llu bytes written in %lu ms", len, time);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000619 if (time > 0) {
620 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500621 print_size(div_u64(len, time) * 1000, "/s");
Simon Glassa8f6ab52013-04-20 08:42:50 +0000622 puts(")");
623 }
624 puts("\n");
625
626 return 0;
627}
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100628
629int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
630 int fstype)
631{
632 int ret;
633 char uuid[37];
634 memset(uuid, 0, sizeof(uuid));
635
636 if (argc < 3 || argc > 4)
637 return CMD_RET_USAGE;
638
639 if (fs_set_blk_dev(argv[1], argv[2], fstype))
640 return 1;
641
642 ret = fs_uuid(uuid);
643 if (ret)
644 return CMD_RET_FAILURE;
645
646 if (argc == 4)
Simon Glass382bee52017-08-03 12:22:09 -0600647 env_set(argv[3], uuid);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100648 else
649 printf("%s\n", uuid);
650
651 return CMD_RET_SUCCESS;
652}
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100653
654int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
655{
656 struct fstype_info *info;
657
658 if (argc < 3 || argc > 4)
659 return CMD_RET_USAGE;
660
661 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
662 return 1;
663
664 info = fs_get_info(fs_type);
665
666 if (argc == 4)
Simon Glass382bee52017-08-03 12:22:09 -0600667 env_set(argv[3], info->name);
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100668 else
669 printf("%s\n", info->name);
670
671 return CMD_RET_SUCCESS;
672}
673