mkimage.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2009
  5. * DENX Software Engineering
  6. * Wolfgang Denk, wd@denx.de
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include "mkimage.h"
  11. #include <image.h>
  12. #include <version.h>
  13. static void copy_file(int, const char *, int);
  14. /* parameters initialized by core will be used by the image type code */
  15. static struct image_tool_params params = {
  16. .os = IH_OS_LINUX,
  17. .arch = IH_ARCH_PPC,
  18. .type = IH_TYPE_KERNEL,
  19. .comp = IH_COMP_GZIP,
  20. .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
  21. .imagename = "",
  22. .imagename2 = "",
  23. };
  24. static int h_compare_image_name(const void *vtype1, const void *vtype2)
  25. {
  26. const int *type1 = vtype1;
  27. const int *type2 = vtype2;
  28. const char *name1 = genimg_get_type_short_name(*type1);
  29. const char *name2 = genimg_get_type_short_name(*type2);
  30. return strcmp(name1, name2);
  31. }
  32. /* Show all image types supported by mkimage */
  33. static void show_image_types(void)
  34. {
  35. struct image_type_params *tparams;
  36. int order[IH_TYPE_COUNT];
  37. int count;
  38. int type;
  39. int i;
  40. /* Sort the names in order of short name for easier reading */
  41. memset(order, '\0', sizeof(order));
  42. for (count = 0, type = 0; type < IH_TYPE_COUNT; type++) {
  43. tparams = imagetool_get_type(type);
  44. if (tparams)
  45. order[count++] = type;
  46. }
  47. qsort(order, count, sizeof(int), h_compare_image_name);
  48. fprintf(stderr, "\nInvalid image type. Supported image types:\n");
  49. for (i = 0; i < count; i++) {
  50. type = order[i];
  51. tparams = imagetool_get_type(type);
  52. if (tparams) {
  53. fprintf(stderr, "\t%-15s %s\n",
  54. genimg_get_type_short_name(type),
  55. genimg_get_type_name(type));
  56. }
  57. }
  58. fprintf(stderr, "\n");
  59. }
  60. static void usage(const char *msg)
  61. {
  62. fprintf(stderr, "Error: %s\n", msg);
  63. fprintf(stderr, "Usage: %s -l image\n"
  64. " -l ==> list image header information\n",
  65. params.cmdname);
  66. fprintf(stderr,
  67. " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
  68. " -A ==> set architecture to 'arch'\n"
  69. " -O ==> set operating system to 'os'\n"
  70. " -T ==> set image type to 'type'\n"
  71. " -C ==> set compression type 'comp'\n"
  72. " -a ==> set load address to 'addr' (hex)\n"
  73. " -e ==> set entry point to 'ep' (hex)\n"
  74. " -n ==> set image name to 'name'\n"
  75. " -d ==> use image data from 'datafile'\n"
  76. " -x ==> set XIP (execute in place)\n",
  77. params.cmdname);
  78. fprintf(stderr,
  79. " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] fit-image\n"
  80. " <dtb> file is used with -f auto, it may occour multiple times.\n",
  81. params.cmdname);
  82. fprintf(stderr,
  83. " -D => set all options for device tree compiler\n"
  84. " -f => input filename for FIT source\n");
  85. #ifdef CONFIG_FIT_SIGNATURE
  86. fprintf(stderr,
  87. "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r]\n"
  88. " -E => place data outside of the FIT structure\n"
  89. " -k => set directory containing private keys\n"
  90. " -K => write public keys to this .dtb file\n"
  91. " -c => add comment in signature node\n"
  92. " -F => re-sign existing FIT image\n"
  93. " -p => place external data at a static position\n"
  94. " -r => mark keys used as 'required' in dtb\n");
  95. #else
  96. fprintf(stderr,
  97. "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
  98. #endif
  99. fprintf(stderr, " %s -V ==> print version information and exit\n",
  100. params.cmdname);
  101. fprintf(stderr, "Use -T to see a list of available image types\n");
  102. exit(EXIT_FAILURE);
  103. }
  104. static int add_content(int type, const char *fname)
  105. {
  106. struct content_info *cont;
  107. cont = calloc(1, sizeof(*cont));
  108. if (!cont)
  109. return -1;
  110. cont->type = type;
  111. cont->fname = fname;
  112. if (params.content_tail)
  113. params.content_tail->next = cont;
  114. else
  115. params.content_head = cont;
  116. params.content_tail = cont;
  117. return 0;
  118. }
  119. static void process_args(int argc, char **argv)
  120. {
  121. char *ptr;
  122. int type = IH_TYPE_INVALID;
  123. char *datafile = NULL;
  124. int opt;
  125. while ((opt = getopt(argc, argv,
  126. "a:A:b:cC:d:D:e:Ef:Fk:K:ln:p:O:rR:qsT:vVx")) != -1) {
  127. switch (opt) {
  128. case 'a':
  129. params.addr = strtoull(optarg, &ptr, 16);
  130. if (*ptr) {
  131. fprintf(stderr, "%s: invalid load address %s\n",
  132. params.cmdname, optarg);
  133. exit(EXIT_FAILURE);
  134. }
  135. break;
  136. case 'A':
  137. params.arch = genimg_get_arch_id(optarg);
  138. if (params.arch < 0)
  139. usage("Invalid architecture");
  140. break;
  141. case 'b':
  142. if (add_content(IH_TYPE_FLATDT, optarg)) {
  143. fprintf(stderr,
  144. "%s: Out of memory adding content '%s'",
  145. params.cmdname, optarg);
  146. exit(EXIT_FAILURE);
  147. }
  148. break;
  149. case 'c':
  150. params.comment = optarg;
  151. break;
  152. case 'C':
  153. params.comp = genimg_get_comp_id(optarg);
  154. if (params.comp < 0)
  155. usage("Invalid compression type");
  156. break;
  157. case 'd':
  158. params.datafile = optarg;
  159. params.dflag = 1;
  160. break;
  161. case 'D':
  162. params.dtc = optarg;
  163. break;
  164. case 'e':
  165. params.ep = strtoull(optarg, &ptr, 16);
  166. if (*ptr) {
  167. fprintf(stderr, "%s: invalid entry point %s\n",
  168. params.cmdname, optarg);
  169. exit(EXIT_FAILURE);
  170. }
  171. params.eflag = 1;
  172. break;
  173. case 'E':
  174. params.external_data = true;
  175. break;
  176. case 'f':
  177. datafile = optarg;
  178. params.auto_its = !strcmp(datafile, "auto");
  179. /* no break */
  180. case 'F':
  181. /*
  182. * The flattened image tree (FIT) format
  183. * requires a flattened device tree image type
  184. */
  185. params.fit_image_type = params.type;
  186. params.type = IH_TYPE_FLATDT;
  187. params.fflag = 1;
  188. break;
  189. case 'k':
  190. params.keydir = optarg;
  191. break;
  192. case 'K':
  193. params.keydest = optarg;
  194. break;
  195. case 'l':
  196. params.lflag = 1;
  197. break;
  198. case 'n':
  199. params.imagename = optarg;
  200. break;
  201. case 'O':
  202. params.os = genimg_get_os_id(optarg);
  203. if (params.os < 0)
  204. usage("Invalid operating system");
  205. break;
  206. case 'p':
  207. params.external_offset = strtoull(optarg, &ptr, 16);
  208. if (*ptr) {
  209. fprintf(stderr, "%s: invalid offset size %s\n",
  210. params.cmdname, optarg);
  211. exit(EXIT_FAILURE);
  212. }
  213. case 'q':
  214. params.quiet = 1;
  215. break;
  216. case 'r':
  217. params.require_keys = 1;
  218. break;
  219. case 'R':
  220. /*
  221. * This entry is for the second configuration
  222. * file, if only one is not enough.
  223. */
  224. params.imagename2 = optarg;
  225. break;
  226. case 's':
  227. params.skipcpy = 1;
  228. break;
  229. case 'T':
  230. type = genimg_get_type_id(optarg);
  231. if (type < 0) {
  232. show_image_types();
  233. usage("Invalid image type");
  234. }
  235. break;
  236. case 'v':
  237. params.vflag++;
  238. break;
  239. case 'V':
  240. printf("mkimage version %s\n", PLAIN_VERSION);
  241. exit(EXIT_SUCCESS);
  242. case 'x':
  243. params.xflag++;
  244. break;
  245. default:
  246. usage("Invalid option");
  247. }
  248. }
  249. /* The last parameter is expected to be the imagefile */
  250. if (optind < argc)
  251. params.imagefile = argv[optind];
  252. /*
  253. * For auto-generated FIT images we need to know the image type to put
  254. * in the FIT, which is separate from the file's image type (which
  255. * will always be IH_TYPE_FLATDT in this case).
  256. */
  257. if (params.type == IH_TYPE_FLATDT) {
  258. params.fit_image_type = type;
  259. if (!params.auto_its)
  260. params.datafile = datafile;
  261. } else if (type != IH_TYPE_INVALID) {
  262. params.type = type;
  263. }
  264. if (!params.imagefile)
  265. usage("Missing output filename");
  266. }
  267. int main(int argc, char **argv)
  268. {
  269. int ifd = -1;
  270. struct stat sbuf;
  271. char *ptr;
  272. int retval = 0;
  273. struct image_type_params *tparams = NULL;
  274. int pad_len = 0;
  275. int dfd;
  276. params.cmdname = *argv;
  277. params.addr = 0;
  278. params.ep = 0;
  279. process_args(argc, argv);
  280. /* set tparams as per input type_id */
  281. tparams = imagetool_get_type(params.type);
  282. if (tparams == NULL) {
  283. fprintf (stderr, "%s: unsupported type %s\n",
  284. params.cmdname, genimg_get_type_name(params.type));
  285. exit (EXIT_FAILURE);
  286. }
  287. /*
  288. * check the passed arguments parameters meets the requirements
  289. * as per image type to be generated/listed
  290. */
  291. if (tparams->check_params)
  292. if (tparams->check_params (&params))
  293. usage("Bad parameters for image type");
  294. if (!params.eflag) {
  295. params.ep = params.addr;
  296. /* If XIP, entry point must be after the U-Boot header */
  297. if (params.xflag)
  298. params.ep += tparams->header_size;
  299. }
  300. if (params.fflag){
  301. if (tparams->fflag_handle)
  302. /*
  303. * in some cases, some additional processing needs
  304. * to be done if fflag is defined
  305. *
  306. * For ex. fit_handle_file for Fit file support
  307. */
  308. retval = tparams->fflag_handle(&params);
  309. if (retval != EXIT_SUCCESS)
  310. exit (retval);
  311. }
  312. if (params.lflag || params.fflag) {
  313. ifd = open (params.imagefile, O_RDONLY|O_BINARY);
  314. } else {
  315. ifd = open (params.imagefile,
  316. O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
  317. }
  318. if (ifd < 0) {
  319. fprintf (stderr, "%s: Can't open %s: %s\n",
  320. params.cmdname, params.imagefile,
  321. strerror(errno));
  322. exit (EXIT_FAILURE);
  323. }
  324. if (params.lflag || params.fflag) {
  325. /*
  326. * list header information of existing image
  327. */
  328. if (fstat(ifd, &sbuf) < 0) {
  329. fprintf (stderr, "%s: Can't stat %s: %s\n",
  330. params.cmdname, params.imagefile,
  331. strerror(errno));
  332. exit (EXIT_FAILURE);
  333. }
  334. if ((unsigned)sbuf.st_size < tparams->header_size) {
  335. fprintf (stderr,
  336. "%s: Bad size: \"%s\" is not valid image\n",
  337. params.cmdname, params.imagefile);
  338. exit (EXIT_FAILURE);
  339. }
  340. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  341. if (ptr == MAP_FAILED) {
  342. fprintf (stderr, "%s: Can't read %s: %s\n",
  343. params.cmdname, params.imagefile,
  344. strerror(errno));
  345. exit (EXIT_FAILURE);
  346. }
  347. /*
  348. * scan through mkimage registry for all supported image types
  349. * and verify the input image file header for match
  350. * Print the image information for matched image type
  351. * Returns the error code if not matched
  352. */
  353. retval = imagetool_verify_print_header(ptr, &sbuf,
  354. tparams, &params);
  355. (void) munmap((void *)ptr, sbuf.st_size);
  356. (void) close (ifd);
  357. exit (retval);
  358. }
  359. if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
  360. dfd = open(params.datafile, O_RDONLY | O_BINARY);
  361. if (dfd < 0) {
  362. fprintf(stderr, "%s: Can't open %s: %s\n",
  363. params.cmdname, params.datafile,
  364. strerror(errno));
  365. exit(EXIT_FAILURE);
  366. }
  367. if (fstat(dfd, &sbuf) < 0) {
  368. fprintf(stderr, "%s: Can't stat %s: %s\n",
  369. params.cmdname, params.datafile,
  370. strerror(errno));
  371. exit(EXIT_FAILURE);
  372. }
  373. params.file_size = sbuf.st_size + tparams->header_size;
  374. close(dfd);
  375. }
  376. /*
  377. * In case there an header with a variable
  378. * length will be added, the corresponding
  379. * function is called. This is responsible to
  380. * allocate memory for the header itself.
  381. */
  382. if (tparams->vrec_header)
  383. pad_len = tparams->vrec_header(&params, tparams);
  384. else
  385. memset(tparams->hdr, 0, tparams->header_size);
  386. if (write(ifd, tparams->hdr, tparams->header_size)
  387. != tparams->header_size) {
  388. fprintf (stderr, "%s: Write error on %s: %s\n",
  389. params.cmdname, params.imagefile, strerror(errno));
  390. exit (EXIT_FAILURE);
  391. }
  392. if (!params.skipcpy) {
  393. if (params.type == IH_TYPE_MULTI ||
  394. params.type == IH_TYPE_SCRIPT) {
  395. char *file = params.datafile;
  396. uint32_t size;
  397. for (;;) {
  398. char *sep = NULL;
  399. if (file) {
  400. if ((sep = strchr(file, ':')) != NULL) {
  401. *sep = '\0';
  402. }
  403. if (stat (file, &sbuf) < 0) {
  404. fprintf (stderr, "%s: Can't stat %s: %s\n",
  405. params.cmdname, file, strerror(errno));
  406. exit (EXIT_FAILURE);
  407. }
  408. size = cpu_to_uimage (sbuf.st_size);
  409. } else {
  410. size = 0;
  411. }
  412. if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
  413. fprintf (stderr, "%s: Write error on %s: %s\n",
  414. params.cmdname, params.imagefile,
  415. strerror(errno));
  416. exit (EXIT_FAILURE);
  417. }
  418. if (!file) {
  419. break;
  420. }
  421. if (sep) {
  422. *sep = ':';
  423. file = sep + 1;
  424. } else {
  425. file = NULL;
  426. }
  427. }
  428. file = params.datafile;
  429. for (;;) {
  430. char *sep = strchr(file, ':');
  431. if (sep) {
  432. *sep = '\0';
  433. copy_file (ifd, file, 1);
  434. *sep++ = ':';
  435. file = sep;
  436. } else {
  437. copy_file (ifd, file, 0);
  438. break;
  439. }
  440. }
  441. } else if (params.type == IH_TYPE_PBLIMAGE) {
  442. /* PBL has special Image format, implements its' own */
  443. pbl_load_uboot(ifd, &params);
  444. } else {
  445. copy_file(ifd, params.datafile, pad_len);
  446. }
  447. }
  448. /* We're a bit of paranoid */
  449. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  450. !defined(__sun__) && \
  451. !defined(__FreeBSD__) && \
  452. !defined(__OpenBSD__) && \
  453. !defined(__APPLE__)
  454. (void) fdatasync (ifd);
  455. #else
  456. (void) fsync (ifd);
  457. #endif
  458. if (fstat(ifd, &sbuf) < 0) {
  459. fprintf (stderr, "%s: Can't stat %s: %s\n",
  460. params.cmdname, params.imagefile, strerror(errno));
  461. exit (EXIT_FAILURE);
  462. }
  463. params.file_size = sbuf.st_size;
  464. ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
  465. if (ptr == MAP_FAILED) {
  466. fprintf (stderr, "%s: Can't map %s: %s\n",
  467. params.cmdname, params.imagefile, strerror(errno));
  468. exit (EXIT_FAILURE);
  469. }
  470. /* Setup the image header as per input image type*/
  471. if (tparams->set_header)
  472. tparams->set_header (ptr, &sbuf, ifd, &params);
  473. else {
  474. fprintf (stderr, "%s: Can't set header for %s: %s\n",
  475. params.cmdname, tparams->name, strerror(errno));
  476. exit (EXIT_FAILURE);
  477. }
  478. /* Print the image information by processing image header */
  479. if (tparams->print_header)
  480. tparams->print_header (ptr);
  481. else {
  482. fprintf (stderr, "%s: Can't print header for %s: %s\n",
  483. params.cmdname, tparams->name, strerror(errno));
  484. exit (EXIT_FAILURE);
  485. }
  486. (void) munmap((void *)ptr, sbuf.st_size);
  487. /* We're a bit of paranoid */
  488. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  489. !defined(__sun__) && \
  490. !defined(__FreeBSD__) && \
  491. !defined(__OpenBSD__) && \
  492. !defined(__APPLE__)
  493. (void) fdatasync (ifd);
  494. #else
  495. (void) fsync (ifd);
  496. #endif
  497. if (close(ifd)) {
  498. fprintf (stderr, "%s: Write error on %s: %s\n",
  499. params.cmdname, params.imagefile, strerror(errno));
  500. exit (EXIT_FAILURE);
  501. }
  502. exit (EXIT_SUCCESS);
  503. }
  504. static void
  505. copy_file (int ifd, const char *datafile, int pad)
  506. {
  507. int dfd;
  508. struct stat sbuf;
  509. unsigned char *ptr;
  510. int tail;
  511. int zero = 0;
  512. uint8_t zeros[4096];
  513. int offset = 0;
  514. int size;
  515. struct image_type_params *tparams = imagetool_get_type(params.type);
  516. memset(zeros, 0, sizeof(zeros));
  517. if (params.vflag) {
  518. fprintf (stderr, "Adding Image %s\n", datafile);
  519. }
  520. if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
  521. fprintf (stderr, "%s: Can't open %s: %s\n",
  522. params.cmdname, datafile, strerror(errno));
  523. exit (EXIT_FAILURE);
  524. }
  525. if (fstat(dfd, &sbuf) < 0) {
  526. fprintf (stderr, "%s: Can't stat %s: %s\n",
  527. params.cmdname, datafile, strerror(errno));
  528. exit (EXIT_FAILURE);
  529. }
  530. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  531. if (ptr == MAP_FAILED) {
  532. fprintf (stderr, "%s: Can't read %s: %s\n",
  533. params.cmdname, datafile, strerror(errno));
  534. exit (EXIT_FAILURE);
  535. }
  536. if (params.xflag) {
  537. unsigned char *p = NULL;
  538. /*
  539. * XIP: do not append the image_header_t at the
  540. * beginning of the file, but consume the space
  541. * reserved for it.
  542. */
  543. if ((unsigned)sbuf.st_size < tparams->header_size) {
  544. fprintf (stderr,
  545. "%s: Bad size: \"%s\" is too small for XIP\n",
  546. params.cmdname, datafile);
  547. exit (EXIT_FAILURE);
  548. }
  549. for (p = ptr; p < ptr + tparams->header_size; p++) {
  550. if ( *p != 0xff ) {
  551. fprintf (stderr,
  552. "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
  553. params.cmdname, datafile);
  554. exit (EXIT_FAILURE);
  555. }
  556. }
  557. offset = tparams->header_size;
  558. }
  559. size = sbuf.st_size - offset;
  560. if (write(ifd, ptr + offset, size) != size) {
  561. fprintf (stderr, "%s: Write error on %s: %s\n",
  562. params.cmdname, params.imagefile, strerror(errno));
  563. exit (EXIT_FAILURE);
  564. }
  565. tail = size % 4;
  566. if ((pad == 1) && (tail != 0)) {
  567. if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
  568. fprintf (stderr, "%s: Write error on %s: %s\n",
  569. params.cmdname, params.imagefile,
  570. strerror(errno));
  571. exit (EXIT_FAILURE);
  572. }
  573. } else if (pad > 1) {
  574. while (pad > 0) {
  575. int todo = sizeof(zeros);
  576. if (todo > pad)
  577. todo = pad;
  578. if (write(ifd, (char *)&zeros, todo) != todo) {
  579. fprintf(stderr, "%s: Write error on %s: %s\n",
  580. params.cmdname, params.imagefile,
  581. strerror(errno));
  582. exit(EXIT_FAILURE);
  583. }
  584. pad -= todo;
  585. }
  586. }
  587. (void) munmap((void *)ptr, sbuf.st_size);
  588. (void) close (dfd);
  589. }