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+
19 #if defined(CONFIG_BZIP2)
22 #include <asm/byteorder.h>
25 #ifndef CONFIG_SYS_XIMG_LEN
26 /* use 8MByte as default max gunzip size */
27 #define CONFIG_SYS_XIMG_LEN 0x800000
31 do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
33 ulong addr = load_addr;
38 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
40 image_header_t *hdr = NULL;
42 #if defined(CONFIG_FIT)
43 const char *uname = NULL;
50 uint unc_len = CONFIG_SYS_XIMG_LEN;
54 verify = getenv_yesno("verify");
57 addr = simple_strtoul(argv[1], NULL, 16);
60 part = simple_strtoul(argv[2], NULL, 16);
61 #if defined(CONFIG_FIT)
66 dest = simple_strtoul(argv[3], NULL, 16);
69 switch (genimg_get_format((void *)addr)) {
70 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
71 case IMAGE_FORMAT_LEGACY:
73 printf("## Copying part %d from legacy image "
74 "at %08lx ...\n", part, addr);
76 hdr = (image_header_t *)addr;
77 if (!image_check_magic(hdr)) {
78 printf("Bad Magic Number\n");
82 if (!image_check_hcrc(hdr)) {
83 printf("Bad Header Checksum\n");
87 image_print_contents(hdr);
90 if (!image_check_type(hdr, IH_TYPE_MULTI)) {
91 printf("Wrong Image Type for %s command\n",
96 comp = image_get_comp(hdr);
97 if ((comp != IH_COMP_NONE) && (argc < 4)) {
98 printf("Must specify load address for %s command "
99 "with compressed image\n",
105 printf(" Verifying Checksum ... ");
106 if (!image_check_dcrc(hdr)) {
107 printf("Bad Data CRC\n");
113 count = image_multi_count(hdr);
115 printf("Bad Image Part\n");
119 image_multi_getimg(hdr, part, &data, &len);
122 #if defined(CONFIG_FIT)
123 case IMAGE_FORMAT_FIT:
125 puts("No FIT subimage unit name\n");
129 printf("## Copying '%s' subimage from FIT image "
130 "at %08lx ...\n", uname, addr);
132 fit_hdr = (const void *)addr;
133 if (!fit_check_format(fit_hdr)) {
134 puts("Bad FIT image format\n");
138 /* get subimage node offset */
139 noffset = fit_image_get_node(fit_hdr, uname);
141 printf("Can't find '%s' FIT subimage\n", uname);
145 if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
147 printf("Must specify load address for %s command "
148 "with compressed image\n",
153 /* verify integrity */
155 if (!fit_image_verify(fit_hdr, noffset)) {
156 puts("Bad Data Hash\n");
161 /* get subimage data address and length */
162 if (fit_image_get_data(fit_hdr, noffset,
163 &fit_data, &fit_len)) {
164 puts("Could not find script subimage data\n");
168 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
169 puts("Could not find script subimage "
170 "compression type\n");
174 data = (ulong)fit_data;
175 len = (ulong)fit_len;
179 puts("Invalid image type for imxtract\n");
186 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
190 void *to = (void *) dest;
191 void *from = (void *)data;
193 printf(" Loading part %d ... ", part);
196 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
198 memmove(to, from, tail);
204 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
205 printf(" Loading part %d ... ", part);
206 memmove((char *) dest, (char *)data, len);
207 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
211 printf(" Uncompressing part %d ... ", part);
212 if (gunzip((void *) dest, unc_len,
213 (uchar *) data, &len) != 0) {
214 puts("GUNZIP ERROR - image not loaded\n");
219 #if defined(CONFIG_BZIP2) && defined(CONFIG_IMAGE_FORMAT_LEGACY)
224 printf(" Uncompressing part %d ... ", part);
226 * If we've got less than 4 MB of malloc()
227 * space, use slower decompression algorithm
228 * which requires at most 2300 KB of memory.
230 i = BZ2_bzBuffToBuffDecompress(
231 map_sysmem(ntohl(hdr->ih_load), 0),
232 &unc_len, (char *)data, len,
233 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
236 printf("BUNZIP2 ERROR %d - "
237 "image not loaded\n", i);
242 #endif /* CONFIG_BZIP2 */
244 printf("Unimplemented compression type %d\n", comp);
250 flush_cache(dest, len);
252 setenv_hex("fileaddr", data);
253 setenv_hex("filesize", len);
258 #ifdef CONFIG_SYS_LONGHELP
259 static char imgextract_help_text[] =
261 " - extract <part> from legacy image at <addr> and copy to <dest>"
262 #if defined(CONFIG_FIT)
264 "addr uname [dest]\n"
265 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
271 imxtract, 4, 1, do_imgextract,
272 "extract a part of a multi-image", imgextract_help_text