tss2_nvextend.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. } ctx;
  13. /* Parse command line parameters */
  14. static bool on_option(char key, char *value) {
  15. switch (key) {
  16. case 'i':
  17. ctx.data = value;
  18. break;
  19. case 'p':
  20. ctx.nvPath = value;
  21. break;
  22. case 'l':
  23. ctx.logData = value;
  24. break;
  25. }
  26. return true;
  27. }
  28. /* Define possible command line parameters */
  29. static bool tss2_tool_onstart(tpm2_options **opts) {
  30. struct option topts[] = {
  31. {"data" , required_argument, NULL, 'i'},
  32. {"nvPath" , required_argument, NULL, 'p'},
  33. {"logData" , required_argument, NULL, 'l'}
  34. };
  35. return (*opts = tpm2_options_new ("i:p:l:", 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.nvPath) {
  42. fprintf (stderr, "No NV path provided, use --nvPath\n");
  43. return -1;
  44. }
  45. if (!ctx.data) {
  46. fprintf (stderr, "No file for input provided, use --data\n");
  47. return -1;
  48. }
  49. /* Check exclusive access to stdin */
  50. int count_in = 0;
  51. if (ctx.data && !strcmp (ctx.data, "-")) count_in +=1;
  52. if (ctx.logData && !strcmp (ctx.logData, "-")) count_in +=1;
  53. if (count_in > 1) {
  54. fprintf (stderr, "Only one of --data and --logData can read from - "\
  55. "(standard input)\n");
  56. return -1;
  57. }
  58. /* Read data to extend from file */
  59. uint8_t *data;
  60. size_t data_len;
  61. TSS2_RC r = open_read_and_close (ctx.data, (void**)&data, &data_len);
  62. if (r){
  63. return 1;
  64. }
  65. char *logData = NULL;
  66. if (ctx.logData){
  67. TSS2_RC r = open_read_and_close (ctx.logData, (void**)&logData, 0);
  68. if (r){
  69. free (data);
  70. return 1;
  71. }
  72. }
  73. /* Execute FAPI command with passed arguments */
  74. r = Fapi_NvExtend(fctx, ctx.nvPath, data, data_len, logData);
  75. if (r != TSS2_RC_SUCCESS){
  76. free (data);
  77. free (logData);
  78. LOG_PERR("Fapi_NvExtend", r);
  79. return 1;
  80. }
  81. free (data);
  82. free (logData);
  83. return 0;
  84. }
  85. TSS2_TOOL_REGISTER("nvextend", tss2_tool_onstart, tss2_tool_onrun, NULL)