tpm2_shutdown.c 1.3 KB

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