tpm2_rc_decode.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <inttypes.h>
  3. #include <tss2/tss2_rc.h>
  4. #include "log.h"
  5. #include "tpm2_tool.h"
  6. #define TPM2_RC_MAX 0xffffffff
  7. typedef struct tpm2_rc_ctx tpm2_rc_ctx;
  8. struct tpm2_rc_ctx {
  9. TSS2_RC rc;
  10. };
  11. static tpm2_rc_ctx ctx;
  12. static bool str_to_tpm_rc(const char *rc_str, TSS2_RC *rc) {
  13. uintmax_t rc_read = 0;
  14. char *end_ptr = NULL;
  15. rc_read = strtoumax(rc_str, &end_ptr, 0);
  16. if (rc_read > TPM2_RC_MAX) {
  17. LOG_ERR("invalid TSS2_RC");
  18. return false;
  19. }
  20. /* apply the TPM2_RC_MAX mask to the possibly larger uintmax_t */
  21. *rc = rc_read & TPM2_RC_MAX;
  22. return true;
  23. }
  24. static bool on_arg(int argc, char **argv) {
  25. if (argc != 1) {
  26. LOG_ERR("Expected 1 rc code, got: %d", argc);
  27. }
  28. return str_to_tpm_rc(argv[0], &ctx.rc);
  29. }
  30. static bool tpm2_tool_onstart(tpm2_options **opts) {
  31. *opts = tpm2_options_new(NULL, 0, NULL, NULL, on_arg, TPM2_OPTIONS_NO_SAPI);
  32. return *opts != NULL;
  33. }
  34. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  35. UNUSED(flags);
  36. UNUSED(ectx);
  37. const char *e = Tss2_RC_Decode(ctx.rc);
  38. tpm2_tool_output("%s\n", e);
  39. return tool_rc_success;
  40. }
  41. // Register this tool with tpm2_tool.c
  42. TPM2_TOOL_REGISTER("rc_decode", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)