tss2_list.c 1.7 KB

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