blob: 75499b4b84b7421da0502e8d1132ca53a21fd9ef [file] [log] [blame]
wdenk0359dde2004-06-09 10:15:00 +00001/*
2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2003
6 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
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.
15 *
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.
20 *
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,
24 * MA 02111-1307 USA
25 */
26
wdenk0359dde2004-06-09 10:15:00 +000027
28/*
29 * Multi Image extract
30 */
31#include <common.h>
32#include <command.h>
33#include <image.h>
Wolfgang Wegner781afa92009-12-10 10:11:21 +010034#include <watchdog.h>
35#if defined(CONFIG_BZIP2)
36#include <bzlib.h>
37#endif
wdenk0359dde2004-06-09 10:15:00 +000038#include <asm/byteorder.h>
39
Wolfgang Wegner781afa92009-12-10 10:11:21 +010040#ifndef CONFIG_SYS_XIMG_LEN
41/* use 8MByte as default max gunzip size */
42#define CONFIG_SYS_XIMG_LEN 0x800000
43#endif
44
wdenk0359dde2004-06-09 10:15:00 +000045int
46do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
47{
Marian Balakowicz2321ed02008-03-12 10:33:00 +010048 ulong addr = load_addr;
49 ulong dest = 0;
50 ulong data, len, count;
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010051 int verify;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010052 int part = 0;
53 char pbuf[10];
Marian Balakowicz2321ed02008-03-12 10:33:00 +010054 image_header_t *hdr;
55#if defined(CONFIG_FIT)
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010056 const char *uname = NULL;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010057 const void* fit_hdr;
58 int noffset;
59 const void *fit_data;
60 size_t fit_len;
61#endif
Wolfgang Wegner781afa92009-12-10 10:11:21 +010062 uint unc_len = CONFIG_SYS_XIMG_LEN;
63 uint8_t comp;
wdenk0359dde2004-06-09 10:15:00 +000064
Bartlomiej Sieka47868592008-04-18 12:39:23 +020065 verify = getenv_yesno ("verify");
wdenk0359dde2004-06-09 10:15:00 +000066
67 if (argc > 1) {
68 addr = simple_strtoul(argv[1], NULL, 16);
69 }
70 if (argc > 2) {
71 part = simple_strtoul(argv[2], NULL, 16);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010072#if defined(CONFIG_FIT)
73 uname = argv[2];
74#endif
wdenk0359dde2004-06-09 10:15:00 +000075 }
76 if (argc > 3) {
77 dest = simple_strtoul(argv[3], NULL, 16);
78 }
79
Marian Balakowiczd7c88a42008-02-29 14:58:34 +010080 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010081 case IMAGE_FORMAT_LEGACY:
wdenk0359dde2004-06-09 10:15:00 +000082
Marian Balakowicz2321ed02008-03-12 10:33:00 +010083 printf("## Copying part %d from legacy image "
84 "at %08lx ...\n", part, addr);
85
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010086 hdr = (image_header_t *)addr;
87 if (!image_check_magic (hdr)) {
88 printf("Bad Magic Number\n");
89 return 1;
90 }
wdenk0359dde2004-06-09 10:15:00 +000091
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010092 if (!image_check_hcrc (hdr)) {
93 printf("Bad Header Checksum\n");
94 return 1;
95 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +010096#ifdef DEBUG
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010097 image_print_contents (hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010098#endif
wdenk0359dde2004-06-09 10:15:00 +000099
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100100 if (!image_check_type (hdr, IH_TYPE_MULTI)) {
101 printf("Wrong Image Type for %s command\n",
102 cmdtp->name);
103 return 1;
104 }
wdenk0359dde2004-06-09 10:15:00 +0000105
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100106 comp = image_get_comp (hdr);
107 if ((comp != IH_COMP_NONE) && (argc < 4)) {
108 printf("Must specify load address for %s command "
109 "with compressed image\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100110 cmdtp->name);
wdenk0359dde2004-06-09 10:15:00 +0000111 return 1;
112 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100113
114 if (verify) {
115 printf(" Verifying Checksum ... ");
116 if (!image_check_dcrc (hdr)) {
117 printf("Bad Data CRC\n");
118 return 1;
119 }
120 printf("OK\n");
121 }
wdenk0359dde2004-06-09 10:15:00 +0000122
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100123 count = image_multi_count (hdr);
124 if (part >= count) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100125 printf("Bad Image Part\n");
126 return 1;
127 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100128
129 image_multi_getimg (hdr, part, &data, &len);
130 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100131#if defined(CONFIG_FIT)
132 case IMAGE_FORMAT_FIT:
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100133 if (uname == NULL) {
134 puts ("No FIT subimage unit name\n");
135 return 1;
136 }
137
138 printf("## Copying '%s' subimage from FIT image "
139 "at %08lx ...\n", uname, addr);
140
141 fit_hdr = (const void *)addr;
142 if (!fit_check_format (fit_hdr)) {
143 puts ("Bad FIT image format\n");
144 return 1;
145 }
146
147 /* get subimage node offset */
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +0100148 noffset = fit_image_get_node (fit_hdr, uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100149 if (noffset < 0) {
150 printf ("Can't find '%s' FIT subimage\n", uname);
151 return 1;
152 }
153
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100154 if (fit_image_check_comp (fit_hdr, noffset, IH_COMP_NONE)
155 && (argc < 4)) {
156 printf("Must specify load address for %s command "
157 "with compressed image\n",
158 cmdtp->name);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100159 return 1;
160 }
161
162 /* verify integrity */
163 if (verify) {
164 if (!fit_image_check_hashes (fit_hdr, noffset)) {
165 puts ("Bad Data Hash\n");
166 return 1;
167 }
168 }
169
170 /* get subimage data address and length */
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100171 if (fit_image_get_data (fit_hdr, noffset,
172 &fit_data, &fit_len)) {
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100173 puts ("Could not find script subimage data\n");
174 return 1;
175 }
176
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100177 if (fit_image_get_comp (fit_hdr, noffset, &comp)) {
178 puts ("Could not find script subimage "
179 "compression type\n");
180 return 1;
181 }
182
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +0100183 data = (ulong)fit_data;
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100184 len = (ulong)fit_len;
185 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100186#endif
187 default:
188 puts ("Invalid image type for imxtract\n");
wdenk0359dde2004-06-09 10:15:00 +0000189 return 1;
190 }
wdenk0359dde2004-06-09 10:15:00 +0000191
192 if (argc > 3) {
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100193 switch (comp) {
194 case IH_COMP_NONE:
195#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
196 {
197 size_t l = len;
198 size_t tail;
199 void *to = (void *) dest;
200 void *from = (void *)data;
201
202 printf (" Loading part %d ... ", part);
203
204 while (l > 0) {
205 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
206 WATCHDOG_RESET();
207 memmove (to, from, tail);
208 to += tail;
209 from += tail;
210 l -= tail;
211 }
212 }
213#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
214 printf (" Loading part %d ... ", part);
215 memmove ((char *) dest, (char *)data, len);
216#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
217 break;
218 case IH_COMP_GZIP:
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");
223 return 1;
224 }
225 break;
226#if defined(CONFIG_BZIP2)
227 case IH_COMP_BZIP2:
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100228 {
229 int i;
230
231 printf (" Uncompressing part %d ... ", part);
232 /*
Wolfgang Denka2cfb242010-03-12 23:06:04 +0100233 * If we've got less than 4 MB of malloc()
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100234 * space, use slower decompression algorithm
235 * which requires at most 2300 KB of memory.
236 */
237 i = BZ2_bzBuffToBuffDecompress(
238 (char*)ntohl(hdr->ih_load),
239 &unc_len, (char *)data, len,
240 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
241 0);
242 if (i != BZ_OK) {
243 printf ("BUNZIP2 ERROR %d - "
244 "image not loaded\n", i);
245 return 1;
246 }
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100247 }
248 break;
249#endif /* CONFIG_BZIP2 */
250 default:
251 printf ("Unimplemented compression type %d\n", comp);
252 return 1;
253 }
254 puts ("OK\n");
wdenk0359dde2004-06-09 10:15:00 +0000255 }
256
257 sprintf(pbuf, "%8lx", data);
258 setenv("fileaddr", pbuf);
259 sprintf(pbuf, "%8lx", len);
260 setenv("filesize", pbuf);
261
262 return 0;
263}
264
265U_BOOT_CMD(imxtract, 4, 1, do_imgextract,
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200266 "extract a part of a multi-image",
267 "addr part [dest]\n"
268 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100269#if defined(CONFIG_FIT)
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200270 "\n"
271 "addr uname [dest]\n"
272 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100273#endif
274);