tss2_nvsetbits.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include "tools/fapi/tss2_template.h"
  5. /* Context struct used to store passed command line parameters */
  6. static struct cxt {
  7. char const *path;
  8. uint64_t bitmap;
  9. } ctx;
  10. /* Parse command line parameters */
  11. static bool on_option(char key, char *value) {
  12. switch (key) {
  13. case 'i': {
  14. uint64_t i;
  15. if (!tpm2_util_string_to_uint64 (value, &i) || i == 0) {
  16. fprintf (stderr, "%s cannot be converted to a positive integer or "\
  17. "is larger than 2**64 - 1\n", value);
  18. return false;
  19. }
  20. ctx.bitmap = i; /* cast from uint32 to size_t */
  21. }
  22. break;
  23. case 'p':
  24. ctx.path = value;
  25. break;
  26. }
  27. return true;
  28. }
  29. /* Define possible command line parameters */
  30. static bool tss2_tool_onstart(tpm2_options **opts) {
  31. struct option topts[] = {
  32. {"bitmap", required_argument, NULL, 'i'},
  33. {"nvPath" , required_argument, NULL, 'p'}
  34. };
  35. return (*opts = tpm2_options_new ("i:p:", 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, "No path to the NV provided, use --nvPath\n");
  43. return -1;
  44. }
  45. if (!ctx.bitmap) {
  46. fprintf (stderr, "No bits provided, use --bitmap [0x...]\n");
  47. return -1;
  48. }
  49. /* Execute FAPI command with passed arguments */
  50. TSS2_RC r = Fapi_NvSetBits(fctx, ctx.path, ctx.bitmap);
  51. if (r != TSS2_RC_SUCCESS){
  52. LOG_PERR ("Fapi_NvSetBits", r);
  53. return 1;
  54. }
  55. return 0;
  56. }
  57. TSS2_TOOL_REGISTER("nvsetbits", tss2_tool_onstart, tss2_tool_onrun, NULL)