1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2004
22 #if defined(CONFIG_BZIP2)
25 #include <asm/byteorder.h>
28 #ifndef CONFIG_SYS_XIMG_LEN
29 /* use 8MByte as default max gunzip size */
30 #define CONFIG_SYS_XIMG_LEN 0x800000
34 do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
36 ulong addr = load_addr;
41 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
43 image_header_t *hdr = NULL;
45 #if defined(CONFIG_FIT)
46 const char *uname = NULL;
53 uint unc_len = CONFIG_SYS_XIMG_LEN;
57 verify = env_get_yesno("verify");
60 addr = simple_strtoul(argv[1], NULL, 16);
63 part = simple_strtoul(argv[2], NULL, 16);
64 #if defined(CONFIG_FIT)
69 dest = simple_strtoul(argv[3], NULL, 16);
72 switch (genimg_get_format((void *)addr)) {
73 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
74 case IMAGE_FORMAT_LEGACY:
76 printf("## Copying part %d from legacy image "
77 "at %08lx ...\n", part, addr);
79 hdr = (image_header_t *)addr;
80 if (!image_check_magic(hdr)) {
81 printf("Bad Magic Number\n");
85 if (!image_check_hcrc(hdr)) {
86 printf("Bad Header Checksum\n");
90 image_print_contents(hdr);
93 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
94 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
95 printf("Wrong Image Type for %s command\n",
100 comp = image_get_comp(hdr);
101 if ((comp != IH_COMP_NONE) && (argc < 4)) {
102 printf("Must specify load address for %s command "
103 "with compressed image\n",
109 printf(" Verifying Checksum ... ");
110 if (!image_check_dcrc(hdr)) {
111 printf("Bad Data CRC\n");
117 count = image_multi_count(hdr);
119 printf("Bad Image Part\n");
123 image_multi_getimg(hdr, part, &data, &len);
126 #if defined(CONFIG_FIT)
127 case IMAGE_FORMAT_FIT:
129 puts("No FIT subimage unit name\n");
133 printf("## Copying '%s' subimage from FIT image "
134 "at %08lx ...\n", uname, addr);
136 fit_hdr = (const void *)addr;
137 if (!fit_check_format(fit_hdr)) {
138 puts("Bad FIT image format\n");
142 /* get subimage node offset */
143 noffset = fit_image_get_node(fit_hdr, uname);
145 printf("Can't find '%s' FIT subimage\n", uname);
149 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
151 printf("Must specify load address for %s command "
152 "with compressed image\n",
157 /* verify integrity */
159 if (!fit_image_verify(fit_hdr, noffset)) {
160 puts("Bad Data Hash\n");
165 /* get subimage/external data address and length */
166 if (fit_image_get_data_and_size(fit_hdr, noffset,
167 &fit_data, &fit_len)) {
168 puts("Could not find script subimage data\n");
172 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
173 puts("Could not find script subimage "
174 "compression type\n");
178 data = (ulong)fit_data;
179 len = (ulong)fit_len;
183 puts("Invalid image type for imxtract\n");
190 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
194 void *to = (void *) dest;
195 void *from = (void *)data;
197 printf(" Loading part %d ... ", part);
200 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
202 memmove(to, from, tail);
208 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
209 printf(" Loading part %d ... ", part);
210 memmove((char *) dest, (char *)data, len);
211 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
215 printf(" Uncompressing part %d ... ", part);
216 if (gunzip((void *) dest, unc_len,
217 (uchar *) data, &len) != 0) {
218 puts("GUNZIP ERROR - image not loaded\n");
223 #if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
228 printf(" Uncompressing part %d ... ", part);
230 * If we've got less than 4 MB of malloc()
231 * space, use slower decompression algorithm
232 * which requires at most 2300 KB of memory.
234 i = BZ2_bzBuffToBuffDecompress(
235 map_sysmem(ntohl(hdr->ih_load), 0),
236 &unc_len, (char *)data, len,
237 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
240 printf("BUNZIP2 ERROR %d - "
241 "image not loaded\n", i);
246 #endif /* CONFIG_BZIP2 */
248 printf("Unimplemented compression type %d\n", comp);
254 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
256 env_set_hex("fileaddr", data);
257 env_set_hex("filesize", len);
262 #ifdef CONFIG_SYS_LONGHELP
263 static char imgextract_help_text[] =
265 " - extract <part> from legacy image at <addr> and copy to <dest>"
266 #if defined(CONFIG_FIT)
268 "addr uname [dest]\n"
269 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
275 imxtract, 4, 1, do_imgextract,
276 "extract a part of a multi-image", imgextract_help_text