Blame view

cmd/ximg.c 5.95 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
48abe7bfa   wdenk   Patch by Robert S...
2
3
4
5
6
7
  /*
   * (C) Copyright 2000-2004
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
   * (C) Copyright 2003
   * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
48abe7bfa   wdenk   Patch by Robert S...
8
   */
48abe7bfa   wdenk   Patch by Robert S...
9
10
11
12
13
14
  
  /*
   * Multi Image extract
   */
  #include <common.h>
  #include <command.h>
1eb69ae49   Simon Glass   common: Move ARM ...
15
  #include <cpu_func.h>
c7694dd48   Simon Glass   env: Move env_set...
16
  #include <env.h>
0c670fc14   Simon Glass   common: Move gzip...
17
  #include <gzip.h>
48abe7bfa   wdenk   Patch by Robert S...
18
  #include <image.h>
336d4615f   Simon Glass   dm: core: Create ...
19
  #include <malloc.h>
0eb25b619   Joe Hershberger   common: Make sure...
20
  #include <mapmem.h>
5912d3650   Wolfgang Wegner   add ability to ha...
21
22
23
24
  #include <watchdog.h>
  #if defined(CONFIG_BZIP2)
  #include <bzlib.h>
  #endif
48abe7bfa   wdenk   Patch by Robert S...
25
  #include <asm/byteorder.h>
628af1790   Simon Glass   sandbox: Correct ...
26
  #include <asm/io.h>
48abe7bfa   wdenk   Patch by Robert S...
27

5912d3650   Wolfgang Wegner   add ability to ha...
28
29
30
31
  #ifndef CONFIG_SYS_XIMG_LEN
  /* use 8MByte as default max gunzip size */
  #define CONFIG_SYS_XIMG_LEN	0x800000
  #endif
088f1b199   Kim Phillips   common/cmd_*.c: s...
32
  static int
54841ab50   Wolfgang Denk   Make sure that ar...
33
  do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
48abe7bfa   wdenk   Patch by Robert S...
34
  {
bb872dd93   Simon Glass   image: Rename loa...
35
  	ulong		addr = image_load_addr;
1b7897f28   Marian Balakowicz   [new uImage] Add ...
36
  	ulong		dest = 0;
21d29f7f9   Heiko Schocher   bootm: make use o...
37
  	ulong		data, len;
fbe7a1550   Bartlomiej Sieka   [new uImage] Comp...
38
  	int		verify;
1b7897f28   Marian Balakowicz   [new uImage] Add ...
39
  	int		part = 0;
c76c93a3d   Tom Rini   configs: Rename C...
40
  #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
21d29f7f9   Heiko Schocher   bootm: make use o...
41
  	ulong		count;
628af1790   Simon Glass   sandbox: Correct ...
42
  	image_header_t	*hdr = NULL;
21d29f7f9   Heiko Schocher   bootm: make use o...
43
  #endif
1b7897f28   Marian Balakowicz   [new uImage] Add ...
44
  #if defined(CONFIG_FIT)
fbe7a1550   Bartlomiej Sieka   [new uImage] Comp...
45
  	const char	*uname = NULL;
1b7897f28   Marian Balakowicz   [new uImage] Add ...
46
47
48
49
50
  	const void*	fit_hdr;
  	int		noffset;
  	const void	*fit_data;
  	size_t		fit_len;
  #endif
a92181b39   Simon Glass   x86: Fix warning ...
51
  #ifdef CONFIG_GZIP
5912d3650   Wolfgang Wegner   add ability to ha...
52
  	uint		unc_len = CONFIG_SYS_XIMG_LEN;
a92181b39   Simon Glass   x86: Fix warning ...
53
  #endif
5912d3650   Wolfgang Wegner   add ability to ha...
54
  	uint8_t		comp;
48abe7bfa   wdenk   Patch by Robert S...
55

bfebc8c96   Simon Glass   env: Rename geten...
56
  	verify = env_get_yesno("verify");
48abe7bfa   wdenk   Patch by Robert S...
57
58
59
60
61
62
  
  	if (argc > 1) {
  		addr = simple_strtoul(argv[1], NULL, 16);
  	}
  	if (argc > 2) {
  		part = simple_strtoul(argv[2], NULL, 16);
1b7897f28   Marian Balakowicz   [new uImage] Add ...
63
64
65
  #if defined(CONFIG_FIT)
  		uname = argv[2];
  #endif
48abe7bfa   wdenk   Patch by Robert S...
66
67
68
69
  	}
  	if (argc > 3) {
  		dest = simple_strtoul(argv[3], NULL, 16);
  	}
712fbcf38   Stephen Warren   checkpatch whites...
70
  	switch (genimg_get_format((void *)addr)) {
c76c93a3d   Tom Rini   configs: Rename C...
71
  #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
d5934ad77   Marian Balakowicz   [new uImage] Add ...
72
  	case IMAGE_FORMAT_LEGACY:
48abe7bfa   wdenk   Patch by Robert S...
73

1b7897f28   Marian Balakowicz   [new uImage] Add ...
74
75
76
  		printf("## Copying part %d from legacy image "
  			"at %08lx ...
  ", part, addr);
d5934ad77   Marian Balakowicz   [new uImage] Add ...
77
  		hdr = (image_header_t *)addr;
712fbcf38   Stephen Warren   checkpatch whites...
78
  		if (!image_check_magic(hdr)) {
d5934ad77   Marian Balakowicz   [new uImage] Add ...
79
80
81
82
  			printf("Bad Magic Number
  ");
  			return 1;
  		}
48abe7bfa   wdenk   Patch by Robert S...
83

712fbcf38   Stephen Warren   checkpatch whites...
84
  		if (!image_check_hcrc(hdr)) {
d5934ad77   Marian Balakowicz   [new uImage] Add ...
85
86
87
88
  			printf("Bad Header Checksum
  ");
  			return 1;
  		}
1b7897f28   Marian Balakowicz   [new uImage] Add ...
89
  #ifdef DEBUG
712fbcf38   Stephen Warren   checkpatch whites...
90
  		image_print_contents(hdr);
1b7897f28   Marian Balakowicz   [new uImage] Add ...
91
  #endif
48abe7bfa   wdenk   Patch by Robert S...
92

83636fa09   Pierre Aubert   Allow imxtract to...
93
94
  		if (!image_check_type(hdr, IH_TYPE_MULTI) &&
  		    !image_check_type(hdr, IH_TYPE_SCRIPT)) {
d5934ad77   Marian Balakowicz   [new uImage] Add ...
95
96
97
98
99
  			printf("Wrong Image Type for %s command
  ",
  					cmdtp->name);
  			return 1;
  		}
48abe7bfa   wdenk   Patch by Robert S...
100

712fbcf38   Stephen Warren   checkpatch whites...
101
  		comp = image_get_comp(hdr);
5912d3650   Wolfgang Wegner   add ability to ha...
102
103
104
105
  		if ((comp != IH_COMP_NONE) && (argc < 4)) {
  			printf("Must specify load address for %s command "
  					"with compressed image
  ",
d5934ad77   Marian Balakowicz   [new uImage] Add ...
106
  					cmdtp->name);
48abe7bfa   wdenk   Patch by Robert S...
107
108
  			return 1;
  		}
48abe7bfa   wdenk   Patch by Robert S...
109

d5934ad77   Marian Balakowicz   [new uImage] Add ...
110
111
  		if (verify) {
  			printf("   Verifying Checksum ... ");
712fbcf38   Stephen Warren   checkpatch whites...
112
  			if (!image_check_dcrc(hdr)) {
d5934ad77   Marian Balakowicz   [new uImage] Add ...
113
114
115
  				printf("Bad Data CRC
  ");
  				return 1;
48abe7bfa   wdenk   Patch by Robert S...
116
  			}
d5934ad77   Marian Balakowicz   [new uImage] Add ...
117
118
  			printf("OK
  ");
48abe7bfa   wdenk   Patch by Robert S...
119
  		}
d5934ad77   Marian Balakowicz   [new uImage] Add ...
120

712fbcf38   Stephen Warren   checkpatch whites...
121
  		count = image_multi_count(hdr);
1b7897f28   Marian Balakowicz   [new uImage] Add ...
122
  		if (part >= count) {
d5934ad77   Marian Balakowicz   [new uImage] Add ...
123
124
125
126
  			printf("Bad Image Part
  ");
  			return 1;
  		}
1b7897f28   Marian Balakowicz   [new uImage] Add ...
127

712fbcf38   Stephen Warren   checkpatch whites...
128
  		image_multi_getimg(hdr, part, &data, &len);
1b7897f28   Marian Balakowicz   [new uImage] Add ...
129
  		break;
21d29f7f9   Heiko Schocher   bootm: make use o...
130
  #endif
d5934ad77   Marian Balakowicz   [new uImage] Add ...
131
132
  #if defined(CONFIG_FIT)
  	case IMAGE_FORMAT_FIT:
1b7897f28   Marian Balakowicz   [new uImage] Add ...
133
  		if (uname == NULL) {
712fbcf38   Stephen Warren   checkpatch whites...
134
135
  			puts("No FIT subimage unit name
  ");
1b7897f28   Marian Balakowicz   [new uImage] Add ...
136
137
138
139
140
141
142
143
  			return 1;
  		}
  
  		printf("## Copying '%s' subimage from FIT image "
  			"at %08lx ...
  ", uname, addr);
  
  		fit_hdr = (const void *)addr;
712fbcf38   Stephen Warren   checkpatch whites...
144
145
146
  		if (!fit_check_format(fit_hdr)) {
  			puts("Bad FIT image format
  ");
1b7897f28   Marian Balakowicz   [new uImage] Add ...
147
148
149
150
  			return 1;
  		}
  
  		/* get subimage node offset */
712fbcf38   Stephen Warren   checkpatch whites...
151
  		noffset = fit_image_get_node(fit_hdr, uname);
1b7897f28   Marian Balakowicz   [new uImage] Add ...
152
  		if (noffset < 0) {
712fbcf38   Stephen Warren   checkpatch whites...
153
154
  			printf("Can't find '%s' FIT subimage
  ", uname);
1b7897f28   Marian Balakowicz   [new uImage] Add ...
155
156
  			return 1;
  		}
240e58ee5   Stefan Theil   cmd: ximg: Invert...
157
  		if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
5912d3650   Wolfgang Wegner   add ability to ha...
158
159
160
161
162
  		    && (argc < 4)) {
  			printf("Must specify load address for %s command "
  				"with compressed image
  ",
  				cmdtp->name);
1b7897f28   Marian Balakowicz   [new uImage] Add ...
163
164
165
166
167
  			return 1;
  		}
  
  		/* verify integrity */
  		if (verify) {
b8da83665   Simon Glass   image: Rename fit...
168
  			if (!fit_image_verify(fit_hdr, noffset)) {
712fbcf38   Stephen Warren   checkpatch whites...
169
170
  				puts("Bad Data Hash
  ");
1b7897f28   Marian Balakowicz   [new uImage] Add ...
171
172
173
  				return 1;
  			}
  		}
3695ea5de   Tien Fong Chee   cmd: ximg.c: Add ...
174
175
176
  		/* get subimage/external data address and length */
  		if (fit_image_get_data_and_size(fit_hdr, noffset,
  					       &fit_data, &fit_len)) {
712fbcf38   Stephen Warren   checkpatch whites...
177
178
  			puts("Could not find script subimage data
  ");
1b7897f28   Marian Balakowicz   [new uImage] Add ...
179
180
  			return 1;
  		}
712fbcf38   Stephen Warren   checkpatch whites...
181
182
  		if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
  			puts("Could not find script subimage "
5912d3650   Wolfgang Wegner   add ability to ha...
183
184
185
186
  				"compression type
  ");
  			return 1;
  		}
fbe7a1550   Bartlomiej Sieka   [new uImage] Comp...
187
  		data = (ulong)fit_data;
1b7897f28   Marian Balakowicz   [new uImage] Add ...
188
189
  		len = (ulong)fit_len;
  		break;
d5934ad77   Marian Balakowicz   [new uImage] Add ...
190
191
  #endif
  	default:
712fbcf38   Stephen Warren   checkpatch whites...
192
193
  		puts("Invalid image type for imxtract
  ");
48abe7bfa   wdenk   Patch by Robert S...
194
195
  		return 1;
  	}
48abe7bfa   wdenk   Patch by Robert S...
196
197
  
  	if (argc > 3) {
5912d3650   Wolfgang Wegner   add ability to ha...
198
199
200
201
202
203
204
205
  		switch (comp) {
  		case IH_COMP_NONE:
  #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  			{
  				size_t l = len;
  				size_t tail;
  				void *to = (void *) dest;
  				void *from = (void *)data;
712fbcf38   Stephen Warren   checkpatch whites...
206
  				printf("   Loading part %d ... ", part);
5912d3650   Wolfgang Wegner   add ability to ha...
207
208
209
210
  
  				while (l > 0) {
  					tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  					WATCHDOG_RESET();
712fbcf38   Stephen Warren   checkpatch whites...
211
  					memmove(to, from, tail);
5912d3650   Wolfgang Wegner   add ability to ha...
212
213
214
215
216
217
  					to += tail;
  					from += tail;
  					l -= tail;
  				}
  			}
  #else	/* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
712fbcf38   Stephen Warren   checkpatch whites...
218
219
  			printf("   Loading part %d ... ", part);
  			memmove((char *) dest, (char *)data, len);
5912d3650   Wolfgang Wegner   add ability to ha...
220
221
  #endif	/* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  			break;
0e0996ef4   Matthew McClintock   common/cmd_ximg.c...
222
  #ifdef CONFIG_GZIP
5912d3650   Wolfgang Wegner   add ability to ha...
223
  		case IH_COMP_GZIP:
712fbcf38   Stephen Warren   checkpatch whites...
224
225
226
227
228
  			printf("   Uncompressing part %d ... ", part);
  			if (gunzip((void *) dest, unc_len,
  				   (uchar *) data, &len) != 0) {
  				puts("GUNZIP ERROR - image not loaded
  ");
5912d3650   Wolfgang Wegner   add ability to ha...
229
230
231
  				return 1;
  			}
  			break;
0e0996ef4   Matthew McClintock   common/cmd_ximg.c...
232
  #endif
c76c93a3d   Tom Rini   configs: Rename C...
233
  #if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
5912d3650   Wolfgang Wegner   add ability to ha...
234
  		case IH_COMP_BZIP2:
5f566f454   Wolfgang Denk   cmd_ximg.c: fix e...
235
236
  			{
  				int i;
712fbcf38   Stephen Warren   checkpatch whites...
237
  				printf("   Uncompressing part %d ... ", part);
5f566f454   Wolfgang Denk   cmd_ximg.c: fix e...
238
  				/*
93910edb5   Wolfgang Denk   Prepare v2010.03-rc1
239
  				 * If we've got less than 4 MB of malloc()
5f566f454   Wolfgang Denk   cmd_ximg.c: fix e...
240
241
242
243
  				 * space, use slower decompression algorithm
  				 * which requires at most 2300 KB of memory.
  				 */
  				i = BZ2_bzBuffToBuffDecompress(
628af1790   Simon Glass   sandbox: Correct ...
244
  					map_sysmem(ntohl(hdr->ih_load), 0),
5f566f454   Wolfgang Denk   cmd_ximg.c: fix e...
245
246
247
248
  					&unc_len, (char *)data, len,
  					CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
  					0);
  				if (i != BZ_OK) {
712fbcf38   Stephen Warren   checkpatch whites...
249
  					printf("BUNZIP2 ERROR %d - "
5f566f454   Wolfgang Denk   cmd_ximg.c: fix e...
250
251
252
253
  						"image not loaded
  ", i);
  					return 1;
  				}
5912d3650   Wolfgang Wegner   add ability to ha...
254
255
256
257
  			}
  			break;
  #endif /* CONFIG_BZIP2 */
  		default:
712fbcf38   Stephen Warren   checkpatch whites...
258
259
  			printf("Unimplemented compression type %d
  ", comp);
5912d3650   Wolfgang Wegner   add ability to ha...
260
261
  			return 1;
  		}
712fbcf38   Stephen Warren   checkpatch whites...
262
263
  		puts("OK
  ");
48abe7bfa   wdenk   Patch by Robert S...
264
  	}
8354aa278   Mario Six   cmd: ximg: Respec...
265
  	flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
c72b65ccd   Pieter Voorthuijsen   common: add cache...
266

018f53032   Simon Glass   env: Rename commo...
267
268
  	env_set_hex("fileaddr", data);
  	env_set_hex("filesize", len);
48abe7bfa   wdenk   Patch by Robert S...
269
270
271
  
  	return 0;
  }
088f1b199   Kim Phillips   common/cmd_*.c: s...
272
273
  #ifdef CONFIG_SYS_LONGHELP
  static char imgextract_help_text[] =
a89c33db9   Wolfgang Denk   General help mess...
274
275
276
  	"addr part [dest]
  "
  	"    - extract <part> from legacy image at <addr> and copy to <dest>"
1b7897f28   Marian Balakowicz   [new uImage] Add ...
277
  #if defined(CONFIG_FIT)
a89c33db9   Wolfgang Denk   General help mess...
278
279
280
281
282
  	"
  "
  	"addr uname [dest]
  "
  	"    - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
1b7897f28   Marian Balakowicz   [new uImage] Add ...
283
  #endif
088f1b199   Kim Phillips   common/cmd_*.c: s...
284
285
286
287
288
289
  	"";
  #endif
  
  U_BOOT_CMD(
  	imxtract, 4, 1, do_imgextract,
  	"extract a part of a multi-image", imgextract_help_text
1b7897f28   Marian Balakowicz   [new uImage] Add ...
290
  );