cmd_fat.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * (C) Copyright 2002
  3. * Richard Jones, rjones@nexus-tech.net
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Boot support
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <s_record.h>
  29. #include <net.h>
  30. #include <ata.h>
  31. #include <part.h>
  32. #if (CONFIG_COMMANDS & CFG_CMD_FAT)
  33. #undef DEBUG
  34. #include <fat.h>
  35. int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  36. {
  37. long size;
  38. unsigned long offset;
  39. unsigned long count;
  40. char buf [12];
  41. block_dev_desc_t *dev_desc=NULL;
  42. int dev=0;
  43. int part=1;
  44. char *ep;
  45. if (argc < 5) {
  46. printf ("usage: fatload <interface> <dev[:part]> <addr> <filename> [bytes]\n");
  47. return 1;
  48. }
  49. dev = (int)simple_strtoul (argv[2], &ep, 16);
  50. dev_desc=get_dev(argv[1],dev);
  51. if (dev_desc==NULL) {
  52. puts ("\n** Invalid boot device **\n");
  53. return 1;
  54. }
  55. if (*ep) {
  56. if (*ep != ':') {
  57. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  58. return 1;
  59. }
  60. part = (int)simple_strtoul(++ep, NULL, 16);
  61. }
  62. if (fat_register_device(dev_desc,part)!=0) {
  63. printf ("\n** Unable to use %s %d:%d for fatload **\n",argv[1],dev,part);
  64. return 1;
  65. }
  66. offset = simple_strtoul (argv[3], NULL, 16);
  67. if (argc == 6)
  68. count = simple_strtoul (argv[5], NULL, 16);
  69. else
  70. count = 0;
  71. size = file_fat_read (argv[4], (unsigned char *) offset, count);
  72. if(size==-1) {
  73. printf("\n** Unable to read \"%s\" from %s %d:%d **\n",argv[4],argv[1],dev,part);
  74. return 1;
  75. }
  76. printf ("\n%ld bytes read\n", size);
  77. sprintf(buf, "%lX", size);
  78. setenv("filesize", buf);
  79. return 0;
  80. }
  81. U_BOOT_CMD(
  82. fatload, 6, 0, do_fat_fsload,
  83. "fatload - load binary file from a dos filesystem\n",
  84. "<interface> <dev[:part]> <addr> <filename> [bytes]\n"
  85. " - load binary file 'filename' from 'dev' on 'interface'\n"
  86. " to address 'addr' from dos filesystem\n"
  87. );
  88. int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  89. {
  90. char *filename = "/";
  91. int ret;
  92. int dev=0;
  93. int part=1;
  94. char *ep;
  95. block_dev_desc_t *dev_desc=NULL;
  96. if (argc < 3) {
  97. printf ("usage: fatls <interface> <dev[:part]> [directory]\n");
  98. return (0);
  99. }
  100. dev = (int)simple_strtoul (argv[2], &ep, 16);
  101. dev_desc=get_dev(argv[1],dev);
  102. if (dev_desc==NULL) {
  103. puts ("\n** Invalid boot device **\n");
  104. return 1;
  105. }
  106. if (*ep) {
  107. if (*ep != ':') {
  108. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  109. return 1;
  110. }
  111. part = (int)simple_strtoul(++ep, NULL, 16);
  112. }
  113. if (fat_register_device(dev_desc,part)!=0) {
  114. printf ("\n** Unable to use %s %d:%d for fatls **\n",argv[1],dev,part);
  115. return 1;
  116. }
  117. if (argc == 4)
  118. ret = file_fat_ls (argv[3]);
  119. else
  120. ret = file_fat_ls (filename);
  121. if(ret!=0)
  122. printf("No Fat FS detected\n");
  123. return (ret);
  124. }
  125. U_BOOT_CMD(
  126. fatls, 4, 1, do_fat_ls,
  127. "fatls - list files in a directory (default /)\n",
  128. "<interface> <dev[:part]> [directory]\n"
  129. " - list files from 'dev' on 'interface' in a 'directory'\n"
  130. );
  131. int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  132. {
  133. int dev=0;
  134. int part=1;
  135. char *ep;
  136. block_dev_desc_t *dev_desc=NULL;
  137. if (argc < 2) {
  138. printf ("usage: fatinfo <interface> <dev[:part]>\n");
  139. return (0);
  140. }
  141. dev = (int)simple_strtoul (argv[2], &ep, 16);
  142. dev_desc=get_dev(argv[1],dev);
  143. if (dev_desc==NULL) {
  144. puts ("\n** Invalid boot device **\n");
  145. return 1;
  146. }
  147. if (*ep) {
  148. if (*ep != ':') {
  149. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  150. return 1;
  151. }
  152. part = (int)simple_strtoul(++ep, NULL, 16);
  153. }
  154. if (fat_register_device(dev_desc,part)!=0) {
  155. printf ("\n** Unable to use %s %d:%d for fatinfo **\n",argv[1],dev,part);
  156. return 1;
  157. }
  158. return (file_fat_detectfs ());
  159. }
  160. U_BOOT_CMD(
  161. fatinfo, 3, 1, do_fat_fsinfo,
  162. "fatinfo - print information about filesystem\n",
  163. "<interface> <dev[:part]>\n"
  164. " - print information about filesystem from 'dev' on 'interface'\n"
  165. );
  166. #ifdef NOT_IMPLEMENTED_YET
  167. /* find first device whose first partition is a DOS filesystem */
  168. int find_fat_partition (void)
  169. {
  170. int i, j;
  171. block_dev_desc_t *dev_desc;
  172. unsigned char *part_table;
  173. unsigned char buffer[ATA_BLOCKSIZE];
  174. for (i = 0; i < CFG_IDE_MAXDEVICE; i++) {
  175. dev_desc = ide_get_dev (i);
  176. if (!dev_desc) {
  177. debug ("couldn't get ide device!\n");
  178. return (-1);
  179. }
  180. if (dev_desc->part_type == PART_TYPE_DOS) {
  181. if (dev_desc->
  182. block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
  183. debug ("can't perform block_read!\n");
  184. return (-1);
  185. }
  186. part_table = &buffer[0x1be]; /* start with partition #4 */
  187. for (j = 0; j < 4; j++) {
  188. if ((part_table[4] == 1 || /* 12-bit FAT */
  189. part_table[4] == 4 || /* 16-bit FAT */
  190. part_table[4] == 6) && /* > 32Meg part */
  191. part_table[0] == 0x80) { /* bootable? */
  192. curr_dev = i;
  193. part_offset = part_table[11];
  194. part_offset <<= 8;
  195. part_offset |= part_table[10];
  196. part_offset <<= 8;
  197. part_offset |= part_table[9];
  198. part_offset <<= 8;
  199. part_offset |= part_table[8];
  200. debug ("found partition start at %ld\n", part_offset);
  201. return (0);
  202. }
  203. part_table += 16;
  204. }
  205. }
  206. }
  207. debug ("no valid devices found!\n");
  208. return (-1);
  209. }
  210. int
  211. do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
  212. {
  213. __u8 block[1024];
  214. int ret;
  215. int bknum;
  216. ret = 0;
  217. if (argc != 2) {
  218. printf ("needs an argument!\n");
  219. return (0);
  220. }
  221. bknum = simple_strtoul (argv[1], NULL, 10);
  222. if (disk_read (0, bknum, block) != 0) {
  223. printf ("Error: reading block\n");
  224. return -1;
  225. }
  226. printf ("FAT dump: %d\n", bknum);
  227. hexdump (512, block);
  228. return (ret);
  229. }
  230. int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
  231. {
  232. ulong tot;
  233. block_dev_desc_t *dev_desc;
  234. if (curr_dev < 0) {
  235. if (find_fat_partition () != 0)
  236. return (-1);
  237. }
  238. dev_desc = ide_get_dev (curr_dev);
  239. if (!dev_desc) {
  240. debug ("couldn't get ide device\n");
  241. return (-1);
  242. }
  243. tot = dev_desc->block_read (0, startblock + part_offset,
  244. getsize, (ulong *) bufptr);
  245. /* should we do this here?
  246. flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
  247. */
  248. if (tot == getsize)
  249. return (0);
  250. debug ("unable to read from device!\n");
  251. return (-1);
  252. }
  253. static int isprint (unsigned char ch)
  254. {
  255. if (ch >= 32 && ch < 127)
  256. return (1);
  257. return (0);
  258. }
  259. void hexdump (int cnt, unsigned char *data)
  260. {
  261. int i;
  262. int run;
  263. int offset;
  264. offset = 0;
  265. while (cnt) {
  266. printf ("%04X : ", offset);
  267. if (cnt >= 16)
  268. run = 16;
  269. else
  270. run = cnt;
  271. cnt -= run;
  272. for (i = 0; i < run; i++)
  273. printf ("%02X ", (unsigned int) data[i]);
  274. printf (": ");
  275. for (i = 0; i < run; i++)
  276. printf ("%c", isprint (data[i]) ? data[i] : '.');
  277. printf ("\n");
  278. data = &data[16];
  279. offset += run;
  280. }
  281. }
  282. #endif /* NOT_IMPLEMENTED_YET */
  283. #endif /* CFG_CMD_FAT */