mkimage.c 16 KB

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