fit_image.c 19 KB

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