mkimage.c 24 KB

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