mkimage.c 14 KB

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