Blame view

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

89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
16
  /* parameters initialized by core will be used by the image type code */
cc7a64447   Simon Glass   mkimage: Make 'pa...
17
  static struct image_tool_params params = {
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
18
19
20
21
22
  	.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...
23
  	.imagename = "",
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
24
  	.imagename2 = "",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
25
  };
306642251   Simon Glass   mkimage: Allow di...
26
27
28
29
30
31
32
33
34
35
36
  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...
37
  static int show_valid_options(enum ih_category category)
306642251   Simon Glass   mkimage: Allow di...
38
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
  {
  	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...
68
  	free(order);
306642251   Simon Glass   mkimage: Allow di...
69
70
71
  
  	return 0;
  }
153103483   Simon Glass   mkimage: Show an ...
72
  static void usage(const char *msg)
b0a487a4e   Simon Glass   mkimage: Move usa...
73
  {
153103483   Simon Glass   mkimage: Show an ...
74
75
  	fprintf(stderr, "Error: %s
  ", msg);
b0a487a4e   Simon Glass   mkimage: Move usa...
76
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
  	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...
104
105
  		"       %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 "...
106
107
  		"           <dtb> file is used with -f auto, it may occur multiple times.
  ",
b0a487a4e   Simon Glass   mkimage: Move usa...
108
109
110
111
  		params.cmdname);
  	fprintf(stderr,
  		"          -D => set all options for device tree compiler
  "
0f7c6cdc8   Tomeu Vizoso   mkimage: Allow in...
112
113
114
115
  		"          -f => input filename for FIT source
  "
  		"          -i => input filename for ramdisk file
  ");
b0a487a4e   Simon Glass   mkimage: Move usa...
116
117
  #ifdef CONFIG_FIT_SIGNATURE
  	fprintf(stderr,
f1ca1fdeb   George McCollister   mkimage: Add supp...
118
119
  		"Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]
  "
f8f9107d9   Teddy Reed   mkimage: fit: spl...
120
121
  		"          -E => place data outside of the FIT structure
  "
b0a487a4e   Simon Glass   mkimage: Move usa...
122
123
124
125
126
127
128
129
  		"          -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...
130
131
  		"          -p => place external data at a static position
  "
f1ca1fdeb   George McCollister   mkimage: Add supp...
132
133
134
135
  		"          -r => mark keys used as 'required' in dtb
  "
  		"          -N => engine to use for signing (pkcs11)
  ");
b0a487a4e   Simon Glass   mkimage: Move usa...
136
137
138
139
140
141
142
143
  #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...
144
145
  	fprintf(stderr, "Use '-T list' to see a list of available image types
  ");
b0a487a4e   Simon Glass   mkimage: Move usa...
146
147
148
  
  	exit(EXIT_FAILURE);
  }
fb4cce0f9   Simon Glass   mkimage: Support ...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  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...
166
  static void process_args(int argc, char **argv)
5b1d71372   wdenk   Initial revision
167
  {
a2513e27e   Peter Tyser   mkimage: Fix stri...
168
  	char *ptr;
d505a09c1   Simon Glass   mkimage: Allow a ...
169
170
  	int type = IH_TYPE_INVALID;
  	char *datafile = NULL;
a02221f29   Simon Glass   mkimage: Convert ...
171
172
173
  	int opt;
  
  	while ((opt = getopt(argc, argv,
f1ca1fdeb   George McCollister   mkimage: Add supp...
174
  			     "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 ...
175
  		switch (opt) {
074500814   Simon Glass   mkimage: Sort the...
176
177
178
179
180
181
182
183
  		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 ...
184
185
186
  			break;
  		case 'A':
  			params.arch = genimg_get_arch_id(optarg);
51f03e6a7   Simon Glass   mkimage: Show ite...
187
188
  			if (params.arch < 0) {
  				show_valid_options(IH_ARCH);
153103483   Simon Glass   mkimage: Show an ...
189
  				usage("Invalid architecture");
51f03e6a7   Simon Glass   mkimage: Show ite...
190
  			}
a02221f29   Simon Glass   mkimage: Convert ...
191
  			break;
fb4cce0f9   Simon Glass   mkimage: Support ...
192
  		case 'b':
8edeac86d   Andreas Bießmann   mkimage: fix gene...
193
  			if (add_content(IH_TYPE_FLATDT, optarg)) {
7a439cadc   Andreas Bießmann   mkimage: fix argu...
194
195
196
197
198
  				fprintf(stderr,
  					"%s: Out of memory adding content '%s'",
  					params.cmdname, optarg);
  				exit(EXIT_FAILURE);
  			}
fb4cce0f9   Simon Glass   mkimage: Support ...
199
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
200
201
202
203
204
  		case 'c':
  			params.comment = optarg;
  			break;
  		case 'C':
  			params.comp = genimg_get_comp_id(optarg);
51f03e6a7   Simon Glass   mkimage: Show ite...
205
206
  			if (params.comp < 0) {
  				show_valid_options(IH_COMP);
153103483   Simon Glass   mkimage: Show an ...
207
  				usage("Invalid compression type");
51f03e6a7   Simon Glass   mkimage: Show ite...
208
  			}
a02221f29   Simon Glass   mkimage: Convert ...
209
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
210
211
212
213
  		case 'd':
  			params.datafile = optarg;
  			params.dflag = 1;
  			break;
074500814   Simon Glass   mkimage: Sort the...
214
215
216
  		case 'D':
  			params.dtc = optarg;
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
217
218
219
220
221
222
223
  		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
224
  			}
a02221f29   Simon Glass   mkimage: Convert ...
225
226
  			params.eflag = 1;
  			break;
722ebc8f8   Simon Glass   mkimage: Support ...
227
228
229
  		case 'E':
  			params.external_data = true;
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
230
  		case 'f':
8e35bb07e   Simon Glass   mkimage: Support ...
231
232
  			datafile = optarg;
  			params.auto_its = !strcmp(datafile, "auto");
a02221f29   Simon Glass   mkimage: Convert ...
233
234
235
236
237
238
239
240
241
  			/* 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...
242
243
244
  		case 'i':
  			params.fit_ramdisk = optarg;
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
245
246
247
248
249
250
  		case 'k':
  			params.keydir = optarg;
  			break;
  		case 'K':
  			params.keydest = optarg;
  			break;
074500814   Simon Glass   mkimage: Sort the...
251
252
253
  		case 'l':
  			params.lflag = 1;
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
254
255
256
  		case 'n':
  			params.imagename = optarg;
  			break;
f1ca1fdeb   George McCollister   mkimage: Add supp...
257
258
259
  		case 'N':
  			params.engine_id = optarg;
  			break;
074500814   Simon Glass   mkimage: Sort the...
260
261
  		case 'O':
  			params.os = genimg_get_os_id(optarg);
51f03e6a7   Simon Glass   mkimage: Show ite...
262
263
  			if (params.os < 0) {
  				show_valid_options(IH_OS);
153103483   Simon Glass   mkimage: Show an ...
264
  				usage("Invalid operating system");
51f03e6a7   Simon Glass   mkimage: Show ite...
265
  			}
074500814   Simon Glass   mkimage: Sort the...
266
  			break;
f8f9107d9   Teddy Reed   mkimage: fit: spl...
267
268
269
270
271
272
273
274
  		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...
275
  			break;
bd6e14209   Simon Glass   mkimage: Add a qu...
276
277
278
  		case 'q':
  			params.quiet = 1;
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
279
280
281
282
283
284
285
286
287
288
289
290
291
  		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...
292
  		case 'T':
79aa33cdb   Baruch Siach   mkimage: fix disp...
293
294
295
296
  			if (strcmp(optarg, "list") == 0) {
  				show_valid_options(IH_TYPE);
  				exit(EXIT_SUCCESS);
  			}
d505a09c1   Simon Glass   mkimage: Allow a ...
297
298
  			type = genimg_get_type_id(optarg);
  			if (type < 0) {
f24e10500   Simon Glass   mkimage: Use gene...
299
  				show_valid_options(IH_TYPE);
153103483   Simon Glass   mkimage: Show an ...
300
  				usage("Invalid image type");
074500814   Simon Glass   mkimage: Sort the...
301
302
  			}
  			break;
a02221f29   Simon Glass   mkimage: Convert ...
303
304
305
306
307
308
309
310
311
312
313
  		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 ...
314
  			usage("Invalid option");
5b1d71372   wdenk   Initial revision
315
  		}
5b1d71372   wdenk   Initial revision
316
  	}
8edeac86d   Andreas Bießmann   mkimage: fix gene...
317
318
  	/* The last parameter is expected to be the imagefile */
  	if (optind < argc)
7a439cadc   Andreas Bießmann   mkimage: fix argu...
319
  		params.imagefile = argv[optind];
d505a09c1   Simon Glass   mkimage: Allow a ...
320
321
322
323
324
325
  	/*
  	 * 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...
326
  		params.fit_image_type = type ? type : IH_TYPE_KERNEL;
3c23c0fea   Simon Glass   mkimage: Explain ...
327
  		/* For auto_its, datafile is always 'auto' */
8e35bb07e   Simon Glass   mkimage: Support ...
328
329
  		if (!params.auto_its)
  			params.datafile = datafile;
e324a9253   Simon Glass   mkimage: Require ...
330
331
  		else if (!params.datafile)
  			usage("Missing data file for auto-FIT (use -d)");
d505a09c1   Simon Glass   mkimage: Allow a ...
332
  	} else if (type != IH_TYPE_INVALID) {
8c84287a0   Alex Kiernan   tools: mkimage: C...
333
334
  		if (type == IH_TYPE_SCRIPT && !params.datafile)
  			usage("Missing data file for script (use -d)");
d505a09c1   Simon Glass   mkimage: Allow a ...
335
336
337
338
  		params.type = type;
  	}
  
  	if (!params.imagefile)
153103483   Simon Glass   mkimage: Show an ...
339
  		usage("Missing output filename");
0b443dee6   Simon Glass   mkimage: Move arg...
340
  }
0b443dee6   Simon Glass   mkimage: Move arg...
341
342
343
344
345
346
347
348
349
  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;
8961c8ad2   Mark Tomlinson   tools: mkimage: E...
350
  	size_t map_len;
0b443dee6   Simon Glass   mkimage: Move arg...
351
352
353
354
355
356
  
  	params.cmdname = *argv;
  	params.addr = 0;
  	params.ep = 0;
  
  	process_args(argc, argv);
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
357
358
  
  	/* set tparams as per input type_id */
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
359
  	tparams = imagetool_get_type(params.type);
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
360
361
362
363
364
365
  	if (tparams == NULL) {
  		fprintf (stderr, "%s: unsupported type %s
  ",
  			params.cmdname, genimg_get_type_name(params.type));
  		exit (EXIT_FAILURE);
  	}
5b1d71372   wdenk   Initial revision
366

89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
367
368
369
370
371
372
  	/*
  	 * 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 ...
373
  			usage("Bad parameters for image type");
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
374
375
376
  
  	if (!params.eflag) {
  		params.ep = params.addr;
5b1d71372   wdenk   Initial revision
377
  		/* If XIP, entry point must be after the U-Boot header */
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
378
379
  		if (params.xflag)
  			params.ep += tparams->header_size;
5b1d71372   wdenk   Initial revision
380
  	}
c81296c16   Peter Tyser   tools/mkimage: Pr...
381
382
383
384
385
386
387
388
389
  	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
390

c81296c16   Peter Tyser   tools/mkimage: Pr...
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
  		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
408
  	}
c81296c16   Peter Tyser   tools/mkimage: Pr...
409
  	if (params.lflag || params.fflag) {
5b1d71372   wdenk   Initial revision
410
411
412
413
414
415
  		/*
  		 * 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...
416
417
  				params.cmdname, params.imagefile,
  				strerror(errno));
5b1d71372   wdenk   Initial revision
418
419
  			exit (EXIT_FAILURE);
  		}
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
420
  		if ((unsigned)sbuf.st_size < tparams->header_size) {
5b1d71372   wdenk   Initial revision
421
  			fprintf (stderr,
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
422
423
424
  				"%s: Bad size: \"%s\" is not valid image
  ",
  				params.cmdname, params.imagefile);
5b1d71372   wdenk   Initial revision
425
426
  			exit (EXIT_FAILURE);
  		}
fa956fde6   Mike Frysinger   mkimage: make mma...
427
428
  		ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  		if (ptr == MAP_FAILED) {
5b1d71372   wdenk   Initial revision
429
430
  			fprintf (stderr, "%s: Can't read %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
431
432
  				params.cmdname, params.imagefile,
  				strerror(errno));
5b1d71372   wdenk   Initial revision
433
434
  			exit (EXIT_FAILURE);
  		}
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
435
436
437
438
439
440
  		/*
  		 * 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
  		 */
d32aa3cae   Jordan Hand   fdt: Fix FIT head...
441
  		retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
442
  				tparams, &params);
5b1d71372   wdenk   Initial revision
443

5b1d71372   wdenk   Initial revision
444
445
  		(void) munmap((void *)ptr, sbuf.st_size);
  		(void) close (ifd);
f7644c0bf   Prafulla Wadaskar   tools: mkimage : ...
446
  		exit (retval);
5b1d71372   wdenk   Initial revision
447
  	}
346331412   Marek Vasut   mkimage: Fix warn...
448
  	if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
6ae6e1600   Philippe De Swert   mkimage : Fix gen...
449
450
451
452
453
454
455
456
  		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...
457

6ae6e1600   Philippe De Swert   mkimage : Fix gen...
458
459
460
461
462
463
464
  		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...
465

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

5b1d71372   wdenk   Initial revision
470
  	/*
f0662105b   Stefano Babic   mkimage: Add vari...
471
472
473
474
  	 * 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
475
  	 */
f0662105b   Stefano Babic   mkimage: Add vari...
476
  	if (tparams->vrec_header)
9bac0bb37   Stefano Babic   tools: add variab...
477
  		pad_len = tparams->vrec_header(&params, tparams);
f0662105b   Stefano Babic   mkimage: Add vari...
478
479
  	else
  		memset(tparams->hdr, 0, tparams->header_size);
5b1d71372   wdenk   Initial revision
480

89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
481
482
  	if (write(ifd, tparams->hdr, tparams->header_size)
  					!= tparams->header_size) {
5b1d71372   wdenk   Initial revision
483
484
  		fprintf (stderr, "%s: Write error on %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
485
  			params.cmdname, params.imagefile, strerror(errno));
5b1d71372   wdenk   Initial revision
486
487
  		exit (EXIT_FAILURE);
  	}
d1be8f922   Christian Riesch   mkimage: Fix vari...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
  	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
511
  				}
d1be8f922   Christian Riesch   mkimage: Fix vari...
512
513
514
515
516
  				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
517
518
  					exit (EXIT_FAILURE);
  				}
5b1d71372   wdenk   Initial revision
519

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

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

d1be8f922   Christian Riesch   mkimage: Fix vari...
533
534
535
536
537
538
539
540
541
542
543
  			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
544
  			}
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
545
546
547
  		} else if (params.type == IH_TYPE_PBLIMAGE) {
  			/* PBL has special Image format, implements its' own */
  			pbl_load_uboot(ifd, &params);
6915dcf35   Alexander Graf   tools: zynqmpimag...
548
549
550
551
552
553
554
  		} else if (params.type == IH_TYPE_ZYNQMPBIF) {
  			/* Image file is meta, walk through actual targets */
  			int ret;
  
  			ret = zynqmpbif_copy_image(ifd, &params);
  			if (ret)
  				return ret;
a2b96ece5   Peng Fan   tools: add i.MX8/...
555
556
557
558
559
560
561
  		} else if (params.type == IH_TYPE_IMX8IMAGE) {
  			/* i.MX8/8X has special Image format */
  			int ret;
  
  			ret = imx8image_copy_image(ifd, &params);
  			if (ret)
  				return ret;
6609c2663   Peng Fan   tools: add i.MX8M...
562
563
564
565
566
567
568
  		} else if (params.type == IH_TYPE_IMX8MIMAGE) {
  			/* i.MX8M has special Image format */
  			int ret;
  
  			ret = imx8mimage_copy_image(ifd, &params);
  			if (ret)
  				return ret;
d1be8f922   Christian Riesch   mkimage: Fix vari...
569
  		} else {
9bac0bb37   Stefano Babic   tools: add variab...
570
  			copy_file(ifd, params.datafile, pad_len);
5b1d71372   wdenk   Initial revision
571
  		}
d21bd69b6   Sven Ebenfeld   tools: mkimage: a...
572
573
574
575
576
577
578
579
580
581
582
583
584
  		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...
585
  				if (write(ifd, (char *) &i, 1) != 1) {
d21bd69b6   Sven Ebenfeld   tools: mkimage: a...
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
  					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
605
606
607
  	}
  
  	/* We're a bit of paranoid */
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
608
609
610
  #if defined(_POSIX_SYNCHRONIZED_IO) && \
     !defined(__sun__) && \
     !defined(__FreeBSD__) && \
31cbe80c3   Luka Perkov   mkimage: fix comp...
611
     !defined(__OpenBSD__) && \
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
612
     !defined(__APPLE__)
5b1d71372   wdenk   Initial revision
613
614
615
616
617
618
619
620
  	(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...
621
  			params.cmdname, params.imagefile, strerror(errno));
5b1d71372   wdenk   Initial revision
622
623
  		exit (EXIT_FAILURE);
  	}
92a655c32   Simon Glass   mkimage: Set up a...
624
  	params.file_size = sbuf.st_size;
5b1d71372   wdenk   Initial revision
625

8961c8ad2   Mark Tomlinson   tools: mkimage: E...
626
627
  	map_len = sbuf.st_size;
  	ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
fa956fde6   Mike Frysinger   mkimage: make mma...
628
  	if (ptr == MAP_FAILED) {
5b1d71372   wdenk   Initial revision
629
630
  		fprintf (stderr, "%s: Can't map %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
631
  			params.cmdname, params.imagefile, strerror(errno));
5b1d71372   wdenk   Initial revision
632
633
  		exit (EXIT_FAILURE);
  	}
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
634
635
636
637
638
639
640
641
642
  	/* 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
643

89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
644
645
646
647
  	/* Print the image information by processing image header */
  	if (tparams->print_header)
  		tparams->print_header (ptr);
  	else {
004d00914   Guillaume GARDET   mkimage: do not f...
648
649
650
  		fprintf (stderr, "%s: Can't print header for %s
  ",
  			params.cmdname, tparams->name);
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
651
  	}
5b1d71372   wdenk   Initial revision
652

8961c8ad2   Mark Tomlinson   tools: mkimage: E...
653
  	(void)munmap((void *)ptr, map_len);
5b1d71372   wdenk   Initial revision
654
655
  
  	/* We're a bit of paranoid */
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
656
657
658
  #if defined(_POSIX_SYNCHRONIZED_IO) && \
     !defined(__sun__) && \
     !defined(__FreeBSD__) && \
31cbe80c3   Luka Perkov   mkimage: fix comp...
659
     !defined(__OpenBSD__) && \
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
660
     !defined(__APPLE__)
5b1d71372   wdenk   Initial revision
661
662
663
664
665
666
667
668
  	(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...
669
  			params.cmdname, params.imagefile, strerror(errno));
5b1d71372   wdenk   Initial revision
670
671
672
673
674
675
676
677
678
679
680
681
682
683
  		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...
684
  	uint8_t zeros[4096];
5b1d71372   wdenk   Initial revision
685
686
  	int offset = 0;
  	int size;
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
687
  	struct image_type_params *tparams = imagetool_get_type(params.type);
5b1d71372   wdenk   Initial revision
688

9bac0bb37   Stefano Babic   tools: add variab...
689
  	memset(zeros, 0, sizeof(zeros));
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
690
  	if (params.vflag) {
5b1d71372   wdenk   Initial revision
691
692
693
  		fprintf (stderr, "Adding Image %s
  ", datafile);
  	}
ef1464cc0   wdenk   * Patch by Anders...
694
  	if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
5b1d71372   wdenk   Initial revision
695
696
  		fprintf (stderr, "%s: Can't open %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
697
  			params.cmdname, datafile, strerror(errno));
5b1d71372   wdenk   Initial revision
698
699
700
701
702
703
  		exit (EXIT_FAILURE);
  	}
  
  	if (fstat(dfd, &sbuf) < 0) {
  		fprintf (stderr, "%s: Can't stat %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
704
  			params.cmdname, datafile, strerror(errno));
5b1d71372   wdenk   Initial revision
705
706
  		exit (EXIT_FAILURE);
  	}
fa956fde6   Mike Frysinger   mkimage: make mma...
707
708
  	ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  	if (ptr == MAP_FAILED) {
5b1d71372   wdenk   Initial revision
709
710
  		fprintf (stderr, "%s: Can't read %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
711
  			params.cmdname, datafile, strerror(errno));
5b1d71372   wdenk   Initial revision
712
713
  		exit (EXIT_FAILURE);
  	}
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
714
  	if (params.xflag) {
5b1d71372   wdenk   Initial revision
715
716
717
718
719
720
  		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...
721
  		if ((unsigned)sbuf.st_size < tparams->header_size) {
5b1d71372   wdenk   Initial revision
722
723
724
  			fprintf (stderr,
  				"%s: Bad size: \"%s\" is too small for XIP
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
725
  				params.cmdname, datafile);
5b1d71372   wdenk   Initial revision
726
727
  			exit (EXIT_FAILURE);
  		}
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
728
  		for (p = ptr; p < ptr + tparams->header_size; p++) {
5b1d71372   wdenk   Initial revision
729
730
731
732
  			if ( *p != 0xff ) {
  				fprintf (stderr,
  					"%s: Bad file: \"%s\" has invalid buffer for XIP
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
733
  					params.cmdname, datafile);
5b1d71372   wdenk   Initial revision
734
735
736
  				exit (EXIT_FAILURE);
  			}
  		}
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
737
  		offset = tparams->header_size;
5b1d71372   wdenk   Initial revision
738
739
740
741
742
743
  	}
  
  	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...
744
  			params.cmdname, params.imagefile, strerror(errno));
5b1d71372   wdenk   Initial revision
745
746
  		exit (EXIT_FAILURE);
  	}
9bac0bb37   Stefano Babic   tools: add variab...
747
748
  	tail = size % 4;
  	if ((pad == 1) && (tail != 0)) {
5b1d71372   wdenk   Initial revision
749
750
751
752
  
  		if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
  			fprintf (stderr, "%s: Write error on %s: %s
  ",
89a4d6b12   Prafulla Wadaskar   tools: mkimage: s...
753
754
  				params.cmdname, params.imagefile,
  				strerror(errno));
5b1d71372   wdenk   Initial revision
755
756
  			exit (EXIT_FAILURE);
  		}
9bac0bb37   Stefano Babic   tools: add variab...
757
  	} else if (pad > 1) {
424b86ae5   Simon Glass   mkimage: Allow pa...
758
759
760
761
762
763
764
765
766
767
768
769
770
  		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...
771
  		}
5b1d71372   wdenk   Initial revision
772
773
774
775
776
  	}
  
  	(void) munmap((void *)ptr, sbuf.st_size);
  	(void) close (dfd);
  }