cmd_ext2.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #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 defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
  43. #include <usb.h>
  44. #endif
  45. #if !defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_EFI_PARTITION)
  46. #error DOS or EFI 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. int do_ext2ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  55. {
  56. char *filename = "/";
  57. int dev=0;
  58. int part=1;
  59. char *ep;
  60. block_dev_desc_t *dev_desc=NULL;
  61. int part_length;
  62. if (argc < 3) {
  63. cmd_usage(cmdtp);
  64. return(1);
  65. }
  66. dev = (int)simple_strtoul (argv[2], &ep, 16);
  67. dev_desc = get_dev(argv[1],dev);
  68. if (dev_desc == NULL) {
  69. printf ("\n** Block device %s %d not supported\n", argv[1], dev);
  70. return(1);
  71. }
  72. if (*ep) {
  73. if (*ep != ':') {
  74. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  75. return(1);
  76. }
  77. part = (int)simple_strtoul(++ep, NULL, 16);
  78. }
  79. if (argc == 4) {
  80. filename = argv[3];
  81. }
  82. PRINTF("Using device %s %d:%d, directory: %s\n", argv[1], dev, part, filename);
  83. if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
  84. printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
  85. ext2fs_close();
  86. return(1);
  87. }
  88. if (!ext2fs_mount(part_length)) {
  89. printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part);
  90. ext2fs_close();
  91. return(1);
  92. }
  93. if (ext2fs_ls (filename)) {
  94. printf ("** Error ext2fs_ls() **\n");
  95. ext2fs_close();
  96. return(1);
  97. };
  98. ext2fs_close();
  99. return(0);
  100. }
  101. U_BOOT_CMD(
  102. ext2ls, 4, 1, do_ext2ls,
  103. "list files in a directory (default /)",
  104. "<interface> <dev[:part]> [directory]\n"
  105. " - list files from 'dev' on 'interface' in a 'directory'\n"
  106. );
  107. /******************************************************************************
  108. * Ext2fs boot command intepreter. Derived from diskboot
  109. */
  110. int do_ext2load (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  111. {
  112. char *filename = NULL;
  113. char *ep;
  114. int dev, part = 1;
  115. ulong addr = 0, part_length, filelen;
  116. disk_partition_t info;
  117. block_dev_desc_t *dev_desc = NULL;
  118. char buf [12];
  119. unsigned long count;
  120. char *addr_str;
  121. switch (argc) {
  122. case 3:
  123. addr_str = getenv("loadaddr");
  124. if (addr_str != NULL) {
  125. addr = simple_strtoul (addr_str, NULL, 16);
  126. } else {
  127. addr = CONFIG_SYS_LOAD_ADDR;
  128. }
  129. filename = getenv ("bootfile");
  130. count = 0;
  131. break;
  132. case 4:
  133. addr = simple_strtoul (argv[3], NULL, 16);
  134. filename = getenv ("bootfile");
  135. count = 0;
  136. break;
  137. case 5:
  138. addr = simple_strtoul (argv[3], NULL, 16);
  139. filename = argv[4];
  140. count = 0;
  141. break;
  142. case 6:
  143. addr = simple_strtoul (argv[3], NULL, 16);
  144. filename = argv[4];
  145. count = simple_strtoul (argv[5], NULL, 16);
  146. break;
  147. default:
  148. cmd_usage(cmdtp);
  149. return(1);
  150. }
  151. if (!filename) {
  152. puts ("\n** No boot file defined **\n");
  153. return(1);
  154. }
  155. dev = (int)simple_strtoul (argv[2], &ep, 16);
  156. dev_desc = get_dev(argv[1],dev);
  157. if (dev_desc==NULL) {
  158. printf ("\n** Block device %s %d not supported\n", argv[1], dev);
  159. return(1);
  160. }
  161. if (*ep) {
  162. if (*ep != ':') {
  163. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  164. return(1);
  165. }
  166. part = (int)simple_strtoul(++ep, NULL, 16);
  167. }
  168. PRINTF("Using device %s%d, partition %d\n", argv[1], dev, part);
  169. if (part != 0) {
  170. if (get_partition_info (dev_desc, part, &info)) {
  171. printf ("** Bad partition %d **\n", part);
  172. return(1);
  173. }
  174. if (strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
  175. printf ("\n** Invalid partition type \"%.32s\""
  176. " (expect \"" BOOT_PART_TYPE "\")\n",
  177. info.type);
  178. return(1);
  179. }
  180. PRINTF ("\nLoading from block device %s device %d, partition %d: "
  181. "Name: %.32s Type: %.32s File:%s\n",
  182. argv[1], dev, part, info.name, info.type, filename);
  183. } else {
  184. PRINTF ("\nLoading from block device %s device %d, File:%s\n",
  185. argv[1], dev, filename);
  186. }
  187. if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
  188. printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
  189. ext2fs_close();
  190. return(1);
  191. }
  192. if (!ext2fs_mount(part_length)) {
  193. printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part);
  194. ext2fs_close();
  195. return(1);
  196. }
  197. filelen = ext2fs_open(filename);
  198. if (filelen < 0) {
  199. printf("** File not found %s\n", filename);
  200. ext2fs_close();
  201. return(1);
  202. }
  203. if ((count < filelen) && (count != 0)) {
  204. filelen = count;
  205. }
  206. if (ext2fs_read((char *)addr, filelen) != filelen) {
  207. printf("\n** Unable to read \"%s\" from %s %d:%d **\n", filename, argv[1], dev, part);
  208. ext2fs_close();
  209. return(1);
  210. }
  211. ext2fs_close();
  212. /* Loading ok, update default load address */
  213. load_addr = addr;
  214. printf ("\n%ld bytes read\n", filelen);
  215. sprintf(buf, "%lX", filelen);
  216. setenv("filesize", buf);
  217. return(filelen);
  218. }
  219. U_BOOT_CMD(
  220. ext2load, 6, 0, do_ext2load,
  221. "load binary file from a Ext2 filesystem",
  222. "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
  223. " - load binary file 'filename' from 'dev' on 'interface'\n"
  224. " to address 'addr' from ext2 filesystem\n"
  225. );