fit_image.c 21 KB

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