fit_image.c 19 KB

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