blob: 483273fe20b8212075f74526cb86aa2ae3e36988 [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>
20#include <part.h>
21#include <ext4fs.h>
22#include <fat.h>
23#include <fs.h>
Simon Glass92ccc962012-12-26 09:53:35 +000024#include <sandboxfs.h>
Simon Glass117e0502012-12-26 09:53:32 +000025#include <asm/io.h>
Tom Rini9e374e72014-11-24 11:50:46 -050026#include <div64.h>
27#include <linux/math64.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000028
Stephen Warrena1b231c2012-10-30 07:50:47 +000029DECLARE_GLOBAL_DATA_PTR;
30
Stephen Warren045fa1e2012-10-22 06:43:51 +000031static block_dev_desc_t *fs_dev_desc;
32static disk_partition_t fs_partition;
33static int fs_type = FS_TYPE_ANY;
34
Simon Glass2ded0d42012-12-26 09:53:31 +000035static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
36 disk_partition_t *fs_partition)
Simon Glass436e2b72012-12-26 09:53:29 +000037{
38 printf("** Unrecognized filesystem type **\n");
39 return -1;
40}
41
Stephen Warren045fa1e2012-10-22 06:43:51 +000042static inline int fs_ls_unsupported(const char *dirname)
43{
Stephen Warren045fa1e2012-10-22 06:43:51 +000044 return -1;
45}
46
Stephen Warren61529162014-02-03 13:21:00 -070047static inline int fs_exists_unsupported(const char *filename)
48{
49 return 0;
50}
51
Suriyan Ramasamid455d872014-11-17 14:39:38 -080052static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -060053{
54 return -1;
55}
56
Simon Glass117e0502012-12-26 09:53:32 +000057static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080058 loff_t offset, loff_t len,
59 loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +000060{
Stephen Warren045fa1e2012-10-22 06:43:51 +000061 return -1;
62}
63
Simon Glassa8f6ab52013-04-20 08:42:50 +000064static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080065 loff_t offset, loff_t len,
66 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +000067{
68 return -1;
69}
70
Simon Glass436e2b72012-12-26 09:53:29 +000071static inline void fs_close_unsupported(void)
72{
73}
74
Christian Gmeiner59e890e2014-11-12 14:35:04 +010075static inline int fs_uuid_unsupported(char *uuid_str)
76{
77 return -1;
78}
79
Simon Glass436e2b72012-12-26 09:53:29 +000080struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +000081 int fstype;
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +010082 char *name;
Stephen Warren377202b2014-02-03 13:21:01 -070083 /*
84 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
85 * should be false in most cases. For "virtual" filesystems which
86 * aren't based on a U-Boot block device (e.g. sandbox), this can be
87 * set to true. This should also be true for the dumm entry at the end
88 * of fstypes[], since that is essentially a "virtual" (non-existent)
89 * filesystem.
90 */
91 bool null_dev_desc_ok;
Simon Glass2ded0d42012-12-26 09:53:31 +000092 int (*probe)(block_dev_desc_t *fs_dev_desc,
93 disk_partition_t *fs_partition);
Simon Glass436e2b72012-12-26 09:53:29 +000094 int (*ls)(const char *dirname);
Stephen Warren61529162014-02-03 13:21:00 -070095 int (*exists)(const char *filename);
Suriyan Ramasamid455d872014-11-17 14:39:38 -080096 int (*size)(const char *filename, loff_t *size);
97 int (*read)(const char *filename, void *buf, loff_t offset,
98 loff_t len, loff_t *actread);
99 int (*write)(const char *filename, void *buf, loff_t offset,
100 loff_t len, loff_t *actwrite);
Simon Glass436e2b72012-12-26 09:53:29 +0000101 void (*close)(void);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100102 int (*uuid)(char *uuid_str);
Simon Glass436e2b72012-12-26 09:53:29 +0000103};
104
105static struct fstype_info fstypes[] = {
106#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +0000107 {
108 .fstype = FS_TYPE_FAT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100109 .name = "fat",
Stephen Warren377202b2014-02-03 13:21:01 -0700110 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000111 .probe = fat_set_blk_dev,
112 .close = fat_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000113 .ls = file_fat_ls,
Stephen Warrenb7b5f312014-02-03 13:21:10 -0700114 .exists = fat_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600115 .size = fat_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000116 .read = fat_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800117#ifdef CONFIG_FAT_WRITE
118 .write = file_fat_write,
119#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700120 .write = fs_write_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800121#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100122 .uuid = fs_uuid_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000123 },
Simon Glass436e2b72012-12-26 09:53:29 +0000124#endif
125#ifdef CONFIG_FS_EXT4
Stephen Warren045fa1e2012-10-22 06:43:51 +0000126 {
127 .fstype = FS_TYPE_EXT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100128 .name = "ext4",
Stephen Warren377202b2014-02-03 13:21:01 -0700129 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000130 .probe = ext4fs_probe,
131 .close = ext4fs_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000132 .ls = ext4fs_ls,
Stephen Warren55af5c92014-02-03 13:21:09 -0700133 .exists = ext4fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600134 .size = ext4fs_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000135 .read = ext4_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800136#ifdef CONFIG_CMD_EXT4_WRITE
137 .write = ext4_write_file,
138#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700139 .write = fs_write_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800140#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100141 .uuid = ext4fs_uuid,
Simon Glass436e2b72012-12-26 09:53:29 +0000142 },
143#endif
Simon Glass92ccc962012-12-26 09:53:35 +0000144#ifdef CONFIG_SANDBOX
145 {
146 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100147 .name = "sandbox",
Stephen Warren377202b2014-02-03 13:21:01 -0700148 .null_dev_desc_ok = true,
Simon Glass92ccc962012-12-26 09:53:35 +0000149 .probe = sandbox_fs_set_blk_dev,
150 .close = sandbox_fs_close,
151 .ls = sandbox_fs_ls,
Stephen Warren0a30aa12014-02-03 13:21:07 -0700152 .exists = sandbox_fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600153 .size = sandbox_fs_size,
Simon Glass92ccc962012-12-26 09:53:35 +0000154 .read = fs_read_sandbox,
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000155 .write = fs_write_sandbox,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100156 .uuid = fs_uuid_unsupported,
Simon Glass92ccc962012-12-26 09:53:35 +0000157 },
158#endif
Simon Glass436e2b72012-12-26 09:53:29 +0000159 {
160 .fstype = FS_TYPE_ANY,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100161 .name = "unsupported",
Stephen Warren377202b2014-02-03 13:21:01 -0700162 .null_dev_desc_ok = true,
Simon Glass436e2b72012-12-26 09:53:29 +0000163 .probe = fs_probe_unsupported,
164 .close = fs_close_unsupported,
165 .ls = fs_ls_unsupported,
Stephen Warren61529162014-02-03 13:21:00 -0700166 .exists = fs_exists_unsupported,
Stephen Warrencf659812014-06-11 12:47:26 -0600167 .size = fs_size_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000168 .read = fs_read_unsupported,
Simon Glassa8f6ab52013-04-20 08:42:50 +0000169 .write = fs_write_unsupported,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100170 .uuid = fs_uuid_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000171 },
172};
173
Simon Glassc6f548d2012-12-26 09:53:30 +0000174static struct fstype_info *fs_get_info(int fstype)
175{
176 struct fstype_info *info;
177 int i;
178
179 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
180 if (fstype == info->fstype)
181 return info;
182 }
183
184 /* Return the 'unsupported' sentinel */
185 return info;
186}
187
Stephen Warren045fa1e2012-10-22 06:43:51 +0000188int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
189{
Simon Glass436e2b72012-12-26 09:53:29 +0000190 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000191 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000192#ifdef CONFIG_NEEDS_MANUAL_RELOC
193 static int relocated;
194
195 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000196 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
197 i++, info++) {
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100198 info->name += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000199 info->probe += gd->reloc_off;
200 info->close += gd->reloc_off;
201 info->ls += gd->reloc_off;
202 info->read += gd->reloc_off;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000203 info->write += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000204 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000205 relocated = 1;
206 }
207#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000208
209 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
210 &fs_partition, 1);
211 if (part < 0)
212 return -1;
213
Simon Glass436e2b72012-12-26 09:53:29 +0000214 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
215 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
216 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000217 continue;
218
Stephen Warren377202b2014-02-03 13:21:01 -0700219 if (!fs_dev_desc && !info->null_dev_desc_ok)
220 continue;
221
Simon Glass2ded0d42012-12-26 09:53:31 +0000222 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass436e2b72012-12-26 09:53:29 +0000223 fs_type = info->fstype;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000224 return 0;
225 }
226 }
227
Stephen Warren045fa1e2012-10-22 06:43:51 +0000228 return -1;
229}
230
231static void fs_close(void)
232{
Simon Glassc6f548d2012-12-26 09:53:30 +0000233 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000234
Simon Glassc6f548d2012-12-26 09:53:30 +0000235 info->close();
Simon Glasse6d52412012-12-26 09:53:33 +0000236
Stephen Warren045fa1e2012-10-22 06:43:51 +0000237 fs_type = FS_TYPE_ANY;
238}
239
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100240int fs_uuid(char *uuid_str)
241{
242 struct fstype_info *info = fs_get_info(fs_type);
243
244 return info->uuid(uuid_str);
245}
246
Stephen Warren045fa1e2012-10-22 06:43:51 +0000247int fs_ls(const char *dirname)
248{
249 int ret;
250
Simon Glassc6f548d2012-12-26 09:53:30 +0000251 struct fstype_info *info = fs_get_info(fs_type);
252
253 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000254
Simon Glasse6d52412012-12-26 09:53:33 +0000255 fs_type = FS_TYPE_ANY;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000256 fs_close();
257
258 return ret;
259}
260
Stephen Warren61529162014-02-03 13:21:00 -0700261int fs_exists(const char *filename)
262{
263 int ret;
264
265 struct fstype_info *info = fs_get_info(fs_type);
266
267 ret = info->exists(filename);
268
269 fs_close();
270
271 return ret;
272}
273
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800274int fs_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -0600275{
276 int ret;
277
278 struct fstype_info *info = fs_get_info(fs_type);
279
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800280 ret = info->size(filename, size);
Stephen Warrencf659812014-06-11 12:47:26 -0600281
282 fs_close();
283
284 return ret;
285}
286
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800287int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
288 loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000289{
Simon Glassc6f548d2012-12-26 09:53:30 +0000290 struct fstype_info *info = fs_get_info(fs_type);
Simon Glass117e0502012-12-26 09:53:32 +0000291 void *buf;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000292 int ret;
293
Simon Glass117e0502012-12-26 09:53:32 +0000294 /*
295 * We don't actually know how many bytes are being read, since len==0
296 * means read the whole file.
297 */
298 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800299 ret = info->read(filename, buf, offset, len, actread);
Simon Glass117e0502012-12-26 09:53:32 +0000300 unmap_sysmem(buf);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000301
Simon Glassc6f548d2012-12-26 09:53:30 +0000302 /* If we requested a specific number of bytes, check we got it */
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800303 if (ret == 0 && len && *actread != len) {
Simon Glassc6f548d2012-12-26 09:53:30 +0000304 printf("** Unable to read file %s **\n", filename);
305 ret = -1;
306 }
Stephen Warren045fa1e2012-10-22 06:43:51 +0000307 fs_close();
308
309 return ret;
310}
311
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800312int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
313 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000314{
315 struct fstype_info *info = fs_get_info(fs_type);
316 void *buf;
317 int ret;
318
Simon Glassa8f6ab52013-04-20 08:42:50 +0000319 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800320 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000321 unmap_sysmem(buf);
322
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800323 if (ret < 0 && len != *actwrite) {
Simon Glassa8f6ab52013-04-20 08:42:50 +0000324 printf("** Unable to write file %s **\n", filename);
325 ret = -1;
326 }
327 fs_close();
328
329 return ret;
330}
331
Stephen Warrencf659812014-06-11 12:47:26 -0600332int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
333 int fstype)
334{
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800335 loff_t size;
Stephen Warrencf659812014-06-11 12:47:26 -0600336
337 if (argc != 4)
338 return CMD_RET_USAGE;
339
340 if (fs_set_blk_dev(argv[1], argv[2], fstype))
341 return 1;
342
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800343 if (fs_size(argv[3], &size) < 0)
Stephen Warrencf659812014-06-11 12:47:26 -0600344 return CMD_RET_FAILURE;
345
346 setenv_hex("filesize", size);
347
348 return 0;
349}
350
Stephen Warrenf9b55e22012-10-31 11:05:07 +0000351int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200352 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000353{
354 unsigned long addr;
355 const char *addr_str;
356 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800357 loff_t bytes;
358 loff_t pos;
359 loff_t len_read;
360 int ret;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100361 unsigned long time;
Pavel Machek949bbd72014-07-09 22:42:57 +0200362 char *ep;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000363
Stephen Warrene9b0f992012-10-30 12:04:17 +0000364 if (argc < 2)
365 return CMD_RET_USAGE;
366 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000367 return CMD_RET_USAGE;
368
Stephen Warrene9b0f992012-10-30 12:04:17 +0000369 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000370 return 1;
371
372 if (argc >= 4) {
Pavel Machek949bbd72014-07-09 22:42:57 +0200373 addr = simple_strtoul(argv[3], &ep, 16);
374 if (ep == argv[3] || *ep != '\0')
375 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000376 } else {
377 addr_str = getenv("loadaddr");
378 if (addr_str != NULL)
379 addr = simple_strtoul(addr_str, NULL, 16);
380 else
381 addr = CONFIG_SYS_LOAD_ADDR;
382 }
383 if (argc >= 5) {
384 filename = argv[4];
385 } else {
386 filename = getenv("bootfile");
387 if (!filename) {
388 puts("** No boot file defined **\n");
389 return 1;
390 }
391 }
392 if (argc >= 6)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200393 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000394 else
395 bytes = 0;
396 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200397 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000398 else
399 pos = 0;
400
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100401 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800402 ret = fs_read(filename, addr, pos, bytes, &len_read);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100403 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800404 if (ret < 0)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000405 return 1;
406
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800407 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100408 if (time > 0) {
409 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500410 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100411 puts(")");
412 }
413 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000414
Simon Glass49c4f032013-02-24 17:33:23 +0000415 setenv_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000416
417 return 0;
418}
419
420int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
421 int fstype)
422{
423 if (argc < 2)
424 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000425 if (argc > 4)
426 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000427
428 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
429 return 1;
430
Stephen Warrene9b0f992012-10-30 12:04:17 +0000431 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000432 return 1;
433
434 return 0;
435}
Simon Glassa8f6ab52013-04-20 08:42:50 +0000436
Stephen Warren61529162014-02-03 13:21:00 -0700437int file_exists(const char *dev_type, const char *dev_part, const char *file,
438 int fstype)
439{
440 if (fs_set_blk_dev(dev_type, dev_part, fstype))
441 return 0;
442
443 return fs_exists(file);
444}
445
Simon Glassa8f6ab52013-04-20 08:42:50 +0000446int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200447 int fstype)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000448{
449 unsigned long addr;
450 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800451 loff_t bytes;
452 loff_t pos;
453 loff_t len;
454 int ret;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000455 unsigned long time;
456
457 if (argc < 6 || argc > 7)
458 return CMD_RET_USAGE;
459
460 if (fs_set_blk_dev(argv[1], argv[2], fstype))
461 return 1;
462
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800463 addr = simple_strtoul(argv[3], NULL, 16);
464 filename = argv[4];
Wolfgang Denkb770e882013-10-05 21:07:25 +0200465 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000466 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200467 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000468 else
469 pos = 0;
470
471 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800472 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000473 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800474 if (ret < 0)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000475 return 1;
476
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800477 printf("%llu bytes written in %lu ms", len, time);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000478 if (time > 0) {
479 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500480 print_size(div_u64(len, time) * 1000, "/s");
Simon Glassa8f6ab52013-04-20 08:42:50 +0000481 puts(")");
482 }
483 puts("\n");
484
485 return 0;
486}
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100487
488int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
489 int fstype)
490{
491 int ret;
492 char uuid[37];
493 memset(uuid, 0, sizeof(uuid));
494
495 if (argc < 3 || argc > 4)
496 return CMD_RET_USAGE;
497
498 if (fs_set_blk_dev(argv[1], argv[2], fstype))
499 return 1;
500
501 ret = fs_uuid(uuid);
502 if (ret)
503 return CMD_RET_FAILURE;
504
505 if (argc == 4)
506 setenv(argv[3], uuid);
507 else
508 printf("%s\n", uuid);
509
510 return CMD_RET_SUCCESS;
511}
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100512
513int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
514{
515 struct fstype_info *info;
516
517 if (argc < 3 || argc > 4)
518 return CMD_RET_USAGE;
519
520 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
521 return 1;
522
523 info = fs_get_info(fs_type);
524
525 if (argc == 4)
526 setenv(argv[3], info->name);
527 else
528 printf("%s\n", info->name);
529
530 return CMD_RET_SUCCESS;
531}
532