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