mkimage.c 16 KB

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