mkimage.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. size_t map_len;
  300. params.cmdname = *argv;
  301. params.addr = 0;
  302. params.ep = 0;
  303. process_args(argc, argv);
  304. /* set tparams as per input type_id */
  305. tparams = imagetool_get_type(params.type);
  306. if (tparams == NULL) {
  307. fprintf (stderr, "%s: unsupported type %s\n",
  308. params.cmdname, genimg_get_type_name(params.type));
  309. exit (EXIT_FAILURE);
  310. }
  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))
  317. usage("Bad parameters for image type");
  318. if (!params.eflag) {
  319. params.ep = params.addr;
  320. /* If XIP, entry point must be after the U-Boot header */
  321. if (params.xflag)
  322. params.ep += tparams->header_size;
  323. }
  324. if (params.fflag){
  325. if (tparams->fflag_handle)
  326. /*
  327. * in some cases, some additional processing needs
  328. * to be done if fflag is defined
  329. *
  330. * For ex. fit_handle_file for Fit file support
  331. */
  332. retval = tparams->fflag_handle(&params);
  333. if (retval != EXIT_SUCCESS)
  334. exit (retval);
  335. }
  336. if (params.lflag || params.fflag) {
  337. ifd = open (params.imagefile, O_RDONLY|O_BINARY);
  338. } else {
  339. ifd = open (params.imagefile,
  340. O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
  341. }
  342. if (ifd < 0) {
  343. fprintf (stderr, "%s: Can't open %s: %s\n",
  344. params.cmdname, params.imagefile,
  345. strerror(errno));
  346. exit (EXIT_FAILURE);
  347. }
  348. if (params.lflag || params.fflag) {
  349. /*
  350. * list header information of existing image
  351. */
  352. if (fstat(ifd, &sbuf) < 0) {
  353. fprintf (stderr, "%s: Can't stat %s: %s\n",
  354. params.cmdname, params.imagefile,
  355. strerror(errno));
  356. exit (EXIT_FAILURE);
  357. }
  358. if ((unsigned)sbuf.st_size < tparams->header_size) {
  359. fprintf (stderr,
  360. "%s: Bad size: \"%s\" is not valid image\n",
  361. params.cmdname, params.imagefile);
  362. exit (EXIT_FAILURE);
  363. }
  364. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  365. if (ptr == MAP_FAILED) {
  366. fprintf (stderr, "%s: Can't read %s: %s\n",
  367. params.cmdname, params.imagefile,
  368. strerror(errno));
  369. exit (EXIT_FAILURE);
  370. }
  371. /*
  372. * scan through mkimage registry for all supported image types
  373. * and verify the input image file header for match
  374. * Print the image information for matched image type
  375. * Returns the error code if not matched
  376. */
  377. retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
  378. tparams, &params);
  379. (void) munmap((void *)ptr, sbuf.st_size);
  380. (void) close (ifd);
  381. exit (retval);
  382. }
  383. if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
  384. dfd = open(params.datafile, O_RDONLY | O_BINARY);
  385. if (dfd < 0) {
  386. fprintf(stderr, "%s: Can't open %s: %s\n",
  387. params.cmdname, params.datafile,
  388. strerror(errno));
  389. exit(EXIT_FAILURE);
  390. }
  391. if (fstat(dfd, &sbuf) < 0) {
  392. fprintf(stderr, "%s: Can't stat %s: %s\n",
  393. params.cmdname, params.datafile,
  394. strerror(errno));
  395. exit(EXIT_FAILURE);
  396. }
  397. params.file_size = sbuf.st_size + tparams->header_size;
  398. close(dfd);
  399. }
  400. /*
  401. * In case there an header with a variable
  402. * length will be added, the corresponding
  403. * function is called. This is responsible to
  404. * allocate memory for the header itself.
  405. */
  406. if (tparams->vrec_header)
  407. pad_len = tparams->vrec_header(&params, tparams);
  408. else
  409. memset(tparams->hdr, 0, tparams->header_size);
  410. if (write(ifd, tparams->hdr, tparams->header_size)
  411. != tparams->header_size) {
  412. fprintf (stderr, "%s: Write error on %s: %s\n",
  413. params.cmdname, params.imagefile, strerror(errno));
  414. exit (EXIT_FAILURE);
  415. }
  416. if (!params.skipcpy) {
  417. if (params.type == IH_TYPE_MULTI ||
  418. params.type == IH_TYPE_SCRIPT) {
  419. char *file = params.datafile;
  420. uint32_t size;
  421. for (;;) {
  422. char *sep = NULL;
  423. if (file) {
  424. if ((sep = strchr(file, ':')) != NULL) {
  425. *sep = '\0';
  426. }
  427. if (stat (file, &sbuf) < 0) {
  428. fprintf (stderr, "%s: Can't stat %s: %s\n",
  429. params.cmdname, file, strerror(errno));
  430. exit (EXIT_FAILURE);
  431. }
  432. size = cpu_to_uimage (sbuf.st_size);
  433. } else {
  434. size = 0;
  435. }
  436. if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
  437. fprintf (stderr, "%s: Write error on %s: %s\n",
  438. params.cmdname, params.imagefile,
  439. strerror(errno));
  440. exit (EXIT_FAILURE);
  441. }
  442. if (!file) {
  443. break;
  444. }
  445. if (sep) {
  446. *sep = ':';
  447. file = sep + 1;
  448. } else {
  449. file = NULL;
  450. }
  451. }
  452. file = params.datafile;
  453. for (;;) {
  454. char *sep = strchr(file, ':');
  455. if (sep) {
  456. *sep = '\0';
  457. copy_file (ifd, file, 1);
  458. *sep++ = ':';
  459. file = sep;
  460. } else {
  461. copy_file (ifd, file, 0);
  462. break;
  463. }
  464. }
  465. } else if (params.type == IH_TYPE_PBLIMAGE) {
  466. /* PBL has special Image format, implements its' own */
  467. pbl_load_uboot(ifd, &params);
  468. } else if (params.type == IH_TYPE_ZYNQMPBIF) {
  469. /* Image file is meta, walk through actual targets */
  470. int ret;
  471. ret = zynqmpbif_copy_image(ifd, &params);
  472. if (ret)
  473. return ret;
  474. } else if (params.type == IH_TYPE_IMX8IMAGE) {
  475. /* i.MX8/8X has special Image format */
  476. int ret;
  477. ret = imx8image_copy_image(ifd, &params);
  478. if (ret)
  479. return ret;
  480. } else if (params.type == IH_TYPE_IMX8MIMAGE) {
  481. /* i.MX8M has special Image format */
  482. int ret;
  483. ret = imx8mimage_copy_image(ifd, &params);
  484. if (ret)
  485. return ret;
  486. } else {
  487. copy_file(ifd, params.datafile, pad_len);
  488. }
  489. if (params.type == IH_TYPE_FIRMWARE_IVT) {
  490. /* Add alignment and IVT */
  491. uint32_t aligned_filesize = (params.file_size + 0x1000
  492. - 1) & ~(0x1000 - 1);
  493. flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
  494. params.addr, 0, 0, 0, params.addr
  495. + aligned_filesize
  496. - tparams->header_size,
  497. params.addr + aligned_filesize
  498. - tparams->header_size
  499. + 0x20, 0 };
  500. int i = params.file_size;
  501. for (; i < aligned_filesize; i++) {
  502. if (write(ifd, (char *) &i, 1) != 1) {
  503. fprintf(stderr,
  504. "%s: Write error on %s: %s\n",
  505. params.cmdname,
  506. params.imagefile,
  507. strerror(errno));
  508. exit(EXIT_FAILURE);
  509. }
  510. }
  511. if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
  512. != sizeof(flash_header_v2_t)) {
  513. fprintf(stderr, "%s: Write error on %s: %s\n",
  514. params.cmdname,
  515. params.imagefile,
  516. strerror(errno));
  517. exit(EXIT_FAILURE);
  518. }
  519. }
  520. }
  521. /* We're a bit of paranoid */
  522. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  523. !defined(__sun__) && \
  524. !defined(__FreeBSD__) && \
  525. !defined(__OpenBSD__) && \
  526. !defined(__APPLE__)
  527. (void) fdatasync (ifd);
  528. #else
  529. (void) fsync (ifd);
  530. #endif
  531. if (fstat(ifd, &sbuf) < 0) {
  532. fprintf (stderr, "%s: Can't stat %s: %s\n",
  533. params.cmdname, params.imagefile, strerror(errno));
  534. exit (EXIT_FAILURE);
  535. }
  536. params.file_size = sbuf.st_size;
  537. map_len = sbuf.st_size;
  538. ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
  539. if (ptr == MAP_FAILED) {
  540. fprintf (stderr, "%s: Can't map %s: %s\n",
  541. params.cmdname, params.imagefile, strerror(errno));
  542. exit (EXIT_FAILURE);
  543. }
  544. /* Setup the image header as per input image type*/
  545. if (tparams->set_header)
  546. tparams->set_header (ptr, &sbuf, ifd, &params);
  547. else {
  548. fprintf (stderr, "%s: Can't set header for %s: %s\n",
  549. params.cmdname, tparams->name, strerror(errno));
  550. exit (EXIT_FAILURE);
  551. }
  552. /* Print the image information by processing image header */
  553. if (tparams->print_header)
  554. tparams->print_header (ptr);
  555. else {
  556. fprintf (stderr, "%s: Can't print header for %s\n",
  557. params.cmdname, tparams->name);
  558. }
  559. (void)munmap((void *)ptr, map_len);
  560. /* We're a bit of paranoid */
  561. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  562. !defined(__sun__) && \
  563. !defined(__FreeBSD__) && \
  564. !defined(__OpenBSD__) && \
  565. !defined(__APPLE__)
  566. (void) fdatasync (ifd);
  567. #else
  568. (void) fsync (ifd);
  569. #endif
  570. if (close(ifd)) {
  571. fprintf (stderr, "%s: Write error on %s: %s\n",
  572. params.cmdname, params.imagefile, strerror(errno));
  573. exit (EXIT_FAILURE);
  574. }
  575. exit (EXIT_SUCCESS);
  576. }
  577. static void
  578. copy_file (int ifd, const char *datafile, int pad)
  579. {
  580. int dfd;
  581. struct stat sbuf;
  582. unsigned char *ptr;
  583. int tail;
  584. int zero = 0;
  585. uint8_t zeros[4096];
  586. int offset = 0;
  587. int size;
  588. struct image_type_params *tparams = imagetool_get_type(params.type);
  589. memset(zeros, 0, sizeof(zeros));
  590. if (params.vflag) {
  591. fprintf (stderr, "Adding Image %s\n", datafile);
  592. }
  593. if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
  594. fprintf (stderr, "%s: Can't open %s: %s\n",
  595. params.cmdname, datafile, strerror(errno));
  596. exit (EXIT_FAILURE);
  597. }
  598. if (fstat(dfd, &sbuf) < 0) {
  599. fprintf (stderr, "%s: Can't stat %s: %s\n",
  600. params.cmdname, datafile, strerror(errno));
  601. exit (EXIT_FAILURE);
  602. }
  603. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  604. if (ptr == MAP_FAILED) {
  605. fprintf (stderr, "%s: Can't read %s: %s\n",
  606. params.cmdname, datafile, strerror(errno));
  607. exit (EXIT_FAILURE);
  608. }
  609. if (params.xflag) {
  610. unsigned char *p = NULL;
  611. /*
  612. * XIP: do not append the image_header_t at the
  613. * beginning of the file, but consume the space
  614. * reserved for it.
  615. */
  616. if ((unsigned)sbuf.st_size < tparams->header_size) {
  617. fprintf (stderr,
  618. "%s: Bad size: \"%s\" is too small for XIP\n",
  619. params.cmdname, datafile);
  620. exit (EXIT_FAILURE);
  621. }
  622. for (p = ptr; p < ptr + tparams->header_size; p++) {
  623. if ( *p != 0xff ) {
  624. fprintf (stderr,
  625. "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
  626. params.cmdname, datafile);
  627. exit (EXIT_FAILURE);
  628. }
  629. }
  630. offset = tparams->header_size;
  631. }
  632. size = sbuf.st_size - offset;
  633. if (write(ifd, ptr + offset, size) != size) {
  634. fprintf (stderr, "%s: Write error on %s: %s\n",
  635. params.cmdname, params.imagefile, strerror(errno));
  636. exit (EXIT_FAILURE);
  637. }
  638. tail = size % 4;
  639. if ((pad == 1) && (tail != 0)) {
  640. if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
  641. fprintf (stderr, "%s: Write error on %s: %s\n",
  642. params.cmdname, params.imagefile,
  643. strerror(errno));
  644. exit (EXIT_FAILURE);
  645. }
  646. } else if (pad > 1) {
  647. while (pad > 0) {
  648. int todo = sizeof(zeros);
  649. if (todo > pad)
  650. todo = pad;
  651. if (write(ifd, (char *)&zeros, todo) != todo) {
  652. fprintf(stderr, "%s: Write error on %s: %s\n",
  653. params.cmdname, params.imagefile,
  654. strerror(errno));
  655. exit(EXIT_FAILURE);
  656. }
  657. pad -= todo;
  658. }
  659. }
  660. (void) munmap((void *)ptr, sbuf.st_size);
  661. (void) close (dfd);
  662. }