fs.c 6.1 KB

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