]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/ximg.c
Merge tag 'video-for-2019.07-rc1' of git://git.denx.de/u-boot-video
[thirdparty/u-boot.git] / cmd / ximg.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
48abe7bf
WD
2/*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2003
7 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
48abe7bf
WD
8 */
9
48abe7bf
WD
10
11/*
12 * Multi Image extract
13 */
14#include <common.h>
15#include <command.h>
16#include <image.h>
0eb25b61 17#include <mapmem.h>
5912d365
WW
18#include <watchdog.h>
19#if defined(CONFIG_BZIP2)
20#include <bzlib.h>
21#endif
48abe7bf 22#include <asm/byteorder.h>
628af179 23#include <asm/io.h>
48abe7bf 24
5912d365
WW
25#ifndef CONFIG_SYS_XIMG_LEN
26/* use 8MByte as default max gunzip size */
27#define CONFIG_SYS_XIMG_LEN 0x800000
28#endif
29
088f1b19 30static int
54841ab5 31do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
48abe7bf 32{
1b7897f2
MB
33 ulong addr = load_addr;
34 ulong dest = 0;
21d29f7f 35 ulong data, len;
fbe7a155 36 int verify;
1b7897f2 37 int part = 0;
21d29f7f
HS
38#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
39 ulong count;
628af179 40 image_header_t *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
5912d365 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) {
57 addr = simple_strtoul(argv[1], NULL, 16);
58 }
59 if (argc > 2) {
60 part = simple_strtoul(argv[2], NULL, 16);
1b7897f2
MB
61#if defined(CONFIG_FIT)
62 uname = argv[2];
63#endif
48abe7bf
WD
64 }
65 if (argc > 3) {
66 dest = simple_strtoul(argv[3], NULL, 16);
67 }
68
712fbcf3 69 switch (genimg_get_format((void *)addr)) {
21d29f7f 70#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
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
d5934ad7 76 hdr = (image_header_t *)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;
712fbcf3
SW
134 if (!fit_check_format(fit_hdr)) {
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
162 /* get subimage data address and length */
712fbcf3 163 if (fit_image_get_data(fit_hdr, noffset,
5912d365 164 &fit_data, &fit_len)) {
712fbcf3 165 puts("Could not find script subimage data\n");
1b7897f2
MB
166 return 1;
167 }
168
712fbcf3
SW
169 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
170 puts("Could not find script subimage "
5912d365
WW
171 "compression type\n");
172 return 1;
173 }
174
fbe7a155 175 data = (ulong)fit_data;
1b7897f2
MB
176 len = (ulong)fit_len;
177 break;
d5934ad7
MB
178#endif
179 default:
712fbcf3 180 puts("Invalid image type for imxtract\n");
48abe7bf
WD
181 return 1;
182 }
48abe7bf
WD
183
184 if (argc > 3) {
5912d365
WW
185 switch (comp) {
186 case IH_COMP_NONE:
187#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
188 {
189 size_t l = len;
190 size_t tail;
191 void *to = (void *) dest;
192 void *from = (void *)data;
193
712fbcf3 194 printf(" Loading part %d ... ", part);
5912d365
WW
195
196 while (l > 0) {
197 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
198 WATCHDOG_RESET();
712fbcf3 199 memmove(to, from, tail);
5912d365
WW
200 to += tail;
201 from += tail;
202 l -= tail;
203 }
204 }
205#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
712fbcf3
SW
206 printf(" Loading part %d ... ", part);
207 memmove((char *) dest, (char *)data, len);
5912d365
WW
208#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
209 break;
0e0996ef 210#ifdef CONFIG_GZIP
5912d365 211 case IH_COMP_GZIP:
712fbcf3
SW
212 printf(" Uncompressing part %d ... ", part);
213 if (gunzip((void *) dest, unc_len,
214 (uchar *) data, &len) != 0) {
215 puts("GUNZIP ERROR - image not loaded\n");
5912d365
WW
216 return 1;
217 }
218 break;
0e0996ef 219#endif
21d29f7f 220#if defined(CONFIG_BZIP2) && defined(CONFIG_IMAGE_FORMAT_LEGACY)
5912d365 221 case IH_COMP_BZIP2:
5f566f45
WD
222 {
223 int i;
224
712fbcf3 225 printf(" Uncompressing part %d ... ", part);
5f566f45 226 /*
93910edb 227 * If we've got less than 4 MB of malloc()
5f566f45
WD
228 * space, use slower decompression algorithm
229 * which requires at most 2300 KB of memory.
230 */
231 i = BZ2_bzBuffToBuffDecompress(
628af179 232 map_sysmem(ntohl(hdr->ih_load), 0),
5f566f45
WD
233 &unc_len, (char *)data, len,
234 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
235 0);
236 if (i != BZ_OK) {
712fbcf3 237 printf("BUNZIP2 ERROR %d - "
5f566f45
WD
238 "image not loaded\n", i);
239 return 1;
240 }
5912d365
WW
241 }
242 break;
243#endif /* CONFIG_BZIP2 */
244 default:
712fbcf3 245 printf("Unimplemented compression type %d\n", comp);
5912d365
WW
246 return 1;
247 }
712fbcf3 248 puts("OK\n");
48abe7bf
WD
249 }
250
8354aa27 251 flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
c72b65cc 252
018f5303
SG
253 env_set_hex("fileaddr", data);
254 env_set_hex("filesize", len);
48abe7bf
WD
255
256 return 0;
257}
258
088f1b19
KP
259#ifdef CONFIG_SYS_LONGHELP
260static char imgextract_help_text[] =
a89c33db
WD
261 "addr part [dest]\n"
262 " - extract <part> from legacy image at <addr> and copy to <dest>"
1b7897f2 263#if defined(CONFIG_FIT)
a89c33db
WD
264 "\n"
265 "addr uname [dest]\n"
266 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
1b7897f2 267#endif
088f1b19
KP
268 "";
269#endif
270
271U_BOOT_CMD(
272 imxtract, 4, 1, do_imgextract,
273 "extract a part of a multi-image", imgextract_help_text
1b7897f2 274);