mkimage.c 14 KB

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