fit_image.c 20 KB

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