fs.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. DECLARE_GLOBAL_DATA_PTR;
  23. static block_dev_desc_t *fs_dev_desc;
  24. static disk_partition_t fs_partition;
  25. static int fs_type = FS_TYPE_ANY;
  26. static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
  27. disk_partition_t *fs_partition)
  28. {
  29. printf("** Unrecognized filesystem type **\n");
  30. return -1;
  31. }
  32. static inline int fs_ls_unsupported(const char *dirname)
  33. {
  34. return -1;
  35. }
  36. static inline int fs_read_unsupported(const char *filename, ulong addr,
  37. int offset, int len)
  38. {
  39. return -1;
  40. }
  41. static inline void fs_close_unsupported(void)
  42. {
  43. }
  44. #ifdef CONFIG_FS_FAT
  45. static int fs_probe_fat(block_dev_desc_t *fs_dev_desc,
  46. disk_partition_t *fs_partition)
  47. {
  48. return fat_set_blk_dev(fs_dev_desc, fs_partition);
  49. }
  50. static void fs_close_fat(void)
  51. {
  52. }
  53. #define fs_ls_fat file_fat_ls
  54. static int fs_read_fat(const char *filename, ulong addr, int offset, int len)
  55. {
  56. int len_read;
  57. len_read = file_fat_read_at(filename, offset,
  58. (unsigned char *)addr, 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, ulong addr, 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((char *)addr, 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, ulong addr, 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. int ret;
  223. ret = info->read(filename, addr, offset, len);
  224. /* If we requested a specific number of bytes, check we got it */
  225. if (ret >= 0 && len && ret != len) {
  226. printf("** Unable to read file %s **\n", filename);
  227. ret = -1;
  228. }
  229. fs_close();
  230. return ret;
  231. }
  232. int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  233. int fstype, int cmdline_base)
  234. {
  235. unsigned long addr;
  236. const char *addr_str;
  237. const char *filename;
  238. unsigned long bytes;
  239. unsigned long pos;
  240. int len_read;
  241. unsigned long time;
  242. if (argc < 2)
  243. return CMD_RET_USAGE;
  244. if (argc > 7)
  245. return CMD_RET_USAGE;
  246. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  247. return 1;
  248. if (argc >= 4) {
  249. addr = simple_strtoul(argv[3], NULL, cmdline_base);
  250. } else {
  251. addr_str = getenv("loadaddr");
  252. if (addr_str != NULL)
  253. addr = simple_strtoul(addr_str, NULL, 16);
  254. else
  255. addr = CONFIG_SYS_LOAD_ADDR;
  256. }
  257. if (argc >= 5) {
  258. filename = argv[4];
  259. } else {
  260. filename = getenv("bootfile");
  261. if (!filename) {
  262. puts("** No boot file defined **\n");
  263. return 1;
  264. }
  265. }
  266. if (argc >= 6)
  267. bytes = simple_strtoul(argv[5], NULL, cmdline_base);
  268. else
  269. bytes = 0;
  270. if (argc >= 7)
  271. pos = simple_strtoul(argv[6], NULL, cmdline_base);
  272. else
  273. pos = 0;
  274. time = get_timer(0);
  275. len_read = fs_read(filename, addr, pos, bytes);
  276. time = get_timer(time);
  277. if (len_read <= 0)
  278. return 1;
  279. printf("%d bytes read in %lu ms", len_read, time);
  280. if (time > 0) {
  281. puts(" (");
  282. print_size(len_read / time * 1000, "/s");
  283. puts(")");
  284. }
  285. puts("\n");
  286. setenv_hex("filesize", len_read);
  287. return 0;
  288. }
  289. int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  290. int fstype)
  291. {
  292. if (argc < 2)
  293. return CMD_RET_USAGE;
  294. if (argc > 4)
  295. return CMD_RET_USAGE;
  296. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  297. return 1;
  298. if (fs_ls(argc >= 4 ? argv[3] : "/"))
  299. return 1;
  300. return 0;
  301. }