cmd_ext2.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * (C) Copyright 2004
  3. * esd gmbh <www.esd-electronics.com>
  4. * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
  5. *
  6. * made from cmd_reiserfs by
  7. *
  8. * (C) Copyright 2003 - 2004
  9. * Sysgo Real-Time Solutions, AG <www.elinos.com>
  10. * Pavel Bartusek <pba@sysgo.com>
  11. *
  12. * See file CREDITS for list of people who contributed to this
  13. * project.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  28. * MA 02111-1307 USA
  29. *
  30. */
  31. /*
  32. * Ext2fs support
  33. */
  34. #include <common.h>
  35. #include <part.h>
  36. #if defined(CONFIG_CMD_EXT2)
  37. #include <config.h>
  38. #include <command.h>
  39. #include <image.h>
  40. #include <linux/ctype.h>
  41. #include <asm/byteorder.h>
  42. #include <ext2fs.h>
  43. #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
  44. #include <usb.h>
  45. #endif
  46. #ifndef CONFIG_DOS_PARTITION
  47. #error DOS partition support must be selected
  48. #endif
  49. /* #define EXT2_DEBUG */
  50. #ifdef EXT2_DEBUG
  51. #define PRINTF(fmt,args...) printf (fmt ,##args)
  52. #else
  53. #define PRINTF(fmt,args...)
  54. #endif
  55. int do_ext2ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  56. {
  57. char *filename = "/";
  58. int dev=0;
  59. int part=1;
  60. char *ep;
  61. block_dev_desc_t *dev_desc=NULL;
  62. int part_length;
  63. if (argc < 3) {
  64. printf ("Usage:\n%s\n", cmdtp->usage);
  65. return(1);
  66. }
  67. dev = (int)simple_strtoul (argv[2], &ep, 16);
  68. dev_desc = get_dev(argv[1],dev);
  69. if (dev_desc == NULL) {
  70. printf ("\n** Block device %s %d not supported\n", argv[1], dev);
  71. return(1);
  72. }
  73. if (*ep) {
  74. if (*ep != ':') {
  75. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  76. return(1);
  77. }
  78. part = (int)simple_strtoul(++ep, NULL, 16);
  79. }
  80. if (argc == 4) {
  81. filename = argv[3];
  82. }
  83. PRINTF("Using device %s %d:%d, directory: %s\n", argv[1], dev, part, filename);
  84. if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
  85. printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
  86. ext2fs_close();
  87. return(1);
  88. }
  89. if (!ext2fs_mount(part_length)) {
  90. printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part);
  91. ext2fs_close();
  92. return(1);
  93. }
  94. if (ext2fs_ls (filename)) {
  95. printf ("** Error ext2fs_ls() **\n");
  96. ext2fs_close();
  97. return(1);
  98. };
  99. ext2fs_close();
  100. return(0);
  101. }
  102. U_BOOT_CMD(
  103. ext2ls, 4, 1, do_ext2ls,
  104. "ext2ls - list files in a directory (default /)\n",
  105. "<interface> <dev[:part]> [directory]\n"
  106. " - list files from 'dev' on 'interface' in a 'directory'\n"
  107. );
  108. /******************************************************************************
  109. * Ext2fs boot command intepreter. Derived from diskboot
  110. */
  111. int do_ext2load (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  112. {
  113. char *filename = NULL;
  114. char *ep;
  115. int dev, part = 1;
  116. ulong addr = 0, part_length, filelen;
  117. disk_partition_t info;
  118. block_dev_desc_t *dev_desc = NULL;
  119. char buf [12];
  120. unsigned long count;
  121. char *addr_str;
  122. switch (argc) {
  123. case 3:
  124. addr_str = getenv("loadaddr");
  125. if (addr_str != NULL) {
  126. addr = simple_strtoul (addr_str, NULL, 16);
  127. } else {
  128. addr = CFG_LOAD_ADDR;
  129. }
  130. filename = getenv ("bootfile");
  131. count = 0;
  132. break;
  133. case 4:
  134. addr = simple_strtoul (argv[3], NULL, 16);
  135. filename = getenv ("bootfile");
  136. count = 0;
  137. break;
  138. case 5:
  139. addr = simple_strtoul (argv[3], NULL, 16);
  140. filename = argv[4];
  141. count = 0;
  142. break;
  143. case 6:
  144. addr = simple_strtoul (argv[3], NULL, 16);
  145. filename = argv[4];
  146. count = simple_strtoul (argv[5], NULL, 16);
  147. break;
  148. default:
  149. printf ("Usage:\n%s\n", cmdtp->usage);
  150. return(1);
  151. }
  152. if (!filename) {
  153. puts ("\n** No boot file defined **\n");
  154. return(1);
  155. }
  156. dev = (int)simple_strtoul (argv[2], &ep, 16);
  157. dev_desc = get_dev(argv[1],dev);
  158. if (dev_desc==NULL) {
  159. printf ("\n** Block device %s %d not supported\n", argv[1], dev);
  160. return(1);
  161. }
  162. if (*ep) {
  163. if (*ep != ':') {
  164. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  165. return(1);
  166. }
  167. part = (int)simple_strtoul(++ep, NULL, 16);
  168. }
  169. PRINTF("Using device %s%d, partition %d\n", argv[1], dev, part);
  170. if (part != 0) {
  171. if (get_partition_info (dev_desc, part, &info)) {
  172. printf ("** Bad partition %d **\n", part);
  173. return(1);
  174. }
  175. if (strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
  176. printf ("\n** Invalid partition type \"%.32s\""
  177. " (expect \"" BOOT_PART_TYPE "\")\n",
  178. info.type);
  179. return(1);
  180. }
  181. PRINTF ("\nLoading from block device %s device %d, partition %d: "
  182. "Name: %.32s Type: %.32s File:%s\n",
  183. argv[1], dev, part, info.name, info.type, filename);
  184. } else {
  185. PRINTF ("\nLoading from block device %s device %d, File:%s\n",
  186. argv[1], dev, filename);
  187. }
  188. if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
  189. printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
  190. ext2fs_close();
  191. return(1);
  192. }
  193. if (!ext2fs_mount(part_length)) {
  194. printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part);
  195. ext2fs_close();
  196. return(1);
  197. }
  198. filelen = ext2fs_open(filename);
  199. if (filelen < 0) {
  200. printf("** File not found %s\n", filename);
  201. ext2fs_close();
  202. return(1);
  203. }
  204. if ((count < filelen) && (count != 0)) {
  205. filelen = count;
  206. }
  207. if (ext2fs_read((char *)addr, filelen) != filelen) {
  208. printf("\n** Unable to read \"%s\" from %s %d:%d **\n", filename, argv[1], dev, part);
  209. ext2fs_close();
  210. return(1);
  211. }
  212. ext2fs_close();
  213. /* Loading ok, update default load address */
  214. load_addr = addr;
  215. printf ("\n%ld bytes read\n", filelen);
  216. sprintf(buf, "%lX", filelen);
  217. setenv("filesize", buf);
  218. return(filelen);
  219. }
  220. U_BOOT_CMD(
  221. ext2load, 6, 0, do_ext2load,
  222. "ext2load- load binary file from a Ext2 filesystem\n",
  223. "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
  224. " - load binary file 'filename' from 'dev' on 'interface'\n"
  225. " to address 'addr' from ext2 filesystem\n"
  226. );
  227. #endif