Fork of the vendor (Boundary Devices) u-boot for Reform 2, with minor tweaks. The goal is to migrate to mainstream u-boot or barebox ASAP. The main impediment so far is the 4GB RAM config.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

827 lines
20 KiB

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