blob: 770f6a3eede78bf2edce966cf831f3f5e027dc24 [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 Glass208d8e82013-08-30 11:00:09 -060027#include <asm/io.h>
wdenk0359dde2004-06-09 10:15:00 +000028
Wolfgang Wegner781afa92009-12-10 10:11:21 +010029#ifndef CONFIG_SYS_XIMG_LEN
30/* use 8MByte as default max gunzip size */
31#define CONFIG_SYS_XIMG_LEN 0x800000
32#endif
33
Kim Phillipsdc00a682012-10-29 13:34:31 +000034static int
Wolfgang Denk6262d0212010-06-28 22:00:46 +020035do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk0359dde2004-06-09 10:15:00 +000036{
Simon Glass892265d2019-12-28 10:45:02 -070037 ulong addr = image_load_addr;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010038 ulong dest = 0;
Heiko Schocher515eb122014-05-28 11:33:33 +020039 ulong data, len;
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010040 int verify;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010041 int part = 0;
Tom Rinic220bd92019-05-23 07:14:07 -040042#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Heiko Schocher515eb122014-05-28 11:33:33 +020043 ulong count;
Simon Glass208d8e82013-08-30 11:00:09 -060044 image_header_t *hdr = NULL;
Heiko Schocher515eb122014-05-28 11:33:33 +020045#endif
Marian Balakowicz2321ed02008-03-12 10:33:00 +010046#if defined(CONFIG_FIT)
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010047 const char *uname = NULL;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010048 const void* fit_hdr;
49 int noffset;
50 const void *fit_data;
51 size_t fit_len;
52#endif
Simon Glassa0395272013-04-17 16:13:45 +000053#ifdef CONFIG_GZIP
Wolfgang Wegner781afa92009-12-10 10:11:21 +010054 uint unc_len = CONFIG_SYS_XIMG_LEN;
Simon Glassa0395272013-04-17 16:13:45 +000055#endif
Wolfgang Wegner781afa92009-12-10 10:11:21 +010056 uint8_t comp;
wdenk0359dde2004-06-09 10:15:00 +000057
Simon Glass22c34c22017-08-03 12:22:13 -060058 verify = env_get_yesno("verify");
wdenk0359dde2004-06-09 10:15:00 +000059
60 if (argc > 1) {
61 addr = simple_strtoul(argv[1], NULL, 16);
62 }
63 if (argc > 2) {
64 part = simple_strtoul(argv[2], NULL, 16);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010065#if defined(CONFIG_FIT)
66 uname = argv[2];
67#endif
wdenk0359dde2004-06-09 10:15:00 +000068 }
69 if (argc > 3) {
70 dest = simple_strtoul(argv[3], NULL, 16);
71 }
72
Stephen Warren6904b6e2011-10-18 11:11:49 +000073 switch (genimg_get_format((void *)addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -040074#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010075 case IMAGE_FORMAT_LEGACY:
wdenk0359dde2004-06-09 10:15:00 +000076
Marian Balakowicz2321ed02008-03-12 10:33:00 +010077 printf("## Copying part %d from legacy image "
78 "at %08lx ...\n", part, addr);
79
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010080 hdr = (image_header_t *)addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +000081 if (!image_check_magic(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010082 printf("Bad Magic Number\n");
83 return 1;
84 }
wdenk0359dde2004-06-09 10:15:00 +000085
Stephen Warren6904b6e2011-10-18 11:11:49 +000086 if (!image_check_hcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010087 printf("Bad Header Checksum\n");
88 return 1;
89 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +010090#ifdef DEBUG
Stephen Warren6904b6e2011-10-18 11:11:49 +000091 image_print_contents(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010092#endif
wdenk0359dde2004-06-09 10:15:00 +000093
Pierre Aubertde4e9492015-09-16 08:29:11 +020094 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
95 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010096 printf("Wrong Image Type for %s command\n",
97 cmdtp->name);
98 return 1;
99 }
wdenk0359dde2004-06-09 10:15:00 +0000100
Stephen Warren6904b6e2011-10-18 11:11:49 +0000101 comp = image_get_comp(hdr);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100102 if ((comp != IH_COMP_NONE) && (argc < 4)) {
103 printf("Must specify load address for %s command "
104 "with compressed image\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100105 cmdtp->name);
wdenk0359dde2004-06-09 10:15:00 +0000106 return 1;
107 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100108
109 if (verify) {
110 printf(" Verifying Checksum ... ");
Stephen Warren6904b6e2011-10-18 11:11:49 +0000111 if (!image_check_dcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100112 printf("Bad Data CRC\n");
113 return 1;
114 }
115 printf("OK\n");
116 }
wdenk0359dde2004-06-09 10:15:00 +0000117
Stephen Warren6904b6e2011-10-18 11:11:49 +0000118 count = image_multi_count(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100119 if (part >= count) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100120 printf("Bad Image Part\n");
121 return 1;
122 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100123
Stephen Warren6904b6e2011-10-18 11:11:49 +0000124 image_multi_getimg(hdr, part, &data, &len);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100125 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200126#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100127#if defined(CONFIG_FIT)
128 case IMAGE_FORMAT_FIT:
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100129 if (uname == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000130 puts("No FIT subimage unit name\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100131 return 1;
132 }
133
134 printf("## Copying '%s' subimage from FIT image "
135 "at %08lx ...\n", uname, addr);
136
137 fit_hdr = (const void *)addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000138 if (!fit_check_format(fit_hdr)) {
139 puts("Bad FIT image format\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100140 return 1;
141 }
142
143 /* get subimage node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000144 noffset = fit_image_get_node(fit_hdr, uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100145 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000146 printf("Can't find '%s' FIT subimage\n", uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100147 return 1;
148 }
149
Stefan Theil69b0ef52019-01-07 10:25:52 +0100150 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100151 && (argc < 4)) {
152 printf("Must specify load address for %s command "
153 "with compressed image\n",
154 cmdtp->name);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100155 return 1;
156 }
157
158 /* verify integrity */
159 if (verify) {
Simon Glass7428ad12013-05-07 06:11:57 +0000160 if (!fit_image_verify(fit_hdr, noffset)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000161 puts("Bad Data Hash\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100162 return 1;
163 }
164 }
165
Tien Fong Chee8a869582019-02-12 20:49:30 +0800166 /* get subimage/external data address and length */
167 if (fit_image_get_data_and_size(fit_hdr, noffset,
168 &fit_data, &fit_len)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000169 puts("Could not find script subimage data\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100170 return 1;
171 }
172
Stephen Warren6904b6e2011-10-18 11:11:49 +0000173 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
174 puts("Could not find script subimage "
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100175 "compression type\n");
176 return 1;
177 }
178
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +0100179 data = (ulong)fit_data;
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100180 len = (ulong)fit_len;
181 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100182#endif
183 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000184 puts("Invalid image type for imxtract\n");
wdenk0359dde2004-06-09 10:15:00 +0000185 return 1;
186 }
wdenk0359dde2004-06-09 10:15:00 +0000187
188 if (argc > 3) {
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100189 switch (comp) {
190 case IH_COMP_NONE:
191#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
192 {
193 size_t l = len;
194 size_t tail;
195 void *to = (void *) dest;
196 void *from = (void *)data;
197
Stephen Warren6904b6e2011-10-18 11:11:49 +0000198 printf(" Loading part %d ... ", part);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100199
200 while (l > 0) {
201 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
202 WATCHDOG_RESET();
Stephen Warren6904b6e2011-10-18 11:11:49 +0000203 memmove(to, from, tail);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100204 to += tail;
205 from += tail;
206 l -= tail;
207 }
208 }
209#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000210 printf(" Loading part %d ... ", part);
211 memmove((char *) dest, (char *)data, len);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100212#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
213 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000214#ifdef CONFIG_GZIP
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100215 case IH_COMP_GZIP:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000216 printf(" Uncompressing part %d ... ", part);
217 if (gunzip((void *) dest, unc_len,
218 (uchar *) data, &len) != 0) {
219 puts("GUNZIP ERROR - image not loaded\n");
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100220 return 1;
221 }
222 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000223#endif
Tom Rinic220bd92019-05-23 07:14:07 -0400224#if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100225 case IH_COMP_BZIP2:
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100226 {
227 int i;
228
Stephen Warren6904b6e2011-10-18 11:11:49 +0000229 printf(" Uncompressing part %d ... ", part);
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100230 /*
Wolfgang Denka2cfb242010-03-12 23:06:04 +0100231 * If we've got less than 4 MB of malloc()
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100232 * space, use slower decompression algorithm
233 * which requires at most 2300 KB of memory.
234 */
235 i = BZ2_bzBuffToBuffDecompress(
Simon Glass208d8e82013-08-30 11:00:09 -0600236 map_sysmem(ntohl(hdr->ih_load), 0),
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100237 &unc_len, (char *)data, len,
238 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
239 0);
240 if (i != BZ_OK) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000241 printf("BUNZIP2 ERROR %d - "
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100242 "image not loaded\n", i);
243 return 1;
244 }
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100245 }
246 break;
247#endif /* CONFIG_BZIP2 */
248 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000249 printf("Unimplemented compression type %d\n", comp);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100250 return 1;
251 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000252 puts("OK\n");
wdenk0359dde2004-06-09 10:15:00 +0000253 }
254
Mario Six7feefe42018-03-28 14:39:10 +0200255 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
Pieter Voorthuijsenf52d2812015-01-12 16:23:18 +0100256
Simon Glass4d949a22017-08-03 12:22:10 -0600257 env_set_hex("fileaddr", data);
258 env_set_hex("filesize", len);
wdenk0359dde2004-06-09 10:15:00 +0000259
260 return 0;
261}
262
Kim Phillipsdc00a682012-10-29 13:34:31 +0000263#ifdef CONFIG_SYS_LONGHELP
264static char imgextract_help_text[] =
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200265 "addr part [dest]\n"
266 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100267#if defined(CONFIG_FIT)
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200268 "\n"
269 "addr uname [dest]\n"
270 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100271#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000272 "";
273#endif
274
275U_BOOT_CMD(
276 imxtract, 4, 1, do_imgextract,
277 "extract a part of a multi-image", imgextract_help_text
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100278);