fs.c 19 KB

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