]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
48abe7bf WD |
2 | /* |
3 | * (C) Copyright 2000-2004 | |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
5 | * | |
6 | * (C) Copyright 2003 | |
7 | * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <[email protected]> | |
48abe7bf WD |
8 | */ |
9 | ||
48abe7bf WD |
10 | /* |
11 | * Multi Image extract | |
12 | */ | |
48abe7bf | 13 | #include <command.h> |
1eb69ae4 | 14 | #include <cpu_func.h> |
c7694dd4 | 15 | #include <env.h> |
0c670fc1 | 16 | #include <gzip.h> |
822911d5 DG |
17 | #if IS_ENABLED(CONFIG_ZSTD) |
18 | #include <linux/zstd.h> | |
19 | #endif | |
48abe7bf | 20 | #include <image.h> |
336d4615 | 21 | #include <malloc.h> |
0eb25b61 | 22 | #include <mapmem.h> |
5912d365 WW |
23 | #include <watchdog.h> |
24 | #if defined(CONFIG_BZIP2) | |
25 | #include <bzlib.h> | |
26 | #endif | |
48abe7bf | 27 | #include <asm/byteorder.h> |
90526e9f | 28 | #include <asm/cache.h> |
628af179 | 29 | #include <asm/io.h> |
48abe7bf | 30 | |
088f1b19 | 31 | static int |
09140113 | 32 | do_imgextract(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
48abe7bf | 33 | { |
bb872dd9 | 34 | ulong addr = image_load_addr; |
1b7897f2 | 35 | ulong dest = 0; |
21d29f7f | 36 | ulong data, len; |
fbe7a155 | 37 | int verify; |
1b7897f2 | 38 | int part = 0; |
c76c93a3 | 39 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
21d29f7f | 40 | ulong count; |
f3543e69 | 41 | struct legacy_img_hdr *hdr = NULL; |
21d29f7f | 42 | #endif |
1b7897f2 | 43 | #if defined(CONFIG_FIT) |
fbe7a155 | 44 | const char *uname = NULL; |
1b7897f2 MB |
45 | const void* fit_hdr; |
46 | int noffset; | |
47 | const void *fit_data; | |
48 | size_t fit_len; | |
49 | #endif | |
a92181b3 | 50 | #ifdef CONFIG_GZIP |
05e8e240 | 51 | uint unc_len = CONFIG_SYS_XIMG_LEN; |
a92181b3 | 52 | #endif |
5912d365 | 53 | uint8_t comp; |
48abe7bf | 54 | |
bfebc8c9 | 55 | verify = env_get_yesno("verify"); |
48abe7bf WD |
56 | |
57 | if (argc > 1) { | |
7e5f460e | 58 | addr = hextoul(argv[1], NULL); |
48abe7bf WD |
59 | } |
60 | if (argc > 2) { | |
7e5f460e | 61 | part = hextoul(argv[2], NULL); |
1b7897f2 MB |
62 | #if defined(CONFIG_FIT) |
63 | uname = argv[2]; | |
64 | #endif | |
48abe7bf WD |
65 | } |
66 | if (argc > 3) { | |
7e5f460e | 67 | dest = hextoul(argv[3], NULL); |
48abe7bf WD |
68 | } |
69 | ||
712fbcf3 | 70 | switch (genimg_get_format((void *)addr)) { |
c76c93a3 | 71 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
d5934ad7 | 72 | case IMAGE_FORMAT_LEGACY: |
48abe7bf | 73 | |
1b7897f2 MB |
74 | printf("## Copying part %d from legacy image " |
75 | "at %08lx ...\n", part, addr); | |
76 | ||
f3543e69 | 77 | hdr = (struct legacy_img_hdr *)addr; |
712fbcf3 | 78 | if (!image_check_magic(hdr)) { |
d5934ad7 MB |
79 | printf("Bad Magic Number\n"); |
80 | return 1; | |
81 | } | |
48abe7bf | 82 | |
712fbcf3 | 83 | if (!image_check_hcrc(hdr)) { |
d5934ad7 MB |
84 | printf("Bad Header Checksum\n"); |
85 | return 1; | |
86 | } | |
1b7897f2 | 87 | #ifdef DEBUG |
712fbcf3 | 88 | image_print_contents(hdr); |
1b7897f2 | 89 | #endif |
48abe7bf | 90 | |
83636fa0 PA |
91 | if (!image_check_type(hdr, IH_TYPE_MULTI) && |
92 | !image_check_type(hdr, IH_TYPE_SCRIPT)) { | |
d5934ad7 MB |
93 | printf("Wrong Image Type for %s command\n", |
94 | cmdtp->name); | |
95 | return 1; | |
96 | } | |
48abe7bf | 97 | |
712fbcf3 | 98 | comp = image_get_comp(hdr); |
5912d365 WW |
99 | if ((comp != IH_COMP_NONE) && (argc < 4)) { |
100 | printf("Must specify load address for %s command " | |
101 | "with compressed image\n", | |
d5934ad7 | 102 | cmdtp->name); |
48abe7bf WD |
103 | return 1; |
104 | } | |
48abe7bf | 105 | |
d5934ad7 MB |
106 | if (verify) { |
107 | printf(" Verifying Checksum ... "); | |
712fbcf3 | 108 | if (!image_check_dcrc(hdr)) { |
d5934ad7 MB |
109 | printf("Bad Data CRC\n"); |
110 | return 1; | |
48abe7bf | 111 | } |
d5934ad7 | 112 | printf("OK\n"); |
48abe7bf | 113 | } |
d5934ad7 | 114 | |
712fbcf3 | 115 | count = image_multi_count(hdr); |
1b7897f2 | 116 | if (part >= count) { |
d5934ad7 MB |
117 | printf("Bad Image Part\n"); |
118 | return 1; | |
119 | } | |
1b7897f2 | 120 | |
712fbcf3 | 121 | image_multi_getimg(hdr, part, &data, &len); |
1b7897f2 | 122 | break; |
21d29f7f | 123 | #endif |
d5934ad7 MB |
124 | #if defined(CONFIG_FIT) |
125 | case IMAGE_FORMAT_FIT: | |
1b7897f2 | 126 | if (uname == NULL) { |
712fbcf3 | 127 | puts("No FIT subimage unit name\n"); |
1b7897f2 MB |
128 | return 1; |
129 | } | |
130 | ||
131 | printf("## Copying '%s' subimage from FIT image " | |
132 | "at %08lx ...\n", uname, addr); | |
133 | ||
134 | fit_hdr = (const void *)addr; | |
c5819701 | 135 | if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { |
712fbcf3 | 136 | puts("Bad FIT image format\n"); |
1b7897f2 MB |
137 | return 1; |
138 | } | |
139 | ||
140 | /* get subimage node offset */ | |
712fbcf3 | 141 | noffset = fit_image_get_node(fit_hdr, uname); |
1b7897f2 | 142 | if (noffset < 0) { |
712fbcf3 | 143 | printf("Can't find '%s' FIT subimage\n", uname); |
1b7897f2 MB |
144 | return 1; |
145 | } | |
146 | ||
240e58ee | 147 | if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE) |
5912d365 WW |
148 | && (argc < 4)) { |
149 | printf("Must specify load address for %s command " | |
150 | "with compressed image\n", | |
151 | cmdtp->name); | |
1b7897f2 MB |
152 | return 1; |
153 | } | |
154 | ||
155 | /* verify integrity */ | |
156 | if (verify) { | |
b8da8366 | 157 | if (!fit_image_verify(fit_hdr, noffset)) { |
712fbcf3 | 158 | puts("Bad Data Hash\n"); |
1b7897f2 MB |
159 | return 1; |
160 | } | |
161 | } | |
162 | ||
3695ea5d TFC |
163 | /* get subimage/external data address and length */ |
164 | if (fit_image_get_data_and_size(fit_hdr, noffset, | |
165 | &fit_data, &fit_len)) { | |
712fbcf3 | 166 | puts("Could not find script subimage data\n"); |
1b7897f2 MB |
167 | return 1; |
168 | } | |
169 | ||
88de6c51 DG |
170 | if (fit_image_get_comp(fit_hdr, noffset, &comp)) |
171 | comp = IH_COMP_NONE; | |
5912d365 | 172 | |
fbe7a155 | 173 | data = (ulong)fit_data; |
1b7897f2 MB |
174 | len = (ulong)fit_len; |
175 | break; | |
d5934ad7 MB |
176 | #endif |
177 | default: | |
712fbcf3 | 178 | puts("Invalid image type for imxtract\n"); |
48abe7bf WD |
179 | return 1; |
180 | } | |
48abe7bf WD |
181 | |
182 | if (argc > 3) { | |
5912d365 WW |
183 | switch (comp) { |
184 | case IH_COMP_NONE: | |
185 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
186 | { | |
187 | size_t l = len; | |
188 | size_t tail; | |
189 | void *to = (void *) dest; | |
190 | void *from = (void *)data; | |
191 | ||
712fbcf3 | 192 | printf(" Loading part %d ... ", part); |
5912d365 WW |
193 | |
194 | while (l > 0) { | |
195 | tail = (l > CHUNKSZ) ? CHUNKSZ : l; | |
29caf930 | 196 | schedule(); |
712fbcf3 | 197 | memmove(to, from, tail); |
5912d365 WW |
198 | to += tail; |
199 | from += tail; | |
200 | l -= tail; | |
201 | } | |
202 | } | |
203 | #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */ | |
712fbcf3 SW |
204 | printf(" Loading part %d ... ", part); |
205 | memmove((char *) dest, (char *)data, len); | |
5912d365 WW |
206 | #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */ |
207 | break; | |
0e0996ef | 208 | #ifdef CONFIG_GZIP |
5912d365 | 209 | case IH_COMP_GZIP: |
712fbcf3 SW |
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"); | |
5912d365 WW |
214 | return 1; |
215 | } | |
216 | break; | |
0e0996ef | 217 | #endif |
c76c93a3 | 218 | #if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT) |
5912d365 | 219 | case IH_COMP_BZIP2: |
5f566f45 WD |
220 | { |
221 | int i; | |
222 | ||
712fbcf3 | 223 | printf(" Uncompressing part %d ... ", part); |
5f566f45 | 224 | /* |
93910edb | 225 | * If we've got less than 4 MB of malloc() |
5f566f45 WD |
226 | * space, use slower decompression algorithm |
227 | * which requires at most 2300 KB of memory. | |
228 | */ | |
229 | i = BZ2_bzBuffToBuffDecompress( | |
628af179 | 230 | map_sysmem(ntohl(hdr->ih_load), 0), |
5f566f45 WD |
231 | &unc_len, (char *)data, len, |
232 | CONFIG_SYS_MALLOC_LEN < (4096 * 1024), | |
233 | 0); | |
234 | if (i != BZ_OK) { | |
712fbcf3 | 235 | printf("BUNZIP2 ERROR %d - " |
5f566f45 WD |
236 | "image not loaded\n", i); |
237 | return 1; | |
238 | } | |
5912d365 WW |
239 | } |
240 | break; | |
241 | #endif /* CONFIG_BZIP2 */ | |
822911d5 DG |
242 | #if IS_ENABLED(CONFIG_ZSTD) |
243 | case IH_COMP_ZSTD: | |
244 | { | |
245 | int ret; | |
246 | struct abuf in, out; | |
247 | ||
248 | printf(" Uncompressing part %d ... ", part); | |
249 | ||
250 | abuf_init_set(&in, (void *)data, len); | |
251 | abuf_init_set(&out, (void *)dest, unc_len); | |
252 | ret = zstd_decompress(&in, &out); | |
253 | if (ret < 0) { | |
254 | printf("ZSTD ERROR %d - " | |
255 | "image not loaded\n", ret); | |
256 | return 1; | |
257 | } | |
258 | len = ret; | |
259 | } | |
260 | break; | |
261 | #endif | |
5912d365 | 262 | default: |
712fbcf3 | 263 | printf("Unimplemented compression type %d\n", comp); |
5912d365 WW |
264 | return 1; |
265 | } | |
712fbcf3 | 266 | puts("OK\n"); |
48abe7bf WD |
267 | } |
268 | ||
8354aa27 | 269 | flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN)); |
c72b65cc | 270 | |
018f5303 SG |
271 | env_set_hex("fileaddr", data); |
272 | env_set_hex("filesize", len); | |
48abe7bf WD |
273 | |
274 | return 0; | |
275 | } | |
276 | ||
3616218b | 277 | U_BOOT_LONGHELP(imgextract, |
a89c33db WD |
278 | "addr part [dest]\n" |
279 | " - extract <part> from legacy image at <addr> and copy to <dest>" | |
1b7897f2 | 280 | #if defined(CONFIG_FIT) |
a89c33db WD |
281 | "\n" |
282 | "addr uname [dest]\n" | |
283 | " - extract <uname> subimage from FIT image at <addr> and copy to <dest>" | |
1b7897f2 | 284 | #endif |
3616218b | 285 | ); |
088f1b19 KP |
286 | |
287 | U_BOOT_CMD( | |
288 | imxtract, 4, 1, do_imgextract, | |
289 | "extract a part of a multi-image", imgextract_help_text | |
1b7897f2 | 290 | ); |