mkimage.c 19 KB

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