tpm-utils.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. * Pack data into a byte string. The data types are specified in
  18. * the format string: 'b' means unsigned byte, 'w' unsigned word,
  19. * 'd' unsigned double word, and 's' byte string. The data are a
  20. * series of offsets and values (for type byte string there are also
  21. * lengths). The data values are packed into the byte string
  22. * sequentially, and so a latter value could over-write a former
  23. * value.
  24. *
  25. * @param str output string
  26. * @param size size of output string
  27. * @param format format string
  28. * @param ... data points
  29. * @return 0 on success, non-0 on error
  30. */
  31. int pack_byte_string(u8 *str, size_t size, const char *format, ...);
  32. /**
  33. * Unpack data from 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 pointers (for type byte string there are also
  37. * lengths).
  38. *
  39. * @param str output string
  40. * @param size size of output string
  41. * @param format format string
  42. * @param ... data points
  43. * @return 0 on success, non-0 on error
  44. */
  45. int unpack_byte_string(const u8 *str, size_t size, const char *format, ...);
  46. /**
  47. * Get TPM command size.
  48. *
  49. * @param command byte string of TPM command
  50. * @return command size of the TPM command
  51. */
  52. u32 tpm_command_size(const void *command);
  53. /**
  54. * Get TPM response return code, which is one of TPM_RESULT values.
  55. *
  56. * @param response byte string of TPM response
  57. * @return return code of the TPM response
  58. */
  59. u32 tpm_return_code(const void *response);
  60. /**
  61. * Send a TPM command and return response's return code, and optionally
  62. * return response to caller.
  63. *
  64. * @param command byte string of TPM command
  65. * @param response output buffer for TPM response, or NULL if the
  66. * caller does not care about it
  67. * @param size_ptr output buffer size (input parameter) and TPM
  68. * response length (output parameter); this parameter
  69. * is a bidirectional
  70. * @return return code of the TPM response
  71. */
  72. u32 tpm_sendrecv_command(struct udevice *dev, const void *command,
  73. void *response, size_t *size_ptr);
  74. #endif /* __TPM_UTILS_H */