]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_ximg.c
Licenses: introduce SPDX Unique Lincense Identifiers
[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 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
48abe7bf
WD
27
28/*
29 * Multi Image extract
30 */
31#include <common.h>
32#include <command.h>
33#include <image.h>
5912d365
WW
34#include <watchdog.h>
35#if defined(CONFIG_BZIP2)
36#include <bzlib.h>
37#endif
48abe7bf
WD
38#include <asm/byteorder.h>
39
5912d365
WW
40#ifndef CONFIG_SYS_XIMG_LEN
41/* use 8MByte as default max gunzip size */
42#define CONFIG_SYS_XIMG_LEN 0x800000
43#endif
44
088f1b19 45static int
54841ab5 46do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
48abe7bf 47{
1b7897f2
MB
48 ulong addr = load_addr;
49 ulong dest = 0;
50 ulong data, len, count;
fbe7a155 51 int verify;
1b7897f2 52 int part = 0;
1b7897f2
MB
53 image_header_t *hdr;
54#if defined(CONFIG_FIT)
fbe7a155 55 const char *uname = NULL;
1b7897f2
MB
56 const void* fit_hdr;
57 int noffset;
58 const void *fit_data;
59 size_t fit_len;
60#endif
a92181b3 61#ifdef CONFIG_GZIP
5912d365 62 uint unc_len = CONFIG_SYS_XIMG_LEN;
a92181b3 63#endif
5912d365 64 uint8_t comp;
48abe7bf 65
712fbcf3 66 verify = getenv_yesno("verify");
48abe7bf
WD
67
68 if (argc > 1) {
69 addr = simple_strtoul(argv[1], NULL, 16);
70 }
71 if (argc > 2) {
72 part = simple_strtoul(argv[2], NULL, 16);
1b7897f2
MB
73#if defined(CONFIG_FIT)
74 uname = argv[2];
75#endif
48abe7bf
WD
76 }
77 if (argc > 3) {
78 dest = simple_strtoul(argv[3], NULL, 16);
79 }
80
712fbcf3 81 switch (genimg_get_format((void *)addr)) {
d5934ad7 82 case IMAGE_FORMAT_LEGACY:
48abe7bf 83
1b7897f2
MB
84 printf("## Copying part %d from legacy image "
85 "at %08lx ...\n", part, addr);
86
d5934ad7 87 hdr = (image_header_t *)addr;
712fbcf3 88 if (!image_check_magic(hdr)) {
d5934ad7
MB
89 printf("Bad Magic Number\n");
90 return 1;
91 }
48abe7bf 92
712fbcf3 93 if (!image_check_hcrc(hdr)) {
d5934ad7
MB
94 printf("Bad Header Checksum\n");
95 return 1;
96 }
1b7897f2 97#ifdef DEBUG
712fbcf3 98 image_print_contents(hdr);
1b7897f2 99#endif
48abe7bf 100
712fbcf3 101 if (!image_check_type(hdr, IH_TYPE_MULTI)) {
d5934ad7
MB
102 printf("Wrong Image Type for %s command\n",
103 cmdtp->name);
104 return 1;
105 }
48abe7bf 106
712fbcf3 107 comp = image_get_comp(hdr);
5912d365
WW
108 if ((comp != IH_COMP_NONE) && (argc < 4)) {
109 printf("Must specify load address for %s command "
110 "with compressed image\n",
d5934ad7 111 cmdtp->name);
48abe7bf
WD
112 return 1;
113 }
48abe7bf 114
d5934ad7
MB
115 if (verify) {
116 printf(" Verifying Checksum ... ");
712fbcf3 117 if (!image_check_dcrc(hdr)) {
d5934ad7
MB
118 printf("Bad Data CRC\n");
119 return 1;
48abe7bf 120 }
d5934ad7 121 printf("OK\n");
48abe7bf 122 }
d5934ad7 123
712fbcf3 124 count = image_multi_count(hdr);
1b7897f2 125 if (part >= count) {
d5934ad7
MB
126 printf("Bad Image Part\n");
127 return 1;
128 }
1b7897f2 129
712fbcf3 130 image_multi_getimg(hdr, part, &data, &len);
1b7897f2 131 break;
d5934ad7
MB
132#if defined(CONFIG_FIT)
133 case IMAGE_FORMAT_FIT:
1b7897f2 134 if (uname == NULL) {
712fbcf3 135 puts("No FIT subimage unit name\n");
1b7897f2
MB
136 return 1;
137 }
138
139 printf("## Copying '%s' subimage from FIT image "
140 "at %08lx ...\n", uname, addr);
141
142 fit_hdr = (const void *)addr;
712fbcf3
SW
143 if (!fit_check_format(fit_hdr)) {
144 puts("Bad FIT image format\n");
1b7897f2
MB
145 return 1;
146 }
147
148 /* get subimage node offset */
712fbcf3 149 noffset = fit_image_get_node(fit_hdr, uname);
1b7897f2 150 if (noffset < 0) {
712fbcf3 151 printf("Can't find '%s' FIT subimage\n", uname);
1b7897f2
MB
152 return 1;
153 }
154
712fbcf3 155 if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
5912d365
WW
156 && (argc < 4)) {
157 printf("Must specify load address for %s command "
158 "with compressed image\n",
159 cmdtp->name);
1b7897f2
MB
160 return 1;
161 }
162
163 /* verify integrity */
164 if (verify) {
b8da8366 165 if (!fit_image_verify(fit_hdr, noffset)) {
712fbcf3 166 puts("Bad Data Hash\n");
1b7897f2
MB
167 return 1;
168 }
169 }
170
171 /* get subimage data address and length */
712fbcf3 172 if (fit_image_get_data(fit_hdr, noffset,
5912d365 173 &fit_data, &fit_len)) {
712fbcf3 174 puts("Could not find script subimage data\n");
1b7897f2
MB
175 return 1;
176 }
177
712fbcf3
SW
178 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
179 puts("Could not find script subimage "
5912d365
WW
180 "compression type\n");
181 return 1;
182 }
183
fbe7a155 184 data = (ulong)fit_data;
1b7897f2
MB
185 len = (ulong)fit_len;
186 break;
d5934ad7
MB
187#endif
188 default:
712fbcf3 189 puts("Invalid image type for imxtract\n");
48abe7bf
WD
190 return 1;
191 }
48abe7bf
WD
192
193 if (argc > 3) {
5912d365
WW
194 switch (comp) {
195 case IH_COMP_NONE:
196#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
197 {
198 size_t l = len;
199 size_t tail;
200 void *to = (void *) dest;
201 void *from = (void *)data;
202
712fbcf3 203 printf(" Loading part %d ... ", part);
5912d365
WW
204
205 while (l > 0) {
206 tail = (l > CHUNKSZ) ? CHUNKSZ : l;
207 WATCHDOG_RESET();
712fbcf3 208 memmove(to, from, tail);
5912d365
WW
209 to += tail;
210 from += tail;
211 l -= tail;
212 }
213 }
214#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
712fbcf3
SW
215 printf(" Loading part %d ... ", part);
216 memmove((char *) dest, (char *)data, len);
5912d365
WW
217#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
218 break;
0e0996ef 219#ifdef CONFIG_GZIP
5912d365 220 case IH_COMP_GZIP:
712fbcf3
SW
221 printf(" Uncompressing part %d ... ", part);
222 if (gunzip((void *) dest, unc_len,
223 (uchar *) data, &len) != 0) {
224 puts("GUNZIP ERROR - image not loaded\n");
5912d365
WW
225 return 1;
226 }
227 break;
0e0996ef 228#endif
5912d365
WW
229#if defined(CONFIG_BZIP2)
230 case IH_COMP_BZIP2:
5f566f45
WD
231 {
232 int i;
233
712fbcf3 234 printf(" Uncompressing part %d ... ", part);
5f566f45 235 /*
93910edb 236 * If we've got less than 4 MB of malloc()
5f566f45
WD
237 * space, use slower decompression algorithm
238 * which requires at most 2300 KB of memory.
239 */
240 i = BZ2_bzBuffToBuffDecompress(
712fbcf3 241 (char *)ntohl(hdr->ih_load),
5f566f45
WD
242 &unc_len, (char *)data, len,
243 CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
244 0);
245 if (i != BZ_OK) {
712fbcf3 246 printf("BUNZIP2 ERROR %d - "
5f566f45
WD
247 "image not loaded\n", i);
248 return 1;
249 }
5912d365
WW
250 }
251 break;
252#endif /* CONFIG_BZIP2 */
253 default:
712fbcf3 254 printf("Unimplemented compression type %d\n", comp);
5912d365
WW
255 return 1;
256 }
712fbcf3 257 puts("OK\n");
48abe7bf
WD
258 }
259
41ef372c
SG
260 setenv_hex("fileaddr", data);
261 setenv_hex("filesize", len);
48abe7bf
WD
262
263 return 0;
264}
265
088f1b19
KP
266#ifdef CONFIG_SYS_LONGHELP
267static char imgextract_help_text[] =
a89c33db
WD
268 "addr part [dest]\n"
269 " - extract <part> from legacy image at <addr> and copy to <dest>"
1b7897f2 270#if defined(CONFIG_FIT)
a89c33db
WD
271 "\n"
272 "addr uname [dest]\n"
273 " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
1b7897f2 274#endif
088f1b19
KP
275 "";
276#endif
277
278U_BOOT_CMD(
279 imxtract, 4, 1, do_imgextract,
280 "extract a part of a multi-image", imgextract_help_text
1b7897f2 281);