fs.c 8.3 KB

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