mkimage.c 16 KB

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