fs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0
  5. */
  6. #include <config.h>
  7. #include <errno.h>
  8. #include <common.h>
  9. #include <mapmem.h>
  10. #include <part.h>
  11. #include <ext4fs.h>
  12. #include <fat.h>
  13. #include <fs.h>
  14. #include <sandboxfs.h>
  15. #include <ubifs_uboot.h>
  16. #include <btrfs.h>
  17. #include <asm/io.h>
  18. #include <div64.h>
  19. #include <linux/math64.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. static struct blk_desc *fs_dev_desc;
  22. static int fs_dev_part;
  23. static disk_partition_t fs_partition;
  24. static int fs_type = FS_TYPE_ANY;
  25. static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
  26. disk_partition_t *fs_partition)
  27. {
  28. printf("** Unrecognized filesystem type **\n");
  29. return -1;
  30. }
  31. static inline int fs_ls_unsupported(const char *dirname)
  32. {
  33. return -1;
  34. }
  35. /* generic implementation of ls in terms of opendir/readdir/closedir */
  36. __maybe_unused
  37. static int fs_ls_generic(const char *dirname)
  38. {
  39. struct fs_dir_stream *dirs;
  40. struct fs_dirent *dent;
  41. int nfiles = 0, ndirs = 0;
  42. dirs = fs_opendir(dirname);
  43. if (!dirs)
  44. return -errno;
  45. while ((dent = fs_readdir(dirs))) {
  46. if (dent->type == FS_DT_DIR) {
  47. printf(" %s/\n", dent->name);
  48. ndirs++;
  49. } else {
  50. printf(" %8lld %s\n", dent->size, dent->name);
  51. nfiles++;
  52. }
  53. }
  54. fs_closedir(dirs);
  55. printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
  56. return 0;
  57. }
  58. static inline int fs_exists_unsupported(const char *filename)
  59. {
  60. return 0;
  61. }
  62. static inline int fs_size_unsupported(const char *filename, loff_t *size)
  63. {
  64. return -1;
  65. }
  66. static inline int fs_read_unsupported(const char *filename, void *buf,
  67. loff_t offset, loff_t len,
  68. loff_t *actread)
  69. {
  70. return -1;
  71. }
  72. static inline int fs_write_unsupported(const char *filename, void *buf,
  73. loff_t offset, loff_t len,
  74. loff_t *actwrite)
  75. {
  76. return -1;
  77. }
  78. static inline void fs_close_unsupported(void)
  79. {
  80. }
  81. static inline int fs_uuid_unsupported(char *uuid_str)
  82. {
  83. return -1;
  84. }
  85. static inline int fs_opendir_unsupported(const char *filename,
  86. struct fs_dir_stream **dirs)
  87. {
  88. return -EACCES;
  89. }
  90. struct fstype_info {
  91. int fstype;
  92. char *name;
  93. /*
  94. * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
  95. * should be false in most cases. For "virtual" filesystems which
  96. * aren't based on a U-Boot block device (e.g. sandbox), this can be
  97. * set to true. This should also be true for the dumm entry at the end
  98. * of fstypes[], since that is essentially a "virtual" (non-existent)
  99. * filesystem.
  100. */
  101. bool null_dev_desc_ok;
  102. int (*probe)(struct blk_desc *fs_dev_desc,
  103. disk_partition_t *fs_partition);
  104. int (*ls)(const char *dirname);
  105. int (*exists)(const char *filename);
  106. int (*size)(const char *filename, loff_t *size);
  107. int (*read)(const char *filename, void *buf, loff_t offset,
  108. loff_t len, loff_t *actread);
  109. int (*write)(const char *filename, void *buf, loff_t offset,
  110. loff_t len, loff_t *actwrite);
  111. void (*close)(void);
  112. int (*uuid)(char *uuid_str);
  113. /*
  114. * Open a directory stream. On success return 0 and directory
  115. * stream pointer via 'dirsp'. On error, return -errno. See
  116. * fs_opendir().
  117. */
  118. int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
  119. /*
  120. * Read next entry from directory stream. On success return 0
  121. * and directory entry pointer via 'dentp'. On error return
  122. * -errno. See fs_readdir().
  123. */
  124. int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
  125. /* see fs_closedir() */
  126. void (*closedir)(struct fs_dir_stream *dirs);
  127. };
  128. static struct fstype_info fstypes[] = {
  129. #ifdef CONFIG_FS_FAT
  130. {
  131. .fstype = FS_TYPE_FAT,
  132. .name = "fat",
  133. .null_dev_desc_ok = false,
  134. .probe = fat_set_blk_dev,
  135. .close = fat_close,
  136. .ls = fs_ls_generic,
  137. .exists = fat_exists,
  138. .size = fat_size,
  139. .read = fat_read_file,
  140. #ifdef CONFIG_FAT_WRITE
  141. .write = file_fat_write,
  142. #else
  143. .write = fs_write_unsupported,
  144. #endif
  145. .uuid = fs_uuid_unsupported,
  146. .opendir = fat_opendir,
  147. .readdir = fat_readdir,
  148. .closedir = fat_closedir,
  149. },
  150. #endif
  151. #ifdef CONFIG_FS_EXT4
  152. {
  153. .fstype = FS_TYPE_EXT,
  154. .name = "ext4",
  155. .null_dev_desc_ok = false,
  156. .probe = ext4fs_probe,
  157. .close = ext4fs_close,
  158. .ls = ext4fs_ls,
  159. .exists = ext4fs_exists,
  160. .size = ext4fs_size,
  161. .read = ext4_read_file,
  162. #ifdef CONFIG_CMD_EXT4_WRITE
  163. .write = ext4_write_file,
  164. #else
  165. .write = fs_write_unsupported,
  166. #endif
  167. .uuid = ext4fs_uuid,
  168. .opendir = fs_opendir_unsupported,
  169. },
  170. #endif
  171. #ifdef CONFIG_SANDBOX
  172. {
  173. .fstype = FS_TYPE_SANDBOX,
  174. .name = "sandbox",
  175. .null_dev_desc_ok = true,
  176. .probe = sandbox_fs_set_blk_dev,
  177. .close = sandbox_fs_close,
  178. .ls = sandbox_fs_ls,
  179. .exists = sandbox_fs_exists,
  180. .size = sandbox_fs_size,
  181. .read = fs_read_sandbox,
  182. .write = fs_write_sandbox,
  183. .uuid = fs_uuid_unsupported,
  184. .opendir = fs_opendir_unsupported,
  185. },
  186. #endif
  187. #ifdef CONFIG_CMD_UBIFS
  188. {
  189. .fstype = FS_TYPE_UBIFS,
  190. .name = "ubifs",
  191. .null_dev_desc_ok = true,
  192. .probe = ubifs_set_blk_dev,
  193. .close = ubifs_close,
  194. .ls = ubifs_ls,
  195. .exists = ubifs_exists,
  196. .size = ubifs_size,
  197. .read = ubifs_read,
  198. .write = fs_write_unsupported,
  199. .uuid = fs_uuid_unsupported,
  200. .opendir = fs_opendir_unsupported,
  201. },
  202. #endif
  203. #ifdef CONFIG_FS_BTRFS
  204. {
  205. .fstype = FS_TYPE_BTRFS,
  206. .name = "btrfs",
  207. .null_dev_desc_ok = false,
  208. .probe = btrfs_probe,
  209. .close = btrfs_close,
  210. .ls = btrfs_ls,
  211. .exists = btrfs_exists,
  212. .size = btrfs_size,
  213. .read = btrfs_read,
  214. .write = fs_write_unsupported,
  215. .uuid = btrfs_uuid,
  216. .opendir = fs_opendir_unsupported,
  217. },
  218. #endif
  219. {
  220. .fstype = FS_TYPE_ANY,
  221. .name = "unsupported",
  222. .null_dev_desc_ok = true,
  223. .probe = fs_probe_unsupported,
  224. .close = fs_close_unsupported,
  225. .ls = fs_ls_unsupported,
  226. .exists = fs_exists_unsupported,
  227. .size = fs_size_unsupported,
  228. .read = fs_read_unsupported,
  229. .write = fs_write_unsupported,
  230. .uuid = fs_uuid_unsupported,
  231. .opendir = fs_opendir_unsupported,
  232. },
  233. };
  234. static struct fstype_info *fs_get_info(int fstype)
  235. {
  236. struct fstype_info *info;
  237. int i;
  238. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
  239. if (fstype == info->fstype)
  240. return info;
  241. }
  242. /* Return the 'unsupported' sentinel */
  243. return info;
  244. }
  245. int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
  246. {
  247. struct fstype_info *info;
  248. int part, i;
  249. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  250. static int relocated;
  251. if (!relocated) {
  252. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
  253. i++, info++) {
  254. info->name += gd->reloc_off;
  255. info->probe += gd->reloc_off;
  256. info->close += gd->reloc_off;
  257. info->ls += gd->reloc_off;
  258. info->read += gd->reloc_off;
  259. info->write += gd->reloc_off;
  260. }
  261. relocated = 1;
  262. }
  263. #endif
  264. part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
  265. &fs_partition, 1);
  266. if (part < 0)
  267. return -1;
  268. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
  269. if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
  270. fstype != info->fstype)
  271. continue;
  272. if (!fs_dev_desc && !info->null_dev_desc_ok)
  273. continue;
  274. if (!info->probe(fs_dev_desc, &fs_partition)) {
  275. fs_type = info->fstype;
  276. fs_dev_part = part;
  277. return 0;
  278. }
  279. }
  280. return -1;
  281. }
  282. /* set current blk device w/ blk_desc + partition # */
  283. int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
  284. {
  285. struct fstype_info *info;
  286. int ret, i;
  287. if (part >= 1)
  288. ret = part_get_info(desc, part, &fs_partition);
  289. else
  290. ret = part_get_info_whole_disk(desc, &fs_partition);
  291. if (ret)
  292. return ret;
  293. fs_dev_desc = desc;
  294. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
  295. if (!info->probe(fs_dev_desc, &fs_partition)) {
  296. fs_type = info->fstype;
  297. return 0;
  298. }
  299. }
  300. return -1;
  301. }
  302. static void fs_close(void)
  303. {
  304. struct fstype_info *info = fs_get_info(fs_type);
  305. info->close();
  306. fs_type = FS_TYPE_ANY;
  307. }
  308. int fs_uuid(char *uuid_str)
  309. {
  310. struct fstype_info *info = fs_get_info(fs_type);
  311. return info->uuid(uuid_str);
  312. }
  313. int fs_ls(const char *dirname)
  314. {
  315. int ret;
  316. struct fstype_info *info = fs_get_info(fs_type);
  317. ret = info->ls(dirname);
  318. fs_type = FS_TYPE_ANY;
  319. fs_close();
  320. return ret;
  321. }
  322. int fs_exists(const char *filename)
  323. {
  324. int ret;
  325. struct fstype_info *info = fs_get_info(fs_type);
  326. ret = info->exists(filename);
  327. fs_close();
  328. return ret;
  329. }
  330. int fs_size(const char *filename, loff_t *size)
  331. {
  332. int ret;
  333. struct fstype_info *info = fs_get_info(fs_type);
  334. ret = info->size(filename, size);
  335. fs_close();
  336. return ret;
  337. }
  338. int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
  339. loff_t *actread)
  340. {
  341. struct fstype_info *info = fs_get_info(fs_type);
  342. void *buf;
  343. int ret;
  344. /*
  345. * We don't actually know how many bytes are being read, since len==0
  346. * means read the whole file.
  347. */
  348. buf = map_sysmem(addr, len);
  349. ret = info->read(filename, buf, offset, len, actread);
  350. unmap_sysmem(buf);
  351. /* If we requested a specific number of bytes, check we got it */
  352. if (ret == 0 && len && *actread != len)
  353. printf("** %s shorter than offset + len **\n", filename);
  354. fs_close();
  355. return ret;
  356. }
  357. int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
  358. loff_t *actwrite)
  359. {
  360. struct fstype_info *info = fs_get_info(fs_type);
  361. void *buf;
  362. int ret;
  363. buf = map_sysmem(addr, len);
  364. ret = info->write(filename, buf, offset, len, actwrite);
  365. unmap_sysmem(buf);
  366. if (ret < 0 && len != *actwrite) {
  367. printf("** Unable to write file %s **\n", filename);
  368. ret = -1;
  369. }
  370. fs_close();
  371. return ret;
  372. }
  373. struct fs_dir_stream *fs_opendir(const char *filename)
  374. {
  375. struct fstype_info *info = fs_get_info(fs_type);
  376. struct fs_dir_stream *dirs = NULL;
  377. int ret;
  378. ret = info->opendir(filename, &dirs);
  379. fs_close();
  380. if (ret) {
  381. errno = -ret;
  382. return NULL;
  383. }
  384. dirs->desc = fs_dev_desc;
  385. dirs->part = fs_dev_part;
  386. return dirs;
  387. }
  388. struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
  389. {
  390. struct fstype_info *info;
  391. struct fs_dirent *dirent;
  392. int ret;
  393. fs_set_blk_dev_with_part(dirs->desc, dirs->part);
  394. info = fs_get_info(fs_type);
  395. ret = info->readdir(dirs, &dirent);
  396. fs_close();
  397. if (ret) {
  398. errno = -ret;
  399. return NULL;
  400. }
  401. return dirent;
  402. }
  403. void fs_closedir(struct fs_dir_stream *dirs)
  404. {
  405. struct fstype_info *info;
  406. if (!dirs)
  407. return;
  408. fs_set_blk_dev_with_part(dirs->desc, dirs->part);
  409. info = fs_get_info(fs_type);
  410. info->closedir(dirs);
  411. fs_close();
  412. }
  413. int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  414. int fstype)
  415. {
  416. loff_t size;
  417. if (argc != 4)
  418. return CMD_RET_USAGE;
  419. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  420. return 1;
  421. if (fs_size(argv[3], &size) < 0)
  422. return CMD_RET_FAILURE;
  423. env_set_hex("filesize", size);
  424. return 0;
  425. }
  426. int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  427. int fstype)
  428. {
  429. unsigned long addr;
  430. const char *addr_str;
  431. const char *filename;
  432. loff_t bytes;
  433. loff_t pos;
  434. loff_t len_read;
  435. int ret;
  436. unsigned long time;
  437. char *ep;
  438. if (argc < 2)
  439. return CMD_RET_USAGE;
  440. if (argc > 7)
  441. return CMD_RET_USAGE;
  442. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  443. return 1;
  444. if (argc >= 4) {
  445. addr = simple_strtoul(argv[3], &ep, 16);
  446. if (ep == argv[3] || *ep != '\0')
  447. return CMD_RET_USAGE;
  448. } else {
  449. addr_str = env_get("loadaddr");
  450. if (addr_str != NULL)
  451. addr = simple_strtoul(addr_str, NULL, 16);
  452. else
  453. addr = CONFIG_SYS_LOAD_ADDR;
  454. }
  455. if (argc >= 5) {
  456. filename = argv[4];
  457. } else {
  458. filename = env_get("bootfile");
  459. if (!filename) {
  460. puts("** No boot file defined **\n");
  461. return 1;
  462. }
  463. }
  464. if (argc >= 6)
  465. bytes = simple_strtoul(argv[5], NULL, 16);
  466. else
  467. bytes = 0;
  468. if (argc >= 7)
  469. pos = simple_strtoul(argv[6], NULL, 16);
  470. else
  471. pos = 0;
  472. time = get_timer(0);
  473. ret = fs_read(filename, addr, pos, bytes, &len_read);
  474. time = get_timer(time);
  475. if (ret < 0)
  476. return 1;
  477. printf("%llu bytes read in %lu ms", len_read, time);
  478. if (time > 0) {
  479. puts(" (");
  480. print_size(div_u64(len_read, time) * 1000, "/s");
  481. puts(")");
  482. }
  483. puts("\n");
  484. env_set_hex("fileaddr", addr);
  485. env_set_hex("filesize", len_read);
  486. return 0;
  487. }
  488. int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  489. int fstype)
  490. {
  491. if (argc < 2)
  492. return CMD_RET_USAGE;
  493. if (argc > 4)
  494. return CMD_RET_USAGE;
  495. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  496. return 1;
  497. if (fs_ls(argc >= 4 ? argv[3] : "/"))
  498. return 1;
  499. return 0;
  500. }
  501. int file_exists(const char *dev_type, const char *dev_part, const char *file,
  502. int fstype)
  503. {
  504. if (fs_set_blk_dev(dev_type, dev_part, fstype))
  505. return 0;
  506. return fs_exists(file);
  507. }
  508. int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  509. int fstype)
  510. {
  511. unsigned long addr;
  512. const char *filename;
  513. loff_t bytes;
  514. loff_t pos;
  515. loff_t len;
  516. int ret;
  517. unsigned long time;
  518. if (argc < 6 || argc > 7)
  519. return CMD_RET_USAGE;
  520. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  521. return 1;
  522. addr = simple_strtoul(argv[3], NULL, 16);
  523. filename = argv[4];
  524. bytes = simple_strtoul(argv[5], NULL, 16);
  525. if (argc >= 7)
  526. pos = simple_strtoul(argv[6], NULL, 16);
  527. else
  528. pos = 0;
  529. time = get_timer(0);
  530. ret = fs_write(filename, addr, pos, bytes, &len);
  531. time = get_timer(time);
  532. if (ret < 0)
  533. return 1;
  534. printf("%llu bytes written in %lu ms", len, time);
  535. if (time > 0) {
  536. puts(" (");
  537. print_size(div_u64(len, time) * 1000, "/s");
  538. puts(")");
  539. }
  540. puts("\n");
  541. return 0;
  542. }
  543. int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  544. int fstype)
  545. {
  546. int ret;
  547. char uuid[37];
  548. memset(uuid, 0, sizeof(uuid));
  549. if (argc < 3 || argc > 4)
  550. return CMD_RET_USAGE;
  551. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  552. return 1;
  553. ret = fs_uuid(uuid);
  554. if (ret)
  555. return CMD_RET_FAILURE;
  556. if (argc == 4)
  557. env_set(argv[3], uuid);
  558. else
  559. printf("%s\n", uuid);
  560. return CMD_RET_SUCCESS;
  561. }
  562. int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  563. {
  564. struct fstype_info *info;
  565. if (argc < 3 || argc > 4)
  566. return CMD_RET_USAGE;
  567. if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
  568. return 1;
  569. info = fs_get_info(fs_type);
  570. if (argc == 4)
  571. env_set(argv[3], info->name);
  572. else
  573. printf("%s\n", info->name);
  574. return CMD_RET_SUCCESS;
  575. }