fit_image.c 19 KB

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