mkimage.c 14 KB

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