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;
35 ulong data, len, count;
38 image_header_t *hdr = NULL;
39 #if defined(CONFIG_FIT)
40 const char *uname = NULL;
47 uint unc_len = CONFIG_SYS_XIMG_LEN;
51 verify = getenv_yesno("verify");
54 addr = simple_strtoul(argv[1], NULL, 16);
57 part = simple_strtoul(argv[2], NULL, 16);
58 #if defined(CONFIG_FIT)
63 dest = simple_strtoul(argv[3], NULL, 16);
66 switch (genimg_get_format((void *)addr)) {
67 case IMAGE_FORMAT_LEGACY:
69 printf("## Copying part %d from legacy image "
70 "at %08lx ...\n", part, addr);
72 hdr = (image_header_t *)addr;
73 if (!image_check_magic(hdr)) {
74 printf("Bad Magic Number\n");
78 if (!image_check_hcrc(hdr)) {
79 printf("Bad Header Checksum\n");
83 image_print_contents(hdr);
86 if (!image_check_type(hdr, IH_TYPE_MULTI)) {
87 printf("Wrong Image Type for %s command\n",
92 comp = image_get_comp(hdr);
93 if ((comp != IH_COMP_NONE) && (argc < 4)) {
94 printf("Must specify load address for %s command "
95 "with compressed image\n",
101 printf(" Verifying Checksum ... ");
102 if (!image_check_dcrc(hdr)) {
103 printf("Bad Data CRC\n");
109 count = image_multi_count(hdr);
111 printf("Bad Image Part\n");
115 image_multi_getimg(hdr, part, &data, &len);
117 #if defined(CONFIG_FIT)
118 case IMAGE_FORMAT_FIT:
120 puts("No FIT subimage unit name\n");
124 printf("## Copying '%s' subimage from FIT image "
125 "at %08lx ...\n", uname, addr);
127 fit_hdr = (const void *)addr;
128 if (!fit_check_format(fit_hdr)) {
129 puts("Bad FIT image format\n");
133 /* get subimage node offset */
134 noffset = fit_image_get_node(fit_hdr, uname);
136 printf("Can't find '%s' FIT subimage\n", uname);
140 if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
142 printf("Must specify load address for %s command "
143 "with compressed image\n",
148 /* verify integrity */
150 if (!fit_image_verify(fit_hdr, noffset)) {
151 puts("Bad Data Hash\n");
156 /* get subimage data address and length */
157 if (fit_image_get_data(fit_hdr, noffset,
158 &fit_data, &fit_len)) {
159 puts("Could not find script subimage data\n");
163 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
164 puts("Could not find script subimage "
165 "compression type\n");
169 data = (ulong)fit_data;
170 len = (ulong)fit_len;
174 puts("Invalid image type for imxtract\n");
181 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
185 void *to = (void *) dest;
186 void *from = (void *)data;
188 printf(" Loading part %d ... ", part);
191 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
193 memmove(to, from, tail);
199 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
200 printf(" Loading part %d ... ", part);
201 memmove((char *) dest, (char *)data, len);
202 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
206 printf(" Uncompressing part %d ... ", part);
207 if (gunzip((void *) dest, unc_len,
208 (uchar *) data, &len) != 0) {
209 puts("GUNZIP ERROR - image not loaded\n");
214 #if defined(CONFIG_BZIP2)
219 printf(" Uncompressing part %d ... ", part);
221 * If we've got less than 4 MB of malloc()
222 * space, use slower decompression algorithm
223 * which requires at most 2300 KB of memory.
225 i = BZ2_bzBuffToBuffDecompress(
226 map_sysmem(ntohl(hdr->ih_load), 0),
227 &unc_len, (char *)data, len,
228 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
231 printf("BUNZIP2 ERROR %d - "
232 "image not loaded\n", i);
237 #endif /* CONFIG_BZIP2 */
239 printf("Unimplemented compression type %d\n", comp);
245 setenv_hex("fileaddr", data);
246 setenv_hex("filesize", len);
251 #ifdef CONFIG_SYS_LONGHELP
252 static char imgextract_help_text[] =
254 " - extract <part> from legacy image at <addr> and copy to <dest>"
255 #if defined(CONFIG_FIT)
257 "addr uname [dest]\n"
258 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
264 imxtract, 4, 1, do_imgextract,
265 "extract a part of a multi-image", imgextract_help_text