cmd_fat.c 8.0 KB

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