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