blob: 0e7eead8d192e992f13b66767a096fe1ba5479ce [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/*
12 * Multi Image extract
13 */
14#include <common.h>
15#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070016#include <cpu_func.h>
Simon Glass313112a2019-08-01 09:46:46 -060017#include <env.h>
Simon Glass1a974af2019-08-01 09:46:36 -060018#include <gzip.h>
wdenk0359dde2004-06-09 10:15:00 +000019#include <image.h>
Simon Glass9bc15642020-02-03 07:36:16 -070020#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050021#include <mapmem.h>
Wolfgang Wegner781afa92009-12-10 10:11:21 +010022#include <watchdog.h>
23#if defined(CONFIG_BZIP2)
24#include <bzlib.h>
25#endif
wdenk0359dde2004-06-09 10:15:00 +000026#include <asm/byteorder.h>
Simon Glass274e0b02020-05-10 11:39:56 -060027#include <asm/cache.h>
Simon Glass208d8e82013-08-30 11:00:09 -060028#include <asm/io.h>
wdenk0359dde2004-06-09 10:15:00 +000029
Kim Phillipsdc00a682012-10-29 13:34:31 +000030static int
Simon Glassed38aef2020-05-10 11:40:03 -060031do_imgextract(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk0359dde2004-06-09 10:15:00 +000032{
Simon Glass892265d2019-12-28 10:45:02 -070033 ulong addr = image_load_addr;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010034 ulong dest = 0;
Heiko Schocher515eb122014-05-28 11:33:33 +020035 ulong data, len;
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010036 int verify;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010037 int part = 0;
Tom Rinic220bd92019-05-23 07:14:07 -040038#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Heiko Schocher515eb122014-05-28 11:33:33 +020039 ulong count;
Simon Glassbb7d3bb2022-09-06 20:26:52 -060040 struct legacy_img_hdr *hdr = NULL;
Heiko Schocher515eb122014-05-28 11:33:33 +020041#endif
Marian Balakowicz2321ed02008-03-12 10:33:00 +010042#if defined(CONFIG_FIT)
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010043 const char *uname = NULL;
Marian Balakowicz2321ed02008-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 Glassa0395272013-04-17 16:13:45 +000049#ifdef CONFIG_GZIP
Nikita Shubin25498ed2022-12-19 11:05:27 +030050 uint unc_len = CONFIG_SYS_XIMG_LEN;
Simon Glassa0395272013-04-17 16:13:45 +000051#endif
Wolfgang Wegner781afa92009-12-10 10:11:21 +010052 uint8_t comp;
wdenk0359dde2004-06-09 10:15:00 +000053
Simon Glass22c34c22017-08-03 12:22:13 -060054 verify = env_get_yesno("verify");
wdenk0359dde2004-06-09 10:15:00 +000055
56 if (argc > 1) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060057 addr = hextoul(argv[1], NULL);
wdenk0359dde2004-06-09 10:15:00 +000058 }
59 if (argc > 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060060 part = hextoul(argv[2], NULL);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010061#if defined(CONFIG_FIT)
62 uname = argv[2];
63#endif
wdenk0359dde2004-06-09 10:15:00 +000064 }
65 if (argc > 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060066 dest = hextoul(argv[3], NULL);
wdenk0359dde2004-06-09 10:15:00 +000067 }
68
Stephen Warren6904b6e2011-10-18 11:11:49 +000069 switch (genimg_get_format((void *)addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -040070#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010071 case IMAGE_FORMAT_LEGACY:
wdenk0359dde2004-06-09 10:15:00 +000072
Marian Balakowicz2321ed02008-03-12 10:33:00 +010073 printf("## Copying part %d from legacy image "
74 "at %08lx ...\n", part, addr);
75
Simon Glassbb7d3bb2022-09-06 20:26:52 -060076 hdr = (struct legacy_img_hdr *)addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +000077 if (!image_check_magic(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010078 printf("Bad Magic Number\n");
79 return 1;
80 }
wdenk0359dde2004-06-09 10:15:00 +000081
Stephen Warren6904b6e2011-10-18 11:11:49 +000082 if (!image_check_hcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010083 printf("Bad Header Checksum\n");
84 return 1;
85 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +010086#ifdef DEBUG
Stephen Warren6904b6e2011-10-18 11:11:49 +000087 image_print_contents(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010088#endif
wdenk0359dde2004-06-09 10:15:00 +000089
Pierre Aubertde4e9492015-09-16 08:29:11 +020090 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
91 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010092 printf("Wrong Image Type for %s command\n",
93 cmdtp->name);
94 return 1;
95 }
wdenk0359dde2004-06-09 10:15:00 +000096
Stephen Warren6904b6e2011-10-18 11:11:49 +000097 comp = image_get_comp(hdr);
Wolfgang Wegner781afa92009-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 Balakowiczdbdd16a2008-02-04 08:28:09 +0100101 cmdtp->name);
wdenk0359dde2004-06-09 10:15:00 +0000102 return 1;
103 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100104
105 if (verify) {
106 printf(" Verifying Checksum ... ");
Stephen Warren6904b6e2011-10-18 11:11:49 +0000107 if (!image_check_dcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100108 printf("Bad Data CRC\n");
109 return 1;
110 }
111 printf("OK\n");
112 }
wdenk0359dde2004-06-09 10:15:00 +0000113
Stephen Warren6904b6e2011-10-18 11:11:49 +0000114 count = image_multi_count(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100115 if (part >= count) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100116 printf("Bad Image Part\n");
117 return 1;
118 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100119
Stephen Warren6904b6e2011-10-18 11:11:49 +0000120 image_multi_getimg(hdr, part, &data, &len);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100121 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200122#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100123#if defined(CONFIG_FIT)
124 case IMAGE_FORMAT_FIT:
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100125 if (uname == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000126 puts("No FIT subimage unit name\n");
Marian Balakowicz2321ed02008-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;
Simon Glassd563c252021-02-15 17:08:09 -0700134 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000135 puts("Bad FIT image format\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100136 return 1;
137 }
138
139 /* get subimage node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000140 noffset = fit_image_get_node(fit_hdr, uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100141 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000142 printf("Can't find '%s' FIT subimage\n", uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100143 return 1;
144 }
145
Stefan Theil69b0ef52019-01-07 10:25:52 +0100146 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
Wolfgang Wegner781afa92009-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 Balakowicz2321ed02008-03-12 10:33:00 +0100151 return 1;
152 }
153
154 /* verify integrity */
155 if (verify) {
Simon Glass7428ad12013-05-07 06:11:57 +0000156 if (!fit_image_verify(fit_hdr, noffset)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000157 puts("Bad Data Hash\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100158 return 1;
159 }
160 }
161
Tien Fong Chee8a869582019-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 Warren6904b6e2011-10-18 11:11:49 +0000165 puts("Could not find script subimage data\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100166 return 1;
167 }
168
Daniel Golle2ebfd222022-08-27 04:17:28 +0100169 if (fit_image_get_comp(fit_hdr, noffset, &comp))
170 comp = IH_COMP_NONE;
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100171
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +0100172 data = (ulong)fit_data;
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100173 len = (ulong)fit_len;
174 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100175#endif
176 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000177 puts("Invalid image type for imxtract\n");
wdenk0359dde2004-06-09 10:15:00 +0000178 return 1;
179 }
wdenk0359dde2004-06-09 10:15:00 +0000180
181 if (argc > 3) {
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100182 switch (comp) {
183 case IH_COMP_NONE:
184#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
185 {
186 size_t l = len;
187 size_t tail;
188 void *to = (void *) dest;
189 void *from = (void *)data;
190
Stephen Warren6904b6e2011-10-18 11:11:49 +0000191 printf(" Loading part %d ... ", part);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100192
193 while (l > 0) {
194 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
Stefan Roese80877fa2022-09-02 14:10:46 +0200195 schedule();
Stephen Warren6904b6e2011-10-18 11:11:49 +0000196 memmove(to, from, tail);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100197 to += tail;
198 from += tail;
199 l -= tail;
200 }
201 }
202#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000203 printf(" Loading part %d ... ", part);
204 memmove((char *) dest, (char *)data, len);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100205#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
206 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000207#ifdef CONFIG_GZIP
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100208 case IH_COMP_GZIP:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000209 printf(" Uncompressing part %d ... ", part);
210 if (gunzip((void *) dest, unc_len,
211 (uchar *) data, &len) != 0) {
212 puts("GUNZIP ERROR - image not loaded\n");
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100213 return 1;
214 }
215 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000216#endif
Tom Rinic220bd92019-05-23 07:14:07 -0400217#if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100218 case IH_COMP_BZIP2:
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100219 {
220 int i;
221
Stephen Warren6904b6e2011-10-18 11:11:49 +0000222 printf(" Uncompressing part %d ... ", part);
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100223 /*
Wolfgang Denka2cfb242010-03-12 23:06:04 +0100224 * If we've got less than 4 MB of malloc()
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100225 * space, use slower decompression algorithm
226 * which requires at most 2300 KB of memory.
227 */
228 i = BZ2_bzBuffToBuffDecompress(
Simon Glass208d8e82013-08-30 11:00:09 -0600229 map_sysmem(ntohl(hdr->ih_load), 0),
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100230 &unc_len, (char *)data, len,
231 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
232 0);
233 if (i != BZ_OK) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000234 printf("BUNZIP2 ERROR %d - "
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100235 "image not loaded\n", i);
236 return 1;
237 }
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100238 }
239 break;
240#endif /* CONFIG_BZIP2 */
241 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000242 printf("Unimplemented compression type %d\n", comp);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100243 return 1;
244 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000245 puts("OK\n");
wdenk0359dde2004-06-09 10:15:00 +0000246 }
247
Mario Six7feefe42018-03-28 14:39:10 +0200248 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
Pieter Voorthuijsenf52d2812015-01-12 16:23:18 +0100249
Simon Glass4d949a22017-08-03 12:22:10 -0600250 env_set_hex("fileaddr", data);
251 env_set_hex("filesize", len);
wdenk0359dde2004-06-09 10:15:00 +0000252
253 return 0;
254}
255
Tom Rini03f146c2023-10-07 15:13:08 -0400256U_BOOT_LONGHELP(imgextract,
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200257 "addr part [dest]\n"
258 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100259#if defined(CONFIG_FIT)
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200260 "\n"
261 "addr uname [dest]\n"
262 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100263#endif
Tom Rini03f146c2023-10-07 15:13:08 -0400264 );
Kim Phillipsdc00a682012-10-29 13:34:31 +0000265
266U_BOOT_CMD(
267 imxtract, 4, 1, do_imgextract,
268 "extract a part of a multi-image", imgextract_help_text
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100269);