fit_image.c 20 KB

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