fs.c 18 KB

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