fs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. /**
  245. * fs_get_type_name() - Get type of current filesystem
  246. *
  247. * Return: Pointer to filesystem name
  248. *
  249. * Returns a string describing the current filesystem, or the sentinel
  250. * "unsupported" for any unrecognised filesystem.
  251. */
  252. const char *fs_get_type_name(void)
  253. {
  254. return fs_get_info(fs_type)->name;
  255. }
  256. int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
  257. {
  258. struct fstype_info *info;
  259. int part, i;
  260. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  261. static int relocated;
  262. if (!relocated) {
  263. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
  264. i++, info++) {
  265. info->name += gd->reloc_off;
  266. info->probe += gd->reloc_off;
  267. info->close += gd->reloc_off;
  268. info->ls += gd->reloc_off;
  269. info->read += gd->reloc_off;
  270. info->write += gd->reloc_off;
  271. }
  272. relocated = 1;
  273. }
  274. #endif
  275. part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
  276. &fs_partition, 1);
  277. if (part < 0)
  278. return -1;
  279. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
  280. if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
  281. fstype != info->fstype)
  282. continue;
  283. if (!fs_dev_desc && !info->null_dev_desc_ok)
  284. continue;
  285. if (!info->probe(fs_dev_desc, &fs_partition)) {
  286. fs_type = info->fstype;
  287. fs_dev_part = part;
  288. return 0;
  289. }
  290. }
  291. return -1;
  292. }
  293. /* set current blk device w/ blk_desc + partition # */
  294. int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
  295. {
  296. struct fstype_info *info;
  297. int ret, i;
  298. if (part >= 1)
  299. ret = part_get_info(desc, part, &fs_partition);
  300. else
  301. ret = part_get_info_whole_disk(desc, &fs_partition);
  302. if (ret)
  303. return ret;
  304. fs_dev_desc = desc;
  305. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
  306. if (!info->probe(fs_dev_desc, &fs_partition)) {
  307. fs_type = info->fstype;
  308. return 0;
  309. }
  310. }
  311. return -1;
  312. }
  313. static void fs_close(void)
  314. {
  315. struct fstype_info *info = fs_get_info(fs_type);
  316. info->close();
  317. fs_type = FS_TYPE_ANY;
  318. }
  319. int fs_uuid(char *uuid_str)
  320. {
  321. struct fstype_info *info = fs_get_info(fs_type);
  322. return info->uuid(uuid_str);
  323. }
  324. int fs_ls(const char *dirname)
  325. {
  326. int ret;
  327. struct fstype_info *info = fs_get_info(fs_type);
  328. ret = info->ls(dirname);
  329. fs_type = FS_TYPE_ANY;
  330. fs_close();
  331. return ret;
  332. }
  333. int fs_exists(const char *filename)
  334. {
  335. int ret;
  336. struct fstype_info *info = fs_get_info(fs_type);
  337. ret = info->exists(filename);
  338. fs_close();
  339. return ret;
  340. }
  341. int fs_size(const char *filename, loff_t *size)
  342. {
  343. int ret;
  344. struct fstype_info *info = fs_get_info(fs_type);
  345. ret = info->size(filename, size);
  346. fs_close();
  347. return ret;
  348. }
  349. int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
  350. loff_t *actread)
  351. {
  352. struct fstype_info *info = fs_get_info(fs_type);
  353. void *buf;
  354. int ret;
  355. /*
  356. * We don't actually know how many bytes are being read, since len==0
  357. * means read the whole file.
  358. */
  359. buf = map_sysmem(addr, len);
  360. ret = info->read(filename, buf, offset, len, actread);
  361. unmap_sysmem(buf);
  362. /* If we requested a specific number of bytes, check we got it */
  363. if (ret == 0 && len && *actread != len)
  364. debug("** %s shorter than offset + len **\n", filename);
  365. fs_close();
  366. return ret;
  367. }
  368. int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
  369. loff_t *actwrite)
  370. {
  371. struct fstype_info *info = fs_get_info(fs_type);
  372. void *buf;
  373. int ret;
  374. buf = map_sysmem(addr, len);
  375. ret = info->write(filename, buf, offset, len, actwrite);
  376. unmap_sysmem(buf);
  377. if (ret < 0 && len != *actwrite) {
  378. printf("** Unable to write file %s **\n", filename);
  379. ret = -1;
  380. }
  381. fs_close();
  382. return ret;
  383. }
  384. struct fs_dir_stream *fs_opendir(const char *filename)
  385. {
  386. struct fstype_info *info = fs_get_info(fs_type);
  387. struct fs_dir_stream *dirs = NULL;
  388. int ret;
  389. ret = info->opendir(filename, &dirs);
  390. fs_close();
  391. if (ret) {
  392. errno = -ret;
  393. return NULL;
  394. }
  395. dirs->desc = fs_dev_desc;
  396. dirs->part = fs_dev_part;
  397. return dirs;
  398. }
  399. struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
  400. {
  401. struct fstype_info *info;
  402. struct fs_dirent *dirent;
  403. int ret;
  404. fs_set_blk_dev_with_part(dirs->desc, dirs->part);
  405. info = fs_get_info(fs_type);
  406. ret = info->readdir(dirs, &dirent);
  407. fs_close();
  408. if (ret) {
  409. errno = -ret;
  410. return NULL;
  411. }
  412. return dirent;
  413. }
  414. void fs_closedir(struct fs_dir_stream *dirs)
  415. {
  416. struct fstype_info *info;
  417. if (!dirs)
  418. return;
  419. fs_set_blk_dev_with_part(dirs->desc, dirs->part);
  420. info = fs_get_info(fs_type);
  421. info->closedir(dirs);
  422. fs_close();
  423. }
  424. int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  425. int fstype)
  426. {
  427. loff_t size;
  428. if (argc != 4)
  429. return CMD_RET_USAGE;
  430. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  431. return 1;
  432. if (fs_size(argv[3], &size) < 0)
  433. return CMD_RET_FAILURE;
  434. env_set_hex("filesize", size);
  435. return 0;
  436. }
  437. int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  438. int fstype)
  439. {
  440. unsigned long addr;
  441. const char *addr_str;
  442. const char *filename;
  443. loff_t bytes;
  444. loff_t pos;
  445. loff_t len_read;
  446. int ret;
  447. unsigned long time;
  448. char *ep;
  449. if (argc < 2)
  450. return CMD_RET_USAGE;
  451. if (argc > 7)
  452. return CMD_RET_USAGE;
  453. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  454. return 1;
  455. if (argc >= 4) {
  456. addr = simple_strtoul(argv[3], &ep, 16);
  457. if (ep == argv[3] || *ep != '\0')
  458. return CMD_RET_USAGE;
  459. } else {
  460. addr_str = env_get("loadaddr");
  461. if (addr_str != NULL)
  462. addr = simple_strtoul(addr_str, NULL, 16);
  463. else
  464. addr = CONFIG_SYS_LOAD_ADDR;
  465. }
  466. if (argc >= 5) {
  467. filename = argv[4];
  468. } else {
  469. filename = env_get("bootfile");
  470. if (!filename) {
  471. puts("** No boot file defined **\n");
  472. return 1;
  473. }
  474. }
  475. if (argc >= 6)
  476. bytes = simple_strtoul(argv[5], NULL, 16);
  477. else
  478. bytes = 0;
  479. if (argc >= 7)
  480. pos = simple_strtoul(argv[6], NULL, 16);
  481. else
  482. pos = 0;
  483. time = get_timer(0);
  484. ret = fs_read(filename, addr, pos, bytes, &len_read);
  485. time = get_timer(time);
  486. if (ret < 0)
  487. return 1;
  488. printf("%llu bytes read in %lu ms", len_read, time);
  489. if (time > 0) {
  490. puts(" (");
  491. print_size(div_u64(len_read, time) * 1000, "/s");
  492. puts(")");
  493. }
  494. puts("\n");
  495. env_set_hex("fileaddr", addr);
  496. env_set_hex("filesize", len_read);
  497. return 0;
  498. }
  499. int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  500. int fstype)
  501. {
  502. if (argc < 2)
  503. return CMD_RET_USAGE;
  504. if (argc > 4)
  505. return CMD_RET_USAGE;
  506. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  507. return 1;
  508. if (fs_ls(argc >= 4 ? argv[3] : "/"))
  509. return 1;
  510. return 0;
  511. }
  512. int file_exists(const char *dev_type, const char *dev_part, const char *file,
  513. int fstype)
  514. {
  515. if (fs_set_blk_dev(dev_type, dev_part, fstype))
  516. return 0;
  517. return fs_exists(file);
  518. }
  519. int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  520. int fstype)
  521. {
  522. unsigned long addr;
  523. const char *filename;
  524. loff_t bytes;
  525. loff_t pos;
  526. loff_t len;
  527. int ret;
  528. unsigned long time;
  529. if (argc < 6 || argc > 7)
  530. return CMD_RET_USAGE;
  531. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  532. return 1;
  533. addr = simple_strtoul(argv[3], NULL, 16);
  534. filename = argv[4];
  535. bytes = simple_strtoul(argv[5], NULL, 16);
  536. if (argc >= 7)
  537. pos = simple_strtoul(argv[6], NULL, 16);
  538. else
  539. pos = 0;
  540. time = get_timer(0);
  541. ret = fs_write(filename, addr, pos, bytes, &len);
  542. time = get_timer(time);
  543. if (ret < 0)
  544. return 1;
  545. printf("%llu bytes written in %lu ms", len, time);
  546. if (time > 0) {
  547. puts(" (");
  548. print_size(div_u64(len, time) * 1000, "/s");
  549. puts(")");
  550. }
  551. puts("\n");
  552. return 0;
  553. }
  554. int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  555. int fstype)
  556. {
  557. int ret;
  558. char uuid[37];
  559. memset(uuid, 0, sizeof(uuid));
  560. if (argc < 3 || argc > 4)
  561. return CMD_RET_USAGE;
  562. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  563. return 1;
  564. ret = fs_uuid(uuid);
  565. if (ret)
  566. return CMD_RET_FAILURE;
  567. if (argc == 4)
  568. env_set(argv[3], uuid);
  569. else
  570. printf("%s\n", uuid);
  571. return CMD_RET_SUCCESS;
  572. }
  573. int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  574. {
  575. struct fstype_info *info;
  576. if (argc < 3 || argc > 4)
  577. return CMD_RET_USAGE;
  578. if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
  579. return 1;
  580. info = fs_get_info(fs_type);
  581. if (argc == 4)
  582. env_set(argv[3], info->name);
  583. else
  584. printf("%s\n", info->name);
  585. return CMD_RET_SUCCESS;
  586. }