fit_image.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
  201. fdt_begin_node(fdt, str);
  202. get_basename(str, sizeof(str), cont->fname);
  203. fdt_property_string(fdt, FIT_DESC_PROP, str);
  204. ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
  205. cont->fname);
  206. if (ret)
  207. return ret;
  208. fdt_property_string(fdt, FIT_TYPE_PROP, typename);
  209. fdt_property_string(fdt, FIT_ARCH_PROP,
  210. genimg_get_arch_short_name(params->arch));
  211. fdt_property_string(fdt, FIT_COMP_PROP,
  212. genimg_get_comp_short_name(IH_COMP_NONE));
  213. fdt_end_node(fdt);
  214. }
  215. /* And a ramdisk file if available */
  216. if (params->fit_ramdisk) {
  217. fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
  218. fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP);
  219. fdt_property_string(fdt, FIT_OS_PROP,
  220. genimg_get_os_short_name(params->os));
  221. ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
  222. params->fit_ramdisk);
  223. if (ret)
  224. return ret;
  225. fdt_end_node(fdt);
  226. }
  227. fdt_end_node(fdt);
  228. return 0;
  229. }
  230. /**
  231. * fit_write_configs() - Write out a list of configurations to the FIT
  232. *
  233. * If there are device tree files, we include a configuration for each, which
  234. * selects the main image (params->datafile) and its corresponding device
  235. * tree file.
  236. *
  237. * Otherwise we just create a configuration with the main image in it.
  238. */
  239. static void fit_write_configs(struct image_tool_params *params, char *fdt)
  240. {
  241. struct content_info *cont;
  242. const char *typename;
  243. char str[100];
  244. int upto;
  245. fdt_begin_node(fdt, "configurations");
  246. fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1");
  247. upto = 0;
  248. for (cont = params->content_head; cont; cont = cont->next) {
  249. if (cont->type != IH_TYPE_FLATDT)
  250. continue;
  251. typename = genimg_get_type_short_name(cont->type);
  252. snprintf(str, sizeof(str), "conf-%d", ++upto);
  253. fdt_begin_node(fdt, str);
  254. get_basename(str, sizeof(str), cont->fname);
  255. fdt_property_string(fdt, FIT_DESC_PROP, str);
  256. typename = genimg_get_type_short_name(params->fit_image_type);
  257. snprintf(str, sizeof(str), "%s-1", typename);
  258. fdt_property_string(fdt, typename, str);
  259. fdt_property_string(fdt, FIT_LOADABLE_PROP, str);
  260. if (params->fit_ramdisk)
  261. fdt_property_string(fdt, FIT_RAMDISK_PROP,
  262. FIT_RAMDISK_PROP "-1");
  263. snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
  264. fdt_property_string(fdt, FIT_FDT_PROP, str);
  265. fdt_end_node(fdt);
  266. }
  267. if (!upto) {
  268. fdt_begin_node(fdt, "conf-1");
  269. typename = genimg_get_type_short_name(params->fit_image_type);
  270. snprintf(str, sizeof(str), "%s-1", typename);
  271. fdt_property_string(fdt, typename, str);
  272. if (params->fit_ramdisk)
  273. fdt_property_string(fdt, FIT_RAMDISK_PROP,
  274. FIT_RAMDISK_PROP "-1");
  275. fdt_end_node(fdt);
  276. }
  277. fdt_end_node(fdt);
  278. }
  279. static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
  280. {
  281. int ret;
  282. ret = fdt_create(fdt, size);
  283. if (ret)
  284. return ret;
  285. fdt_finish_reservemap(fdt);
  286. fdt_begin_node(fdt, "");
  287. fdt_property_strf(fdt, FIT_DESC_PROP,
  288. "%s image with one or more FDT blobs",
  289. genimg_get_type_name(params->fit_image_type));
  290. fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
  291. fdt_property_u32(fdt, "#address-cells", 1);
  292. ret = fit_write_images(params, fdt);
  293. if (ret)
  294. return ret;
  295. fit_write_configs(params, fdt);
  296. fdt_end_node(fdt);
  297. ret = fdt_finish(fdt);
  298. if (ret)
  299. return ret;
  300. return fdt_totalsize(fdt);
  301. }
  302. static int fit_build(struct image_tool_params *params, const char *fname)
  303. {
  304. char *buf;
  305. int size;
  306. int ret;
  307. int fd;
  308. size = fit_calc_size(params);
  309. if (size < 0)
  310. return -1;
  311. buf = malloc(size);
  312. if (!buf) {
  313. fprintf(stderr, "%s: Out of memory (%d bytes)\n",
  314. params->cmdname, size);
  315. return -1;
  316. }
  317. ret = fit_build_fdt(params, buf, size);
  318. if (ret < 0) {
  319. fprintf(stderr, "%s: Failed to build FIT image\n",
  320. params->cmdname);
  321. goto err_buf;
  322. }
  323. size = ret;
  324. fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
  325. if (fd < 0) {
  326. fprintf(stderr, "%s: Can't open %s: %s\n",
  327. params->cmdname, fname, strerror(errno));
  328. goto err_buf;
  329. }
  330. ret = write(fd, buf, size);
  331. if (ret != size) {
  332. fprintf(stderr, "%s: Can't write %s: %s\n",
  333. params->cmdname, fname, strerror(errno));
  334. goto err;
  335. }
  336. close(fd);
  337. free(buf);
  338. return 0;
  339. err:
  340. close(fd);
  341. err_buf:
  342. free(buf);
  343. return -1;
  344. }
  345. /**
  346. * fit_extract_data() - Move all data outside the FIT
  347. *
  348. * This takes a normal FIT file and removes all the 'data' properties from it.
  349. * The data is placed in an area after the FIT so that it can be accessed
  350. * using an offset into that area. The 'data' properties turn into
  351. * 'data-offset' properties.
  352. *
  353. * This function cannot cope with FITs with 'data-offset' properties. All
  354. * data must be in 'data' properties on entry.
  355. */
  356. static int fit_extract_data(struct image_tool_params *params, const char *fname)
  357. {
  358. void *buf;
  359. int buf_ptr;
  360. int fit_size, new_size;
  361. int fd;
  362. struct stat sbuf;
  363. void *fdt;
  364. int ret;
  365. int images;
  366. int node;
  367. fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false);
  368. if (fd < 0)
  369. return -EIO;
  370. fit_size = fdt_totalsize(fdt);
  371. /* Allocate space to hold the image data we will extract */
  372. buf = malloc(fit_size);
  373. if (!buf) {
  374. ret = -ENOMEM;
  375. goto err_munmap;
  376. }
  377. buf_ptr = 0;
  378. images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
  379. if (images < 0) {
  380. debug("%s: Cannot find /images node: %d\n", __func__, images);
  381. ret = -EINVAL;
  382. goto err_munmap;
  383. }
  384. for (node = fdt_first_subnode(fdt, images);
  385. node >= 0;
  386. node = fdt_next_subnode(fdt, node)) {
  387. const char *data;
  388. int len;
  389. data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len);
  390. if (!data)
  391. continue;
  392. memcpy(buf + buf_ptr, data, len);
  393. debug("Extracting data size %x\n", len);
  394. ret = fdt_delprop(fdt, node, FIT_DATA_PROP);
  395. if (ret) {
  396. ret = -EPERM;
  397. goto err_munmap;
  398. }
  399. if (params->external_offset > 0) {
  400. /* An external offset positions the data absolutely. */
  401. fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP,
  402. params->external_offset + buf_ptr);
  403. } else {
  404. fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP,
  405. buf_ptr);
  406. }
  407. fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
  408. buf_ptr += (len + 3) & ~3;
  409. }
  410. /* Pack the FDT and place the data after it */
  411. fdt_pack(fdt);
  412. debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
  413. debug("External data size %x\n", buf_ptr);
  414. new_size = fdt_totalsize(fdt);
  415. new_size = (new_size + 3) & ~3;
  416. munmap(fdt, sbuf.st_size);
  417. if (ftruncate(fd, new_size)) {
  418. debug("%s: Failed to truncate file: %s\n", __func__,
  419. strerror(errno));
  420. ret = -EIO;
  421. goto err;
  422. }
  423. /* Check if an offset for the external data was set. */
  424. if (params->external_offset > 0) {
  425. if (params->external_offset < new_size) {
  426. debug("External offset %x overlaps FIT length %x",
  427. params->external_offset, new_size);
  428. ret = -EINVAL;
  429. goto err;
  430. }
  431. new_size = params->external_offset;
  432. }
  433. if (lseek(fd, new_size, SEEK_SET) < 0) {
  434. debug("%s: Failed to seek to end of file: %s\n", __func__,
  435. strerror(errno));
  436. ret = -EIO;
  437. goto err;
  438. }
  439. if (write(fd, buf, buf_ptr) != buf_ptr) {
  440. debug("%s: Failed to write external data to file %s\n",
  441. __func__, strerror(errno));
  442. ret = -EIO;
  443. goto err;
  444. }
  445. free(buf);
  446. close(fd);
  447. return 0;
  448. err_munmap:
  449. munmap(fdt, sbuf.st_size);
  450. err:
  451. if (buf)
  452. free(buf);
  453. close(fd);
  454. return ret;
  455. }
  456. static int fit_import_data(struct image_tool_params *params, const char *fname)
  457. {
  458. void *fdt, *old_fdt;
  459. int fit_size, new_size, size, data_base;
  460. int fd;
  461. struct stat sbuf;
  462. int ret;
  463. int images;
  464. int node;
  465. fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false);
  466. if (fd < 0)
  467. return -EIO;
  468. fit_size = fdt_totalsize(old_fdt);
  469. data_base = (fit_size + 3) & ~3;
  470. /* Allocate space to hold the new FIT */
  471. size = sbuf.st_size + 16384;
  472. fdt = malloc(size);
  473. if (!fdt) {
  474. fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
  475. __func__, size);
  476. ret = -ENOMEM;
  477. goto err_has_fd;
  478. }
  479. ret = fdt_open_into(old_fdt, fdt, size);
  480. if (ret) {
  481. debug("%s: Failed to expand FIT: %s\n", __func__,
  482. fdt_strerror(errno));
  483. ret = -EINVAL;
  484. goto err_has_fd;
  485. }
  486. images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
  487. if (images < 0) {
  488. debug("%s: Cannot find /images node: %d\n", __func__, images);
  489. ret = -EINVAL;
  490. goto err_has_fd;
  491. }
  492. for (node = fdt_first_subnode(fdt, images);
  493. node >= 0;
  494. node = fdt_next_subnode(fdt, node)) {
  495. int buf_ptr;
  496. int len;
  497. buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
  498. len = fdtdec_get_int(fdt, node, "data-size", -1);
  499. if (buf_ptr == -1 || len == -1)
  500. continue;
  501. debug("Importing data size %x\n", len);
  502. ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
  503. len);
  504. if (ret) {
  505. debug("%s: Failed to write property: %s\n", __func__,
  506. fdt_strerror(ret));
  507. ret = -EINVAL;
  508. goto err_has_fd;
  509. }
  510. }
  511. /* Close the old fd so we can re-use it. */
  512. close(fd);
  513. /* Pack the FDT and place the data after it */
  514. fdt_pack(fdt);
  515. new_size = fdt_totalsize(fdt);
  516. debug("Size expanded from %x to %x\n", fit_size, new_size);
  517. fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
  518. if (fd < 0) {
  519. fprintf(stderr, "%s: Can't open %s: %s\n",
  520. params->cmdname, fname, strerror(errno));
  521. ret = -EIO;
  522. goto err_no_fd;
  523. }
  524. if (write(fd, fdt, new_size) != new_size) {
  525. debug("%s: Failed to write external data to file %s\n",
  526. __func__, strerror(errno));
  527. ret = -EIO;
  528. goto err_has_fd;
  529. }
  530. ret = 0;
  531. err_has_fd:
  532. close(fd);
  533. err_no_fd:
  534. munmap(old_fdt, sbuf.st_size);
  535. free(fdt);
  536. return ret;
  537. }
  538. /**
  539. * fit_handle_file - main FIT file processing function
  540. *
  541. * fit_handle_file() runs dtc to convert .its to .itb, includes
  542. * binary data, updates timestamp property and calculates hashes.
  543. *
  544. * datafile - .its file
  545. * imagefile - .itb file
  546. *
  547. * returns:
  548. * only on success, otherwise calls exit (EXIT_FAILURE);
  549. */
  550. static int fit_handle_file(struct image_tool_params *params)
  551. {
  552. char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
  553. char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
  554. size_t size_inc;
  555. int ret;
  556. /* Flattened Image Tree (FIT) format handling */
  557. debug ("FIT format handling\n");
  558. /* call dtc to include binary properties into the tmp file */
  559. if (strlen (params->imagefile) +
  560. strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
  561. fprintf (stderr, "%s: Image file name (%s) too long, "
  562. "can't create tmpfile",
  563. params->imagefile, params->cmdname);
  564. return (EXIT_FAILURE);
  565. }
  566. sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
  567. /* We either compile the source file, or use the existing FIT image */
  568. if (params->auto_its) {
  569. if (fit_build(params, tmpfile)) {
  570. fprintf(stderr, "%s: failed to build FIT\n",
  571. params->cmdname);
  572. return EXIT_FAILURE;
  573. }
  574. *cmd = '\0';
  575. } else if (params->datafile) {
  576. /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */
  577. snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"",
  578. MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
  579. debug("Trying to execute \"%s\"\n", cmd);
  580. } else {
  581. snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
  582. params->imagefile, tmpfile);
  583. }
  584. if (*cmd && system(cmd) == -1) {
  585. fprintf (stderr, "%s: system(%s) failed: %s\n",
  586. params->cmdname, cmd, strerror(errno));
  587. goto err_system;
  588. }
  589. /* Move the data so it is internal to the FIT, if needed */
  590. ret = fit_import_data(params, tmpfile);
  591. if (ret)
  592. goto err_system;
  593. /*
  594. * Set hashes for images in the blob. Unfortunately we may need more
  595. * space in either FDT, so keep trying until we succeed.
  596. *
  597. * Note: this is pretty inefficient for signing, since we must
  598. * calculate the signature every time. It would be better to calculate
  599. * all the data and then store it in a separate step. However, this
  600. * would be considerably more complex to implement. Generally a few
  601. * steps of this loop is enough to sign with several keys.
  602. */
  603. for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
  604. ret = fit_add_file_data(params, size_inc, tmpfile);
  605. if (!ret || ret != -ENOSPC)
  606. break;
  607. }
  608. if (ret) {
  609. fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
  610. params->cmdname, ret);
  611. goto err_system;
  612. }
  613. /* Move the data so it is external to the FIT, if requested */
  614. if (params->external_data) {
  615. ret = fit_extract_data(params, tmpfile);
  616. if (ret)
  617. goto err_system;
  618. }
  619. if (rename (tmpfile, params->imagefile) == -1) {
  620. fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
  621. params->cmdname, tmpfile, params->imagefile,
  622. strerror (errno));
  623. unlink (tmpfile);
  624. unlink (params->imagefile);
  625. return EXIT_FAILURE;
  626. }
  627. return EXIT_SUCCESS;
  628. err_system:
  629. unlink(tmpfile);
  630. return -1;
  631. }
  632. /**
  633. * fit_image_extract - extract a FIT component image
  634. * @fit: pointer to the FIT format image header
  635. * @image_noffset: offset of the component image node
  636. * @file_name: name of the file to store the FIT sub-image
  637. *
  638. * returns:
  639. * zero in case of success or a negative value if fail.
  640. */
  641. static int fit_image_extract(
  642. const void *fit,
  643. int image_noffset,
  644. const char *file_name)
  645. {
  646. const void *file_data;
  647. size_t file_size = 0;
  648. /* get the "data" property of component at offset "image_noffset" */
  649. fit_image_get_data(fit, image_noffset, &file_data, &file_size);
  650. /* save the "file_data" into the file specified by "file_name" */
  651. return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
  652. }
  653. /**
  654. * fit_extract_contents - retrieve a sub-image component from the FIT image
  655. * @ptr: pointer to the FIT format image header
  656. * @params: command line parameters
  657. *
  658. * returns:
  659. * zero in case of success or a negative value if fail.
  660. */
  661. static int fit_extract_contents(void *ptr, struct image_tool_params *params)
  662. {
  663. int images_noffset;
  664. int noffset;
  665. int ndepth;
  666. const void *fit = ptr;
  667. int count = 0;
  668. const char *p;
  669. /* Indent string is defined in header image.h */
  670. p = IMAGE_INDENT_STRING;
  671. if (!fit_check_format(fit)) {
  672. printf("Bad FIT image format\n");
  673. return -1;
  674. }
  675. /* Find images parent node offset */
  676. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  677. if (images_noffset < 0) {
  678. printf("Can't find images parent node '%s' (%s)\n",
  679. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  680. return -1;
  681. }
  682. /* Avoid any overrun */
  683. count = fit_get_subimage_count(fit, images_noffset);
  684. if ((params->pflag < 0) || (count <= params->pflag)) {
  685. printf("No such component at '%d'\n", params->pflag);
  686. return -1;
  687. }
  688. /* Process its subnodes, extract the desired component from image */
  689. for (ndepth = 0, count = 0,
  690. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  691. (noffset >= 0) && (ndepth > 0);
  692. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  693. if (ndepth == 1) {
  694. /*
  695. * Direct child node of the images parent node,
  696. * i.e. component image node.
  697. */
  698. if (params->pflag == count) {
  699. printf("Extracted:\n%s Image %u (%s)\n", p,
  700. count, fit_get_name(fit, noffset, NULL));
  701. fit_image_print(fit, noffset, p);
  702. return fit_image_extract(fit, noffset,
  703. params->outfile);
  704. }
  705. count++;
  706. }
  707. }
  708. return 0;
  709. }
  710. static int fit_check_params(struct image_tool_params *params)
  711. {
  712. if (params->auto_its)
  713. return 0;
  714. return ((params->dflag && (params->fflag || params->lflag)) ||
  715. (params->fflag && (params->dflag || params->lflag)) ||
  716. (params->lflag && (params->dflag || params->fflag)));
  717. }
  718. U_BOOT_IMAGE_TYPE(
  719. fitimage,
  720. "FIT Image support",
  721. sizeof(image_header_t),
  722. (void *)&header,
  723. fit_check_params,
  724. fit_verify_header,
  725. fit_print_contents,
  726. NULL,
  727. fit_extract_contents,
  728. fit_check_image_types,
  729. fit_handle_file,
  730. NULL /* FIT images use DTB header */
  731. );