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 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
35 #if defined(CONFIG_BZIP2)
38 #include <asm/byteorder.h>
40 #ifndef CONFIG_SYS_XIMG_LEN
41 /* use 8MByte as default max gunzip size */
42 #define CONFIG_SYS_XIMG_LEN 0x800000
46 do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
48 ulong addr = load_addr;
50 ulong data, len, count;
54 #if defined(CONFIG_FIT)
55 const char *uname = NULL;
62 uint unc_len = CONFIG_SYS_XIMG_LEN;
66 verify = getenv_yesno("verify");
69 addr = simple_strtoul(argv[1], NULL, 16);
72 part = simple_strtoul(argv[2], NULL, 16);
73 #if defined(CONFIG_FIT)
78 dest = simple_strtoul(argv[3], NULL, 16);
81 switch (genimg_get_format((void *)addr)) {
82 case IMAGE_FORMAT_LEGACY:
84 printf("## Copying part %d from legacy image "
85 "at %08lx ...\n", part, addr);
87 hdr = (image_header_t *)addr;
88 if (!image_check_magic(hdr)) {
89 printf("Bad Magic Number\n");
93 if (!image_check_hcrc(hdr)) {
94 printf("Bad Header Checksum\n");
98 image_print_contents(hdr);
101 if (!image_check_type(hdr, IH_TYPE_MULTI)) {
102 printf("Wrong Image Type for %s command\n",
107 comp = image_get_comp(hdr);
108 if ((comp != IH_COMP_NONE) && (argc < 4)) {
109 printf("Must specify load address for %s command "
110 "with compressed image\n",
116 printf(" Verifying Checksum ... ");
117 if (!image_check_dcrc(hdr)) {
118 printf("Bad Data CRC\n");
124 count = image_multi_count(hdr);
126 printf("Bad Image Part\n");
130 image_multi_getimg(hdr, part, &data, &len);
132 #if defined(CONFIG_FIT)
133 case IMAGE_FORMAT_FIT:
135 puts("No FIT subimage unit name\n");
139 printf("## Copying '%s' subimage from FIT image "
140 "at %08lx ...\n", uname, addr);
142 fit_hdr = (const void *)addr;
143 if (!fit_check_format(fit_hdr)) {
144 puts("Bad FIT image format\n");
148 /* get subimage node offset */
149 noffset = fit_image_get_node(fit_hdr, uname);
151 printf("Can't find '%s' FIT subimage\n", uname);
155 if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
157 printf("Must specify load address for %s command "
158 "with compressed image\n",
163 /* verify integrity */
165 if (!fit_image_verify(fit_hdr, noffset)) {
166 puts("Bad Data Hash\n");
171 /* get subimage data address and length */
172 if (fit_image_get_data(fit_hdr, noffset,
173 &fit_data, &fit_len)) {
174 puts("Could not find script subimage data\n");
178 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
179 puts("Could not find script subimage "
180 "compression type\n");
184 data = (ulong)fit_data;
185 len = (ulong)fit_len;
189 puts("Invalid image type for imxtract\n");
196 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
200 void *to = (void *) dest;
201 void *from = (void *)data;
203 printf(" Loading part %d ... ", part);
206 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
208 memmove(to, from, tail);
214 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
215 printf(" Loading part %d ... ", part);
216 memmove((char *) dest, (char *)data, len);
217 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
221 printf(" Uncompressing part %d ... ", part);
222 if (gunzip((void *) dest, unc_len,
223 (uchar *) data, &len) != 0) {
224 puts("GUNZIP ERROR - image not loaded\n");
229 #if defined(CONFIG_BZIP2)
234 printf(" Uncompressing part %d ... ", part);
236 * If we've got less than 4 MB of malloc()
237 * space, use slower decompression algorithm
238 * which requires at most 2300 KB of memory.
240 i = BZ2_bzBuffToBuffDecompress(
241 (char *)ntohl(hdr->ih_load),
242 &unc_len, (char *)data, len,
243 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
246 printf("BUNZIP2 ERROR %d - "
247 "image not loaded\n", i);
252 #endif /* CONFIG_BZIP2 */
254 printf("Unimplemented compression type %d\n", comp);
260 setenv_hex("fileaddr", data);
261 setenv_hex("filesize", len);
266 #ifdef CONFIG_SYS_LONGHELP
267 static char imgextract_help_text[] =
269 " - extract <part> from legacy image at <addr> and copy to <dest>"
270 #if defined(CONFIG_FIT)
272 "addr uname [dest]\n"
273 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
279 imxtract, 4, 1, do_imgextract,
280 "extract a part of a multi-image", imgextract_help_text