1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Common features for sandbox TPM1 and TPM2 implementations
5 * Copyright 2021 Google LLC
8 #ifndef __TPM_SANDBOX_COMMON_H
9 #define __TPM_SANDBOX_COMMON_H
12 * These numbers derive from adding the sizes of command fields as shown in
13 * the TPM commands manual.
15 #define TPM_HDR_LEN 10
17 /* These are the different non-volatile spaces that we emulate */
18 enum sandbox_nv_space {
19 NV_SEQ_ENABLE_LOCKING,
30 /* TPM NVRAM location indices */
31 #define FIRMWARE_NV_INDEX 0x1007
32 #define KERNEL_NV_INDEX 0x1008
33 #define BACKUP_NV_INDEX 0x1009
34 #define FWMP_NV_INDEX 0x100a
35 #define MRC_REC_HASH_NV_INDEX 0x100b
37 /* Size of each non-volatile space */
38 #define NV_DATA_SIZE 0x28
41 * struct nvdata_state - state of a single non-volatile-data 'space'
43 * @present: true if present
44 * @length: length in bytes (max NV_DATA_SIZE)
45 * @data: contents of non-volatile space
50 u8 data[NV_DATA_SIZE];
54 * sb_tpm_index_to_seq() - convert an index into a space sequence number
56 * This converts the index as used by the vboot code into an internal sequence
57 * number used by the sandbox emulation.
59 * @index: Index to use (FIRMWARE_NV_INDEX, etc.)
60 * @return associated space (enum sandbox_nv_space)
62 int sb_tpm_index_to_seq(uint index);
65 * sb_tpm_read_data() - Read non-volatile data
67 * This handles a TPM read of nvdata. If the nvdata is not present, a
68 * TPM_BADINDEX error is put in the buffer. If @length is too large,
69 * TPM_BAD_DATASIZE is put in the buffer.
71 * @nvdata: Current nvdata state
72 * @seq: Sequence number to read
73 * @recvbuf: Buffer to update with the TPM response, assumed to contain zeroes
74 * @data_ofs: Offset of the 'data' portion of @recvbuf
75 * @length: Number of bytes to read
77 void sb_tpm_read_data(const struct nvdata_state nvdata[NV_SEQ_COUNT],
78 enum sandbox_nv_space seq, u8 *recvbuf, int data_ofs,
82 * sb_tpm_write_data() - Write non-volatile data
84 * If @length is too large, an error is logged and nothing is written.
86 * @nvdata: Current nvdata state
87 * @seq: Sequence number to read
88 * @buf: Buffer containing the data to write
89 * @data_ofs: Offset of the 'data' portion of @buf
90 * @length: Number of bytes to write
92 void sb_tpm_write_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
93 enum sandbox_nv_space seq, const u8 *buf, int data_ofs,
97 * sb_tpm_define_data() - Set up non-volatile data
99 * If @length is too large, an error is logged and nothing is written.
101 * @nvdata: Current nvdata state
102 * @seq: Sequence number to set up
103 * @length: Length of space in bytes
105 void sb_tpm_define_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
106 enum sandbox_nv_space seq, int length);