ximg.c 5.8 KB

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