mkimage.c 15 KB

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