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