tpm2_incrementalselftest.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "log.h"
  4. #include "tpm2.h"
  5. #include "tpm2_alg_util.h"
  6. #include "tpm2_tool.h"
  7. typedef struct tpm_incrementalselftest_ctx tpm_incrementalselftest_ctx;
  8. struct tpm_incrementalselftest_ctx {
  9. TPML_ALG inputalgs;
  10. };
  11. static tpm_incrementalselftest_ctx ctx;
  12. static tool_rc do_tpm_incrementalselftest(ESYS_CONTEXT *ectx) {
  13. TPML_ALG *to_do_list = NULL;
  14. tool_rc rc = tpm2_incrementalselftest(ectx, &(ctx.inputalgs), &to_do_list);
  15. if (rc != tool_rc_success) {
  16. return rc;
  17. }
  18. tpm2_tool_output("status: ");
  19. print_yaml_indent(1);
  20. if (to_do_list->count == 0) {
  21. tpm2_tool_output("complete\n");
  22. } else {
  23. tpm2_tool_output("success\n");
  24. tpm2_tool_output("remaining:\n");
  25. uint32_t i;
  26. for (i = 0; i < to_do_list->count; i++) {
  27. print_yaml_indent(1);
  28. tpm2_tool_output("%s",
  29. tpm2_alg_util_algtostr(to_do_list->algorithms[i],
  30. tpm2_alg_util_flags_any));
  31. tpm2_tool_output("\n");
  32. }
  33. }
  34. free(to_do_list);
  35. return tool_rc_success;
  36. }
  37. static bool on_arg(int argc, char **argv) {
  38. int i;
  39. TPM2_ALG_ID algorithm;
  40. LOG_INFO("tocheck :");
  41. for (i = 0; i < argc; i++) {
  42. algorithm = tpm2_alg_util_from_optarg(argv[i], tpm2_alg_util_flags_any);
  43. if (algorithm == TPM2_ALG_ERROR) {
  44. LOG_INFO("\n");
  45. LOG_ERR("Got invalid or unsupported algorithm: \"%s\"", argv[i]);
  46. return false;
  47. }
  48. ctx.inputalgs.algorithms[i] = algorithm;
  49. ctx.inputalgs.count += 1;
  50. LOG_INFO(" - %s", argv[i]);
  51. }
  52. LOG_INFO("\n");
  53. return true;
  54. }
  55. static bool tpm2_tool_onstart(tpm2_options **opts) {
  56. *opts = tpm2_options_new(NULL, 0, NULL, NULL, on_arg, 0);
  57. return *opts != NULL;
  58. }
  59. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  60. UNUSED(flags);
  61. return do_tpm_incrementalselftest(ectx);
  62. }
  63. // Register this tool with tpm2_tool.c
  64. TPM2_TOOL_REGISTER("incrementalselftest", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)