mkimage.c 20 KB

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