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;
61 uint unc_len = CONFIG_SYS_XIMG_LEN;
64 verify = getenv_yesno("verify");
67 addr = simple_strtoul(argv[1], NULL, 16);
70 part = simple_strtoul(argv[2], NULL, 16);
71 #if defined(CONFIG_FIT)
76 dest = simple_strtoul(argv[3], NULL, 16);
79 switch (genimg_get_format((void *)addr)) {
80 case IMAGE_FORMAT_LEGACY:
82 printf("## Copying part %d from legacy image "
83 "at %08lx ...\n", part, addr);
85 hdr = (image_header_t *)addr;
86 if (!image_check_magic(hdr)) {
87 printf("Bad Magic Number\n");
91 if (!image_check_hcrc(hdr)) {
92 printf("Bad Header Checksum\n");
96 image_print_contents(hdr);
99 if (!image_check_type(hdr, IH_TYPE_MULTI)) {
100 printf("Wrong Image Type for %s command\n",
105 comp = image_get_comp(hdr);
106 if ((comp != IH_COMP_NONE) && (argc < 4)) {
107 printf("Must specify load address for %s command "
108 "with compressed image\n",
114 printf(" Verifying Checksum ... ");
115 if (!image_check_dcrc(hdr)) {
116 printf("Bad Data CRC\n");
122 count = image_multi_count(hdr);
124 printf("Bad Image Part\n");
128 image_multi_getimg(hdr, part, &data, &len);
130 #if defined(CONFIG_FIT)
131 case IMAGE_FORMAT_FIT:
133 puts("No FIT subimage unit name\n");
137 printf("## Copying '%s' subimage from FIT image "
138 "at %08lx ...\n", uname, addr);
140 fit_hdr = (const void *)addr;
141 if (!fit_check_format(fit_hdr)) {
142 puts("Bad FIT image format\n");
146 /* get subimage node offset */
147 noffset = fit_image_get_node(fit_hdr, uname);
149 printf("Can't find '%s' FIT subimage\n", uname);
153 if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
155 printf("Must specify load address for %s command "
156 "with compressed image\n",
161 /* verify integrity */
163 if (!fit_image_check_hashes(fit_hdr, noffset)) {
164 puts("Bad Data Hash\n");
169 /* get subimage data address and length */
170 if (fit_image_get_data(fit_hdr, noffset,
171 &fit_data, &fit_len)) {
172 puts("Could not find script subimage data\n");
176 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
177 puts("Could not find script subimage "
178 "compression type\n");
182 data = (ulong)fit_data;
183 len = (ulong)fit_len;
187 puts("Invalid image type for imxtract\n");
194 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
198 void *to = (void *) dest;
199 void *from = (void *)data;
201 printf(" Loading part %d ... ", part);
204 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
206 memmove(to, from, tail);
212 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
213 printf(" Loading part %d ... ", part);
214 memmove((char *) dest, (char *)data, len);
215 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
219 printf(" Uncompressing part %d ... ", part);
220 if (gunzip((void *) dest, unc_len,
221 (uchar *) data, &len) != 0) {
222 puts("GUNZIP ERROR - image not loaded\n");
227 #if defined(CONFIG_BZIP2)
232 printf(" Uncompressing part %d ... ", part);
234 * If we've got less than 4 MB of malloc()
235 * space, use slower decompression algorithm
236 * which requires at most 2300 KB of memory.
238 i = BZ2_bzBuffToBuffDecompress(
239 (char *)ntohl(hdr->ih_load),
240 &unc_len, (char *)data, len,
241 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
244 printf("BUNZIP2 ERROR %d - "
245 "image not loaded\n", i);
250 #endif /* CONFIG_BZIP2 */
252 printf("Unimplemented compression type %d\n", comp);
258 setenv_hex("fileaddr", data);
259 setenv_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