tpm2_tool.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #ifndef MAIN_H
  3. #define MAIN_H
  4. #include <tss2/tss2_esys.h>
  5. #include <stdbool.h>
  6. #include "tool_rc.h"
  7. #include "tpm2_options.h"
  8. #include "tpm2_tool_output.h"
  9. /**
  10. * An optional interface for tools to specify what options they support.
  11. * They are concatenated with main's options and passed to getopt_long.
  12. * @param opts
  13. * The callee can choose to set *opts to a tpm_options pointer allocated
  14. * via tpm2_options_new(). Setting *opts to NULL is not an error, and
  15. * Indicates that no options are specified by the tool.
  16. *
  17. * @return
  18. * True on success, false on error.
  19. */
  20. typedef bool (*tpm2_tool_onstart_t)(tpm2_options **opts);
  21. /**
  22. * This is the main interface for tools, after tcti and sapi/esapi initialization
  23. * are performed.
  24. * @param ectx
  25. * The system/esapi api context.
  26. * @param flags
  27. * Flags that tools may wish to respect.
  28. * @return
  29. * A tool_rc indicating status.
  30. */
  31. typedef tool_rc (*tpm2_tool_onrun_t)(ESYS_CONTEXT *ectx, tpm2_option_flags flags);
  32. /**
  33. * Called after tpm2_tool_onrun() is invoked. ESAPI context is still valid during this call.
  34. * @param ectx
  35. * The system/esapi api context.
  36. * @return
  37. * A tool_rc indicating status.
  38. */
  39. typedef tool_rc (*tpm2_tool_onstop_t)(ESYS_CONTEXT *ectx);
  40. /**
  41. * Called when the tool is exiting, useful for cleanup.
  42. */
  43. typedef void (*tpm2_tool_onexit_t)(void);
  44. typedef struct {
  45. const char * name;
  46. tpm2_tool_onstart_t onstart;
  47. tpm2_tool_onrun_t onrun;
  48. tpm2_tool_onstop_t onstop;
  49. tpm2_tool_onexit_t onexit;
  50. } tpm2_tool;
  51. void tpm2_tool_register(const tpm2_tool * tool);
  52. #define TPM2_TOOL_REGISTER(tool_name,tool_onstart,tool_onrun,tool_onstop,tool_onexit) \
  53. static const tpm2_tool tool = { \
  54. .name = tool_name, \
  55. .onstart = tool_onstart, \
  56. .onrun = tool_onrun, \
  57. .onstop = tool_onstop, \
  58. .onexit = tool_onexit, \
  59. }; \
  60. static void \
  61. __attribute__((__constructor__)) \
  62. __attribute__((__used__)) \
  63. _tpm2_tool_init(void) \
  64. { \
  65. tpm2_tool_register(&tool); \
  66. }
  67. #endif /* MAIN_H */