mkimage.c 14 KB

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