ximg.c 6.0 KB

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