fs.c 19 KB

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