cmd_fat.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. extern block_dev_desc_t *ide_get_dev (int dev);
  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. if (argc < 3) {
  41. printf ("usage:fatload <filename> <addr> [bytes]\n");
  42. return (0);
  43. }
  44. offset = simple_strtoul (argv[2], NULL, 16);
  45. if (argc == 4)
  46. count = simple_strtoul (argv[3], NULL, 16);
  47. else
  48. count = 0;
  49. size = file_fat_read (argv[1], (unsigned char *) offset, count);
  50. printf ("%ld bytes read\n", size);
  51. return size;
  52. }
  53. U_BOOT_CMD(
  54. fatload, 4, 0, do_fat_fsload,
  55. "fatload - load binary file from a dos filesystem\n",
  56. "[ off ] [ filename ]\n"
  57. " - load binary file from dos filesystem\n"
  58. " with offset 'off'\n"
  59. );
  60. int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  61. {
  62. char *filename = "/";
  63. int ret;
  64. if (argc == 2)
  65. ret = file_fat_ls (argv[1]);
  66. else
  67. ret = file_fat_ls (filename);
  68. return (ret);
  69. }
  70. U_BOOT_CMD(
  71. fatls, 2, 1, do_fat_ls,
  72. "fatls - list files in a directory (default /)\n",
  73. "[ directory ]\n"
  74. " - list files in a directory\n"
  75. );
  76. int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  77. {
  78. int ret;
  79. ret = 0;
  80. printf ("FAT info: %d\n", file_fat_detectfs ());
  81. return (ret);
  82. }
  83. U_BOOT_CMD(
  84. fatinfo, 1, 1, do_fat_fsinfo,
  85. "fatinfo - print information about filesystem\n",
  86. "\n"
  87. " - print information about filesystem\n"
  88. );
  89. #ifdef NOT_IMPLEMENTED_YET
  90. /* find first device whose first partition is a DOS filesystem */
  91. int find_fat_partition (void)
  92. {
  93. int i, j;
  94. block_dev_desc_t *dev_desc;
  95. unsigned char *part_table;
  96. unsigned char buffer[ATA_BLOCKSIZE];
  97. for (i = 0; i < CFG_IDE_MAXDEVICE; i++) {
  98. dev_desc = ide_get_dev (i);
  99. if (!dev_desc) {
  100. debug ("couldn't get ide device!\n");
  101. return (-1);
  102. }
  103. if (dev_desc->part_type == PART_TYPE_DOS) {
  104. if (dev_desc->
  105. block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
  106. debug ("can't perform block_read!\n");
  107. return (-1);
  108. }
  109. part_table = &buffer[0x1be]; /* start with partition #4 */
  110. for (j = 0; j < 4; j++) {
  111. if ((part_table[4] == 1 || /* 12-bit FAT */
  112. part_table[4] == 4 || /* 16-bit FAT */
  113. part_table[4] == 6) && /* > 32Meg part */
  114. part_table[0] == 0x80) { /* bootable? */
  115. curr_dev = i;
  116. part_offset = part_table[11];
  117. part_offset <<= 8;
  118. part_offset |= part_table[10];
  119. part_offset <<= 8;
  120. part_offset |= part_table[9];
  121. part_offset <<= 8;
  122. part_offset |= part_table[8];
  123. debug ("found partition start at %ld\n", part_offset);
  124. return (0);
  125. }
  126. part_table += 16;
  127. }
  128. }
  129. }
  130. debug ("no valid devices found!\n");
  131. return (-1);
  132. }
  133. int
  134. do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
  135. {
  136. __u8 block[1024];
  137. int ret;
  138. int bknum;
  139. ret = 0;
  140. if (argc != 2) {
  141. printf ("needs an argument!\n");
  142. return (0);
  143. }
  144. bknum = simple_strtoul (argv[1], NULL, 10);
  145. if (disk_read (0, bknum, block) != 0) {
  146. printf ("Error: reading block\n");
  147. return -1;
  148. }
  149. printf ("FAT dump: %d\n", bknum);
  150. hexdump (512, block);
  151. return (ret);
  152. }
  153. int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
  154. {
  155. ulong tot;
  156. block_dev_desc_t *dev_desc;
  157. if (curr_dev < 0) {
  158. if (find_fat_partition () != 0)
  159. return (-1);
  160. }
  161. dev_desc = ide_get_dev (curr_dev);
  162. if (!dev_desc) {
  163. debug ("couldn't get ide device\n");
  164. return (-1);
  165. }
  166. tot = dev_desc->block_read (0, startblock + part_offset,
  167. getsize, (ulong *) bufptr);
  168. /* should we do this here?
  169. flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
  170. */
  171. if (tot == getsize)
  172. return (0);
  173. debug ("unable to read from device!\n");
  174. return (-1);
  175. }
  176. static int isprint (unsigned char ch)
  177. {
  178. if (ch >= 32 && ch < 127)
  179. return (1);
  180. return (0);
  181. }
  182. void hexdump (int cnt, unsigned char *data)
  183. {
  184. int i;
  185. int run;
  186. int offset;
  187. offset = 0;
  188. while (cnt) {
  189. printf ("%04X : ", offset);
  190. if (cnt >= 16)
  191. run = 16;
  192. else
  193. run = cnt;
  194. cnt -= run;
  195. for (i = 0; i < run; i++)
  196. printf ("%02X ", (unsigned int) data[i]);
  197. printf (": ");
  198. for (i = 0; i < run; i++)
  199. printf ("%c", isprint (data[i]) ? data[i] : '.');
  200. printf ("\n");
  201. data = &data[16];
  202. offset += run;
  203. }
  204. }
  205. #endif /* NOT_IMPLEMENTED_YET */
  206. #endif /* CFG_CMD_FAT */