tpm2_gettestresult.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "log.h"
  4. #include "tpm2.h"
  5. #include "tpm2_tool.h"
  6. static bool tpm2_tool_onstart(tpm2_options **opts) {
  7. *opts = tpm2_options_new(NULL, 0, NULL, NULL, NULL, 0);
  8. return *opts != NULL;
  9. }
  10. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  11. UNUSED(flags);
  12. tool_rc rc = tool_rc_general_error;
  13. TPM2_RC status;
  14. TPM2B_MAX_BUFFER *output = NULL;
  15. /*
  16. * If TPM2_SelfTest() has not been executed and a testable function has not been tested, testResult will be
  17. * TPM_RC_NEEDS_TEST. If TPM2_SelfTest() has been received and the tests are not complete,
  18. * testResult will be TPM_RC_TESTING. If testing of all functions is complete without functional failures,
  19. * testResult will be TPM_RC_SUCCESS. If any test failed, testResult will be TPM_RC_FAILURE.
  20. */
  21. tool_rc tmp_rc = tpm2_gettestresult(ectx, &output, &status);
  22. if (tmp_rc != tool_rc_success) {
  23. return tmp_rc;
  24. }
  25. tpm2_tool_output("status: ");
  26. print_yaml_indent(1);
  27. status &= TPM2_RC_TESTING;
  28. switch (status) {
  29. case TPM2_RC_SUCCESS:
  30. tpm2_tool_output("success");
  31. break;
  32. case TPM2_RC_TESTING:
  33. tpm2_tool_output("testing");
  34. break;
  35. case TPM2_RC_NEEDS_TEST:
  36. tpm2_tool_output("needs-test");
  37. break;
  38. default:
  39. LOG_ERR("Unknown testing result, got: 0x%x", status);
  40. goto out;
  41. }
  42. if (output->size > 0) {
  43. tpm2_tool_output("\ndata: ");
  44. print_yaml_indent(1);
  45. tpm2_util_hexdump(output->buffer, output->size);
  46. }
  47. tpm2_tool_output("\n");
  48. rc = tool_rc_success;
  49. out: free(output);
  50. return rc;
  51. }
  52. // Register this tool with tpm2_tool.c
  53. TPM2_TOOL_REGISTER("gettestresult", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)