mkimage.c 15 KB

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