mkimage.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. /* parameters initialized by core will be used by the image type code */
  16. struct image_tool_params params = {
  17. .os = IH_OS_LINUX,
  18. .arch = IH_ARCH_PPC,
  19. .type = IH_TYPE_KERNEL,
  20. .comp = IH_COMP_GZIP,
  21. .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
  22. .imagename = "",
  23. .imagename2 = "",
  24. };
  25. static int h_compare_image_name(const void *vtype1, const void *vtype2)
  26. {
  27. const int *type1 = vtype1;
  28. const int *type2 = vtype2;
  29. const char *name1 = genimg_get_type_short_name(*type1);
  30. const char *name2 = genimg_get_type_short_name(*type2);
  31. return strcmp(name1, name2);
  32. }
  33. /* Show all image types supported by mkimage */
  34. static void show_image_types(void)
  35. {
  36. struct image_type_params *tparams;
  37. int order[IH_TYPE_COUNT];
  38. int count;
  39. int type;
  40. int i;
  41. /* Sort the names in order of short name for easier reading */
  42. memset(order, '\0', sizeof(order));
  43. for (count = 0, type = 0; type < IH_TYPE_COUNT; type++) {
  44. tparams = imagetool_get_type(type);
  45. if (tparams)
  46. order[count++] = type;
  47. }
  48. qsort(order, count, sizeof(int), h_compare_image_name);
  49. fprintf(stderr, "\nInvalid image type. Supported image types:\n");
  50. for (i = 0; i < count; i++) {
  51. type = order[i];
  52. tparams = imagetool_get_type(type);
  53. if (tparams) {
  54. fprintf(stderr, "\t%-15s %s\n",
  55. genimg_get_type_short_name(type),
  56. genimg_get_type_name(type));
  57. }
  58. }
  59. fprintf(stderr, "\n");
  60. }
  61. static void process_args(int argc, char **argv)
  62. {
  63. char *ptr;
  64. int opt;
  65. while ((opt = getopt(argc, argv,
  66. "a:A:cC:d:D:e:f:Fk:K:ln:O:rR:sT:vVx")) != -1) {
  67. switch (opt) {
  68. case 'a':
  69. params.addr = strtoull(optarg, &ptr, 16);
  70. if (*ptr) {
  71. fprintf(stderr, "%s: invalid load address %s\n",
  72. params.cmdname, optarg);
  73. exit(EXIT_FAILURE);
  74. }
  75. break;
  76. case 'A':
  77. params.arch = genimg_get_arch_id(optarg);
  78. if (params.arch < 0)
  79. usage();
  80. break;
  81. case 'c':
  82. params.comment = optarg;
  83. break;
  84. case 'C':
  85. params.comp = genimg_get_comp_id(optarg);
  86. if (params.comp < 0)
  87. usage();
  88. break;
  89. case 'd':
  90. params.datafile = optarg;
  91. params.dflag = 1;
  92. break;
  93. case 'D':
  94. params.dtc = optarg;
  95. break;
  96. case 'e':
  97. params.ep = strtoull(optarg, &ptr, 16);
  98. if (*ptr) {
  99. fprintf(stderr, "%s: invalid entry point %s\n",
  100. params.cmdname, optarg);
  101. exit(EXIT_FAILURE);
  102. }
  103. params.eflag = 1;
  104. break;
  105. case 'f':
  106. params.datafile = optarg;
  107. /* no break */
  108. case 'F':
  109. /*
  110. * The flattened image tree (FIT) format
  111. * requires a flattened device tree image type
  112. */
  113. params.type = IH_TYPE_FLATDT;
  114. params.fflag = 1;
  115. break;
  116. case 'k':
  117. params.keydir = optarg;
  118. break;
  119. case 'K':
  120. params.keydest = optarg;
  121. break;
  122. case 'l':
  123. params.lflag = 1;
  124. break;
  125. case 'n':
  126. params.imagename = optarg;
  127. break;
  128. case 'O':
  129. params.os = genimg_get_os_id(optarg);
  130. if (params.os < 0)
  131. usage();
  132. break;
  133. case 'r':
  134. params.require_keys = 1;
  135. break;
  136. case 'R':
  137. /*
  138. * This entry is for the second configuration
  139. * file, if only one is not enough.
  140. */
  141. params.imagename2 = optarg;
  142. break;
  143. case 's':
  144. params.skipcpy = 1;
  145. break;
  146. case 'T':
  147. params.type = genimg_get_type_id(optarg);
  148. if (params.type < 0) {
  149. show_image_types();
  150. usage();
  151. }
  152. break;
  153. case 'v':
  154. params.vflag++;
  155. break;
  156. case 'V':
  157. printf("mkimage version %s\n", PLAIN_VERSION);
  158. exit(EXIT_SUCCESS);
  159. case 'x':
  160. params.xflag++;
  161. break;
  162. default:
  163. usage();
  164. }
  165. }
  166. if (optind >= argc)
  167. usage();
  168. params.imagefile = argv[optind];
  169. }
  170. int main(int argc, char **argv)
  171. {
  172. int ifd = -1;
  173. struct stat sbuf;
  174. char *ptr;
  175. int retval = 0;
  176. struct image_type_params *tparams = NULL;
  177. int pad_len = 0;
  178. int dfd;
  179. params.cmdname = *argv;
  180. params.addr = 0;
  181. params.ep = 0;
  182. process_args(argc, argv);
  183. /* set tparams as per input type_id */
  184. tparams = imagetool_get_type(params.type);
  185. if (tparams == NULL) {
  186. fprintf (stderr, "%s: unsupported type %s\n",
  187. params.cmdname, genimg_get_type_name(params.type));
  188. exit (EXIT_FAILURE);
  189. }
  190. /*
  191. * check the passed arguments parameters meets the requirements
  192. * as per image type to be generated/listed
  193. */
  194. if (tparams->check_params)
  195. if (tparams->check_params (&params))
  196. usage();
  197. if (!params.eflag) {
  198. params.ep = params.addr;
  199. /* If XIP, entry point must be after the U-Boot header */
  200. if (params.xflag)
  201. params.ep += tparams->header_size;
  202. }
  203. if (params.fflag){
  204. if (tparams->fflag_handle)
  205. /*
  206. * in some cases, some additional processing needs
  207. * to be done if fflag is defined
  208. *
  209. * For ex. fit_handle_file for Fit file support
  210. */
  211. retval = tparams->fflag_handle(&params);
  212. if (retval != EXIT_SUCCESS)
  213. exit (retval);
  214. }
  215. if (params.lflag || params.fflag) {
  216. ifd = open (params.imagefile, O_RDONLY|O_BINARY);
  217. } else {
  218. ifd = open (params.imagefile,
  219. O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
  220. }
  221. if (ifd < 0) {
  222. fprintf (stderr, "%s: Can't open %s: %s\n",
  223. params.cmdname, params.imagefile,
  224. strerror(errno));
  225. exit (EXIT_FAILURE);
  226. }
  227. if (params.lflag || params.fflag) {
  228. /*
  229. * list header information of existing image
  230. */
  231. if (fstat(ifd, &sbuf) < 0) {
  232. fprintf (stderr, "%s: Can't stat %s: %s\n",
  233. params.cmdname, params.imagefile,
  234. strerror(errno));
  235. exit (EXIT_FAILURE);
  236. }
  237. if ((unsigned)sbuf.st_size < tparams->header_size) {
  238. fprintf (stderr,
  239. "%s: Bad size: \"%s\" is not valid image\n",
  240. params.cmdname, params.imagefile);
  241. exit (EXIT_FAILURE);
  242. }
  243. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  244. if (ptr == MAP_FAILED) {
  245. fprintf (stderr, "%s: Can't read %s: %s\n",
  246. params.cmdname, params.imagefile,
  247. strerror(errno));
  248. exit (EXIT_FAILURE);
  249. }
  250. /*
  251. * scan through mkimage registry for all supported image types
  252. * and verify the input image file header for match
  253. * Print the image information for matched image type
  254. * Returns the error code if not matched
  255. */
  256. retval = imagetool_verify_print_header(ptr, &sbuf,
  257. tparams, &params);
  258. (void) munmap((void *)ptr, sbuf.st_size);
  259. (void) close (ifd);
  260. exit (retval);
  261. }
  262. if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
  263. dfd = open(params.datafile, O_RDONLY | O_BINARY);
  264. if (dfd < 0) {
  265. fprintf(stderr, "%s: Can't open %s: %s\n",
  266. params.cmdname, params.datafile,
  267. strerror(errno));
  268. exit(EXIT_FAILURE);
  269. }
  270. if (fstat(dfd, &sbuf) < 0) {
  271. fprintf(stderr, "%s: Can't stat %s: %s\n",
  272. params.cmdname, params.datafile,
  273. strerror(errno));
  274. exit(EXIT_FAILURE);
  275. }
  276. params.file_size = sbuf.st_size + tparams->header_size;
  277. close(dfd);
  278. }
  279. /*
  280. * In case there an header with a variable
  281. * length will be added, the corresponding
  282. * function is called. This is responsible to
  283. * allocate memory for the header itself.
  284. */
  285. if (tparams->vrec_header)
  286. pad_len = tparams->vrec_header(&params, tparams);
  287. else
  288. memset(tparams->hdr, 0, tparams->header_size);
  289. if (write(ifd, tparams->hdr, tparams->header_size)
  290. != tparams->header_size) {
  291. fprintf (stderr, "%s: Write error on %s: %s\n",
  292. params.cmdname, params.imagefile, strerror(errno));
  293. exit (EXIT_FAILURE);
  294. }
  295. if (!params.skipcpy) {
  296. if (params.type == IH_TYPE_MULTI ||
  297. params.type == IH_TYPE_SCRIPT) {
  298. char *file = params.datafile;
  299. uint32_t size;
  300. for (;;) {
  301. char *sep = NULL;
  302. if (file) {
  303. if ((sep = strchr(file, ':')) != NULL) {
  304. *sep = '\0';
  305. }
  306. if (stat (file, &sbuf) < 0) {
  307. fprintf (stderr, "%s: Can't stat %s: %s\n",
  308. params.cmdname, file, strerror(errno));
  309. exit (EXIT_FAILURE);
  310. }
  311. size = cpu_to_uimage (sbuf.st_size);
  312. } else {
  313. size = 0;
  314. }
  315. if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
  316. fprintf (stderr, "%s: Write error on %s: %s\n",
  317. params.cmdname, params.imagefile,
  318. strerror(errno));
  319. exit (EXIT_FAILURE);
  320. }
  321. if (!file) {
  322. break;
  323. }
  324. if (sep) {
  325. *sep = ':';
  326. file = sep + 1;
  327. } else {
  328. file = NULL;
  329. }
  330. }
  331. file = params.datafile;
  332. for (;;) {
  333. char *sep = strchr(file, ':');
  334. if (sep) {
  335. *sep = '\0';
  336. copy_file (ifd, file, 1);
  337. *sep++ = ':';
  338. file = sep;
  339. } else {
  340. copy_file (ifd, file, 0);
  341. break;
  342. }
  343. }
  344. } else if (params.type == IH_TYPE_PBLIMAGE) {
  345. /* PBL has special Image format, implements its' own */
  346. pbl_load_uboot(ifd, &params);
  347. } else {
  348. copy_file(ifd, params.datafile, pad_len);
  349. }
  350. }
  351. /* We're a bit of paranoid */
  352. #if defined(_POSIX_SYNCHRONIZED_IO) && \
  353. !defined(__sun__) && \
  354. !defined(__FreeBSD__) && \
  355. !defined(__OpenBSD__) && \
  356. !defined(__APPLE__)
  357. (void) fdatasync (ifd);
  358. #else
  359. (void) fsync (ifd);
  360. #endif
  361. if (fstat(ifd, &sbuf) < 0) {
  362. fprintf (stderr, "%s: Can't stat %s: %s\n",
  363. params.cmdname, params.imagefile, strerror(errno));
  364. exit (EXIT_FAILURE);
  365. }
  366. params.file_size = sbuf.st_size;
  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. memset(zeros, 0, sizeof(zeros));
  420. if (params.vflag) {
  421. fprintf (stderr, "Adding Image %s\n", datafile);
  422. }
  423. if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
  424. fprintf (stderr, "%s: Can't open %s: %s\n",
  425. params.cmdname, datafile, strerror(errno));
  426. exit (EXIT_FAILURE);
  427. }
  428. if (fstat(dfd, &sbuf) < 0) {
  429. fprintf (stderr, "%s: Can't stat %s: %s\n",
  430. params.cmdname, datafile, strerror(errno));
  431. exit (EXIT_FAILURE);
  432. }
  433. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  434. if (ptr == MAP_FAILED) {
  435. fprintf (stderr, "%s: Can't read %s: %s\n",
  436. params.cmdname, datafile, strerror(errno));
  437. exit (EXIT_FAILURE);
  438. }
  439. if (params.xflag) {
  440. unsigned char *p = NULL;
  441. /*
  442. * XIP: do not append the image_header_t at the
  443. * beginning of the file, but consume the space
  444. * reserved for it.
  445. */
  446. if ((unsigned)sbuf.st_size < tparams->header_size) {
  447. fprintf (stderr,
  448. "%s: Bad size: \"%s\" is too small for XIP\n",
  449. params.cmdname, datafile);
  450. exit (EXIT_FAILURE);
  451. }
  452. for (p = ptr; p < ptr + tparams->header_size; p++) {
  453. if ( *p != 0xff ) {
  454. fprintf (stderr,
  455. "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
  456. params.cmdname, datafile);
  457. exit (EXIT_FAILURE);
  458. }
  459. }
  460. offset = tparams->header_size;
  461. }
  462. size = sbuf.st_size - offset;
  463. if (write(ifd, ptr + offset, size) != size) {
  464. fprintf (stderr, "%s: Write error on %s: %s\n",
  465. params.cmdname, params.imagefile, strerror(errno));
  466. exit (EXIT_FAILURE);
  467. }
  468. tail = size % 4;
  469. if ((pad == 1) && (tail != 0)) {
  470. if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
  471. fprintf (stderr, "%s: Write error on %s: %s\n",
  472. params.cmdname, params.imagefile,
  473. strerror(errno));
  474. exit (EXIT_FAILURE);
  475. }
  476. } else if (pad > 1) {
  477. while (pad > 0) {
  478. int todo = sizeof(zeros);
  479. if (todo > pad)
  480. todo = pad;
  481. if (write(ifd, (char *)&zeros, todo) != todo) {
  482. fprintf(stderr, "%s: Write error on %s: %s\n",
  483. params.cmdname, params.imagefile,
  484. strerror(errno));
  485. exit(EXIT_FAILURE);
  486. }
  487. pad -= todo;
  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 all 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. fprintf(stderr, "Use -T to see a list of available image types\n");
  527. exit (EXIT_FAILURE);
  528. }