Blame view

tools/mkimage.c 17.5 KB
5b1d71372   wdenk   Initial revision
1
  /*
9d25438fe   Bartlomiej Sieka   [new uImage] Add ...
2
3
   * (C) Copyright 2008 Semihalf
   *
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
4
   * (C) Copyright 2000-2009
5b1d71372   wdenk   Initial revision
5
6
   * DENX Software Engineering
   * Wolfgang Denk, wd@denx.de
0c852a288   wdenk   * Patch by Rahul ...
7
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
8
   * SPDX-License-Identifier:	GPL-2.0+
5b1d71372   wdenk   Initial revision
9
   */
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
10
  #include "mkimage.h"
d21bd69b6   Sven Ebenfeld   tools: mkimage: a...
11
  #include "imximage.h"
5b1d71372   wdenk   Initial revision
12
  #include <image.h>
976b38c07   Wolfgang Denk   mkimage: add "-V"...
13
  #include <version.h>
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
14
15
  
  static void copy_file(int, const char *, int);
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
16

89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
17
  /* parameters initialized by core will be used by the image type code */
cc7a64447   Simon Glass   mkimage: Make 'pa...
18
  static struct image_tool_params params = {
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
19
20
21
22
23
  	.os = IH_OS_LINUX,
  	.arch = IH_ARCH_PPC,
  	.type = IH_TYPE_KERNEL,
  	.comp = IH_COMP_GZIP,
  	.dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
04387d24a   Wolfgang Denk   mkimage: fix Segm...
24
  	.imagename = "",
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
25
  	.imagename2 = "",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
26
  };
306642251   Simon Glass   mkimage: Allow di...
27
28
29
30
31
32
33
34
35
36
37
  static enum ih_category cur_category;
  
  static int h_compare_category_name(const void *vtype1, const void *vtype2)
  {
  	const int *type1 = vtype1;
  	const int *type2 = vtype2;
  	const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
  	const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
  
  	return strcmp(name1, name2);
  }
f24e10500   Simon Glass   mkimage: Use gene...
38
  static int show_valid_options(enum ih_category category)
306642251   Simon Glass   mkimage: Allow di...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  {
  	int *order;
  	int count;
  	int item;
  	int i;
  
  	count = genimg_get_cat_count(category);
  	order = calloc(count, sizeof(*order));
  	if (!order)
  		return -ENOMEM;
  
  	/* Sort the names in order of short name for easier reading */
  	for (item = 0; item < count; item++)
  		order[item] = item;
  	cur_category = category;
  	qsort(order, count, sizeof(int), h_compare_category_name);
  
  	fprintf(stderr, "
  Invalid %s, supported are:
  ",
  		genimg_get_cat_desc(category));
  	for (i = 0; i < count; i++) {
  		item = order[i];
  		fprintf(stderr, "\t%-15s  %s
  ",
  			genimg_get_cat_short_name(category, item),
  			genimg_get_cat_name(category, item));
  	}
  	fprintf(stderr, "
  ");
0cd82e255   Simon Glass   mkimage: Fix miss...
69
  	free(order);
306642251   Simon Glass   mkimage: Allow di...
70
71
72
  
  	return 0;
  }
153103483   Simon Glass   mkimage: Show an ...
73
  static void usage(const char *msg)
b0a487a4e   Simon Glass   mkimage: Move usa...
74
  {
153103483   Simon Glass   mkimage: Show an ...
75
76
  	fprintf(stderr, "Error: %s
  ", msg);
b0a487a4e   Simon Glass   mkimage: Move usa...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  	fprintf(stderr, "Usage: %s -l image
  "
  			 "          -l ==> list image header information
  ",
  		params.cmdname);
  	fprintf(stderr,
  		"       %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image
  "
  		"          -A ==> set architecture to 'arch'
  "
  		"          -O ==> set operating system to 'os'
  "
  		"          -T ==> set image type to 'type'
  "
  		"          -C ==> set compression type 'comp'
  "
  		"          -a ==> set load address to 'addr' (hex)
  "
  		"          -e ==> set entry point to 'ep' (hex)
  "
  		"          -n ==> set image name to 'name'
  "
  		"          -d ==> use image data from 'datafile'
  "
  		"          -x ==> set XIP (execute in place)
  ",
  		params.cmdname);
  	fprintf(stderr,
0f7c6cdc8   Tomeu Vizoso   mkimage: Allow in...
105
106
  		"       %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image
  "
82bd2f29e   Vagrant Cascadian   Fix spelling of "...
107
108
  		"           <dtb> file is used with -f auto, it may occur multiple times.
  ",
b0a487a4e   Simon Glass   mkimage: Move usa...
109
110
111
112
  		params.cmdname);
  	fprintf(stderr,
  		"          -D => set all options for device tree compiler
  "
0f7c6cdc8   Tomeu Vizoso   mkimage: Allow in...
113
114
115
116
  		"          -f => input filename for FIT source
  "
  		"          -i => input filename for ramdisk file
  ");
b0a487a4e   Simon Glass   mkimage: Move usa...
117
118
  #ifdef CONFIG_FIT_SIGNATURE
  	fprintf(stderr,
f1ca1fdeb   George McCollister   mkimage: Add supp...
119
120
  		"Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]
  "
f8f9107d9   Teddy Reed   mkimage: fit: spl...
121
122
  		"          -E => place data outside of the FIT structure
  "
b0a487a4e   Simon Glass   mkimage: Move usa...
123
124
125
126
127
128
129
130
  		"          -k => set directory containing private keys
  "
  		"          -K => write public keys to this .dtb file
  "
  		"          -c => add comment in signature node
  "
  		"          -F => re-sign existing FIT image
  "
f8f9107d9   Teddy Reed   mkimage: fit: spl...
131
132
  		"          -p => place external data at a static position
  "
f1ca1fdeb   George McCollister   mkimage: Add supp...
133
134
135
136
  		"          -r => mark keys used as 'required' in dtb
  "
  		"          -N => engine to use for signing (pkcs11)
  ");
b0a487a4e   Simon Glass   mkimage: Move usa...
137
138
139
140
141
142
143
144
  #else
  	fprintf(stderr,
  		"Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)
  ");
  #endif
  	fprintf(stderr, "       %s -V ==> print version information and exit
  ",
  		params.cmdname);
79aa33cdb   Baruch Siach   mkimage: fix disp...
145
146
  	fprintf(stderr, "Use '-T list' to see a list of available image types
  ");
b0a487a4e   Simon Glass   mkimage: Move usa...
147
148
149
  
  	exit(EXIT_FAILURE);
  }
fb4cce0f9   Simon Glass   mkimage: Support ...
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  static int add_content(int type, const char *fname)
  {
  	struct content_info *cont;
  
  	cont = calloc(1, sizeof(*cont));
  	if (!cont)
  		return -1;
  	cont->type = type;
  	cont->fname = fname;
  	if (params.content_tail)
  		params.content_tail->next = cont;
  	else
  		params.content_head = cont;
  	params.content_tail = cont;
  
  	return 0;
  }
0b443dee6   Simon Glass   mkimage: Move arg...
167
  static void process_args(int argc, char **argv)
5b1d71372   wdenk   Initial revision
168
  {
a2513e27e   Peter Tyser   mkimage: Fix stri...
169
  	char *ptr;
d505a09c1   Simon Glass   mkimage: Allow a ...
170
171
  	int type = IH_TYPE_INVALID;
  	char *datafile = NULL;
a02221f29   Simon Glass   mkimage: Convert ...
172
173
174
  	int opt;
  
  	while ((opt = getopt(argc, argv,
f1ca1fdeb   George McCollister   mkimage: Add supp...
175
  			     "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVx")) != -1) {
a02221f29   Simon Glass   mkimage: Convert ...
176
  		switch (opt) {
074500814   Simon Glass   mkimage: Sort the...
177
178
179
180
181
182
183
184
  		case 'a':
  			params.addr = strtoull(optarg, &ptr, 16);
  			if (*ptr) {
  				fprintf(stderr, "%s: invalid load address %s
  ",
  					params.cmdname, optarg);
  				exit(EXIT_FAILURE);
  			}
a02221f29   Simon Glass   mkimage: Convert ...
185
186
187
  			break;
  		case 'A':
  			params.arch = genimg_get_arch_id(optarg);
51f03e6a7   Simon Glass   mkimage: Show ite...
188
189
  			if (params.arch < 0) {
  				show_valid_options(IH_ARCH);
153103483   Simon Glass   mkimage: Show an ...
190
  				usage("Invalid architecture");
51f03e6a7   Simon Glass   mkimage: Show ite...
191
  			}
a02221f29   Simon Glass   mkimage: Convert ...
192
  			break;
fb4cce0f9   Simon Glass   mkimage: Support ...
193
  		case 'b':
8edeac86d   Andreas Bießmann   mkimage: fix gene...
194
  			if (add_content(IH_TYPE_FLATDT, optarg)) {
7a439cadc   Andreas Bießmann   mkimage: fix argu...
195
196
197
198
199
  				fprintf(stderr,
  					"%s: Out of memory adding content '%s'",
  					params.cmdname, optarg);
  				exit(EXIT_FAILURE);
  			}
fb4cce0f9   Simon Glass   mkimage: Support ...
200
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
201
202
203
204
205
  		case 'c':
  			params.comment = optarg;
  			break;
  		case 'C':
  			params.comp = genimg_get_comp_id(optarg);
51f03e6a7   Simon Glass   mkimage: Show ite...
206
207
  			if (params.comp < 0) {
  				show_valid_options(IH_COMP);
153103483   Simon Glass   mkimage: Show an ...
208
  				usage("Invalid compression type");
51f03e6a7   Simon Glass   mkimage: Show ite...
209
  			}
a02221f29   Simon Glass   mkimage: Convert ...
210
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
211
212
213
214
  		case 'd':
  			params.datafile = optarg;
  			params.dflag = 1;
  			break;
074500814   Simon Glass   mkimage: Sort the...
215
216
217
  		case 'D':
  			params.dtc = optarg;
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
218
219
220
221
222
223
224
  		case 'e':
  			params.ep = strtoull(optarg, &ptr, 16);
  			if (*ptr) {
  				fprintf(stderr, "%s: invalid entry point %s
  ",
  					params.cmdname, optarg);
  				exit(EXIT_FAILURE);
5b1d71372   wdenk   Initial revision
225
  			}
a02221f29   Simon Glass   mkimage: Convert ...
226
227
  			params.eflag = 1;
  			break;
722ebc8f8   Simon Glass   mkimage: Support ...
228
229
230
  		case 'E':
  			params.external_data = true;
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
231
  		case 'f':
8e35bb07e   Simon Glass   mkimage: Support ...
232
233
  			datafile = optarg;
  			params.auto_its = !strcmp(datafile, "auto");
a02221f29   Simon Glass   mkimage: Convert ...
234
235
236
237
238
239
240
241
242
  			/* no break */
  		case 'F':
  			/*
  			 * The flattened image tree (FIT) format
  			 * requires a flattened device tree image type
  			 */
  			params.type = IH_TYPE_FLATDT;
  			params.fflag = 1;
  			break;
0f7c6cdc8   Tomeu Vizoso   mkimage: Allow in...
243
244
245
  		case 'i':
  			params.fit_ramdisk = optarg;
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
246
247
248
249
250
251
  		case 'k':
  			params.keydir = optarg;
  			break;
  		case 'K':
  			params.keydest = optarg;
  			break;
074500814   Simon Glass   mkimage: Sort the...
252
253
254
  		case 'l':
  			params.lflag = 1;
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
255
256
257
  		case 'n':
  			params.imagename = optarg;
  			break;
f1ca1fdeb   George McCollister   mkimage: Add supp...
258
259
260
  		case 'N':
  			params.engine_id = optarg;
  			break;
074500814   Simon Glass   mkimage: Sort the...
261
262
  		case 'O':
  			params.os = genimg_get_os_id(optarg);
51f03e6a7   Simon Glass   mkimage: Show ite...
263
264
  			if (params.os < 0) {
  				show_valid_options(IH_OS);
153103483   Simon Glass   mkimage: Show an ...
265
  				usage("Invalid operating system");
51f03e6a7   Simon Glass   mkimage: Show ite...
266
  			}
074500814   Simon Glass   mkimage: Sort the...
267
  			break;
f8f9107d9   Teddy Reed   mkimage: fit: spl...
268
269
270
271
272
273
274
275
  		case 'p':
  			params.external_offset = strtoull(optarg, &ptr, 16);
  			if (*ptr) {
  				fprintf(stderr, "%s: invalid offset size %s
  ",
  					params.cmdname, optarg);
  				exit(EXIT_FAILURE);
  			}
b6fefa76d   Teddy Reed   mkimage: fix miss...
276
  			break;
bd6e14209   Simon Glass   mkimage: Add a qu...
277
278
279
  		case 'q':
  			params.quiet = 1;
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
280
281
282
283
284
285
286
287
288
289
290
291
292
  		case 'r':
  			params.require_keys = 1;
  			break;
  		case 'R':
  			/*
  			 * This entry is for the second configuration
  			 * file, if only one is not enough.
  			 */
  			params.imagename2 = optarg;
  			break;
  		case 's':
  			params.skipcpy = 1;
  			break;
074500814   Simon Glass   mkimage: Sort the...
293
  		case 'T':
79aa33cdb   Baruch Siach   mkimage: fix disp...
294
295
296
297
  			if (strcmp(optarg, "list") == 0) {
  				show_valid_options(IH_TYPE);
  				exit(EXIT_SUCCESS);
  			}
d505a09c1   Simon Glass   mkimage: Allow a ...
298
299
  			type = genimg_get_type_id(optarg);
  			if (type < 0) {
f24e10500   Simon Glass   mkimage: Use gene...
300
  				show_valid_options(IH_TYPE);
153103483   Simon Glass   mkimage: Show an ...
301
  				usage("Invalid image type");
074500814   Simon Glass   mkimage: Sort the...
302
303
  			}
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
304
305
306
307
308
309
310
311
312
313
314
  		case 'v':
  			params.vflag++;
  			break;
  		case 'V':
  			printf("mkimage version %s
  ", PLAIN_VERSION);
  			exit(EXIT_SUCCESS);
  		case 'x':
  			params.xflag++;
  			break;
  		default:
153103483   Simon Glass   mkimage: Show an ...
315
  			usage("Invalid option");
5b1d71372   wdenk   Initial revision
316
  		}
5b1d71372   wdenk   Initial revision
317
  	}
8edeac86d   Andreas Bießmann   mkimage: fix gene...
318
319
  	/* The last parameter is expected to be the imagefile */
  	if (optind < argc)
7a439cadc   Andreas Bießmann   mkimage: fix argu...
320
  		params.imagefile = argv[optind];
d505a09c1   Simon Glass   mkimage: Allow a ...
321
322
323
324
325
326
  	/*
  	 * For auto-generated FIT images we need to know the image type to put
  	 * in the FIT, which is separate from the file's image type (which
  	 * will always be IH_TYPE_FLATDT in this case).
  	 */
  	if (params.type == IH_TYPE_FLATDT) {
20deaddd4   Simon Glass   mkimage: Honour t...
327
  		params.fit_image_type = type ? type : IH_TYPE_KERNEL;
3c23c0fea   Simon Glass   mkimage: Explain ...
328
  		/* For auto_its, datafile is always 'auto' */
8e35bb07e   Simon Glass   mkimage: Support ...
329
330
  		if (!params.auto_its)
  			params.datafile = datafile;
e324a9253   Simon Glass   mkimage: Require ...
331
332
  		else if (!params.datafile)
  			usage("Missing data file for auto-FIT (use -d)");
d505a09c1   Simon Glass   mkimage: Allow a ...
333
334
335
336
337
  	} else if (type != IH_TYPE_INVALID) {
  		params.type = type;
  	}
  
  	if (!params.imagefile)
153103483   Simon Glass   mkimage: Show an ...
338
  		usage("Missing output filename");
0b443dee6   Simon Glass   mkimage: Move arg...
339
  }
0b443dee6   Simon Glass   mkimage: Move arg...
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  int main(int argc, char **argv)
  {
  	int ifd = -1;
  	struct stat sbuf;
  	char *ptr;
  	int retval = 0;
  	struct image_type_params *tparams = NULL;
  	int pad_len = 0;
  	int dfd;
  
  	params.cmdname = *argv;
  	params.addr = 0;
  	params.ep = 0;
  
  	process_args(argc, argv);
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
355
356
  
  	/* set tparams as per input type_id */
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
357
  	tparams = imagetool_get_type(params.type);
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
358
359
360
361
362
363
  	if (tparams == NULL) {
  		fprintf (stderr, "%s: unsupported type %s
  ",
  			params.cmdname, genimg_get_type_name(params.type));
  		exit (EXIT_FAILURE);
  	}
5b1d71372   wdenk   Initial revision
364

89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
365
366
367
368
369
370
  	/*
  	 * check the passed arguments parameters meets the requirements
  	 * as per image type to be generated/listed
  	 */
  	if (tparams->check_params)
  		if (tparams->check_params (&params))
153103483   Simon Glass   mkimage: Show an ...
371
  			usage("Bad parameters for image type");
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
372
373
374
  
  	if (!params.eflag) {
  		params.ep = params.addr;
5b1d71372   wdenk   Initial revision
375
  		/* If XIP, entry point must be after the U-Boot header */
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
376
377
  		if (params.xflag)
  			params.ep += tparams->header_size;
5b1d71372   wdenk   Initial revision
378
  	}
c81296c16   Peter Tyser   tools/mkimage: Pr...
379
380
381
382
383
384
385
386
387
  	if (params.fflag){
  		if (tparams->fflag_handle)
  			/*
  			 * in some cases, some additional processing needs
  			 * to be done if fflag is defined
  			 *
  			 * For ex. fit_handle_file for Fit file support
  			 */
  			retval = tparams->fflag_handle(&params);
5b1d71372   wdenk   Initial revision
388

c81296c16   Peter Tyser   tools/mkimage: Pr...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
  		if (retval != EXIT_SUCCESS)
  			exit (retval);
  	}
  
  	if (params.lflag || params.fflag) {
  		ifd = open (params.imagefile, O_RDONLY|O_BINARY);
  	} else {
  		ifd = open (params.imagefile,
  			O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
  	}
  
  	if (ifd < 0) {
  		fprintf (stderr, "%s: Can't open %s: %s
  ",
  			params.cmdname, params.imagefile,
  			strerror(errno));
  		exit (EXIT_FAILURE);
5b1d71372   wdenk   Initial revision
406
  	}
c81296c16   Peter Tyser   tools/mkimage: Pr...
407
  	if (params.lflag || params.fflag) {
5b1d71372   wdenk   Initial revision
408
409
410
411
412
413
  		/*
  		 * list header information of existing image
  		 */
  		if (fstat(ifd, &sbuf) < 0) {
  			fprintf (stderr, "%s: Can't stat %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
414
415
  				params.cmdname, params.imagefile,
  				strerror(errno));
5b1d71372   wdenk   Initial revision
416
417
  			exit (EXIT_FAILURE);
  		}
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
418
  		if ((unsigned)sbuf.st_size < tparams->header_size) {
5b1d71372   wdenk   Initial revision
419
  			fprintf (stderr,
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
420
421
422
  				"%s: Bad size: \"%s\" is not valid image
  ",
  				params.cmdname, params.imagefile);
5b1d71372   wdenk   Initial revision
423
424
  			exit (EXIT_FAILURE);
  		}
fa956fde6   Mike Frysinger   mkimage: make mma...
425
426
  		ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  		if (ptr == MAP_FAILED) {
5b1d71372   wdenk   Initial revision
427
428
  			fprintf (stderr, "%s: Can't read %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
429
430
  				params.cmdname, params.imagefile,
  				strerror(errno));
5b1d71372   wdenk   Initial revision
431
432
  			exit (EXIT_FAILURE);
  		}
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
433
434
435
436
437
438
  		/*
  		 * scan through mkimage registry for all supported image types
  		 * and verify the input image file header for match
  		 * Print the image information for matched image type
  		 * Returns the error code if not matched
  		 */
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
439
440
  		retval = imagetool_verify_print_header(ptr, &sbuf,
  				tparams, &params);
5b1d71372   wdenk   Initial revision
441

5b1d71372   wdenk   Initial revision
442
443
  		(void) munmap((void *)ptr, sbuf.st_size);
  		(void) close (ifd);
f7644c0bf   Prafulla Wadaskar   tools: mkimage : ...
444
  		exit (retval);
5b1d71372   wdenk   Initial revision
445
  	}
346331412   Marek Vasut   mkimage: Fix warn...
446
  	if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
6ae6e1600   Philippe De Swert   mkimage : Fix gen...
447
448
449
450
451
452
453
454
  		dfd = open(params.datafile, O_RDONLY | O_BINARY);
  		if (dfd < 0) {
  			fprintf(stderr, "%s: Can't open %s: %s
  ",
  				params.cmdname, params.datafile,
  				strerror(errno));
  			exit(EXIT_FAILURE);
  		}
92a655c32   Simon Glass   mkimage: Set up a...
455

6ae6e1600   Philippe De Swert   mkimage : Fix gen...
456
457
458
459
460
461
462
  		if (fstat(dfd, &sbuf) < 0) {
  			fprintf(stderr, "%s: Can't stat %s: %s
  ",
  				params.cmdname, params.datafile,
  				strerror(errno));
  			exit(EXIT_FAILURE);
  		}
92a655c32   Simon Glass   mkimage: Set up a...
463

6ae6e1600   Philippe De Swert   mkimage : Fix gen...
464
465
466
  		params.file_size = sbuf.st_size + tparams->header_size;
  		close(dfd);
  	}
92a655c32   Simon Glass   mkimage: Set up a...
467

5b1d71372   wdenk   Initial revision
468
  	/*
f0662105b   Stefano Babic   mkimage: Add vari...
469
470
471
472
  	 * In case there an header with a variable
  	 * length will be added, the corresponding
  	 * function is called. This is responsible to
  	 * allocate memory for the header itself.
5b1d71372   wdenk   Initial revision
473
  	 */
f0662105b   Stefano Babic   mkimage: Add vari...
474
  	if (tparams->vrec_header)
9bac0bb37   Stefano Babic   tools: add variab...
475
  		pad_len = tparams->vrec_header(&params, tparams);
f0662105b   Stefano Babic   mkimage: Add vari...
476
477
  	else
  		memset(tparams->hdr, 0, tparams->header_size);
5b1d71372   wdenk   Initial revision
478

89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
479
480
  	if (write(ifd, tparams->hdr, tparams->header_size)
  					!= tparams->header_size) {
5b1d71372   wdenk   Initial revision
481
482
  		fprintf (stderr, "%s: Write error on %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
483
  			params.cmdname, params.imagefile, strerror(errno));
5b1d71372   wdenk   Initial revision
484
485
  		exit (EXIT_FAILURE);
  	}
d1be8f922   Christian Riesch   mkimage: Fix vari...
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
  	if (!params.skipcpy) {
  		if (params.type == IH_TYPE_MULTI ||
  		    params.type == IH_TYPE_SCRIPT) {
  			char *file = params.datafile;
  			uint32_t size;
  
  			for (;;) {
  				char *sep = NULL;
  
  				if (file) {
  					if ((sep = strchr(file, ':')) != NULL) {
  						*sep = '\0';
  					}
  
  					if (stat (file, &sbuf) < 0) {
  						fprintf (stderr, "%s: Can't stat %s: %s
  ",
  							 params.cmdname, file, strerror(errno));
  						exit (EXIT_FAILURE);
  					}
  					size = cpu_to_uimage (sbuf.st_size);
  				} else {
  					size = 0;
5b1d71372   wdenk   Initial revision
509
  				}
d1be8f922   Christian Riesch   mkimage: Fix vari...
510
511
512
513
514
  				if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
  					fprintf (stderr, "%s: Write error on %s: %s
  ",
  						 params.cmdname, params.imagefile,
  						 strerror(errno));
5b1d71372   wdenk   Initial revision
515
516
  					exit (EXIT_FAILURE);
  				}
5b1d71372   wdenk   Initial revision
517

d1be8f922   Christian Riesch   mkimage: Fix vari...
518
519
520
  				if (!file) {
  					break;
  				}
5b1d71372   wdenk   Initial revision
521

d1be8f922   Christian Riesch   mkimage: Fix vari...
522
523
524
525
526
527
  				if (sep) {
  					*sep = ':';
  					file = sep + 1;
  				} else {
  					file = NULL;
  				}
5b1d71372   wdenk   Initial revision
528
  			}
d1be8f922   Christian Riesch   mkimage: Fix vari...
529
  			file = params.datafile;
5b1d71372   wdenk   Initial revision
530

d1be8f922   Christian Riesch   mkimage: Fix vari...
531
532
533
534
535
536
537
538
539
540
541
  			for (;;) {
  				char *sep = strchr(file, ':');
  				if (sep) {
  					*sep = '\0';
  					copy_file (ifd, file, 1);
  					*sep++ = ':';
  					file = sep;
  				} else {
  					copy_file (ifd, file, 0);
  					break;
  				}
5b1d71372   wdenk   Initial revision
542
  			}
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
543
544
545
  		} else if (params.type == IH_TYPE_PBLIMAGE) {
  			/* PBL has special Image format, implements its' own */
  			pbl_load_uboot(ifd, &params);
d1be8f922   Christian Riesch   mkimage: Fix vari...
546
  		} else {
9bac0bb37   Stefano Babic   tools: add variab...
547
  			copy_file(ifd, params.datafile, pad_len);
5b1d71372   wdenk   Initial revision
548
  		}
d21bd69b6   Sven Ebenfeld   tools: mkimage: a...
549
550
551
552
553
554
555
556
557
558
559
560
561
  		if (params.type == IH_TYPE_FIRMWARE_IVT) {
  			/* Add alignment and IVT */
  			uint32_t aligned_filesize = (params.file_size + 0x1000
  					- 1) & ~(0x1000 - 1);
  			flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
  					params.addr, 0, 0, 0, params.addr
  							+ aligned_filesize
  							- tparams->header_size,
  					params.addr + aligned_filesize
  							- tparams->header_size
  							+ 0x20, 0 };
  			int i = params.file_size;
  			for (; i < aligned_filesize; i++) {
b4e923a80   Sven Ebenfeld   tools: mkimage: f...
562
  				if (write(ifd, (char *) &i, 1) != 1) {
d21bd69b6   Sven Ebenfeld   tools: mkimage: a...
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
  					fprintf(stderr,
  							"%s: Write error on %s: %s
  ",
  							params.cmdname,
  							params.imagefile,
  							strerror(errno));
  					exit(EXIT_FAILURE);
  				}
  			}
  			if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
  					!= sizeof(flash_header_v2_t)) {
  				fprintf(stderr, "%s: Write error on %s: %s
  ",
  						params.cmdname,
  						params.imagefile,
  						strerror(errno));
  				exit(EXIT_FAILURE);
  			}
  		}
5b1d71372   wdenk   Initial revision
582
583
584
  	}
  
  	/* We're a bit of paranoid */
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
585
586
587
  #if defined(_POSIX_SYNCHRONIZED_IO) && \
     !defined(__sun__) && \
     !defined(__FreeBSD__) && \
31cbe80c3   Luka Perkov   mkimage: fix comp...
588
     !defined(__OpenBSD__) && \
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
589
     !defined(__APPLE__)
5b1d71372   wdenk   Initial revision
590
591
592
593
594
595
596
597
  	(void) fdatasync (ifd);
  #else
  	(void) fsync (ifd);
  #endif
  
  	if (fstat(ifd, &sbuf) < 0) {
  		fprintf (stderr, "%s: Can't stat %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
598
  			params.cmdname, params.imagefile, strerror(errno));
5b1d71372   wdenk   Initial revision
599
600
  		exit (EXIT_FAILURE);
  	}
92a655c32   Simon Glass   mkimage: Set up a...
601
  	params.file_size = sbuf.st_size;
5b1d71372   wdenk   Initial revision
602

fa956fde6   Mike Frysinger   mkimage: make mma...
603
604
  	ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
  	if (ptr == MAP_FAILED) {
5b1d71372   wdenk   Initial revision
605
606
  		fprintf (stderr, "%s: Can't map %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
607
  			params.cmdname, params.imagefile, strerror(errno));
5b1d71372   wdenk   Initial revision
608
609
  		exit (EXIT_FAILURE);
  	}
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
610
611
612
613
614
615
616
617
618
  	/* Setup the image header as per input image type*/
  	if (tparams->set_header)
  		tparams->set_header (ptr, &sbuf, ifd, &params);
  	else {
  		fprintf (stderr, "%s: Can't set header for %s: %s
  ",
  			params.cmdname, tparams->name, strerror(errno));
  		exit (EXIT_FAILURE);
  	}
5b1d71372   wdenk   Initial revision
619

89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
620
621
622
623
624
625
626
627
628
  	/* Print the image information by processing image header */
  	if (tparams->print_header)
  		tparams->print_header (ptr);
  	else {
  		fprintf (stderr, "%s: Can't print header for %s: %s
  ",
  			params.cmdname, tparams->name, strerror(errno));
  		exit (EXIT_FAILURE);
  	}
5b1d71372   wdenk   Initial revision
629
630
631
632
  
  	(void) munmap((void *)ptr, sbuf.st_size);
  
  	/* We're a bit of paranoid */
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
633
634
635
  #if defined(_POSIX_SYNCHRONIZED_IO) && \
     !defined(__sun__) && \
     !defined(__FreeBSD__) && \
31cbe80c3   Luka Perkov   mkimage: fix comp...
636
     !defined(__OpenBSD__) && \
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
637
     !defined(__APPLE__)
5b1d71372   wdenk   Initial revision
638
639
640
641
642
643
644
645
  	(void) fdatasync (ifd);
  #else
  	(void) fsync (ifd);
  #endif
  
  	if (close(ifd)) {
  		fprintf (stderr, "%s: Write error on %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
646
  			params.cmdname, params.imagefile, strerror(errno));
5b1d71372   wdenk   Initial revision
647
648
649
650
651
652
653
654
655
656
657
658
659
660
  		exit (EXIT_FAILURE);
  	}
  
  	exit (EXIT_SUCCESS);
  }
  
  static void
  copy_file (int ifd, const char *datafile, int pad)
  {
  	int dfd;
  	struct stat sbuf;
  	unsigned char *ptr;
  	int tail;
  	int zero = 0;
9bac0bb37   Stefano Babic   tools: add variab...
661
  	uint8_t zeros[4096];
5b1d71372   wdenk   Initial revision
662
663
  	int offset = 0;
  	int size;
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
664
  	struct image_type_params *tparams = imagetool_get_type(params.type);
5b1d71372   wdenk   Initial revision
665

9bac0bb37   Stefano Babic   tools: add variab...
666
  	memset(zeros, 0, sizeof(zeros));
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
667
  	if (params.vflag) {
5b1d71372   wdenk   Initial revision
668
669
670
  		fprintf (stderr, "Adding Image %s
  ", datafile);
  	}
ef1464cc0   wdenk   * Patch by Anders...
671
  	if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
5b1d71372   wdenk   Initial revision
672
673
  		fprintf (stderr, "%s: Can't open %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
674
  			params.cmdname, datafile, strerror(errno));
5b1d71372   wdenk   Initial revision
675
676
677
678
679
680
  		exit (EXIT_FAILURE);
  	}
  
  	if (fstat(dfd, &sbuf) < 0) {
  		fprintf (stderr, "%s: Can't stat %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
681
  			params.cmdname, datafile, strerror(errno));
5b1d71372   wdenk   Initial revision
682
683
  		exit (EXIT_FAILURE);
  	}
fa956fde6   Mike Frysinger   mkimage: make mma...
684
685
  	ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  	if (ptr == MAP_FAILED) {
5b1d71372   wdenk   Initial revision
686
687
  		fprintf (stderr, "%s: Can't read %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
688
  			params.cmdname, datafile, strerror(errno));
5b1d71372   wdenk   Initial revision
689
690
  		exit (EXIT_FAILURE);
  	}
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
691
  	if (params.xflag) {
5b1d71372   wdenk   Initial revision
692
693
694
695
696
697
  		unsigned char *p = NULL;
  		/*
  		 * XIP: do not append the image_header_t at the
  		 * beginning of the file, but consume the space
  		 * reserved for it.
  		 */
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
698
  		if ((unsigned)sbuf.st_size < tparams->header_size) {
5b1d71372   wdenk   Initial revision
699
700
701
  			fprintf (stderr,
  				"%s: Bad size: \"%s\" is too small for XIP
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
702
  				params.cmdname, datafile);
5b1d71372   wdenk   Initial revision
703
704
  			exit (EXIT_FAILURE);
  		}
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
705
  		for (p = ptr; p < ptr + tparams->header_size; p++) {
5b1d71372   wdenk   Initial revision
706
707
708
709
  			if ( *p != 0xff ) {
  				fprintf (stderr,
  					"%s: Bad file: \"%s\" has invalid buffer for XIP
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
710
  					params.cmdname, datafile);
5b1d71372   wdenk   Initial revision
711
712
713
  				exit (EXIT_FAILURE);
  			}
  		}
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
714
  		offset = tparams->header_size;
5b1d71372   wdenk   Initial revision
715
716
717
718
719
720
  	}
  
  	size = sbuf.st_size - offset;
  	if (write(ifd, ptr + offset, size) != size) {
  		fprintf (stderr, "%s: Write error on %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
721
  			params.cmdname, params.imagefile, strerror(errno));
5b1d71372   wdenk   Initial revision
722
723
  		exit (EXIT_FAILURE);
  	}
9bac0bb37   Stefano Babic   tools: add variab...
724
725
  	tail = size % 4;
  	if ((pad == 1) && (tail != 0)) {
5b1d71372   wdenk   Initial revision
726
727
728
729
  
  		if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
  			fprintf (stderr, "%s: Write error on %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
730
731
  				params.cmdname, params.imagefile,
  				strerror(errno));
5b1d71372   wdenk   Initial revision
732
733
  			exit (EXIT_FAILURE);
  		}
9bac0bb37   Stefano Babic   tools: add variab...
734
  	} else if (pad > 1) {
424b86ae5   Simon Glass   mkimage: Allow pa...
735
736
737
738
739
740
741
742
743
744
745
746
747
  		while (pad > 0) {
  			int todo = sizeof(zeros);
  
  			if (todo > pad)
  				todo = pad;
  			if (write(ifd, (char *)&zeros, todo) != todo) {
  				fprintf(stderr, "%s: Write error on %s: %s
  ",
  					params.cmdname, params.imagefile,
  					strerror(errno));
  				exit(EXIT_FAILURE);
  			}
  			pad -= todo;
9bac0bb37   Stefano Babic   tools: add variab...
748
  		}
5b1d71372   wdenk   Initial revision
749
750
751
752
753
  	}
  
  	(void) munmap((void *)ptr, sbuf.st_size);
  	(void) close (dfd);
  }