cmd_ximg.c 5.8 KB

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