tpm2_pcrreset.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <string.h>
  3. #include "log.h"
  4. #include "pcr.h"
  5. #include "tpm2.h"
  6. #include "tpm2_tool.h"
  7. #include "tpm2_options.h"
  8. typedef struct tpm_pcr_reset_ctx tpm_pcr_reset_ctx;
  9. struct tpm_pcr_reset_ctx {
  10. bool pcr_list[TPM2_MAX_PCRS];
  11. };
  12. static tpm_pcr_reset_ctx ctx;
  13. static tool_rc pcr_reset_one(ESYS_CONTEXT *ectx, TPMI_DH_PCR pcr_index) {
  14. tool_rc rc = tpm2_pcr_reset(ectx, pcr_index);
  15. if (rc != tool_rc_success) {
  16. LOG_ERR("Could not reset PCR index: %d", pcr_index);
  17. }
  18. return rc;
  19. }
  20. static tool_rc pcr_reset(ESYS_CONTEXT *ectx) {
  21. size_t i;
  22. for (i = 0; i < TPM2_MAX_PCRS; i++) {
  23. if (!ctx.pcr_list[i])
  24. continue;
  25. tool_rc rc = pcr_reset_one(ectx, i);
  26. if (rc != tool_rc_success) {
  27. return rc;
  28. }
  29. }
  30. return tool_rc_success;
  31. }
  32. static bool on_arg(int argc, char** argv) {
  33. int i;
  34. uint32_t pcr;
  35. memset(ctx.pcr_list, 0, TPM2_MAX_PCRS);
  36. if (argc < 1) {
  37. LOG_ERR("Expected at least one PCR index"
  38. "ie: <pcr index>, got: 0");
  39. return false;
  40. }
  41. for (i = 0; i < argc; i++) {
  42. if (!pcr_get_id(argv[i], &pcr))
  43. return false;
  44. ctx.pcr_list[pcr] = 1;
  45. }
  46. return true;
  47. }
  48. static bool tpm2_tool_onstart(tpm2_options **opts) {
  49. *opts = tpm2_options_new(NULL, 0, NULL, NULL, on_arg, 0);
  50. return *opts != NULL;
  51. }
  52. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  53. UNUSED(flags);
  54. return pcr_reset(ectx);
  55. }
  56. // Register this tool with tpm2_tool.c
  57. TPM2_TOOL_REGISTER("pcrreset", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)