blob: 1467484df8d0daa90775c640d9e46290379aa45a [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 */
wdenk0359dde2004-06-09 10:15:00 +000014#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070015#include <cpu_func.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>
Simon Glass9bc15642020-02-03 07:36:16 -070019#include <malloc.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 Glass274e0b02020-05-10 11:39:56 -060026#include <asm/cache.h>
Simon Glass208d8e82013-08-30 11:00:09 -060027#include <asm/io.h>
wdenk0359dde2004-06-09 10:15:00 +000028
Kim Phillipsdc00a682012-10-29 13:34:31 +000029static int
Simon Glassed38aef2020-05-10 11:40:03 -060030do_imgextract(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk0359dde2004-06-09 10:15:00 +000031{
Simon Glass892265d2019-12-28 10:45:02 -070032 ulong addr = image_load_addr;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010033 ulong dest = 0;
Heiko Schocher515eb122014-05-28 11:33:33 +020034 ulong data, len;
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010035 int verify;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010036 int part = 0;
Tom Rinic220bd92019-05-23 07:14:07 -040037#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Heiko Schocher515eb122014-05-28 11:33:33 +020038 ulong count;
Simon Glassbb7d3bb2022-09-06 20:26:52 -060039 struct legacy_img_hdr *hdr = NULL;
Heiko Schocher515eb122014-05-28 11:33:33 +020040#endif
Marian Balakowicz2321ed02008-03-12 10:33:00 +010041#if defined(CONFIG_FIT)
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010042 const char *uname = NULL;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010043 const void* fit_hdr;
44 int noffset;
45 const void *fit_data;
46 size_t fit_len;
47#endif
Simon Glassa0395272013-04-17 16:13:45 +000048#ifdef CONFIG_GZIP
Nikita Shubin25498ed2022-12-19 11:05:27 +030049 uint unc_len = CONFIG_SYS_XIMG_LEN;
Simon Glassa0395272013-04-17 16:13:45 +000050#endif
Wolfgang Wegner781afa92009-12-10 10:11:21 +010051 uint8_t comp;
wdenk0359dde2004-06-09 10:15:00 +000052
Simon Glass22c34c22017-08-03 12:22:13 -060053 verify = env_get_yesno("verify");
wdenk0359dde2004-06-09 10:15:00 +000054
55 if (argc > 1) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060056 addr = hextoul(argv[1], NULL);
wdenk0359dde2004-06-09 10:15:00 +000057 }
58 if (argc > 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060059 part = hextoul(argv[2], NULL);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010060#if defined(CONFIG_FIT)
61 uname = argv[2];
62#endif
wdenk0359dde2004-06-09 10:15:00 +000063 }
64 if (argc > 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060065 dest = hextoul(argv[3], NULL);
wdenk0359dde2004-06-09 10:15:00 +000066 }
67
Stephen Warren6904b6e2011-10-18 11:11:49 +000068 switch (genimg_get_format((void *)addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -040069#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010070 case IMAGE_FORMAT_LEGACY:
wdenk0359dde2004-06-09 10:15:00 +000071
Marian Balakowicz2321ed02008-03-12 10:33:00 +010072 printf("## Copying part %d from legacy image "
73 "at %08lx ...\n", part, addr);
74
Simon Glassbb7d3bb2022-09-06 20:26:52 -060075 hdr = (struct legacy_img_hdr *)addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +000076 if (!image_check_magic(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010077 printf("Bad Magic Number\n");
78 return 1;
79 }
wdenk0359dde2004-06-09 10:15:00 +000080
Stephen Warren6904b6e2011-10-18 11:11:49 +000081 if (!image_check_hcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010082 printf("Bad Header Checksum\n");
83 return 1;
84 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +010085#ifdef DEBUG
Stephen Warren6904b6e2011-10-18 11:11:49 +000086 image_print_contents(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010087#endif
wdenk0359dde2004-06-09 10:15:00 +000088
Pierre Aubertde4e9492015-09-16 08:29:11 +020089 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
90 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010091 printf("Wrong Image Type for %s command\n",
92 cmdtp->name);
93 return 1;
94 }
wdenk0359dde2004-06-09 10:15:00 +000095
Stephen Warren6904b6e2011-10-18 11:11:49 +000096 comp = image_get_comp(hdr);
Wolfgang Wegner781afa92009-12-10 10:11:21 +010097 if ((comp != IH_COMP_NONE) && (argc < 4)) {
98 printf("Must specify load address for %s command "
99 "with compressed image\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100100 cmdtp->name);
wdenk0359dde2004-06-09 10:15:00 +0000101 return 1;
102 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100103
104 if (verify) {
105 printf(" Verifying Checksum ... ");
Stephen Warren6904b6e2011-10-18 11:11:49 +0000106 if (!image_check_dcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100107 printf("Bad Data CRC\n");
108 return 1;
109 }
110 printf("OK\n");
111 }
wdenk0359dde2004-06-09 10:15:00 +0000112
Stephen Warren6904b6e2011-10-18 11:11:49 +0000113 count = image_multi_count(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100114 if (part >= count) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100115 printf("Bad Image Part\n");
116 return 1;
117 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100118
Stephen Warren6904b6e2011-10-18 11:11:49 +0000119 image_multi_getimg(hdr, part, &data, &len);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100120 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200121#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100122#if defined(CONFIG_FIT)
123 case IMAGE_FORMAT_FIT:
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100124 if (uname == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000125 puts("No FIT subimage unit name\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100126 return 1;
127 }
128
129 printf("## Copying '%s' subimage from FIT image "
130 "at %08lx ...\n", uname, addr);
131
132 fit_hdr = (const void *)addr;
Simon Glassd563c252021-02-15 17:08:09 -0700133 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000134 puts("Bad FIT image format\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100135 return 1;
136 }
137
138 /* get subimage node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000139 noffset = fit_image_get_node(fit_hdr, uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100140 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000141 printf("Can't find '%s' FIT subimage\n", uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100142 return 1;
143 }
144
Stefan Theil69b0ef52019-01-07 10:25:52 +0100145 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100146 && (argc < 4)) {
147 printf("Must specify load address for %s command "
148 "with compressed image\n",
149 cmdtp->name);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100150 return 1;
151 }
152
153 /* verify integrity */
154 if (verify) {
Simon Glass7428ad12013-05-07 06:11:57 +0000155 if (!fit_image_verify(fit_hdr, noffset)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000156 puts("Bad Data Hash\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100157 return 1;
158 }
159 }
160
Tien Fong Chee8a869582019-02-12 20:49:30 +0800161 /* get subimage/external data address and length */
162 if (fit_image_get_data_and_size(fit_hdr, noffset,
163 &fit_data, &fit_len)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000164 puts("Could not find script subimage data\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100165 return 1;
166 }
167
Daniel Golle2ebfd222022-08-27 04:17:28 +0100168 if (fit_image_get_comp(fit_hdr, noffset, &comp))
169 comp = IH_COMP_NONE;
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100170
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +0100171 data = (ulong)fit_data;
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100172 len = (ulong)fit_len;
173 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100174#endif
175 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000176 puts("Invalid image type for imxtract\n");
wdenk0359dde2004-06-09 10:15:00 +0000177 return 1;
178 }
wdenk0359dde2004-06-09 10:15:00 +0000179
180 if (argc > 3) {
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100181 switch (comp) {
182 case IH_COMP_NONE:
183#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
184 {
185 size_t l = len;
186 size_t tail;
187 void *to = (void *) dest;
188 void *from = (void *)data;
189
Stephen Warren6904b6e2011-10-18 11:11:49 +0000190 printf(" Loading part %d ... ", part);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100191
192 while (l > 0) {
193 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
Stefan Roese80877fa2022-09-02 14:10:46 +0200194 schedule();
Stephen Warren6904b6e2011-10-18 11:11:49 +0000195 memmove(to, from, tail);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100196 to += tail;
197 from += tail;
198 l -= tail;
199 }
200 }
201#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000202 printf(" Loading part %d ... ", part);
203 memmove((char *) dest, (char *)data, len);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100204#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
205 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000206#ifdef CONFIG_GZIP
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100207 case IH_COMP_GZIP:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000208 printf(" Uncompressing part %d ... ", part);
209 if (gunzip((void *) dest, unc_len,
210 (uchar *) data, &len) != 0) {
211 puts("GUNZIP ERROR - image not loaded\n");
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100212 return 1;
213 }
214 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000215#endif
Tom Rinic220bd92019-05-23 07:14:07 -0400216#if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100217 case IH_COMP_BZIP2:
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100218 {
219 int i;
220
Stephen Warren6904b6e2011-10-18 11:11:49 +0000221 printf(" Uncompressing part %d ... ", part);
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100222 /*
Wolfgang Denka2cfb242010-03-12 23:06:04 +0100223 * If we've got less than 4 MB of malloc()
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100224 * space, use slower decompression algorithm
225 * which requires at most 2300 KB of memory.
226 */
227 i = BZ2_bzBuffToBuffDecompress(
Simon Glass208d8e82013-08-30 11:00:09 -0600228 map_sysmem(ntohl(hdr->ih_load), 0),
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100229 &unc_len, (char *)data, len,
230 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
231 0);
232 if (i != BZ_OK) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000233 printf("BUNZIP2 ERROR %d - "
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100234 "image not loaded\n", i);
235 return 1;
236 }
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100237 }
238 break;
239#endif /* CONFIG_BZIP2 */
240 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000241 printf("Unimplemented compression type %d\n", comp);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100242 return 1;
243 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000244 puts("OK\n");
wdenk0359dde2004-06-09 10:15:00 +0000245 }
246
Mario Six7feefe42018-03-28 14:39:10 +0200247 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
Pieter Voorthuijsenf52d2812015-01-12 16:23:18 +0100248
Simon Glass4d949a22017-08-03 12:22:10 -0600249 env_set_hex("fileaddr", data);
250 env_set_hex("filesize", len);
wdenk0359dde2004-06-09 10:15:00 +0000251
252 return 0;
253}
254
Tom Rini03f146c2023-10-07 15:13:08 -0400255U_BOOT_LONGHELP(imgextract,
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200256 "addr part [dest]\n"
257 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100258#if defined(CONFIG_FIT)
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200259 "\n"
260 "addr uname [dest]\n"
261 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100262#endif
Tom Rini03f146c2023-10-07 15:13:08 -0400263 );
Kim Phillipsdc00a682012-10-29 13:34:31 +0000264
265U_BOOT_CMD(
266 imxtract, 4, 1, do_imgextract,
267 "extract a part of a multi-image", imgextract_help_text
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100268);