mkimage.c 15 KB

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