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