2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
8 * SPDX-License-Identifier: GPL-2.0+
20 #if defined(CONFIG_BZIP2)
23 #include <asm/byteorder.h>
26 #ifndef CONFIG_SYS_XIMG_LEN
27 /* use 8MByte as default max gunzip size */
28 #define CONFIG_SYS_XIMG_LEN 0x800000
32 do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
34 ulong addr = load_addr;
39 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
41 image_header_t *hdr = NULL;
43 #if defined(CONFIG_FIT)
44 const char *uname = NULL;
51 uint unc_len = CONFIG_SYS_XIMG_LEN;
55 verify = getenv_yesno("verify");
58 addr = simple_strtoul(argv[1], NULL, 16);
61 part = simple_strtoul(argv[2], NULL, 16);
62 #if defined(CONFIG_FIT)
67 dest = simple_strtoul(argv[3], NULL, 16);
70 switch (genimg_get_format((void *)addr)) {
71 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
72 case IMAGE_FORMAT_LEGACY:
74 printf("## Copying part %d from legacy image "
75 "at %08lx ...\n", part, addr);
77 hdr = (image_header_t *)addr;
78 if (!image_check_magic(hdr)) {
79 printf("Bad Magic Number\n");
83 if (!image_check_hcrc(hdr)) {
84 printf("Bad Header Checksum\n");
88 image_print_contents(hdr);
91 if (!image_check_type(hdr, IH_TYPE_MULTI)) {
92 printf("Wrong Image Type for %s command\n",
97 comp = image_get_comp(hdr);
98 if ((comp != IH_COMP_NONE) && (argc < 4)) {
99 printf("Must specify load address for %s command "
100 "with compressed image\n",
106 printf(" Verifying Checksum ... ");
107 if (!image_check_dcrc(hdr)) {
108 printf("Bad Data CRC\n");
114 count = image_multi_count(hdr);
116 printf("Bad Image Part\n");
120 image_multi_getimg(hdr, part, &data, &len);
123 #if defined(CONFIG_FIT)
124 case IMAGE_FORMAT_FIT:
126 puts("No FIT subimage unit name\n");
130 printf("## Copying '%s' subimage from FIT image "
131 "at %08lx ...\n", uname, addr);
133 fit_hdr = (const void *)addr;
134 if (!fit_check_format(fit_hdr)) {
135 puts("Bad FIT image format\n");
139 /* get subimage node offset */
140 noffset = fit_image_get_node(fit_hdr, uname);
142 printf("Can't find '%s' FIT subimage\n", uname);
146 if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
148 printf("Must specify load address for %s command "
149 "with compressed image\n",
154 /* verify integrity */
156 if (!fit_image_verify(fit_hdr, noffset)) {
157 puts("Bad Data Hash\n");
162 /* get subimage data address and length */
163 if (fit_image_get_data(fit_hdr, noffset,
164 &fit_data, &fit_len)) {
165 puts("Could not find script subimage data\n");
169 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
170 puts("Could not find script subimage "
171 "compression type\n");
175 data = (ulong)fit_data;
176 len = (ulong)fit_len;
180 puts("Invalid image type for imxtract\n");
187 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
191 void *to = (void *) dest;
192 void *from = (void *)data;
194 printf(" Loading part %d ... ", part);
197 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
199 memmove(to, from, tail);
205 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
206 printf(" Loading part %d ... ", part);
207 memmove((char *) dest, (char *)data, len);
208 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
212 printf(" Uncompressing part %d ... ", part);
213 if (gunzip((void *) dest, unc_len,
214 (uchar *) data, &len) != 0) {
215 puts("GUNZIP ERROR - image not loaded\n");
220 #if defined(CONFIG_BZIP2) && defined(CONFIG_IMAGE_FORMAT_LEGACY)
225 printf(" Uncompressing part %d ... ", part);
227 * If we've got less than 4 MB of malloc()
228 * space, use slower decompression algorithm
229 * which requires at most 2300 KB of memory.
231 i = BZ2_bzBuffToBuffDecompress(
232 map_sysmem(ntohl(hdr->ih_load), 0),
233 &unc_len, (char *)data, len,
234 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
237 printf("BUNZIP2 ERROR %d - "
238 "image not loaded\n", i);
243 #endif /* CONFIG_BZIP2 */
245 printf("Unimplemented compression type %d\n", comp);
251 flush_cache(dest, len);
253 setenv_hex("fileaddr", data);
254 setenv_hex("filesize", len);
259 #ifdef CONFIG_SYS_LONGHELP
260 static char imgextract_help_text[] =
262 " - extract <part> from legacy image at <addr> and copy to <dest>"
263 #if defined(CONFIG_FIT)
265 "addr uname [dest]\n"
266 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
272 imxtract, 4, 1, do_imgextract,
273 "extract a part of a multi-image", imgextract_help_text