cmd_ximg.c 6.2 KB

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