tss2_getdescription.c 2.0 KB

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