mkimage.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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:sT: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 'r':
  205. params.require_keys = 1;
  206. break;
  207. case 'R':
  208. /*
  209. * This entry is for the second configuration
  210. * file, if only one is not enough.
  211. */
  212. params.imagename2 = optarg;
  213. break;
  214. case 's':
  215. params.skipcpy = 1;
  216. break;
  217. case 'T':
  218. type = genimg_get_type_id(optarg);
  219. if (type < 0) {
  220. show_image_types();
  221. usage("Invalid image type");
  222. }
  223. break;
  224. case 'v':
  225. params.vflag++;
  226. break;
  227. case 'V':
  228. printf("mkimage version %s\n", PLAIN_VERSION);
  229. exit(EXIT_SUCCESS);
  230. case 'x':
  231. params.xflag++;
  232. break;
  233. default:
  234. usage("Invalid option");
  235. }
  236. }
  237. /* The last parameter is expected to be the imagefile */
  238. if (optind < argc)
  239. params.imagefile = argv[optind];
  240. /*
  241. * For auto-generated FIT images we need to know the image type to put
  242. * in the FIT, which is separate from the file's image type (which
  243. * will always be IH_TYPE_FLATDT in this case).
  244. */
  245. if (params.type == IH_TYPE_FLATDT) {
  246. params.fit_image_type = type;
  247. if (!params.auto_its)
  248. params.datafile = datafile;
  249. } else if (type != IH_TYPE_INVALID) {
  250. params.type = type;
  251. }
  252. if (!params.imagefile)
  253. usage("Missing output filename");
  254. }
  255. int main(int argc, char **argv)
  256. {
  257. int ifd = -1;
  258. struct stat sbuf;
  259. char *ptr;
  260. int retval = 0;
  261. struct image_type_params *tparams = NULL;
  262. int pad_len = 0;
  263. int dfd;
  264. params.cmdname = *argv;
  265. params.addr = 0;
  266. params.ep = 0;
  267. process_args(argc, argv);
  268. /* set tparams as per input type_id */
  269. tparams = imagetool_get_type(params.type);
  270. if (tparams == NULL) {
  271. fprintf (stderr, "%s: unsupported type %s\n",
  272. params.cmdname, genimg_get_type_name(params.type));
  273. exit (EXIT_FAILURE);
  274. }
  275. /*
  276. * check the passed arguments parameters meets the requirements
  277. * as per image type to be generated/listed
  278. */
  279. if (tparams->check_params)
  280. if (tparams->check_params (&params))
  281. usage("Bad parameters for image type");
  282. if (!params.eflag) {
  283. params.ep = params.addr;
  284. /* If XIP, entry point must be after the U-Boot header */
  285. if (params.xflag)
  286. params.ep += tparams->header_size;
  287. }
  288. if (params.fflag){
  289. if (tparams->fflag_handle)
  290. /*
  291. * in some cases, some additional processing needs
  292. * to be done if fflag is defined
  293. *
  294. * For ex. fit_handle_file for Fit file support
  295. */
  296. retval = tparams->fflag_handle(&params);
  297. if (retval != EXIT_SUCCESS)
  298. exit (retval);
  299. }
  300. if (params.lflag || params.fflag) {
  301. ifd = open (params.imagefile, O_RDONLY|O_BINARY);
  302. } else {
  303. ifd = open (params.imagefile,
  304. O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
  305. }
  306. if (ifd < 0) {
  307. fprintf (stderr, "%s: Can't open %s: %s\n",
  308. params.cmdname, params.imagefile,
  309. strerror(errno));
  310. exit (EXIT_FAILURE);
  311. }
  312. if (params.lflag || params.fflag) {
  313. /*
  314. * list header information of existing image
  315. */
  316. if (fstat(ifd, &sbuf) < 0) {
  317. fprintf (stderr, "%s: Can't stat %s: %s\n",
  318. params.cmdname, params.imagefile,
  319. strerror(errno));
  320. exit (EXIT_FAILURE);
  321. }
  322. if ((unsigned)sbuf.st_size < tparams->header_size) {
  323. fprintf (stderr,
  324. "%s: Bad size: \"%s\" is not valid image\n",
  325. params.cmdname, params.imagefile);
  326. exit (EXIT_FAILURE);
  327. }
  328. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  329. if (ptr == MAP_FAILED) {
  330. fprintf (stderr, "%s: Can't read %s: %s\n",
  331. params.cmdname, params.imagefile,
  332. strerror(errno));
  333. exit (EXIT_FAILURE);
  334. }
  335. /*
  336. * scan through mkimage registry for all supported image types
  337. * and verify the input image file header for match
  338. * Print the image information for matched image type
  339. * Returns the error code if not matched
  340. */
  341. retval = imagetool_verify_print_header(ptr, &sbuf,
  342. tparams, &params);
  343. (void) munmap((void *)ptr, sbuf.st_size);
  344. (void) close (ifd);
  345. exit (retval);
  346. }
  347. if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
  348. dfd = open(params.datafile, O_RDONLY | O_BINARY);
  349. if (dfd < 0) {
  350. fprintf(stderr, "%s: Can't open %s: %s\n",
  351. params.cmdname, params.datafile,
  352. strerror(errno));
  353. exit(EXIT_FAILURE);
  354. }
  355. if (fstat(dfd, &sbuf) < 0) {
  356. fprintf(stderr, "%s: Can't stat %s: %s\n",
  357. params.cmdname, params.datafile,
  358. strerror(errno));
  359. exit(EXIT_FAILURE);
  360. }
  361. params.file_size = sbuf.st_size + tparams->header_size;
  362. close(dfd);
  363. }
  364. /*
  365. * In case there an header with a variable
  366. * length will be added, the corresponding
  367. * function is called. This is responsible to
  368. * allocate memory for the header itself.
  369. */
  370. if (tparams->vrec_header)
  371. pad_len = tparams->vrec_header(&params, tparams);
  372. else
  373. memset(tparams->hdr, 0, tparams->header_size);
  374. if (write(ifd, tparams->hdr, tparams->header_size)
  375. != tparams->header_size) {
  376. fprintf (stderr, "%s: Write error on %s: %s\n",
  377. params.cmdname, params.imagefile, strerror(errno));
  378. exit (EXIT_FAILURE);
  379. }
  380. if (!params.skipcpy) {
  381. if (params.type == IH_TYPE_MULTI ||
  382. params.type == IH_TYPE_SCRIPT) {
  383. char *file = params.datafile;
  384. uint32_t size;
  385. for (;;) {
  386. char *sep = NULL;
  387. if (file) {
  388. if ((sep = strchr(file, ':')) != NULL) {
  389. *sep = '\0';
  390. }
  391. if (stat (file, &sbuf) < 0) {
  392. fprintf (stderr, "%s: Can't stat %s: %s\n",
  393. params.cmdname, file, strerror(errno));
  394. exit (EXIT_FAILURE);
  395. }
  396. size = cpu_to_uimage (sbuf.st_size);
  397. } else {
  398. size = 0;
  399. }
  400. if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
  401. fprintf (stderr, "%s: Write error on %s: %s\n",
  402. params.cmdname, params.imagefile,
  403. strerror(errno));
  404. exit (EXIT_FAILURE);
  405. }
  406. if (!file) {
  407. break;
  408. }
  409. if (sep) {
  410. *sep = ':';
  411. file = sep + 1;
  412. } else {
  413. file = NULL;
  414. }
  415. }
  416. file = params.datafile;
  417. for (;;) {
  418. char *sep = strchr(file, ':');
  419. if (sep) {
  420. *sep = '\0';
  421. copy_file (ifd, file, 1);
  422. *sep++ = ':';
  423. file = sep;
  424. } else {
  425. copy_file (ifd, file, 0);
  426. break;
  427. }
  428. }
  429. } else if (params.type == IH_TYPE_PBLIMAGE) {
  430. /* PBL has special Image format, implements its' own */
  431. pbl_load_uboot(ifd, &params);
  432. } else {
  433. copy_file(ifd, params.datafile, pad_len);
  434. }
  435. }
  436. /* We're a bit of paranoid */
  437. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  438. !defined(__sun__) && \
  439. !defined(__FreeBSD__) && \
  440. !defined(__OpenBSD__) && \
  441. !defined(__APPLE__)
  442. (void) fdatasync (ifd);
  443. #else
  444. (void) fsync (ifd);
  445. #endif
  446. if (fstat(ifd, &sbuf) < 0) {
  447. fprintf (stderr, "%s: Can't stat %s: %s\n",
  448. params.cmdname, params.imagefile, strerror(errno));
  449. exit (EXIT_FAILURE);
  450. }
  451. params.file_size = sbuf.st_size;
  452. ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
  453. if (ptr == MAP_FAILED) {
  454. fprintf (stderr, "%s: Can't map %s: %s\n",
  455. params.cmdname, params.imagefile, strerror(errno));
  456. exit (EXIT_FAILURE);
  457. }
  458. /* Setup the image header as per input image type*/
  459. if (tparams->set_header)
  460. tparams->set_header (ptr, &sbuf, ifd, &params);
  461. else {
  462. fprintf (stderr, "%s: Can't set header for %s: %s\n",
  463. params.cmdname, tparams->name, strerror(errno));
  464. exit (EXIT_FAILURE);
  465. }
  466. /* Print the image information by processing image header */
  467. if (tparams->print_header)
  468. tparams->print_header (ptr);
  469. else {
  470. fprintf (stderr, "%s: Can't print header for %s: %s\n",
  471. params.cmdname, tparams->name, strerror(errno));
  472. exit (EXIT_FAILURE);
  473. }
  474. (void) munmap((void *)ptr, sbuf.st_size);
  475. /* We're a bit of paranoid */
  476. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  477. !defined(__sun__) && \
  478. !defined(__FreeBSD__) && \
  479. !defined(__OpenBSD__) && \
  480. !defined(__APPLE__)
  481. (void) fdatasync (ifd);
  482. #else
  483. (void) fsync (ifd);
  484. #endif
  485. if (close(ifd)) {
  486. fprintf (stderr, "%s: Write error on %s: %s\n",
  487. params.cmdname, params.imagefile, strerror(errno));
  488. exit (EXIT_FAILURE);
  489. }
  490. exit (EXIT_SUCCESS);
  491. }
  492. static void
  493. copy_file (int ifd, const char *datafile, int pad)
  494. {
  495. int dfd;
  496. struct stat sbuf;
  497. unsigned char *ptr;
  498. int tail;
  499. int zero = 0;
  500. uint8_t zeros[4096];
  501. int offset = 0;
  502. int size;
  503. struct image_type_params *tparams = imagetool_get_type(params.type);
  504. memset(zeros, 0, sizeof(zeros));
  505. if (params.vflag) {
  506. fprintf (stderr, "Adding Image %s\n", datafile);
  507. }
  508. if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
  509. fprintf (stderr, "%s: Can't open %s: %s\n",
  510. params.cmdname, datafile, strerror(errno));
  511. exit (EXIT_FAILURE);
  512. }
  513. if (fstat(dfd, &sbuf) < 0) {
  514. fprintf (stderr, "%s: Can't stat %s: %s\n",
  515. params.cmdname, datafile, strerror(errno));
  516. exit (EXIT_FAILURE);
  517. }
  518. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  519. if (ptr == MAP_FAILED) {
  520. fprintf (stderr, "%s: Can't read %s: %s\n",
  521. params.cmdname, datafile, strerror(errno));
  522. exit (EXIT_FAILURE);
  523. }
  524. if (params.xflag) {
  525. unsigned char *p = NULL;
  526. /*
  527. * XIP: do not append the image_header_t at the
  528. * beginning of the file, but consume the space
  529. * reserved for it.
  530. */
  531. if ((unsigned)sbuf.st_size < tparams->header_size) {
  532. fprintf (stderr,
  533. "%s: Bad size: \"%s\" is too small for XIP\n",
  534. params.cmdname, datafile);
  535. exit (EXIT_FAILURE);
  536. }
  537. for (p = ptr; p < ptr + tparams->header_size; p++) {
  538. if ( *p != 0xff ) {
  539. fprintf (stderr,
  540. "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
  541. params.cmdname, datafile);
  542. exit (EXIT_FAILURE);
  543. }
  544. }
  545. offset = tparams->header_size;
  546. }
  547. size = sbuf.st_size - offset;
  548. if (write(ifd, ptr + offset, size) != size) {
  549. fprintf (stderr, "%s: Write error on %s: %s\n",
  550. params.cmdname, params.imagefile, strerror(errno));
  551. exit (EXIT_FAILURE);
  552. }
  553. tail = size % 4;
  554. if ((pad == 1) && (tail != 0)) {
  555. if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
  556. fprintf (stderr, "%s: Write error on %s: %s\n",
  557. params.cmdname, params.imagefile,
  558. strerror(errno));
  559. exit (EXIT_FAILURE);
  560. }
  561. } else if (pad > 1) {
  562. while (pad > 0) {
  563. int todo = sizeof(zeros);
  564. if (todo > pad)
  565. todo = pad;
  566. if (write(ifd, (char *)&zeros, todo) != todo) {
  567. fprintf(stderr, "%s: Write error on %s: %s\n",
  568. params.cmdname, params.imagefile,
  569. strerror(errno));
  570. exit(EXIT_FAILURE);
  571. }
  572. pad -= todo;
  573. }
  574. }
  575. (void) munmap((void *)ptr, sbuf.st_size);
  576. (void) close (dfd);
  577. }