cmd_fat.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #include <part.h>
  32. #include <fat.h>
  33. int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  34. {
  35. long size;
  36. unsigned long offset;
  37. unsigned long count = 0;
  38. unsigned long pos = 0;
  39. char buf [12];
  40. block_dev_desc_t *dev_desc=NULL;
  41. disk_partition_t info;
  42. int part, dev;
  43. if (argc < 5) {
  44. printf("usage: fatload <interface> [<dev[:part]>] "
  45. "<addr> <filename> [bytes [pos]]\n");
  46. return 1;
  47. }
  48. part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
  49. if (part < 0)
  50. return 1;
  51. dev = dev_desc->dev;
  52. if (fat_register_device(dev_desc,part)!=0) {
  53. printf("\n** Unable to use %s %d:%d for fatload **\n",
  54. argv[1], dev, part);
  55. return 1;
  56. }
  57. offset = simple_strtoul(argv[3], NULL, 16);
  58. if (argc >= 6)
  59. count = simple_strtoul(argv[5], NULL, 16);
  60. if (argc >= 7)
  61. pos = simple_strtoul(argv[6], NULL, 16);
  62. size = file_fat_read_at(argv[4], pos, (unsigned char *)offset, count);
  63. if(size==-1) {
  64. printf("\n** Unable to read \"%s\" from %s %d:%d **\n",
  65. argv[4], argv[1], dev, part);
  66. return 1;
  67. }
  68. printf("\n%ld bytes read\n", size);
  69. sprintf(buf, "%lX", size);
  70. setenv("filesize", buf);
  71. return 0;
  72. }
  73. U_BOOT_CMD(
  74. fatload, 7, 0, do_fat_fsload,
  75. "load binary file from a dos filesystem",
  76. "<interface> [<dev[:part]>] <addr> <filename> [bytes [pos]]\n"
  77. " - Load binary file 'filename' from 'dev' on 'interface'\n"
  78. " to address 'addr' from dos filesystem.\n"
  79. " 'pos' gives the file position to start loading from.\n"
  80. " If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
  81. " 'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
  82. " the load stops on end of file."
  83. );
  84. int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  85. {
  86. char *filename = "/";
  87. int ret, dev, part;
  88. block_dev_desc_t *dev_desc=NULL;
  89. disk_partition_t info;
  90. if (argc < 2) {
  91. printf("usage: fatls <interface> [<dev[:part]>] [directory]\n");
  92. return 0;
  93. }
  94. part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
  95. if (part < 0)
  96. return 1;
  97. dev = dev_desc->dev;
  98. if (fat_register_device(dev_desc,part)!=0) {
  99. printf("\n** Unable to use %s %d:%d for fatls **\n",
  100. argv[1], dev, part);
  101. return 1;
  102. }
  103. if (argc == 4)
  104. ret = file_fat_ls(argv[3]);
  105. else
  106. ret = file_fat_ls(filename);
  107. if(ret!=0)
  108. printf("No Fat FS detected\n");
  109. return ret;
  110. }
  111. U_BOOT_CMD(
  112. fatls, 4, 1, do_fat_ls,
  113. "list files in a directory (default /)",
  114. "<interface> [<dev[:part]>] [directory]\n"
  115. " - list files from 'dev' on 'interface' in a 'directory'"
  116. );
  117. int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  118. {
  119. int dev, part;
  120. block_dev_desc_t *dev_desc;
  121. disk_partition_t info;
  122. if (argc < 2) {
  123. printf("usage: fatinfo <interface> [<dev[:part]>]\n");
  124. return 0;
  125. }
  126. part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
  127. if (part < 0)
  128. return 1;
  129. dev = dev_desc->dev;
  130. if (fat_register_device(dev_desc,part)!=0) {
  131. printf("\n** Unable to use %s %d:%d for fatinfo **\n",
  132. argv[1], dev, part);
  133. return 1;
  134. }
  135. return file_fat_detectfs();
  136. }
  137. U_BOOT_CMD(
  138. fatinfo, 3, 1, do_fat_fsinfo,
  139. "print information about filesystem",
  140. "<interface> [<dev[:part]>]\n"
  141. " - print information about filesystem from 'dev' on 'interface'"
  142. );
  143. #ifdef CONFIG_FAT_WRITE
  144. static int do_fat_fswrite(cmd_tbl_t *cmdtp, int flag,
  145. int argc, char * const argv[])
  146. {
  147. long size;
  148. unsigned long addr;
  149. unsigned long count;
  150. block_dev_desc_t *dev_desc = NULL;
  151. disk_partition_t info;
  152. int dev = 0;
  153. int part = 1;
  154. if (argc < 5)
  155. return cmd_usage(cmdtp);
  156. part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
  157. if (part < 0)
  158. return 1;
  159. dev = dev_desc->dev;
  160. if (fat_register_device(dev_desc, part) != 0) {
  161. printf("\n** Unable to use %s %d:%d for fatwrite **\n",
  162. argv[1], dev, part);
  163. return 1;
  164. }
  165. addr = simple_strtoul(argv[3], NULL, 16);
  166. count = simple_strtoul(argv[5], NULL, 16);
  167. size = file_fat_write(argv[4], (void *)addr, count);
  168. if (size == -1) {
  169. printf("\n** Unable to write \"%s\" from %s %d:%d **\n",
  170. argv[4], argv[1], dev, part);
  171. return 1;
  172. }
  173. printf("%ld bytes written\n", size);
  174. return 0;
  175. }
  176. U_BOOT_CMD(
  177. fatwrite, 6, 0, do_fat_fswrite,
  178. "write file into a dos filesystem",
  179. "<interface> <dev[:part]> <addr> <filename> <bytes>\n"
  180. " - write file 'filename' from the address 'addr' in RAM\n"
  181. " to 'dev' on 'interface'"
  182. );
  183. #endif