mkimage.c 16 KB

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