fs.c 9.1 KB

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