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