cmd_fat.c 4.7 KB

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