fs.c 11 KB

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