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