fs.c 11 KB

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