mkimage.c 19 KB

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