mkimage.c 14 KB

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