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