ximg.c 6.0 KB

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