tss2_import.c 1.7 KB

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