fit_image.c 20 KB

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