xref: /OK3568_Linux_fs/u-boot/tools/mkimage.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * (C) Copyright 2008 Semihalf
3  *
4  * (C) Copyright 2000-2009
5  * DENX Software Engineering
6  * Wolfgang Denk, wd@denx.de
7  *
8  * SPDX-License-Identifier:	GPL-2.0+
9  */
10 
11 #include "mkimage.h"
12 #include "imximage.h"
13 #include <image.h>
14 #include <version.h>
15 
16 static void copy_file(int, const char *, int);
17 
18 /* parameters initialized by core will be used by the image type code */
19 static struct image_tool_params params = {
20 	.os = IH_OS_LINUX,
21 	.arch = IH_ARCH_PPC,
22 	.type = IH_TYPE_KERNEL,
23 	.comp = IH_COMP_GZIP,
24 	.dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
25 	.imagename = "",
26 	.imagename2 = "",
27 };
28 
29 static enum ih_category cur_category;
30 
h_compare_category_name(const void * vtype1,const void * vtype2)31 static int h_compare_category_name(const void *vtype1, const void *vtype2)
32 {
33 	const int *type1 = vtype1;
34 	const int *type2 = vtype2;
35 	const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
36 	const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
37 
38 	return strcmp(name1, name2);
39 }
40 
show_valid_options(enum ih_category category)41 static int show_valid_options(enum ih_category category)
42 {
43 	int *order;
44 	int count;
45 	int item;
46 	int i;
47 
48 	count = genimg_get_cat_count(category);
49 	order = calloc(count, sizeof(*order));
50 	if (!order)
51 		return -ENOMEM;
52 
53 	/* Sort the names in order of short name for easier reading */
54 	for (item = 0; item < count; item++)
55 		order[item] = item;
56 	cur_category = category;
57 	qsort(order, count, sizeof(int), h_compare_category_name);
58 
59 	fprintf(stderr, "\nInvalid %s, supported are:\n",
60 		genimg_get_cat_desc(category));
61 	for (i = 0; i < count; i++) {
62 		item = order[i];
63 		fprintf(stderr, "\t%-15s  %s\n",
64 			genimg_get_cat_short_name(category, item),
65 			genimg_get_cat_name(category, item));
66 	}
67 	fprintf(stderr, "\n");
68 	free(order);
69 
70 	return 0;
71 }
72 
usage(const char * msg)73 static void usage(const char *msg)
74 {
75 	fprintf(stderr, "Error: %s\n", msg);
76 	fprintf(stderr, "Usage: %s -l image\n"
77 			 "          -l ==> list image header information\n",
78 		params.cmdname);
79 	fprintf(stderr,
80 		"       %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
81 		"          -A ==> set architecture to 'arch'\n"
82 		"          -O ==> set operating system to 'os'\n"
83 		"          -T ==> set image type to 'type'\n"
84 		"          -C ==> set compression type 'comp'\n"
85 		"          -a ==> set load address to 'addr' (hex)\n"
86 		"          -e ==> set entry point to 'ep' (hex)\n"
87 		"          -n ==> set image name to 'name'\n"
88 		"          -d ==> use image data from 'datafile'\n"
89 		"          -x ==> set XIP (execute in place)\n",
90 		params.cmdname);
91 	fprintf(stderr,
92 		"       %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image\n"
93 		"           <dtb> file is used with -f auto, it may occur multiple times.\n",
94 		params.cmdname);
95 	fprintf(stderr,
96 		"          -D => set all options for device tree compiler\n"
97 		"          -f => input filename for FIT source\n"
98 		"          -i => input filename for ramdisk file\n"
99 		"          -v => set FIT image version in decimal\n");
100 
101 #ifdef CONFIG_FIT_SIGNATURE
102 	fprintf(stderr,
103 		"Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
104 		"          -E => place data outside of the FIT structure\n"
105 		"          -k => set directory containing private keys\n"
106 		"          -K => write public keys to this .dtb file\n"
107 		"          -c => add comment in signature node\n"
108 		"          -F => re-sign existing FIT image\n"
109 		"          -p => place external data at a static position\n"
110 		"          -r => mark keys used as 'required' in dtb\n"
111 		"          -N => engine to use for signing (pkcs11)\n");
112 #else
113 	fprintf(stderr,
114 		"Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
115 #endif
116 	fprintf(stderr, "       %s -V ==> print version information and exit\n",
117 		params.cmdname);
118 	fprintf(stderr, "Use '-T list' to see a list of available image types\n");
119 
120 	exit(EXIT_FAILURE);
121 }
122 
add_content(int type,const char * fname)123 static int add_content(int type, const char *fname)
124 {
125 	struct content_info *cont;
126 
127 	cont = calloc(1, sizeof(*cont));
128 	if (!cont)
129 		return -1;
130 	cont->type = type;
131 	cont->fname = fname;
132 	if (params.content_tail)
133 		params.content_tail->next = cont;
134 	else
135 		params.content_head = cont;
136 	params.content_tail = cont;
137 
138 	return 0;
139 }
140 
process_args(int argc,char ** argv)141 static void process_args(int argc, char **argv)
142 {
143 	char *ptr;
144 	int type = IH_TYPE_INVALID;
145 	char *datafile = NULL;
146 	int opt;
147 
148 	while ((opt = getopt(argc, argv,
149 			     "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:v:VxX:")) != -1) {
150 		switch (opt) {
151 		case 'a':
152 			params.addr = strtoull(optarg, &ptr, 16);
153 			if (*ptr) {
154 				fprintf(stderr, "%s: invalid load address %s\n",
155 					params.cmdname, optarg);
156 				exit(EXIT_FAILURE);
157 			}
158 			break;
159 		case 'A':
160 			params.arch = genimg_get_arch_id(optarg);
161 			if (params.arch < 0) {
162 				show_valid_options(IH_ARCH);
163 				usage("Invalid architecture");
164 			}
165 			break;
166 		case 'b':
167 			if (add_content(IH_TYPE_FLATDT, optarg)) {
168 				fprintf(stderr,
169 					"%s: Out of memory adding content '%s'",
170 					params.cmdname, optarg);
171 				exit(EXIT_FAILURE);
172 			}
173 			break;
174 		case 'c':
175 			params.comment = optarg;
176 			break;
177 		case 'C':
178 			params.comp = genimg_get_comp_id(optarg);
179 			if (params.comp < 0) {
180 				show_valid_options(IH_COMP);
181 				usage("Invalid compression type");
182 			}
183 			break;
184 		case 'd':
185 			params.datafile = optarg;
186 			params.dflag = 1;
187 			break;
188 		case 'D':
189 			params.dtc = optarg;
190 			break;
191 		case 'e':
192 			params.ep = strtoull(optarg, &ptr, 16);
193 			if (*ptr) {
194 				fprintf(stderr, "%s: invalid entry point %s\n",
195 					params.cmdname, optarg);
196 				exit(EXIT_FAILURE);
197 			}
198 			params.eflag = 1;
199 			break;
200 		case 'E':
201 			params.external_data = true;
202 			break;
203 		case 'f':
204 			datafile = optarg;
205 			params.auto_its = !strcmp(datafile, "auto");
206 			/* no break */
207 		case 'F':
208 			/*
209 			 * The flattened image tree (FIT) format
210 			 * requires a flattened device tree image type
211 			 */
212 			params.type = IH_TYPE_FLATDT;
213 			params.fflag = 1;
214 			break;
215 		case 'i':
216 			params.fit_ramdisk = optarg;
217 			break;
218 		case 'k':
219 			params.keydir = optarg;
220 			break;
221 		case 'K':
222 			params.keydest = optarg;
223 			break;
224 		case 'l':
225 			params.lflag = 1;
226 			break;
227 		case 'n':
228 			params.imagename = optarg;
229 			break;
230 		case 'N':
231 			params.engine_id = optarg;
232 			break;
233 		case 'O':
234 			params.os = genimg_get_os_id(optarg);
235 			if (params.os < 0) {
236 				show_valid_options(IH_OS);
237 				usage("Invalid operating system");
238 			}
239 			break;
240 		case 'p':
241 			params.external_offset = strtoull(optarg, &ptr, 16);
242 			if (*ptr) {
243 				fprintf(stderr, "%s: invalid offset size %s\n",
244 					params.cmdname, optarg);
245 				exit(EXIT_FAILURE);
246 			}
247 			break;
248 		case 'q':
249 			params.quiet = 1;
250 			break;
251 		case 'r':
252 			params.require_keys = 1;
253 			break;
254 		case 'R':
255 			/*
256 			 * This entry is for the second configuration
257 			 * file, if only one is not enough.
258 			 */
259 			params.imagename2 = optarg;
260 			break;
261 		case 's':
262 			params.skipcpy = 1;
263 			break;
264 		case 'T':
265 			if (strcmp(optarg, "list") == 0) {
266 				show_valid_options(IH_TYPE);
267 				exit(EXIT_SUCCESS);
268 			}
269 			type = genimg_get_type_id(optarg);
270 			if (type < 0) {
271 				show_valid_options(IH_TYPE);
272 				usage("Invalid image type");
273 			}
274 			break;
275 		case 'v':
276 			params.vflag = strtoull(optarg, &ptr, 10);
277 			if (*ptr) {
278 				fprintf(stderr, "%s: invalid version length %s\n",
279 					params.cmdname, optarg);
280 				exit(EXIT_FAILURE);
281 			}
282 			break;
283 		case 'V':
284 			printf("mkimage version %s\n", PLAIN_VERSION);
285 			exit(EXIT_SUCCESS);
286 		case 'x':
287 			params.xflag++;
288 			break;
289 		case 'X':
290 			params.extraparams = optarg;
291 			break;
292 		default:
293 			usage("Invalid option");
294 		}
295 	}
296 
297 	/* The last parameter is expected to be the imagefile */
298 	if (optind < argc)
299 		params.imagefile = argv[optind];
300 
301 	/*
302 	 * For auto-generated FIT images we need to know the image type to put
303 	 * in the FIT, which is separate from the file's image type (which
304 	 * will always be IH_TYPE_FLATDT in this case).
305 	 */
306 	if (params.type == IH_TYPE_FLATDT) {
307 		params.fit_image_type = type ? type : IH_TYPE_KERNEL;
308 		/* For auto_its, datafile is always 'auto' */
309 		if (!params.auto_its)
310 			params.datafile = datafile;
311 		else if (!params.datafile)
312 			usage("Missing data file for auto-FIT (use -d)");
313 	} else if (type != IH_TYPE_INVALID) {
314 		params.type = type;
315 	}
316 
317 	if (!params.imagefile)
318 		usage("Missing output filename");
319 }
320 
main(int argc,char ** argv)321 int main(int argc, char **argv)
322 {
323 	int ifd = -1;
324 	struct stat sbuf;
325 	char *ptr;
326 	int retval = 0;
327 	struct image_type_params *tparams = NULL;
328 	int pad_len = 0;
329 	int dfd;
330 
331 	params.cmdname = *argv;
332 	params.addr = 0;
333 	params.ep = 0;
334 
335 	process_args(argc, argv);
336 
337 	/* set tparams as per input type_id */
338 	tparams = imagetool_get_type(params.type);
339 	if (tparams == NULL) {
340 		fprintf (stderr, "%s: unsupported type %s\n",
341 			params.cmdname, genimg_get_type_name(params.type));
342 		exit (EXIT_FAILURE);
343 	}
344 
345 	/*
346 	 * check the passed arguments parameters meets the requirements
347 	 * as per image type to be generated/listed
348 	 */
349 	if (tparams->check_params)
350 		if (tparams->check_params (&params))
351 			usage("Bad parameters for image type");
352 
353 	if (!params.eflag) {
354 		params.ep = params.addr;
355 		/* If XIP, entry point must be after the U-Boot header */
356 		if (params.xflag)
357 			params.ep += tparams->header_size;
358 	}
359 
360 	if (params.fflag){
361 		if (tparams->fflag_handle)
362 			/*
363 			 * in some cases, some additional processing needs
364 			 * to be done if fflag is defined
365 			 *
366 			 * For ex. fit_handle_file for Fit file support
367 			 */
368 			retval = tparams->fflag_handle(&params);
369 
370 		if (retval != EXIT_SUCCESS)
371 			exit (retval);
372 	}
373 
374 	if (params.lflag || params.fflag) {
375 		ifd = open (params.imagefile, O_RDONLY|O_BINARY);
376 	} else {
377 		ifd = open (params.imagefile,
378 			O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
379 	}
380 
381 	if (ifd < 0) {
382 		fprintf (stderr, "%s: Can't open %s: %s\n",
383 			params.cmdname, params.imagefile,
384 			strerror(errno));
385 		exit (EXIT_FAILURE);
386 	}
387 
388 	if (params.lflag || params.fflag) {
389 		/*
390 		 * list header information of existing image
391 		 */
392 		if (fstat(ifd, &sbuf) < 0) {
393 			fprintf (stderr, "%s: Can't stat %s: %s\n",
394 				params.cmdname, params.imagefile,
395 				strerror(errno));
396 			exit (EXIT_FAILURE);
397 		}
398 
399 		if ((unsigned)sbuf.st_size < tparams->header_size) {
400 			fprintf (stderr,
401 				"%s: Bad size: \"%s\" is not valid image\n",
402 				params.cmdname, params.imagefile);
403 			exit (EXIT_FAILURE);
404 		}
405 
406 		ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
407 		if (ptr == MAP_FAILED) {
408 			fprintf (stderr, "%s: Can't read %s: %s\n",
409 				params.cmdname, params.imagefile,
410 				strerror(errno));
411 			exit (EXIT_FAILURE);
412 		}
413 
414 		/*
415 		 * scan through mkimage registry for all supported image types
416 		 * and verify the input image file header for match
417 		 * Print the image information for matched image type
418 		 * Returns the error code if not matched
419 		 */
420 		retval = imagetool_verify_print_header(ptr, &sbuf,
421 				tparams, &params);
422 
423 		(void) munmap((void *)ptr, sbuf.st_size);
424 		(void) close (ifd);
425 
426 		exit (retval);
427 	}
428 
429 	if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT) &&
430 	    (params.type != IH_TYPE_RKNAND)) {
431 		dfd = open(params.datafile, O_RDONLY | O_BINARY);
432 		if (dfd < 0) {
433 			fprintf(stderr, "%s: Can't open %s: %s\n",
434 				params.cmdname, params.datafile,
435 				strerror(errno));
436 			exit(EXIT_FAILURE);
437 		}
438 
439 		if (fstat(dfd, &sbuf) < 0) {
440 			fprintf(stderr, "%s: Can't stat %s: %s\n",
441 				params.cmdname, params.datafile,
442 				strerror(errno));
443 			exit(EXIT_FAILURE);
444 		}
445 
446 		params.file_size = sbuf.st_size + tparams->header_size;
447 		close(dfd);
448 	}
449 
450 	/*
451 	 * In case there an header with a variable
452 	 * length will be added, the corresponding
453 	 * function is called. This is responsible to
454 	 * allocate memory for the header itself.
455 	 */
456 	if (tparams->vrec_header)
457 		pad_len = tparams->vrec_header(&params, tparams);
458 	else
459 		memset(tparams->hdr, 0, tparams->header_size);
460 
461 	if (write(ifd, tparams->hdr, tparams->header_size)
462 					!= tparams->header_size) {
463 		fprintf (stderr, "%s: Write error on %s: %s\n",
464 			params.cmdname, params.imagefile, strerror(errno));
465 		exit (EXIT_FAILURE);
466 	}
467 
468 	if (!params.skipcpy) {
469 		if (params.type == IH_TYPE_MULTI ||
470 		    params.type == IH_TYPE_SCRIPT) {
471 			char *file = params.datafile;
472 			uint32_t size;
473 
474 			for (;;) {
475 				char *sep = NULL;
476 
477 				if (file) {
478 					if ((sep = strchr(file, ':')) != NULL) {
479 						*sep = '\0';
480 					}
481 
482 					if (stat (file, &sbuf) < 0) {
483 						fprintf (stderr, "%s: Can't stat %s: %s\n",
484 							 params.cmdname, file, strerror(errno));
485 						exit (EXIT_FAILURE);
486 					}
487 					size = cpu_to_uimage (sbuf.st_size);
488 				} else {
489 					size = IMAGE_PARAM_INVAL;
490 				}
491 
492 				if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
493 					fprintf (stderr, "%s: Write error on %s: %s\n",
494 						 params.cmdname, params.imagefile,
495 						 strerror(errno));
496 					exit (EXIT_FAILURE);
497 				}
498 
499 				if (!file) {
500 					break;
501 				}
502 
503 				if (sep) {
504 					*sep = ':';
505 					file = sep + 1;
506 				} else {
507 					file = NULL;
508 				}
509 			}
510 
511 			file = params.datafile;
512 
513 			for (;;) {
514 				char *sep = strchr(file, ':');
515 				if (sep) {
516 					*sep = '\0';
517 					copy_file (ifd, file, 1);
518 					*sep++ = ':';
519 					file = sep;
520 				} else {
521 					copy_file (ifd, file, 0);
522 					break;
523 				}
524 			}
525 		} else if (params.type == IH_TYPE_PBLIMAGE) {
526 			/* PBL has special Image format, implements its' own */
527 			pbl_load_uboot(ifd, &params);
528 		} else if ((params.type == IH_TYPE_RKSD) ||
529 				(params.type == IH_TYPE_RKSPI) ||
530 				(params.type == IH_TYPE_RKNAND)) {
531 			/* Rockchip has special Image format */
532 			int ret;
533 
534 			ret = rockchip_copy_image(ifd, &params);
535 			if (ret)
536 				return ret;
537 		} else {
538 			copy_file(ifd, params.datafile, pad_len);
539 		}
540 		if (params.type == IH_TYPE_FIRMWARE_IVT) {
541 			/* Add alignment and IVT */
542 			uint32_t aligned_filesize = (params.file_size + 0x1000
543 					- 1) & ~(0x1000 - 1);
544 			flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
545 					params.addr, 0, 0, 0, params.addr
546 							+ aligned_filesize
547 							- tparams->header_size,
548 					params.addr + aligned_filesize
549 							- tparams->header_size
550 							+ 0x20, 0 };
551 			int i = params.file_size;
552 			for (; i < aligned_filesize; i++) {
553 				if (write(ifd, (char *) &i, 1) != 1) {
554 					fprintf(stderr,
555 							"%s: Write error on %s: %s\n",
556 							params.cmdname,
557 							params.imagefile,
558 							strerror(errno));
559 					exit(EXIT_FAILURE);
560 				}
561 			}
562 			if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
563 					!= sizeof(flash_header_v2_t)) {
564 				fprintf(stderr, "%s: Write error on %s: %s\n",
565 						params.cmdname,
566 						params.imagefile,
567 						strerror(errno));
568 				exit(EXIT_FAILURE);
569 			}
570 		}
571 	}
572 
573 	/* We're a bit of paranoid */
574 #if defined(_POSIX_SYNCHRONIZED_IO) && \
575    !defined(__sun__) && \
576    !defined(__FreeBSD__) && \
577    !defined(__OpenBSD__) && \
578    !defined(__APPLE__)
579 	(void) fdatasync (ifd);
580 #else
581 	(void) fsync (ifd);
582 #endif
583 
584 	if (fstat(ifd, &sbuf) < 0) {
585 		fprintf (stderr, "%s: Can't stat %s: %s\n",
586 			params.cmdname, params.imagefile, strerror(errno));
587 		exit (EXIT_FAILURE);
588 	}
589 	params.file_size = sbuf.st_size;
590 
591 	ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
592 	if (ptr == MAP_FAILED) {
593 		fprintf (stderr, "%s: Can't map %s: %s\n",
594 			params.cmdname, params.imagefile, strerror(errno));
595 		exit (EXIT_FAILURE);
596 	}
597 
598 	/* Setup the image header as per input image type*/
599 	if (tparams->set_header)
600 		tparams->set_header (ptr, &sbuf, ifd, &params);
601 	else {
602 		fprintf (stderr, "%s: Can't set header for %s: %s\n",
603 			params.cmdname, tparams->name, strerror(errno));
604 		exit (EXIT_FAILURE);
605 	}
606 
607 	/* Print the image information by processing image header */
608 	if (tparams->print_header)
609 		tparams->print_header (ptr);
610 	else {
611 		fprintf (stderr, "%s: Can't print header for %s: %s\n",
612 			params.cmdname, tparams->name, strerror(errno));
613 		exit (EXIT_FAILURE);
614 	}
615 
616 	(void) munmap((void *)ptr, sbuf.st_size);
617 
618 	/* We're a bit of paranoid */
619 #if defined(_POSIX_SYNCHRONIZED_IO) && \
620    !defined(__sun__) && \
621    !defined(__FreeBSD__) && \
622    !defined(__OpenBSD__) && \
623    !defined(__APPLE__)
624 	(void) fdatasync (ifd);
625 #else
626 	(void) fsync (ifd);
627 #endif
628 
629 	if (close(ifd)) {
630 		fprintf (stderr, "%s: Write error on %s: %s\n",
631 			params.cmdname, params.imagefile, strerror(errno));
632 		exit (EXIT_FAILURE);
633 	}
634 
635 	exit (EXIT_SUCCESS);
636 }
637 
638 static void
copy_file(int ifd,const char * datafile,int pad)639 copy_file (int ifd, const char *datafile, int pad)
640 {
641 	int dfd;
642 	struct stat sbuf;
643 	unsigned char *ptr;
644 	int tail;
645 	int zero = 0;
646 	uint8_t zeros[4096];
647 	int offset = 0;
648 	int size;
649 	struct image_type_params *tparams = imagetool_get_type(params.type);
650 
651 	memset(zeros, 0, sizeof(zeros));
652 
653 	if (params.vflag) {
654 		fprintf (stderr, "Adding Image %s\n", datafile);
655 	}
656 
657 	if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
658 		fprintf (stderr, "%s: Can't open %s: %s\n",
659 			params.cmdname, datafile, strerror(errno));
660 		exit (EXIT_FAILURE);
661 	}
662 
663 	if (fstat(dfd, &sbuf) < 0) {
664 		fprintf (stderr, "%s: Can't stat %s: %s\n",
665 			params.cmdname, datafile, strerror(errno));
666 		exit (EXIT_FAILURE);
667 	}
668 
669 	if (sbuf.st_size == 0) {
670 		(void) close (dfd);
671 		return;
672 	}
673 
674 	ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
675 	if (ptr == MAP_FAILED) {
676 		fprintf (stderr, "%s: Can't read %s: %s\n",
677 			params.cmdname, datafile, strerror(errno));
678 		exit (EXIT_FAILURE);
679 	}
680 
681 	if (params.xflag) {
682 		unsigned char *p = NULL;
683 		/*
684 		 * XIP: do not append the image_header_t at the
685 		 * beginning of the file, but consume the space
686 		 * reserved for it.
687 		 */
688 
689 		if ((unsigned)sbuf.st_size < tparams->header_size) {
690 			fprintf (stderr,
691 				"%s: Bad size: \"%s\" is too small for XIP\n",
692 				params.cmdname, datafile);
693 			exit (EXIT_FAILURE);
694 		}
695 
696 		for (p = ptr; p < ptr + tparams->header_size; p++) {
697 			if ( *p != 0xff ) {
698 				fprintf (stderr,
699 					"%s: Bad file: \"%s\" has invalid buffer for XIP\n",
700 					params.cmdname, datafile);
701 				exit (EXIT_FAILURE);
702 			}
703 		}
704 
705 		offset = tparams->header_size;
706 	}
707 
708 	size = sbuf.st_size - offset;
709 	if (write(ifd, ptr + offset, size) != size) {
710 		fprintf (stderr, "%s: Write error on %s: %s\n",
711 			params.cmdname, params.imagefile, strerror(errno));
712 		exit (EXIT_FAILURE);
713 	}
714 
715 	tail = size % 4;
716 	if ((pad == 1) && (tail != 0)) {
717 
718 		if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
719 			fprintf (stderr, "%s: Write error on %s: %s\n",
720 				params.cmdname, params.imagefile,
721 				strerror(errno));
722 			exit (EXIT_FAILURE);
723 		}
724 	} else if (pad > 1) {
725 		while (pad > 0) {
726 			int todo = sizeof(zeros);
727 
728 			if (todo > pad)
729 				todo = pad;
730 			if (write(ifd, (char *)&zeros, todo) != todo) {
731 				fprintf(stderr, "%s: Write error on %s: %s\n",
732 					params.cmdname, params.imagefile,
733 					strerror(errno));
734 				exit(EXIT_FAILURE);
735 			}
736 			pad -= todo;
737 		}
738 	}
739 
740 	(void) munmap((void *)ptr, sbuf.st_size);
741 	(void) close (dfd);
742 }
743