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