fs.c 19 KB

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