mkimage.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 => openssl engine to use for signing\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. if (params.fflag) {
  372. /*
  373. * Verifies the header format based on the expected header for image
  374. * type in tparams
  375. */
  376. retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
  377. tparams, &params);
  378. } else {
  379. /**
  380. * When listing the image, we are not given the image type. Simply check all
  381. * image types to find one that matches our header
  382. */
  383. retval = imagetool_verify_print_header(ptr, &sbuf,
  384. tparams, &params);
  385. }
  386. (void) munmap((void *)ptr, sbuf.st_size);
  387. (void) close (ifd);
  388. exit (retval);
  389. }
  390. if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
  391. dfd = open(params.datafile, O_RDONLY | O_BINARY);
  392. if (dfd < 0) {
  393. fprintf(stderr, "%s: Can't open %s: %s\n",
  394. params.cmdname, params.datafile,
  395. strerror(errno));
  396. exit(EXIT_FAILURE);
  397. }
  398. if (fstat(dfd, &sbuf) < 0) {
  399. fprintf(stderr, "%s: Can't stat %s: %s\n",
  400. params.cmdname, params.datafile,
  401. strerror(errno));
  402. exit(EXIT_FAILURE);
  403. }
  404. params.file_size = sbuf.st_size + tparams->header_size;
  405. close(dfd);
  406. }
  407. /*
  408. * In case there an header with a variable
  409. * length will be added, the corresponding
  410. * function is called. This is responsible to
  411. * allocate memory for the header itself.
  412. */
  413. if (tparams->vrec_header)
  414. pad_len = tparams->vrec_header(&params, tparams);
  415. else
  416. memset(tparams->hdr, 0, tparams->header_size);
  417. if (write(ifd, tparams->hdr, tparams->header_size)
  418. != tparams->header_size) {
  419. fprintf (stderr, "%s: Write error on %s: %s\n",
  420. params.cmdname, params.imagefile, strerror(errno));
  421. exit (EXIT_FAILURE);
  422. }
  423. if (!params.skipcpy) {
  424. if (params.type == IH_TYPE_MULTI ||
  425. params.type == IH_TYPE_SCRIPT) {
  426. char *file = params.datafile;
  427. uint32_t size;
  428. for (;;) {
  429. char *sep = NULL;
  430. if (file) {
  431. if ((sep = strchr(file, ':')) != NULL) {
  432. *sep = '\0';
  433. }
  434. if (stat (file, &sbuf) < 0) {
  435. fprintf (stderr, "%s: Can't stat %s: %s\n",
  436. params.cmdname, file, strerror(errno));
  437. exit (EXIT_FAILURE);
  438. }
  439. size = cpu_to_uimage (sbuf.st_size);
  440. } else {
  441. size = 0;
  442. }
  443. if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
  444. fprintf (stderr, "%s: Write error on %s: %s\n",
  445. params.cmdname, params.imagefile,
  446. strerror(errno));
  447. exit (EXIT_FAILURE);
  448. }
  449. if (!file) {
  450. break;
  451. }
  452. if (sep) {
  453. *sep = ':';
  454. file = sep + 1;
  455. } else {
  456. file = NULL;
  457. }
  458. }
  459. file = params.datafile;
  460. for (;;) {
  461. char *sep = strchr(file, ':');
  462. if (sep) {
  463. *sep = '\0';
  464. copy_file (ifd, file, 1);
  465. *sep++ = ':';
  466. file = sep;
  467. } else {
  468. copy_file (ifd, file, 0);
  469. break;
  470. }
  471. }
  472. } else if (params.type == IH_TYPE_PBLIMAGE) {
  473. /* PBL has special Image format, implements its' own */
  474. pbl_load_uboot(ifd, &params);
  475. } else if (params.type == IH_TYPE_ZYNQMPBIF) {
  476. /* Image file is meta, walk through actual targets */
  477. int ret;
  478. ret = zynqmpbif_copy_image(ifd, &params);
  479. if (ret)
  480. return ret;
  481. } else if (params.type == IH_TYPE_IMX8IMAGE) {
  482. /* i.MX8/8X has special Image format */
  483. int ret;
  484. ret = imx8image_copy_image(ifd, &params);
  485. if (ret)
  486. return ret;
  487. } else if (params.type == IH_TYPE_IMX8MIMAGE) {
  488. /* i.MX8M has special Image format */
  489. int ret;
  490. ret = imx8mimage_copy_image(ifd, &params);
  491. if (ret)
  492. return ret;
  493. } else if ((params.type == IH_TYPE_RKSD) ||
  494. (params.type == IH_TYPE_RKSPI)) {
  495. /* Rockchip has special Image format */
  496. int ret;
  497. ret = rockchip_copy_image(ifd, &params);
  498. if (ret)
  499. return ret;
  500. } else {
  501. copy_file(ifd, params.datafile, pad_len);
  502. }
  503. if (params.type == IH_TYPE_FIRMWARE_IVT) {
  504. /* Add alignment and IVT */
  505. uint32_t aligned_filesize = (params.file_size + 0x1000
  506. - 1) & ~(0x1000 - 1);
  507. flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
  508. params.addr, 0, 0, 0, params.addr
  509. + aligned_filesize
  510. - tparams->header_size,
  511. params.addr + aligned_filesize
  512. - tparams->header_size
  513. + 0x20, 0 };
  514. int i = params.file_size;
  515. for (; i < aligned_filesize; i++) {
  516. if (write(ifd, (char *) &i, 1) != 1) {
  517. fprintf(stderr,
  518. "%s: Write error on %s: %s\n",
  519. params.cmdname,
  520. params.imagefile,
  521. strerror(errno));
  522. exit(EXIT_FAILURE);
  523. }
  524. }
  525. if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
  526. != sizeof(flash_header_v2_t)) {
  527. fprintf(stderr, "%s: Write error on %s: %s\n",
  528. params.cmdname,
  529. params.imagefile,
  530. strerror(errno));
  531. exit(EXIT_FAILURE);
  532. }
  533. }
  534. }
  535. /* We're a bit of paranoid */
  536. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  537. !defined(__sun__) && \
  538. !defined(__FreeBSD__) && \
  539. !defined(__OpenBSD__) && \
  540. !defined(__APPLE__)
  541. (void) fdatasync (ifd);
  542. #else
  543. (void) fsync (ifd);
  544. #endif
  545. if (fstat(ifd, &sbuf) < 0) {
  546. fprintf (stderr, "%s: Can't stat %s: %s\n",
  547. params.cmdname, params.imagefile, strerror(errno));
  548. exit (EXIT_FAILURE);
  549. }
  550. params.file_size = sbuf.st_size;
  551. map_len = sbuf.st_size;
  552. ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
  553. if (ptr == MAP_FAILED) {
  554. fprintf (stderr, "%s: Can't map %s: %s\n",
  555. params.cmdname, params.imagefile, strerror(errno));
  556. exit (EXIT_FAILURE);
  557. }
  558. /* Setup the image header as per input image type*/
  559. if (tparams->set_header)
  560. tparams->set_header (ptr, &sbuf, ifd, &params);
  561. else {
  562. fprintf (stderr, "%s: Can't set header for %s: %s\n",
  563. params.cmdname, tparams->name, strerror(errno));
  564. exit (EXIT_FAILURE);
  565. }
  566. /* Print the image information by processing image header */
  567. if (tparams->print_header)
  568. tparams->print_header (ptr);
  569. else {
  570. fprintf (stderr, "%s: Can't print header for %s\n",
  571. params.cmdname, tparams->name);
  572. }
  573. (void)munmap((void *)ptr, map_len);
  574. /* We're a bit of paranoid */
  575. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  576. !defined(__sun__) && \
  577. !defined(__FreeBSD__) && \
  578. !defined(__OpenBSD__) && \
  579. !defined(__APPLE__)
  580. (void) fdatasync (ifd);
  581. #else
  582. (void) fsync (ifd);
  583. #endif
  584. if (close(ifd)) {
  585. fprintf (stderr, "%s: Write error on %s: %s\n",
  586. params.cmdname, params.imagefile, strerror(errno));
  587. exit (EXIT_FAILURE);
  588. }
  589. exit (EXIT_SUCCESS);
  590. }
  591. static void
  592. copy_file (int ifd, const char *datafile, int pad)
  593. {
  594. int dfd;
  595. struct stat sbuf;
  596. unsigned char *ptr;
  597. int tail;
  598. int zero = 0;
  599. uint8_t zeros[4096];
  600. int offset = 0;
  601. int size;
  602. struct image_type_params *tparams = imagetool_get_type(params.type);
  603. memset(zeros, 0, sizeof(zeros));
  604. if (params.vflag) {
  605. fprintf (stderr, "Adding Image %s\n", datafile);
  606. }
  607. if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
  608. fprintf (stderr, "%s: Can't open %s: %s\n",
  609. params.cmdname, datafile, strerror(errno));
  610. exit (EXIT_FAILURE);
  611. }
  612. if (fstat(dfd, &sbuf) < 0) {
  613. fprintf (stderr, "%s: Can't stat %s: %s\n",
  614. params.cmdname, datafile, strerror(errno));
  615. exit (EXIT_FAILURE);
  616. }
  617. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  618. if (ptr == MAP_FAILED) {
  619. fprintf (stderr, "%s: Can't read %s: %s\n",
  620. params.cmdname, datafile, strerror(errno));
  621. exit (EXIT_FAILURE);
  622. }
  623. if (params.xflag) {
  624. unsigned char *p = NULL;
  625. /*
  626. * XIP: do not append the image_header_t at the
  627. * beginning of the file, but consume the space
  628. * reserved for it.
  629. */
  630. if ((unsigned)sbuf.st_size < tparams->header_size) {
  631. fprintf (stderr,
  632. "%s: Bad size: \"%s\" is too small for XIP\n",
  633. params.cmdname, datafile);
  634. exit (EXIT_FAILURE);
  635. }
  636. for (p = ptr; p < ptr + tparams->header_size; p++) {
  637. if ( *p != 0xff ) {
  638. fprintf (stderr,
  639. "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
  640. params.cmdname, datafile);
  641. exit (EXIT_FAILURE);
  642. }
  643. }
  644. offset = tparams->header_size;
  645. }
  646. size = sbuf.st_size - offset;
  647. if (write(ifd, ptr + offset, size) != size) {
  648. fprintf (stderr, "%s: Write error on %s: %s\n",
  649. params.cmdname, params.imagefile, strerror(errno));
  650. exit (EXIT_FAILURE);
  651. }
  652. tail = size % 4;
  653. if ((pad == 1) && (tail != 0)) {
  654. if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
  655. fprintf (stderr, "%s: Write error on %s: %s\n",
  656. params.cmdname, params.imagefile,
  657. strerror(errno));
  658. exit (EXIT_FAILURE);
  659. }
  660. } else if (pad > 1) {
  661. while (pad > 0) {
  662. int todo = sizeof(zeros);
  663. if (todo > pad)
  664. todo = pad;
  665. if (write(ifd, (char *)&zeros, todo) != todo) {
  666. fprintf(stderr, "%s: Write error on %s: %s\n",
  667. params.cmdname, params.imagefile,
  668. strerror(errno));
  669. exit(EXIT_FAILURE);
  670. }
  671. pad -= todo;
  672. }
  673. }
  674. (void) munmap((void *)ptr, sbuf.st_size);
  675. (void) close (dfd);
  676. }