fit_image.c 23 KB

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