sandbox_common.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Common features for sandbox TPM1 and TPM2 implementations
  4. *
  5. * Copyright 2021 Google LLC
  6. */
  7. #ifndef __TPM_SANDBOX_COMMON_H
  8. #define __TPM_SANDBOX_COMMON_H
  9. /*
  10. * These numbers derive from adding the sizes of command fields as shown in
  11. * the TPM commands manual.
  12. */
  13. #define TPM_HDR_LEN 10
  14. /* These are the different non-volatile spaces that we emulate */
  15. enum sandbox_nv_space {
  16. NV_SEQ_ENABLE_LOCKING,
  17. NV_SEQ_GLOBAL_LOCK,
  18. NV_SEQ_FIRMWARE,
  19. NV_SEQ_KERNEL,
  20. NV_SEQ_BACKUP,
  21. NV_SEQ_FWMP,
  22. NV_SEQ_REC_HASH,
  23. NV_SEQ_COUNT,
  24. };
  25. /* TPM NVRAM location indices */
  26. #define FIRMWARE_NV_INDEX 0x1007
  27. #define KERNEL_NV_INDEX 0x1008
  28. #define BACKUP_NV_INDEX 0x1009
  29. #define FWMP_NV_INDEX 0x100a
  30. #define MRC_REC_HASH_NV_INDEX 0x100b
  31. /* Size of each non-volatile space */
  32. #define NV_DATA_SIZE 0x28
  33. /**
  34. * struct nvdata_state - state of a single non-volatile-data 'space'
  35. *
  36. * @present: true if present
  37. * @length: length in bytes (max NV_DATA_SIZE)
  38. * @data: contents of non-volatile space
  39. */
  40. struct nvdata_state {
  41. bool present;
  42. int length;
  43. u8 data[NV_DATA_SIZE];
  44. };
  45. /**
  46. * sb_tpm_index_to_seq() - convert an index into a space sequence number
  47. *
  48. * This converts the index as used by the vboot code into an internal sequence
  49. * number used by the sandbox emulation.
  50. *
  51. * @index: Index to use (FIRMWARE_NV_INDEX, etc.)
  52. * @return associated space (enum sandbox_nv_space)
  53. */
  54. int sb_tpm_index_to_seq(uint index);
  55. /**
  56. * sb_tpm_read_data() - Read non-volatile data
  57. *
  58. * This handles a TPM read of nvdata. If the nvdata is not present, a
  59. * TPM_BADINDEX error is put in the buffer. If @length is too large,
  60. * TPM_BAD_DATASIZE is put in the buffer.
  61. *
  62. * @nvdata: Current nvdata state
  63. * @seq: Sequence number to read
  64. * @recvbuf: Buffer to update with the TPM response, assumed to contain zeroes
  65. * @data_ofs: Offset of the 'data' portion of @recvbuf
  66. * @length: Number of bytes to read
  67. */
  68. void sb_tpm_read_data(const struct nvdata_state nvdata[NV_SEQ_COUNT],
  69. enum sandbox_nv_space seq, u8 *recvbuf, int data_ofs,
  70. int length);
  71. /**
  72. * sb_tpm_write_data() - Write non-volatile data
  73. *
  74. * If @length is too large, an error is logged and nothing is written.
  75. *
  76. * @nvdata: Current nvdata state
  77. * @seq: Sequence number to read
  78. * @buf: Buffer containing the data to write
  79. * @data_ofs: Offset of the 'data' portion of @buf
  80. * @length: Number of bytes to write
  81. */
  82. void sb_tpm_write_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
  83. enum sandbox_nv_space seq, const u8 *buf, int data_ofs,
  84. int length);
  85. /**
  86. * sb_tpm_define_data() - Set up non-volatile data
  87. *
  88. * If @length is too large, an error is logged and nothing is written.
  89. *
  90. * @nvdata: Current nvdata state
  91. * @seq: Sequence number to set up
  92. * @length: Length of space in bytes
  93. */
  94. void sb_tpm_define_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
  95. enum sandbox_nv_space seq, int length);
  96. #endif