fit_image.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. ret = 0;
  392. err:
  393. close(fd);
  394. return ret;
  395. }
  396. static int fit_import_data(struct image_tool_params *params, const char *fname)
  397. {
  398. void *fdt, *old_fdt;
  399. int fit_size, new_size, size, data_base;
  400. int fd;
  401. struct stat sbuf;
  402. int ret;
  403. int images;
  404. int node;
  405. fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false);
  406. if (fd < 0)
  407. return -EIO;
  408. fit_size = fdt_totalsize(old_fdt);
  409. data_base = (fit_size + 3) & ~3;
  410. /* Allocate space to hold the new FIT */
  411. size = sbuf.st_size + 16384;
  412. fdt = malloc(size);
  413. if (!fdt) {
  414. fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
  415. __func__, size);
  416. ret = -ENOMEM;
  417. goto err;
  418. }
  419. ret = fdt_open_into(old_fdt, fdt, size);
  420. if (ret) {
  421. debug("%s: Failed to expand FIT: %s\n", __func__,
  422. fdt_strerror(errno));
  423. ret = -EINVAL;
  424. goto err;
  425. }
  426. images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
  427. if (images < 0) {
  428. debug("%s: Cannot find /images node: %d\n", __func__, images);
  429. ret = -EINVAL;
  430. goto err;
  431. }
  432. for (node = fdt_first_subnode(fdt, images);
  433. node >= 0;
  434. node = fdt_next_subnode(fdt, node)) {
  435. int buf_ptr;
  436. int len;
  437. buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
  438. len = fdtdec_get_int(fdt, node, "data-size", -1);
  439. if (buf_ptr == -1 || len == -1)
  440. continue;
  441. debug("Importing data size %x\n", len);
  442. ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
  443. len);
  444. if (ret) {
  445. debug("%s: Failed to write property: %s\n", __func__,
  446. fdt_strerror(ret));
  447. ret = -EINVAL;
  448. goto err;
  449. }
  450. }
  451. munmap(old_fdt, sbuf.st_size);
  452. close(fd);
  453. /* Pack the FDT and place the data after it */
  454. fdt_pack(fdt);
  455. new_size = fdt_totalsize(fdt);
  456. debug("Size expanded from %x to %x\n", fit_size, new_size);
  457. fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
  458. if (fd < 0) {
  459. fprintf(stderr, "%s: Can't open %s: %s\n",
  460. params->cmdname, fname, strerror(errno));
  461. ret = -EIO;
  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. ret = 0;
  471. err:
  472. close(fd);
  473. return ret;
  474. }
  475. /**
  476. * fit_handle_file - main FIT file processing function
  477. *
  478. * fit_handle_file() runs dtc to convert .its to .itb, includes
  479. * binary data, updates timestamp property and calculates hashes.
  480. *
  481. * datafile - .its file
  482. * imagefile - .itb file
  483. *
  484. * returns:
  485. * only on success, otherwise calls exit (EXIT_FAILURE);
  486. */
  487. static int fit_handle_file(struct image_tool_params *params)
  488. {
  489. char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
  490. char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
  491. size_t size_inc;
  492. int ret;
  493. /* Flattened Image Tree (FIT) format handling */
  494. debug ("FIT format handling\n");
  495. /* call dtc to include binary properties into the tmp file */
  496. if (strlen (params->imagefile) +
  497. strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
  498. fprintf (stderr, "%s: Image file name (%s) too long, "
  499. "can't create tmpfile",
  500. params->imagefile, params->cmdname);
  501. return (EXIT_FAILURE);
  502. }
  503. sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
  504. /* We either compile the source file, or use the existing FIT image */
  505. if (params->auto_its) {
  506. if (fit_build(params, tmpfile)) {
  507. fprintf(stderr, "%s: failed to build FIT\n",
  508. params->cmdname);
  509. return EXIT_FAILURE;
  510. }
  511. *cmd = '\0';
  512. } else if (params->datafile) {
  513. /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
  514. snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
  515. MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
  516. debug("Trying to execute \"%s\"\n", cmd);
  517. } else {
  518. snprintf(cmd, sizeof(cmd), "cp %s %s",
  519. params->imagefile, tmpfile);
  520. }
  521. if (*cmd && system(cmd) == -1) {
  522. fprintf (stderr, "%s: system(%s) failed: %s\n",
  523. params->cmdname, cmd, strerror(errno));
  524. goto err_system;
  525. }
  526. /* Move the data so it is internal to the FIT, if needed */
  527. ret = fit_import_data(params, tmpfile);
  528. if (ret)
  529. goto err_system;
  530. /*
  531. * Set hashes for images in the blob. Unfortunately we may need more
  532. * space in either FDT, so keep trying until we succeed.
  533. *
  534. * Note: this is pretty inefficient for signing, since we must
  535. * calculate the signature every time. It would be better to calculate
  536. * all the data and then store it in a separate step. However, this
  537. * would be considerably more complex to implement. Generally a few
  538. * steps of this loop is enough to sign with several keys.
  539. */
  540. for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
  541. ret = fit_add_file_data(params, size_inc, tmpfile);
  542. if (!ret || ret != -ENOSPC)
  543. break;
  544. }
  545. if (ret) {
  546. fprintf(stderr, "%s Can't add hashes to FIT blob\n",
  547. params->cmdname);
  548. goto err_system;
  549. }
  550. /* Move the data so it is external to the FIT, if requested */
  551. if (params->external_data) {
  552. ret = fit_extract_data(params, tmpfile);
  553. if (ret)
  554. goto err_system;
  555. }
  556. if (rename (tmpfile, params->imagefile) == -1) {
  557. fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
  558. params->cmdname, tmpfile, params->imagefile,
  559. strerror (errno));
  560. unlink (tmpfile);
  561. unlink (params->imagefile);
  562. return EXIT_FAILURE;
  563. }
  564. return EXIT_SUCCESS;
  565. err_system:
  566. unlink(tmpfile);
  567. return -1;
  568. }
  569. /**
  570. * fit_image_extract - extract a FIT component image
  571. * @fit: pointer to the FIT format image header
  572. * @image_noffset: offset of the component image node
  573. * @file_name: name of the file to store the FIT sub-image
  574. *
  575. * returns:
  576. * zero in case of success or a negative value if fail.
  577. */
  578. static int fit_image_extract(
  579. const void *fit,
  580. int image_noffset,
  581. const char *file_name)
  582. {
  583. const void *file_data;
  584. size_t file_size = 0;
  585. /* get the "data" property of component at offset "image_noffset" */
  586. fit_image_get_data(fit, image_noffset, &file_data, &file_size);
  587. /* save the "file_data" into the file specified by "file_name" */
  588. return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
  589. }
  590. /**
  591. * fit_extract_contents - retrieve a sub-image component from the FIT image
  592. * @ptr: pointer to the FIT format image header
  593. * @params: command line parameters
  594. *
  595. * returns:
  596. * zero in case of success or a negative value if fail.
  597. */
  598. static int fit_extract_contents(void *ptr, struct image_tool_params *params)
  599. {
  600. int images_noffset;
  601. int noffset;
  602. int ndepth;
  603. const void *fit = ptr;
  604. int count = 0;
  605. const char *p;
  606. /* Indent string is defined in header image.h */
  607. p = IMAGE_INDENT_STRING;
  608. if (!fit_check_format(fit)) {
  609. printf("Bad FIT image format\n");
  610. return -1;
  611. }
  612. /* Find images parent node offset */
  613. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  614. if (images_noffset < 0) {
  615. printf("Can't find images parent node '%s' (%s)\n",
  616. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  617. return -1;
  618. }
  619. /* Avoid any overrun */
  620. count = fit_get_subimage_count(fit, images_noffset);
  621. if ((params->pflag < 0) || (count <= params->pflag)) {
  622. printf("No such component at '%d'\n", params->pflag);
  623. return -1;
  624. }
  625. /* Process its subnodes, extract the desired component from image */
  626. for (ndepth = 0, count = 0,
  627. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  628. (noffset >= 0) && (ndepth > 0);
  629. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  630. if (ndepth == 1) {
  631. /*
  632. * Direct child node of the images parent node,
  633. * i.e. component image node.
  634. */
  635. if (params->pflag == count) {
  636. printf("Extracted:\n%s Image %u (%s)\n", p,
  637. count, fit_get_name(fit, noffset, NULL));
  638. fit_image_print(fit, noffset, p);
  639. return fit_image_extract(fit, noffset,
  640. params->outfile);
  641. }
  642. count++;
  643. }
  644. }
  645. return 0;
  646. }
  647. static int fit_check_params(struct image_tool_params *params)
  648. {
  649. if (params->auto_its)
  650. return 0;
  651. return ((params->dflag && (params->fflag || params->lflag)) ||
  652. (params->fflag && (params->dflag || params->lflag)) ||
  653. (params->lflag && (params->dflag || params->fflag)));
  654. }
  655. U_BOOT_IMAGE_TYPE(
  656. fitimage,
  657. "FIT Image support",
  658. sizeof(image_header_t),
  659. (void *)&header,
  660. fit_check_params,
  661. fit_verify_header,
  662. fit_print_contents,
  663. NULL,
  664. fit_extract_contents,
  665. fit_check_image_types,
  666. fit_handle_file,
  667. NULL /* FIT images use DTB header */
  668. );