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>
24 #ifndef CONFIG_SYS_XIMG_LEN
25 /* use 8MByte as default max gunzip size */
26 #define CONFIG_SYS_XIMG_LEN 0x800000
30 do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
32 ulong addr = load_addr;
34 ulong data, len, count;
38 #if defined(CONFIG_FIT)
39 const char *uname = NULL;
46 uint unc_len = CONFIG_SYS_XIMG_LEN;
50 verify = getenv_yesno("verify");
53 addr = simple_strtoul(argv[1], NULL, 16);
56 part = simple_strtoul(argv[2], NULL, 16);
57 #if defined(CONFIG_FIT)
62 dest = simple_strtoul(argv[3], NULL, 16);
65 switch (genimg_get_format((void *)addr)) {
66 case IMAGE_FORMAT_LEGACY:
68 printf("## Copying part %d from legacy image "
69 "at %08lx ...\n", part, addr);
71 hdr = (image_header_t *)addr;
72 if (!image_check_magic(hdr)) {
73 printf("Bad Magic Number\n");
77 if (!image_check_hcrc(hdr)) {
78 printf("Bad Header Checksum\n");
82 image_print_contents(hdr);
85 if (!image_check_type(hdr, IH_TYPE_MULTI)) {
86 printf("Wrong Image Type for %s command\n",
91 comp = image_get_comp(hdr);
92 if ((comp != IH_COMP_NONE) && (argc < 4)) {
93 printf("Must specify load address for %s command "
94 "with compressed image\n",
100 printf(" Verifying Checksum ... ");
101 if (!image_check_dcrc(hdr)) {
102 printf("Bad Data CRC\n");
108 count = image_multi_count(hdr);
110 printf("Bad Image Part\n");
114 image_multi_getimg(hdr, part, &data, &len);
116 #if defined(CONFIG_FIT)
117 case IMAGE_FORMAT_FIT:
119 puts("No FIT subimage unit name\n");
123 printf("## Copying '%s' subimage from FIT image "
124 "at %08lx ...\n", uname, addr);
126 fit_hdr = (const void *)addr;
127 if (!fit_check_format(fit_hdr)) {
128 puts("Bad FIT image format\n");
132 /* get subimage node offset */
133 noffset = fit_image_get_node(fit_hdr, uname);
135 printf("Can't find '%s' FIT subimage\n", uname);
139 if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
141 printf("Must specify load address for %s command "
142 "with compressed image\n",
147 /* verify integrity */
149 if (!fit_image_verify(fit_hdr, noffset)) {
150 puts("Bad Data Hash\n");
155 /* get subimage data address and length */
156 if (fit_image_get_data(fit_hdr, noffset,
157 &fit_data, &fit_len)) {
158 puts("Could not find script subimage data\n");
162 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
163 puts("Could not find script subimage "
164 "compression type\n");
168 data = (ulong)fit_data;
169 len = (ulong)fit_len;
173 puts("Invalid image type for imxtract\n");
180 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
184 void *to = (void *) dest;
185 void *from = (void *)data;
187 printf(" Loading part %d ... ", part);
190 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
192 memmove(to, from, tail);
198 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
199 printf(" Loading part %d ... ", part);
200 memmove((char *) dest, (char *)data, len);
201 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
205 printf(" Uncompressing part %d ... ", part);
206 if (gunzip((void *) dest, unc_len,
207 (uchar *) data, &len) != 0) {
208 puts("GUNZIP ERROR - image not loaded\n");
213 #if defined(CONFIG_BZIP2)
218 printf(" Uncompressing part %d ... ", part);
220 * If we've got less than 4 MB of malloc()
221 * space, use slower decompression algorithm
222 * which requires at most 2300 KB of memory.
224 i = BZ2_bzBuffToBuffDecompress(
225 (char *)ntohl(hdr->ih_load),
226 &unc_len, (char *)data, len,
227 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
230 printf("BUNZIP2 ERROR %d - "
231 "image not loaded\n", i);
236 #endif /* CONFIG_BZIP2 */
238 printf("Unimplemented compression type %d\n", comp);
244 setenv_hex("fileaddr", data);
245 setenv_hex("filesize", len);
250 #ifdef CONFIG_SYS_LONGHELP
251 static char imgextract_help_text[] =
253 " - extract <part> from legacy image at <addr> and copy to <dest>"
254 #if defined(CONFIG_FIT)
256 "addr uname [dest]\n"
257 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
263 imxtract, 4, 1, do_imgextract,
264 "extract a part of a multi-image", imgextract_help_text