fit_image.c 22 KB

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