cmd_fat.c 8.1 KB

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