mkimage.c 14 KB

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