fit_image.c 19 KB

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