cmd_ext2.c 6.0 KB

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