fit_image.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2008 Semihalf
  4. *
  5. * (C) Copyright 2000-2004
  6. * DENX Software Engineering
  7. * Wolfgang Denk, wd@denx.de
  8. *
  9. * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
  10. * FIT image specific code abstracted from mkimage.c
  11. * some functions added to address abstraction
  12. *
  13. * All rights reserved.
  14. */
  15. #include "imagetool.h"
  16. #include "fit_common.h"
  17. #include "mkimage.h"
  18. #include <image.h>
  19. #include <string.h>
  20. #include <stdarg.h>
  21. #include <version.h>
  22. #include <u-boot/crc.h>
  23. static struct legacy_img_hdr header;
  24. static int fit_add_file_data(struct image_tool_params *params, size_t size_inc,
  25. const char *tmpfile)
  26. {
  27. int tfd, destfd = 0;
  28. void *dest_blob = NULL;
  29. off_t destfd_size = 0;
  30. struct stat sbuf;
  31. void *ptr;
  32. int ret = 0;
  33. tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true,
  34. false);
  35. if (tfd < 0) {
  36. fprintf(stderr, "Cannot map FDT file '%s'\n", tmpfile);
  37. return -EIO;
  38. }
  39. if (params->keydest) {
  40. struct stat dest_sbuf;
  41. destfd = mmap_fdt(params->cmdname, params->keydest, size_inc,
  42. &dest_blob, &dest_sbuf, false,
  43. false);
  44. if (destfd < 0) {
  45. ret = -EIO;
  46. goto err_keydest;
  47. }
  48. destfd_size = dest_sbuf.st_size;
  49. }
  50. /* for first image creation, add a timestamp at offset 0 i.e., root */
  51. if (params->datafile || params->reset_timestamp) {
  52. time_t time = imagetool_get_source_date(params->cmdname,
  53. sbuf.st_mtime);
  54. ret = fit_set_timestamp(ptr, 0, time);
  55. }
  56. if (!ret)
  57. ret = fit_pre_load_data(params->keydir, dest_blob, ptr);
  58. if (!ret) {
  59. ret = fit_cipher_data(params->keydir, dest_blob, ptr,
  60. params->comment,
  61. params->require_keys,
  62. params->engine_id,
  63. params->cmdname);
  64. }
  65. if (!ret) {
  66. ret = fit_add_verification_data(params->keydir,
  67. params->keyfile, dest_blob, ptr,
  68. params->comment,
  69. params->require_keys,
  70. params->engine_id,
  71. params->cmdname,
  72. params->algo_name,
  73. &params->summary);
  74. }
  75. if (dest_blob) {
  76. munmap(dest_blob, destfd_size);
  77. close(destfd);
  78. }
  79. err_keydest:
  80. munmap(ptr, sbuf.st_size);
  81. close(tfd);
  82. return ret;
  83. }
  84. /**
  85. * fit_calc_size() - Calculate the approximate size of the FIT we will generate
  86. */
  87. static int fit_calc_size(struct image_tool_params *params)
  88. {
  89. struct content_info *cont;
  90. int size, total_size;
  91. size = imagetool_get_filesize(params, params->datafile);
  92. if (size < 0)
  93. return -1;
  94. total_size = size;
  95. if (params->fit_ramdisk) {
  96. size = imagetool_get_filesize(params, params->fit_ramdisk);
  97. if (size < 0)
  98. return -1;
  99. total_size += size;
  100. }
  101. for (cont = params->content_head; cont; cont = cont->next) {
  102. size = imagetool_get_filesize(params, cont->fname);
  103. if (size < 0)
  104. return -1;
  105. /* Add space for properties and hash node */
  106. total_size += size + 300;
  107. }
  108. /* Add plenty of space for headers, properties, nodes, etc. */
  109. total_size += 4096;
  110. return total_size;
  111. }
  112. static int fdt_property_file(struct image_tool_params *params,
  113. void *fdt, const char *name, const char *fname)
  114. {
  115. struct stat sbuf;
  116. void *ptr;
  117. int ret;
  118. int fd;
  119. fd = open(fname, O_RDWR | O_BINARY);
  120. if (fd < 0) {
  121. fprintf(stderr, "%s: Can't open %s: %s\n",
  122. params->cmdname, fname, strerror(errno));
  123. return -1;
  124. }
  125. if (fstat(fd, &sbuf) < 0) {
  126. fprintf(stderr, "%s: Can't stat %s: %s\n",
  127. params->cmdname, fname, strerror(errno));
  128. goto err;
  129. }
  130. ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
  131. if (ret)
  132. goto err;
  133. ret = read(fd, ptr, sbuf.st_size);
  134. if (ret != sbuf.st_size) {
  135. fprintf(stderr, "%s: Can't read %s: %s\n",
  136. params->cmdname, fname, strerror(errno));
  137. goto err;
  138. }
  139. close(fd);
  140. return 0;
  141. err:
  142. close(fd);
  143. return -1;
  144. }
  145. static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
  146. {
  147. char str[100];
  148. va_list ptr;
  149. va_start(ptr, fmt);
  150. vsnprintf(str, sizeof(str), fmt, ptr);
  151. va_end(ptr);
  152. return fdt_property_string(fdt, name, str);
  153. }
  154. static void get_basename(char *str, int size, const char *fname)
  155. {
  156. const char *p, *start, *end;
  157. int len;
  158. /*
  159. * Use the base name as the 'name' field. So for example:
  160. *
  161. * "arch/arm/dts/sun7i-a20-bananapro.dtb"
  162. * becomes "sun7i-a20-bananapro"
  163. */
  164. p = strrchr(fname, '/');
  165. start = p ? p + 1 : fname;
  166. p = strrchr(fname, '.');
  167. end = p ? p : fname + strlen(fname);
  168. len = end - start;
  169. if (len >= size)
  170. len = size - 1;
  171. memcpy(str, start, len);
  172. str[len] = '\0';
  173. }
  174. /**
  175. * fit_add_hash_or_sign() - Add a hash or signature node
  176. *
  177. * @params: Image parameters
  178. * @fdt: Device tree to add to (in sequential-write mode)
  179. * @is_images_subnode: true to add hash even if key name hint is provided
  180. *
  181. * If do_add_hash is false (default) and there is a key name hint, try to add
  182. * a sign node to parent. Otherwise, just add a CRC. Rationale: if conf have
  183. * to be signed, image/dt have to be hashed even if there is a key name hint.
  184. */
  185. static void fit_add_hash_or_sign(struct image_tool_params *params, void *fdt,
  186. bool is_images_subnode)
  187. {
  188. const char *hash_algo = "crc32";
  189. bool do_hash = false;
  190. bool do_sign = false;
  191. switch (params->auto_fit) {
  192. case AF_OFF:
  193. break;
  194. case AF_HASHED_IMG:
  195. do_hash = is_images_subnode;
  196. break;
  197. case AF_SIGNED_IMG:
  198. do_sign = is_images_subnode;
  199. break;
  200. case AF_SIGNED_CONF:
  201. if (is_images_subnode) {
  202. do_hash = true;
  203. hash_algo = "sha1";
  204. } else {
  205. do_sign = true;
  206. }
  207. break;
  208. default:
  209. fprintf(stderr,
  210. "%s: Unsupported auto FIT mode %u\n",
  211. params->cmdname, params->auto_fit);
  212. break;
  213. }
  214. if (do_hash) {
  215. fdt_begin_node(fdt, FIT_HASH_NODENAME);
  216. fdt_property_string(fdt, FIT_ALGO_PROP, hash_algo);
  217. fdt_end_node(fdt);
  218. }
  219. if (do_sign) {
  220. fdt_begin_node(fdt, FIT_SIG_NODENAME);
  221. fdt_property_string(fdt, FIT_ALGO_PROP, params->algo_name);
  222. fdt_property_string(fdt, FIT_KEY_HINT, params->keyname);
  223. fdt_end_node(fdt);
  224. }
  225. }
  226. /**
  227. * fit_write_images() - Write out a list of images to the FIT
  228. *
  229. * We always include the main image (params->datafile). If there are device
  230. * tree files, we include an fdt- node for each of those too.
  231. */
  232. static int fit_write_images(struct image_tool_params *params, char *fdt)
  233. {
  234. struct content_info *cont;
  235. const char *typename;
  236. char str[100];
  237. int upto;
  238. int ret;
  239. fdt_begin_node(fdt, "images");
  240. /* First the main image */
  241. typename = genimg_get_type_short_name(params->fit_image_type);
  242. snprintf(str, sizeof(str), "%s-1", typename);
  243. fdt_begin_node(fdt, str);
  244. fdt_property_string(fdt, FIT_DESC_PROP, params->imagename);
  245. fdt_property_string(fdt, FIT_TYPE_PROP, typename);
  246. fdt_property_string(fdt, FIT_ARCH_PROP,
  247. genimg_get_arch_short_name(params->arch));
  248. fdt_property_string(fdt, FIT_OS_PROP,
  249. genimg_get_os_short_name(params->os));
  250. fdt_property_string(fdt, FIT_COMP_PROP,
  251. genimg_get_comp_short_name(params->comp));
  252. fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr);
  253. fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep);
  254. /*
  255. * Put data last since it is large. SPL may only load the first part
  256. * of the DT, so this way it can access all the above fields.
  257. */
  258. ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile);
  259. if (ret)
  260. return ret;
  261. fit_add_hash_or_sign(params, fdt, true);
  262. fdt_end_node(fdt);
  263. /* Now the device tree files if available */
  264. upto = 0;
  265. for (cont = params->content_head; cont; cont = cont->next) {
  266. if (cont->type != IH_TYPE_FLATDT)
  267. continue;
  268. typename = genimg_get_type_short_name(cont->type);
  269. snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
  270. fdt_begin_node(fdt, str);
  271. get_basename(str, sizeof(str), cont->fname);
  272. fdt_property_string(fdt, FIT_DESC_PROP, str);
  273. ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
  274. cont->fname);
  275. if (ret)
  276. return ret;
  277. fdt_property_string(fdt, FIT_TYPE_PROP, typename);
  278. fdt_property_string(fdt, FIT_ARCH_PROP,
  279. genimg_get_arch_short_name(params->arch));
  280. fdt_property_string(fdt, FIT_COMP_PROP,
  281. genimg_get_comp_short_name(IH_COMP_NONE));
  282. fit_add_hash_or_sign(params, fdt, true);
  283. if (ret)
  284. return ret;
  285. fdt_end_node(fdt);
  286. }
  287. /* And a ramdisk file if available */
  288. if (params->fit_ramdisk) {
  289. fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
  290. fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP);
  291. fdt_property_string(fdt, FIT_OS_PROP,
  292. genimg_get_os_short_name(params->os));
  293. fdt_property_string(fdt, FIT_ARCH_PROP,
  294. genimg_get_arch_short_name(params->arch));
  295. ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
  296. params->fit_ramdisk);
  297. if (ret)
  298. return ret;
  299. fit_add_hash_or_sign(params, fdt, true);
  300. if (ret)
  301. return ret;
  302. fdt_end_node(fdt);
  303. }
  304. fdt_end_node(fdt);
  305. return 0;
  306. }
  307. /**
  308. * fit_write_configs() - Write out a list of configurations to the FIT
  309. *
  310. * If there are device tree files, we include a configuration for each, which
  311. * selects the main image (params->datafile) and its corresponding device
  312. * tree file.
  313. *
  314. * Otherwise we just create a configuration with the main image in it.
  315. */
  316. static void fit_write_configs(struct image_tool_params *params, char *fdt)
  317. {
  318. struct content_info *cont;
  319. const char *typename;
  320. char str[100];
  321. int upto;
  322. fdt_begin_node(fdt, "configurations");
  323. fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1");
  324. upto = 0;
  325. for (cont = params->content_head; cont; cont = cont->next) {
  326. if (cont->type != IH_TYPE_FLATDT)
  327. continue;
  328. typename = genimg_get_type_short_name(cont->type);
  329. snprintf(str, sizeof(str), "conf-%d", ++upto);
  330. fdt_begin_node(fdt, str);
  331. get_basename(str, sizeof(str), cont->fname);
  332. fdt_property_string(fdt, FIT_DESC_PROP, str);
  333. typename = genimg_get_type_short_name(params->fit_image_type);
  334. snprintf(str, sizeof(str), "%s-1", typename);
  335. fdt_property_string(fdt, typename, str);
  336. fdt_property_string(fdt, FIT_LOADABLE_PROP, str);
  337. if (params->fit_ramdisk)
  338. fdt_property_string(fdt, FIT_RAMDISK_PROP,
  339. FIT_RAMDISK_PROP "-1");
  340. snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
  341. fdt_property_string(fdt, FIT_FDT_PROP, str);
  342. fit_add_hash_or_sign(params, fdt, false);
  343. fdt_end_node(fdt);
  344. }
  345. if (!upto) {
  346. fdt_begin_node(fdt, "conf-1");
  347. typename = genimg_get_type_short_name(params->fit_image_type);
  348. snprintf(str, sizeof(str), "%s-1", typename);
  349. fdt_property_string(fdt, typename, str);
  350. if (params->fit_ramdisk)
  351. fdt_property_string(fdt, FIT_RAMDISK_PROP,
  352. FIT_RAMDISK_PROP "-1");
  353. fit_add_hash_or_sign(params, fdt, false);
  354. fdt_end_node(fdt);
  355. }
  356. fdt_end_node(fdt);
  357. }
  358. static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
  359. {
  360. int ret;
  361. ret = fdt_create(fdt, size);
  362. if (ret)
  363. return ret;
  364. fdt_finish_reservemap(fdt);
  365. fdt_begin_node(fdt, "");
  366. fdt_property_strf(fdt, FIT_DESC_PROP,
  367. "%s image with one or more FDT blobs",
  368. genimg_get_type_name(params->fit_image_type));
  369. fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
  370. fdt_property_u32(fdt, "#address-cells", 1);
  371. ret = fit_write_images(params, fdt);
  372. if (ret)
  373. return ret;
  374. fit_write_configs(params, fdt);
  375. fdt_end_node(fdt);
  376. ret = fdt_finish(fdt);
  377. if (ret)
  378. return ret;
  379. return fdt_totalsize(fdt);
  380. }
  381. static int fit_build(struct image_tool_params *params, const char *fname)
  382. {
  383. char *buf;
  384. int size;
  385. int ret;
  386. int fd;
  387. size = fit_calc_size(params);
  388. if (size < 0)
  389. return -1;
  390. buf = calloc(1, size);
  391. if (!buf) {
  392. fprintf(stderr, "%s: Out of memory (%d bytes)\n",
  393. params->cmdname, size);
  394. return -1;
  395. }
  396. ret = fit_build_fdt(params, buf, size);
  397. if (ret < 0) {
  398. fprintf(stderr, "%s: Failed to build FIT image\n",
  399. params->cmdname);
  400. goto err_buf;
  401. }
  402. size = ret;
  403. fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
  404. if (fd < 0) {
  405. fprintf(stderr, "%s: Can't open %s: %s\n",
  406. params->cmdname, fname, strerror(errno));
  407. goto err_buf;
  408. }
  409. ret = write(fd, buf, size);
  410. if (ret != size) {
  411. fprintf(stderr, "%s: Can't write %s: %s\n",
  412. params->cmdname, fname, strerror(errno));
  413. goto err;
  414. }
  415. close(fd);
  416. free(buf);
  417. return 0;
  418. err:
  419. close(fd);
  420. err_buf:
  421. free(buf);
  422. return -1;
  423. }
  424. /**
  425. * fit_extract_data() - Move all data outside the FIT
  426. *
  427. * This takes a normal FIT file and removes all the 'data' properties from it.
  428. * The data is placed in an area after the FIT so that it can be accessed
  429. * using an offset into that area. The 'data' properties turn into
  430. * 'data-offset' properties.
  431. *
  432. * This function cannot cope with FITs with 'data-offset' properties. All
  433. * data must be in 'data' properties on entry.
  434. */
  435. static int fit_extract_data(struct image_tool_params *params, const char *fname)
  436. {
  437. void *buf = NULL;
  438. int buf_ptr;
  439. int fit_size, new_size;
  440. int fd;
  441. struct stat sbuf;
  442. void *fdt;
  443. int ret;
  444. int images;
  445. int node;
  446. int image_number;
  447. int align_size;
  448. align_size = params->bl_len ? params->bl_len : 4;
  449. fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false);
  450. if (fd < 0)
  451. return -EIO;
  452. fit_size = fdt_totalsize(fdt);
  453. images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
  454. if (images < 0) {
  455. debug("%s: Cannot find /images node: %d\n", __func__, images);
  456. ret = -EINVAL;
  457. goto err_munmap;
  458. }
  459. image_number = fdtdec_get_child_count(fdt, images);
  460. /*
  461. * Allocate space to hold the image data we will extract,
  462. * extral space allocate for image alignment to prevent overflow.
  463. */
  464. buf = calloc(1, fit_size + (align_size * image_number));
  465. if (!buf) {
  466. ret = -ENOMEM;
  467. goto err_munmap;
  468. }
  469. buf_ptr = 0;
  470. for (node = fdt_first_subnode(fdt, images);
  471. node >= 0;
  472. node = fdt_next_subnode(fdt, node)) {
  473. const char *data;
  474. int len;
  475. data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len);
  476. if (!data)
  477. continue;
  478. memcpy(buf + buf_ptr, data, len);
  479. debug("Extracting data size %x\n", len);
  480. ret = fdt_delprop(fdt, node, FIT_DATA_PROP);
  481. if (ret) {
  482. ret = -EPERM;
  483. goto err_munmap;
  484. }
  485. if (params->external_offset > 0) {
  486. /* An external offset positions the data absolutely. */
  487. fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP,
  488. params->external_offset + buf_ptr);
  489. } else {
  490. fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP,
  491. buf_ptr);
  492. }
  493. fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
  494. buf_ptr += ALIGN(len, align_size);
  495. }
  496. /* Pack the FDT and place the data after it */
  497. fdt_pack(fdt);
  498. new_size = fdt_totalsize(fdt);
  499. new_size = ALIGN(new_size, align_size);
  500. fdt_set_totalsize(fdt, new_size);
  501. debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
  502. debug("External data size %x\n", buf_ptr);
  503. munmap(fdt, sbuf.st_size);
  504. if (ftruncate(fd, new_size)) {
  505. debug("%s: Failed to truncate file: %s\n", __func__,
  506. strerror(errno));
  507. ret = -EIO;
  508. goto err;
  509. }
  510. /* Check if an offset for the external data was set. */
  511. if (params->external_offset > 0) {
  512. if (params->external_offset < new_size) {
  513. fprintf(stderr,
  514. "External offset %x overlaps FIT length %x\n",
  515. params->external_offset, new_size);
  516. ret = -EINVAL;
  517. goto err;
  518. }
  519. new_size = params->external_offset;
  520. }
  521. if (lseek(fd, new_size, SEEK_SET) < 0) {
  522. debug("%s: Failed to seek to end of file: %s\n", __func__,
  523. strerror(errno));
  524. ret = -EIO;
  525. goto err;
  526. }
  527. if (write(fd, buf, buf_ptr) != buf_ptr) {
  528. debug("%s: Failed to write external data to file %s\n",
  529. __func__, strerror(errno));
  530. ret = -EIO;
  531. goto err;
  532. }
  533. free(buf);
  534. close(fd);
  535. return 0;
  536. err_munmap:
  537. munmap(fdt, sbuf.st_size);
  538. err:
  539. free(buf);
  540. close(fd);
  541. return ret;
  542. }
  543. static int fit_import_data(struct image_tool_params *params, const char *fname)
  544. {
  545. void *fdt, *old_fdt;
  546. int fit_size, new_size, size, data_base;
  547. int fd;
  548. struct stat sbuf;
  549. int ret;
  550. int images;
  551. int node;
  552. fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false);
  553. if (fd < 0)
  554. return -EIO;
  555. fit_size = fdt_totalsize(old_fdt);
  556. data_base = ALIGN(fit_size, 4);
  557. /* Allocate space to hold the new FIT */
  558. size = sbuf.st_size + 16384;
  559. fdt = calloc(1, size);
  560. if (!fdt) {
  561. fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
  562. __func__, size);
  563. ret = -ENOMEM;
  564. goto err_munmap;
  565. }
  566. ret = fdt_open_into(old_fdt, fdt, size);
  567. if (ret) {
  568. debug("%s: Failed to expand FIT: %s\n", __func__,
  569. fdt_strerror(errno));
  570. ret = -EINVAL;
  571. goto err_munmap;
  572. }
  573. images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
  574. if (images < 0) {
  575. debug("%s: Cannot find /images node: %d\n", __func__, images);
  576. ret = -EINVAL;
  577. goto err_munmap;
  578. }
  579. for (node = fdt_first_subnode(fdt, images);
  580. node >= 0;
  581. node = fdt_next_subnode(fdt, node)) {
  582. int buf_ptr;
  583. int len;
  584. buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
  585. len = fdtdec_get_int(fdt, node, "data-size", -1);
  586. if (buf_ptr == -1 || len == -1)
  587. continue;
  588. debug("Importing data size %x\n", len);
  589. ret = fdt_setprop(fdt, node, "data",
  590. old_fdt + data_base + buf_ptr, len);
  591. if (ret) {
  592. debug("%s: Failed to write property: %s\n", __func__,
  593. fdt_strerror(ret));
  594. ret = -EINVAL;
  595. goto err_munmap;
  596. }
  597. }
  598. munmap(old_fdt, sbuf.st_size);
  599. /* Close the old fd so we can re-use it. */
  600. close(fd);
  601. /* Pack the FDT and place the data after it */
  602. fdt_pack(fdt);
  603. new_size = fdt_totalsize(fdt);
  604. debug("Size expanded from %x to %x\n", fit_size, new_size);
  605. fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
  606. if (fd < 0) {
  607. fprintf(stderr, "%s: Can't open %s: %s\n",
  608. params->cmdname, fname, strerror(errno));
  609. ret = -EIO;
  610. goto err;
  611. }
  612. if (write(fd, fdt, new_size) != new_size) {
  613. debug("%s: Failed to write external data to file %s\n",
  614. __func__, strerror(errno));
  615. ret = -EIO;
  616. goto err;
  617. }
  618. free(fdt);
  619. close(fd);
  620. return 0;
  621. err_munmap:
  622. munmap(old_fdt, sbuf.st_size);
  623. err:
  624. free(fdt);
  625. close(fd);
  626. return ret;
  627. }
  628. /**
  629. * fit_handle_file - main FIT file processing function
  630. *
  631. * fit_handle_file() runs dtc to convert .its to .itb, includes
  632. * binary data, updates timestamp property and calculates hashes.
  633. *
  634. * datafile - .its file
  635. * imagefile - .itb file
  636. *
  637. * returns:
  638. * only on success, otherwise calls exit (EXIT_FAILURE);
  639. */
  640. static int fit_handle_file(struct image_tool_params *params)
  641. {
  642. char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
  643. char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0};
  644. char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
  645. size_t size_inc;
  646. int ret;
  647. /* Flattened Image Tree (FIT) format handling */
  648. debug ("FIT format handling\n");
  649. /* call dtc to include binary properties into the tmp file */
  650. if (strlen (params->imagefile) +
  651. strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
  652. fprintf (stderr, "%s: Image file name (%s) too long, "
  653. "can't create tmpfile.\n",
  654. params->imagefile, params->cmdname);
  655. return (EXIT_FAILURE);
  656. }
  657. sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
  658. /* We either compile the source file, or use the existing FIT image */
  659. if (params->auto_fit) {
  660. if (fit_build(params, tmpfile)) {
  661. fprintf(stderr, "%s: failed to build FIT\n",
  662. params->cmdname);
  663. return EXIT_FAILURE;
  664. }
  665. *cmd = '\0';
  666. } else if (params->datafile) {
  667. /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */
  668. snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"",
  669. MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
  670. debug("Trying to execute \"%s\"\n", cmd);
  671. } else {
  672. snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
  673. params->imagefile, tmpfile);
  674. }
  675. if (strlen(cmd) >= MKIMAGE_MAX_DTC_CMDLINE_LEN - 1) {
  676. fprintf(stderr, "WARNING: command-line for FIT creation might be truncated and will probably fail.\n");
  677. }
  678. if (*cmd && system(cmd) == -1) {
  679. fprintf (stderr, "%s: system(%s) failed: %s\n",
  680. params->cmdname, cmd, strerror(errno));
  681. goto err_system;
  682. }
  683. /* Move the data so it is internal to the FIT, if needed */
  684. ret = fit_import_data(params, tmpfile);
  685. if (ret)
  686. goto err_system;
  687. /*
  688. * Copy the tmpfile to bakfile, then in the following loop
  689. * we copy bakfile to tmpfile. So we always start from the
  690. * beginning.
  691. */
  692. sprintf(bakfile, "%s%s", tmpfile, ".bak");
  693. rename(tmpfile, bakfile);
  694. /*
  695. * Set hashes for images in the blob. Unfortunately we may need more
  696. * space in either FDT, so keep trying until we succeed.
  697. *
  698. * Note: this is pretty inefficient for signing, since we must
  699. * calculate the signature every time. It would be better to calculate
  700. * all the data and then store it in a separate step. However, this
  701. * would be considerably more complex to implement. Generally a few
  702. * steps of this loop is enough to sign with several keys.
  703. */
  704. for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
  705. if (copyfile(bakfile, tmpfile) < 0) {
  706. printf("Can't copy %s to %s\n", bakfile, tmpfile);
  707. ret = -EIO;
  708. break;
  709. }
  710. ret = fit_add_file_data(params, size_inc, tmpfile);
  711. if (!ret || ret != -ENOSPC)
  712. break;
  713. }
  714. if (ret) {
  715. fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
  716. params->cmdname, ret);
  717. goto err_system;
  718. }
  719. /* Move the data so it is external to the FIT, if requested */
  720. if (params->external_data) {
  721. ret = fit_extract_data(params, tmpfile);
  722. if (ret)
  723. goto err_system;
  724. }
  725. if (rename (tmpfile, params->imagefile) == -1) {
  726. fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
  727. params->cmdname, tmpfile, params->imagefile,
  728. strerror (errno));
  729. unlink (tmpfile);
  730. unlink(bakfile);
  731. unlink (params->imagefile);
  732. return EXIT_FAILURE;
  733. }
  734. unlink(bakfile);
  735. return EXIT_SUCCESS;
  736. err_system:
  737. unlink(tmpfile);
  738. unlink(bakfile);
  739. return -1;
  740. }
  741. /**
  742. * fit_image_extract - extract a FIT component image
  743. * @fit: pointer to the FIT format image header
  744. * @image_noffset: offset of the component image node
  745. * @file_name: name of the file to store the FIT sub-image
  746. *
  747. * returns:
  748. * zero in case of success or a negative value if fail.
  749. */
  750. static int fit_image_extract(
  751. const void *fit,
  752. int image_noffset,
  753. const char *file_name)
  754. {
  755. const void *file_data;
  756. size_t file_size = 0;
  757. int ret;
  758. /* get the data address and size of component at offset "image_noffset" */
  759. ret = fit_image_get_data_and_size(fit, image_noffset, &file_data, &file_size);
  760. if (ret) {
  761. fprintf(stderr, "Could not get component information\n");
  762. return ret;
  763. }
  764. /* save the "file_data" into the file specified by "file_name" */
  765. return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
  766. }
  767. /**
  768. * fit_extract_contents - retrieve a sub-image component from the FIT image
  769. * @ptr: pointer to the FIT format image header
  770. * @params: command line parameters
  771. *
  772. * returns:
  773. * zero in case of success or a negative value if fail.
  774. */
  775. static int fit_extract_contents(void *ptr, struct image_tool_params *params)
  776. {
  777. int images_noffset;
  778. int noffset;
  779. int ndepth;
  780. const void *fit = ptr;
  781. int count = 0;
  782. const char *p;
  783. /* Indent string is defined in header image.h */
  784. p = IMAGE_INDENT_STRING;
  785. /* Find images parent node offset */
  786. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  787. if (images_noffset < 0) {
  788. printf("Can't find images parent node '%s' (%s)\n",
  789. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  790. return -1;
  791. }
  792. /* Avoid any overrun */
  793. count = fit_get_subimage_count(fit, images_noffset);
  794. if ((params->pflag < 0) || (count <= params->pflag)) {
  795. printf("No such component at '%d'\n", params->pflag);
  796. return -1;
  797. }
  798. /* Process its subnodes, extract the desired component from image */
  799. for (ndepth = 0, count = 0,
  800. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  801. (noffset >= 0) && (ndepth > 0);
  802. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  803. if (ndepth == 1) {
  804. /*
  805. * Direct child node of the images parent node,
  806. * i.e. component image node.
  807. */
  808. if (params->pflag == count) {
  809. printf("Extracted:\n%s Image %u (%s)\n", p,
  810. count, fit_get_name(fit, noffset, NULL));
  811. fit_image_print(fit, noffset, p);
  812. return fit_image_extract(fit, noffset,
  813. params->outfile);
  814. }
  815. count++;
  816. }
  817. }
  818. return 0;
  819. }
  820. static int fit_check_params(struct image_tool_params *params)
  821. {
  822. if (params->auto_fit)
  823. return 0;
  824. return ((params->dflag && params->fflag) ||
  825. (params->fflag && params->lflag) ||
  826. (params->lflag && params->dflag));
  827. }
  828. U_BOOT_IMAGE_TYPE(
  829. fitimage,
  830. "FIT Image support",
  831. sizeof(struct legacy_img_hdr),
  832. (void *)&header,
  833. fit_check_params,
  834. fit_verify_header,
  835. fit_print_header,
  836. NULL,
  837. fit_extract_contents,
  838. fit_check_image_types,
  839. fit_handle_file,
  840. NULL /* FIT images use DTB header */
  841. );