blob: 770f6a3eede78bf2edce966cf831f3f5e027dc24 [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>
Simon Glass1eb69ae2019-11-14 12:57:39 -070016#include <cpu_func.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060017#include <env.h>
Simon Glass0c670fc2019-08-01 09:46:36 -060018#include <gzip.h>
wdenk48abe7b2004-06-09 10:15:00 +000019#include <image.h>
Simon Glass336d4612020-02-03 07:36:16 -070020#include <malloc.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050021#include <mapmem.h>
Wolfgang Wegner5912d362009-12-10 10:11:21 +010022#include <watchdog.h>
23#if defined(CONFIG_BZIP2)
24#include <bzlib.h>
25#endif
wdenk48abe7b2004-06-09 10:15:00 +000026#include <asm/byteorder.h>
Simon Glass628af172013-08-30 11:00:09 -060027#include <asm/io.h>
wdenk48abe7b2004-06-09 10:15:00 +000028
Wolfgang Wegner5912d362009-12-10 10:11:21 +010029#ifndef CONFIG_SYS_XIMG_LEN
30/* use 8MByte as default max gunzip size */
31#define CONFIG_SYS_XIMG_LEN 0x800000
32#endif
33
Kim Phillips088f1b12012-10-29 13:34:31 +000034static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +020035do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk48abe7b2004-06-09 10:15:00 +000036{
Simon Glassbb872dd2019-12-28 10:45:02 -070037 ulong addr = image_load_addr;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010038 ulong dest = 0;
Heiko Schocher21d29f72014-05-28 11:33:33 +020039 ulong data, len;
Bartlomiej Siekafbe7a152008-03-20 19:38:45 +010040 int verify;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010041 int part = 0;
Tom Rinic76c93a2019-05-23 07:14:07 -040042#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Heiko Schocher21d29f72014-05-28 11:33:33 +020043 ulong count;
Simon Glass628af172013-08-30 11:00:09 -060044 image_header_t *hdr = NULL;
Heiko Schocher21d29f72014-05-28 11:33:33 +020045#endif
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010046#if defined(CONFIG_FIT)
Bartlomiej Siekafbe7a152008-03-20 19:38:45 +010047 const char *uname = NULL;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010048 const void* fit_hdr;
49 int noffset;
50 const void *fit_data;
51 size_t fit_len;
52#endif
Simon Glassa92181b2013-04-17 16:13:45 +000053#ifdef CONFIG_GZIP
Wolfgang Wegner5912d362009-12-10 10:11:21 +010054 uint unc_len = CONFIG_SYS_XIMG_LEN;
Simon Glassa92181b2013-04-17 16:13:45 +000055#endif
Wolfgang Wegner5912d362009-12-10 10:11:21 +010056 uint8_t comp;
wdenk48abe7b2004-06-09 10:15:00 +000057
Simon Glassbfebc8c2017-08-03 12:22:13 -060058 verify = env_get_yesno("verify");
wdenk48abe7b2004-06-09 10:15:00 +000059
60 if (argc > 1) {
61 addr = simple_strtoul(argv[1], NULL, 16);
62 }
63 if (argc > 2) {
64 part = simple_strtoul(argv[2], NULL, 16);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010065#if defined(CONFIG_FIT)
66 uname = argv[2];
67#endif
wdenk48abe7b2004-06-09 10:15:00 +000068 }
69 if (argc > 3) {
70 dest = simple_strtoul(argv[3], NULL, 16);
71 }
72
Stephen Warren712fbcf2011-10-18 11:11:49 +000073 switch (genimg_get_format((void *)addr)) {
Tom Rinic76c93a2019-05-23 07:14:07 -040074#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010075 case IMAGE_FORMAT_LEGACY:
wdenk48abe7b2004-06-09 10:15:00 +000076
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010077 printf("## Copying part %d from legacy image "
78 "at %08lx ...\n", part, addr);
79
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010080 hdr = (image_header_t *)addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +000081 if (!image_check_magic(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010082 printf("Bad Magic Number\n");
wdenk48abe7b2004-06-09 10:15:00 +000083 return 1;
84 }
wdenk48abe7b2004-06-09 10:15:00 +000085
Stephen Warren712fbcf2011-10-18 11:11:49 +000086 if (!image_check_hcrc(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010087 printf("Bad Header Checksum\n");
88 return 1;
89 }
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010090#ifdef DEBUG
Stephen Warren712fbcf2011-10-18 11:11:49 +000091 image_print_contents(hdr);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010092#endif
wdenk48abe7b2004-06-09 10:15:00 +000093
Pierre Aubert83636fa2015-09-16 08:29:11 +020094 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
95 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010096 printf("Wrong Image Type for %s command\n",
97 cmdtp->name);
98 return 1;
99 }
100
Stephen Warren712fbcf2011-10-18 11:11:49 +0000101 comp = image_get_comp(hdr);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100102 if ((comp != IH_COMP_NONE) && (argc < 4)) {
103 printf("Must specify load address for %s command "
104 "with compressed image\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100105 cmdtp->name);
106 return 1;
107 }
108
109 if (verify) {
110 printf(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000111 if (!image_check_dcrc(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100112 printf("Bad Data CRC\n");
113 return 1;
114 }
115 printf("OK\n");
116 }
117
Stephen Warren712fbcf2011-10-18 11:11:49 +0000118 count = image_multi_count(hdr);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100119 if (part >= count) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100120 printf("Bad Image Part\n");
121 return 1;
122 }
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100123
Stephen Warren712fbcf2011-10-18 11:11:49 +0000124 image_multi_getimg(hdr, part, &data, &len);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100125 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200126#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100127#if defined(CONFIG_FIT)
128 case IMAGE_FORMAT_FIT:
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100129 if (uname == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000130 puts("No FIT subimage unit name\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100131 return 1;
132 }
133
134 printf("## Copying '%s' subimage from FIT image "
135 "at %08lx ...\n", uname, addr);
136
137 fit_hdr = (const void *)addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000138 if (!fit_check_format(fit_hdr)) {
139 puts("Bad FIT image format\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100140 return 1;
141 }
142
143 /* get subimage node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000144 noffset = fit_image_get_node(fit_hdr, uname);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100145 if (noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000146 printf("Can't find '%s' FIT subimage\n", uname);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100147 return 1;
148 }
149
Stefan Theil240e58e2019-01-07 10:25:52 +0100150 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100151 && (argc < 4)) {
152 printf("Must specify load address for %s command "
153 "with compressed image\n",
154 cmdtp->name);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100155 return 1;
156 }
157
158 /* verify integrity */
159 if (verify) {
Simon Glassb8da8362013-05-07 06:11:57 +0000160 if (!fit_image_verify(fit_hdr, noffset)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000161 puts("Bad Data Hash\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100162 return 1;
163 }
164 }
165
Tien Fong Chee3695ea52019-02-12 20:49:30 +0800166 /* get subimage/external data address and length */
167 if (fit_image_get_data_and_size(fit_hdr, noffset,
168 &fit_data, &fit_len)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000169 puts("Could not find script subimage data\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100170 return 1;
171 }
172
Stephen Warren712fbcf2011-10-18 11:11:49 +0000173 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
174 puts("Could not find script subimage "
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100175 "compression type\n");
176 return 1;
177 }
178
Bartlomiej Siekafbe7a152008-03-20 19:38:45 +0100179 data = (ulong)fit_data;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100180 len = (ulong)fit_len;
181 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100182#endif
183 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000184 puts("Invalid image type for imxtract\n");
wdenk48abe7b2004-06-09 10:15:00 +0000185 return 1;
186 }
wdenk48abe7b2004-06-09 10:15:00 +0000187
188 if (argc > 3) {
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100189 switch (comp) {
190 case IH_COMP_NONE:
191#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
192 {
193 size_t l = len;
194 size_t tail;
195 void *to = (void *) dest;
196 void *from = (void *)data;
197
Stephen Warren712fbcf2011-10-18 11:11:49 +0000198 printf(" Loading part %d ... ", part);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100199
200 while (l > 0) {
201 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
202 WATCHDOG_RESET();
Stephen Warren712fbcf2011-10-18 11:11:49 +0000203 memmove(to, from, tail);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100204 to += tail;
205 from += tail;
206 l -= tail;
207 }
208 }
209#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000210 printf(" Loading part %d ... ", part);
211 memmove((char *) dest, (char *)data, len);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100212#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
213 break;
Matthew McClintock0e0996e2011-05-24 05:48:26 +0000214#ifdef CONFIG_GZIP
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100215 case IH_COMP_GZIP:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000216 printf(" Uncompressing part %d ... ", part);
217 if (gunzip((void *) dest, unc_len,
218 (uchar *) data, &len) != 0) {
219 puts("GUNZIP ERROR - image not loaded\n");
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100220 return 1;
221 }
222 break;
Matthew McClintock0e0996e2011-05-24 05:48:26 +0000223#endif
Tom Rinic76c93a2019-05-23 07:14:07 -0400224#if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100225 case IH_COMP_BZIP2:
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100226 {
227 int i;
228
Stephen Warren712fbcf2011-10-18 11:11:49 +0000229 printf(" Uncompressing part %d ... ", part);
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100230 /*
Wolfgang Denk93910ed2010-03-12 23:06:04 +0100231 * If we've got less than 4 MB of malloc()
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100232 * space, use slower decompression algorithm
233 * which requires at most 2300 KB of memory.
234 */
235 i = BZ2_bzBuffToBuffDecompress(
Simon Glass628af172013-08-30 11:00:09 -0600236 map_sysmem(ntohl(hdr->ih_load), 0),
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100237 &unc_len, (char *)data, len,
238 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
239 0);
240 if (i != BZ_OK) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000241 printf("BUNZIP2 ERROR %d - "
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100242 "image not loaded\n", i);
243 return 1;
244 }
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100245 }
246 break;
247#endif /* CONFIG_BZIP2 */
248 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000249 printf("Unimplemented compression type %d\n", comp);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100250 return 1;
251 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000252 puts("OK\n");
wdenk48abe7b2004-06-09 10:15:00 +0000253 }
254
Mario Six8354aa22018-03-28 14:39:10 +0200255 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
Pieter Voorthuijsenc72b65c2015-01-12 16:23:18 +0100256
Simon Glass018f5302017-08-03 12:22:10 -0600257 env_set_hex("fileaddr", data);
258 env_set_hex("filesize", len);
wdenk48abe7b2004-06-09 10:15:00 +0000259
260 return 0;
261}
262
Kim Phillips088f1b12012-10-29 13:34:31 +0000263#ifdef CONFIG_SYS_LONGHELP
264static char imgextract_help_text[] =
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200265 "addr part [dest]\n"
266 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100267#if defined(CONFIG_FIT)
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200268 "\n"
269 "addr uname [dest]\n"
270 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100271#endif
Kim Phillips088f1b12012-10-29 13:34:31 +0000272 "";
273#endif
274
275U_BOOT_CMD(
276 imxtract, 4, 1, do_imgextract,
277 "extract a part of a multi-image", imgextract_help_text
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100278);