blob: 270e8030992c3c4d2d4283b8957f07d82e34d252 [file] [log] [blame]
wdenk48abe7b2004-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
wdenk48abe7b2004-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 Wegner5912d362009-12-10 10:11:21 +010034#include <watchdog.h>
35#if defined(CONFIG_BZIP2)
36#include <bzlib.h>
37#endif
wdenk48abe7b2004-06-09 10:15:00 +000038#include <asm/byteorder.h>
39
Wolfgang Wegner5912d362009-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
Kim Phillips088f1b12012-10-29 13:34:31 +000045static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +020046do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk48abe7b2004-06-09 10:15:00 +000047{
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010048 ulong addr = load_addr;
49 ulong dest = 0;
50 ulong data, len, count;
Bartlomiej Siekafbe7a152008-03-20 19:38:45 +010051 int verify;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010052 int part = 0;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010053 image_header_t *hdr;
54#if defined(CONFIG_FIT)
Bartlomiej Siekafbe7a152008-03-20 19:38:45 +010055 const char *uname = NULL;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010056 const void* fit_hdr;
57 int noffset;
58 const void *fit_data;
59 size_t fit_len;
60#endif
Simon Glassa92181b2013-04-17 16:13:45 +000061#ifdef CONFIG_GZIP
Wolfgang Wegner5912d362009-12-10 10:11:21 +010062 uint unc_len = CONFIG_SYS_XIMG_LEN;
Simon Glassa92181b2013-04-17 16:13:45 +000063#endif
Wolfgang Wegner5912d362009-12-10 10:11:21 +010064 uint8_t comp;
wdenk48abe7b2004-06-09 10:15:00 +000065
Stephen Warren712fbcf2011-10-18 11:11:49 +000066 verify = getenv_yesno("verify");
wdenk48abe7b2004-06-09 10:15:00 +000067
68 if (argc > 1) {
69 addr = simple_strtoul(argv[1], NULL, 16);
70 }
71 if (argc > 2) {
72 part = simple_strtoul(argv[2], NULL, 16);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010073#if defined(CONFIG_FIT)
74 uname = argv[2];
75#endif
wdenk48abe7b2004-06-09 10:15:00 +000076 }
77 if (argc > 3) {
78 dest = simple_strtoul(argv[3], NULL, 16);
79 }
80
Stephen Warren712fbcf2011-10-18 11:11:49 +000081 switch (genimg_get_format((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010082 case IMAGE_FORMAT_LEGACY:
wdenk48abe7b2004-06-09 10:15:00 +000083
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010084 printf("## Copying part %d from legacy image "
85 "at %08lx ...\n", part, addr);
86
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010087 hdr = (image_header_t *)addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +000088 if (!image_check_magic(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010089 printf("Bad Magic Number\n");
wdenk48abe7b2004-06-09 10:15:00 +000090 return 1;
91 }
wdenk48abe7b2004-06-09 10:15:00 +000092
Stephen Warren712fbcf2011-10-18 11:11:49 +000093 if (!image_check_hcrc(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010094 printf("Bad Header Checksum\n");
95 return 1;
96 }
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010097#ifdef DEBUG
Stephen Warren712fbcf2011-10-18 11:11:49 +000098 image_print_contents(hdr);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010099#endif
wdenk48abe7b2004-06-09 10:15:00 +0000100
Stephen Warren712fbcf2011-10-18 11:11:49 +0000101 if (!image_check_type(hdr, IH_TYPE_MULTI)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100102 printf("Wrong Image Type for %s command\n",
103 cmdtp->name);
104 return 1;
105 }
106
Stephen Warren712fbcf2011-10-18 11:11:49 +0000107 comp = image_get_comp(hdr);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100108 if ((comp != IH_COMP_NONE) && (argc < 4)) {
109 printf("Must specify load address for %s command "
110 "with compressed image\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100111 cmdtp->name);
112 return 1;
113 }
114
115 if (verify) {
116 printf(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000117 if (!image_check_dcrc(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100118 printf("Bad Data CRC\n");
119 return 1;
120 }
121 printf("OK\n");
122 }
123
Stephen Warren712fbcf2011-10-18 11:11:49 +0000124 count = image_multi_count(hdr);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100125 if (part >= count) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100126 printf("Bad Image Part\n");
127 return 1;
128 }
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100129
Stephen Warren712fbcf2011-10-18 11:11:49 +0000130 image_multi_getimg(hdr, part, &data, &len);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100131 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100132#if defined(CONFIG_FIT)
133 case IMAGE_FORMAT_FIT:
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100134 if (uname == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000135 puts("No FIT subimage unit name\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100136 return 1;
137 }
138
139 printf("## Copying '%s' subimage from FIT image "
140 "at %08lx ...\n", uname, addr);
141
142 fit_hdr = (const void *)addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000143 if (!fit_check_format(fit_hdr)) {
144 puts("Bad FIT image format\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100145 return 1;
146 }
147
148 /* get subimage node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000149 noffset = fit_image_get_node(fit_hdr, uname);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100150 if (noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000151 printf("Can't find '%s' FIT subimage\n", uname);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100152 return 1;
153 }
154
Stephen Warren712fbcf2011-10-18 11:11:49 +0000155 if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100156 && (argc < 4)) {
157 printf("Must specify load address for %s command "
158 "with compressed image\n",
159 cmdtp->name);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100160 return 1;
161 }
162
163 /* verify integrity */
164 if (verify) {
Simon Glassb8da8362013-05-07 06:11:57 +0000165 if (!fit_image_verify(fit_hdr, noffset)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000166 puts("Bad Data Hash\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100167 return 1;
168 }
169 }
170
171 /* get subimage data address and length */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000172 if (fit_image_get_data(fit_hdr, noffset,
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100173 &fit_data, &fit_len)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000174 puts("Could not find script subimage data\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100175 return 1;
176 }
177
Stephen Warren712fbcf2011-10-18 11:11:49 +0000178 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
179 puts("Could not find script subimage "
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100180 "compression type\n");
181 return 1;
182 }
183
Bartlomiej Siekafbe7a152008-03-20 19:38:45 +0100184 data = (ulong)fit_data;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100185 len = (ulong)fit_len;
186 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100187#endif
188 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000189 puts("Invalid image type for imxtract\n");
wdenk48abe7b2004-06-09 10:15:00 +0000190 return 1;
191 }
wdenk48abe7b2004-06-09 10:15:00 +0000192
193 if (argc > 3) {
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100194 switch (comp) {
195 case IH_COMP_NONE:
196#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
197 {
198 size_t l = len;
199 size_t tail;
200 void *to = (void *) dest;
201 void *from = (void *)data;
202
Stephen Warren712fbcf2011-10-18 11:11:49 +0000203 printf(" Loading part %d ... ", part);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100204
205 while (l > 0) {
206 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
207 WATCHDOG_RESET();
Stephen Warren712fbcf2011-10-18 11:11:49 +0000208 memmove(to, from, tail);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100209 to += tail;
210 from += tail;
211 l -= tail;
212 }
213 }
214#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000215 printf(" Loading part %d ... ", part);
216 memmove((char *) dest, (char *)data, len);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100217#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
218 break;
Matthew McClintock0e0996e2011-05-24 05:48:26 +0000219#ifdef CONFIG_GZIP
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100220 case IH_COMP_GZIP:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000221 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");
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100225 return 1;
226 }
227 break;
Matthew McClintock0e0996e2011-05-24 05:48:26 +0000228#endif
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100229#if defined(CONFIG_BZIP2)
230 case IH_COMP_BZIP2:
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100231 {
232 int i;
233
Stephen Warren712fbcf2011-10-18 11:11:49 +0000234 printf(" Uncompressing part %d ... ", part);
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100235 /*
Wolfgang Denk93910ed2010-03-12 23:06:04 +0100236 * If we've got less than 4 MB of malloc()
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100237 * space, use slower decompression algorithm
238 * which requires at most 2300 KB of memory.
239 */
240 i = BZ2_bzBuffToBuffDecompress(
Stephen Warren712fbcf2011-10-18 11:11:49 +0000241 (char *)ntohl(hdr->ih_load),
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100242 &unc_len, (char *)data, len,
243 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
244 0);
245 if (i != BZ_OK) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000246 printf("BUNZIP2 ERROR %d - "
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100247 "image not loaded\n", i);
248 return 1;
249 }
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100250 }
251 break;
252#endif /* CONFIG_BZIP2 */
253 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000254 printf("Unimplemented compression type %d\n", comp);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100255 return 1;
256 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000257 puts("OK\n");
wdenk48abe7b2004-06-09 10:15:00 +0000258 }
259
Simon Glass41ef3722013-02-24 17:33:22 +0000260 setenv_hex("fileaddr", data);
261 setenv_hex("filesize", len);
wdenk48abe7b2004-06-09 10:15:00 +0000262
263 return 0;
264}
265
Kim Phillips088f1b12012-10-29 13:34:31 +0000266#ifdef CONFIG_SYS_LONGHELP
267static char imgextract_help_text[] =
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200268 "addr part [dest]\n"
269 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100270#if defined(CONFIG_FIT)
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200271 "\n"
272 "addr uname [dest]\n"
273 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100274#endif
Kim Phillips088f1b12012-10-29 13:34:31 +0000275 "";
276#endif
277
278U_BOOT_CMD(
279 imxtract, 4, 1, do_imgextract,
280 "extract a part of a multi-image", imgextract_help_text
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100281);