fs.c 14 KB

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