mkimage.c 14 KB

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