ximg.c 5.9 KB

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