mkimage.c 16 KB

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