fs.c 19 KB

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