Blame view
cmd/ximg.c
5.85 KB
83d290c56
|
1 |
// SPDX-License-Identifier: GPL-2.0+ |
48abe7bfa
|
2 3 4 5 6 7 |
/* * (C) Copyright 2000-2004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. * * (C) Copyright 2003 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de> |
48abe7bfa
|
8 |
*/ |
48abe7bfa
|
9 10 11 12 13 14 15 |
/* * Multi Image extract */ #include <common.h> #include <command.h> #include <image.h> |
0eb25b619
|
16 |
#include <mapmem.h> |
5912d3650
|
17 18 19 20 |
#include <watchdog.h> #if defined(CONFIG_BZIP2) #include <bzlib.h> #endif |
48abe7bfa
|
21 |
#include <asm/byteorder.h> |
628af1790
|
22 |
#include <asm/io.h> |
48abe7bfa
|
23 |
|
5912d3650
|
24 25 26 27 |
#ifndef CONFIG_SYS_XIMG_LEN /* use 8MByte as default max gunzip size */ #define CONFIG_SYS_XIMG_LEN 0x800000 #endif |
088f1b199
|
28 |
static int |
54841ab50
|
29 |
do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) |
48abe7bfa
|
30 |
{ |
1b7897f28
|
31 32 |
ulong addr = load_addr; ulong dest = 0; |
21d29f7f9
|
33 |
ulong data, len; |
fbe7a1550
|
34 |
int verify; |
1b7897f28
|
35 |
int part = 0; |
21d29f7f9
|
36 37 |
#if defined(CONFIG_IMAGE_FORMAT_LEGACY) ulong count; |
628af1790
|
38 |
image_header_t *hdr = NULL; |
21d29f7f9
|
39 |
#endif |
1b7897f28
|
40 |
#if defined(CONFIG_FIT) |
fbe7a1550
|
41 |
const char *uname = NULL; |
1b7897f28
|
42 43 44 45 46 |
const void* fit_hdr; int noffset; const void *fit_data; size_t fit_len; #endif |
a92181b39
|
47 |
#ifdef CONFIG_GZIP |
5912d3650
|
48 |
uint unc_len = CONFIG_SYS_XIMG_LEN; |
a92181b39
|
49 |
#endif |
5912d3650
|
50 |
uint8_t comp; |
48abe7bfa
|
51 |
|
bfebc8c96
|
52 |
verify = env_get_yesno("verify"); |
48abe7bfa
|
53 54 55 56 57 58 |
if (argc > 1) { addr = simple_strtoul(argv[1], NULL, 16); } if (argc > 2) { part = simple_strtoul(argv[2], NULL, 16); |
1b7897f28
|
59 60 61 |
#if defined(CONFIG_FIT) uname = argv[2]; #endif |
48abe7bfa
|
62 63 64 65 |
} if (argc > 3) { dest = simple_strtoul(argv[3], NULL, 16); } |
712fbcf38
|
66 |
switch (genimg_get_format((void *)addr)) { |
21d29f7f9
|
67 |
#if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
d5934ad77
|
68 |
case IMAGE_FORMAT_LEGACY: |
48abe7bfa
|
69 |
|
1b7897f28
|
70 71 72 |
printf("## Copying part %d from legacy image " "at %08lx ... ", part, addr); |
d5934ad77
|
73 |
hdr = (image_header_t *)addr; |
712fbcf38
|
74 |
if (!image_check_magic(hdr)) { |
d5934ad77
|
75 76 77 78 |
printf("Bad Magic Number "); return 1; } |
48abe7bfa
|
79 |
|
712fbcf38
|
80 |
if (!image_check_hcrc(hdr)) { |
d5934ad77
|
81 82 83 84 |
printf("Bad Header Checksum "); return 1; } |
1b7897f28
|
85 |
#ifdef DEBUG |
712fbcf38
|
86 |
image_print_contents(hdr); |
1b7897f28
|
87 |
#endif |
48abe7bfa
|
88 |
|
83636fa09
|
89 90 |
if (!image_check_type(hdr, IH_TYPE_MULTI) && !image_check_type(hdr, IH_TYPE_SCRIPT)) { |
d5934ad77
|
91 92 93 94 95 |
printf("Wrong Image Type for %s command ", cmdtp->name); return 1; } |
48abe7bfa
|
96 |
|
712fbcf38
|
97 |
comp = image_get_comp(hdr); |
5912d3650
|
98 99 100 101 |
if ((comp != IH_COMP_NONE) && (argc < 4)) { printf("Must specify load address for %s command " "with compressed image ", |
d5934ad77
|
102 |
cmdtp->name); |
48abe7bfa
|
103 104 |
return 1; } |
48abe7bfa
|
105 |
|
d5934ad77
|
106 107 |
if (verify) { printf(" Verifying Checksum ... "); |
712fbcf38
|
108 |
if (!image_check_dcrc(hdr)) { |
d5934ad77
|
109 110 111 |
printf("Bad Data CRC "); return 1; |
48abe7bfa
|
112 |
} |
d5934ad77
|
113 114 |
printf("OK "); |
48abe7bfa
|
115 |
} |
d5934ad77
|
116 |
|
712fbcf38
|
117 |
count = image_multi_count(hdr); |
1b7897f28
|
118 |
if (part >= count) { |
d5934ad77
|
119 120 121 122 |
printf("Bad Image Part "); return 1; } |
1b7897f28
|
123 |
|
712fbcf38
|
124 |
image_multi_getimg(hdr, part, &data, &len); |
1b7897f28
|
125 |
break; |
21d29f7f9
|
126 |
#endif |
d5934ad77
|
127 128 |
#if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: |
1b7897f28
|
129 |
if (uname == NULL) { |
712fbcf38
|
130 131 |
puts("No FIT subimage unit name "); |
1b7897f28
|
132 133 134 135 136 137 138 139 |
return 1; } printf("## Copying '%s' subimage from FIT image " "at %08lx ... ", uname, addr); fit_hdr = (const void *)addr; |
712fbcf38
|
140 141 142 |
if (!fit_check_format(fit_hdr)) { puts("Bad FIT image format "); |
1b7897f28
|
143 144 145 146 |
return 1; } /* get subimage node offset */ |
712fbcf38
|
147 |
noffset = fit_image_get_node(fit_hdr, uname); |
1b7897f28
|
148 |
if (noffset < 0) { |
712fbcf38
|
149 150 |
printf("Can't find '%s' FIT subimage ", uname); |
1b7897f28
|
151 152 |
return 1; } |
712fbcf38
|
153 |
if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE) |
5912d3650
|
154 155 156 157 158 |
&& (argc < 4)) { printf("Must specify load address for %s command " "with compressed image ", cmdtp->name); |
1b7897f28
|
159 160 161 162 163 |
return 1; } /* verify integrity */ if (verify) { |
b8da83665
|
164 |
if (!fit_image_verify(fit_hdr, noffset)) { |
712fbcf38
|
165 166 |
puts("Bad Data Hash "); |
1b7897f28
|
167 168 169 170 171 |
return 1; } } /* get subimage data address and length */ |
712fbcf38
|
172 |
if (fit_image_get_data(fit_hdr, noffset, |
5912d3650
|
173 |
&fit_data, &fit_len)) { |
712fbcf38
|
174 175 |
puts("Could not find script subimage data "); |
1b7897f28
|
176 177 |
return 1; } |
712fbcf38
|
178 179 |
if (fit_image_get_comp(fit_hdr, noffset, &comp)) { puts("Could not find script subimage " |
5912d3650
|
180 181 182 183 |
"compression type "); return 1; } |
fbe7a1550
|
184 |
data = (ulong)fit_data; |
1b7897f28
|
185 186 |
len = (ulong)fit_len; break; |
d5934ad77
|
187 188 |
#endif default: |
712fbcf38
|
189 190 |
puts("Invalid image type for imxtract "); |
48abe7bfa
|
191 192 |
return 1; } |
48abe7bfa
|
193 194 |
if (argc > 3) { |
5912d3650
|
195 196 197 198 199 200 201 202 |
switch (comp) { case IH_COMP_NONE: #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) { size_t l = len; size_t tail; void *to = (void *) dest; void *from = (void *)data; |
712fbcf38
|
203 |
printf(" Loading part %d ... ", part); |
5912d3650
|
204 205 206 207 |
while (l > 0) { tail = (l > CHUNKSZ) ? CHUNKSZ : l; WATCHDOG_RESET(); |
712fbcf38
|
208 |
memmove(to, from, tail); |
5912d3650
|
209 210 211 212 213 214 |
to += tail; from += tail; l -= tail; } } #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */ |
712fbcf38
|
215 216 |
printf(" Loading part %d ... ", part); memmove((char *) dest, (char *)data, len); |
5912d3650
|
217 218 |
#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */ break; |
0e0996ef4
|
219 |
#ifdef CONFIG_GZIP |
5912d3650
|
220 |
case IH_COMP_GZIP: |
712fbcf38
|
221 222 223 224 225 |
printf(" Uncompressing part %d ... ", part); if (gunzip((void *) dest, unc_len, (uchar *) data, &len) != 0) { puts("GUNZIP ERROR - image not loaded "); |
5912d3650
|
226 227 228 |
return 1; } break; |
0e0996ef4
|
229 |
#endif |
21d29f7f9
|
230 |
#if defined(CONFIG_BZIP2) && defined(CONFIG_IMAGE_FORMAT_LEGACY) |
5912d3650
|
231 |
case IH_COMP_BZIP2: |
5f566f454
|
232 233 |
{ int i; |
712fbcf38
|
234 |
printf(" Uncompressing part %d ... ", part); |
5f566f454
|
235 |
/* |
93910edb5
|
236 |
* If we've got less than 4 MB of malloc() |
5f566f454
|
237 238 239 240 |
* space, use slower decompression algorithm * which requires at most 2300 KB of memory. */ i = BZ2_bzBuffToBuffDecompress( |
628af1790
|
241 |
map_sysmem(ntohl(hdr->ih_load), 0), |
5f566f454
|
242 243 244 245 |
&unc_len, (char *)data, len, CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0); if (i != BZ_OK) { |
712fbcf38
|
246 |
printf("BUNZIP2 ERROR %d - " |
5f566f454
|
247 248 249 250 |
"image not loaded ", i); return 1; } |
5912d3650
|
251 252 253 254 |
} break; #endif /* CONFIG_BZIP2 */ default: |
712fbcf38
|
255 256 |
printf("Unimplemented compression type %d ", comp); |
5912d3650
|
257 258 |
return 1; } |
712fbcf38
|
259 260 |
puts("OK "); |
48abe7bfa
|
261 |
} |
8354aa278
|
262 |
flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN)); |
c72b65ccd
|
263 |
|
018f53032
|
264 265 |
env_set_hex("fileaddr", data); env_set_hex("filesize", len); |
48abe7bfa
|
266 267 268 |
return 0; } |
088f1b199
|
269 270 |
#ifdef CONFIG_SYS_LONGHELP static char imgextract_help_text[] = |
a89c33db9
|
271 272 273 |
"addr part [dest] " " - extract <part> from legacy image at <addr> and copy to <dest>" |
1b7897f28
|
274 |
#if defined(CONFIG_FIT) |
a89c33db9
|
275 276 277 278 279 |
" " "addr uname [dest] " " - extract <uname> subimage from FIT image at <addr> and copy to <dest>" |
1b7897f28
|
280 |
#endif |
088f1b199
|
281 282 283 284 285 286 |
""; #endif U_BOOT_CMD( imxtract, 4, 1, do_imgextract, "extract a part of a multi-image", imgextract_help_text |
1b7897f28
|
287 |
); |