cmd_ext2.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #if (CONFIG_COMMANDS & CFG_CMD_EXT2)
  36. #include <config.h>
  37. #include <command.h>
  38. #include <image.h>
  39. #include <linux/ctype.h>
  40. #include <asm/byteorder.h>
  41. #include <ext2fs.h>
  42. #ifndef CONFIG_DOS_PARTITION
  43. #error DOS partition support must be selected
  44. #endif
  45. /* #define EXT2_DEBUG */
  46. #ifdef EXT2_DEBUG
  47. #define PRINTF(fmt,args...) printf (fmt ,##args)
  48. #else
  49. #define PRINTF(fmt,args...)
  50. #endif
  51. static block_dev_desc_t *get_dev (char* ifname, int dev)
  52. {
  53. #if (CONFIG_COMMANDS & CFG_CMD_IDE)
  54. if (strncmp(ifname,"ide",3)==0) {
  55. extern block_dev_desc_t * ide_get_dev(int dev);
  56. return((dev >= CFG_IDE_MAXDEVICE) ? NULL : ide_get_dev(dev));
  57. }
  58. #endif
  59. #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
  60. if (strncmp(ifname,"scsi",4)==0) {
  61. extern block_dev_desc_t * scsi_get_dev(int dev);
  62. return((dev >= CFG_SCSI_MAXDEVICE) ? NULL : scsi_get_dev(dev));
  63. }
  64. #endif
  65. #if ((CONFIG_COMMANDS & CFG_CMD_USB) && defined(CONFIG_USB_STORAGE))
  66. if (strncmp(ifname,"usb",3)==0) {
  67. extern block_dev_desc_t * usb_stor_get_dev(int dev);
  68. return((dev >= USB_MAX_STOR_DEV) ? NULL : usb_stor_get_dev(dev));
  69. }
  70. #endif
  71. #if defined(CONFIG_MMC)
  72. if (strncmp(ifname,"mmc",3)==0) {
  73. extern block_dev_desc_t * mmc_get_dev(int dev);
  74. return((dev >= 1) ? NULL : mmc_get_dev(dev));
  75. }
  76. #endif
  77. #if defined(CONFIG_SYSTEMACE)
  78. if (strcmp(ifname,"ace")==0) {
  79. extern block_dev_desc_t * systemace_get_dev(int dev);
  80. return((dev >= 1) ? NULL : systemace_get_dev(dev));
  81. }
  82. #endif
  83. return(NULL);
  84. }
  85. int do_ext2ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  86. {
  87. char *filename = "/";
  88. int dev=0;
  89. int part=1;
  90. char *ep;
  91. block_dev_desc_t *dev_desc=NULL;
  92. int part_length;
  93. if (argc < 3) {
  94. printf ("Usage:\n%s\n", cmdtp->usage);
  95. return(1);
  96. }
  97. dev = (int)simple_strtoul (argv[2], &ep, 16);
  98. dev_desc=get_dev(argv[1],dev);
  99. if (dev_desc == NULL) {
  100. printf ("\n** Block device %s %d not supported\n", argv[1], dev);
  101. return(1);
  102. }
  103. if (*ep) {
  104. if (*ep != ':') {
  105. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  106. return(1);
  107. }
  108. part = (int)simple_strtoul(++ep, NULL, 16);
  109. }
  110. if (argc == 4) {
  111. filename = argv[3];
  112. }
  113. PRINTF("Using device %s %d:%d, directory: %s\n", argv[1], dev, part, filename);
  114. if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
  115. printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
  116. ext2fs_close();
  117. return(1);
  118. }
  119. if (!ext2fs_mount(part_length)) {
  120. printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part);
  121. ext2fs_close();
  122. return(1);
  123. }
  124. if (ext2fs_ls (filename)) {
  125. printf ("** Error ext2fs_ls() **\n");
  126. ext2fs_close();
  127. return(1);
  128. };
  129. ext2fs_close();
  130. return(0);
  131. }
  132. U_BOOT_CMD(
  133. ext2ls, 4, 1, do_ext2ls,
  134. "ext2ls- list files in a directory (default /)\n",
  135. "<interface> <dev[:part]> [directory]\n"
  136. " - list files from 'dev' on 'interface' in a 'directory'\n"
  137. );
  138. /******************************************************************************
  139. * Ext2fs boot command intepreter. Derived from diskboot
  140. */
  141. int do_ext2load (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  142. {
  143. char *filename = NULL;
  144. char *ep;
  145. int dev, part = 0;
  146. ulong addr = 0, part_length, filelen;
  147. disk_partition_t info;
  148. block_dev_desc_t *dev_desc = NULL;
  149. char buf [12];
  150. unsigned long count;
  151. char *addr_str;
  152. switch (argc) {
  153. case 3:
  154. addr_str = getenv("loadaddr");
  155. if (addr_str != NULL) {
  156. addr = simple_strtoul (addr_str, NULL, 16);
  157. } else {
  158. addr = CFG_LOAD_ADDR;
  159. }
  160. filename = getenv ("bootfile");
  161. count = 0;
  162. break;
  163. case 4:
  164. addr = simple_strtoul (argv[3], NULL, 16);
  165. filename = getenv ("bootfile");
  166. count = 0;
  167. break;
  168. case 5:
  169. addr = simple_strtoul (argv[3], NULL, 16);
  170. filename = argv[4];
  171. count = 0;
  172. break;
  173. case 6:
  174. addr = simple_strtoul (argv[3], NULL, 16);
  175. filename = argv[4];
  176. count = simple_strtoul (argv[5], NULL, 16);
  177. break;
  178. default:
  179. printf ("Usage:\n%s\n", cmdtp->usage);
  180. return(1);
  181. }
  182. if (!filename) {
  183. puts ("\n** No boot file defined **\n");
  184. return(1);
  185. }
  186. dev = (int)simple_strtoul (argv[2], &ep, 16);
  187. dev_desc=get_dev(argv[1],dev);
  188. if (dev_desc==NULL) {
  189. printf ("\n** Block device %s %d not supported\n", argv[1], dev);
  190. return(1);
  191. }
  192. if (*ep) {
  193. if (*ep != ':') {
  194. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  195. return(1);
  196. }
  197. part = (int)simple_strtoul(++ep, NULL, 16);
  198. }
  199. PRINTF("Using device %s%d, partition %d\n", argv[1], dev, part);
  200. if (part != 0) {
  201. if (get_partition_info (&dev_desc[dev], part, &info)) {
  202. printf ("** Bad partition %d **\n", part);
  203. return(1);
  204. }
  205. if (strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
  206. printf ("\n** Invalid partition type \"%.32s\""
  207. " (expect \"" BOOT_PART_TYPE "\")\n",
  208. info.type);
  209. return(1);
  210. }
  211. PRINTF ("\nLoading from block device %s device %d, partition %d: "
  212. "Name: %.32s Type: %.32s File:%s\n",
  213. argv[1], dev, part, info.name, info.type, filename);
  214. } else {
  215. PRINTF ("\nLoading from block device %s device %d, File:%s\n",
  216. argv[1], dev, filename);
  217. }
  218. if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
  219. printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
  220. ext2fs_close();
  221. return(1);
  222. }
  223. if (!ext2fs_mount(part_length)) {
  224. printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part);
  225. ext2fs_close();
  226. return(1);
  227. }
  228. filelen = ext2fs_open(filename);
  229. if (filelen < 0) {
  230. printf("** File not found %s\n", filename);
  231. ext2fs_close();
  232. return(1);
  233. }
  234. if ((count < filelen) && (count != 0)) {
  235. filelen = count;
  236. }
  237. if (ext2fs_read((char *)addr, filelen) != filelen) {
  238. printf("\n** Unable to read \"%s\" from %s %d:%d **\n", filename, argv[1], dev, part);
  239. ext2fs_close();
  240. return(1);
  241. }
  242. ext2fs_close();
  243. /* Loading ok, update default load address */
  244. load_addr = addr;
  245. printf ("\n%ld bytes read\n", filelen);
  246. sprintf(buf, "%lX", filelen);
  247. setenv("filesize", buf);
  248. return(filelen);
  249. }
  250. U_BOOT_CMD(
  251. ext2load, 6, 0, do_ext2load,
  252. "ext2load- load binary file from a Ext2 filesystem\n",
  253. "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
  254. " - load binary file 'filename' from 'dev' on 'interface'\n"
  255. " to address 'addr' from ext2 filesystem\n"
  256. );
  257. #endif /* CONFIG_COMMANDS & CFG_CMD_EXT2 */