mkimage.c 17 KB

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