cmd_ext2.c 7.2 KB

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