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>
27 #include <asm/cache.h>
30 #ifndef CONFIG_SYS_XIMG_LEN
31 /* use 8MByte as default max gunzip size */
32 #define CONFIG_SYS_XIMG_LEN 0x800000
36 do_imgextract(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
38 ulong addr = image_load_addr;
43 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
45 image_header_t *hdr = NULL;
47 #if defined(CONFIG_FIT)
48 const char *uname = NULL;
55 uint unc_len = CONFIG_SYS_XIMG_LEN;
59 verify = env_get_yesno("verify");
62 addr = hextoul(argv[1], NULL);
65 part = hextoul(argv[2], NULL);
66 #if defined(CONFIG_FIT)
71 dest = hextoul(argv[3], NULL);
74 switch (genimg_get_format((void *)addr)) {
75 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
76 case IMAGE_FORMAT_LEGACY:
78 printf("## Copying part %d from legacy image "
79 "at %08lx ...\n", part, addr);
81 hdr = (image_header_t *)addr;
82 if (!image_check_magic(hdr)) {
83 printf("Bad Magic Number\n");
87 if (!image_check_hcrc(hdr)) {
88 printf("Bad Header Checksum\n");
92 image_print_contents(hdr);
95 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
96 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
97 printf("Wrong Image Type for %s command\n",
102 comp = image_get_comp(hdr);
103 if ((comp != IH_COMP_NONE) && (argc < 4)) {
104 printf("Must specify load address for %s command "
105 "with compressed image\n",
111 printf(" Verifying Checksum ... ");
112 if (!image_check_dcrc(hdr)) {
113 printf("Bad Data CRC\n");
119 count = image_multi_count(hdr);
121 printf("Bad Image Part\n");
125 image_multi_getimg(hdr, part, &data, &len);
128 #if defined(CONFIG_FIT)
129 case IMAGE_FORMAT_FIT:
131 puts("No FIT subimage unit name\n");
135 printf("## Copying '%s' subimage from FIT image "
136 "at %08lx ...\n", uname, addr);
138 fit_hdr = (const void *)addr;
139 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
140 puts("Bad FIT image format\n");
144 /* get subimage node offset */
145 noffset = fit_image_get_node(fit_hdr, uname);
147 printf("Can't find '%s' FIT subimage\n", uname);
151 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
153 printf("Must specify load address for %s command "
154 "with compressed image\n",
159 /* verify integrity */
161 if (!fit_image_verify(fit_hdr, noffset)) {
162 puts("Bad Data Hash\n");
167 /* get subimage/external data address and length */
168 if (fit_image_get_data_and_size(fit_hdr, noffset,
169 &fit_data, &fit_len)) {
170 puts("Could not find script subimage data\n");
174 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
175 puts("Could not find script subimage "
176 "compression type\n");
180 data = (ulong)fit_data;
181 len = (ulong)fit_len;
185 puts("Invalid image type for imxtract\n");
192 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
196 void *to = (void *) dest;
197 void *from = (void *)data;
199 printf(" Loading part %d ... ", part);
202 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
204 memmove(to, from, tail);
210 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
211 printf(" Loading part %d ... ", part);
212 memmove((char *) dest, (char *)data, len);
213 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
217 printf(" Uncompressing part %d ... ", part);
218 if (gunzip((void *) dest, unc_len,
219 (uchar *) data, &len) != 0) {
220 puts("GUNZIP ERROR - image not loaded\n");
225 #if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
230 printf(" Uncompressing part %d ... ", part);
232 * If we've got less than 4 MB of malloc()
233 * space, use slower decompression algorithm
234 * which requires at most 2300 KB of memory.
236 i = BZ2_bzBuffToBuffDecompress(
237 map_sysmem(ntohl(hdr->ih_load), 0),
238 &unc_len, (char *)data, len,
239 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
242 printf("BUNZIP2 ERROR %d - "
243 "image not loaded\n", i);
248 #endif /* CONFIG_BZIP2 */
250 printf("Unimplemented compression type %d\n", comp);
256 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
258 env_set_hex("fileaddr", data);
259 env_set_hex("filesize", len);
264 #ifdef CONFIG_SYS_LONGHELP
265 static char imgextract_help_text[] =
267 " - extract <part> from legacy image at <addr> and copy to <dest>"
268 #if defined(CONFIG_FIT)
270 "addr uname [dest]\n"
271 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
277 imxtract, 4, 1, do_imgextract,
278 "extract a part of a multi-image", imgextract_help_text