mkimage.c 14 KB

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