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