fs.c 18 KB

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