mkimage.c 14 KB

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