mkimage.c 20 KB

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