mkimage.c 19 KB

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