ximg.c 5.9 KB

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