cmd_ximg.c 5.6 KB

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