blob: a9481004f0369ebf95ebf4b6ee9f6fd25b806f9d [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 Glass313112a2019-08-01 09:46:46 -060016#include <env.h>
Simon Glass1a974af2019-08-01 09:46:36 -060017#include <gzip.h>
wdenk0359dde2004-06-09 10:15:00 +000018#include <image.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050019#include <mapmem.h>
Wolfgang Wegner781afa92009-12-10 10:11:21 +010020#include <watchdog.h>
21#if defined(CONFIG_BZIP2)
22#include <bzlib.h>
23#endif
wdenk0359dde2004-06-09 10:15:00 +000024#include <asm/byteorder.h>
Simon Glass208d8e82013-08-30 11:00:09 -060025#include <asm/io.h>
wdenk0359dde2004-06-09 10:15:00 +000026
Wolfgang Wegner781afa92009-12-10 10:11:21 +010027#ifndef CONFIG_SYS_XIMG_LEN
28/* use 8MByte as default max gunzip size */
29#define CONFIG_SYS_XIMG_LEN 0x800000
30#endif
31
Kim Phillipsdc00a682012-10-29 13:34:31 +000032static int
Wolfgang Denk6262d0212010-06-28 22:00:46 +020033do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk0359dde2004-06-09 10:15:00 +000034{
Marian Balakowicz2321ed02008-03-12 10:33:00 +010035 ulong addr = load_addr;
36 ulong dest = 0;
Heiko Schocher515eb122014-05-28 11:33:33 +020037 ulong data, len;
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010038 int verify;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010039 int part = 0;
Tom Rinic220bd92019-05-23 07:14:07 -040040#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Heiko Schocher515eb122014-05-28 11:33:33 +020041 ulong count;
Simon Glass208d8e82013-08-30 11:00:09 -060042 image_header_t *hdr = NULL;
Heiko Schocher515eb122014-05-28 11:33:33 +020043#endif
Marian Balakowicz2321ed02008-03-12 10:33:00 +010044#if defined(CONFIG_FIT)
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010045 const char *uname = NULL;
Marian Balakowicz2321ed02008-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 Glassa0395272013-04-17 16:13:45 +000051#ifdef CONFIG_GZIP
Wolfgang Wegner781afa92009-12-10 10:11:21 +010052 uint unc_len = CONFIG_SYS_XIMG_LEN;
Simon Glassa0395272013-04-17 16:13:45 +000053#endif
Wolfgang Wegner781afa92009-12-10 10:11:21 +010054 uint8_t comp;
wdenk0359dde2004-06-09 10:15:00 +000055
Simon Glass22c34c22017-08-03 12:22:13 -060056 verify = env_get_yesno("verify");
wdenk0359dde2004-06-09 10:15:00 +000057
58 if (argc > 1) {
59 addr = simple_strtoul(argv[1], NULL, 16);
60 }
61 if (argc > 2) {
62 part = simple_strtoul(argv[2], NULL, 16);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010063#if defined(CONFIG_FIT)
64 uname = argv[2];
65#endif
wdenk0359dde2004-06-09 10:15:00 +000066 }
67 if (argc > 3) {
68 dest = simple_strtoul(argv[3], NULL, 16);
69 }
70
Stephen Warren6904b6e2011-10-18 11:11:49 +000071 switch (genimg_get_format((void *)addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -040072#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010073 case IMAGE_FORMAT_LEGACY:
wdenk0359dde2004-06-09 10:15:00 +000074
Marian Balakowicz2321ed02008-03-12 10:33:00 +010075 printf("## Copying part %d from legacy image "
76 "at %08lx ...\n", part, addr);
77
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010078 hdr = (image_header_t *)addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +000079 if (!image_check_magic(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010080 printf("Bad Magic Number\n");
81 return 1;
82 }
wdenk0359dde2004-06-09 10:15:00 +000083
Stephen Warren6904b6e2011-10-18 11:11:49 +000084 if (!image_check_hcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010085 printf("Bad Header Checksum\n");
86 return 1;
87 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +010088#ifdef DEBUG
Stephen Warren6904b6e2011-10-18 11:11:49 +000089 image_print_contents(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010090#endif
wdenk0359dde2004-06-09 10:15:00 +000091
Pierre Aubertde4e9492015-09-16 08:29:11 +020092 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
93 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010094 printf("Wrong Image Type for %s command\n",
95 cmdtp->name);
96 return 1;
97 }
wdenk0359dde2004-06-09 10:15:00 +000098
Stephen Warren6904b6e2011-10-18 11:11:49 +000099 comp = image_get_comp(hdr);
Wolfgang Wegner781afa92009-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 Balakowiczdbdd16a2008-02-04 08:28:09 +0100103 cmdtp->name);
wdenk0359dde2004-06-09 10:15:00 +0000104 return 1;
105 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100106
107 if (verify) {
108 printf(" Verifying Checksum ... ");
Stephen Warren6904b6e2011-10-18 11:11:49 +0000109 if (!image_check_dcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100110 printf("Bad Data CRC\n");
111 return 1;
112 }
113 printf("OK\n");
114 }
wdenk0359dde2004-06-09 10:15:00 +0000115
Stephen Warren6904b6e2011-10-18 11:11:49 +0000116 count = image_multi_count(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100117 if (part >= count) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100118 printf("Bad Image Part\n");
119 return 1;
120 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100121
Stephen Warren6904b6e2011-10-18 11:11:49 +0000122 image_multi_getimg(hdr, part, &data, &len);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100123 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200124#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100125#if defined(CONFIG_FIT)
126 case IMAGE_FORMAT_FIT:
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100127 if (uname == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000128 puts("No FIT subimage unit name\n");
Marian Balakowicz2321ed02008-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;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000136 if (!fit_check_format(fit_hdr)) {
137 puts("Bad FIT image format\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100138 return 1;
139 }
140
141 /* get subimage node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000142 noffset = fit_image_get_node(fit_hdr, uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100143 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000144 printf("Can't find '%s' FIT subimage\n", uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100145 return 1;
146 }
147
Stefan Theil69b0ef52019-01-07 10:25:52 +0100148 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
Wolfgang Wegner781afa92009-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 Balakowicz2321ed02008-03-12 10:33:00 +0100153 return 1;
154 }
155
156 /* verify integrity */
157 if (verify) {
Simon Glass7428ad12013-05-07 06:11:57 +0000158 if (!fit_image_verify(fit_hdr, noffset)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000159 puts("Bad Data Hash\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100160 return 1;
161 }
162 }
163
Tien Fong Chee8a869582019-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 Warren6904b6e2011-10-18 11:11:49 +0000167 puts("Could not find script subimage data\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100168 return 1;
169 }
170
Stephen Warren6904b6e2011-10-18 11:11:49 +0000171 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
172 puts("Could not find script subimage "
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100173 "compression type\n");
174 return 1;
175 }
176
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +0100177 data = (ulong)fit_data;
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100178 len = (ulong)fit_len;
179 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100180#endif
181 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000182 puts("Invalid image type for imxtract\n");
wdenk0359dde2004-06-09 10:15:00 +0000183 return 1;
184 }
wdenk0359dde2004-06-09 10:15:00 +0000185
186 if (argc > 3) {
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100187 switch (comp) {
188 case IH_COMP_NONE:
189#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
190 {
191 size_t l = len;
192 size_t tail;
193 void *to = (void *) dest;
194 void *from = (void *)data;
195
Stephen Warren6904b6e2011-10-18 11:11:49 +0000196 printf(" Loading part %d ... ", part);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100197
198 while (l > 0) {
199 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
200 WATCHDOG_RESET();
Stephen Warren6904b6e2011-10-18 11:11:49 +0000201 memmove(to, from, tail);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100202 to += tail;
203 from += tail;
204 l -= tail;
205 }
206 }
207#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000208 printf(" Loading part %d ... ", part);
209 memmove((char *) dest, (char *)data, len);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100210#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
211 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000212#ifdef CONFIG_GZIP
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100213 case IH_COMP_GZIP:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000214 printf(" Uncompressing part %d ... ", part);
215 if (gunzip((void *) dest, unc_len,
216 (uchar *) data, &len) != 0) {
217 puts("GUNZIP ERROR - image not loaded\n");
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100218 return 1;
219 }
220 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000221#endif
Tom Rinic220bd92019-05-23 07:14:07 -0400222#if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100223 case IH_COMP_BZIP2:
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100224 {
225 int i;
226
Stephen Warren6904b6e2011-10-18 11:11:49 +0000227 printf(" Uncompressing part %d ... ", part);
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100228 /*
Wolfgang Denka2cfb242010-03-12 23:06:04 +0100229 * If we've got less than 4 MB of malloc()
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100230 * space, use slower decompression algorithm
231 * which requires at most 2300 KB of memory.
232 */
233 i = BZ2_bzBuffToBuffDecompress(
Simon Glass208d8e82013-08-30 11:00:09 -0600234 map_sysmem(ntohl(hdr->ih_load), 0),
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100235 &unc_len, (char *)data, len,
236 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
237 0);
238 if (i != BZ_OK) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000239 printf("BUNZIP2 ERROR %d - "
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100240 "image not loaded\n", i);
241 return 1;
242 }
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100243 }
244 break;
245#endif /* CONFIG_BZIP2 */
246 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000247 printf("Unimplemented compression type %d\n", comp);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100248 return 1;
249 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000250 puts("OK\n");
wdenk0359dde2004-06-09 10:15:00 +0000251 }
252
Mario Six7feefe42018-03-28 14:39:10 +0200253 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
Pieter Voorthuijsenf52d2812015-01-12 16:23:18 +0100254
Simon Glass4d949a22017-08-03 12:22:10 -0600255 env_set_hex("fileaddr", data);
256 env_set_hex("filesize", len);
wdenk0359dde2004-06-09 10:15:00 +0000257
258 return 0;
259}
260
Kim Phillipsdc00a682012-10-29 13:34:31 +0000261#ifdef CONFIG_SYS_LONGHELP
262static char imgextract_help_text[] =
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200263 "addr part [dest]\n"
264 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100265#if defined(CONFIG_FIT)
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200266 "\n"
267 "addr uname [dest]\n"
268 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100269#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000270 "";
271#endif
272
273U_BOOT_CMD(
274 imxtract, 4, 1, do_imgextract,
275 "extract a part of a multi-image", imgextract_help_text
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100276);