blob: 069c6ad39abed4bff19b804a03667ff7d14fe69b [file] [log] [blame]
wdenk0359dde2004-06-09 10:15:00 +00001/*
2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2003
6 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk0359dde2004-06-09 10:15:00 +00009 */
10
wdenk0359dde2004-06-09 10:15:00 +000011
12/*
13 * Multi Image extract
14 */
15#include <common.h>
16#include <command.h>
17#include <image.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050018#include <mapmem.h>
Wolfgang Wegner781afa92009-12-10 10:11:21 +010019#include <watchdog.h>
20#if defined(CONFIG_BZIP2)
21#include <bzlib.h>
22#endif
wdenk0359dde2004-06-09 10:15:00 +000023#include <asm/byteorder.h>
Simon Glass208d8e82013-08-30 11:00:09 -060024#include <asm/io.h>
wdenk0359dde2004-06-09 10:15:00 +000025
Wolfgang Wegner781afa92009-12-10 10:11:21 +010026#ifndef CONFIG_SYS_XIMG_LEN
27/* use 8MByte as default max gunzip size */
28#define CONFIG_SYS_XIMG_LEN 0x800000
29#endif
30
Kim Phillipsdc00a682012-10-29 13:34:31 +000031static int
Wolfgang Denk6262d0212010-06-28 22:00:46 +020032do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk0359dde2004-06-09 10:15:00 +000033{
Marian Balakowicz2321ed02008-03-12 10:33:00 +010034 ulong addr = load_addr;
35 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;
Heiko Schocher515eb122014-05-28 11:33:33 +020039#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
40 ulong count;
Simon Glass208d8e82013-08-30 11:00:09 -060041 image_header_t *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
Wolfgang Wegner781afa92009-12-10 10:11:21 +010051 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) {
58 addr = simple_strtoul(argv[1], NULL, 16);
59 }
60 if (argc > 2) {
61 part = simple_strtoul(argv[2], NULL, 16);
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) {
67 dest = simple_strtoul(argv[3], NULL, 16);
68 }
69
Stephen Warren6904b6e2011-10-18 11:11:49 +000070 switch (genimg_get_format((void *)addr)) {
Heiko Schocher515eb122014-05-28 11:33:33 +020071#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
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
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010077 hdr = (image_header_t *)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;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000135 if (!fit_check_format(fit_hdr)) {
136 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
Stephen Warren6904b6e2011-10-18 11:11:49 +0000147 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
163 /* get subimage data address and length */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000164 if (fit_image_get_data(fit_hdr, noffset,
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100165 &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
Stephen Warren6904b6e2011-10-18 11:11:49 +0000170 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
171 puts("Could not find script subimage "
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100172 "compression type\n");
173 return 1;
174 }
175
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +0100176 data = (ulong)fit_data;
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100177 len = (ulong)fit_len;
178 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100179#endif
180 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000181 puts("Invalid image type for imxtract\n");
wdenk0359dde2004-06-09 10:15:00 +0000182 return 1;
183 }
wdenk0359dde2004-06-09 10:15:00 +0000184
185 if (argc > 3) {
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100186 switch (comp) {
187 case IH_COMP_NONE:
188#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
189 {
190 size_t l = len;
191 size_t tail;
192 void *to = (void *) dest;
193 void *from = (void *)data;
194
Stephen Warren6904b6e2011-10-18 11:11:49 +0000195 printf(" Loading part %d ... ", part);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100196
197 while (l > 0) {
198 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
199 WATCHDOG_RESET();
Stephen Warren6904b6e2011-10-18 11:11:49 +0000200 memmove(to, from, tail);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100201 to += tail;
202 from += tail;
203 l -= tail;
204 }
205 }
206#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000207 printf(" Loading part %d ... ", part);
208 memmove((char *) dest, (char *)data, len);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100209#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
210 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000211#ifdef CONFIG_GZIP
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100212 case IH_COMP_GZIP:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000213 printf(" Uncompressing part %d ... ", part);
214 if (gunzip((void *) dest, unc_len,
215 (uchar *) data, &len) != 0) {
216 puts("GUNZIP ERROR - image not loaded\n");
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100217 return 1;
218 }
219 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000220#endif
Heiko Schocher515eb122014-05-28 11:33:33 +0200221#if defined(CONFIG_BZIP2) && defined(CONFIG_IMAGE_FORMAT_LEGACY)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100222 case IH_COMP_BZIP2:
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100223 {
224 int i;
225
Stephen Warren6904b6e2011-10-18 11:11:49 +0000226 printf(" Uncompressing part %d ... ", part);
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100227 /*
Wolfgang Denka2cfb242010-03-12 23:06:04 +0100228 * If we've got less than 4 MB of malloc()
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100229 * space, use slower decompression algorithm
230 * which requires at most 2300 KB of memory.
231 */
232 i = BZ2_bzBuffToBuffDecompress(
Simon Glass208d8e82013-08-30 11:00:09 -0600233 map_sysmem(ntohl(hdr->ih_load), 0),
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100234 &unc_len, (char *)data, len,
235 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
236 0);
237 if (i != BZ_OK) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000238 printf("BUNZIP2 ERROR %d - "
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100239 "image not loaded\n", i);
240 return 1;
241 }
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100242 }
243 break;
244#endif /* CONFIG_BZIP2 */
245 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000246 printf("Unimplemented compression type %d\n", comp);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100247 return 1;
248 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000249 puts("OK\n");
wdenk0359dde2004-06-09 10:15:00 +0000250 }
251
Mario Six7feefe42018-03-28 14:39:10 +0200252 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
Pieter Voorthuijsenf52d2812015-01-12 16:23:18 +0100253
Simon Glass4d949a22017-08-03 12:22:10 -0600254 env_set_hex("fileaddr", data);
255 env_set_hex("filesize", len);
wdenk0359dde2004-06-09 10:15:00 +0000256
257 return 0;
258}
259
Kim Phillipsdc00a682012-10-29 13:34:31 +0000260#ifdef CONFIG_SYS_LONGHELP
261static char imgextract_help_text[] =
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200262 "addr part [dest]\n"
263 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100264#if defined(CONFIG_FIT)
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200265 "\n"
266 "addr uname [dest]\n"
267 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100268#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000269 "";
270#endif
271
272U_BOOT_CMD(
273 imxtract, 4, 1, do_imgextract,
274 "extract a part of a multi-image", imgextract_help_text
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100275);