fs.c 19 KB

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