cmd_fat.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include <fat.h>
  33. int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  34. {
  35. long size;
  36. unsigned long offset;
  37. unsigned long count;
  38. char buf [12];
  39. block_dev_desc_t *dev_desc=NULL;
  40. int dev=0;
  41. int part=1;
  42. char *ep;
  43. if (argc < 5) {
  44. printf ("usage: fatload <interface> <dev[:part]> <addr> <filename> [bytes]\n");
  45. return 1;
  46. }
  47. dev = (int)simple_strtoul (argv[2], &ep, 16);
  48. dev_desc=get_dev(argv[1],dev);
  49. if (dev_desc==NULL) {
  50. puts ("\n** Invalid boot device **\n");
  51. return 1;
  52. }
  53. if (*ep) {
  54. if (*ep != ':') {
  55. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  56. return 1;
  57. }
  58. part = (int)simple_strtoul(++ep, NULL, 16);
  59. }
  60. if (fat_register_device(dev_desc,part)!=0) {
  61. printf ("\n** Unable to use %s %d:%d for fatload **\n",argv[1],dev,part);
  62. return 1;
  63. }
  64. offset = simple_strtoul (argv[3], NULL, 16);
  65. if (argc == 6)
  66. count = simple_strtoul (argv[5], NULL, 16);
  67. else
  68. count = 0;
  69. size = file_fat_read (argv[4], (unsigned char *) offset, count);
  70. if(size==-1) {
  71. printf("\n** Unable to read \"%s\" from %s %d:%d **\n",argv[4],argv[1],dev,part);
  72. return 1;
  73. }
  74. printf ("\n%ld bytes read\n", size);
  75. sprintf(buf, "%lX", size);
  76. setenv("filesize", buf);
  77. return 0;
  78. }
  79. U_BOOT_CMD(
  80. fatload, 6, 0, do_fat_fsload,
  81. "fatload - load binary file from a dos filesystem\n",
  82. "<interface> <dev[:part]> <addr> <filename> [bytes]\n"
  83. " - load binary file 'filename' from 'dev' on 'interface'\n"
  84. " to address 'addr' from dos filesystem\n"
  85. );
  86. int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  87. {
  88. char *filename = "/";
  89. int ret;
  90. int dev=0;
  91. int part=1;
  92. char *ep;
  93. block_dev_desc_t *dev_desc=NULL;
  94. if (argc < 3) {
  95. printf ("usage: fatls <interface> <dev[:part]> [directory]\n");
  96. return (0);
  97. }
  98. dev = (int)simple_strtoul (argv[2], &ep, 16);
  99. dev_desc=get_dev(argv[1],dev);
  100. if (dev_desc==NULL) {
  101. puts ("\n** Invalid boot device **\n");
  102. return 1;
  103. }
  104. if (*ep) {
  105. if (*ep != ':') {
  106. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  107. return 1;
  108. }
  109. part = (int)simple_strtoul(++ep, NULL, 16);
  110. }
  111. if (fat_register_device(dev_desc,part)!=0) {
  112. printf ("\n** Unable to use %s %d:%d for fatls **\n",argv[1],dev,part);
  113. return 1;
  114. }
  115. if (argc == 4)
  116. ret = file_fat_ls (argv[3]);
  117. else
  118. ret = file_fat_ls (filename);
  119. if(ret!=0)
  120. printf("No Fat FS detected\n");
  121. return (ret);
  122. }
  123. U_BOOT_CMD(
  124. fatls, 4, 1, do_fat_ls,
  125. "fatls - list files in a directory (default /)\n",
  126. "<interface> <dev[:part]> [directory]\n"
  127. " - list files from 'dev' on 'interface' in a 'directory'\n"
  128. );
  129. int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  130. {
  131. int dev=0;
  132. int part=1;
  133. char *ep;
  134. block_dev_desc_t *dev_desc=NULL;
  135. if (argc < 2) {
  136. printf ("usage: fatinfo <interface> <dev[:part]>\n");
  137. return (0);
  138. }
  139. dev = (int)simple_strtoul (argv[2], &ep, 16);
  140. dev_desc=get_dev(argv[1],dev);
  141. if (dev_desc==NULL) {
  142. puts ("\n** Invalid boot device **\n");
  143. return 1;
  144. }
  145. if (*ep) {
  146. if (*ep != ':') {
  147. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  148. return 1;
  149. }
  150. part = (int)simple_strtoul(++ep, NULL, 16);
  151. }
  152. if (fat_register_device(dev_desc,part)!=0) {
  153. printf ("\n** Unable to use %s %d:%d for fatinfo **\n",argv[1],dev,part);
  154. return 1;
  155. }
  156. return (file_fat_detectfs ());
  157. }
  158. U_BOOT_CMD(
  159. fatinfo, 3, 1, do_fat_fsinfo,
  160. "fatinfo - print information about filesystem\n",
  161. "<interface> <dev[:part]>\n"
  162. " - print information about filesystem from 'dev' on 'interface'\n"
  163. );
  164. #ifdef NOT_IMPLEMENTED_YET
  165. /* find first device whose first partition is a DOS filesystem */
  166. int find_fat_partition (void)
  167. {
  168. int i, j;
  169. block_dev_desc_t *dev_desc;
  170. unsigned char *part_table;
  171. unsigned char buffer[ATA_BLOCKSIZE];
  172. for (i = 0; i < CFG_IDE_MAXDEVICE; i++) {
  173. dev_desc = ide_get_dev (i);
  174. if (!dev_desc) {
  175. debug ("couldn't get ide device!\n");
  176. return (-1);
  177. }
  178. if (dev_desc->part_type == PART_TYPE_DOS) {
  179. if (dev_desc->
  180. block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
  181. debug ("can't perform block_read!\n");
  182. return (-1);
  183. }
  184. part_table = &buffer[0x1be]; /* start with partition #4 */
  185. for (j = 0; j < 4; j++) {
  186. if ((part_table[4] == 1 || /* 12-bit FAT */
  187. part_table[4] == 4 || /* 16-bit FAT */
  188. part_table[4] == 6) && /* > 32Meg part */
  189. part_table[0] == 0x80) { /* bootable? */
  190. curr_dev = i;
  191. part_offset = part_table[11];
  192. part_offset <<= 8;
  193. part_offset |= part_table[10];
  194. part_offset <<= 8;
  195. part_offset |= part_table[9];
  196. part_offset <<= 8;
  197. part_offset |= part_table[8];
  198. debug ("found partition start at %ld\n", part_offset);
  199. return (0);
  200. }
  201. part_table += 16;
  202. }
  203. }
  204. }
  205. debug ("no valid devices found!\n");
  206. return (-1);
  207. }
  208. int
  209. do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
  210. {
  211. __u8 block[1024];
  212. int ret;
  213. int bknum;
  214. ret = 0;
  215. if (argc != 2) {
  216. printf ("needs an argument!\n");
  217. return (0);
  218. }
  219. bknum = simple_strtoul (argv[1], NULL, 10);
  220. if (disk_read (0, bknum, block) != 0) {
  221. printf ("Error: reading block\n");
  222. return -1;
  223. }
  224. printf ("FAT dump: %d\n", bknum);
  225. hexdump (512, block);
  226. return (ret);
  227. }
  228. int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
  229. {
  230. ulong tot;
  231. block_dev_desc_t *dev_desc;
  232. if (curr_dev < 0) {
  233. if (find_fat_partition () != 0)
  234. return (-1);
  235. }
  236. dev_desc = ide_get_dev (curr_dev);
  237. if (!dev_desc) {
  238. debug ("couldn't get ide device\n");
  239. return (-1);
  240. }
  241. tot = dev_desc->block_read (0, startblock + part_offset,
  242. getsize, (ulong *) bufptr);
  243. /* should we do this here?
  244. flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
  245. */
  246. if (tot == getsize)
  247. return (0);
  248. debug ("unable to read from device!\n");
  249. return (-1);
  250. }
  251. static int isprint (unsigned char ch)
  252. {
  253. if (ch >= 32 && ch < 127)
  254. return (1);
  255. return (0);
  256. }
  257. void hexdump (int cnt, unsigned char *data)
  258. {
  259. int i;
  260. int run;
  261. int offset;
  262. offset = 0;
  263. while (cnt) {
  264. printf ("%04X : ", offset);
  265. if (cnt >= 16)
  266. run = 16;
  267. else
  268. run = cnt;
  269. cnt -= run;
  270. for (i = 0; i < run; i++)
  271. printf ("%02X ", (unsigned int) data[i]);
  272. printf (": ");
  273. for (i = 0; i < run; i++)
  274. printf ("%c", isprint (data[i]) ? data[i] : '.');
  275. printf ("\n");
  276. data = &data[16];
  277. offset += run;
  278. }
  279. }
  280. #endif /* NOT_IMPLEMENTED_YET */