Blame view

cmd/ximg.c 5.82 KB
48abe7bfa   wdenk   Patch by Robert S...
1
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>
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
8
   * SPDX-License-Identifier:	GPL-2.0+
48abe7bfa   wdenk   Patch by Robert S...
9
   */
48abe7bfa   wdenk   Patch by Robert S...
10
11
12
13
14
15
16
  
  /*
   * Multi Image extract
   */
  #include <common.h>
  #include <command.h>
  #include <image.h>
0eb25b619   Joe Hershberger   common: Make sure...
17
  #include <mapmem.h>
5912d3650   Wolfgang Wegner   add ability to ha...
18
19
20
21
  #include <watchdog.h>
  #if defined(CONFIG_BZIP2)
  #include <bzlib.h>
  #endif
48abe7bfa   wdenk   Patch by Robert S...
22
  #include <asm/byteorder.h>
628af1790   Simon Glass   sandbox: Correct ...
23
  #include <asm/io.h>
48abe7bfa   wdenk   Patch by Robert S...
24

5912d3650   Wolfgang Wegner   add ability to ha...
25
26
27
28
  #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...
29
  static int
54841ab50   Wolfgang Denk   Make sure that ar...
30
  do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
48abe7bfa   wdenk   Patch by Robert S...
31
  {
1b7897f28   Marian Balakowicz   [new uImage] Add ...
32
33
  	ulong		addr = load_addr;
  	ulong		dest = 0;
21d29f7f9   Heiko Schocher   bootm: make use o...
34
  	ulong		data, len;
fbe7a1550   Bartlomiej Sieka   [new uImage] Comp...
35
  	int		verify;
1b7897f28   Marian Balakowicz   [new uImage] Add ...
36
  	int		part = 0;
21d29f7f9   Heiko Schocher   bootm: make use o...
37
38
  #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  	ulong		count;
628af1790   Simon Glass   sandbox: Correct ...
39
  	image_header_t	*hdr = NULL;
21d29f7f9   Heiko Schocher   bootm: make use o...
40
  #endif
1b7897f28   Marian Balakowicz   [new uImage] Add ...
41
  #if defined(CONFIG_FIT)
fbe7a1550   Bartlomiej Sieka   [new uImage] Comp...
42
  	const char	*uname = NULL;
1b7897f28   Marian Balakowicz   [new uImage] Add ...
43
44
45
46
47
  	const void*	fit_hdr;
  	int		noffset;
  	const void	*fit_data;
  	size_t		fit_len;
  #endif
a92181b39   Simon Glass   x86: Fix warning ...
48
  #ifdef CONFIG_GZIP
5912d3650   Wolfgang Wegner   add ability to ha...
49
  	uint		unc_len = CONFIG_SYS_XIMG_LEN;
a92181b39   Simon Glass   x86: Fix warning ...
50
  #endif
5912d3650   Wolfgang Wegner   add ability to ha...
51
  	uint8_t		comp;
48abe7bfa   wdenk   Patch by Robert S...
52

bfebc8c96   Simon Glass   env: Rename geten...
53
  	verify = env_get_yesno("verify");
48abe7bfa   wdenk   Patch by Robert S...
54
55
56
57
58
59
  
  	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 ...
60
61
62
  #if defined(CONFIG_FIT)
  		uname = argv[2];
  #endif
48abe7bfa   wdenk   Patch by Robert S...
63
64
65
66
  	}
  	if (argc > 3) {
  		dest = simple_strtoul(argv[3], NULL, 16);
  	}
712fbcf38   Stephen Warren   checkpatch whites...
67
  	switch (genimg_get_format((void *)addr)) {
21d29f7f9   Heiko Schocher   bootm: make use o...
68
  #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
d5934ad77   Marian Balakowicz   [new uImage] Add ...
69
  	case IMAGE_FORMAT_LEGACY:
48abe7bfa   wdenk   Patch by Robert S...
70

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

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

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

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

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

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

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