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