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 <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 CFG_SYS_XIMG_LEN
  28. /* use 8MByte as default max gunzip size */
  29. #define CFG_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. struct legacy_img_hdr *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 = CFG_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 = (struct legacy_img_hdr *)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. comp = IH_COMP_NONE;
  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. schedule();
  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. );