fs.c 13 KB

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