cmd_fat.c 7.9 KB

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