mkimage.c 17 KB

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