tpm-utils.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2013 The Chromium OS Authors.
  4. * Coypright (c) 2013 Guntermann & Drunck GmbH
  5. */
  6. #ifndef __TPM_UTILS_H
  7. #define __TPM_UTILS_H
  8. #define COMMAND_BUFFER_SIZE 256
  9. /* Internal error of TPM command library */
  10. #define TPM_LIB_ERROR ((u32)~0u)
  11. /* To make strings of commands more easily */
  12. #define __MSB(x) ((x) >> 8)
  13. #define __LSB(x) ((x) & 0xFF)
  14. #define tpm_u16(x) __MSB(x), __LSB(x)
  15. #define tpm_u32(x) tpm_u16((x) >> 16), tpm_u16((x) & 0xFFFF)
  16. /**
  17. * tpm_open() - Request access to locality 0 for the caller
  18. *
  19. * After all commands have been completed the caller is supposed to
  20. * call tpm_close().
  21. *
  22. * Returns 0 on success, -ve on failure.
  23. */
  24. int tpm_open(struct udevice *dev);
  25. /**
  26. * tpm_close() - Close the current session
  27. *
  28. * Releasing the locked locality. Returns 0 on success, -ve 1 on
  29. * failure (in case lock removal did not succeed).
  30. */
  31. int tpm_close(struct udevice *dev);
  32. /**
  33. * Pack data into a byte string. The data types are specified in
  34. * the format string: 'b' means unsigned byte, 'w' unsigned word,
  35. * 'd' unsigned double word, and 's' byte string. The data are a
  36. * series of offsets and values (for type byte string there are also
  37. * lengths). The data values are packed into the byte string
  38. * sequentially, and so a latter value could over-write a former
  39. * value.
  40. *
  41. * @param str output string
  42. * @param size size of output string
  43. * @param format format string
  44. * @param ... data points
  45. * @return 0 on success, non-0 on error
  46. */
  47. int pack_byte_string(u8 *str, size_t size, const char *format, ...);
  48. /**
  49. * Unpack data from a byte string. The data types are specified in
  50. * the format string: 'b' means unsigned byte, 'w' unsigned word,
  51. * 'd' unsigned double word, and 's' byte string. The data are a
  52. * series of offsets and pointers (for type byte string there are also
  53. * lengths).
  54. *
  55. * @param str output string
  56. * @param size size of output string
  57. * @param format format string
  58. * @param ... data points
  59. * @return 0 on success, non-0 on error
  60. */
  61. int unpack_byte_string(const u8 *str, size_t size, const char *format, ...);
  62. /**
  63. * Get TPM command size.
  64. *
  65. * @param command byte string of TPM command
  66. * @return command size of the TPM command
  67. */
  68. u32 tpm_command_size(const void *command);
  69. /**
  70. * Get TPM response return code, which is one of TPM_RESULT values.
  71. *
  72. * @param response byte string of TPM response
  73. * @return return code of the TPM response
  74. */
  75. u32 tpm_return_code(const void *response);
  76. /**
  77. * Send a TPM command and return response's return code, and optionally
  78. * return response to caller.
  79. *
  80. * @param command byte string of TPM command
  81. * @param response output buffer for TPM response, or NULL if the
  82. * caller does not care about it
  83. * @param size_ptr output buffer size (input parameter) and TPM
  84. * response length (output parameter); this parameter
  85. * is a bidirectional
  86. * @return return code of the TPM response
  87. */
  88. u32 tpm_sendrecv_command(const void *command, void *response, size_t *size_ptr);
  89. #endif /* __TPM_UTILS_H */