cmd_ximg.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * (C) Copyright 2000-2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2003
  6. * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /*
  27. * Multi Image extract
  28. */
  29. #include <common.h>
  30. #include <command.h>
  31. #include <image.h>
  32. #include <watchdog.h>
  33. #if defined(CONFIG_BZIP2)
  34. #include <bzlib.h>
  35. #endif
  36. #include <asm/byteorder.h>
  37. #ifndef CONFIG_SYS_XIMG_LEN
  38. /* use 8MByte as default max gunzip size */
  39. #define CONFIG_SYS_XIMG_LEN 0x800000
  40. #endif
  41. static int
  42. do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  43. {
  44. ulong addr = load_addr;
  45. ulong dest = 0;
  46. ulong data, len, count;
  47. int verify;
  48. int part = 0;
  49. image_header_t *hdr;
  50. #if defined(CONFIG_FIT)
  51. const char *uname = NULL;
  52. const void* fit_hdr;
  53. int noffset;
  54. const void *fit_data;
  55. size_t fit_len;
  56. #endif
  57. #ifdef CONFIG_GZIP
  58. uint unc_len = CONFIG_SYS_XIMG_LEN;
  59. #endif
  60. uint8_t comp;
  61. verify = getenv_yesno("verify");
  62. if (argc > 1) {
  63. addr = simple_strtoul(argv[1], NULL, 16);
  64. }
  65. if (argc > 2) {
  66. part = simple_strtoul(argv[2], NULL, 16);
  67. #if defined(CONFIG_FIT)
  68. uname = argv[2];
  69. #endif
  70. }
  71. if (argc > 3) {
  72. dest = simple_strtoul(argv[3], NULL, 16);
  73. }
  74. switch (genimg_get_format((void *)addr)) {
  75. case IMAGE_FORMAT_LEGACY:
  76. printf("## Copying part %d from legacy image "
  77. "at %08lx ...\n", part, addr);
  78. hdr = (image_header_t *)addr;
  79. if (!image_check_magic(hdr)) {
  80. printf("Bad Magic Number\n");
  81. return 1;
  82. }
  83. if (!image_check_hcrc(hdr)) {
  84. printf("Bad Header Checksum\n");
  85. return 1;
  86. }
  87. #ifdef DEBUG
  88. image_print_contents(hdr);
  89. #endif
  90. if (!image_check_type(hdr, IH_TYPE_MULTI)) {
  91. printf("Wrong Image Type for %s command\n",
  92. cmdtp->name);
  93. return 1;
  94. }
  95. comp = image_get_comp(hdr);
  96. if ((comp != IH_COMP_NONE) && (argc < 4)) {
  97. printf("Must specify load address for %s command "
  98. "with compressed image\n",
  99. cmdtp->name);
  100. return 1;
  101. }
  102. if (verify) {
  103. printf(" Verifying Checksum ... ");
  104. if (!image_check_dcrc(hdr)) {
  105. printf("Bad Data CRC\n");
  106. return 1;
  107. }
  108. printf("OK\n");
  109. }
  110. count = image_multi_count(hdr);
  111. if (part >= count) {
  112. printf("Bad Image Part\n");
  113. return 1;
  114. }
  115. image_multi_getimg(hdr, part, &data, &len);
  116. break;
  117. #if defined(CONFIG_FIT)
  118. case IMAGE_FORMAT_FIT:
  119. if (uname == NULL) {
  120. puts("No FIT subimage unit name\n");
  121. return 1;
  122. }
  123. printf("## Copying '%s' subimage from FIT image "
  124. "at %08lx ...\n", uname, addr);
  125. fit_hdr = (const void *)addr;
  126. if (!fit_check_format(fit_hdr)) {
  127. puts("Bad FIT image format\n");
  128. return 1;
  129. }
  130. /* get subimage node offset */
  131. noffset = fit_image_get_node(fit_hdr, uname);
  132. if (noffset < 0) {
  133. printf("Can't find '%s' FIT subimage\n", uname);
  134. return 1;
  135. }
  136. if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
  137. && (argc < 4)) {
  138. printf("Must specify load address for %s command "
  139. "with compressed image\n",
  140. cmdtp->name);
  141. return 1;
  142. }
  143. /* verify integrity */
  144. if (verify) {
  145. if (!fit_image_verify(fit_hdr, noffset)) {
  146. puts("Bad Data Hash\n");
  147. return 1;
  148. }
  149. }
  150. /* get subimage data address and length */
  151. if (fit_image_get_data(fit_hdr, noffset,
  152. &fit_data, &fit_len)) {
  153. puts("Could not find script subimage data\n");
  154. return 1;
  155. }
  156. if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
  157. puts("Could not find script subimage "
  158. "compression type\n");
  159. return 1;
  160. }
  161. data = (ulong)fit_data;
  162. len = (ulong)fit_len;
  163. break;
  164. #endif
  165. default:
  166. puts("Invalid image type for imxtract\n");
  167. return 1;
  168. }
  169. if (argc > 3) {
  170. switch (comp) {
  171. case IH_COMP_NONE:
  172. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  173. {
  174. size_t l = len;
  175. size_t tail;
  176. void *to = (void *) dest;
  177. void *from = (void *)data;
  178. printf(" Loading part %d ... ", part);
  179. while (l > 0) {
  180. tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  181. WATCHDOG_RESET();
  182. memmove(to, from, tail);
  183. to += tail;
  184. from += tail;
  185. l -= tail;
  186. }
  187. }
  188. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  189. printf(" Loading part %d ... ", part);
  190. memmove((char *) dest, (char *)data, len);
  191. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  192. break;
  193. #ifdef CONFIG_GZIP
  194. case IH_COMP_GZIP:
  195. printf(" Uncompressing part %d ... ", part);
  196. if (gunzip((void *) dest, unc_len,
  197. (uchar *) data, &len) != 0) {
  198. puts("GUNZIP ERROR - image not loaded\n");
  199. return 1;
  200. }
  201. break;
  202. #endif
  203. #if defined(CONFIG_BZIP2)
  204. case IH_COMP_BZIP2:
  205. {
  206. int i;
  207. printf(" Uncompressing part %d ... ", part);
  208. /*
  209. * If we've got less than 4 MB of malloc()
  210. * space, use slower decompression algorithm
  211. * which requires at most 2300 KB of memory.
  212. */
  213. i = BZ2_bzBuffToBuffDecompress(
  214. (char *)ntohl(hdr->ih_load),
  215. &unc_len, (char *)data, len,
  216. CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
  217. 0);
  218. if (i != BZ_OK) {
  219. printf("BUNZIP2 ERROR %d - "
  220. "image not loaded\n", i);
  221. return 1;
  222. }
  223. }
  224. break;
  225. #endif /* CONFIG_BZIP2 */
  226. default:
  227. printf("Unimplemented compression type %d\n", comp);
  228. return 1;
  229. }
  230. puts("OK\n");
  231. }
  232. setenv_hex("fileaddr", data);
  233. setenv_hex("filesize", len);
  234. return 0;
  235. }
  236. #ifdef CONFIG_SYS_LONGHELP
  237. static char imgextract_help_text[] =
  238. "addr part [dest]\n"
  239. " - extract <part> from legacy image at <addr> and copy to <dest>"
  240. #if defined(CONFIG_FIT)
  241. "\n"
  242. "addr uname [dest]\n"
  243. " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
  244. #endif
  245. "";
  246. #endif
  247. U_BOOT_CMD(
  248. imxtract, 4, 1, do_imgextract,
  249. "extract a part of a multi-image", imgextract_help_text
  250. );