mkimage.c 19 KB

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