tss2_writeauthorizenv.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "tools/fapi/tss2_template.h"
  6. /* Context struct used to store passed command line parameters */
  7. static struct cxt {
  8. char const *nvPath;
  9. char const *policyPath;
  10. } ctx;
  11. /* Parse command line parameters */
  12. static bool on_option(char key, char *value) {
  13. switch (key) {
  14. case 'p':
  15. ctx.nvPath = value;
  16. break;
  17. case 'P':
  18. ctx.policyPath = value;
  19. break;
  20. }
  21. return true;
  22. }
  23. /* Define possible command line parameters */
  24. static bool tss2_tool_onstart(tpm2_options **opts) {
  25. struct option topts[] = {
  26. {"nvPath" , required_argument, NULL, 'p'},
  27. {"policyPath" , required_argument, NULL, 'P'}
  28. };
  29. return (*opts = tpm2_options_new ("p:P:", ARRAY_LEN(topts), topts,
  30. on_option, NULL, 0)) != NULL;
  31. }
  32. /* Execute specific tool */
  33. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  34. /* Check availability of required parameters */
  35. if (!ctx.nvPath) {
  36. fprintf (stderr, "No NV path provided, use --nvPath\n");
  37. return -1;
  38. }
  39. if (!ctx.policyPath) {
  40. fprintf (stderr, "No policy path provided, use --policyPath\n");
  41. return -1;
  42. }
  43. /* Execute FAPI command with passed arguments */
  44. TSS2_RC r = Fapi_WriteAuthorizeNv(fctx, ctx.nvPath, ctx.policyPath);
  45. if (r != TSS2_RC_SUCCESS){
  46. LOG_PERR ("Fapi_WriteAuthorizeNv", r);
  47. return 1;
  48. }
  49. return 0;
  50. }
  51. TSS2_TOOL_REGISTER("writeauthorizenv", tss2_tool_onstart, tss2_tool_onrun, NULL)