mkimage.c 15 KB

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