fit_image.c 23 KB

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