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