mkimage.c 19 KB

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