fs.c 6.8 KB

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