mkimage.c 14 KB

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