blob: 65ba41320a02350234e96b1f0769ea2a9e128c48 [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 Glass274e0b02020-05-10 11:39:56 -060027#include <asm/cache.h>
Simon Glass208d8e82013-08-30 11:00:09 -060028#include <asm/io.h>
wdenk0359dde2004-06-09 10:15:00 +000029
Wolfgang Wegner781afa92009-12-10 10:11:21 +010030#ifndef CONFIG_SYS_XIMG_LEN
31/* use 8MByte as default max gunzip size */
32#define CONFIG_SYS_XIMG_LEN 0x800000
33#endif
34
Kim Phillipsdc00a682012-10-29 13:34:31 +000035static int
Simon Glassed38aef2020-05-10 11:40:03 -060036do_imgextract(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk0359dde2004-06-09 10:15:00 +000037{
Simon Glass892265d2019-12-28 10:45:02 -070038 ulong addr = image_load_addr;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010039 ulong dest = 0;
Heiko Schocher515eb122014-05-28 11:33:33 +020040 ulong data, len;
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010041 int verify;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010042 int part = 0;
Tom Rinic220bd92019-05-23 07:14:07 -040043#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Heiko Schocher515eb122014-05-28 11:33:33 +020044 ulong count;
Simon Glass208d8e82013-08-30 11:00:09 -060045 image_header_t *hdr = NULL;
Heiko Schocher515eb122014-05-28 11:33:33 +020046#endif
Marian Balakowicz2321ed02008-03-12 10:33:00 +010047#if defined(CONFIG_FIT)
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +010048 const char *uname = NULL;
Marian Balakowicz2321ed02008-03-12 10:33:00 +010049 const void* fit_hdr;
50 int noffset;
51 const void *fit_data;
52 size_t fit_len;
53#endif
Simon Glassa0395272013-04-17 16:13:45 +000054#ifdef CONFIG_GZIP
Wolfgang Wegner781afa92009-12-10 10:11:21 +010055 uint unc_len = CONFIG_SYS_XIMG_LEN;
Simon Glassa0395272013-04-17 16:13:45 +000056#endif
Wolfgang Wegner781afa92009-12-10 10:11:21 +010057 uint8_t comp;
wdenk0359dde2004-06-09 10:15:00 +000058
Simon Glass22c34c22017-08-03 12:22:13 -060059 verify = env_get_yesno("verify");
wdenk0359dde2004-06-09 10:15:00 +000060
61 if (argc > 1) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060062 addr = hextoul(argv[1], NULL);
wdenk0359dde2004-06-09 10:15:00 +000063 }
64 if (argc > 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060065 part = hextoul(argv[2], NULL);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010066#if defined(CONFIG_FIT)
67 uname = argv[2];
68#endif
wdenk0359dde2004-06-09 10:15:00 +000069 }
70 if (argc > 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060071 dest = hextoul(argv[3], NULL);
wdenk0359dde2004-06-09 10:15:00 +000072 }
73
Stephen Warren6904b6e2011-10-18 11:11:49 +000074 switch (genimg_get_format((void *)addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -040075#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010076 case IMAGE_FORMAT_LEGACY:
wdenk0359dde2004-06-09 10:15:00 +000077
Marian Balakowicz2321ed02008-03-12 10:33:00 +010078 printf("## Copying part %d from legacy image "
79 "at %08lx ...\n", part, addr);
80
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010081 hdr = (image_header_t *)addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +000082 if (!image_check_magic(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010083 printf("Bad Magic Number\n");
84 return 1;
85 }
wdenk0359dde2004-06-09 10:15:00 +000086
Stephen Warren6904b6e2011-10-18 11:11:49 +000087 if (!image_check_hcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010088 printf("Bad Header Checksum\n");
89 return 1;
90 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +010091#ifdef DEBUG
Stephen Warren6904b6e2011-10-18 11:11:49 +000092 image_print_contents(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +010093#endif
wdenk0359dde2004-06-09 10:15:00 +000094
Pierre Aubertde4e9492015-09-16 08:29:11 +020095 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
96 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010097 printf("Wrong Image Type for %s command\n",
98 cmdtp->name);
99 return 1;
100 }
wdenk0359dde2004-06-09 10:15:00 +0000101
Stephen Warren6904b6e2011-10-18 11:11:49 +0000102 comp = image_get_comp(hdr);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100103 if ((comp != IH_COMP_NONE) && (argc < 4)) {
104 printf("Must specify load address for %s command "
105 "with compressed image\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100106 cmdtp->name);
wdenk0359dde2004-06-09 10:15:00 +0000107 return 1;
108 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100109
110 if (verify) {
111 printf(" Verifying Checksum ... ");
Stephen Warren6904b6e2011-10-18 11:11:49 +0000112 if (!image_check_dcrc(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100113 printf("Bad Data CRC\n");
114 return 1;
115 }
116 printf("OK\n");
117 }
wdenk0359dde2004-06-09 10:15:00 +0000118
Stephen Warren6904b6e2011-10-18 11:11:49 +0000119 count = image_multi_count(hdr);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100120 if (part >= count) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100121 printf("Bad Image Part\n");
122 return 1;
123 }
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100124
Stephen Warren6904b6e2011-10-18 11:11:49 +0000125 image_multi_getimg(hdr, part, &data, &len);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100126 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200127#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100128#if defined(CONFIG_FIT)
129 case IMAGE_FORMAT_FIT:
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100130 if (uname == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000131 puts("No FIT subimage unit name\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100132 return 1;
133 }
134
135 printf("## Copying '%s' subimage from FIT image "
136 "at %08lx ...\n", uname, addr);
137
138 fit_hdr = (const void *)addr;
Simon Glassd563c252021-02-15 17:08:09 -0700139 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000140 puts("Bad FIT image format\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100141 return 1;
142 }
143
144 /* get subimage node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000145 noffset = fit_image_get_node(fit_hdr, uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100146 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000147 printf("Can't find '%s' FIT subimage\n", uname);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100148 return 1;
149 }
150
Stefan Theil69b0ef52019-01-07 10:25:52 +0100151 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100152 && (argc < 4)) {
153 printf("Must specify load address for %s command "
154 "with compressed image\n",
155 cmdtp->name);
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100156 return 1;
157 }
158
159 /* verify integrity */
160 if (verify) {
Simon Glass7428ad12013-05-07 06:11:57 +0000161 if (!fit_image_verify(fit_hdr, noffset)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000162 puts("Bad Data Hash\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100163 return 1;
164 }
165 }
166
Tien Fong Chee8a869582019-02-12 20:49:30 +0800167 /* get subimage/external data address and length */
168 if (fit_image_get_data_and_size(fit_hdr, noffset,
169 &fit_data, &fit_len)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000170 puts("Could not find script subimage data\n");
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100171 return 1;
172 }
173
Stephen Warren6904b6e2011-10-18 11:11:49 +0000174 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
175 puts("Could not find script subimage "
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100176 "compression type\n");
177 return 1;
178 }
179
Bartlomiej Sieka3b8cc712008-03-20 19:38:45 +0100180 data = (ulong)fit_data;
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100181 len = (ulong)fit_len;
182 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100183#endif
184 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000185 puts("Invalid image type for imxtract\n");
wdenk0359dde2004-06-09 10:15:00 +0000186 return 1;
187 }
wdenk0359dde2004-06-09 10:15:00 +0000188
189 if (argc > 3) {
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100190 switch (comp) {
191 case IH_COMP_NONE:
192#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
193 {
194 size_t l = len;
195 size_t tail;
196 void *to = (void *) dest;
197 void *from = (void *)data;
198
Stephen Warren6904b6e2011-10-18 11:11:49 +0000199 printf(" Loading part %d ... ", part);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100200
201 while (l > 0) {
202 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
203 WATCHDOG_RESET();
Stephen Warren6904b6e2011-10-18 11:11:49 +0000204 memmove(to, from, tail);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100205 to += tail;
206 from += tail;
207 l -= tail;
208 }
209 }
210#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000211 printf(" Loading part %d ... ", part);
212 memmove((char *) dest, (char *)data, len);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100213#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
214 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000215#ifdef CONFIG_GZIP
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100216 case IH_COMP_GZIP:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000217 printf(" Uncompressing part %d ... ", part);
218 if (gunzip((void *) dest, unc_len,
219 (uchar *) data, &len) != 0) {
220 puts("GUNZIP ERROR - image not loaded\n");
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100221 return 1;
222 }
223 break;
Matthew McClintockc93a7542011-05-24 05:48:26 +0000224#endif
Tom Rinic220bd92019-05-23 07:14:07 -0400225#if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100226 case IH_COMP_BZIP2:
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100227 {
228 int i;
229
Stephen Warren6904b6e2011-10-18 11:11:49 +0000230 printf(" Uncompressing part %d ... ", part);
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100231 /*
Wolfgang Denka2cfb242010-03-12 23:06:04 +0100232 * If we've got less than 4 MB of malloc()
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100233 * space, use slower decompression algorithm
234 * which requires at most 2300 KB of memory.
235 */
236 i = BZ2_bzBuffToBuffDecompress(
Simon Glass208d8e82013-08-30 11:00:09 -0600237 map_sysmem(ntohl(hdr->ih_load), 0),
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100238 &unc_len, (char *)data, len,
239 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
240 0);
241 if (i != BZ_OK) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000242 printf("BUNZIP2 ERROR %d - "
Wolfgang Denk3207afd2010-01-31 21:51:43 +0100243 "image not loaded\n", i);
244 return 1;
245 }
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100246 }
247 break;
248#endif /* CONFIG_BZIP2 */
249 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000250 printf("Unimplemented compression type %d\n", comp);
Wolfgang Wegner781afa92009-12-10 10:11:21 +0100251 return 1;
252 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000253 puts("OK\n");
wdenk0359dde2004-06-09 10:15:00 +0000254 }
255
Mario Six7feefe42018-03-28 14:39:10 +0200256 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
Pieter Voorthuijsenf52d2812015-01-12 16:23:18 +0100257
Simon Glass4d949a22017-08-03 12:22:10 -0600258 env_set_hex("fileaddr", data);
259 env_set_hex("filesize", len);
wdenk0359dde2004-06-09 10:15:00 +0000260
261 return 0;
262}
263
Kim Phillipsdc00a682012-10-29 13:34:31 +0000264#ifdef CONFIG_SYS_LONGHELP
265static char imgextract_help_text[] =
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200266 "addr part [dest]\n"
267 " - extract <part> from legacy image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100268#if defined(CONFIG_FIT)
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200269 "\n"
270 "addr uname [dest]\n"
271 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100272#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000273 "";
274#endif
275
276U_BOOT_CMD(
277 imxtract, 4, 1, do_imgextract,
278 "extract a part of a multi-image", imgextract_help_text
Marian Balakowicz2321ed02008-03-12 10:33:00 +0100279);