blob: c79e8a078605aeca9d55f475d0ceb8cc85bafde7 [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 */
wdenk48abe7b2004-06-09 10:15:00 +000014#include <command.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070015#include <cpu_func.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060016#include <env.h>
Simon Glass0c670fc2019-08-01 09:46:36 -060017#include <gzip.h>
Dmitry Gerasimov822911d2024-06-02 17:25:52 +020018#if IS_ENABLED(CONFIG_ZSTD)
19#include <linux/zstd.h>
20#endif
wdenk48abe7b2004-06-09 10:15:00 +000021#include <image.h>
Simon Glass336d4612020-02-03 07:36:16 -070022#include <malloc.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050023#include <mapmem.h>
Wolfgang Wegner5912d362009-12-10 10:11:21 +010024#include <watchdog.h>
25#if defined(CONFIG_BZIP2)
26#include <bzlib.h>
27#endif
wdenk48abe7b2004-06-09 10:15:00 +000028#include <asm/byteorder.h>
Simon Glass90526e92020-05-10 11:39:56 -060029#include <asm/cache.h>
Simon Glass628af172013-08-30 11:00:09 -060030#include <asm/io.h>
wdenk48abe7b2004-06-09 10:15:00 +000031
Kim Phillips088f1b12012-10-29 13:34:31 +000032static int
Simon Glass09140112020-05-10 11:40:03 -060033do_imgextract(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk48abe7b2004-06-09 10:15:00 +000034{
Simon Glassbb872dd2019-12-28 10:45:02 -070035 ulong addr = image_load_addr;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010036 ulong dest = 0;
Heiko Schocher21d29f72014-05-28 11:33:33 +020037 ulong data, len;
Bartlomiej Siekafbe7a152008-03-20 19:38:45 +010038 int verify;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010039 int part = 0;
Tom Rinic76c93a2019-05-23 07:14:07 -040040#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Heiko Schocher21d29f72014-05-28 11:33:33 +020041 ulong count;
Simon Glassf3543e62022-09-06 20:26:52 -060042 struct legacy_img_hdr *hdr = NULL;
Heiko Schocher21d29f72014-05-28 11:33:33 +020043#endif
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010044#if defined(CONFIG_FIT)
Bartlomiej Siekafbe7a152008-03-20 19:38:45 +010045 const char *uname = NULL;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010046 const void* fit_hdr;
47 int noffset;
48 const void *fit_data;
49 size_t fit_len;
50#endif
Simon Glassa92181b2013-04-17 16:13:45 +000051#ifdef CONFIG_GZIP
Nikita Shubin05e8e242022-12-19 11:05:27 +030052 uint unc_len = CONFIG_SYS_XIMG_LEN;
Simon Glassa92181b2013-04-17 16:13:45 +000053#endif
Wolfgang Wegner5912d362009-12-10 10:11:21 +010054 uint8_t comp;
wdenk48abe7b2004-06-09 10:15:00 +000055
Simon Glassbfebc8c2017-08-03 12:22:13 -060056 verify = env_get_yesno("verify");
wdenk48abe7b2004-06-09 10:15:00 +000057
58 if (argc > 1) {
Simon Glass7e5f4602021-07-24 09:03:29 -060059 addr = hextoul(argv[1], NULL);
wdenk48abe7b2004-06-09 10:15:00 +000060 }
61 if (argc > 2) {
Simon Glass7e5f4602021-07-24 09:03:29 -060062 part = hextoul(argv[2], NULL);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010063#if defined(CONFIG_FIT)
64 uname = argv[2];
65#endif
wdenk48abe7b2004-06-09 10:15:00 +000066 }
67 if (argc > 3) {
Simon Glass7e5f4602021-07-24 09:03:29 -060068 dest = hextoul(argv[3], NULL);
wdenk48abe7b2004-06-09 10:15:00 +000069 }
70
Stephen Warren712fbcf2011-10-18 11:11:49 +000071 switch (genimg_get_format((void *)addr)) {
Tom Rinic76c93a2019-05-23 07:14:07 -040072#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010073 case IMAGE_FORMAT_LEGACY:
wdenk48abe7b2004-06-09 10:15:00 +000074
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010075 printf("## Copying part %d from legacy image "
76 "at %08lx ...\n", part, addr);
77
Simon Glassf3543e62022-09-06 20:26:52 -060078 hdr = (struct legacy_img_hdr *)addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +000079 if (!image_check_magic(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010080 printf("Bad Magic Number\n");
wdenk48abe7b2004-06-09 10:15:00 +000081 return 1;
82 }
wdenk48abe7b2004-06-09 10:15:00 +000083
Stephen Warren712fbcf2011-10-18 11:11:49 +000084 if (!image_check_hcrc(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010085 printf("Bad Header Checksum\n");
86 return 1;
87 }
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010088#ifdef DEBUG
Stephen Warren712fbcf2011-10-18 11:11:49 +000089 image_print_contents(hdr);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +010090#endif
wdenk48abe7b2004-06-09 10:15:00 +000091
Pierre Aubert83636fa2015-09-16 08:29:11 +020092 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
93 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010094 printf("Wrong Image Type for %s command\n",
95 cmdtp->name);
96 return 1;
97 }
98
Stephen Warren712fbcf2011-10-18 11:11:49 +000099 comp = image_get_comp(hdr);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100100 if ((comp != IH_COMP_NONE) && (argc < 4)) {
101 printf("Must specify load address for %s command "
102 "with compressed image\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100103 cmdtp->name);
104 return 1;
105 }
106
107 if (verify) {
108 printf(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000109 if (!image_check_dcrc(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100110 printf("Bad Data CRC\n");
111 return 1;
112 }
113 printf("OK\n");
114 }
115
Stephen Warren712fbcf2011-10-18 11:11:49 +0000116 count = image_multi_count(hdr);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100117 if (part >= count) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100118 printf("Bad Image Part\n");
119 return 1;
120 }
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100121
Stephen Warren712fbcf2011-10-18 11:11:49 +0000122 image_multi_getimg(hdr, part, &data, &len);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100123 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200124#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100125#if defined(CONFIG_FIT)
126 case IMAGE_FORMAT_FIT:
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100127 if (uname == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000128 puts("No FIT subimage unit name\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100129 return 1;
130 }
131
132 printf("## Copying '%s' subimage from FIT image "
133 "at %08lx ...\n", uname, addr);
134
135 fit_hdr = (const void *)addr;
Simon Glassc5819702021-02-15 17:08:09 -0700136 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000137 puts("Bad FIT image format\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100138 return 1;
139 }
140
141 /* get subimage node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000142 noffset = fit_image_get_node(fit_hdr, uname);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100143 if (noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000144 printf("Can't find '%s' FIT subimage\n", uname);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100145 return 1;
146 }
147
Stefan Theil240e58e2019-01-07 10:25:52 +0100148 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100149 && (argc < 4)) {
150 printf("Must specify load address for %s command "
151 "with compressed image\n",
152 cmdtp->name);
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100153 return 1;
154 }
155
156 /* verify integrity */
157 if (verify) {
Simon Glassb8da8362013-05-07 06:11:57 +0000158 if (!fit_image_verify(fit_hdr, noffset)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000159 puts("Bad Data Hash\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100160 return 1;
161 }
162 }
163
Tien Fong Chee3695ea52019-02-12 20:49:30 +0800164 /* get subimage/external data address and length */
165 if (fit_image_get_data_and_size(fit_hdr, noffset,
166 &fit_data, &fit_len)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000167 puts("Could not find script subimage data\n");
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100168 return 1;
169 }
170
Daniel Golle88de6c52022-08-27 04:17:28 +0100171 if (fit_image_get_comp(fit_hdr, noffset, &comp))
172 comp = IH_COMP_NONE;
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100173
Bartlomiej Siekafbe7a152008-03-20 19:38:45 +0100174 data = (ulong)fit_data;
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100175 len = (ulong)fit_len;
176 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100177#endif
178 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000179 puts("Invalid image type for imxtract\n");
wdenk48abe7b2004-06-09 10:15:00 +0000180 return 1;
181 }
wdenk48abe7b2004-06-09 10:15:00 +0000182
183 if (argc > 3) {
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100184 switch (comp) {
185 case IH_COMP_NONE:
186#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
187 {
188 size_t l = len;
189 size_t tail;
190 void *to = (void *) dest;
191 void *from = (void *)data;
192
Stephen Warren712fbcf2011-10-18 11:11:49 +0000193 printf(" Loading part %d ... ", part);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100194
195 while (l > 0) {
196 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
Stefan Roese29caf932022-09-02 14:10:46 +0200197 schedule();
Stephen Warren712fbcf2011-10-18 11:11:49 +0000198 memmove(to, from, tail);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100199 to += tail;
200 from += tail;
201 l -= tail;
202 }
203 }
204#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000205 printf(" Loading part %d ... ", part);
206 memmove((char *) dest, (char *)data, len);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100207#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
208 break;
Matthew McClintock0e0996e2011-05-24 05:48:26 +0000209#ifdef CONFIG_GZIP
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100210 case IH_COMP_GZIP:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000211 printf(" Uncompressing part %d ... ", part);
212 if (gunzip((void *) dest, unc_len,
213 (uchar *) data, &len) != 0) {
214 puts("GUNZIP ERROR - image not loaded\n");
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100215 return 1;
216 }
217 break;
Matthew McClintock0e0996e2011-05-24 05:48:26 +0000218#endif
Tom Rinic76c93a2019-05-23 07:14:07 -0400219#if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100220 case IH_COMP_BZIP2:
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100221 {
222 int i;
223
Stephen Warren712fbcf2011-10-18 11:11:49 +0000224 printf(" Uncompressing part %d ... ", part);
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100225 /*
Wolfgang Denk93910ed2010-03-12 23:06:04 +0100226 * If we've got less than 4 MB of malloc()
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100227 * space, use slower decompression algorithm
228 * which requires at most 2300 KB of memory.
229 */
230 i = BZ2_bzBuffToBuffDecompress(
Simon Glass628af172013-08-30 11:00:09 -0600231 map_sysmem(ntohl(hdr->ih_load), 0),
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100232 &unc_len, (char *)data, len,
233 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
234 0);
235 if (i != BZ_OK) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000236 printf("BUNZIP2 ERROR %d - "
Wolfgang Denk5f566f42010-01-31 21:51:43 +0100237 "image not loaded\n", i);
238 return 1;
239 }
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100240 }
241 break;
242#endif /* CONFIG_BZIP2 */
Dmitry Gerasimov822911d2024-06-02 17:25:52 +0200243#if IS_ENABLED(CONFIG_ZSTD)
244 case IH_COMP_ZSTD:
245 {
246 int ret;
247 struct abuf in, out;
248
249 printf(" Uncompressing part %d ... ", part);
250
251 abuf_init_set(&in, (void *)data, len);
252 abuf_init_set(&out, (void *)dest, unc_len);
253 ret = zstd_decompress(&in, &out);
254 if (ret < 0) {
255 printf("ZSTD ERROR %d - "
256 "image not loaded\n", ret);
257 return 1;
258 }
259 len = ret;
260 }
261 break;
262#endif
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100263 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000264 printf("Unimplemented compression type %d\n", comp);
Wolfgang Wegner5912d362009-12-10 10:11:21 +0100265 return 1;
266 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000267 puts("OK\n");
wdenk48abe7b2004-06-09 10:15:00 +0000268 }
269
Mario Six8354aa22018-03-28 14:39:10 +0200270 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
Pieter Voorthuijsenc72b65c2015-01-12 16:23:18 +0100271
Simon Glass018f5302017-08-03 12:22:10 -0600272 env_set_hex("fileaddr", data);
273 env_set_hex("filesize", len);
wdenk48abe7b2004-06-09 10:15:00 +0000274
275 return 0;
276}
277
Tom Rini36162182023-10-07 15:13:08 -0400278U_BOOT_LONGHELP(imgextract,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200279 "addr part [dest]\n"
280 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100281#if defined(CONFIG_FIT)
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200282 "\n"
283 "addr uname [dest]\n"
284 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100285#endif
Tom Rini36162182023-10-07 15:13:08 -0400286 );
Kim Phillips088f1b12012-10-29 13:34:31 +0000287
288U_BOOT_CMD(
289 imxtract, 4, 1, do_imgextract,
290 "extract a part of a multi-image", imgextract_help_text
Marian Balakowicz1b7897f2008-03-12 10:33:00 +0100291);