blob: 22b2037a33f51187a5c024717811d372a434d99b [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>
Joe Hershberger65b905b2015-03-22 17:08:59 -050020#include <mapmem.h>
Wolfgang Wegner781afa92009-12-10 10:11:21 +010021#include <watchdog.h>
22#if defined(CONFIG_BZIP2)
23#include <bzlib.h>
24#endif
wdenk0359dde2004-06-09 10:15:00 +000025#include <asm/byteorder.h>
Simon Glass208d8e82013-08-30 11:00:09 -060026#include <asm/io.h>
wdenk0359dde2004-06-09 10:15:00 +000027
Wolfgang Wegner781afa92009-12-10 10:11:21 +010028#ifndef CONFIG_SYS_XIMG_LEN
29/* use 8MByte as default max gunzip size */
30#define CONFIG_SYS_XIMG_LEN 0x800000
31#endif
32
Kim Phillipsdc00a682012-10-29 13:34:31 +000033static int
Wolfgang Denk6262d0212010-06-28 22:00:46 +020034do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk0359dde2004-06-09 10:15:00 +000035{
Marian Balakowicz2321ed02008-03-12 10:33:00 +010036 ulong addr = load_addr;
37 ulong dest = 0;
Heiko Schocher515eb122014-05-28 11:33:33 +020038 ulong data, len;
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010039 int verify;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010040 int part = 0;
Tom Rinic220bd92019-05-23 07:14:07 -040041#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Heiko Schocher515eb122014-05-28 11:33:33 +020042 ulong count;
Simon Glass208d8e82013-08-30 11:00:09 -060043 image_header_t *hdr = NULL;
Heiko Schocher515eb122014-05-28 11:33:33 +020044#endif
Marian Balakowicz2321ed02008-03-12 10:33:00 +010045#if defined(CONFIG_FIT)
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010046 const char *uname = NULL;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010047 const void* fit_hdr;
48 int noffset;
49 const void *fit_data;
50 size_t fit_len;
51#endif
Simon Glassa0395272013-04-17 16:13:45 +000052#ifdef CONFIG_GZIP
Wolfgang Wegner781afa92009-12-10 10:11:21 +010053 uint unc_len = CONFIG_SYS_XIMG_LEN;
Simon Glassa0395272013-04-17 16:13:45 +000054#endif
Wolfgang Wegner781afa92009-12-10 10:11:21 +010055 uint8_t comp;
wdenk0359dde2004-06-09 10:15:00 +000056
Simon Glass22c34c22017-08-03 12:22:13 -060057 verify = env_get_yesno("verify");
wdenk0359dde2004-06-09 10:15:00 +000058
59 if (argc > 1) {
60 addr = simple_strtoul(argv[1], NULL, 16);
61 }
62 if (argc > 2) {
63 part = simple_strtoul(argv[2], NULL, 16);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010064#if defined(CONFIG_FIT)
65 uname = argv[2];
66#endif
wdenk0359dde2004-06-09 10:15:00 +000067 }
68 if (argc > 3) {
69 dest = simple_strtoul(argv[3], NULL, 16);
70 }
71
Stephen Warren6904b6e2011-10-18 11:11:49 +000072 switch (genimg_get_format((void *)addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -040073#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010074 case IMAGE_FORMAT_LEGACY:
wdenk0359dde2004-06-09 10:15:00 +000075
Marian Balakowicz2321ed02008-03-12 10:33:00 +010076 printf("## Copying part %d from legacy image "
77 "at %08lx ...\n", part, addr);
78
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010079 hdr = (image_header_t *)addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +000080 if (!image_check_magic(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010081 printf("Bad Magic Number\n");
82 return 1;
83 }
wdenk0359dde2004-06-09 10:15:00 +000084
Stephen Warren6904b6e2011-10-18 11:11:49 +000085 if (!image_check_hcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010086 printf("Bad Header Checksum\n");
87 return 1;
88 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +010089#ifdef DEBUG
Stephen Warren6904b6e2011-10-18 11:11:49 +000090 image_print_contents(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010091#endif
wdenk0359dde2004-06-09 10:15:00 +000092
Pierre Aubertde4e9492015-09-16 08:29:11 +020093 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
94 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010095 printf("Wrong Image Type for %s command\n",
96 cmdtp->name);
97 return 1;
98 }
wdenk0359dde2004-06-09 10:15:00 +000099
Stephen Warren6904b6e2011-10-18 11:11:49 +0000100 comp = image_get_comp(hdr);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100101 if ((comp != IH_COMP_NONE) && (argc < 4)) {
102 printf("Must specify load address for %s command "
103 "with compressed image\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100104 cmdtp->name);
wdenk0359dde2004-06-09 10:15:00 +0000105 return 1;
106 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100107
108 if (verify) {
109 printf(" Verifying Checksum ... ");
Stephen Warren6904b6e2011-10-18 11:11:49 +0000110 if (!image_check_dcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100111 printf("Bad Data CRC\n");
112 return 1;
113 }
114 printf("OK\n");
115 }
wdenk0359dde2004-06-09 10:15:00 +0000116
Stephen Warren6904b6e2011-10-18 11:11:49 +0000117 count = image_multi_count(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100118 if (part >= count) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100119 printf("Bad Image Part\n");
120 return 1;
121 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100122
Stephen Warren6904b6e2011-10-18 11:11:49 +0000123 image_multi_getimg(hdr, part, &data, &len);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100124 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200125#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100126#if defined(CONFIG_FIT)
127 case IMAGE_FORMAT_FIT:
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100128 if (uname == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000129 puts("No FIT subimage unit name\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100130 return 1;
131 }
132
133 printf("## Copying '%s' subimage from FIT image "
134 "at %08lx ...\n", uname, addr);
135
136 fit_hdr = (const void *)addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000137 if (!fit_check_format(fit_hdr)) {
138 puts("Bad FIT image format\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100139 return 1;
140 }
141
142 /* get subimage node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000143 noffset = fit_image_get_node(fit_hdr, uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100144 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000145 printf("Can't find '%s' FIT subimage\n", uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100146 return 1;
147 }
148
Stefan Theil69b0ef52019-01-07 10:25:52 +0100149 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100150 && (argc < 4)) {
151 printf("Must specify load address for %s command "
152 "with compressed image\n",
153 cmdtp->name);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100154 return 1;
155 }
156
157 /* verify integrity */
158 if (verify) {
Simon Glass7428ad12013-05-07 06:11:57 +0000159 if (!fit_image_verify(fit_hdr, noffset)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000160 puts("Bad Data Hash\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100161 return 1;
162 }
163 }
164
Tien Fong Chee8a869582019-02-12 20:49:30 +0800165 /* get subimage/external data address and length */
166 if (fit_image_get_data_and_size(fit_hdr, noffset,
167 &fit_data, &fit_len)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000168 puts("Could not find script subimage data\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100169 return 1;
170 }
171
Stephen Warren6904b6e2011-10-18 11:11:49 +0000172 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
173 puts("Could not find script subimage "
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100174 "compression type\n");
175 return 1;
176 }
177
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +0100178 data = (ulong)fit_data;
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100179 len = (ulong)fit_len;
180 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100181#endif
182 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000183 puts("Invalid image type for imxtract\n");
wdenk0359dde2004-06-09 10:15:00 +0000184 return 1;
185 }
wdenk0359dde2004-06-09 10:15:00 +0000186
187 if (argc > 3) {
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100188 switch (comp) {
189 case IH_COMP_NONE:
190#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
191 {
192 size_t l = len;
193 size_t tail;
194 void *to = (void *) dest;
195 void *from = (void *)data;
196
Stephen Warren6904b6e2011-10-18 11:11:49 +0000197 printf(" Loading part %d ... ", part);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100198
199 while (l > 0) {
200 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
201 WATCHDOG_RESET();
Stephen Warren6904b6e2011-10-18 11:11:49 +0000202 memmove(to, from, tail);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100203 to += tail;
204 from += tail;
205 l -= tail;
206 }
207 }
208#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000209 printf(" Loading part %d ... ", part);
210 memmove((char *) dest, (char *)data, len);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100211#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
212 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000213#ifdef CONFIG_GZIP
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100214 case IH_COMP_GZIP:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000215 printf(" Uncompressing part %d ... ", part);
216 if (gunzip((void *) dest, unc_len,
217 (uchar *) data, &len) != 0) {
218 puts("GUNZIP ERROR - image not loaded\n");
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100219 return 1;
220 }
221 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000222#endif
Tom Rinic220bd92019-05-23 07:14:07 -0400223#if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100224 case IH_COMP_BZIP2:
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100225 {
226 int i;
227
Stephen Warren6904b6e2011-10-18 11:11:49 +0000228 printf(" Uncompressing part %d ... ", part);
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100229 /*
Wolfgang Denka2cfb242010-03-12 23:06:04 +0100230 * If we've got less than 4 MB of malloc()
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100231 * space, use slower decompression algorithm
232 * which requires at most 2300 KB of memory.
233 */
234 i = BZ2_bzBuffToBuffDecompress(
Simon Glass208d8e82013-08-30 11:00:09 -0600235 map_sysmem(ntohl(hdr->ih_load), 0),
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100236 &unc_len, (char *)data, len,
237 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
238 0);
239 if (i != BZ_OK) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000240 printf("BUNZIP2 ERROR %d - "
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100241 "image not loaded\n", i);
242 return 1;
243 }
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100244 }
245 break;
246#endif /* CONFIG_BZIP2 */
247 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000248 printf("Unimplemented compression type %d\n", comp);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100249 return 1;
250 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000251 puts("OK\n");
wdenk0359dde2004-06-09 10:15:00 +0000252 }
253
Mario Six7feefe42018-03-28 14:39:10 +0200254 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
Pieter Voorthuijsenf52d2812015-01-12 16:23:18 +0100255
Simon Glass4d949a22017-08-03 12:22:10 -0600256 env_set_hex("fileaddr", data);
257 env_set_hex("filesize", len);
wdenk0359dde2004-06-09 10:15:00 +0000258
259 return 0;
260}
261
Kim Phillipsdc00a682012-10-29 13:34:31 +0000262#ifdef CONFIG_SYS_LONGHELP
263static char imgextract_help_text[] =
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200264 "addr part [dest]\n"
265 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100266#if defined(CONFIG_FIT)
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200267 "\n"
268 "addr uname [dest]\n"
269 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100270#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000271 "";
272#endif
273
274U_BOOT_CMD(
275 imxtract, 4, 1, do_imgextract,
276 "extract a part of a multi-image", imgextract_help_text
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100277);