1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2004
18 #if IS_ENABLED(CONFIG_ZSTD)
19 #include <linux/zstd.h>
25 #if defined(CONFIG_BZIP2)
28 #include <asm/byteorder.h>
29 #include <asm/cache.h>
33 do_imgextract(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
35 ulong addr = image_load_addr;
40 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
42 struct legacy_img_hdr *hdr = NULL;
44 #if defined(CONFIG_FIT)
45 const char *uname = NULL;
52 uint unc_len = CONFIG_SYS_XIMG_LEN;
56 verify = env_get_yesno("verify");
59 addr = hextoul(argv[1], NULL);
62 part = hextoul(argv[2], NULL);
63 #if defined(CONFIG_FIT)
68 dest = hextoul(argv[3], NULL);
71 switch (genimg_get_format((void *)addr)) {
72 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
73 case IMAGE_FORMAT_LEGACY:
75 printf("## Copying part %d from legacy image "
76 "at %08lx ...\n", part, addr);
78 hdr = (struct legacy_img_hdr *)addr;
79 if (!image_check_magic(hdr)) {
80 printf("Bad Magic Number\n");
84 if (!image_check_hcrc(hdr)) {
85 printf("Bad Header Checksum\n");
89 image_print_contents(hdr);
92 if (!image_check_type(hdr, IH_TYPE_MULTI) &&
93 !image_check_type(hdr, IH_TYPE_SCRIPT)) {
94 printf("Wrong Image Type for %s command\n",
99 comp = image_get_comp(hdr);
100 if ((comp != IH_COMP_NONE) && (argc < 4)) {
101 printf("Must specify load address for %s command "
102 "with compressed image\n",
108 printf(" Verifying Checksum ... ");
109 if (!image_check_dcrc(hdr)) {
110 printf("Bad Data CRC\n");
116 count = image_multi_count(hdr);
118 printf("Bad Image Part\n");
122 image_multi_getimg(hdr, part, &data, &len);
125 #if defined(CONFIG_FIT)
126 case IMAGE_FORMAT_FIT:
128 puts("No FIT subimage unit name\n");
132 printf("## Copying '%s' subimage from FIT image "
133 "at %08lx ...\n", uname, addr);
135 fit_hdr = (const void *)addr;
136 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
137 puts("Bad FIT image format\n");
141 /* get subimage node offset */
142 noffset = fit_image_get_node(fit_hdr, uname);
144 printf("Can't find '%s' FIT subimage\n", uname);
148 if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
150 printf("Must specify load address for %s command "
151 "with compressed image\n",
156 /* verify integrity */
158 if (!fit_image_verify(fit_hdr, noffset)) {
159 puts("Bad Data Hash\n");
164 /* get subimage/external data address and length */
165 if (fit_image_get_data_and_size(fit_hdr, noffset,
166 &fit_data, &fit_len)) {
167 puts("Could not find script subimage data\n");
171 if (fit_image_get_comp(fit_hdr, noffset, &comp))
174 data = (ulong)fit_data;
175 len = (ulong)fit_len;
179 puts("Invalid image type for imxtract\n");
186 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
190 void *to = (void *) dest;
191 void *from = (void *)data;
193 printf(" Loading part %d ... ", part);
196 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
198 memmove(to, from, tail);
204 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
205 printf(" Loading part %d ... ", part);
206 memmove((char *) dest, (char *)data, len);
207 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
211 printf(" Uncompressing part %d ... ", part);
212 if (gunzip((void *) dest, unc_len,
213 (uchar *) data, &len) != 0) {
214 puts("GUNZIP ERROR - image not loaded\n");
219 #if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
224 printf(" Uncompressing part %d ... ", part);
226 * If we've got less than 4 MB of malloc()
227 * space, use slower decompression algorithm
228 * which requires at most 2300 KB of memory.
230 i = BZ2_bzBuffToBuffDecompress(
231 map_sysmem(ntohl(hdr->ih_load), 0),
232 &unc_len, (char *)data, len,
233 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
236 printf("BUNZIP2 ERROR %d - "
237 "image not loaded\n", i);
242 #endif /* CONFIG_BZIP2 */
243 #if IS_ENABLED(CONFIG_ZSTD)
249 printf(" Uncompressing part %d ... ", part);
251 abuf_init_set(&in, (void *)data, len);
252 abuf_init_set(&out, (void *)dest, unc_len);
253 ret = zstd_decompress(&in, &out);
255 printf("ZSTD ERROR %d - "
256 "image not loaded\n", ret);
264 printf("Unimplemented compression type %d\n", comp);
270 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
272 env_set_hex("fileaddr", data);
273 env_set_hex("filesize", len);
278 U_BOOT_LONGHELP(imgextract,
280 " - extract <part> from legacy image at <addr> and copy to <dest>"
281 #if defined(CONFIG_FIT)
283 "addr uname [dest]\n"
284 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
289 imxtract, 4, 1, do_imgextract,
290 "extract a part of a multi-image", imgextract_help_text