blob: 827b143e85e5538f9a250565ce2a54b0455fdab8 [file] [log] [blame]
Stephen Warren045fa1e2012-10-22 06:43:51 +00001/*
2 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <config.h>
Christian Gmeiner59e890e2014-11-12 14:35:04 +010018#include <errno.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000019#include <common.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050020#include <mapmem.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000021#include <part.h>
22#include <ext4fs.h>
23#include <fat.h>
24#include <fs.h>
Simon Glass92ccc962012-12-26 09:53:35 +000025#include <sandboxfs.h>
Simon Glass117e0502012-12-26 09:53:32 +000026#include <asm/io.h>
Tom Rini9e374e72014-11-24 11:50:46 -050027#include <div64.h>
28#include <linux/math64.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000029
Stephen Warrena1b231c2012-10-30 07:50:47 +000030DECLARE_GLOBAL_DATA_PTR;
31
Stephen Warren045fa1e2012-10-22 06:43:51 +000032static block_dev_desc_t *fs_dev_desc;
33static disk_partition_t fs_partition;
34static int fs_type = FS_TYPE_ANY;
35
Simon Glass2ded0d42012-12-26 09:53:31 +000036static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
37 disk_partition_t *fs_partition)
Simon Glass436e2b72012-12-26 09:53:29 +000038{
39 printf("** Unrecognized filesystem type **\n");
40 return -1;
41}
42
Stephen Warren045fa1e2012-10-22 06:43:51 +000043static inline int fs_ls_unsupported(const char *dirname)
44{
Stephen Warren045fa1e2012-10-22 06:43:51 +000045 return -1;
46}
47
Stephen Warren61529162014-02-03 13:21:00 -070048static inline int fs_exists_unsupported(const char *filename)
49{
50 return 0;
51}
52
Suriyan Ramasamid455d872014-11-17 14:39:38 -080053static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -060054{
55 return -1;
56}
57
Simon Glass117e0502012-12-26 09:53:32 +000058static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080059 loff_t offset, loff_t len,
60 loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +000061{
Stephen Warren045fa1e2012-10-22 06:43:51 +000062 return -1;
63}
64
Simon Glassa8f6ab52013-04-20 08:42:50 +000065static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080066 loff_t offset, loff_t len,
67 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +000068{
69 return -1;
70}
71
Simon Glass436e2b72012-12-26 09:53:29 +000072static inline void fs_close_unsupported(void)
73{
74}
75
Christian Gmeiner59e890e2014-11-12 14:35:04 +010076static inline int fs_uuid_unsupported(char *uuid_str)
77{
78 return -1;
79}
80
Simon Glass436e2b72012-12-26 09:53:29 +000081struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +000082 int fstype;
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +010083 char *name;
Stephen Warren377202b2014-02-03 13:21:01 -070084 /*
85 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
86 * should be false in most cases. For "virtual" filesystems which
87 * aren't based on a U-Boot block device (e.g. sandbox), this can be
88 * set to true. This should also be true for the dumm entry at the end
89 * of fstypes[], since that is essentially a "virtual" (non-existent)
90 * filesystem.
91 */
92 bool null_dev_desc_ok;
Simon Glass2ded0d42012-12-26 09:53:31 +000093 int (*probe)(block_dev_desc_t *fs_dev_desc,
94 disk_partition_t *fs_partition);
Simon Glass436e2b72012-12-26 09:53:29 +000095 int (*ls)(const char *dirname);
Stephen Warren61529162014-02-03 13:21:00 -070096 int (*exists)(const char *filename);
Suriyan Ramasamid455d872014-11-17 14:39:38 -080097 int (*size)(const char *filename, loff_t *size);
98 int (*read)(const char *filename, void *buf, loff_t offset,
99 loff_t len, loff_t *actread);
100 int (*write)(const char *filename, void *buf, loff_t offset,
101 loff_t len, loff_t *actwrite);
Simon Glass436e2b72012-12-26 09:53:29 +0000102 void (*close)(void);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100103 int (*uuid)(char *uuid_str);
Simon Glass436e2b72012-12-26 09:53:29 +0000104};
105
106static struct fstype_info fstypes[] = {
107#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +0000108 {
109 .fstype = FS_TYPE_FAT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100110 .name = "fat",
Stephen Warren377202b2014-02-03 13:21:01 -0700111 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000112 .probe = fat_set_blk_dev,
113 .close = fat_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000114 .ls = file_fat_ls,
Stephen Warrenb7b5f312014-02-03 13:21:10 -0700115 .exists = fat_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600116 .size = fat_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000117 .read = fat_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800118#ifdef CONFIG_FAT_WRITE
119 .write = file_fat_write,
120#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700121 .write = fs_write_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800122#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100123 .uuid = fs_uuid_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000124 },
Simon Glass436e2b72012-12-26 09:53:29 +0000125#endif
126#ifdef CONFIG_FS_EXT4
Stephen Warren045fa1e2012-10-22 06:43:51 +0000127 {
128 .fstype = FS_TYPE_EXT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100129 .name = "ext4",
Stephen Warren377202b2014-02-03 13:21:01 -0700130 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000131 .probe = ext4fs_probe,
132 .close = ext4fs_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000133 .ls = ext4fs_ls,
Stephen Warren55af5c92014-02-03 13:21:09 -0700134 .exists = ext4fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600135 .size = ext4fs_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000136 .read = ext4_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800137#ifdef CONFIG_CMD_EXT4_WRITE
138 .write = ext4_write_file,
139#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700140 .write = fs_write_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800141#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100142 .uuid = ext4fs_uuid,
Simon Glass436e2b72012-12-26 09:53:29 +0000143 },
144#endif
Simon Glass92ccc962012-12-26 09:53:35 +0000145#ifdef CONFIG_SANDBOX
146 {
147 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100148 .name = "sandbox",
Stephen Warren377202b2014-02-03 13:21:01 -0700149 .null_dev_desc_ok = true,
Simon Glass92ccc962012-12-26 09:53:35 +0000150 .probe = sandbox_fs_set_blk_dev,
151 .close = sandbox_fs_close,
152 .ls = sandbox_fs_ls,
Stephen Warren0a30aa12014-02-03 13:21:07 -0700153 .exists = sandbox_fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600154 .size = sandbox_fs_size,
Simon Glass92ccc962012-12-26 09:53:35 +0000155 .read = fs_read_sandbox,
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000156 .write = fs_write_sandbox,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100157 .uuid = fs_uuid_unsupported,
Simon Glass92ccc962012-12-26 09:53:35 +0000158 },
159#endif
Simon Glass436e2b72012-12-26 09:53:29 +0000160 {
161 .fstype = FS_TYPE_ANY,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100162 .name = "unsupported",
Stephen Warren377202b2014-02-03 13:21:01 -0700163 .null_dev_desc_ok = true,
Simon Glass436e2b72012-12-26 09:53:29 +0000164 .probe = fs_probe_unsupported,
165 .close = fs_close_unsupported,
166 .ls = fs_ls_unsupported,
Stephen Warren61529162014-02-03 13:21:00 -0700167 .exists = fs_exists_unsupported,
Stephen Warrencf659812014-06-11 12:47:26 -0600168 .size = fs_size_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000169 .read = fs_read_unsupported,
Simon Glassa8f6ab52013-04-20 08:42:50 +0000170 .write = fs_write_unsupported,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100171 .uuid = fs_uuid_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000172 },
173};
174
Simon Glassc6f548d2012-12-26 09:53:30 +0000175static struct fstype_info *fs_get_info(int fstype)
176{
177 struct fstype_info *info;
178 int i;
179
180 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
181 if (fstype == info->fstype)
182 return info;
183 }
184
185 /* Return the 'unsupported' sentinel */
186 return info;
187}
188
Stephen Warren045fa1e2012-10-22 06:43:51 +0000189int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
190{
Simon Glass436e2b72012-12-26 09:53:29 +0000191 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000192 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000193#ifdef CONFIG_NEEDS_MANUAL_RELOC
194 static int relocated;
195
196 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000197 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
198 i++, info++) {
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100199 info->name += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000200 info->probe += gd->reloc_off;
201 info->close += gd->reloc_off;
202 info->ls += gd->reloc_off;
203 info->read += gd->reloc_off;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000204 info->write += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000205 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000206 relocated = 1;
207 }
208#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000209
210 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
211 &fs_partition, 1);
212 if (part < 0)
213 return -1;
214
Simon Glass436e2b72012-12-26 09:53:29 +0000215 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
216 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
217 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000218 continue;
219
Stephen Warren377202b2014-02-03 13:21:01 -0700220 if (!fs_dev_desc && !info->null_dev_desc_ok)
221 continue;
222
Simon Glass2ded0d42012-12-26 09:53:31 +0000223 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass436e2b72012-12-26 09:53:29 +0000224 fs_type = info->fstype;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000225 return 0;
226 }
227 }
228
Stephen Warren045fa1e2012-10-22 06:43:51 +0000229 return -1;
230}
231
232static void fs_close(void)
233{
Simon Glassc6f548d2012-12-26 09:53:30 +0000234 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000235
Simon Glassc6f548d2012-12-26 09:53:30 +0000236 info->close();
Simon Glasse6d52412012-12-26 09:53:33 +0000237
Stephen Warren045fa1e2012-10-22 06:43:51 +0000238 fs_type = FS_TYPE_ANY;
239}
240
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100241int fs_uuid(char *uuid_str)
242{
243 struct fstype_info *info = fs_get_info(fs_type);
244
245 return info->uuid(uuid_str);
246}
247
Stephen Warren045fa1e2012-10-22 06:43:51 +0000248int fs_ls(const char *dirname)
249{
250 int ret;
251
Simon Glassc6f548d2012-12-26 09:53:30 +0000252 struct fstype_info *info = fs_get_info(fs_type);
253
254 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000255
Simon Glasse6d52412012-12-26 09:53:33 +0000256 fs_type = FS_TYPE_ANY;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000257 fs_close();
258
259 return ret;
260}
261
Stephen Warren61529162014-02-03 13:21:00 -0700262int fs_exists(const char *filename)
263{
264 int ret;
265
266 struct fstype_info *info = fs_get_info(fs_type);
267
268 ret = info->exists(filename);
269
270 fs_close();
271
272 return ret;
273}
274
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800275int fs_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -0600276{
277 int ret;
278
279 struct fstype_info *info = fs_get_info(fs_type);
280
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800281 ret = info->size(filename, size);
Stephen Warrencf659812014-06-11 12:47:26 -0600282
283 fs_close();
284
285 return ret;
286}
287
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800288int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
289 loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000290{
Simon Glassc6f548d2012-12-26 09:53:30 +0000291 struct fstype_info *info = fs_get_info(fs_type);
Simon Glass117e0502012-12-26 09:53:32 +0000292 void *buf;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000293 int ret;
294
Simon Glass117e0502012-12-26 09:53:32 +0000295 /*
296 * We don't actually know how many bytes are being read, since len==0
297 * means read the whole file.
298 */
299 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800300 ret = info->read(filename, buf, offset, len, actread);
Simon Glass117e0502012-12-26 09:53:32 +0000301 unmap_sysmem(buf);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000302
Simon Glassc6f548d2012-12-26 09:53:30 +0000303 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher7a3e70c2015-08-05 17:16:58 +0200304 if (ret == 0 && len && *actread != len)
305 printf("** %s shorter than offset + len **\n", filename);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000306 fs_close();
307
308 return ret;
309}
310
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800311int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
312 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000313{
314 struct fstype_info *info = fs_get_info(fs_type);
315 void *buf;
316 int ret;
317
Simon Glassa8f6ab52013-04-20 08:42:50 +0000318 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800319 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000320 unmap_sysmem(buf);
321
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800322 if (ret < 0 && len != *actwrite) {
Simon Glassa8f6ab52013-04-20 08:42:50 +0000323 printf("** Unable to write file %s **\n", filename);
324 ret = -1;
325 }
326 fs_close();
327
328 return ret;
329}
330
Stephen Warrencf659812014-06-11 12:47:26 -0600331int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
332 int fstype)
333{
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800334 loff_t size;
Stephen Warrencf659812014-06-11 12:47:26 -0600335
336 if (argc != 4)
337 return CMD_RET_USAGE;
338
339 if (fs_set_blk_dev(argv[1], argv[2], fstype))
340 return 1;
341
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800342 if (fs_size(argv[3], &size) < 0)
Stephen Warrencf659812014-06-11 12:47:26 -0600343 return CMD_RET_FAILURE;
344
345 setenv_hex("filesize", size);
346
347 return 0;
348}
349
Stephen Warrenf9b55e22012-10-31 11:05:07 +0000350int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200351 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000352{
353 unsigned long addr;
354 const char *addr_str;
355 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800356 loff_t bytes;
357 loff_t pos;
358 loff_t len_read;
359 int ret;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100360 unsigned long time;
Pavel Machek949bbd72014-07-09 22:42:57 +0200361 char *ep;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000362
Stephen Warrene9b0f992012-10-30 12:04:17 +0000363 if (argc < 2)
364 return CMD_RET_USAGE;
365 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000366 return CMD_RET_USAGE;
367
Stephen Warrene9b0f992012-10-30 12:04:17 +0000368 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000369 return 1;
370
371 if (argc >= 4) {
Pavel Machek949bbd72014-07-09 22:42:57 +0200372 addr = simple_strtoul(argv[3], &ep, 16);
373 if (ep == argv[3] || *ep != '\0')
374 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000375 } else {
376 addr_str = getenv("loadaddr");
377 if (addr_str != NULL)
378 addr = simple_strtoul(addr_str, NULL, 16);
379 else
380 addr = CONFIG_SYS_LOAD_ADDR;
381 }
382 if (argc >= 5) {
383 filename = argv[4];
384 } else {
385 filename = getenv("bootfile");
386 if (!filename) {
387 puts("** No boot file defined **\n");
388 return 1;
389 }
390 }
391 if (argc >= 6)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200392 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000393 else
394 bytes = 0;
395 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200396 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000397 else
398 pos = 0;
399
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100400 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800401 ret = fs_read(filename, addr, pos, bytes, &len_read);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100402 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800403 if (ret < 0)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000404 return 1;
405
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800406 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100407 if (time > 0) {
408 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500409 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100410 puts(")");
411 }
412 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000413
Simon Glass49c4f032013-02-24 17:33:23 +0000414 setenv_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000415
416 return 0;
417}
418
419int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
420 int fstype)
421{
422 if (argc < 2)
423 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000424 if (argc > 4)
425 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000426
427 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
428 return 1;
429
Stephen Warrene9b0f992012-10-30 12:04:17 +0000430 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000431 return 1;
432
433 return 0;
434}
Simon Glassa8f6ab52013-04-20 08:42:50 +0000435
Stephen Warren61529162014-02-03 13:21:00 -0700436int file_exists(const char *dev_type, const char *dev_part, const char *file,
437 int fstype)
438{
439 if (fs_set_blk_dev(dev_type, dev_part, fstype))
440 return 0;
441
442 return fs_exists(file);
443}
444
Simon Glassa8f6ab52013-04-20 08:42:50 +0000445int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200446 int fstype)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000447{
448 unsigned long addr;
449 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800450 loff_t bytes;
451 loff_t pos;
452 loff_t len;
453 int ret;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000454 unsigned long time;
455
456 if (argc < 6 || argc > 7)
457 return CMD_RET_USAGE;
458
459 if (fs_set_blk_dev(argv[1], argv[2], fstype))
460 return 1;
461
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800462 addr = simple_strtoul(argv[3], NULL, 16);
463 filename = argv[4];
Wolfgang Denkb770e882013-10-05 21:07:25 +0200464 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000465 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200466 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000467 else
468 pos = 0;
469
470 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800471 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000472 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800473 if (ret < 0)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000474 return 1;
475
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800476 printf("%llu bytes written in %lu ms", len, time);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000477 if (time > 0) {
478 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500479 print_size(div_u64(len, time) * 1000, "/s");
Simon Glassa8f6ab52013-04-20 08:42:50 +0000480 puts(")");
481 }
482 puts("\n");
483
484 return 0;
485}
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100486
487int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
488 int fstype)
489{
490 int ret;
491 char uuid[37];
492 memset(uuid, 0, sizeof(uuid));
493
494 if (argc < 3 || argc > 4)
495 return CMD_RET_USAGE;
496
497 if (fs_set_blk_dev(argv[1], argv[2], fstype))
498 return 1;
499
500 ret = fs_uuid(uuid);
501 if (ret)
502 return CMD_RET_FAILURE;
503
504 if (argc == 4)
505 setenv(argv[3], uuid);
506 else
507 printf("%s\n", uuid);
508
509 return CMD_RET_SUCCESS;
510}
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100511
512int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
513{
514 struct fstype_info *info;
515
516 if (argc < 3 || argc > 4)
517 return CMD_RET_USAGE;
518
519 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
520 return 1;
521
522 info = fs_get_info(fs_type);
523
524 if (argc == 4)
525 setenv(argv[3], info->name);
526 else
527 printf("%s\n", info->name);
528
529 return CMD_RET_SUCCESS;
530}
531