tpm2_options.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #ifndef OPTIONS_H
  3. #define OPTIONS_H
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <getopt.h>
  8. #include <tss2/tss2_sys.h>
  9. #define TPM2TOOLS_ENV_TCTI "TPM2TOOLS_TCTI"
  10. #define TPM2TOOLS_ENV_ENABLE_ERRATA "TPM2TOOLS_ENABLE_ERRATA"
  11. typedef union tpm2_option_flags tpm2_option_flags;
  12. union tpm2_option_flags {
  13. struct {
  14. uint8_t verbose :1;
  15. uint8_t quiet :1;
  16. uint8_t enable_errata :1;
  17. };
  18. uint8_t all;
  19. };
  20. /**
  21. * Tools may implement this optional interface if they need
  22. * to handle options.
  23. * @param key
  24. * The key of the option, ie short option return value from getopt_long().
  25. * @param value
  26. * The getopt_long optarg value.
  27. * @return
  28. * true on success, false on error.
  29. * @note
  30. * LOG_INFO and TOOL_OUTPUT will not work correctly during this callback.
  31. * This is called after onstart() finishes, but before
  32. * onrun() is invoked.
  33. *
  34. */
  35. typedef bool (*tpm2_option_handler)(char key, char *value);
  36. /**
  37. * Called after option handling to process arguments, if specified.
  38. * @param argc
  39. * The number of args in argv.
  40. * @param argv
  41. * The arguments.
  42. * @return
  43. * true on success, false otherwise.
  44. * @note
  45. * LOG_INFO adn TOOL_OUTPUT will not work correctly during this callback.
  46. * This is called after onstart() and tpm2_option_handler() (if specified),
  47. * but before onrun() is invoked.
  48. *
  49. */
  50. typedef bool (*tpm2_arg_handler)(int argc, char **argv);
  51. /**
  52. * TPM2_OPTIONS_* flags change default behavior of the argument parser
  53. *
  54. * TPM2_OPTIONS_NO_SAPI:
  55. * Skip SAPI initialization. Removes the "-T" common option.
  56. */
  57. #define TPM2_OPTIONS_NO_SAPI 0x1
  58. #define TPM2_OPTIONS_OPTIONAL_SAPI 0x2
  59. struct tpm2_options {
  60. struct {
  61. tpm2_option_handler on_opt;
  62. tpm2_arg_handler on_arg;
  63. } callbacks;
  64. char *short_opts;
  65. size_t len;
  66. uint32_t flags;
  67. struct option long_opts[];
  68. };
  69. typedef struct tpm2_options tpm2_options;
  70. /**
  71. * The onstart() routine expects a return of NULL or a tpm2_options structure.
  72. * This routine initializes said object.
  73. * @param short_opts
  74. * Any short options you wish to specify to getopt_long.
  75. * @param len
  76. * The length of the long_opts array.
  77. * @param long_opts
  78. * Any long options you wish to specify to getopt_long().
  79. * @param on_opt
  80. * An option handling callback, which may be null if you don't wish
  81. * to handle options.
  82. * @param on_arg
  83. * An argument handling callback, which may be null if you don't wish
  84. * to handle arguments.
  85. * @param flags
  86. * TPM2_OPTIONS_* bit flags
  87. * @return
  88. * NULL on failure or an initialized tpm2_options object.
  89. */
  90. tpm2_options *tpm2_options_new(const char *short_opts, size_t len,
  91. const struct option *long_opts, tpm2_option_handler on_opt,
  92. tpm2_arg_handler on_arg, uint32_t flags);
  93. /**
  94. * Concatenates two tpm2_options objects, with src appended on
  95. * dest. The internal callbacks for tpm2_arg_handler and tpm2_option_handler
  96. * which were specified during tpm2_options_new() are copied from src to
  97. * dest, thus overwriting dest. Short and long options are concatenated.
  98. * @param dest
  99. * The tpm2_options object to append to.
  100. * @param src
  101. * The source tpm2_options to append onto dest.
  102. * @return
  103. * true on success, false otherwise.
  104. */
  105. bool tpm2_options_cat(tpm2_options **dest, tpm2_options *src);
  106. /**
  107. * Free's a tpm2_options created via tpm2_options_new().
  108. * @param opts
  109. * The tpm2_options object to deallocate.
  110. */
  111. void tpm2_options_free(tpm2_options *opts);
  112. typedef enum tpm2_option_code tpm2_option_code;
  113. enum tpm2_option_code {
  114. tpm2_option_code_continue,
  115. tpm2_option_code_stop,
  116. tpm2_option_code_err
  117. };
  118. /**
  119. * Parses the tpm2_tool command line.
  120. *
  121. * @param argc
  122. * The argc from main.
  123. * @param argv
  124. * The argv from main.
  125. * @param tool_opts
  126. * The tool options gathered during onstart() lifecycle call.
  127. * @param flags
  128. * The tpm2_option_flags to set during parsing.
  129. * @param tcti
  130. * The tcti initialized from the tcti options.
  131. * @return
  132. * A tpm option code indicating if an error, further processing
  133. * or an immediate exit is desired.
  134. * @note
  135. * Used by tpm2_tool, and likely should only be used there.
  136. *
  137. */
  138. tpm2_option_code tpm2_handle_options(int argc, char **argv,
  139. tpm2_options *tool_opts, tpm2_option_flags *flags,
  140. TSS2_TCTI_CONTEXT **tcti);
  141. /**
  142. * Print usage summary for a given tpm2 tool.
  143. *
  144. * @param command
  145. * The command to print its usage summary text.
  146. * @param tool_opts
  147. * The tpm2_options array that contains the tool options to print as a summary.
  148. */
  149. void tpm2_print_usage(const char *command, struct tpm2_options *tool_opts);
  150. #endif /* OPTIONS_H */