fit_image.c 22 KB

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