mkimage.c 17 KB

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