]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0 |
045fa1e1 SW |
2 | /* |
3 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. | |
045fa1e1 SW |
4 | */ |
5 | ||
6 | #include <config.h> | |
59e890ef | 7 | #include <errno.h> |
045fa1e1 | 8 | #include <common.h> |
0eb25b61 | 9 | #include <mapmem.h> |
045fa1e1 SW |
10 | #include <part.h> |
11 | #include <ext4fs.h> | |
12 | #include <fat.h> | |
13 | #include <fs.h> | |
92ccc96b | 14 | #include <sandboxfs.h> |
251cee0d | 15 | #include <ubifs_uboot.h> |
0c936ee3 | 16 | #include <btrfs.h> |
117e0507 | 17 | #include <asm/io.h> |
9e374e7b TR |
18 | #include <div64.h> |
19 | #include <linux/math64.h> | |
045fa1e1 | 20 | |
a1b231ce SW |
21 | DECLARE_GLOBAL_DATA_PTR; |
22 | ||
4101f687 | 23 | static struct blk_desc *fs_dev_desc; |
4bbcc965 | 24 | static int fs_dev_part; |
045fa1e1 SW |
25 | static disk_partition_t fs_partition; |
26 | static int fs_type = FS_TYPE_ANY; | |
27 | ||
4101f687 | 28 | static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc, |
2ded0d47 | 29 | disk_partition_t *fs_partition) |
436e2b73 SG |
30 | { |
31 | printf("** Unrecognized filesystem type **\n"); | |
32 | return -1; | |
33 | } | |
34 | ||
045fa1e1 SW |
35 | static inline int fs_ls_unsupported(const char *dirname) |
36 | { | |
045fa1e1 SW |
37 | return -1; |
38 | } | |
39 | ||
89191d62 RC |
40 | /* generic implementation of ls in terms of opendir/readdir/closedir */ |
41 | __maybe_unused | |
42 | static 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 | ||
6152916a SW |
69 | static inline int fs_exists_unsupported(const char *filename) |
70 | { | |
71 | return 0; | |
72 | } | |
73 | ||
d455d878 | 74 | static inline int fs_size_unsupported(const char *filename, loff_t *size) |
cf659819 SW |
75 | { |
76 | return -1; | |
77 | } | |
78 | ||
117e0507 | 79 | static inline int fs_read_unsupported(const char *filename, void *buf, |
d455d878 SR |
80 | loff_t offset, loff_t len, |
81 | loff_t *actread) | |
045fa1e1 | 82 | { |
045fa1e1 SW |
83 | return -1; |
84 | } | |
85 | ||
a8f6ab52 | 86 | static inline int fs_write_unsupported(const char *filename, void *buf, |
d455d878 SR |
87 | loff_t offset, loff_t len, |
88 | loff_t *actwrite) | |
a8f6ab52 SG |
89 | { |
90 | return -1; | |
91 | } | |
92 | ||
436e2b73 SG |
93 | static inline void fs_close_unsupported(void) |
94 | { | |
95 | } | |
96 | ||
59e890ef CG |
97 | static inline int fs_uuid_unsupported(char *uuid_str) |
98 | { | |
99 | return -1; | |
100 | } | |
101 | ||
4bbcc965 RC |
102 | static inline int fs_opendir_unsupported(const char *filename, |
103 | struct fs_dir_stream **dirs) | |
104 | { | |
105 | return -EACCES; | |
106 | } | |
107 | ||
e2519daf AT |
108 | static inline int fs_unlink_unsupported(const char *filename) |
109 | { | |
110 | return -1; | |
111 | } | |
112 | ||
e7074cff AT |
113 | static inline int fs_mkdir_unsupported(const char *dirname) |
114 | { | |
115 | return -1; | |
116 | } | |
117 | ||
436e2b73 | 118 | struct fstype_info { |
045fa1e1 | 119 | int fstype; |
1a1ad8e0 | 120 | char *name; |
377202b5 SW |
121 | /* |
122 | * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This | |
123 | * should be false in most cases. For "virtual" filesystems which | |
124 | * aren't based on a U-Boot block device (e.g. sandbox), this can be | |
ca230b09 | 125 | * set to true. This should also be true for the dummy entry at the end |
377202b5 SW |
126 | * of fstypes[], since that is essentially a "virtual" (non-existent) |
127 | * filesystem. | |
128 | */ | |
129 | bool null_dev_desc_ok; | |
4101f687 | 130 | int (*probe)(struct blk_desc *fs_dev_desc, |
2ded0d47 | 131 | disk_partition_t *fs_partition); |
436e2b73 | 132 | int (*ls)(const char *dirname); |
6152916a | 133 | int (*exists)(const char *filename); |
d455d878 SR |
134 | int (*size)(const char *filename, loff_t *size); |
135 | int (*read)(const char *filename, void *buf, loff_t offset, | |
136 | loff_t len, loff_t *actread); | |
137 | int (*write)(const char *filename, void *buf, loff_t offset, | |
138 | loff_t len, loff_t *actwrite); | |
436e2b73 | 139 | void (*close)(void); |
59e890ef | 140 | int (*uuid)(char *uuid_str); |
4bbcc965 RC |
141 | /* |
142 | * Open a directory stream. On success return 0 and directory | |
143 | * stream pointer via 'dirsp'. On error, return -errno. See | |
144 | * fs_opendir(). | |
145 | */ | |
146 | int (*opendir)(const char *filename, struct fs_dir_stream **dirsp); | |
147 | /* | |
148 | * Read next entry from directory stream. On success return 0 | |
149 | * and directory entry pointer via 'dentp'. On error return | |
150 | * -errno. See fs_readdir(). | |
151 | */ | |
152 | int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp); | |
153 | /* see fs_closedir() */ | |
154 | void (*closedir)(struct fs_dir_stream *dirs); | |
e2519daf | 155 | int (*unlink)(const char *filename); |
e7074cff | 156 | int (*mkdir)(const char *dirname); |
436e2b73 SG |
157 | }; |
158 | ||
159 | static struct fstype_info fstypes[] = { | |
160 | #ifdef CONFIG_FS_FAT | |
045fa1e1 SW |
161 | { |
162 | .fstype = FS_TYPE_FAT, | |
1a1ad8e0 | 163 | .name = "fat", |
377202b5 | 164 | .null_dev_desc_ok = false, |
e6d52415 SG |
165 | .probe = fat_set_blk_dev, |
166 | .close = fat_close, | |
89191d62 | 167 | .ls = fs_ls_generic, |
b7b5f319 | 168 | .exists = fat_exists, |
cf659819 | 169 | .size = fat_size, |
e6d52415 | 170 | .read = fat_read_file, |
d455d878 SR |
171 | #ifdef CONFIG_FAT_WRITE |
172 | .write = file_fat_write, | |
f8240ce9 | 173 | .unlink = fat_unlink, |
31a18d57 | 174 | .mkdir = fat_mkdir, |
d455d878 | 175 | #else |
bd6fb31f | 176 | .write = fs_write_unsupported, |
f8240ce9 | 177 | .unlink = fs_unlink_unsupported, |
31a18d57 | 178 | .mkdir = fs_mkdir_unsupported, |
d455d878 | 179 | #endif |
59e890ef | 180 | .uuid = fs_uuid_unsupported, |
89191d62 RC |
181 | .opendir = fat_opendir, |
182 | .readdir = fat_readdir, | |
183 | .closedir = fat_closedir, | |
045fa1e1 | 184 | }, |
436e2b73 SG |
185 | #endif |
186 | #ifdef CONFIG_FS_EXT4 | |
045fa1e1 SW |
187 | { |
188 | .fstype = FS_TYPE_EXT, | |
1a1ad8e0 | 189 | .name = "ext4", |
377202b5 | 190 | .null_dev_desc_ok = false, |
e6d52415 SG |
191 | .probe = ext4fs_probe, |
192 | .close = ext4fs_close, | |
436e2b73 | 193 | .ls = ext4fs_ls, |
55af5c93 | 194 | .exists = ext4fs_exists, |
cf659819 | 195 | .size = ext4fs_size, |
e6d52415 | 196 | .read = ext4_read_file, |
d455d878 SR |
197 | #ifdef CONFIG_CMD_EXT4_WRITE |
198 | .write = ext4_write_file, | |
199 | #else | |
bd6fb31f | 200 | .write = fs_write_unsupported, |
d455d878 | 201 | #endif |
59e890ef | 202 | .uuid = ext4fs_uuid, |
4bbcc965 | 203 | .opendir = fs_opendir_unsupported, |
e2519daf | 204 | .unlink = fs_unlink_unsupported, |
e7074cff | 205 | .mkdir = fs_mkdir_unsupported, |
436e2b73 | 206 | }, |
92ccc96b SG |
207 | #endif |
208 | #ifdef CONFIG_SANDBOX | |
209 | { | |
210 | .fstype = FS_TYPE_SANDBOX, | |
1a1ad8e0 | 211 | .name = "sandbox", |
377202b5 | 212 | .null_dev_desc_ok = true, |
92ccc96b SG |
213 | .probe = sandbox_fs_set_blk_dev, |
214 | .close = sandbox_fs_close, | |
215 | .ls = sandbox_fs_ls, | |
0a30aa1e | 216 | .exists = sandbox_fs_exists, |
cf659819 | 217 | .size = sandbox_fs_size, |
92ccc96b | 218 | .read = fs_read_sandbox, |
7eb2c8d5 | 219 | .write = fs_write_sandbox, |
59e890ef | 220 | .uuid = fs_uuid_unsupported, |
4bbcc965 | 221 | .opendir = fs_opendir_unsupported, |
e2519daf | 222 | .unlink = fs_unlink_unsupported, |
e7074cff | 223 | .mkdir = fs_mkdir_unsupported, |
92ccc96b | 224 | }, |
251cee0d HG |
225 | #endif |
226 | #ifdef CONFIG_CMD_UBIFS | |
227 | { | |
228 | .fstype = FS_TYPE_UBIFS, | |
229 | .name = "ubifs", | |
230 | .null_dev_desc_ok = true, | |
231 | .probe = ubifs_set_blk_dev, | |
232 | .close = ubifs_close, | |
233 | .ls = ubifs_ls, | |
234 | .exists = ubifs_exists, | |
235 | .size = ubifs_size, | |
236 | .read = ubifs_read, | |
237 | .write = fs_write_unsupported, | |
238 | .uuid = fs_uuid_unsupported, | |
4bbcc965 | 239 | .opendir = fs_opendir_unsupported, |
e2519daf | 240 | .unlink = fs_unlink_unsupported, |
e7074cff | 241 | .mkdir = fs_mkdir_unsupported, |
251cee0d | 242 | }, |
0c936ee3 MB |
243 | #endif |
244 | #ifdef CONFIG_FS_BTRFS | |
245 | { | |
246 | .fstype = FS_TYPE_BTRFS, | |
247 | .name = "btrfs", | |
248 | .null_dev_desc_ok = false, | |
249 | .probe = btrfs_probe, | |
250 | .close = btrfs_close, | |
251 | .ls = btrfs_ls, | |
252 | .exists = btrfs_exists, | |
253 | .size = btrfs_size, | |
254 | .read = btrfs_read, | |
255 | .write = fs_write_unsupported, | |
256 | .uuid = btrfs_uuid, | |
38fc683d | 257 | .opendir = fs_opendir_unsupported, |
e2519daf | 258 | .unlink = fs_unlink_unsupported, |
e7074cff | 259 | .mkdir = fs_mkdir_unsupported, |
0c936ee3 | 260 | }, |
436e2b73 SG |
261 | #endif |
262 | { | |
263 | .fstype = FS_TYPE_ANY, | |
1a1ad8e0 | 264 | .name = "unsupported", |
377202b5 | 265 | .null_dev_desc_ok = true, |
436e2b73 SG |
266 | .probe = fs_probe_unsupported, |
267 | .close = fs_close_unsupported, | |
268 | .ls = fs_ls_unsupported, | |
6152916a | 269 | .exists = fs_exists_unsupported, |
cf659819 | 270 | .size = fs_size_unsupported, |
436e2b73 | 271 | .read = fs_read_unsupported, |
a8f6ab52 | 272 | .write = fs_write_unsupported, |
59e890ef | 273 | .uuid = fs_uuid_unsupported, |
4bbcc965 | 274 | .opendir = fs_opendir_unsupported, |
e2519daf | 275 | .unlink = fs_unlink_unsupported, |
e7074cff | 276 | .mkdir = fs_mkdir_unsupported, |
045fa1e1 SW |
277 | }, |
278 | }; | |
279 | ||
c6f548d2 SG |
280 | static struct fstype_info *fs_get_info(int fstype) |
281 | { | |
282 | struct fstype_info *info; | |
283 | int i; | |
284 | ||
285 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) { | |
286 | if (fstype == info->fstype) | |
287 | return info; | |
288 | } | |
289 | ||
290 | /* Return the 'unsupported' sentinel */ | |
291 | return info; | |
292 | } | |
293 | ||
0d488e8f AK |
294 | /** |
295 | * fs_get_type_name() - Get type of current filesystem | |
296 | * | |
297 | * Return: Pointer to filesystem name | |
298 | * | |
299 | * Returns a string describing the current filesystem, or the sentinel | |
300 | * "unsupported" for any unrecognised filesystem. | |
301 | */ | |
302 | const char *fs_get_type_name(void) | |
303 | { | |
304 | return fs_get_info(fs_type)->name; | |
305 | } | |
306 | ||
045fa1e1 SW |
307 | int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) |
308 | { | |
436e2b73 | 309 | struct fstype_info *info; |
045fa1e1 | 310 | int part, i; |
a1b231ce SW |
311 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
312 | static int relocated; | |
313 | ||
314 | if (!relocated) { | |
436e2b73 SG |
315 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); |
316 | i++, info++) { | |
1a1ad8e0 | 317 | info->name += gd->reloc_off; |
436e2b73 SG |
318 | info->probe += gd->reloc_off; |
319 | info->close += gd->reloc_off; | |
320 | info->ls += gd->reloc_off; | |
321 | info->read += gd->reloc_off; | |
a8f6ab52 | 322 | info->write += gd->reloc_off; |
436e2b73 | 323 | } |
a1b231ce SW |
324 | relocated = 1; |
325 | } | |
326 | #endif | |
045fa1e1 | 327 | |
e35929e4 | 328 | part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc, |
045fa1e1 SW |
329 | &fs_partition, 1); |
330 | if (part < 0) | |
331 | return -1; | |
332 | ||
436e2b73 SG |
333 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
334 | if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY && | |
335 | fstype != info->fstype) | |
045fa1e1 SW |
336 | continue; |
337 | ||
377202b5 SW |
338 | if (!fs_dev_desc && !info->null_dev_desc_ok) |
339 | continue; | |
340 | ||
4bbcc965 RC |
341 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
342 | fs_type = info->fstype; | |
343 | fs_dev_part = part; | |
344 | return 0; | |
345 | } | |
346 | } | |
347 | ||
348 | return -1; | |
349 | } | |
350 | ||
351 | /* set current blk device w/ blk_desc + partition # */ | |
352 | int fs_set_blk_dev_with_part(struct blk_desc *desc, int part) | |
353 | { | |
354 | struct fstype_info *info; | |
355 | int ret, i; | |
356 | ||
357 | if (part >= 1) | |
358 | ret = part_get_info(desc, part, &fs_partition); | |
359 | else | |
360 | ret = part_get_info_whole_disk(desc, &fs_partition); | |
361 | if (ret) | |
362 | return ret; | |
363 | fs_dev_desc = desc; | |
364 | ||
365 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { | |
2ded0d47 | 366 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
436e2b73 | 367 | fs_type = info->fstype; |
045fa1e1 SW |
368 | return 0; |
369 | } | |
370 | } | |
371 | ||
045fa1e1 SW |
372 | return -1; |
373 | } | |
374 | ||
375 | static void fs_close(void) | |
376 | { | |
c6f548d2 | 377 | struct fstype_info *info = fs_get_info(fs_type); |
045fa1e1 | 378 | |
c6f548d2 | 379 | info->close(); |
e6d52415 | 380 | |
045fa1e1 SW |
381 | fs_type = FS_TYPE_ANY; |
382 | } | |
383 | ||
59e890ef CG |
384 | int fs_uuid(char *uuid_str) |
385 | { | |
386 | struct fstype_info *info = fs_get_info(fs_type); | |
387 | ||
388 | return info->uuid(uuid_str); | |
389 | } | |
390 | ||
045fa1e1 SW |
391 | int fs_ls(const char *dirname) |
392 | { | |
393 | int ret; | |
394 | ||
c6f548d2 SG |
395 | struct fstype_info *info = fs_get_info(fs_type); |
396 | ||
397 | ret = info->ls(dirname); | |
045fa1e1 | 398 | |
e6d52415 | 399 | fs_type = FS_TYPE_ANY; |
045fa1e1 SW |
400 | fs_close(); |
401 | ||
402 | return ret; | |
403 | } | |
404 | ||
6152916a SW |
405 | int fs_exists(const char *filename) |
406 | { | |
407 | int ret; | |
408 | ||
409 | struct fstype_info *info = fs_get_info(fs_type); | |
410 | ||
411 | ret = info->exists(filename); | |
412 | ||
413 | fs_close(); | |
414 | ||
415 | return ret; | |
416 | } | |
417 | ||
d455d878 | 418 | int fs_size(const char *filename, loff_t *size) |
cf659819 SW |
419 | { |
420 | int ret; | |
421 | ||
422 | struct fstype_info *info = fs_get_info(fs_type); | |
423 | ||
d455d878 | 424 | ret = info->size(filename, size); |
cf659819 SW |
425 | |
426 | fs_close(); | |
427 | ||
428 | return ret; | |
429 | } | |
430 | ||
d455d878 SR |
431 | int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
432 | loff_t *actread) | |
045fa1e1 | 433 | { |
c6f548d2 | 434 | struct fstype_info *info = fs_get_info(fs_type); |
117e0507 | 435 | void *buf; |
045fa1e1 SW |
436 | int ret; |
437 | ||
117e0507 SG |
438 | /* |
439 | * We don't actually know how many bytes are being read, since len==0 | |
440 | * means read the whole file. | |
441 | */ | |
442 | buf = map_sysmem(addr, len); | |
d455d878 | 443 | ret = info->read(filename, buf, offset, len, actread); |
117e0507 | 444 | unmap_sysmem(buf); |
045fa1e1 | 445 | |
c6f548d2 | 446 | /* If we requested a specific number of bytes, check we got it */ |
7a3e70cf | 447 | if (ret == 0 && len && *actread != len) |
a327bde7 | 448 | debug("** %s shorter than offset + len **\n", filename); |
045fa1e1 SW |
449 | fs_close(); |
450 | ||
451 | return ret; | |
452 | } | |
453 | ||
d455d878 SR |
454 | int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len, |
455 | loff_t *actwrite) | |
a8f6ab52 SG |
456 | { |
457 | struct fstype_info *info = fs_get_info(fs_type); | |
458 | void *buf; | |
459 | int ret; | |
460 | ||
a8f6ab52 | 461 | buf = map_sysmem(addr, len); |
d455d878 | 462 | ret = info->write(filename, buf, offset, len, actwrite); |
a8f6ab52 SG |
463 | unmap_sysmem(buf); |
464 | ||
d455d878 | 465 | if (ret < 0 && len != *actwrite) { |
a8f6ab52 SG |
466 | printf("** Unable to write file %s **\n", filename); |
467 | ret = -1; | |
468 | } | |
469 | fs_close(); | |
470 | ||
471 | return ret; | |
472 | } | |
473 | ||
4bbcc965 RC |
474 | struct fs_dir_stream *fs_opendir(const char *filename) |
475 | { | |
476 | struct fstype_info *info = fs_get_info(fs_type); | |
477 | struct fs_dir_stream *dirs = NULL; | |
478 | int ret; | |
479 | ||
480 | ret = info->opendir(filename, &dirs); | |
481 | fs_close(); | |
482 | if (ret) { | |
483 | errno = -ret; | |
484 | return NULL; | |
485 | } | |
486 | ||
487 | dirs->desc = fs_dev_desc; | |
488 | dirs->part = fs_dev_part; | |
489 | ||
490 | return dirs; | |
491 | } | |
492 | ||
493 | struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs) | |
494 | { | |
495 | struct fstype_info *info; | |
496 | struct fs_dirent *dirent; | |
497 | int ret; | |
498 | ||
499 | fs_set_blk_dev_with_part(dirs->desc, dirs->part); | |
500 | info = fs_get_info(fs_type); | |
501 | ||
502 | ret = info->readdir(dirs, &dirent); | |
503 | fs_close(); | |
504 | if (ret) { | |
505 | errno = -ret; | |
506 | return NULL; | |
507 | } | |
508 | ||
509 | return dirent; | |
510 | } | |
511 | ||
512 | void fs_closedir(struct fs_dir_stream *dirs) | |
513 | { | |
514 | struct fstype_info *info; | |
515 | ||
516 | if (!dirs) | |
517 | return; | |
518 | ||
519 | fs_set_blk_dev_with_part(dirs->desc, dirs->part); | |
520 | info = fs_get_info(fs_type); | |
521 | ||
522 | info->closedir(dirs); | |
523 | fs_close(); | |
524 | } | |
525 | ||
e2519daf AT |
526 | int fs_unlink(const char *filename) |
527 | { | |
528 | int ret; | |
529 | ||
530 | struct fstype_info *info = fs_get_info(fs_type); | |
531 | ||
532 | ret = info->unlink(filename); | |
533 | ||
534 | fs_type = FS_TYPE_ANY; | |
535 | fs_close(); | |
536 | ||
537 | return ret; | |
538 | } | |
4bbcc965 | 539 | |
e7074cff AT |
540 | int fs_mkdir(const char *dirname) |
541 | { | |
542 | int ret; | |
543 | ||
544 | struct fstype_info *info = fs_get_info(fs_type); | |
545 | ||
546 | ret = info->mkdir(dirname); | |
547 | ||
548 | fs_type = FS_TYPE_ANY; | |
549 | fs_close(); | |
550 | ||
551 | return ret; | |
552 | } | |
553 | ||
cf659819 SW |
554 | int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
555 | int fstype) | |
556 | { | |
d455d878 | 557 | loff_t size; |
cf659819 SW |
558 | |
559 | if (argc != 4) | |
560 | return CMD_RET_USAGE; | |
561 | ||
562 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) | |
563 | return 1; | |
564 | ||
d455d878 | 565 | if (fs_size(argv[3], &size) < 0) |
cf659819 SW |
566 | return CMD_RET_FAILURE; |
567 | ||
018f5303 | 568 | env_set_hex("filesize", size); |
cf659819 SW |
569 | |
570 | return 0; | |
571 | } | |
572 | ||
f9b55e22 | 573 | int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
b770e88a | 574 | int fstype) |
045fa1e1 SW |
575 | { |
576 | unsigned long addr; | |
577 | const char *addr_str; | |
578 | const char *filename; | |
d455d878 SR |
579 | loff_t bytes; |
580 | loff_t pos; | |
581 | loff_t len_read; | |
582 | int ret; | |
da1fd96c | 583 | unsigned long time; |
949bbd7c | 584 | char *ep; |
045fa1e1 | 585 | |
e9b0f99e SW |
586 | if (argc < 2) |
587 | return CMD_RET_USAGE; | |
588 | if (argc > 7) | |
045fa1e1 SW |
589 | return CMD_RET_USAGE; |
590 | ||
e9b0f99e | 591 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
045fa1e1 SW |
592 | return 1; |
593 | ||
594 | if (argc >= 4) { | |
949bbd7c PM |
595 | addr = simple_strtoul(argv[3], &ep, 16); |
596 | if (ep == argv[3] || *ep != '\0') | |
597 | return CMD_RET_USAGE; | |
045fa1e1 | 598 | } else { |
00caae6d | 599 | addr_str = env_get("loadaddr"); |
045fa1e1 SW |
600 | if (addr_str != NULL) |
601 | addr = simple_strtoul(addr_str, NULL, 16); | |
602 | else | |
603 | addr = CONFIG_SYS_LOAD_ADDR; | |
604 | } | |
605 | if (argc >= 5) { | |
606 | filename = argv[4]; | |
607 | } else { | |
00caae6d | 608 | filename = env_get("bootfile"); |
045fa1e1 SW |
609 | if (!filename) { |
610 | puts("** No boot file defined **\n"); | |
611 | return 1; | |
612 | } | |
613 | } | |
614 | if (argc >= 6) | |
b770e88a | 615 | bytes = simple_strtoul(argv[5], NULL, 16); |
045fa1e1 SW |
616 | else |
617 | bytes = 0; | |
618 | if (argc >= 7) | |
b770e88a | 619 | pos = simple_strtoul(argv[6], NULL, 16); |
045fa1e1 SW |
620 | else |
621 | pos = 0; | |
622 | ||
da1fd96c | 623 | time = get_timer(0); |
d455d878 | 624 | ret = fs_read(filename, addr, pos, bytes, &len_read); |
da1fd96c | 625 | time = get_timer(time); |
d455d878 | 626 | if (ret < 0) |
045fa1e1 SW |
627 | return 1; |
628 | ||
d455d878 | 629 | printf("%llu bytes read in %lu ms", len_read, time); |
da1fd96c AB |
630 | if (time > 0) { |
631 | puts(" ("); | |
9e374e7b | 632 | print_size(div_u64(len_read, time) * 1000, "/s"); |
da1fd96c AB |
633 | puts(")"); |
634 | } | |
635 | puts("\n"); | |
045fa1e1 | 636 | |
018f5303 SG |
637 | env_set_hex("fileaddr", addr); |
638 | env_set_hex("filesize", len_read); | |
045fa1e1 SW |
639 | |
640 | return 0; | |
641 | } | |
642 | ||
643 | int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], | |
644 | int fstype) | |
645 | { | |
646 | if (argc < 2) | |
647 | return CMD_RET_USAGE; | |
e9b0f99e SW |
648 | if (argc > 4) |
649 | return CMD_RET_USAGE; | |
045fa1e1 SW |
650 | |
651 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) | |
652 | return 1; | |
653 | ||
e9b0f99e | 654 | if (fs_ls(argc >= 4 ? argv[3] : "/")) |
045fa1e1 SW |
655 | return 1; |
656 | ||
657 | return 0; | |
658 | } | |
a8f6ab52 | 659 | |
6152916a SW |
660 | int file_exists(const char *dev_type, const char *dev_part, const char *file, |
661 | int fstype) | |
662 | { | |
663 | if (fs_set_blk_dev(dev_type, dev_part, fstype)) | |
664 | return 0; | |
665 | ||
666 | return fs_exists(file); | |
667 | } | |
668 | ||
a8f6ab52 | 669 | int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
b770e88a | 670 | int fstype) |
a8f6ab52 SG |
671 | { |
672 | unsigned long addr; | |
673 | const char *filename; | |
d455d878 SR |
674 | loff_t bytes; |
675 | loff_t pos; | |
676 | loff_t len; | |
677 | int ret; | |
a8f6ab52 SG |
678 | unsigned long time; |
679 | ||
680 | if (argc < 6 || argc > 7) | |
681 | return CMD_RET_USAGE; | |
682 | ||
683 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) | |
684 | return 1; | |
685 | ||
d455d878 SR |
686 | addr = simple_strtoul(argv[3], NULL, 16); |
687 | filename = argv[4]; | |
b770e88a | 688 | bytes = simple_strtoul(argv[5], NULL, 16); |
a8f6ab52 | 689 | if (argc >= 7) |
b770e88a | 690 | pos = simple_strtoul(argv[6], NULL, 16); |
a8f6ab52 SG |
691 | else |
692 | pos = 0; | |
693 | ||
694 | time = get_timer(0); | |
d455d878 | 695 | ret = fs_write(filename, addr, pos, bytes, &len); |
a8f6ab52 | 696 | time = get_timer(time); |
d455d878 | 697 | if (ret < 0) |
a8f6ab52 SG |
698 | return 1; |
699 | ||
d455d878 | 700 | printf("%llu bytes written in %lu ms", len, time); |
a8f6ab52 SG |
701 | if (time > 0) { |
702 | puts(" ("); | |
9e374e7b | 703 | print_size(div_u64(len, time) * 1000, "/s"); |
a8f6ab52 SG |
704 | puts(")"); |
705 | } | |
706 | puts("\n"); | |
707 | ||
708 | return 0; | |
709 | } | |
59e890ef CG |
710 | |
711 | int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], | |
712 | int fstype) | |
713 | { | |
714 | int ret; | |
715 | char uuid[37]; | |
716 | memset(uuid, 0, sizeof(uuid)); | |
717 | ||
718 | if (argc < 3 || argc > 4) | |
719 | return CMD_RET_USAGE; | |
720 | ||
721 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) | |
722 | return 1; | |
723 | ||
724 | ret = fs_uuid(uuid); | |
725 | if (ret) | |
726 | return CMD_RET_FAILURE; | |
727 | ||
728 | if (argc == 4) | |
382bee57 | 729 | env_set(argv[3], uuid); |
59e890ef CG |
730 | else |
731 | printf("%s\n", uuid); | |
732 | ||
733 | return CMD_RET_SUCCESS; | |
734 | } | |
1a1ad8e0 SS |
735 | |
736 | int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
737 | { | |
738 | struct fstype_info *info; | |
739 | ||
740 | if (argc < 3 || argc > 4) | |
741 | return CMD_RET_USAGE; | |
742 | ||
743 | if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY)) | |
744 | return 1; | |
745 | ||
746 | info = fs_get_info(fs_type); | |
747 | ||
748 | if (argc == 4) | |
382bee57 | 749 | env_set(argv[3], info->name); |
1a1ad8e0 SS |
750 | else |
751 | printf("%s\n", info->name); | |
752 | ||
753 | return CMD_RET_SUCCESS; | |
754 | } | |
755 | ||
e2519daf AT |
756 | int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
757 | int fstype) | |
758 | { | |
759 | if (argc != 4) | |
760 | return CMD_RET_USAGE; | |
761 | ||
762 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) | |
763 | return 1; | |
764 | ||
765 | if (fs_unlink(argv[3])) | |
766 | return 1; | |
767 | ||
768 | return 0; | |
769 | } | |
770 | ||
e7074cff AT |
771 | int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
772 | int fstype) | |
773 | { | |
774 | int ret; | |
775 | ||
776 | if (argc != 4) | |
777 | return CMD_RET_USAGE; | |
778 | ||
779 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) | |
780 | return 1; | |
781 | ||
782 | ret = fs_mkdir(argv[3]); | |
783 | if (ret) { | |
784 | printf("** Unable to create a directory \"%s\" **\n", argv[3]); | |
785 | return 1; | |
786 | } | |
787 | ||
788 | return 0; | |
789 | } |