fs.c 7.4 KB

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