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