fs.c 7.3 KB

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