mkimage.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. (params.comp =
  171. genimg_get_comp_id (*++argv)) < 0)
  172. usage ();
  173. goto NXTARG;
  174. case 'D':
  175. if (--argc <= 0)
  176. usage ();
  177. params.dtc = *++argv;
  178. goto NXTARG;
  179. case 'O':
  180. if ((--argc <= 0) ||
  181. (params.os =
  182. genimg_get_os_id (*++argv)) < 0)
  183. usage ();
  184. goto NXTARG;
  185. case 'T':
  186. if ((--argc <= 0) ||
  187. (params.type =
  188. genimg_get_type_id (*++argv)) < 0)
  189. usage ();
  190. goto NXTARG;
  191. case 'a':
  192. if (--argc <= 0)
  193. usage ();
  194. params.addr = strtoul (*++argv, &ptr, 16);
  195. if (*ptr) {
  196. fprintf (stderr,
  197. "%s: invalid load address %s\n",
  198. params.cmdname, *argv);
  199. exit (EXIT_FAILURE);
  200. }
  201. goto NXTARG;
  202. case 'd':
  203. if (--argc <= 0)
  204. usage ();
  205. params.datafile = *++argv;
  206. params.dflag = 1;
  207. goto NXTARG;
  208. case 'e':
  209. if (--argc <= 0)
  210. usage ();
  211. params.ep = strtoul (*++argv, &ptr, 16);
  212. if (*ptr) {
  213. fprintf (stderr,
  214. "%s: invalid entry point %s\n",
  215. params.cmdname, *argv);
  216. exit (EXIT_FAILURE);
  217. }
  218. params.eflag = 1;
  219. goto NXTARG;
  220. case 'f':
  221. if (--argc <= 0)
  222. usage ();
  223. /*
  224. * The flattened image tree (FIT) format
  225. * requires a flattened device tree image type
  226. */
  227. params.type = IH_TYPE_FLATDT;
  228. params.datafile = *++argv;
  229. params.fflag = 1;
  230. goto NXTARG;
  231. case 'n':
  232. if (--argc <= 0)
  233. usage ();
  234. params.imagename = *++argv;
  235. goto NXTARG;
  236. case 'R':
  237. if (--argc <= 0)
  238. usage();
  239. /*
  240. * This entry is for the second configuration
  241. * file, if only one is not enough.
  242. */
  243. params.imagename2 = *++argv;
  244. goto NXTARG;
  245. case 's':
  246. params.skipcpy = 1;
  247. break;
  248. case 'v':
  249. params.vflag++;
  250. break;
  251. case 'V':
  252. printf("mkimage version %s\n", PLAIN_VERSION);
  253. exit(EXIT_SUCCESS);
  254. case 'x':
  255. params.xflag++;
  256. break;
  257. default:
  258. usage ();
  259. }
  260. }
  261. NXTARG: ;
  262. }
  263. if (argc != 1)
  264. usage ();
  265. /* set tparams as per input type_id */
  266. tparams = mkimage_get_type(params.type);
  267. if (tparams == NULL) {
  268. fprintf (stderr, "%s: unsupported type %s\n",
  269. params.cmdname, genimg_get_type_name(params.type));
  270. exit (EXIT_FAILURE);
  271. }
  272. /*
  273. * check the passed arguments parameters meets the requirements
  274. * as per image type to be generated/listed
  275. */
  276. if (tparams->check_params)
  277. if (tparams->check_params (&params))
  278. usage ();
  279. if (!params.eflag) {
  280. params.ep = params.addr;
  281. /* If XIP, entry point must be after the U-Boot header */
  282. if (params.xflag)
  283. params.ep += tparams->header_size;
  284. }
  285. params.imagefile = *argv;
  286. if (params.fflag){
  287. if (tparams->fflag_handle)
  288. /*
  289. * in some cases, some additional processing needs
  290. * to be done if fflag is defined
  291. *
  292. * For ex. fit_handle_file for Fit file support
  293. */
  294. retval = tparams->fflag_handle(&params);
  295. if (retval != EXIT_SUCCESS)
  296. exit (retval);
  297. }
  298. if (params.lflag || params.fflag) {
  299. ifd = open (params.imagefile, O_RDONLY|O_BINARY);
  300. } else {
  301. ifd = open (params.imagefile,
  302. O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
  303. }
  304. if (ifd < 0) {
  305. fprintf (stderr, "%s: Can't open %s: %s\n",
  306. params.cmdname, params.imagefile,
  307. strerror(errno));
  308. exit (EXIT_FAILURE);
  309. }
  310. if (params.lflag || params.fflag) {
  311. /*
  312. * list header information of existing image
  313. */
  314. if (fstat(ifd, &sbuf) < 0) {
  315. fprintf (stderr, "%s: Can't stat %s: %s\n",
  316. params.cmdname, params.imagefile,
  317. strerror(errno));
  318. exit (EXIT_FAILURE);
  319. }
  320. if ((unsigned)sbuf.st_size < tparams->header_size) {
  321. fprintf (stderr,
  322. "%s: Bad size: \"%s\" is not valid image\n",
  323. params.cmdname, params.imagefile);
  324. exit (EXIT_FAILURE);
  325. }
  326. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  327. if (ptr == MAP_FAILED) {
  328. fprintf (stderr, "%s: Can't read %s: %s\n",
  329. params.cmdname, params.imagefile,
  330. strerror(errno));
  331. exit (EXIT_FAILURE);
  332. }
  333. /*
  334. * scan through mkimage registry for all supported image types
  335. * and verify the input image file header for match
  336. * Print the image information for matched image type
  337. * Returns the error code if not matched
  338. */
  339. retval = mkimage_verify_print_header (ptr, &sbuf);
  340. (void) munmap((void *)ptr, sbuf.st_size);
  341. (void) close (ifd);
  342. exit (retval);
  343. }
  344. /*
  345. * In case there an header with a variable
  346. * length will be added, the corresponding
  347. * function is called. This is responsible to
  348. * allocate memory for the header itself.
  349. */
  350. if (tparams->vrec_header)
  351. tparams->vrec_header(&params, tparams);
  352. else
  353. memset(tparams->hdr, 0, tparams->header_size);
  354. if (write(ifd, tparams->hdr, tparams->header_size)
  355. != tparams->header_size) {
  356. fprintf (stderr, "%s: Write error on %s: %s\n",
  357. params.cmdname, params.imagefile, strerror(errno));
  358. exit (EXIT_FAILURE);
  359. }
  360. if (!params.skipcpy) {
  361. if (params.type == IH_TYPE_MULTI ||
  362. params.type == IH_TYPE_SCRIPT) {
  363. char *file = params.datafile;
  364. uint32_t size;
  365. for (;;) {
  366. char *sep = NULL;
  367. if (file) {
  368. if ((sep = strchr(file, ':')) != NULL) {
  369. *sep = '\0';
  370. }
  371. if (stat (file, &sbuf) < 0) {
  372. fprintf (stderr, "%s: Can't stat %s: %s\n",
  373. params.cmdname, file, strerror(errno));
  374. exit (EXIT_FAILURE);
  375. }
  376. size = cpu_to_uimage (sbuf.st_size);
  377. } else {
  378. size = 0;
  379. }
  380. if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
  381. fprintf (stderr, "%s: Write error on %s: %s\n",
  382. params.cmdname, params.imagefile,
  383. strerror(errno));
  384. exit (EXIT_FAILURE);
  385. }
  386. if (!file) {
  387. break;
  388. }
  389. if (sep) {
  390. *sep = ':';
  391. file = sep + 1;
  392. } else {
  393. file = NULL;
  394. }
  395. }
  396. file = params.datafile;
  397. for (;;) {
  398. char *sep = strchr(file, ':');
  399. if (sep) {
  400. *sep = '\0';
  401. copy_file (ifd, file, 1);
  402. *sep++ = ':';
  403. file = sep;
  404. } else {
  405. copy_file (ifd, file, 0);
  406. break;
  407. }
  408. }
  409. } else if (params.type == IH_TYPE_PBLIMAGE) {
  410. /* PBL has special Image format, implements its' own */
  411. pbl_load_uboot(ifd, &params);
  412. } else {
  413. copy_file (ifd, params.datafile, 0);
  414. }
  415. }
  416. /* We're a bit of paranoid */
  417. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  418. !defined(__sun__) && \
  419. !defined(__FreeBSD__) && \
  420. !defined(__APPLE__)
  421. (void) fdatasync (ifd);
  422. #else
  423. (void) fsync (ifd);
  424. #endif
  425. if (fstat(ifd, &sbuf) < 0) {
  426. fprintf (stderr, "%s: Can't stat %s: %s\n",
  427. params.cmdname, params.imagefile, strerror(errno));
  428. exit (EXIT_FAILURE);
  429. }
  430. ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
  431. if (ptr == MAP_FAILED) {
  432. fprintf (stderr, "%s: Can't map %s: %s\n",
  433. params.cmdname, params.imagefile, strerror(errno));
  434. exit (EXIT_FAILURE);
  435. }
  436. /* Setup the image header as per input image type*/
  437. if (tparams->set_header)
  438. tparams->set_header (ptr, &sbuf, ifd, &params);
  439. else {
  440. fprintf (stderr, "%s: Can't set header for %s: %s\n",
  441. params.cmdname, tparams->name, strerror(errno));
  442. exit (EXIT_FAILURE);
  443. }
  444. /* Print the image information by processing image header */
  445. if (tparams->print_header)
  446. tparams->print_header (ptr);
  447. else {
  448. fprintf (stderr, "%s: Can't print header for %s: %s\n",
  449. params.cmdname, tparams->name, strerror(errno));
  450. exit (EXIT_FAILURE);
  451. }
  452. (void) munmap((void *)ptr, sbuf.st_size);
  453. /* We're a bit of paranoid */
  454. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  455. !defined(__sun__) && \
  456. !defined(__FreeBSD__) && \
  457. !defined(__APPLE__)
  458. (void) fdatasync (ifd);
  459. #else
  460. (void) fsync (ifd);
  461. #endif
  462. if (close(ifd)) {
  463. fprintf (stderr, "%s: Write error on %s: %s\n",
  464. params.cmdname, params.imagefile, strerror(errno));
  465. exit (EXIT_FAILURE);
  466. }
  467. exit (EXIT_SUCCESS);
  468. }
  469. static void
  470. copy_file (int ifd, const char *datafile, int pad)
  471. {
  472. int dfd;
  473. struct stat sbuf;
  474. unsigned char *ptr;
  475. int tail;
  476. int zero = 0;
  477. int offset = 0;
  478. int size;
  479. struct image_type_params *tparams = mkimage_get_type (params.type);
  480. if (params.vflag) {
  481. fprintf (stderr, "Adding Image %s\n", datafile);
  482. }
  483. if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
  484. fprintf (stderr, "%s: Can't open %s: %s\n",
  485. params.cmdname, datafile, strerror(errno));
  486. exit (EXIT_FAILURE);
  487. }
  488. if (fstat(dfd, &sbuf) < 0) {
  489. fprintf (stderr, "%s: Can't stat %s: %s\n",
  490. params.cmdname, datafile, strerror(errno));
  491. exit (EXIT_FAILURE);
  492. }
  493. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  494. if (ptr == MAP_FAILED) {
  495. fprintf (stderr, "%s: Can't read %s: %s\n",
  496. params.cmdname, datafile, strerror(errno));
  497. exit (EXIT_FAILURE);
  498. }
  499. if (params.xflag) {
  500. unsigned char *p = NULL;
  501. /*
  502. * XIP: do not append the image_header_t at the
  503. * beginning of the file, but consume the space
  504. * reserved for it.
  505. */
  506. if ((unsigned)sbuf.st_size < tparams->header_size) {
  507. fprintf (stderr,
  508. "%s: Bad size: \"%s\" is too small for XIP\n",
  509. params.cmdname, datafile);
  510. exit (EXIT_FAILURE);
  511. }
  512. for (p = ptr; p < ptr + tparams->header_size; p++) {
  513. if ( *p != 0xff ) {
  514. fprintf (stderr,
  515. "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
  516. params.cmdname, datafile);
  517. exit (EXIT_FAILURE);
  518. }
  519. }
  520. offset = tparams->header_size;
  521. }
  522. size = sbuf.st_size - offset;
  523. if (write(ifd, ptr + offset, size) != size) {
  524. fprintf (stderr, "%s: Write error on %s: %s\n",
  525. params.cmdname, params.imagefile, strerror(errno));
  526. exit (EXIT_FAILURE);
  527. }
  528. if (pad && ((tail = size % 4) != 0)) {
  529. if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
  530. fprintf (stderr, "%s: Write error on %s: %s\n",
  531. params.cmdname, params.imagefile,
  532. strerror(errno));
  533. exit (EXIT_FAILURE);
  534. }
  535. }
  536. (void) munmap((void *)ptr, sbuf.st_size);
  537. (void) close (dfd);
  538. }
  539. void
  540. usage ()
  541. {
  542. fprintf (stderr, "Usage: %s -l image\n"
  543. " -l ==> list image header information\n",
  544. params.cmdname);
  545. fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
  546. "-a addr -e ep -n name -d data_file[:data_file...] image\n"
  547. " -A ==> set architecture to 'arch'\n"
  548. " -O ==> set operating system to 'os'\n"
  549. " -T ==> set image type to 'type'\n"
  550. " -C ==> set compression type 'comp'\n"
  551. " -a ==> set load address to 'addr' (hex)\n"
  552. " -e ==> set entry point to 'ep' (hex)\n"
  553. " -n ==> set image name to 'name'\n"
  554. " -d ==> use image data from 'datafile'\n"
  555. " -x ==> set XIP (execute in place)\n",
  556. params.cmdname);
  557. fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
  558. params.cmdname);
  559. fprintf (stderr, " %s -V ==> print version information and exit\n",
  560. params.cmdname);
  561. exit (EXIT_FAILURE);
  562. }