blob: 9e53cc45572fc7b4a41bb0380bbede2669d1eb26 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk48abe7b2004-06-09 10:15:00 +00002/*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2003
7 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
wdenk48abe7b2004-06-09 10:15:00 +00008 */
9
wdenk48abe7b2004-06-09 10:15:00 +000010
11/*
12 * Multi Image extract
13 */
14#include <common.h>
15#include <command.h>
16#include <image.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050017#include <mapmem.h>
Wolfgang Wegner5912d362009-12-10 10:11:21 +010018#include <watchdog.h>
19#if defined(CONFIG_BZIP2)
20#include <bzlib.h>
21#endif
wdenk48abe7b2004-06-09 10:15:00 +000022#include <asm/byteorder.h>
Simon Glass628af172013-08-30 11:00:09 -060023#include <asm/io.h>
wdenk48abe7b2004-06-09 10:15:00 +000024
Wolfgang Wegner5912d362009-12-10 10:11:21 +010025#ifndef CONFIG_SYS_XIMG_LEN
26/* use 8MByte as default max gunzip size */
27#define CONFIG_SYS_XIMG_LEN 0x800000
28#endif
29
Kim Phillips088f1b12012-10-29 13:34:31 +000030static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +020031do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk48abe7b2004-06-09 10:15:00 +000032{
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010033 ulong addr = load_addr;
34 ulong dest = 0;
Heiko Schocher21d29f72014-05-28 11:33:33 +020035 ulong data, len;
Bartlomiej Siekafbe7a152008-03-20 19:38:45 +010036 int verify;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010037 int part = 0;
Tom Rinic76c93a2019-05-23 07:14:07 -040038#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Heiko Schocher21d29f72014-05-28 11:33:33 +020039 ulong count;
Simon Glass628af172013-08-30 11:00:09 -060040 image_header_t *hdr = NULL;
Heiko Schocher21d29f72014-05-28 11:33:33 +020041#endif
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010042#if defined(CONFIG_FIT)
Bartlomiej Siekafbe7a152008-03-20 19:38:45 +010043 const char *uname = NULL;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010044 const void* fit_hdr;
45 int noffset;
46 const void *fit_data;
47 size_t fit_len;
48#endif
Simon Glassa92181b2013-04-17 16:13:45 +000049#ifdef CONFIG_GZIP
Wolfgang Wegner5912d362009-12-10 10:11:21 +010050 uint unc_len = CONFIG_SYS_XIMG_LEN;
Simon Glassa92181b2013-04-17 16:13:45 +000051#endif
Wolfgang Wegner5912d362009-12-10 10:11:21 +010052 uint8_t comp;
wdenk48abe7b2004-06-09 10:15:00 +000053
Simon Glassbfebc8c2017-08-03 12:22:13 -060054 verify = env_get_yesno("verify");
wdenk48abe7b2004-06-09 10:15:00 +000055
56 if (argc > 1) {
57 addr = simple_strtoul(argv[1], NULL, 16);
58 }
59 if (argc > 2) {
60 part = simple_strtoul(argv[2], NULL, 16);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010061#if defined(CONFIG_FIT)
62 uname = argv[2];
63#endif
wdenk48abe7b2004-06-09 10:15:00 +000064 }
65 if (argc > 3) {
66 dest = simple_strtoul(argv[3], NULL, 16);
67 }
68
Stephen Warren712fbcf2011-10-18 11:11:49 +000069 switch (genimg_get_format((void *)addr)) {
Tom Rinic76c93a2019-05-23 07:14:07 -040070#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010071 case IMAGE_FORMAT_LEGACY:
wdenk48abe7b2004-06-09 10:15:00 +000072
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010073 printf("## Copying part %d from legacy image "
74 "at %08lx ...\n", part, addr);
75
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010076 hdr = (image_header_t *)addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +000077 if (!image_check_magic(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010078 printf("Bad Magic Number\n");
wdenk48abe7b2004-06-09 10:15:00 +000079 return 1;
80 }
wdenk48abe7b2004-06-09 10:15:00 +000081
Stephen Warren712fbcf2011-10-18 11:11:49 +000082 if (!image_check_hcrc(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010083 printf("Bad Header Checksum\n");
84 return 1;
85 }
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010086#ifdef DEBUG
Stephen Warren712fbcf2011-10-18 11:11:49 +000087 image_print_contents(hdr);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010088#endif
wdenk48abe7b2004-06-09 10:15:00 +000089
Pierre Aubert83636fa2015-09-16 08:29:11 +020090 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
91 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010092 printf("Wrong Image Type for %s command\n",
93 cmdtp->name);
94 return 1;
95 }
96
Stephen Warren712fbcf2011-10-18 11:11:49 +000097 comp = image_get_comp(hdr);
Wolfgang Wegner5912d362009-12-10 10:11:21 +010098 if ((comp != IH_COMP_NONE) && (argc < 4)) {
99 printf("Must specify load address for %s command "
100 "with compressed image\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100101 cmdtp->name);
102 return 1;
103 }
104
105 if (verify) {
106 printf(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000107 if (!image_check_dcrc(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100108 printf("Bad Data CRC\n");
109 return 1;
110 }
111 printf("OK\n");
112 }
113
Stephen Warren712fbcf2011-10-18 11:11:49 +0000114 count = image_multi_count(hdr);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100115 if (part >= count) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100116 printf("Bad Image Part\n");
117 return 1;
118 }
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100119
Stephen Warren712fbcf2011-10-18 11:11:49 +0000120 image_multi_getimg(hdr, part, &data, &len);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100121 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200122#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100123#if defined(CONFIG_FIT)
124 case IMAGE_FORMAT_FIT:
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100125 if (uname == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000126 puts("No FIT subimage unit name\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100127 return 1;
128 }
129
130 printf("## Copying '%s' subimage from FIT image "
131 "at %08lx ...\n", uname, addr);
132
133 fit_hdr = (const void *)addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000134 if (!fit_check_format(fit_hdr)) {
135 puts("Bad FIT image format\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100136 return 1;
137 }
138
139 /* get subimage node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000140 noffset = fit_image_get_node(fit_hdr, uname);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100141 if (noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000142 printf("Can't find '%s' FIT subimage\n", uname);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100143 return 1;
144 }
145
Stefan Theil240e58e2019-01-07 10:25:52 +0100146 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100147 && (argc < 4)) {
148 printf("Must specify load address for %s command "
149 "with compressed image\n",
150 cmdtp->name);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100151 return 1;
152 }
153
154 /* verify integrity */
155 if (verify) {
Simon Glassb8da8362013-05-07 06:11:57 +0000156 if (!fit_image_verify(fit_hdr, noffset)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000157 puts("Bad Data Hash\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100158 return 1;
159 }
160 }
161
Tien Fong Chee3695ea52019-02-12 20:49:30 +0800162 /* get subimage/external data address and length */
163 if (fit_image_get_data_and_size(fit_hdr, noffset,
164 &fit_data, &fit_len)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000165 puts("Could not find script subimage data\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100166 return 1;
167 }
168
Stephen Warren712fbcf2011-10-18 11:11:49 +0000169 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
170 puts("Could not find script subimage "
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100171 "compression type\n");
172 return 1;
173 }
174
Bartlomiej Siekafbe7a152008-03-20 19:38:45 +0100175 data = (ulong)fit_data;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100176 len = (ulong)fit_len;
177 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100178#endif
179 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000180 puts("Invalid image type for imxtract\n");
wdenk48abe7b2004-06-09 10:15:00 +0000181 return 1;
182 }
wdenk48abe7b2004-06-09 10:15:00 +0000183
184 if (argc > 3) {
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100185 switch (comp) {
186 case IH_COMP_NONE:
187#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
188 {
189 size_t l = len;
190 size_t tail;
191 void *to = (void *) dest;
192 void *from = (void *)data;
193
Stephen Warren712fbcf2011-10-18 11:11:49 +0000194 printf(" Loading part %d ... ", part);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100195
196 while (l > 0) {
197 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
198 WATCHDOG_RESET();
Stephen Warren712fbcf2011-10-18 11:11:49 +0000199 memmove(to, from, tail);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100200 to += tail;
201 from += tail;
202 l -= tail;
203 }
204 }
205#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000206 printf(" Loading part %d ... ", part);
207 memmove((char *) dest, (char *)data, len);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100208#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
209 break;
Matthew McClintock0e0996e2011-05-24 05:48:26 +0000210#ifdef CONFIG_GZIP
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100211 case IH_COMP_GZIP:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000212 printf(" Uncompressing part %d ... ", part);
213 if (gunzip((void *) dest, unc_len,
214 (uchar *) data, &len) != 0) {
215 puts("GUNZIP ERROR - image not loaded\n");
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100216 return 1;
217 }
218 break;
Matthew McClintock0e0996e2011-05-24 05:48:26 +0000219#endif
Tom Rinic76c93a2019-05-23 07:14:07 -0400220#if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100221 case IH_COMP_BZIP2:
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100222 {
223 int i;
224
Stephen Warren712fbcf2011-10-18 11:11:49 +0000225 printf(" Uncompressing part %d ... ", part);
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100226 /*
Wolfgang Denk93910ed2010-03-12 23:06:04 +0100227 * If we've got less than 4 MB of malloc()
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100228 * space, use slower decompression algorithm
229 * which requires at most 2300 KB of memory.
230 */
231 i = BZ2_bzBuffToBuffDecompress(
Simon Glass628af172013-08-30 11:00:09 -0600232 map_sysmem(ntohl(hdr->ih_load), 0),
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100233 &unc_len, (char *)data, len,
234 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
235 0);
236 if (i != BZ_OK) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000237 printf("BUNZIP2 ERROR %d - "
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100238 "image not loaded\n", i);
239 return 1;
240 }
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100241 }
242 break;
243#endif /* CONFIG_BZIP2 */
244 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000245 printf("Unimplemented compression type %d\n", comp);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100246 return 1;
247 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000248 puts("OK\n");
wdenk48abe7b2004-06-09 10:15:00 +0000249 }
250
Mario Six8354aa22018-03-28 14:39:10 +0200251 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
Pieter Voorthuijsenc72b65c2015-01-12 16:23:18 +0100252
Simon Glass018f5302017-08-03 12:22:10 -0600253 env_set_hex("fileaddr", data);
254 env_set_hex("filesize", len);
wdenk48abe7b2004-06-09 10:15:00 +0000255
256 return 0;
257}
258
Kim Phillips088f1b12012-10-29 13:34:31 +0000259#ifdef CONFIG_SYS_LONGHELP
260static char imgextract_help_text[] =
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200261 "addr part [dest]\n"
262 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100263#if defined(CONFIG_FIT)
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200264 "\n"
265 "addr uname [dest]\n"
266 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100267#endif
Kim Phillips088f1b12012-10-29 13:34:31 +0000268 "";
269#endif
270
271U_BOOT_CMD(
272 imxtract, 4, 1, do_imgextract,
273 "extract a part of a multi-image", imgextract_help_text
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100274);