ximg.c 5.9 KB

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