]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_ximg.c
ARM: zynq: Fix bootmode mask
[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;
35 ulong data, len, count;
fbe7a155 36 int verify;
1b7897f2 37 int part = 0;
628af179 38 image_header_t *hdr = NULL;
1b7897f2 39#if defined(CONFIG_FIT)
fbe7a155 40 const char *uname = NULL;
1b7897f2
MB
41 const void* fit_hdr;
42 int noffset;
43 const void *fit_data;
44 size_t fit_len;
45#endif
a92181b3 46#ifdef CONFIG_GZIP
5912d365 47 uint unc_len = CONFIG_SYS_XIMG_LEN;
a92181b3 48#endif
5912d365 49 uint8_t comp;
48abe7bf 50
712fbcf3 51 verify = getenv_yesno("verify");
48abe7bf
WD
52
53 if (argc > 1) {
54 addr = simple_strtoul(argv[1], NULL, 16);
55 }
56 if (argc > 2) {
57 part = simple_strtoul(argv[2], NULL, 16);
1b7897f2
MB
58#if defined(CONFIG_FIT)
59 uname = argv[2];
60#endif
48abe7bf
WD
61 }
62 if (argc > 3) {
63 dest = simple_strtoul(argv[3], NULL, 16);
64 }
65
712fbcf3 66 switch (genimg_get_format((void *)addr)) {
d5934ad7 67 case IMAGE_FORMAT_LEGACY:
48abe7bf 68
1b7897f2
MB
69 printf("## Copying part %d from legacy image "
70 "at %08lx ...\n", part, addr);
71
d5934ad7 72 hdr = (image_header_t *)addr;
712fbcf3 73 if (!image_check_magic(hdr)) {
d5934ad7
MB
74 printf("Bad Magic Number\n");
75 return 1;
76 }
48abe7bf 77
712fbcf3 78 if (!image_check_hcrc(hdr)) {
d5934ad7
MB
79 printf("Bad Header Checksum\n");
80 return 1;
81 }
1b7897f2 82#ifdef DEBUG
712fbcf3 83 image_print_contents(hdr);
1b7897f2 84#endif
48abe7bf 85
712fbcf3 86 if (!image_check_type(hdr, IH_TYPE_MULTI)) {
d5934ad7
MB
87 printf("Wrong Image Type for %s command\n",
88 cmdtp->name);
89 return 1;
90 }
48abe7bf 91
712fbcf3 92 comp = image_get_comp(hdr);
5912d365
WW
93 if ((comp != IH_COMP_NONE) && (argc < 4)) {
94 printf("Must specify load address for %s command "
95 "with compressed image\n",
d5934ad7 96 cmdtp->name);
48abe7bf
WD
97 return 1;
98 }
48abe7bf 99
d5934ad7
MB
100 if (verify) {
101 printf(" Verifying Checksum ... ");
712fbcf3 102 if (!image_check_dcrc(hdr)) {
d5934ad7
MB
103 printf("Bad Data CRC\n");
104 return 1;
48abe7bf 105 }
d5934ad7 106 printf("OK\n");
48abe7bf 107 }
d5934ad7 108
712fbcf3 109 count = image_multi_count(hdr);
1b7897f2 110 if (part >= count) {
d5934ad7
MB
111 printf("Bad Image Part\n");
112 return 1;
113 }
1b7897f2 114
712fbcf3 115 image_multi_getimg(hdr, part, &data, &len);
1b7897f2 116 break;
d5934ad7
MB
117#if defined(CONFIG_FIT)
118 case IMAGE_FORMAT_FIT:
1b7897f2 119 if (uname == NULL) {
712fbcf3 120 puts("No FIT subimage unit name\n");
1b7897f2
MB
121 return 1;
122 }
123
124 printf("## Copying '%s' subimage from FIT image "
125 "at %08lx ...\n", uname, addr);
126
127 fit_hdr = (const void *)addr;
712fbcf3
SW
128 if (!fit_check_format(fit_hdr)) {
129 puts("Bad FIT image format\n");
1b7897f2
MB
130 return 1;
131 }
132
133 /* get subimage node offset */
712fbcf3 134 noffset = fit_image_get_node(fit_hdr, uname);
1b7897f2 135 if (noffset < 0) {
712fbcf3 136 printf("Can't find '%s' FIT subimage\n", uname);
1b7897f2
MB
137 return 1;
138 }
139
712fbcf3 140 if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
5912d365
WW
141 && (argc < 4)) {
142 printf("Must specify load address for %s command "
143 "with compressed image\n",
144 cmdtp->name);
1b7897f2
MB
145 return 1;
146 }
147
148 /* verify integrity */
149 if (verify) {
b8da8366 150 if (!fit_image_verify(fit_hdr, noffset)) {
712fbcf3 151 puts("Bad Data Hash\n");
1b7897f2
MB
152 return 1;
153 }
154 }
155
156 /* get subimage data address and length */
712fbcf3 157 if (fit_image_get_data(fit_hdr, noffset,
5912d365 158 &fit_data, &fit_len)) {
712fbcf3 159 puts("Could not find script subimage data\n");
1b7897f2
MB
160 return 1;
161 }
162
712fbcf3
SW
163 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
164 puts("Could not find script subimage "
5912d365
WW
165 "compression type\n");
166 return 1;
167 }
168
fbe7a155 169 data = (ulong)fit_data;
1b7897f2
MB
170 len = (ulong)fit_len;
171 break;
d5934ad7
MB
172#endif
173 default:
712fbcf3 174 puts("Invalid image type for imxtract\n");
48abe7bf
WD
175 return 1;
176 }
48abe7bf
WD
177
178 if (argc > 3) {
5912d365
WW
179 switch (comp) {
180 case IH_COMP_NONE:
181#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
182 {
183 size_t l = len;
184 size_t tail;
185 void *to = (void *) dest;
186 void *from = (void *)data;
187
712fbcf3 188 printf(" Loading part %d ... ", part);
5912d365
WW
189
190 while (l > 0) {
191 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
192 WATCHDOG_RESET();
712fbcf3 193 memmove(to, from, tail);
5912d365
WW
194 to += tail;
195 from += tail;
196 l -= tail;
197 }
198 }
199#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
712fbcf3
SW
200 printf(" Loading part %d ... ", part);
201 memmove((char *) dest, (char *)data, len);
5912d365
WW
202#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
203 break;
0e0996ef 204#ifdef CONFIG_GZIP
5912d365 205 case IH_COMP_GZIP:
712fbcf3
SW
206 printf(" Uncompressing part %d ... ", part);
207 if (gunzip((void *) dest, unc_len,
208 (uchar *) data, &len) != 0) {
209 puts("GUNZIP ERROR - image not loaded\n");
5912d365
WW
210 return 1;
211 }
212 break;
0e0996ef 213#endif
5912d365
WW
214#if defined(CONFIG_BZIP2)
215 case IH_COMP_BZIP2:
5f566f45
WD
216 {
217 int i;
218
712fbcf3 219 printf(" Uncompressing part %d ... ", part);
5f566f45 220 /*
93910edb 221 * If we've got less than 4 MB of malloc()
5f566f45
WD
222 * space, use slower decompression algorithm
223 * which requires at most 2300 KB of memory.
224 */
225 i = BZ2_bzBuffToBuffDecompress(
628af179 226 map_sysmem(ntohl(hdr->ih_load), 0),
5f566f45
WD
227 &unc_len, (char *)data, len,
228 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
229 0);
230 if (i != BZ_OK) {
712fbcf3 231 printf("BUNZIP2 ERROR %d - "
5f566f45
WD
232 "image not loaded\n", i);
233 return 1;
234 }
5912d365
WW
235 }
236 break;
237#endif /* CONFIG_BZIP2 */
238 default:
712fbcf3 239 printf("Unimplemented compression type %d\n", comp);
5912d365
WW
240 return 1;
241 }
712fbcf3 242 puts("OK\n");
48abe7bf
WD
243 }
244
41ef372c
SG
245 setenv_hex("fileaddr", data);
246 setenv_hex("filesize", len);
48abe7bf
WD
247
248 return 0;
249}
250
088f1b19
KP
251#ifdef CONFIG_SYS_LONGHELP
252static char imgextract_help_text[] =
a89c33db
WD
253 "addr part [dest]\n"
254 " - extract <part> from legacy image at <addr> and copy to <dest>"
1b7897f2 255#if defined(CONFIG_FIT)
a89c33db
WD
256 "\n"
257 "addr uname [dest]\n"
258 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
1b7897f2 259#endif
088f1b19
KP
260 "";
261#endif
262
263U_BOOT_CMD(
264 imxtract, 4, 1, do_imgextract,
265 "extract a part of a multi-image", imgextract_help_text
1b7897f2 266);