tss2_getappdata.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 commandline parameters */
  8. static struct cxt {
  9. char const *data;
  10. char const *path;
  11. bool overwrite;
  12. } ctx;
  13. /* Parse commandline parameters */
  14. static bool on_option(char key, char *value) {
  15. switch (key) {
  16. case 'o':
  17. ctx.data = value;
  18. break;
  19. case 'f':
  20. ctx.overwrite = true;
  21. break;
  22. case 'p':
  23. ctx.path = value;
  24. break;
  25. }
  26. return true;
  27. }
  28. /* Define possible commandline parameters */
  29. static bool tss2_tool_onstart(tpm2_options **opts) {
  30. struct option topts[] = {
  31. {"path", required_argument, NULL, 'p'},
  32. {"appData", 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. /* Initialize return variables */
  46. uint8_t *appData;
  47. size_t appDataSize;
  48. /* Execute FAPI command with passed arguments */
  49. TSS2_RC r = Fapi_GetAppData (fctx, ctx.path, &appData, &appDataSize);
  50. if (r != TSS2_RC_SUCCESS) {
  51. LOG_PERR ("Fapi_GetAppData", r);
  52. return 1;
  53. }
  54. /* Write returned data to file(s) */
  55. if (appData && ctx.data) {
  56. r = open_write_and_close (ctx.data, ctx.overwrite, appData,
  57. appDataSize);
  58. if (r != TSS2_RC_SUCCESS) {
  59. return 1;
  60. }
  61. }
  62. /* Free allocated variables */
  63. Fapi_Free (appData);
  64. return 0;
  65. }
  66. TSS2_TOOL_REGISTER("getappdata", tss2_tool_onstart, tss2_tool_onrun, NULL)