Fork of the vendor (Boundary Devices) u-boot for Reform 2, with minor tweaks. The goal is to migrate to mainstream u-boot or barebox ASAP. The main impediment so far is the 4GB RAM config.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

714 lines
17 KiB

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