tss2_setappdata.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 *appData;
  10. char const *path;
  11. } ctx;
  12. /* Parse commandline parameters */
  13. static bool on_option(char key, char *value) {
  14. switch (key) {
  15. case 'i':
  16. ctx.appData = value;
  17. break;
  18. case 'p':
  19. ctx.path = 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. {"appData", required_argument, NULL, 'i'},
  28. {"path", required_argument, NULL, 'p'},
  29. };
  30. return (*opts = tpm2_options_new ("i:p:", ARRAY_LEN(topts), topts,
  31. on_option, NULL, 0)) != NULL;
  32. }
  33. /* Execute specific tool */
  34. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  35. /* Check availability of required parameters */
  36. if (!ctx.path) {
  37. fprintf (stderr, "path is missing, use --path\n");
  38. return -1;
  39. }
  40. /* Read appData from file */
  41. TSS2_RC r;
  42. uint8_t* appData = NULL;
  43. size_t appDataSize = 0;
  44. if (ctx.appData) {
  45. r = open_read_and_close (ctx.appData, (void**)&appData,
  46. &appDataSize);
  47. if (r) {
  48. return 1;
  49. }
  50. }
  51. /* Execute FAPI command with passed arguments */
  52. r = Fapi_SetAppData (fctx, ctx.path, appData, appDataSize);
  53. if (r != TSS2_RC_SUCCESS) {
  54. LOG_PERR ("Fapi_SetAppData", r);
  55. free(appData);
  56. return 1;
  57. }
  58. free(appData);
  59. return 0;
  60. }
  61. TSS2_TOOL_REGISTER("setappdata", tss2_tool_onstart, tss2_tool_onrun, NULL)