1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2004
17 #if IS_ENABLED(CONFIG_ZSTD)
18 #include <linux/zstd.h>
24 #if defined(CONFIG_BZIP2)
27 #include <asm/byteorder.h>
28 #include <asm/cache.h>
32 do_imgextract(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
34 ulong addr = image_load_addr;
39 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
41 struct legacy_img_hdr *hdr = NULL;
43 #if defined(CONFIG_FIT)
44 const char *uname = NULL;
51 uint unc_len = CONFIG_SYS_XIMG_LEN;
55 verify = env_get_yesno("verify");
58 addr = hextoul(argv[1], NULL);
61 part = hextoul(argv[2], NULL);
62 #if defined(CONFIG_FIT)
67 dest = hextoul(argv[3], NULL);
70 switch (genimg_get_format((void *)addr)) {
71 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
72 case IMAGE_FORMAT_LEGACY:
74 printf("## Copying part %d from legacy image "
75 "at %08lx ...\n", part, addr);
77 hdr = (struct legacy_img_hdr *)addr;
78 if (!image_check_magic(hdr)) {
79 printf("Bad Magic Number\n");
83 if (!image_check_hcrc(hdr)) {
84 printf("Bad Header Checksum\n");
88 image_print_contents(hdr);
91 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
92 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
93 printf("Wrong Image Type for %s command\n",
98 comp = image_get_comp(hdr);
99 if ((comp != IH_COMP_NONE) && (argc < 4)) {
100 printf("Must specify load address for %s command "
101 "with compressed image\n",
107 printf(" Verifying Checksum ... ");
108 if (!image_check_dcrc(hdr)) {
109 printf("Bad Data CRC\n");
115 count = image_multi_count(hdr);
117 printf("Bad Image Part\n");
121 image_multi_getimg(hdr, part, &data, &len);
124 #if defined(CONFIG_FIT)
125 case IMAGE_FORMAT_FIT:
127 puts("No FIT subimage unit name\n");
131 printf("## Copying '%s' subimage from FIT image "
132 "at %08lx ...\n", uname, addr);
134 fit_hdr = (const void *)addr;
135 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
136 puts("Bad FIT image format\n");
140 /* get subimage node offset */
141 noffset = fit_image_get_node(fit_hdr, uname);
143 printf("Can't find '%s' FIT subimage\n", uname);
147 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
149 printf("Must specify load address for %s command "
150 "with compressed image\n",
155 /* verify integrity */
157 if (!fit_image_verify(fit_hdr, noffset)) {
158 puts("Bad Data Hash\n");
163 /* get subimage/external data address and length */
164 if (fit_image_get_data_and_size(fit_hdr, noffset,
165 &fit_data, &fit_len)) {
166 puts("Could not find script subimage data\n");
170 if (fit_image_get_comp(fit_hdr, noffset, &comp))
173 data = (ulong)fit_data;
174 len = (ulong)fit_len;
178 puts("Invalid image type for imxtract\n");
185 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
189 void *to = (void *) dest;
190 void *from = (void *)data;
192 printf(" Loading part %d ... ", part);
195 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
197 memmove(to, from, tail);
203 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
204 printf(" Loading part %d ... ", part);
205 memmove((char *) dest, (char *)data, len);
206 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
210 printf(" Uncompressing part %d ... ", part);
211 if (gunzip((void *) dest, unc_len,
212 (uchar *) data, &len) != 0) {
213 puts("GUNZIP ERROR - image not loaded\n");
218 #if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
223 printf(" Uncompressing part %d ... ", part);
225 * If we've got less than 4 MB of malloc()
226 * space, use slower decompression algorithm
227 * which requires at most 2300 KB of memory.
229 i = BZ2_bzBuffToBuffDecompress(
230 map_sysmem(ntohl(hdr->ih_load), 0),
231 &unc_len, (char *)data, len,
232 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
235 printf("BUNZIP2 ERROR %d - "
236 "image not loaded\n", i);
241 #endif /* CONFIG_BZIP2 */
242 #if IS_ENABLED(CONFIG_ZSTD)
248 printf(" Uncompressing part %d ... ", part);
250 abuf_init_set(&in, (void *)data, len);
251 abuf_init_set(&out, (void *)dest, unc_len);
252 ret = zstd_decompress(&in, &out);
254 printf("ZSTD ERROR %d - "
255 "image not loaded\n", ret);
263 printf("Unimplemented compression type %d\n", comp);
269 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
271 env_set_hex("fileaddr", data);
272 env_set_hex("filesize", len);
277 U_BOOT_LONGHELP(imgextract,
279 " - extract <part> from legacy image at <addr> and copy to <dest>"
280 #if defined(CONFIG_FIT)
282 "addr uname [dest]\n"
283 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
288 imxtract, 4, 1, do_imgextract,
289 "extract a part of a multi-image", imgextract_help_text