cmd_fat.c 4.7 KB

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