fs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. .null_dev_desc_ok = false,
  266. .probe = sqfs_probe,
  267. .opendir = sqfs_opendir,
  268. .readdir = sqfs_readdir,
  269. .ls = fs_ls_generic,
  270. .read = sqfs_read,
  271. .size = sqfs_size,
  272. .close = sqfs_close,
  273. .closedir = sqfs_closedir,
  274. .exists = sqfs_exists,
  275. .uuid = fs_uuid_unsupported,
  276. .write = fs_write_unsupported,
  277. .ln = fs_ln_unsupported,
  278. .unlink = fs_unlink_unsupported,
  279. .mkdir = fs_mkdir_unsupported,
  280. },
  281. #endif
  282. {
  283. .fstype = FS_TYPE_ANY,
  284. .name = "unsupported",
  285. .null_dev_desc_ok = true,
  286. .probe = fs_probe_unsupported,
  287. .close = fs_close_unsupported,
  288. .ls = fs_ls_unsupported,
  289. .exists = fs_exists_unsupported,
  290. .size = fs_size_unsupported,
  291. .read = fs_read_unsupported,
  292. .write = fs_write_unsupported,
  293. .uuid = fs_uuid_unsupported,
  294. .opendir = fs_opendir_unsupported,
  295. .unlink = fs_unlink_unsupported,
  296. .mkdir = fs_mkdir_unsupported,
  297. .ln = fs_ln_unsupported,
  298. },
  299. };
  300. static struct fstype_info *fs_get_info(int fstype)
  301. {
  302. struct fstype_info *info;
  303. int i;
  304. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
  305. if (fstype == info->fstype)
  306. return info;
  307. }
  308. /* Return the 'unsupported' sentinel */
  309. return info;
  310. }
  311. /**
  312. * fs_get_type() - Get type of current filesystem
  313. *
  314. * Return: filesystem type
  315. *
  316. * Returns filesystem type representing the current filesystem, or
  317. * FS_TYPE_ANY for any unrecognised filesystem.
  318. */
  319. int fs_get_type(void)
  320. {
  321. return fs_type;
  322. }
  323. /**
  324. * fs_get_type_name() - Get type of current filesystem
  325. *
  326. * Return: Pointer to filesystem name
  327. *
  328. * Returns a string describing the current filesystem, or the sentinel
  329. * "unsupported" for any unrecognised filesystem.
  330. */
  331. const char *fs_get_type_name(void)
  332. {
  333. return fs_get_info(fs_type)->name;
  334. }
  335. int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
  336. {
  337. struct fstype_info *info;
  338. int part, i;
  339. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  340. static int relocated;
  341. if (!relocated) {
  342. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
  343. i++, info++) {
  344. info->name += gd->reloc_off;
  345. info->probe += gd->reloc_off;
  346. info->close += gd->reloc_off;
  347. info->ls += gd->reloc_off;
  348. info->read += gd->reloc_off;
  349. info->write += gd->reloc_off;
  350. }
  351. relocated = 1;
  352. }
  353. #endif
  354. part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
  355. &fs_partition, 1);
  356. if (part < 0)
  357. return -1;
  358. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
  359. if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
  360. fstype != info->fstype)
  361. continue;
  362. if (!fs_dev_desc && !info->null_dev_desc_ok)
  363. continue;
  364. if (!info->probe(fs_dev_desc, &fs_partition)) {
  365. fs_type = info->fstype;
  366. fs_dev_part = part;
  367. return 0;
  368. }
  369. }
  370. return -1;
  371. }
  372. /* set current blk device w/ blk_desc + partition # */
  373. int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
  374. {
  375. struct fstype_info *info;
  376. int ret, i;
  377. if (part >= 1)
  378. ret = part_get_info(desc, part, &fs_partition);
  379. else
  380. ret = part_get_info_whole_disk(desc, &fs_partition);
  381. if (ret)
  382. return ret;
  383. fs_dev_desc = desc;
  384. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
  385. if (!info->probe(fs_dev_desc, &fs_partition)) {
  386. fs_type = info->fstype;
  387. fs_dev_part = part;
  388. return 0;
  389. }
  390. }
  391. return -1;
  392. }
  393. void fs_close(void)
  394. {
  395. struct fstype_info *info = fs_get_info(fs_type);
  396. info->close();
  397. fs_type = FS_TYPE_ANY;
  398. }
  399. int fs_uuid(char *uuid_str)
  400. {
  401. struct fstype_info *info = fs_get_info(fs_type);
  402. return info->uuid(uuid_str);
  403. }
  404. int fs_ls(const char *dirname)
  405. {
  406. int ret;
  407. struct fstype_info *info = fs_get_info(fs_type);
  408. ret = info->ls(dirname);
  409. fs_close();
  410. return ret;
  411. }
  412. int fs_exists(const char *filename)
  413. {
  414. int ret;
  415. struct fstype_info *info = fs_get_info(fs_type);
  416. ret = info->exists(filename);
  417. fs_close();
  418. return ret;
  419. }
  420. int fs_size(const char *filename, loff_t *size)
  421. {
  422. int ret;
  423. struct fstype_info *info = fs_get_info(fs_type);
  424. ret = info->size(filename, size);
  425. fs_close();
  426. return ret;
  427. }
  428. #ifdef CONFIG_LMB
  429. /* Check if a file may be read to the given address */
  430. static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
  431. loff_t len, struct fstype_info *info)
  432. {
  433. struct lmb lmb;
  434. int ret;
  435. loff_t size;
  436. loff_t read_len;
  437. /* get the actual size of the file */
  438. ret = info->size(filename, &size);
  439. if (ret)
  440. return ret;
  441. if (offset >= size) {
  442. /* offset >= EOF, no bytes will be written */
  443. return 0;
  444. }
  445. read_len = size - offset;
  446. /* limit to 'len' if it is smaller */
  447. if (len && len < read_len)
  448. read_len = len;
  449. lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
  450. lmb_dump_all(&lmb);
  451. if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
  452. return 0;
  453. log_err("** Reading file would overwrite reserved memory **\n");
  454. return -ENOSPC;
  455. }
  456. #endif
  457. static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
  458. int do_lmb_check, loff_t *actread)
  459. {
  460. struct fstype_info *info = fs_get_info(fs_type);
  461. void *buf;
  462. int ret;
  463. #ifdef CONFIG_LMB
  464. if (do_lmb_check) {
  465. ret = fs_read_lmb_check(filename, addr, offset, len, info);
  466. if (ret)
  467. return ret;
  468. }
  469. #endif
  470. /*
  471. * We don't actually know how many bytes are being read, since len==0
  472. * means read the whole file.
  473. */
  474. buf = map_sysmem(addr, len);
  475. ret = info->read(filename, buf, offset, len, actread);
  476. unmap_sysmem(buf);
  477. /* If we requested a specific number of bytes, check we got it */
  478. if (ret == 0 && len && *actread != len)
  479. log_debug("** %s shorter than offset + len **\n", filename);
  480. fs_close();
  481. return ret;
  482. }
  483. int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
  484. loff_t *actread)
  485. {
  486. return _fs_read(filename, addr, offset, len, 0, actread);
  487. }
  488. int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
  489. loff_t *actwrite)
  490. {
  491. struct fstype_info *info = fs_get_info(fs_type);
  492. void *buf;
  493. int ret;
  494. buf = map_sysmem(addr, len);
  495. ret = info->write(filename, buf, offset, len, actwrite);
  496. unmap_sysmem(buf);
  497. if (ret < 0 && len != *actwrite) {
  498. log_err("** Unable to write file %s **\n", filename);
  499. ret = -1;
  500. }
  501. fs_close();
  502. return ret;
  503. }
  504. struct fs_dir_stream *fs_opendir(const char *filename)
  505. {
  506. struct fstype_info *info = fs_get_info(fs_type);
  507. struct fs_dir_stream *dirs = NULL;
  508. int ret;
  509. ret = info->opendir(filename, &dirs);
  510. fs_close();
  511. if (ret) {
  512. errno = -ret;
  513. return NULL;
  514. }
  515. dirs->desc = fs_dev_desc;
  516. dirs->part = fs_dev_part;
  517. return dirs;
  518. }
  519. struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
  520. {
  521. struct fstype_info *info;
  522. struct fs_dirent *dirent;
  523. int ret;
  524. fs_set_blk_dev_with_part(dirs->desc, dirs->part);
  525. info = fs_get_info(fs_type);
  526. ret = info->readdir(dirs, &dirent);
  527. fs_close();
  528. if (ret) {
  529. errno = -ret;
  530. return NULL;
  531. }
  532. return dirent;
  533. }
  534. void fs_closedir(struct fs_dir_stream *dirs)
  535. {
  536. struct fstype_info *info;
  537. if (!dirs)
  538. return;
  539. fs_set_blk_dev_with_part(dirs->desc, dirs->part);
  540. info = fs_get_info(fs_type);
  541. info->closedir(dirs);
  542. fs_close();
  543. }
  544. int fs_unlink(const char *filename)
  545. {
  546. int ret;
  547. struct fstype_info *info = fs_get_info(fs_type);
  548. ret = info->unlink(filename);
  549. fs_close();
  550. return ret;
  551. }
  552. int fs_mkdir(const char *dirname)
  553. {
  554. int ret;
  555. struct fstype_info *info = fs_get_info(fs_type);
  556. ret = info->mkdir(dirname);
  557. fs_close();
  558. return ret;
  559. }
  560. int fs_ln(const char *fname, const char *target)
  561. {
  562. struct fstype_info *info = fs_get_info(fs_type);
  563. int ret;
  564. ret = info->ln(fname, target);
  565. if (ret < 0) {
  566. log_err("** Unable to create link %s -> %s **\n", fname, target);
  567. ret = -1;
  568. }
  569. fs_close();
  570. return ret;
  571. }
  572. int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  573. int fstype)
  574. {
  575. loff_t size;
  576. if (argc != 4)
  577. return CMD_RET_USAGE;
  578. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  579. return 1;
  580. if (fs_size(argv[3], &size) < 0)
  581. return CMD_RET_FAILURE;
  582. env_set_hex("filesize", size);
  583. return 0;
  584. }
  585. int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  586. int fstype)
  587. {
  588. unsigned long addr;
  589. const char *addr_str;
  590. const char *filename;
  591. loff_t bytes;
  592. loff_t pos;
  593. loff_t len_read;
  594. int ret;
  595. unsigned long time;
  596. char *ep;
  597. if (argc < 2)
  598. return CMD_RET_USAGE;
  599. if (argc > 7)
  600. return CMD_RET_USAGE;
  601. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  602. return 1;
  603. if (argc >= 4) {
  604. addr = simple_strtoul(argv[3], &ep, 16);
  605. if (ep == argv[3] || *ep != '\0')
  606. return CMD_RET_USAGE;
  607. } else {
  608. addr_str = env_get("loadaddr");
  609. if (addr_str != NULL)
  610. addr = simple_strtoul(addr_str, NULL, 16);
  611. else
  612. addr = CONFIG_SYS_LOAD_ADDR;
  613. }
  614. if (argc >= 5) {
  615. filename = argv[4];
  616. } else {
  617. filename = env_get("bootfile");
  618. if (!filename) {
  619. puts("** No boot file defined **\n");
  620. return 1;
  621. }
  622. }
  623. if (argc >= 6)
  624. bytes = simple_strtoul(argv[5], NULL, 16);
  625. else
  626. bytes = 0;
  627. if (argc >= 7)
  628. pos = simple_strtoul(argv[6], NULL, 16);
  629. else
  630. pos = 0;
  631. time = get_timer(0);
  632. ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
  633. time = get_timer(time);
  634. if (ret < 0) {
  635. log_err("Failed to load '%s'\n", filename);
  636. return 1;
  637. }
  638. if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
  639. efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
  640. (argc > 4) ? argv[4] : "");
  641. printf("%llu bytes read in %lu ms", len_read, time);
  642. if (time > 0) {
  643. puts(" (");
  644. print_size(div_u64(len_read, time) * 1000, "/s");
  645. puts(")");
  646. }
  647. puts("\n");
  648. env_set_hex("fileaddr", addr);
  649. env_set_hex("filesize", len_read);
  650. return 0;
  651. }
  652. int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  653. int fstype)
  654. {
  655. if (argc < 2)
  656. return CMD_RET_USAGE;
  657. if (argc > 4)
  658. return CMD_RET_USAGE;
  659. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  660. return 1;
  661. if (fs_ls(argc >= 4 ? argv[3] : "/"))
  662. return 1;
  663. return 0;
  664. }
  665. int file_exists(const char *dev_type, const char *dev_part, const char *file,
  666. int fstype)
  667. {
  668. if (fs_set_blk_dev(dev_type, dev_part, fstype))
  669. return 0;
  670. return fs_exists(file);
  671. }
  672. int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  673. int fstype)
  674. {
  675. unsigned long addr;
  676. const char *filename;
  677. loff_t bytes;
  678. loff_t pos;
  679. loff_t len;
  680. int ret;
  681. unsigned long time;
  682. if (argc < 6 || argc > 7)
  683. return CMD_RET_USAGE;
  684. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  685. return 1;
  686. addr = simple_strtoul(argv[3], NULL, 16);
  687. filename = argv[4];
  688. bytes = simple_strtoul(argv[5], NULL, 16);
  689. if (argc >= 7)
  690. pos = simple_strtoul(argv[6], NULL, 16);
  691. else
  692. pos = 0;
  693. time = get_timer(0);
  694. ret = fs_write(filename, addr, pos, bytes, &len);
  695. time = get_timer(time);
  696. if (ret < 0)
  697. return 1;
  698. printf("%llu bytes written in %lu ms", len, time);
  699. if (time > 0) {
  700. puts(" (");
  701. print_size(div_u64(len, time) * 1000, "/s");
  702. puts(")");
  703. }
  704. puts("\n");
  705. return 0;
  706. }
  707. int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  708. int fstype)
  709. {
  710. int ret;
  711. char uuid[37];
  712. memset(uuid, 0, sizeof(uuid));
  713. if (argc < 3 || argc > 4)
  714. return CMD_RET_USAGE;
  715. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  716. return 1;
  717. ret = fs_uuid(uuid);
  718. if (ret)
  719. return CMD_RET_FAILURE;
  720. if (argc == 4)
  721. env_set(argv[3], uuid);
  722. else
  723. printf("%s\n", uuid);
  724. return CMD_RET_SUCCESS;
  725. }
  726. int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  727. {
  728. struct fstype_info *info;
  729. if (argc < 3 || argc > 4)
  730. return CMD_RET_USAGE;
  731. if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
  732. return 1;
  733. info = fs_get_info(fs_type);
  734. if (argc == 4)
  735. env_set(argv[3], info->name);
  736. else
  737. printf("%s\n", info->name);
  738. fs_close();
  739. return CMD_RET_SUCCESS;
  740. }
  741. int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  742. int fstype)
  743. {
  744. if (argc != 4)
  745. return CMD_RET_USAGE;
  746. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  747. return 1;
  748. if (fs_unlink(argv[3]))
  749. return 1;
  750. return 0;
  751. }
  752. int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  753. int fstype)
  754. {
  755. int ret;
  756. if (argc != 4)
  757. return CMD_RET_USAGE;
  758. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  759. return 1;
  760. ret = fs_mkdir(argv[3]);
  761. if (ret) {
  762. log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
  763. return 1;
  764. }
  765. return 0;
  766. }
  767. int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  768. int fstype)
  769. {
  770. if (argc != 5)
  771. return CMD_RET_USAGE;
  772. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  773. return 1;
  774. if (fs_ln(argv[3], argv[4]))
  775. return 1;
  776. return 0;
  777. }
  778. int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
  779. {
  780. struct fstype_info *drv = fstypes;
  781. const int n_ents = ARRAY_SIZE(fstypes);
  782. struct fstype_info *entry;
  783. int i = 0;
  784. puts("Supported filesystems");
  785. for (entry = drv; entry != drv + n_ents; entry++) {
  786. if (entry->fstype != FS_TYPE_ANY) {
  787. printf("%c %s", i ? ',' : ':', entry->name);
  788. i++;
  789. }
  790. }
  791. if (!i)
  792. puts(": <none>");
  793. puts("\n");
  794. return CMD_RET_SUCCESS;
  795. }