mkimage.c 14 KB

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