fs.c 7.4 KB

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