tss2_nvread.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.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 *nvPath;
  10. char const *data;
  11. char const *logData;
  12. bool overwrite;
  13. } ctx;
  14. /* Parse command line parameters */
  15. static bool on_option(char key, char *value) {
  16. switch (key) {
  17. case 'f':
  18. ctx.overwrite = true;
  19. break;
  20. case 'o':
  21. ctx.data = value;
  22. break;
  23. case 'p':
  24. ctx.nvPath = value;
  25. break;
  26. case 'l':
  27. ctx.logData = value;
  28. break;
  29. }
  30. return true;
  31. }
  32. /* Define possible command line parameters */
  33. static bool tss2_tool_onstart(tpm2_options **opts) {
  34. struct option topts[] = {
  35. {"nvPath" , required_argument, NULL, 'p'},
  36. {"force" , no_argument , NULL, 'f'},
  37. {"data", required_argument, NULL, 'o'},
  38. {"logData", required_argument, NULL, 'l'}
  39. };
  40. return (*opts = tpm2_options_new ("fo:p:l:", ARRAY_LEN(topts), topts,
  41. on_option, NULL, 0)) != NULL;
  42. }
  43. /* Execute specific tool */
  44. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  45. /* Check availability of required parameters */
  46. if (!ctx.nvPath) {
  47. fprintf (stderr, "No NV path provided, use --nvPath\n");
  48. return -1;
  49. }
  50. if (!ctx.data) {
  51. fprintf (stderr, "No file for output provided, use --data\n");
  52. return -1;
  53. }
  54. /* Check exclusive access to stdout */
  55. int count_out = 0;
  56. if (ctx.data && !strcmp (ctx.data, "-")) count_out +=1;
  57. if (ctx.logData && !strcmp (ctx.logData, "-")) count_out +=1;
  58. if (count_out > 1) {
  59. fprintf (stderr, "Only one of --data and --logData can print to - "\
  60. "(standard output)\n");
  61. return -1;
  62. }
  63. /* Execute FAPI command with passed arguments */
  64. uint8_t *data;
  65. size_t data_len;
  66. char *logData = NULL;
  67. TSS2_RC r = Fapi_NvRead(fctx, ctx.nvPath, &data, &data_len, &logData);
  68. if (r != TSS2_RC_SUCCESS){
  69. LOG_PERR ("Fapi_NvRead", r);
  70. return 1;
  71. }
  72. /* Write returned data to file(s) */
  73. r = open_write_and_close (ctx.data, ctx.overwrite, data, data_len);
  74. if (r) {
  75. Fapi_Free (data);
  76. return 1;
  77. }
  78. if (ctx.logData && logData) {
  79. r = open_write_and_close (ctx.logData, ctx.overwrite, logData,
  80. strlen(logData));
  81. if (r) {
  82. Fapi_Free (data);
  83. Fapi_Free (logData);
  84. return 1;
  85. }
  86. }
  87. Fapi_Free (data);
  88. Fapi_Free (logData);
  89. return 0;
  90. }
  91. TSS2_TOOL_REGISTER("nvread", tss2_tool_onstart, tss2_tool_onrun, NULL)