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 = env_get_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 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
93 printf("Wrong Image Type for %s command\n",
98 comp = image_get_comp(hdr);
99 if ((comp != IH_COMP_NONE) && (argc < 4)) {
100 printf("Must specify load address for %s command "
101 "with compressed image\n",
107 printf(" Verifying Checksum ... ");
108 if (!image_check_dcrc(hdr)) {
109 printf("Bad Data CRC\n");
115 count = image_multi_count(hdr);
117 printf("Bad Image Part\n");
121 image_multi_getimg(hdr, part, &data, &len);
124 #if defined(CONFIG_FIT)
125 case IMAGE_FORMAT_FIT:
127 puts("No FIT subimage unit name\n");
131 printf("## Copying '%s' subimage from FIT image "
132 "at %08lx ...\n", uname, addr);
134 fit_hdr = (const void *)addr;
135 if (!fit_check_format(fit_hdr)) {
136 puts("Bad FIT image format\n");
140 /* get subimage node offset */
141 noffset = fit_image_get_node(fit_hdr, uname);
143 printf("Can't find '%s' FIT subimage\n", uname);
147 if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
149 printf("Must specify load address for %s command "
150 "with compressed image\n",
155 /* verify integrity */
157 if (!fit_image_verify(fit_hdr, noffset)) {
158 puts("Bad Data Hash\n");
163 /* get subimage data address and length */
164 if (fit_image_get_data(fit_hdr, noffset,
165 &fit_data, &fit_len)) {
166 puts("Could not find script subimage data\n");
170 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
171 puts("Could not find script subimage "
172 "compression type\n");
176 data = (ulong)fit_data;
177 len = (ulong)fit_len;
181 puts("Invalid image type for imxtract\n");
188 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
192 void *to = (void *) dest;
193 void *from = (void *)data;
195 printf(" Loading part %d ... ", part);
198 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
200 memmove(to, from, tail);
206 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
207 printf(" Loading part %d ... ", part);
208 memmove((char *) dest, (char *)data, len);
209 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
213 printf(" Uncompressing part %d ... ", part);
214 if (gunzip((void *) dest, unc_len,
215 (uchar *) data, &len) != 0) {
216 puts("GUNZIP ERROR - image not loaded\n");
221 #if defined(CONFIG_BZIP2) && defined(CONFIG_IMAGE_FORMAT_LEGACY)
226 printf(" Uncompressing part %d ... ", part);
228 * If we've got less than 4 MB of malloc()
229 * space, use slower decompression algorithm
230 * which requires at most 2300 KB of memory.
232 i = BZ2_bzBuffToBuffDecompress(
233 map_sysmem(ntohl(hdr->ih_load), 0),
234 &unc_len, (char *)data, len,
235 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
238 printf("BUNZIP2 ERROR %d - "
239 "image not loaded\n", i);
244 #endif /* CONFIG_BZIP2 */
246 printf("Unimplemented compression type %d\n", comp);
252 flush_cache(dest, len);
254 env_set_hex("fileaddr", data);
255 env_set_hex("filesize", len);
260 #ifdef CONFIG_SYS_LONGHELP
261 static char imgextract_help_text[] =
263 " - extract <part> from legacy image at <addr> and copy to <dest>"
264 #if defined(CONFIG_FIT)
266 "addr uname [dest]\n"
267 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
273 imxtract, 4, 1, do_imgextract,
274 "extract a part of a multi-image", imgextract_help_text