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