1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
21 #if defined(CONFIG_BZIP2)
24 #include <asm/byteorder.h>
27 #ifndef CONFIG_SYS_XIMG_LEN
28 /* use 8MByte as default max gunzip size */
29 #define CONFIG_SYS_XIMG_LEN 0x800000
33 do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
35 ulong addr = load_addr;
40 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
42 image_header_t *hdr = NULL;
44 #if defined(CONFIG_FIT)
45 const char *uname = NULL;
52 uint unc_len = CONFIG_SYS_XIMG_LEN;
56 verify = env_get_yesno("verify");
59 addr = simple_strtoul(argv[1], NULL, 16);
62 part = simple_strtoul(argv[2], NULL, 16);
63 #if defined(CONFIG_FIT)
68 dest = simple_strtoul(argv[3], NULL, 16);
71 switch (genimg_get_format((void *)addr)) {
72 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
73 case IMAGE_FORMAT_LEGACY:
75 printf("## Copying part %d from legacy image "
76 "at %08lx ...\n", part, addr);
78 hdr = (image_header_t *)addr;
79 if (!image_check_magic(hdr)) {
80 printf("Bad Magic Number\n");
84 if (!image_check_hcrc(hdr)) {
85 printf("Bad Header Checksum\n");
89 image_print_contents(hdr);
92 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
93 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
94 printf("Wrong Image Type for %s command\n",
99 comp = image_get_comp(hdr);
100 if ((comp != IH_COMP_NONE) && (argc < 4)) {
101 printf("Must specify load address for %s command "
102 "with compressed image\n",
108 printf(" Verifying Checksum ... ");
109 if (!image_check_dcrc(hdr)) {
110 printf("Bad Data CRC\n");
116 count = image_multi_count(hdr);
118 printf("Bad Image Part\n");
122 image_multi_getimg(hdr, part, &data, &len);
125 #if defined(CONFIG_FIT)
126 case IMAGE_FORMAT_FIT:
128 puts("No FIT subimage unit name\n");
132 printf("## Copying '%s' subimage from FIT image "
133 "at %08lx ...\n", uname, addr);
135 fit_hdr = (const void *)addr;
136 if (!fit_check_format(fit_hdr)) {
137 puts("Bad FIT image format\n");
141 /* get subimage node offset */
142 noffset = fit_image_get_node(fit_hdr, uname);
144 printf("Can't find '%s' FIT subimage\n", uname);
148 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
150 printf("Must specify load address for %s command "
151 "with compressed image\n",
156 /* verify integrity */
158 if (!fit_image_verify(fit_hdr, noffset)) {
159 puts("Bad Data Hash\n");
164 /* get subimage/external data address and length */
165 if (fit_image_get_data_and_size(fit_hdr, noffset,
166 &fit_data, &fit_len)) {
167 puts("Could not find script subimage data\n");
171 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
172 puts("Could not find script subimage "
173 "compression type\n");
177 data = (ulong)fit_data;
178 len = (ulong)fit_len;
182 puts("Invalid image type for imxtract\n");
189 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
193 void *to = (void *) dest;
194 void *from = (void *)data;
196 printf(" Loading part %d ... ", part);
199 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
201 memmove(to, from, tail);
207 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
208 printf(" Loading part %d ... ", part);
209 memmove((char *) dest, (char *)data, len);
210 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
214 printf(" Uncompressing part %d ... ", part);
215 if (gunzip((void *) dest, unc_len,
216 (uchar *) data, &len) != 0) {
217 puts("GUNZIP ERROR - image not loaded\n");
222 #if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
227 printf(" Uncompressing part %d ... ", part);
229 * If we've got less than 4 MB of malloc()
230 * space, use slower decompression algorithm
231 * which requires at most 2300 KB of memory.
233 i = BZ2_bzBuffToBuffDecompress(
234 map_sysmem(ntohl(hdr->ih_load), 0),
235 &unc_len, (char *)data, len,
236 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
239 printf("BUNZIP2 ERROR %d - "
240 "image not loaded\n", i);
245 #endif /* CONFIG_BZIP2 */
247 printf("Unimplemented compression type %d\n", comp);
253 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
255 env_set_hex("fileaddr", data);
256 env_set_hex("filesize", len);
261 #ifdef CONFIG_SYS_LONGHELP
262 static char imgextract_help_text[] =
264 " - extract <part> from legacy image at <addr> and copy to <dest>"
265 #if defined(CONFIG_FIT)
267 "addr uname [dest]\n"
268 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
274 imxtract, 4, 1, do_imgextract,
275 "extract a part of a multi-image", imgextract_help_text