fit_image.c 23 KB

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