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