tpm2_startup.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include "log.h"
  3. #include "tpm2.h"
  4. #include "tpm2_tool.h"
  5. /*
  6. * Both the Microsoft and IBM TPM2 simulators require some specific setup
  7. * before they can be used by the SAPI. This setup is specific to the
  8. * simulators and is something that the low-level hardware / firmware does
  9. * for a discrete TPM.
  10. * NOTE: In the code that interacts with a TPM this can be a very ugly
  11. * abstraction leak.
  12. */
  13. typedef struct tpm2_startup_ctx tpm2_startup_ctx;
  14. struct tpm2_startup_ctx {
  15. UINT8 clear :1;
  16. };
  17. static tpm2_startup_ctx ctx;
  18. static bool on_option(char key, char *value) {
  19. UNUSED(value);
  20. switch (key) {
  21. case 'c':
  22. ctx.clear = 1;
  23. break;
  24. /*no default */
  25. }
  26. return true;
  27. }
  28. static bool tpm2_tool_onstart(tpm2_options **opts) {
  29. static struct option topts [] = {
  30. { "clear", no_argument, NULL, 'c' },
  31. };
  32. *opts = tpm2_options_new("c", ARRAY_LEN(topts), topts, on_option, NULL, 0);
  33. return *opts != NULL;
  34. }
  35. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *context, tpm2_option_flags flags) {
  36. UNUSED(flags);
  37. TPM2_SU startup_type = ctx.clear ? TPM2_SU_CLEAR : TPM2_SU_STATE;
  38. LOG_INFO("Sending TPM_Startup command with type: %s",
  39. ctx.clear ? "TPM2_SU_CLEAR" : "TPM2_SU_STATE");
  40. return tpm2_startup(context, startup_type);
  41. }
  42. // Register this tool with tpm2_tool.c
  43. TPM2_TOOL_REGISTER("startup", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)