cmd_ximg.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. uint unc_len = CONFIG_SYS_XIMG_LEN;
  58. uint8_t comp;
  59. verify = getenv_yesno("verify");
  60. if (argc > 1) {
  61. addr = simple_strtoul(argv[1], NULL, 16);
  62. }
  63. if (argc > 2) {
  64. part = simple_strtoul(argv[2], NULL, 16);
  65. #if defined(CONFIG_FIT)
  66. uname = argv[2];
  67. #endif
  68. }
  69. if (argc > 3) {
  70. dest = simple_strtoul(argv[3], NULL, 16);
  71. }
  72. switch (genimg_get_format((void *)addr)) {
  73. case IMAGE_FORMAT_LEGACY:
  74. printf("## Copying part %d from legacy image "
  75. "at %08lx ...\n", part, addr);
  76. hdr = (image_header_t *)addr;
  77. if (!image_check_magic(hdr)) {
  78. printf("Bad Magic Number\n");
  79. return 1;
  80. }
  81. if (!image_check_hcrc(hdr)) {
  82. printf("Bad Header Checksum\n");
  83. return 1;
  84. }
  85. #ifdef DEBUG
  86. image_print_contents(hdr);
  87. #endif
  88. if (!image_check_type(hdr, IH_TYPE_MULTI)) {
  89. printf("Wrong Image Type for %s command\n",
  90. cmdtp->name);
  91. return 1;
  92. }
  93. comp = image_get_comp(hdr);
  94. if ((comp != IH_COMP_NONE) && (argc < 4)) {
  95. printf("Must specify load address for %s command "
  96. "with compressed image\n",
  97. cmdtp->name);
  98. return 1;
  99. }
  100. if (verify) {
  101. printf(" Verifying Checksum ... ");
  102. if (!image_check_dcrc(hdr)) {
  103. printf("Bad Data CRC\n");
  104. return 1;
  105. }
  106. printf("OK\n");
  107. }
  108. count = image_multi_count(hdr);
  109. if (part >= count) {
  110. printf("Bad Image Part\n");
  111. return 1;
  112. }
  113. image_multi_getimg(hdr, part, &data, &len);
  114. break;
  115. #if defined(CONFIG_FIT)
  116. case IMAGE_FORMAT_FIT:
  117. if (uname == NULL) {
  118. puts("No FIT subimage unit name\n");
  119. return 1;
  120. }
  121. printf("## Copying '%s' subimage from FIT image "
  122. "at %08lx ...\n", uname, addr);
  123. fit_hdr = (const void *)addr;
  124. if (!fit_check_format(fit_hdr)) {
  125. puts("Bad FIT image format\n");
  126. return 1;
  127. }
  128. /* get subimage node offset */
  129. noffset = fit_image_get_node(fit_hdr, uname);
  130. if (noffset < 0) {
  131. printf("Can't find '%s' FIT subimage\n", uname);
  132. return 1;
  133. }
  134. if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
  135. && (argc < 4)) {
  136. printf("Must specify load address for %s command "
  137. "with compressed image\n",
  138. cmdtp->name);
  139. return 1;
  140. }
  141. /* verify integrity */
  142. if (verify) {
  143. if (!fit_image_check_hashes(fit_hdr, noffset)) {
  144. puts("Bad Data Hash\n");
  145. return 1;
  146. }
  147. }
  148. /* get subimage data address and length */
  149. if (fit_image_get_data(fit_hdr, noffset,
  150. &fit_data, &fit_len)) {
  151. puts("Could not find script subimage data\n");
  152. return 1;
  153. }
  154. if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
  155. puts("Could not find script subimage "
  156. "compression type\n");
  157. return 1;
  158. }
  159. data = (ulong)fit_data;
  160. len = (ulong)fit_len;
  161. break;
  162. #endif
  163. default:
  164. puts("Invalid image type for imxtract\n");
  165. return 1;
  166. }
  167. if (argc > 3) {
  168. switch (comp) {
  169. case IH_COMP_NONE:
  170. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  171. {
  172. size_t l = len;
  173. size_t tail;
  174. void *to = (void *) dest;
  175. void *from = (void *)data;
  176. printf(" Loading part %d ... ", part);
  177. while (l > 0) {
  178. tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  179. WATCHDOG_RESET();
  180. memmove(to, from, tail);
  181. to += tail;
  182. from += tail;
  183. l -= tail;
  184. }
  185. }
  186. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  187. printf(" Loading part %d ... ", part);
  188. memmove((char *) dest, (char *)data, len);
  189. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  190. break;
  191. #ifdef CONFIG_GZIP
  192. case IH_COMP_GZIP:
  193. printf(" Uncompressing part %d ... ", part);
  194. if (gunzip((void *) dest, unc_len,
  195. (uchar *) data, &len) != 0) {
  196. puts("GUNZIP ERROR - image not loaded\n");
  197. return 1;
  198. }
  199. break;
  200. #endif
  201. #if defined(CONFIG_BZIP2)
  202. case IH_COMP_BZIP2:
  203. {
  204. int i;
  205. printf(" Uncompressing part %d ... ", part);
  206. /*
  207. * If we've got less than 4 MB of malloc()
  208. * space, use slower decompression algorithm
  209. * which requires at most 2300 KB of memory.
  210. */
  211. i = BZ2_bzBuffToBuffDecompress(
  212. (char *)ntohl(hdr->ih_load),
  213. &unc_len, (char *)data, len,
  214. CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
  215. 0);
  216. if (i != BZ_OK) {
  217. printf("BUNZIP2 ERROR %d - "
  218. "image not loaded\n", i);
  219. return 1;
  220. }
  221. }
  222. break;
  223. #endif /* CONFIG_BZIP2 */
  224. default:
  225. printf("Unimplemented compression type %d\n", comp);
  226. return 1;
  227. }
  228. puts("OK\n");
  229. }
  230. setenv_hex("fileaddr", data);
  231. setenv_hex("filesize", len);
  232. return 0;
  233. }
  234. #ifdef CONFIG_SYS_LONGHELP
  235. static char imgextract_help_text[] =
  236. "addr part [dest]\n"
  237. " - extract <part> from legacy image at <addr> and copy to <dest>"
  238. #if defined(CONFIG_FIT)
  239. "\n"
  240. "addr uname [dest]\n"
  241. " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
  242. #endif
  243. "";
  244. #endif
  245. U_BOOT_CMD(
  246. imxtract, 4, 1, do_imgextract,
  247. "extract a part of a multi-image", imgextract_help_text
  248. );