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>
23 #if defined(CONFIG_BZIP2)
26 #include <asm/byteorder.h>
29 #ifndef CONFIG_SYS_XIMG_LEN
30 /* use 8MByte as default max gunzip size */
31 #define CONFIG_SYS_XIMG_LEN 0x800000
35 do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
37 ulong addr = image_load_addr;
42 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
44 image_header_t *hdr = NULL;
46 #if defined(CONFIG_FIT)
47 const char *uname = NULL;
54 uint unc_len = CONFIG_SYS_XIMG_LEN;
58 verify = env_get_yesno("verify");
61 addr = simple_strtoul(argv[1], NULL, 16);
64 part = simple_strtoul(argv[2], NULL, 16);
65 #if defined(CONFIG_FIT)
70 dest = simple_strtoul(argv[3], NULL, 16);
73 switch (genimg_get_format((void *)addr)) {
74 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
75 case IMAGE_FORMAT_LEGACY:
77 printf("## Copying part %d from legacy image "
78 "at %08lx ...\n", part, addr);
80 hdr = (image_header_t *)addr;
81 if (!image_check_magic(hdr)) {
82 printf("Bad Magic Number\n");
86 if (!image_check_hcrc(hdr)) {
87 printf("Bad Header Checksum\n");
91 image_print_contents(hdr);
94 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
95 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
96 printf("Wrong Image Type for %s command\n",
101 comp = image_get_comp(hdr);
102 if ((comp != IH_COMP_NONE) && (argc < 4)) {
103 printf("Must specify load address for %s command "
104 "with compressed image\n",
110 printf(" Verifying Checksum ... ");
111 if (!image_check_dcrc(hdr)) {
112 printf("Bad Data CRC\n");
118 count = image_multi_count(hdr);
120 printf("Bad Image Part\n");
124 image_multi_getimg(hdr, part, &data, &len);
127 #if defined(CONFIG_FIT)
128 case IMAGE_FORMAT_FIT:
130 puts("No FIT subimage unit name\n");
134 printf("## Copying '%s' subimage from FIT image "
135 "at %08lx ...\n", uname, addr);
137 fit_hdr = (const void *)addr;
138 if (!fit_check_format(fit_hdr)) {
139 puts("Bad FIT image format\n");
143 /* get subimage node offset */
144 noffset = fit_image_get_node(fit_hdr, uname);
146 printf("Can't find '%s' FIT subimage\n", uname);
150 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
152 printf("Must specify load address for %s command "
153 "with compressed image\n",
158 /* verify integrity */
160 if (!fit_image_verify(fit_hdr, noffset)) {
161 puts("Bad Data Hash\n");
166 /* get subimage/external data address and length */
167 if (fit_image_get_data_and_size(fit_hdr, noffset,
168 &fit_data, &fit_len)) {
169 puts("Could not find script subimage data\n");
173 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
174 puts("Could not find script subimage "
175 "compression type\n");
179 data = (ulong)fit_data;
180 len = (ulong)fit_len;
184 puts("Invalid image type for imxtract\n");
191 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
195 void *to = (void *) dest;
196 void *from = (void *)data;
198 printf(" Loading part %d ... ", part);
201 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
203 memmove(to, from, tail);
209 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
210 printf(" Loading part %d ... ", part);
211 memmove((char *) dest, (char *)data, len);
212 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
216 printf(" Uncompressing part %d ... ", part);
217 if (gunzip((void *) dest, unc_len,
218 (uchar *) data, &len) != 0) {
219 puts("GUNZIP ERROR - image not loaded\n");
224 #if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
229 printf(" Uncompressing part %d ... ", part);
231 * If we've got less than 4 MB of malloc()
232 * space, use slower decompression algorithm
233 * which requires at most 2300 KB of memory.
235 i = BZ2_bzBuffToBuffDecompress(
236 map_sysmem(ntohl(hdr->ih_load), 0),
237 &unc_len, (char *)data, len,
238 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
241 printf("BUNZIP2 ERROR %d - "
242 "image not loaded\n", i);
247 #endif /* CONFIG_BZIP2 */
249 printf("Unimplemented compression type %d\n", comp);
255 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
257 env_set_hex("fileaddr", data);
258 env_set_hex("filesize", len);
263 #ifdef CONFIG_SYS_LONGHELP
264 static char imgextract_help_text[] =
266 " - extract <part> from legacy image at <addr> and copy to <dest>"
267 #if defined(CONFIG_FIT)
269 "addr uname [dest]\n"
270 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
276 imxtract, 4, 1, do_imgextract,
277 "extract a part of a multi-image", imgextract_help_text