blob: 1c96f5a0a1f680a9d7b98a039287a19ba48be2a6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk0359dde2004-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>
wdenk0359dde2004-06-09 10:15:00 +00008 */
9
wdenk0359dde2004-06-09 10:15:00 +000010/*
11 * Multi Image extract
12 */
wdenk0359dde2004-06-09 10:15:00 +000013#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070014#include <cpu_func.h>
Simon Glass313112a2019-08-01 09:46:46 -060015#include <env.h>
Simon Glass1a974af2019-08-01 09:46:36 -060016#include <gzip.h>
Dmitry Gerasimovf57988d2024-06-02 17:25:52 +020017#if IS_ENABLED(CONFIG_ZSTD)
18#include <linux/zstd.h>
19#endif
wdenk0359dde2004-06-09 10:15:00 +000020#include <image.h>
Simon Glass9bc15642020-02-03 07:36:16 -070021#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050022#include <mapmem.h>
Wolfgang Wegner781afa92009-12-10 10:11:21 +010023#include <watchdog.h>
24#if defined(CONFIG_BZIP2)
25#include <bzlib.h>
26#endif
wdenk0359dde2004-06-09 10:15:00 +000027#include <asm/byteorder.h>
Simon Glass274e0b02020-05-10 11:39:56 -060028#include <asm/cache.h>
Simon Glass208d8e82013-08-30 11:00:09 -060029#include <asm/io.h>
wdenk0359dde2004-06-09 10:15:00 +000030
Kim Phillipsdc00a682012-10-29 13:34:31 +000031static int
Simon Glassed38aef2020-05-10 11:40:03 -060032do_imgextract(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk0359dde2004-06-09 10:15:00 +000033{
Simon Glass892265d2019-12-28 10:45:02 -070034 ulong addr = image_load_addr;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010035 ulong dest = 0;
Heiko Schocher515eb122014-05-28 11:33:33 +020036 ulong data, len;
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010037 int verify;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010038 int part = 0;
Tom Rinic220bd92019-05-23 07:14:07 -040039#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Heiko Schocher515eb122014-05-28 11:33:33 +020040 ulong count;
Simon Glassbb7d3bb2022-09-06 20:26:52 -060041 struct legacy_img_hdr *hdr = NULL;
Heiko Schocher515eb122014-05-28 11:33:33 +020042#endif
Marian Balakowicz2321ed02008-03-12 10:33:00 +010043#if defined(CONFIG_FIT)
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010044 const char *uname = NULL;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010045 const void* fit_hdr;
46 int noffset;
47 const void *fit_data;
48 size_t fit_len;
49#endif
Simon Glassa0395272013-04-17 16:13:45 +000050#ifdef CONFIG_GZIP
Nikita Shubin25498ed2022-12-19 11:05:27 +030051 uint unc_len = CONFIG_SYS_XIMG_LEN;
Simon Glassa0395272013-04-17 16:13:45 +000052#endif
Wolfgang Wegner781afa92009-12-10 10:11:21 +010053 uint8_t comp;
wdenk0359dde2004-06-09 10:15:00 +000054
Simon Glass22c34c22017-08-03 12:22:13 -060055 verify = env_get_yesno("verify");
wdenk0359dde2004-06-09 10:15:00 +000056
57 if (argc > 1) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060058 addr = hextoul(argv[1], NULL);
wdenk0359dde2004-06-09 10:15:00 +000059 }
60 if (argc > 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060061 part = hextoul(argv[2], NULL);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010062#if defined(CONFIG_FIT)
63 uname = argv[2];
64#endif
wdenk0359dde2004-06-09 10:15:00 +000065 }
66 if (argc > 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060067 dest = hextoul(argv[3], NULL);
wdenk0359dde2004-06-09 10:15:00 +000068 }
69
Stephen Warren6904b6e2011-10-18 11:11:49 +000070 switch (genimg_get_format((void *)addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -040071#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010072 case IMAGE_FORMAT_LEGACY:
wdenk0359dde2004-06-09 10:15:00 +000073
Marian Balakowicz2321ed02008-03-12 10:33:00 +010074 printf("## Copying part %d from legacy image "
75 "at %08lx ...\n", part, addr);
76
Simon Glassbb7d3bb2022-09-06 20:26:52 -060077 hdr = (struct legacy_img_hdr *)addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +000078 if (!image_check_magic(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010079 printf("Bad Magic Number\n");
80 return 1;
81 }
wdenk0359dde2004-06-09 10:15:00 +000082
Stephen Warren6904b6e2011-10-18 11:11:49 +000083 if (!image_check_hcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010084 printf("Bad Header Checksum\n");
85 return 1;
86 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +010087#ifdef DEBUG
Stephen Warren6904b6e2011-10-18 11:11:49 +000088 image_print_contents(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010089#endif
wdenk0359dde2004-06-09 10:15:00 +000090
Pierre Aubertde4e9492015-09-16 08:29:11 +020091 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
92 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010093 printf("Wrong Image Type for %s command\n",
94 cmdtp->name);
95 return 1;
96 }
wdenk0359dde2004-06-09 10:15:00 +000097
Stephen Warren6904b6e2011-10-18 11:11:49 +000098 comp = image_get_comp(hdr);
Wolfgang Wegner781afa92009-12-10 10:11:21 +010099 if ((comp != IH_COMP_NONE) && (argc < 4)) {
100 printf("Must specify load address for %s command "
101 "with compressed image\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100102 cmdtp->name);
wdenk0359dde2004-06-09 10:15:00 +0000103 return 1;
104 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100105
106 if (verify) {
107 printf(" Verifying Checksum ... ");
Stephen Warren6904b6e2011-10-18 11:11:49 +0000108 if (!image_check_dcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100109 printf("Bad Data CRC\n");
110 return 1;
111 }
112 printf("OK\n");
113 }
wdenk0359dde2004-06-09 10:15:00 +0000114
Stephen Warren6904b6e2011-10-18 11:11:49 +0000115 count = image_multi_count(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100116 if (part >= count) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100117 printf("Bad Image Part\n");
118 return 1;
119 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100120
Stephen Warren6904b6e2011-10-18 11:11:49 +0000121 image_multi_getimg(hdr, part, &data, &len);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100122 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200123#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100124#if defined(CONFIG_FIT)
125 case IMAGE_FORMAT_FIT:
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100126 if (uname == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000127 puts("No FIT subimage unit name\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100128 return 1;
129 }
130
131 printf("## Copying '%s' subimage from FIT image "
132 "at %08lx ...\n", uname, addr);
133
134 fit_hdr = (const void *)addr;
Simon Glassd563c252021-02-15 17:08:09 -0700135 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000136 puts("Bad FIT image format\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100137 return 1;
138 }
139
140 /* get subimage node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000141 noffset = fit_image_get_node(fit_hdr, uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100142 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000143 printf("Can't find '%s' FIT subimage\n", uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100144 return 1;
145 }
146
Stefan Theil69b0ef52019-01-07 10:25:52 +0100147 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100148 && (argc < 4)) {
149 printf("Must specify load address for %s command "
150 "with compressed image\n",
151 cmdtp->name);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100152 return 1;
153 }
154
155 /* verify integrity */
156 if (verify) {
Simon Glass7428ad12013-05-07 06:11:57 +0000157 if (!fit_image_verify(fit_hdr, noffset)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000158 puts("Bad Data Hash\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100159 return 1;
160 }
161 }
162
Tien Fong Chee8a869582019-02-12 20:49:30 +0800163 /* get subimage/external data address and length */
164 if (fit_image_get_data_and_size(fit_hdr, noffset,
165 &fit_data, &fit_len)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000166 puts("Could not find script subimage data\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100167 return 1;
168 }
169
Daniel Golle2ebfd222022-08-27 04:17:28 +0100170 if (fit_image_get_comp(fit_hdr, noffset, &comp))
171 comp = IH_COMP_NONE;
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100172
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +0100173 data = (ulong)fit_data;
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100174 len = (ulong)fit_len;
175 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100176#endif
177 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000178 puts("Invalid image type for imxtract\n");
wdenk0359dde2004-06-09 10:15:00 +0000179 return 1;
180 }
wdenk0359dde2004-06-09 10:15:00 +0000181
182 if (argc > 3) {
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100183 switch (comp) {
184 case IH_COMP_NONE:
185#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
186 {
187 size_t l = len;
188 size_t tail;
189 void *to = (void *) dest;
190 void *from = (void *)data;
191
Stephen Warren6904b6e2011-10-18 11:11:49 +0000192 printf(" Loading part %d ... ", part);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100193
194 while (l > 0) {
195 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
Stefan Roese80877fa2022-09-02 14:10:46 +0200196 schedule();
Stephen Warren6904b6e2011-10-18 11:11:49 +0000197 memmove(to, from, tail);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100198 to += tail;
199 from += tail;
200 l -= tail;
201 }
202 }
203#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000204 printf(" Loading part %d ... ", part);
205 memmove((char *) dest, (char *)data, len);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100206#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
207 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000208#ifdef CONFIG_GZIP
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100209 case IH_COMP_GZIP:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000210 printf(" Uncompressing part %d ... ", part);
211 if (gunzip((void *) dest, unc_len,
212 (uchar *) data, &len) != 0) {
213 puts("GUNZIP ERROR - image not loaded\n");
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100214 return 1;
215 }
216 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000217#endif
Tom Rinic220bd92019-05-23 07:14:07 -0400218#if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100219 case IH_COMP_BZIP2:
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100220 {
221 int i;
222
Stephen Warren6904b6e2011-10-18 11:11:49 +0000223 printf(" Uncompressing part %d ... ", part);
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100224 /*
Wolfgang Denka2cfb242010-03-12 23:06:04 +0100225 * If we've got less than 4 MB of malloc()
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100226 * space, use slower decompression algorithm
227 * which requires at most 2300 KB of memory.
228 */
229 i = BZ2_bzBuffToBuffDecompress(
Simon Glass208d8e82013-08-30 11:00:09 -0600230 map_sysmem(ntohl(hdr->ih_load), 0),
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100231 &unc_len, (char *)data, len,
232 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
233 0);
234 if (i != BZ_OK) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000235 printf("BUNZIP2 ERROR %d - "
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100236 "image not loaded\n", i);
237 return 1;
238 }
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100239 }
240 break;
241#endif /* CONFIG_BZIP2 */
Dmitry Gerasimovf57988d2024-06-02 17:25:52 +0200242#if IS_ENABLED(CONFIG_ZSTD)
243 case IH_COMP_ZSTD:
244 {
245 int ret;
246 struct abuf in, out;
247
248 printf(" Uncompressing part %d ... ", part);
249
250 abuf_init_set(&in, (void *)data, len);
251 abuf_init_set(&out, (void *)dest, unc_len);
252 ret = zstd_decompress(&in, &out);
253 if (ret < 0) {
254 printf("ZSTD ERROR %d - "
255 "image not loaded\n", ret);
256 return 1;
257 }
258 len = ret;
259 }
260 break;
261#endif
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100262 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000263 printf("Unimplemented compression type %d\n", comp);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100264 return 1;
265 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000266 puts("OK\n");
wdenk0359dde2004-06-09 10:15:00 +0000267 }
268
Mario Six7feefe42018-03-28 14:39:10 +0200269 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
Pieter Voorthuijsenf52d2812015-01-12 16:23:18 +0100270
Simon Glass4d949a22017-08-03 12:22:10 -0600271 env_set_hex("fileaddr", data);
272 env_set_hex("filesize", len);
wdenk0359dde2004-06-09 10:15:00 +0000273
274 return 0;
275}
276
Tom Rini03f146c2023-10-07 15:13:08 -0400277U_BOOT_LONGHELP(imgextract,
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200278 "addr part [dest]\n"
279 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100280#if defined(CONFIG_FIT)
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200281 "\n"
282 "addr uname [dest]\n"
283 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100284#endif
Tom Rini03f146c2023-10-07 15:13:08 -0400285 );
Kim Phillipsdc00a682012-10-29 13:34:31 +0000286
287U_BOOT_CMD(
288 imxtract, 4, 1, do_imgextract,
289 "extract a part of a multi-image", imgextract_help_text
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100290);