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