fs.c 18 KB

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