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