cmd_ext2.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. PRINTF("Using device %s %d:%d, directory: %s\n", argv[1], dev, part, filename);
  82. if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
  83. printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
  84. ext2fs_close();
  85. return 1;
  86. }
  87. if (!ext2fs_mount(part_length)) {
  88. printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part);
  89. ext2fs_close();
  90. return 1;
  91. }
  92. if (ext2fs_ls (filename)) {
  93. printf ("** Error ext2fs_ls() **\n");
  94. ext2fs_close();
  95. return 1;
  96. };
  97. ext2fs_close();
  98. return 0;
  99. }
  100. U_BOOT_CMD(
  101. ext2ls, 4, 1, do_ext2ls,
  102. "list files in a directory (default /)",
  103. "<interface> <dev[:part]> [directory]\n"
  104. " - list files from 'dev' on 'interface' in a 'directory'"
  105. );
  106. /******************************************************************************
  107. * Ext2fs boot command intepreter. Derived from diskboot
  108. */
  109. int do_ext2load (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  110. {
  111. char *filename = NULL;
  112. char *ep;
  113. int dev, part = 1;
  114. ulong addr = 0, part_length;
  115. int 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. filename = getenv ("bootfile");
  129. count = 0;
  130. break;
  131. case 4:
  132. addr = simple_strtoul (argv[3], NULL, 16);
  133. filename = getenv ("bootfile");
  134. count = 0;
  135. break;
  136. case 5:
  137. addr = simple_strtoul (argv[3], NULL, 16);
  138. filename = argv[4];
  139. count = 0;
  140. break;
  141. case 6:
  142. addr = simple_strtoul (argv[3], NULL, 16);
  143. filename = argv[4];
  144. count = simple_strtoul (argv[5], NULL, 16);
  145. break;
  146. default:
  147. cmd_usage(cmdtp);
  148. return 1;
  149. }
  150. if (!filename) {
  151. puts ("** No boot file defined **\n");
  152. return 1;
  153. }
  154. dev = (int)simple_strtoul (argv[2], &ep, 16);
  155. dev_desc = get_dev(argv[1],dev);
  156. if (dev_desc==NULL) {
  157. printf ("** Block device %s %d not supported\n", argv[1], dev);
  158. return 1;
  159. }
  160. if (*ep) {
  161. if (*ep != ':') {
  162. puts ("** Invalid boot device, use `dev[:part]' **\n");
  163. return 1;
  164. }
  165. part = (int)simple_strtoul(++ep, NULL, 16);
  166. }
  167. PRINTF("Using device %s%d, partition %d\n", argv[1], dev, part);
  168. if (part != 0) {
  169. if (get_partition_info (dev_desc, part, &info)) {
  170. printf ("** Bad partition %d **\n", part);
  171. return 1;
  172. }
  173. if (strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
  174. printf ("** Invalid partition type \"%.32s\""
  175. " (expect \"" BOOT_PART_TYPE "\")\n",
  176. info.type);
  177. return 1;
  178. }
  179. printf ("Loading file \"%s\" "
  180. "from %s device %d:%d (%.32s)\n",
  181. filename,
  182. argv[1], dev, part, info.name);
  183. } else {
  184. printf ("Loading file \"%s\" from %s device %d\n",
  185. filename, argv[1], dev);
  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",
  194. 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("** Unable to read \"%s\" from %s %d:%d **\n",
  209. filename, argv[1], dev, part);
  210. ext2fs_close();
  211. return 1;
  212. }
  213. ext2fs_close();
  214. /* Loading ok, update default load address */
  215. load_addr = addr;
  216. printf ("%d bytes read\n", filelen);
  217. sprintf(buf, "%X", filelen);
  218. setenv("filesize", buf);
  219. return 0;
  220. }
  221. U_BOOT_CMD(
  222. ext2load, 6, 0, do_ext2load,
  223. "load binary file from a Ext2 filesystem",
  224. "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
  225. " - load binary file 'filename' from 'dev' on 'interface'\n"
  226. " to address 'addr' from ext2 filesystem"
  227. );