mkimage.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2009
  5. * DENX Software Engineering
  6. * Wolfgang Denk, wd@denx.de
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include "mkimage.h"
  11. #include <image.h>
  12. #include <version.h>
  13. static void copy_file(int, const char *, int);
  14. static void usage(void);
  15. /* image_type_params link list to maintain registered image type supports */
  16. struct image_type_params *mkimage_tparams = NULL;
  17. /* parameters initialized by core will be used by the image type code */
  18. struct image_tool_params params = {
  19. .os = IH_OS_LINUX,
  20. .arch = IH_ARCH_PPC,
  21. .type = IH_TYPE_KERNEL,
  22. .comp = IH_COMP_GZIP,
  23. .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
  24. .imagename = "",
  25. .imagename2 = "",
  26. };
  27. int
  28. main (int argc, char **argv)
  29. {
  30. int ifd = -1;
  31. struct stat sbuf;
  32. char *ptr;
  33. int retval = 0;
  34. struct image_type_params *tparams = NULL;
  35. int pad_len = 0;
  36. params.cmdname = *argv;
  37. params.addr = params.ep = 0;
  38. while (--argc > 0 && **++argv == '-') {
  39. while (*++*argv) {
  40. switch (**argv) {
  41. case 'l':
  42. params.lflag = 1;
  43. break;
  44. case 'A':
  45. if ((--argc <= 0) ||
  46. (params.arch =
  47. genimg_get_arch_id (*++argv)) < 0)
  48. usage ();
  49. goto NXTARG;
  50. case 'c':
  51. if (--argc <= 0)
  52. usage();
  53. params.comment = *++argv;
  54. goto NXTARG;
  55. case 'C':
  56. if ((--argc <= 0) ||
  57. (params.comp =
  58. genimg_get_comp_id (*++argv)) < 0)
  59. usage ();
  60. goto NXTARG;
  61. case 'D':
  62. if (--argc <= 0)
  63. usage ();
  64. params.dtc = *++argv;
  65. goto NXTARG;
  66. case 'O':
  67. if ((--argc <= 0) ||
  68. (params.os =
  69. genimg_get_os_id (*++argv)) < 0)
  70. usage ();
  71. goto NXTARG;
  72. case 'T':
  73. if ((--argc <= 0) ||
  74. (params.type =
  75. genimg_get_type_id (*++argv)) < 0)
  76. usage ();
  77. goto NXTARG;
  78. case 'a':
  79. if (--argc <= 0)
  80. usage ();
  81. params.addr = strtoul (*++argv, &ptr, 16);
  82. if (*ptr) {
  83. fprintf (stderr,
  84. "%s: invalid load address %s\n",
  85. params.cmdname, *argv);
  86. exit (EXIT_FAILURE);
  87. }
  88. goto NXTARG;
  89. case 'd':
  90. if (--argc <= 0)
  91. usage ();
  92. params.datafile = *++argv;
  93. params.dflag = 1;
  94. goto NXTARG;
  95. case 'e':
  96. if (--argc <= 0)
  97. usage ();
  98. params.ep = strtoul (*++argv, &ptr, 16);
  99. if (*ptr) {
  100. fprintf (stderr,
  101. "%s: invalid entry point %s\n",
  102. params.cmdname, *argv);
  103. exit (EXIT_FAILURE);
  104. }
  105. params.eflag = 1;
  106. goto NXTARG;
  107. case 'f':
  108. if (--argc <= 0)
  109. usage ();
  110. params.datafile = *++argv;
  111. /* no break */
  112. case 'F':
  113. /*
  114. * The flattened image tree (FIT) format
  115. * requires a flattened device tree image type
  116. */
  117. params.type = IH_TYPE_FLATDT;
  118. params.fflag = 1;
  119. goto NXTARG;
  120. case 'k':
  121. if (--argc <= 0)
  122. usage();
  123. params.keydir = *++argv;
  124. goto NXTARG;
  125. case 'K':
  126. if (--argc <= 0)
  127. usage();
  128. params.keydest = *++argv;
  129. goto NXTARG;
  130. case 'n':
  131. if (--argc <= 0)
  132. usage ();
  133. params.imagename = *++argv;
  134. goto NXTARG;
  135. case 'r':
  136. params.require_keys = 1;
  137. break;
  138. case 'R':
  139. if (--argc <= 0)
  140. usage();
  141. /*
  142. * This entry is for the second configuration
  143. * file, if only one is not enough.
  144. */
  145. params.imagename2 = *++argv;
  146. goto NXTARG;
  147. case 's':
  148. params.skipcpy = 1;
  149. break;
  150. case 'v':
  151. params.vflag++;
  152. break;
  153. case 'V':
  154. printf("mkimage version %s\n", PLAIN_VERSION);
  155. exit(EXIT_SUCCESS);
  156. case 'x':
  157. params.xflag++;
  158. break;
  159. default:
  160. usage ();
  161. }
  162. }
  163. NXTARG: ;
  164. }
  165. if (argc != 1)
  166. usage ();
  167. /* set tparams as per input type_id */
  168. tparams = imagetool_get_type(params.type);
  169. if (tparams == NULL) {
  170. fprintf (stderr, "%s: unsupported type %s\n",
  171. params.cmdname, genimg_get_type_name(params.type));
  172. exit (EXIT_FAILURE);
  173. }
  174. /*
  175. * check the passed arguments parameters meets the requirements
  176. * as per image type to be generated/listed
  177. */
  178. if (tparams->check_params)
  179. if (tparams->check_params (&params))
  180. usage ();
  181. if (!params.eflag) {
  182. params.ep = params.addr;
  183. /* If XIP, entry point must be after the U-Boot header */
  184. if (params.xflag)
  185. params.ep += tparams->header_size;
  186. }
  187. params.imagefile = *argv;
  188. if (params.fflag){
  189. if (tparams->fflag_handle)
  190. /*
  191. * in some cases, some additional processing needs
  192. * to be done if fflag is defined
  193. *
  194. * For ex. fit_handle_file for Fit file support
  195. */
  196. retval = tparams->fflag_handle(&params);
  197. if (retval != EXIT_SUCCESS)
  198. exit (retval);
  199. }
  200. if (params.lflag || params.fflag) {
  201. ifd = open (params.imagefile, O_RDONLY|O_BINARY);
  202. } else {
  203. ifd = open (params.imagefile,
  204. O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
  205. }
  206. if (ifd < 0) {
  207. fprintf (stderr, "%s: Can't open %s: %s\n",
  208. params.cmdname, params.imagefile,
  209. strerror(errno));
  210. exit (EXIT_FAILURE);
  211. }
  212. if (params.lflag || params.fflag) {
  213. /*
  214. * list header information of existing image
  215. */
  216. if (fstat(ifd, &sbuf) < 0) {
  217. fprintf (stderr, "%s: Can't stat %s: %s\n",
  218. params.cmdname, params.imagefile,
  219. strerror(errno));
  220. exit (EXIT_FAILURE);
  221. }
  222. if ((unsigned)sbuf.st_size < tparams->header_size) {
  223. fprintf (stderr,
  224. "%s: Bad size: \"%s\" is not valid image\n",
  225. params.cmdname, params.imagefile);
  226. exit (EXIT_FAILURE);
  227. }
  228. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  229. if (ptr == MAP_FAILED) {
  230. fprintf (stderr, "%s: Can't read %s: %s\n",
  231. params.cmdname, params.imagefile,
  232. strerror(errno));
  233. exit (EXIT_FAILURE);
  234. }
  235. /*
  236. * scan through mkimage registry for all supported image types
  237. * and verify the input image file header for match
  238. * Print the image information for matched image type
  239. * Returns the error code if not matched
  240. */
  241. retval = imagetool_verify_print_header(ptr, &sbuf,
  242. tparams, &params);
  243. (void) munmap((void *)ptr, sbuf.st_size);
  244. (void) close (ifd);
  245. exit (retval);
  246. }
  247. /*
  248. * In case there an header with a variable
  249. * length will be added, the corresponding
  250. * function is called. This is responsible to
  251. * allocate memory for the header itself.
  252. */
  253. if (tparams->vrec_header)
  254. pad_len = tparams->vrec_header(&params, tparams);
  255. else
  256. memset(tparams->hdr, 0, tparams->header_size);
  257. if (write(ifd, tparams->hdr, tparams->header_size)
  258. != tparams->header_size) {
  259. fprintf (stderr, "%s: Write error on %s: %s\n",
  260. params.cmdname, params.imagefile, strerror(errno));
  261. exit (EXIT_FAILURE);
  262. }
  263. if (!params.skipcpy) {
  264. if (params.type == IH_TYPE_MULTI ||
  265. params.type == IH_TYPE_SCRIPT) {
  266. char *file = params.datafile;
  267. uint32_t size;
  268. for (;;) {
  269. char *sep = NULL;
  270. if (file) {
  271. if ((sep = strchr(file, ':')) != NULL) {
  272. *sep = '\0';
  273. }
  274. if (stat (file, &sbuf) < 0) {
  275. fprintf (stderr, "%s: Can't stat %s: %s\n",
  276. params.cmdname, file, strerror(errno));
  277. exit (EXIT_FAILURE);
  278. }
  279. size = cpu_to_uimage (sbuf.st_size);
  280. } else {
  281. size = 0;
  282. }
  283. if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
  284. fprintf (stderr, "%s: Write error on %s: %s\n",
  285. params.cmdname, params.imagefile,
  286. strerror(errno));
  287. exit (EXIT_FAILURE);
  288. }
  289. if (!file) {
  290. break;
  291. }
  292. if (sep) {
  293. *sep = ':';
  294. file = sep + 1;
  295. } else {
  296. file = NULL;
  297. }
  298. }
  299. file = params.datafile;
  300. for (;;) {
  301. char *sep = strchr(file, ':');
  302. if (sep) {
  303. *sep = '\0';
  304. copy_file (ifd, file, 1);
  305. *sep++ = ':';
  306. file = sep;
  307. } else {
  308. copy_file (ifd, file, 0);
  309. break;
  310. }
  311. }
  312. } else if (params.type == IH_TYPE_PBLIMAGE) {
  313. /* PBL has special Image format, implements its' own */
  314. pbl_load_uboot(ifd, &params);
  315. } else {
  316. copy_file(ifd, params.datafile, pad_len);
  317. }
  318. }
  319. /* We're a bit of paranoid */
  320. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  321. !defined(__sun__) && \
  322. !defined(__FreeBSD__) && \
  323. !defined(__OpenBSD__) && \
  324. !defined(__APPLE__)
  325. (void) fdatasync (ifd);
  326. #else
  327. (void) fsync (ifd);
  328. #endif
  329. if (fstat(ifd, &sbuf) < 0) {
  330. fprintf (stderr, "%s: Can't stat %s: %s\n",
  331. params.cmdname, params.imagefile, strerror(errno));
  332. exit (EXIT_FAILURE);
  333. }
  334. ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
  335. if (ptr == MAP_FAILED) {
  336. fprintf (stderr, "%s: Can't map %s: %s\n",
  337. params.cmdname, params.imagefile, strerror(errno));
  338. exit (EXIT_FAILURE);
  339. }
  340. /* Setup the image header as per input image type*/
  341. if (tparams->set_header)
  342. tparams->set_header (ptr, &sbuf, ifd, &params);
  343. else {
  344. fprintf (stderr, "%s: Can't set header for %s: %s\n",
  345. params.cmdname, tparams->name, strerror(errno));
  346. exit (EXIT_FAILURE);
  347. }
  348. /* Print the image information by processing image header */
  349. if (tparams->print_header)
  350. tparams->print_header (ptr);
  351. else {
  352. fprintf (stderr, "%s: Can't print header for %s: %s\n",
  353. params.cmdname, tparams->name, strerror(errno));
  354. exit (EXIT_FAILURE);
  355. }
  356. (void) munmap((void *)ptr, sbuf.st_size);
  357. /* We're a bit of paranoid */
  358. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  359. !defined(__sun__) && \
  360. !defined(__FreeBSD__) && \
  361. !defined(__OpenBSD__) && \
  362. !defined(__APPLE__)
  363. (void) fdatasync (ifd);
  364. #else
  365. (void) fsync (ifd);
  366. #endif
  367. if (close(ifd)) {
  368. fprintf (stderr, "%s: Write error on %s: %s\n",
  369. params.cmdname, params.imagefile, strerror(errno));
  370. exit (EXIT_FAILURE);
  371. }
  372. exit (EXIT_SUCCESS);
  373. }
  374. static void
  375. copy_file (int ifd, const char *datafile, int pad)
  376. {
  377. int dfd;
  378. struct stat sbuf;
  379. unsigned char *ptr;
  380. int tail;
  381. int zero = 0;
  382. uint8_t zeros[4096];
  383. int offset = 0;
  384. int size;
  385. struct image_type_params *tparams = imagetool_get_type(params.type);
  386. if (pad >= sizeof(zeros)) {
  387. fprintf(stderr, "%s: Can't pad to %d\n",
  388. params.cmdname, pad);
  389. exit(EXIT_FAILURE);
  390. }
  391. memset(zeros, 0, sizeof(zeros));
  392. if (params.vflag) {
  393. fprintf (stderr, "Adding Image %s\n", datafile);
  394. }
  395. if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
  396. fprintf (stderr, "%s: Can't open %s: %s\n",
  397. params.cmdname, datafile, strerror(errno));
  398. exit (EXIT_FAILURE);
  399. }
  400. if (fstat(dfd, &sbuf) < 0) {
  401. fprintf (stderr, "%s: Can't stat %s: %s\n",
  402. params.cmdname, datafile, strerror(errno));
  403. exit (EXIT_FAILURE);
  404. }
  405. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  406. if (ptr == MAP_FAILED) {
  407. fprintf (stderr, "%s: Can't read %s: %s\n",
  408. params.cmdname, datafile, strerror(errno));
  409. exit (EXIT_FAILURE);
  410. }
  411. if (params.xflag) {
  412. unsigned char *p = NULL;
  413. /*
  414. * XIP: do not append the image_header_t at the
  415. * beginning of the file, but consume the space
  416. * reserved for it.
  417. */
  418. if ((unsigned)sbuf.st_size < tparams->header_size) {
  419. fprintf (stderr,
  420. "%s: Bad size: \"%s\" is too small for XIP\n",
  421. params.cmdname, datafile);
  422. exit (EXIT_FAILURE);
  423. }
  424. for (p = ptr; p < ptr + tparams->header_size; p++) {
  425. if ( *p != 0xff ) {
  426. fprintf (stderr,
  427. "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
  428. params.cmdname, datafile);
  429. exit (EXIT_FAILURE);
  430. }
  431. }
  432. offset = tparams->header_size;
  433. }
  434. size = sbuf.st_size - offset;
  435. if (write(ifd, ptr + offset, size) != size) {
  436. fprintf (stderr, "%s: Write error on %s: %s\n",
  437. params.cmdname, params.imagefile, strerror(errno));
  438. exit (EXIT_FAILURE);
  439. }
  440. tail = size % 4;
  441. if ((pad == 1) && (tail != 0)) {
  442. if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
  443. fprintf (stderr, "%s: Write error on %s: %s\n",
  444. params.cmdname, params.imagefile,
  445. strerror(errno));
  446. exit (EXIT_FAILURE);
  447. }
  448. } else if (pad > 1) {
  449. if (write(ifd, (char *)&zeros, pad) != pad) {
  450. fprintf(stderr, "%s: Write error on %s: %s\n",
  451. params.cmdname, params.imagefile,
  452. strerror(errno));
  453. exit(EXIT_FAILURE);
  454. }
  455. }
  456. (void) munmap((void *)ptr, sbuf.st_size);
  457. (void) close (dfd);
  458. }
  459. static void usage(void)
  460. {
  461. fprintf (stderr, "Usage: %s -l image\n"
  462. " -l ==> list image header information\n",
  463. params.cmdname);
  464. fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
  465. "-a addr -e ep -n name -d data_file[:data_file...] image\n"
  466. " -A ==> set architecture to 'arch'\n"
  467. " -O ==> set operating system to 'os'\n"
  468. " -T ==> set image type to 'type'\n"
  469. " -C ==> set compression type 'comp'\n"
  470. " -a ==> set load address to 'addr' (hex)\n"
  471. " -e ==> set entry point to 'ep' (hex)\n"
  472. " -n ==> set image name to 'name'\n"
  473. " -d ==> use image data from 'datafile'\n"
  474. " -x ==> set XIP (execute in place)\n",
  475. params.cmdname);
  476. fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
  477. params.cmdname);
  478. fprintf(stderr, " -D => set options for device tree compiler\n"
  479. " -f => input filename for FIT source\n");
  480. #ifdef CONFIG_FIT_SIGNATURE
  481. fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
  482. " -k => set directory containing private keys\n"
  483. " -K => write public keys to this .dtb file\n"
  484. " -c => add comment in signature node\n"
  485. " -F => re-sign existing FIT image\n"
  486. " -r => mark keys used as 'required' in dtb\n");
  487. #else
  488. fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
  489. #endif
  490. fprintf (stderr, " %s -V ==> print version information and exit\n",
  491. params.cmdname);
  492. exit (EXIT_FAILURE);
  493. }