]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_ximg.c
usb: dwc3: add dwc3 folder from linux kernel to u-boot
[people/ms/u-boot.git] / common / cmd_ximg.c
CommitLineData
48abe7bf
WD
1/*
2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2003
6 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
7 *
1a459660 8 * SPDX-License-Identifier: GPL-2.0+
48abe7bf
WD
9 */
10
48abe7bf
WD
11
12/*
13 * Multi Image extract
14 */
15#include <common.h>
16#include <command.h>
17#include <image.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
712fbcf3 54 verify = getenv_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
712fbcf3 90 if (!image_check_type(hdr, IH_TYPE_MULTI)) {
d5934ad7
MB
91 printf("Wrong Image Type for %s command\n",
92 cmdtp->name);
93 return 1;
94 }
48abe7bf 95
712fbcf3 96 comp = image_get_comp(hdr);
5912d365
WW
97 if ((comp != IH_COMP_NONE) && (argc < 4)) {
98 printf("Must specify load address for %s command "
99 "with compressed image\n",
d5934ad7 100 cmdtp->name);
48abe7bf
WD
101 return 1;
102 }
48abe7bf 103
d5934ad7
MB
104 if (verify) {
105 printf(" Verifying Checksum ... ");
712fbcf3 106 if (!image_check_dcrc(hdr)) {
d5934ad7
MB
107 printf("Bad Data CRC\n");
108 return 1;
48abe7bf 109 }
d5934ad7 110 printf("OK\n");
48abe7bf 111 }
d5934ad7 112
712fbcf3 113 count = image_multi_count(hdr);
1b7897f2 114 if (part >= count) {
d5934ad7
MB
115 printf("Bad Image Part\n");
116 return 1;
117 }
1b7897f2 118
712fbcf3 119 image_multi_getimg(hdr, part, &data, &len);
1b7897f2 120 break;
21d29f7f 121#endif
d5934ad7
MB
122#if defined(CONFIG_FIT)
123 case IMAGE_FORMAT_FIT:
1b7897f2 124 if (uname == NULL) {
712fbcf3 125 puts("No FIT subimage unit name\n");
1b7897f2
MB
126 return 1;
127 }
128
129 printf("## Copying '%s' subimage from FIT image "
130 "at %08lx ...\n", uname, addr);
131
132 fit_hdr = (const void *)addr;
712fbcf3
SW
133 if (!fit_check_format(fit_hdr)) {
134 puts("Bad FIT image format\n");
1b7897f2
MB
135 return 1;
136 }
137
138 /* get subimage node offset */
712fbcf3 139 noffset = fit_image_get_node(fit_hdr, uname);
1b7897f2 140 if (noffset < 0) {
712fbcf3 141 printf("Can't find '%s' FIT subimage\n", uname);
1b7897f2
MB
142 return 1;
143 }
144
712fbcf3 145 if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
5912d365
WW
146 && (argc < 4)) {
147 printf("Must specify load address for %s command "
148 "with compressed image\n",
149 cmdtp->name);
1b7897f2
MB
150 return 1;
151 }
152
153 /* verify integrity */
154 if (verify) {
b8da8366 155 if (!fit_image_verify(fit_hdr, noffset)) {
712fbcf3 156 puts("Bad Data Hash\n");
1b7897f2
MB
157 return 1;
158 }
159 }
160
161 /* get subimage data address and length */
712fbcf3 162 if (fit_image_get_data(fit_hdr, noffset,
5912d365 163 &fit_data, &fit_len)) {
712fbcf3 164 puts("Could not find script subimage data\n");
1b7897f2
MB
165 return 1;
166 }
167
712fbcf3
SW
168 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
169 puts("Could not find script subimage "
5912d365
WW
170 "compression type\n");
171 return 1;
172 }
173
fbe7a155 174 data = (ulong)fit_data;
1b7897f2
MB
175 len = (ulong)fit_len;
176 break;
d5934ad7
MB
177#endif
178 default:
712fbcf3 179 puts("Invalid image type for imxtract\n");
48abe7bf
WD
180 return 1;
181 }
48abe7bf
WD
182
183 if (argc > 3) {
5912d365
WW
184 switch (comp) {
185 case IH_COMP_NONE:
186#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
187 {
188 size_t l = len;
189 size_t tail;
190 void *to = (void *) dest;
191 void *from = (void *)data;
192
712fbcf3 193 printf(" Loading part %d ... ", part);
5912d365
WW
194
195 while (l > 0) {
196 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
197 WATCHDOG_RESET();
712fbcf3 198 memmove(to, from, tail);
5912d365
WW
199 to += tail;
200 from += tail;
201 l -= tail;
202 }
203 }
204#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
712fbcf3
SW
205 printf(" Loading part %d ... ", part);
206 memmove((char *) dest, (char *)data, len);
5912d365
WW
207#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
208 break;
0e0996ef 209#ifdef CONFIG_GZIP
5912d365 210 case IH_COMP_GZIP:
712fbcf3
SW
211 printf(" Uncompressing part %d ... ", part);
212 if (gunzip((void *) dest, unc_len,
213 (uchar *) data, &len) != 0) {
214 puts("GUNZIP ERROR - image not loaded\n");
5912d365
WW
215 return 1;
216 }
217 break;
0e0996ef 218#endif
21d29f7f 219#if defined(CONFIG_BZIP2) && defined(CONFIG_IMAGE_FORMAT_LEGACY)
5912d365 220 case IH_COMP_BZIP2:
5f566f45
WD
221 {
222 int i;
223
712fbcf3 224 printf(" Uncompressing part %d ... ", part);
5f566f45 225 /*
93910edb 226 * If we've got less than 4 MB of malloc()
5f566f45
WD
227 * space, use slower decompression algorithm
228 * which requires at most 2300 KB of memory.
229 */
230 i = BZ2_bzBuffToBuffDecompress(
628af179 231 map_sysmem(ntohl(hdr->ih_load), 0),
5f566f45
WD
232 &unc_len, (char *)data, len,
233 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
234 0);
235 if (i != BZ_OK) {
712fbcf3 236 printf("BUNZIP2 ERROR %d - "
5f566f45
WD
237 "image not loaded\n", i);
238 return 1;
239 }
5912d365
WW
240 }
241 break;
242#endif /* CONFIG_BZIP2 */
243 default:
712fbcf3 244 printf("Unimplemented compression type %d\n", comp);
5912d365
WW
245 return 1;
246 }
712fbcf3 247 puts("OK\n");
48abe7bf
WD
248 }
249
c72b65cc
PV
250 flush_cache(dest, len);
251
41ef372c
SG
252 setenv_hex("fileaddr", data);
253 setenv_hex("filesize", len);
48abe7bf
WD
254
255 return 0;
256}
257
088f1b19
KP
258#ifdef CONFIG_SYS_LONGHELP
259static char imgextract_help_text[] =
a89c33db
WD
260 "addr part [dest]\n"
261 " - extract <part> from legacy image at <addr> and copy to <dest>"
1b7897f2 262#if defined(CONFIG_FIT)
a89c33db
WD
263 "\n"
264 "addr uname [dest]\n"
265 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
1b7897f2 266#endif
088f1b19
KP
267 "";
268#endif
269
270U_BOOT_CMD(
271 imxtract, 4, 1, do_imgextract,
272 "extract a part of a multi-image", imgextract_help_text
1b7897f2 273);