mkimage.c 16 KB

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