mkimage.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2008 Semihalf
  4. *
  5. * (C) Copyright 2000-2009
  6. * DENX Software Engineering
  7. * Wolfgang Denk, wd@denx.de
  8. */
  9. #include "mkimage.h"
  10. #include "imximage.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. static 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. free(order);
  58. return 0;
  59. }
  60. static void usage(const char *msg)
  61. {
  62. fprintf(stderr, "Error: %s\n", msg);
  63. fprintf(stderr, "Usage: %s -l image\n"
  64. " -l ==> list image header information\n",
  65. params.cmdname);
  66. fprintf(stderr,
  67. " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
  68. " -A ==> set architecture to 'arch'\n"
  69. " -O ==> set operating system to 'os'\n"
  70. " -T ==> set image type to 'type'\n"
  71. " -C ==> set compression type 'comp'\n"
  72. " -a ==> set load address to 'addr' (hex)\n"
  73. " -e ==> set entry point to 'ep' (hex)\n"
  74. " -n ==> set image name to 'name'\n"
  75. " -d ==> use image data from 'datafile'\n"
  76. " -x ==> set XIP (execute in place)\n",
  77. params.cmdname);
  78. fprintf(stderr,
  79. " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image\n"
  80. " <dtb> file is used with -f auto, it may occur multiple times.\n",
  81. params.cmdname);
  82. fprintf(stderr,
  83. " -D => set all options for device tree compiler\n"
  84. " -f => input filename for FIT source\n"
  85. " -i => input filename for ramdisk file\n");
  86. #ifdef CONFIG_FIT_SIGNATURE
  87. fprintf(stderr,
  88. "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
  89. " -E => place data outside of the FIT structure\n"
  90. " -k => set directory containing private keys\n"
  91. " -K => write public keys to this .dtb file\n"
  92. " -c => add comment in signature node\n"
  93. " -F => re-sign existing FIT image\n"
  94. " -p => place external data at a static position\n"
  95. " -r => mark keys used as 'required' in dtb\n"
  96. " -N => engine to use for signing (pkcs11)\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 list' 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:N: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 'N':
  210. params.engine_id = optarg;
  211. break;
  212. case 'O':
  213. params.os = genimg_get_os_id(optarg);
  214. if (params.os < 0) {
  215. show_valid_options(IH_OS);
  216. usage("Invalid operating system");
  217. }
  218. break;
  219. case 'p':
  220. params.external_offset = strtoull(optarg, &ptr, 16);
  221. if (*ptr) {
  222. fprintf(stderr, "%s: invalid offset size %s\n",
  223. params.cmdname, optarg);
  224. exit(EXIT_FAILURE);
  225. }
  226. break;
  227. case 'q':
  228. params.quiet = 1;
  229. break;
  230. case 'r':
  231. params.require_keys = 1;
  232. break;
  233. case 'R':
  234. /*
  235. * This entry is for the second configuration
  236. * file, if only one is not enough.
  237. */
  238. params.imagename2 = optarg;
  239. break;
  240. case 's':
  241. params.skipcpy = 1;
  242. break;
  243. case 'T':
  244. if (strcmp(optarg, "list") == 0) {
  245. show_valid_options(IH_TYPE);
  246. exit(EXIT_SUCCESS);
  247. }
  248. type = genimg_get_type_id(optarg);
  249. if (type < 0) {
  250. show_valid_options(IH_TYPE);
  251. usage("Invalid image type");
  252. }
  253. break;
  254. case 'v':
  255. params.vflag++;
  256. break;
  257. case 'V':
  258. printf("mkimage version %s\n", PLAIN_VERSION);
  259. exit(EXIT_SUCCESS);
  260. case 'x':
  261. params.xflag++;
  262. break;
  263. default:
  264. usage("Invalid option");
  265. }
  266. }
  267. /* The last parameter is expected to be the imagefile */
  268. if (optind < argc)
  269. params.imagefile = argv[optind];
  270. /*
  271. * For auto-generated FIT images we need to know the image type to put
  272. * in the FIT, which is separate from the file's image type (which
  273. * will always be IH_TYPE_FLATDT in this case).
  274. */
  275. if (params.type == IH_TYPE_FLATDT) {
  276. params.fit_image_type = type ? type : IH_TYPE_KERNEL;
  277. /* For auto_its, datafile is always 'auto' */
  278. if (!params.auto_its)
  279. params.datafile = datafile;
  280. else if (!params.datafile)
  281. usage("Missing data file for auto-FIT (use -d)");
  282. } else if (type != IH_TYPE_INVALID) {
  283. if (type == IH_TYPE_SCRIPT && !params.datafile)
  284. usage("Missing data file for script (use -d)");
  285. params.type = type;
  286. }
  287. if (!params.imagefile)
  288. usage("Missing output filename");
  289. }
  290. int main(int argc, char **argv)
  291. {
  292. int ifd = -1;
  293. struct stat sbuf;
  294. char *ptr;
  295. int retval = 0;
  296. struct image_type_params *tparams = NULL;
  297. int pad_len = 0;
  298. int dfd;
  299. params.cmdname = *argv;
  300. params.addr = 0;
  301. params.ep = 0;
  302. process_args(argc, argv);
  303. /* set tparams as per input type_id */
  304. tparams = imagetool_get_type(params.type);
  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. }
  310. /*
  311. * check the passed arguments parameters meets the requirements
  312. * as per image type to be generated/listed
  313. */
  314. if (tparams->check_params)
  315. if (tparams->check_params (&params))
  316. usage("Bad parameters for image type");
  317. if (!params.eflag) {
  318. params.ep = params.addr;
  319. /* If XIP, entry point must be after the U-Boot header */
  320. if (params.xflag)
  321. params.ep += tparams->header_size;
  322. }
  323. if (params.fflag){
  324. if (tparams->fflag_handle)
  325. /*
  326. * in some cases, some additional processing needs
  327. * to be done if fflag is defined
  328. *
  329. * For ex. fit_handle_file for Fit file support
  330. */
  331. retval = tparams->fflag_handle(&params);
  332. if (retval != EXIT_SUCCESS)
  333. exit (retval);
  334. }
  335. if (params.lflag || params.fflag) {
  336. ifd = open (params.imagefile, O_RDONLY|O_BINARY);
  337. } else {
  338. ifd = open (params.imagefile,
  339. O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
  340. }
  341. if (ifd < 0) {
  342. fprintf (stderr, "%s: Can't open %s: %s\n",
  343. params.cmdname, params.imagefile,
  344. strerror(errno));
  345. exit (EXIT_FAILURE);
  346. }
  347. if (params.lflag || params.fflag) {
  348. /*
  349. * list header information of existing image
  350. */
  351. if (fstat(ifd, &sbuf) < 0) {
  352. fprintf (stderr, "%s: Can't stat %s: %s\n",
  353. params.cmdname, params.imagefile,
  354. strerror(errno));
  355. exit (EXIT_FAILURE);
  356. }
  357. if ((unsigned)sbuf.st_size < tparams->header_size) {
  358. fprintf (stderr,
  359. "%s: Bad size: \"%s\" is not valid image\n",
  360. params.cmdname, params.imagefile);
  361. exit (EXIT_FAILURE);
  362. }
  363. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  364. if (ptr == MAP_FAILED) {
  365. fprintf (stderr, "%s: Can't read %s: %s\n",
  366. params.cmdname, params.imagefile,
  367. strerror(errno));
  368. exit (EXIT_FAILURE);
  369. }
  370. /*
  371. * scan through mkimage registry for all supported image types
  372. * and verify the input image file header for match
  373. * Print the image information for matched image type
  374. * Returns the error code if not matched
  375. */
  376. retval = imagetool_verify_print_header(ptr, &sbuf,
  377. tparams, &params);
  378. (void) munmap((void *)ptr, sbuf.st_size);
  379. (void) close (ifd);
  380. exit (retval);
  381. }
  382. if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
  383. dfd = open(params.datafile, O_RDONLY | O_BINARY);
  384. if (dfd < 0) {
  385. fprintf(stderr, "%s: Can't open %s: %s\n",
  386. params.cmdname, params.datafile,
  387. strerror(errno));
  388. exit(EXIT_FAILURE);
  389. }
  390. if (fstat(dfd, &sbuf) < 0) {
  391. fprintf(stderr, "%s: Can't stat %s: %s\n",
  392. params.cmdname, params.datafile,
  393. strerror(errno));
  394. exit(EXIT_FAILURE);
  395. }
  396. params.file_size = sbuf.st_size + tparams->header_size;
  397. close(dfd);
  398. }
  399. /*
  400. * In case there an header with a variable
  401. * length will be added, the corresponding
  402. * function is called. This is responsible to
  403. * allocate memory for the header itself.
  404. */
  405. if (tparams->vrec_header)
  406. pad_len = tparams->vrec_header(&params, tparams);
  407. else
  408. memset(tparams->hdr, 0, tparams->header_size);
  409. if (write(ifd, tparams->hdr, tparams->header_size)
  410. != tparams->header_size) {
  411. fprintf (stderr, "%s: Write error on %s: %s\n",
  412. params.cmdname, params.imagefile, strerror(errno));
  413. exit (EXIT_FAILURE);
  414. }
  415. if (!params.skipcpy) {
  416. if (params.type == IH_TYPE_MULTI ||
  417. params.type == IH_TYPE_SCRIPT) {
  418. char *file = params.datafile;
  419. uint32_t size;
  420. for (;;) {
  421. char *sep = NULL;
  422. if (file) {
  423. if ((sep = strchr(file, ':')) != NULL) {
  424. *sep = '\0';
  425. }
  426. if (stat (file, &sbuf) < 0) {
  427. fprintf (stderr, "%s: Can't stat %s: %s\n",
  428. params.cmdname, file, strerror(errno));
  429. exit (EXIT_FAILURE);
  430. }
  431. size = cpu_to_uimage (sbuf.st_size);
  432. } else {
  433. size = 0;
  434. }
  435. if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
  436. fprintf (stderr, "%s: Write error on %s: %s\n",
  437. params.cmdname, params.imagefile,
  438. strerror(errno));
  439. exit (EXIT_FAILURE);
  440. }
  441. if (!file) {
  442. break;
  443. }
  444. if (sep) {
  445. *sep = ':';
  446. file = sep + 1;
  447. } else {
  448. file = NULL;
  449. }
  450. }
  451. file = params.datafile;
  452. for (;;) {
  453. char *sep = strchr(file, ':');
  454. if (sep) {
  455. *sep = '\0';
  456. copy_file (ifd, file, 1);
  457. *sep++ = ':';
  458. file = sep;
  459. } else {
  460. copy_file (ifd, file, 0);
  461. break;
  462. }
  463. }
  464. } else if (params.type == IH_TYPE_PBLIMAGE) {
  465. /* PBL has special Image format, implements its' own */
  466. pbl_load_uboot(ifd, &params);
  467. } else if (params.type == IH_TYPE_ZYNQMPBIF) {
  468. /* Image file is meta, walk through actual targets */
  469. int ret;
  470. ret = zynqmpbif_copy_image(ifd, &params);
  471. if (ret)
  472. return ret;
  473. } else {
  474. copy_file(ifd, params.datafile, pad_len);
  475. }
  476. if (params.type == IH_TYPE_FIRMWARE_IVT) {
  477. /* Add alignment and IVT */
  478. uint32_t aligned_filesize = (params.file_size + 0x1000
  479. - 1) & ~(0x1000 - 1);
  480. flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
  481. params.addr, 0, 0, 0, params.addr
  482. + aligned_filesize
  483. - tparams->header_size,
  484. params.addr + aligned_filesize
  485. - tparams->header_size
  486. + 0x20, 0 };
  487. int i = params.file_size;
  488. for (; i < aligned_filesize; i++) {
  489. if (write(ifd, (char *) &i, 1) != 1) {
  490. fprintf(stderr,
  491. "%s: Write error on %s: %s\n",
  492. params.cmdname,
  493. params.imagefile,
  494. strerror(errno));
  495. exit(EXIT_FAILURE);
  496. }
  497. }
  498. if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
  499. != sizeof(flash_header_v2_t)) {
  500. fprintf(stderr, "%s: Write error on %s: %s\n",
  501. params.cmdname,
  502. params.imagefile,
  503. strerror(errno));
  504. exit(EXIT_FAILURE);
  505. }
  506. }
  507. }
  508. /* We're a bit of paranoid */
  509. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  510. !defined(__sun__) && \
  511. !defined(__FreeBSD__) && \
  512. !defined(__OpenBSD__) && \
  513. !defined(__APPLE__)
  514. (void) fdatasync (ifd);
  515. #else
  516. (void) fsync (ifd);
  517. #endif
  518. if (fstat(ifd, &sbuf) < 0) {
  519. fprintf (stderr, "%s: Can't stat %s: %s\n",
  520. params.cmdname, params.imagefile, strerror(errno));
  521. exit (EXIT_FAILURE);
  522. }
  523. params.file_size = sbuf.st_size;
  524. ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
  525. if (ptr == MAP_FAILED) {
  526. fprintf (stderr, "%s: Can't map %s: %s\n",
  527. params.cmdname, params.imagefile, strerror(errno));
  528. exit (EXIT_FAILURE);
  529. }
  530. /* Setup the image header as per input image type*/
  531. if (tparams->set_header)
  532. tparams->set_header (ptr, &sbuf, ifd, &params);
  533. else {
  534. fprintf (stderr, "%s: Can't set header for %s: %s\n",
  535. params.cmdname, tparams->name, strerror(errno));
  536. exit (EXIT_FAILURE);
  537. }
  538. /* Print the image information by processing image header */
  539. if (tparams->print_header)
  540. tparams->print_header (ptr);
  541. else {
  542. fprintf (stderr, "%s: Can't print header for %s\n",
  543. params.cmdname, tparams->name);
  544. }
  545. (void) munmap((void *)ptr, sbuf.st_size);
  546. /* We're a bit of paranoid */
  547. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  548. !defined(__sun__) && \
  549. !defined(__FreeBSD__) && \
  550. !defined(__OpenBSD__) && \
  551. !defined(__APPLE__)
  552. (void) fdatasync (ifd);
  553. #else
  554. (void) fsync (ifd);
  555. #endif
  556. if (close(ifd)) {
  557. fprintf (stderr, "%s: Write error on %s: %s\n",
  558. params.cmdname, params.imagefile, strerror(errno));
  559. exit (EXIT_FAILURE);
  560. }
  561. exit (EXIT_SUCCESS);
  562. }
  563. static void
  564. copy_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;
  571. uint8_t zeros[4096];
  572. int offset = 0;
  573. int size;
  574. struct image_type_params *tparams = imagetool_get_type(params.type);
  575. memset(zeros, 0, sizeof(zeros));
  576. if (params.vflag) {
  577. fprintf (stderr, "Adding Image %s\n", datafile);
  578. }
  579. if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
  580. fprintf (stderr, "%s: Can't open %s: %s\n",
  581. params.cmdname, datafile, strerror(errno));
  582. exit (EXIT_FAILURE);
  583. }
  584. if (fstat(dfd, &sbuf) < 0) {
  585. fprintf (stderr, "%s: Can't stat %s: %s\n",
  586. params.cmdname, datafile, strerror(errno));
  587. exit (EXIT_FAILURE);
  588. }
  589. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  590. if (ptr == MAP_FAILED) {
  591. fprintf (stderr, "%s: Can't read %s: %s\n",
  592. params.cmdname, datafile, strerror(errno));
  593. exit (EXIT_FAILURE);
  594. }
  595. if (params.xflag) {
  596. unsigned char *p = NULL;
  597. /*
  598. * XIP: do not append the image_header_t at the
  599. * beginning of the file, but consume the space
  600. * reserved for it.
  601. */
  602. if ((unsigned)sbuf.st_size < tparams->header_size) {
  603. fprintf (stderr,
  604. "%s: Bad size: \"%s\" is too small for XIP\n",
  605. params.cmdname, datafile);
  606. exit (EXIT_FAILURE);
  607. }
  608. for (p = ptr; p < ptr + tparams->header_size; p++) {
  609. if ( *p != 0xff ) {
  610. fprintf (stderr,
  611. "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
  612. params.cmdname, datafile);
  613. exit (EXIT_FAILURE);
  614. }
  615. }
  616. offset = tparams->header_size;
  617. }
  618. size = sbuf.st_size - offset;
  619. if (write(ifd, ptr + offset, size) != size) {
  620. fprintf (stderr, "%s: Write error on %s: %s\n",
  621. params.cmdname, params.imagefile, strerror(errno));
  622. exit (EXIT_FAILURE);
  623. }
  624. tail = size % 4;
  625. if ((pad == 1) && (tail != 0)) {
  626. if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
  627. fprintf (stderr, "%s: Write error on %s: %s\n",
  628. params.cmdname, params.imagefile,
  629. strerror(errno));
  630. exit (EXIT_FAILURE);
  631. }
  632. } else if (pad > 1) {
  633. while (pad > 0) {
  634. int todo = sizeof(zeros);
  635. if (todo > pad)
  636. todo = pad;
  637. if (write(ifd, (char *)&zeros, todo) != todo) {
  638. fprintf(stderr, "%s: Write error on %s: %s\n",
  639. params.cmdname, params.imagefile,
  640. strerror(errno));
  641. exit(EXIT_FAILURE);
  642. }
  643. pad -= todo;
  644. }
  645. }
  646. (void) munmap((void *)ptr, sbuf.st_size);
  647. (void) close (dfd);
  648. }