image-host.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Google Inc.
  4. *
  5. * (C) Copyright 2008 Semihalf
  6. *
  7. * (C) Copyright 2000-2006
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. */
  10. #include "mkimage.h"
  11. #include <bootm.h>
  12. #include <image.h>
  13. #include <version.h>
  14. /**
  15. * fit_set_hash_value - set hash value in requested has node
  16. * @fit: pointer to the FIT format image header
  17. * @noffset: hash node offset
  18. * @value: hash value to be set
  19. * @value_len: hash value length
  20. *
  21. * fit_set_hash_value() attempts to set hash value in a node at offset
  22. * given and returns operation status to the caller.
  23. *
  24. * returns
  25. * 0, on success
  26. * -1, on failure
  27. */
  28. static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
  29. int value_len)
  30. {
  31. int ret;
  32. ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
  33. if (ret) {
  34. printf("Can't set hash '%s' property for '%s' node(%s)\n",
  35. FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
  36. fdt_strerror(ret));
  37. return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
  38. }
  39. return 0;
  40. }
  41. /**
  42. * fit_image_process_hash - Process a single subnode of the images/ node
  43. *
  44. * Check each subnode and process accordingly. For hash nodes we generate
  45. * a hash of the supplised data and store it in the node.
  46. *
  47. * @fit: pointer to the FIT format image header
  48. * @image_name: name of image being processes (used to display errors)
  49. * @noffset: subnode offset
  50. * @data: data to process
  51. * @size: size of data in bytes
  52. * @return 0 if ok, -1 on error
  53. */
  54. static int fit_image_process_hash(void *fit, const char *image_name,
  55. int noffset, const void *data, size_t size)
  56. {
  57. uint8_t value[FIT_MAX_HASH_LEN];
  58. const char *node_name;
  59. int value_len;
  60. char *algo;
  61. int ret;
  62. node_name = fit_get_name(fit, noffset, NULL);
  63. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  64. printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
  65. node_name, image_name);
  66. return -ENOENT;
  67. }
  68. if (calculate_hash(data, size, algo, value, &value_len)) {
  69. printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
  70. algo, node_name, image_name);
  71. return -EPROTONOSUPPORT;
  72. }
  73. ret = fit_set_hash_value(fit, noffset, value, value_len);
  74. if (ret) {
  75. printf("Can't set hash value for '%s' hash node in '%s' image node\n",
  76. node_name, image_name);
  77. return ret;
  78. }
  79. return 0;
  80. }
  81. /**
  82. * fit_image_write_sig() - write the signature to a FIT
  83. *
  84. * This writes the signature and signer data to the FIT.
  85. *
  86. * @fit: pointer to the FIT format image header
  87. * @noffset: hash node offset
  88. * @value: signature value to be set
  89. * @value_len: signature value length
  90. * @comment: Text comment to write (NULL for none)
  91. *
  92. * returns
  93. * 0, on success
  94. * -FDT_ERR_..., on failure
  95. */
  96. static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
  97. int value_len, const char *comment, const char *region_prop,
  98. int region_proplen, const char *cmdname)
  99. {
  100. int string_size;
  101. int ret;
  102. /*
  103. * Get the current string size, before we update the FIT and add
  104. * more
  105. */
  106. string_size = fdt_size_dt_strings(fit);
  107. ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
  108. if (!ret) {
  109. ret = fdt_setprop_string(fit, noffset, "signer-name",
  110. "mkimage");
  111. }
  112. if (!ret) {
  113. ret = fdt_setprop_string(fit, noffset, "signer-version",
  114. PLAIN_VERSION);
  115. }
  116. if (comment && !ret)
  117. ret = fdt_setprop_string(fit, noffset, "comment", comment);
  118. if (!ret) {
  119. time_t timestamp = imagetool_get_source_date(cmdname,
  120. time(NULL));
  121. ret = fit_set_timestamp(fit, noffset, timestamp);
  122. }
  123. if (region_prop && !ret) {
  124. uint32_t strdata[2];
  125. ret = fdt_setprop(fit, noffset, "hashed-nodes",
  126. region_prop, region_proplen);
  127. /* This is a legacy offset, it is unused, and must remain 0. */
  128. strdata[0] = 0;
  129. strdata[1] = cpu_to_fdt32(string_size);
  130. if (!ret) {
  131. ret = fdt_setprop(fit, noffset, "hashed-strings",
  132. strdata, sizeof(strdata));
  133. }
  134. }
  135. return ret;
  136. }
  137. static int fit_image_setup_sig(struct image_sign_info *info,
  138. const char *keydir, void *fit, const char *image_name,
  139. int noffset, const char *require_keys, const char *engine_id)
  140. {
  141. const char *node_name;
  142. char *algo_name;
  143. node_name = fit_get_name(fit, noffset, NULL);
  144. if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
  145. printf("Can't get algo property for '%s' signature node in '%s' image node\n",
  146. node_name, image_name);
  147. return -1;
  148. }
  149. memset(info, '\0', sizeof(*info));
  150. info->keydir = keydir;
  151. info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
  152. info->fit = fit;
  153. info->node_offset = noffset;
  154. info->name = strdup(algo_name);
  155. info->checksum = image_get_checksum_algo(algo_name);
  156. info->crypto = image_get_crypto_algo(algo_name);
  157. info->require_keys = require_keys;
  158. info->engine_id = engine_id;
  159. if (!info->checksum || !info->crypto) {
  160. printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
  161. algo_name, node_name, image_name);
  162. return -1;
  163. }
  164. return 0;
  165. }
  166. /**
  167. * fit_image_process_sig- Process a single subnode of the images/ node
  168. *
  169. * Check each subnode and process accordingly. For signature nodes we
  170. * generate a signed hash of the supplised data and store it in the node.
  171. *
  172. * @keydir: Directory containing keys to use for signing
  173. * @keydest: Destination FDT blob to write public keys into
  174. * @fit: pointer to the FIT format image header
  175. * @image_name: name of image being processes (used to display errors)
  176. * @noffset: subnode offset
  177. * @data: data to process
  178. * @size: size of data in bytes
  179. * @comment: Comment to add to signature nodes
  180. * @require_keys: Mark all keys as 'required'
  181. * @engine_id: Engine to use for signing
  182. * @return 0 if ok, -1 on error
  183. */
  184. static int fit_image_process_sig(const char *keydir, void *keydest,
  185. void *fit, const char *image_name,
  186. int noffset, const void *data, size_t size,
  187. const char *comment, int require_keys, const char *engine_id,
  188. const char *cmdname)
  189. {
  190. struct image_sign_info info;
  191. struct image_region region;
  192. const char *node_name;
  193. uint8_t *value;
  194. uint value_len;
  195. int ret;
  196. if (fit_image_setup_sig(&info, keydir, fit, image_name, noffset,
  197. require_keys ? "image" : NULL, engine_id))
  198. return -1;
  199. node_name = fit_get_name(fit, noffset, NULL);
  200. region.data = data;
  201. region.size = size;
  202. ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
  203. if (ret) {
  204. printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
  205. node_name, image_name, ret);
  206. /* We allow keys to be missing */
  207. if (ret == -ENOENT)
  208. return 0;
  209. return -1;
  210. }
  211. ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
  212. NULL, 0, cmdname);
  213. if (ret) {
  214. if (ret == -FDT_ERR_NOSPACE)
  215. return -ENOSPC;
  216. printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
  217. node_name, image_name, fdt_strerror(ret));
  218. return -1;
  219. }
  220. free(value);
  221. /* Get keyname again, as FDT has changed and invalidated our pointer */
  222. info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
  223. /*
  224. * Write the public key into the supplied FDT file; this might fail
  225. * several times, since we try signing with successively increasing
  226. * size values
  227. */
  228. if (keydest) {
  229. ret = info.crypto->add_verify_data(&info, keydest);
  230. if (ret) {
  231. printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
  232. node_name, image_name);
  233. return ret;
  234. }
  235. }
  236. return 0;
  237. }
  238. /**
  239. * fit_image_add_verification_data() - calculate/set verig. data for image node
  240. *
  241. * This adds hash and signature values for an component image node.
  242. *
  243. * All existing hash subnodes are checked, if algorithm property is set to
  244. * one of the supported hash algorithms, hash value is computed and
  245. * corresponding hash node property is set, for example:
  246. *
  247. * Input component image node structure:
  248. *
  249. * o image-1 (at image_noffset)
  250. * | - data = [binary data]
  251. * o hash-1
  252. * |- algo = "sha1"
  253. *
  254. * Output component image node structure:
  255. *
  256. * o image-1 (at image_noffset)
  257. * | - data = [binary data]
  258. * o hash-1
  259. * |- algo = "sha1"
  260. * |- value = sha1(data)
  261. *
  262. * For signature details, please see doc/uImage.FIT/signature.txt
  263. *
  264. * @keydir Directory containing *.key and *.crt files (or NULL)
  265. * @keydest FDT Blob to write public keys into (NULL if none)
  266. * @fit: Pointer to the FIT format image header
  267. * @image_noffset: Requested component image node
  268. * @comment: Comment to add to signature nodes
  269. * @require_keys: Mark all keys as 'required'
  270. * @engine_id: Engine to use for signing
  271. * @return: 0 on success, <0 on failure
  272. */
  273. int fit_image_add_verification_data(const char *keydir, void *keydest,
  274. void *fit, int image_noffset, const char *comment,
  275. int require_keys, const char *engine_id, const char *cmdname)
  276. {
  277. const char *image_name;
  278. const void *data;
  279. size_t size;
  280. int noffset;
  281. /* Get image data and data length */
  282. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  283. printf("Can't get image data/size\n");
  284. return -1;
  285. }
  286. image_name = fit_get_name(fit, image_noffset, NULL);
  287. /* Process all hash subnodes of the component image node */
  288. for (noffset = fdt_first_subnode(fit, image_noffset);
  289. noffset >= 0;
  290. noffset = fdt_next_subnode(fit, noffset)) {
  291. const char *node_name;
  292. int ret = 0;
  293. /*
  294. * Check subnode name, must be equal to "hash" or "signature".
  295. * Multiple hash nodes require unique unit node
  296. * names, e.g. hash-1, hash-2, signature-1, etc.
  297. */
  298. node_name = fit_get_name(fit, noffset, NULL);
  299. if (!strncmp(node_name, FIT_HASH_NODENAME,
  300. strlen(FIT_HASH_NODENAME))) {
  301. ret = fit_image_process_hash(fit, image_name, noffset,
  302. data, size);
  303. } else if (IMAGE_ENABLE_SIGN && keydir &&
  304. !strncmp(node_name, FIT_SIG_NODENAME,
  305. strlen(FIT_SIG_NODENAME))) {
  306. ret = fit_image_process_sig(keydir, keydest,
  307. fit, image_name, noffset, data, size,
  308. comment, require_keys, engine_id, cmdname);
  309. }
  310. if (ret)
  311. return ret;
  312. }
  313. return 0;
  314. }
  315. struct strlist {
  316. int count;
  317. char **strings;
  318. };
  319. static void strlist_init(struct strlist *list)
  320. {
  321. memset(list, '\0', sizeof(*list));
  322. }
  323. static void strlist_free(struct strlist *list)
  324. {
  325. int i;
  326. for (i = 0; i < list->count; i++)
  327. free(list->strings[i]);
  328. free(list->strings);
  329. }
  330. static int strlist_add(struct strlist *list, const char *str)
  331. {
  332. char *dup;
  333. dup = strdup(str);
  334. list->strings = realloc(list->strings,
  335. (list->count + 1) * sizeof(char *));
  336. if (!list || !str)
  337. return -1;
  338. list->strings[list->count++] = dup;
  339. return 0;
  340. }
  341. static const char *fit_config_get_image_list(void *fit, int noffset,
  342. int *lenp, int *allow_missingp)
  343. {
  344. static const char default_list[] = FIT_KERNEL_PROP "\0"
  345. FIT_FDT_PROP;
  346. const char *prop;
  347. /* If there is an "image" property, use that */
  348. prop = fdt_getprop(fit, noffset, "sign-images", lenp);
  349. if (prop) {
  350. *allow_missingp = 0;
  351. return *lenp ? prop : NULL;
  352. }
  353. /* Default image list */
  354. *allow_missingp = 1;
  355. *lenp = sizeof(default_list);
  356. return default_list;
  357. }
  358. static int fit_config_get_hash_list(void *fit, int conf_noffset,
  359. int sig_offset, struct strlist *node_inc)
  360. {
  361. int allow_missing;
  362. const char *prop, *iname, *end;
  363. const char *conf_name, *sig_name;
  364. char name[200], path[200];
  365. int image_count;
  366. int ret, len;
  367. conf_name = fit_get_name(fit, conf_noffset, NULL);
  368. sig_name = fit_get_name(fit, sig_offset, NULL);
  369. /*
  370. * Build a list of nodes we need to hash. We always need the root
  371. * node and the configuration.
  372. */
  373. strlist_init(node_inc);
  374. snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
  375. if (strlist_add(node_inc, "/") ||
  376. strlist_add(node_inc, name))
  377. goto err_mem;
  378. /* Get a list of images that we intend to sign */
  379. prop = fit_config_get_image_list(fit, sig_offset, &len,
  380. &allow_missing);
  381. if (!prop)
  382. return 0;
  383. /* Locate the images */
  384. end = prop + len;
  385. image_count = 0;
  386. for (iname = prop; iname < end; iname += strlen(iname) + 1) {
  387. int noffset;
  388. int image_noffset;
  389. int hash_count;
  390. image_noffset = fit_conf_get_prop_node(fit, conf_noffset,
  391. iname);
  392. if (image_noffset < 0) {
  393. printf("Failed to find image '%s' in configuration '%s/%s'\n",
  394. iname, conf_name, sig_name);
  395. if (allow_missing)
  396. continue;
  397. return -ENOENT;
  398. }
  399. ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
  400. if (ret < 0)
  401. goto err_path;
  402. if (strlist_add(node_inc, path))
  403. goto err_mem;
  404. snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH,
  405. conf_name);
  406. /* Add all this image's hashes */
  407. hash_count = 0;
  408. for (noffset = fdt_first_subnode(fit, image_noffset);
  409. noffset >= 0;
  410. noffset = fdt_next_subnode(fit, noffset)) {
  411. const char *name = fit_get_name(fit, noffset, NULL);
  412. if (strncmp(name, FIT_HASH_NODENAME,
  413. strlen(FIT_HASH_NODENAME)))
  414. continue;
  415. ret = fdt_get_path(fit, noffset, path, sizeof(path));
  416. if (ret < 0)
  417. goto err_path;
  418. if (strlist_add(node_inc, path))
  419. goto err_mem;
  420. hash_count++;
  421. }
  422. if (!hash_count) {
  423. printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
  424. conf_name, sig_name, iname);
  425. return -ENOMSG;
  426. }
  427. image_count++;
  428. }
  429. if (!image_count) {
  430. printf("Failed to find any images for configuration '%s/%s'\n",
  431. conf_name, sig_name);
  432. return -ENOMSG;
  433. }
  434. return 0;
  435. err_mem:
  436. printf("Out of memory processing configuration '%s/%s'\n", conf_name,
  437. sig_name);
  438. return -ENOMEM;
  439. err_path:
  440. printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
  441. iname, conf_name, sig_name, fdt_strerror(ret));
  442. return -ENOENT;
  443. }
  444. static int fit_config_get_data(void *fit, int conf_noffset, int noffset,
  445. struct image_region **regionp, int *region_countp,
  446. char **region_propp, int *region_proplen)
  447. {
  448. char * const exc_prop[] = {"data"};
  449. struct strlist node_inc;
  450. struct image_region *region;
  451. struct fdt_region fdt_regions[100];
  452. const char *conf_name, *sig_name;
  453. char path[200];
  454. int count, i;
  455. char *region_prop;
  456. int ret, len;
  457. conf_name = fit_get_name(fit, conf_noffset, NULL);
  458. sig_name = fit_get_name(fit, noffset, NULL);
  459. debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
  460. /* Get a list of nodes we want to hash */
  461. ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc);
  462. if (ret)
  463. return ret;
  464. /* Get a list of regions to hash */
  465. count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
  466. exc_prop, ARRAY_SIZE(exc_prop),
  467. fdt_regions, ARRAY_SIZE(fdt_regions),
  468. path, sizeof(path), 1);
  469. if (count < 0) {
  470. printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
  471. sig_name, fdt_strerror(ret));
  472. return -EIO;
  473. }
  474. if (count == 0) {
  475. printf("No data to hash for configuration '%s/%s': %s\n",
  476. conf_name, sig_name, fdt_strerror(ret));
  477. return -EINVAL;
  478. }
  479. /* Build our list of data blocks */
  480. region = fit_region_make_list(fit, fdt_regions, count, NULL);
  481. if (!region) {
  482. printf("Out of memory hashing configuration '%s/%s'\n",
  483. conf_name, sig_name);
  484. return -ENOMEM;
  485. }
  486. /* Create a list of all hashed properties */
  487. debug("Hash nodes:\n");
  488. for (i = len = 0; i < node_inc.count; i++) {
  489. debug(" %s\n", node_inc.strings[i]);
  490. len += strlen(node_inc.strings[i]) + 1;
  491. }
  492. region_prop = malloc(len);
  493. if (!region_prop) {
  494. printf("Out of memory setting up regions for configuration '%s/%s'\n",
  495. conf_name, sig_name);
  496. return -ENOMEM;
  497. }
  498. for (i = len = 0; i < node_inc.count;
  499. len += strlen(node_inc.strings[i]) + 1, i++)
  500. strcpy(region_prop + len, node_inc.strings[i]);
  501. strlist_free(&node_inc);
  502. *region_countp = count;
  503. *regionp = region;
  504. *region_propp = region_prop;
  505. *region_proplen = len;
  506. return 0;
  507. }
  508. static int fit_config_process_sig(const char *keydir, void *keydest,
  509. void *fit, const char *conf_name, int conf_noffset,
  510. int noffset, const char *comment, int require_keys,
  511. const char *engine_id, const char *cmdname)
  512. {
  513. struct image_sign_info info;
  514. const char *node_name;
  515. struct image_region *region;
  516. char *region_prop;
  517. int region_proplen;
  518. int region_count;
  519. uint8_t *value;
  520. uint value_len;
  521. int ret;
  522. node_name = fit_get_name(fit, noffset, NULL);
  523. if (fit_config_get_data(fit, conf_noffset, noffset, &region,
  524. &region_count, &region_prop, &region_proplen))
  525. return -1;
  526. if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset,
  527. require_keys ? "conf" : NULL, engine_id))
  528. return -1;
  529. ret = info.crypto->sign(&info, region, region_count, &value,
  530. &value_len);
  531. free(region);
  532. if (ret) {
  533. printf("Failed to sign '%s' signature node in '%s' conf node\n",
  534. node_name, conf_name);
  535. /* We allow keys to be missing */
  536. if (ret == -ENOENT)
  537. return 0;
  538. return -1;
  539. }
  540. ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
  541. region_prop, region_proplen, cmdname);
  542. if (ret) {
  543. if (ret == -FDT_ERR_NOSPACE)
  544. return -ENOSPC;
  545. printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
  546. node_name, conf_name, fdt_strerror(ret));
  547. return -1;
  548. }
  549. free(value);
  550. free(region_prop);
  551. /* Get keyname again, as FDT has changed and invalidated our pointer */
  552. info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
  553. /* Write the public key into the supplied FDT file */
  554. if (keydest) {
  555. ret = info.crypto->add_verify_data(&info, keydest);
  556. if (ret) {
  557. printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
  558. node_name, conf_name);
  559. }
  560. return ret;
  561. }
  562. return 0;
  563. }
  564. static int fit_config_add_verification_data(const char *keydir, void *keydest,
  565. void *fit, int conf_noffset, const char *comment,
  566. int require_keys, const char *engine_id, const char *cmdname)
  567. {
  568. const char *conf_name;
  569. int noffset;
  570. conf_name = fit_get_name(fit, conf_noffset, NULL);
  571. /* Process all hash subnodes of the configuration node */
  572. for (noffset = fdt_first_subnode(fit, conf_noffset);
  573. noffset >= 0;
  574. noffset = fdt_next_subnode(fit, noffset)) {
  575. const char *node_name;
  576. int ret = 0;
  577. node_name = fit_get_name(fit, noffset, NULL);
  578. if (!strncmp(node_name, FIT_SIG_NODENAME,
  579. strlen(FIT_SIG_NODENAME))) {
  580. ret = fit_config_process_sig(keydir, keydest,
  581. fit, conf_name, conf_noffset, noffset, comment,
  582. require_keys, engine_id, cmdname);
  583. }
  584. if (ret)
  585. return ret;
  586. }
  587. return 0;
  588. }
  589. int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
  590. const char *comment, int require_keys,
  591. const char *engine_id, const char *cmdname)
  592. {
  593. int images_noffset, confs_noffset;
  594. int noffset;
  595. int ret;
  596. /* Find images parent node offset */
  597. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  598. if (images_noffset < 0) {
  599. printf("Can't find images parent node '%s' (%s)\n",
  600. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  601. return images_noffset;
  602. }
  603. /* Process its subnodes, print out component images details */
  604. for (noffset = fdt_first_subnode(fit, images_noffset);
  605. noffset >= 0;
  606. noffset = fdt_next_subnode(fit, noffset)) {
  607. /*
  608. * Direct child node of the images parent node,
  609. * i.e. component image node.
  610. */
  611. ret = fit_image_add_verification_data(keydir, keydest,
  612. fit, noffset, comment, require_keys, engine_id,
  613. cmdname);
  614. if (ret)
  615. return ret;
  616. }
  617. /* If there are no keys, we can't sign configurations */
  618. if (!IMAGE_ENABLE_SIGN || !keydir)
  619. return 0;
  620. /* Find configurations parent node offset */
  621. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  622. if (confs_noffset < 0) {
  623. printf("Can't find images parent node '%s' (%s)\n",
  624. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  625. return -ENOENT;
  626. }
  627. /* Process its subnodes, print out component images details */
  628. for (noffset = fdt_first_subnode(fit, confs_noffset);
  629. noffset >= 0;
  630. noffset = fdt_next_subnode(fit, noffset)) {
  631. ret = fit_config_add_verification_data(keydir, keydest,
  632. fit, noffset, comment,
  633. require_keys,
  634. engine_id, cmdname);
  635. if (ret)
  636. return ret;
  637. }
  638. return 0;
  639. }
  640. #ifdef CONFIG_FIT_SIGNATURE
  641. int fit_check_sign(const void *fit, const void *key)
  642. {
  643. int cfg_noffset;
  644. int ret;
  645. cfg_noffset = fit_conf_get_node(fit, NULL);
  646. if (!cfg_noffset)
  647. return -1;
  648. printf("Verifying Hash Integrity ... ");
  649. ret = fit_config_verify(fit, cfg_noffset);
  650. if (ret)
  651. return ret;
  652. ret = bootm_host_load_images(fit, cfg_noffset);
  653. return ret;
  654. }
  655. #endif