fs.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 void fs_close_unsupported(void)
  44. {
  45. }
  46. struct fstype_info {
  47. int fstype;
  48. int (*probe)(block_dev_desc_t *fs_dev_desc,
  49. disk_partition_t *fs_partition);
  50. int (*ls)(const char *dirname);
  51. int (*read)(const char *filename, void *buf, int offset, int len);
  52. void (*close)(void);
  53. };
  54. static struct fstype_info fstypes[] = {
  55. #ifdef CONFIG_FS_FAT
  56. {
  57. .fstype = FS_TYPE_FAT,
  58. .probe = fat_set_blk_dev,
  59. .close = fat_close,
  60. .ls = file_fat_ls,
  61. .read = fat_read_file,
  62. },
  63. #endif
  64. #ifdef CONFIG_FS_EXT4
  65. {
  66. .fstype = FS_TYPE_EXT,
  67. .probe = ext4fs_probe,
  68. .close = ext4fs_close,
  69. .ls = ext4fs_ls,
  70. .read = ext4_read_file,
  71. },
  72. #endif
  73. #ifdef CONFIG_SANDBOX
  74. {
  75. .fstype = FS_TYPE_SANDBOX,
  76. .probe = sandbox_fs_set_blk_dev,
  77. .close = sandbox_fs_close,
  78. .ls = sandbox_fs_ls,
  79. .read = fs_read_sandbox,
  80. },
  81. #endif
  82. {
  83. .fstype = FS_TYPE_ANY,
  84. .probe = fs_probe_unsupported,
  85. .close = fs_close_unsupported,
  86. .ls = fs_ls_unsupported,
  87. .read = fs_read_unsupported,
  88. },
  89. };
  90. static struct fstype_info *fs_get_info(int fstype)
  91. {
  92. struct fstype_info *info;
  93. int i;
  94. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
  95. if (fstype == info->fstype)
  96. return info;
  97. }
  98. /* Return the 'unsupported' sentinel */
  99. return info;
  100. }
  101. int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
  102. {
  103. struct fstype_info *info;
  104. int part, i;
  105. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  106. static int relocated;
  107. if (!relocated) {
  108. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
  109. i++, info++) {
  110. info->probe += gd->reloc_off;
  111. info->close += gd->reloc_off;
  112. info->ls += gd->reloc_off;
  113. info->read += gd->reloc_off;
  114. }
  115. relocated = 1;
  116. }
  117. #endif
  118. part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
  119. &fs_partition, 1);
  120. if (part < 0)
  121. return -1;
  122. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
  123. if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
  124. fstype != info->fstype)
  125. continue;
  126. if (!info->probe(fs_dev_desc, &fs_partition)) {
  127. fs_type = info->fstype;
  128. return 0;
  129. }
  130. }
  131. return -1;
  132. }
  133. static void fs_close(void)
  134. {
  135. struct fstype_info *info = fs_get_info(fs_type);
  136. info->close();
  137. fs_type = FS_TYPE_ANY;
  138. }
  139. int fs_ls(const char *dirname)
  140. {
  141. int ret;
  142. struct fstype_info *info = fs_get_info(fs_type);
  143. ret = info->ls(dirname);
  144. fs_type = FS_TYPE_ANY;
  145. fs_close();
  146. return ret;
  147. }
  148. int fs_read(const char *filename, ulong addr, int offset, int len)
  149. {
  150. struct fstype_info *info = fs_get_info(fs_type);
  151. void *buf;
  152. int ret;
  153. /*
  154. * We don't actually know how many bytes are being read, since len==0
  155. * means read the whole file.
  156. */
  157. buf = map_sysmem(addr, len);
  158. ret = info->read(filename, buf, offset, len);
  159. unmap_sysmem(buf);
  160. /* If we requested a specific number of bytes, check we got it */
  161. if (ret >= 0 && len && ret != len) {
  162. printf("** Unable to read file %s **\n", filename);
  163. ret = -1;
  164. }
  165. fs_close();
  166. return ret;
  167. }
  168. int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  169. int fstype, int cmdline_base)
  170. {
  171. unsigned long addr;
  172. const char *addr_str;
  173. const char *filename;
  174. unsigned long bytes;
  175. unsigned long pos;
  176. int len_read;
  177. unsigned long time;
  178. if (argc < 2)
  179. return CMD_RET_USAGE;
  180. if (argc > 7)
  181. return CMD_RET_USAGE;
  182. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  183. return 1;
  184. if (argc >= 4) {
  185. addr = simple_strtoul(argv[3], NULL, cmdline_base);
  186. } else {
  187. addr_str = getenv("loadaddr");
  188. if (addr_str != NULL)
  189. addr = simple_strtoul(addr_str, NULL, 16);
  190. else
  191. addr = CONFIG_SYS_LOAD_ADDR;
  192. }
  193. if (argc >= 5) {
  194. filename = argv[4];
  195. } else {
  196. filename = getenv("bootfile");
  197. if (!filename) {
  198. puts("** No boot file defined **\n");
  199. return 1;
  200. }
  201. }
  202. if (argc >= 6)
  203. bytes = simple_strtoul(argv[5], NULL, cmdline_base);
  204. else
  205. bytes = 0;
  206. if (argc >= 7)
  207. pos = simple_strtoul(argv[6], NULL, cmdline_base);
  208. else
  209. pos = 0;
  210. time = get_timer(0);
  211. len_read = fs_read(filename, addr, pos, bytes);
  212. time = get_timer(time);
  213. if (len_read <= 0)
  214. return 1;
  215. printf("%d bytes read in %lu ms", len_read, time);
  216. if (time > 0) {
  217. puts(" (");
  218. print_size(len_read / time * 1000, "/s");
  219. puts(")");
  220. }
  221. puts("\n");
  222. setenv_hex("filesize", len_read);
  223. return 0;
  224. }
  225. int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  226. int fstype)
  227. {
  228. if (argc < 2)
  229. return CMD_RET_USAGE;
  230. if (argc > 4)
  231. return CMD_RET_USAGE;
  232. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  233. return 1;
  234. if (fs_ls(argc >= 4 ? argv[3] : "/"))
  235. return 1;
  236. return 0;
  237. }