tss2_getinfo.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "tools/fapi/tss2_template.h"
  7. /* Context struct used to store passed commandline parameters */
  8. static struct cxt {
  9. char *info;
  10. bool overwrite;
  11. } ctx;
  12. /* Parse commandline parameters */
  13. static bool on_option(char key, char *value) {
  14. switch (key) {
  15. case 'f':
  16. ctx.overwrite = true;
  17. break;
  18. case 'o':
  19. ctx.info = value;
  20. break;
  21. }
  22. return true;
  23. }
  24. /* Define possible commandline parameters */
  25. static bool tss2_tool_onstart(tpm2_options **opts) {
  26. struct option topts[] = {
  27. {"force" , no_argument , NULL, 'f'},
  28. /* output file */
  29. {"info" , required_argument, NULL, 'o'}
  30. };
  31. return (*opts = tpm2_options_new ("fo:", ARRAY_LEN(topts), topts,
  32. on_option, NULL, 0)) != NULL;
  33. }
  34. /* Execute specific tool */
  35. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  36. /* Check availability of required parameters */
  37. if (!ctx.info) {
  38. fprintf (stderr, "info parameter is missing, pass --info\n");
  39. return -1;
  40. }
  41. /* Execute FAPI command with passed arguments */
  42. char *info;
  43. TSS2_RC r = Fapi_GetInfo (fctx, &info);
  44. if (r != TSS2_RC_SUCCESS) {
  45. LOG_PERR ("Fapi_GetInfo", r);
  46. return 1;
  47. }
  48. /* Write returned data to file(s) */
  49. r = open_write_and_close (ctx.info, ctx.overwrite, info, strlen(info));
  50. if (r) {
  51. Fapi_Free (info);
  52. return 1;
  53. }
  54. Fapi_Free (info);
  55. return 0;
  56. }
  57. TSS2_TOOL_REGISTER("getinfo", tss2_tool_onstart, tss2_tool_onrun, NULL)