1 | /** @file
|
---|
2 | This library is only intended to be used by TPM modules.
|
---|
3 | It provides basic TPM Interface Specification (TIS) and Command functions.
|
---|
4 |
|
---|
5 | Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
|
---|
6 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
7 |
|
---|
8 | **/
|
---|
9 |
|
---|
10 | #ifndef _TPM_COMM_LIB_H_
|
---|
11 | #define _TPM_COMM_LIB_H_
|
---|
12 |
|
---|
13 | #include <IndustryStandard/Tpm12.h>
|
---|
14 |
|
---|
15 | typedef EFI_HANDLE TIS_TPM_HANDLE;
|
---|
16 |
|
---|
17 | ///
|
---|
18 | /// TPM register base address.
|
---|
19 | ///
|
---|
20 | #define TPM_BASE_ADDRESS 0xfed40000
|
---|
21 |
|
---|
22 | //
|
---|
23 | // Set structure alignment to 1-byte
|
---|
24 | //
|
---|
25 | #pragma pack (1)
|
---|
26 |
|
---|
27 | //
|
---|
28 | // Register set map as specified in TIS specification Chapter 10
|
---|
29 | //
|
---|
30 | typedef struct {
|
---|
31 | ///
|
---|
32 | /// Used to gain ownership for this particular port.
|
---|
33 | ///
|
---|
34 | UINT8 Access; // 0
|
---|
35 | UINT8 Reserved1[7]; // 1
|
---|
36 | ///
|
---|
37 | /// Controls interrupts.
|
---|
38 | ///
|
---|
39 | UINT32 IntEnable; // 8
|
---|
40 | ///
|
---|
41 | /// SIRQ vector to be used by the TPM.
|
---|
42 | ///
|
---|
43 | UINT8 IntVector; // 0ch
|
---|
44 | UINT8 Reserved2[3]; // 0dh
|
---|
45 | ///
|
---|
46 | /// What caused interrupt.
|
---|
47 | ///
|
---|
48 | UINT32 IntSts; // 10h
|
---|
49 | ///
|
---|
50 | /// Shows which interrupts are supported by that particular TPM.
|
---|
51 | ///
|
---|
52 | UINT32 IntfCapability; // 14h
|
---|
53 | ///
|
---|
54 | /// Status Register. Provides status of the TPM.
|
---|
55 | ///
|
---|
56 | UINT8 Status; // 18h
|
---|
57 | ///
|
---|
58 | /// Number of consecutive writes that can be done to the TPM.
|
---|
59 | ///
|
---|
60 | UINT16 BurstCount; // 19h
|
---|
61 | UINT8 Reserved3[9];
|
---|
62 | ///
|
---|
63 | /// Read or write FIFO, depending on transaction.
|
---|
64 | ///
|
---|
65 | UINT32 DataFifo; // 24
|
---|
66 | UINT8 Reserved4[0xed8]; // 28h
|
---|
67 | ///
|
---|
68 | /// Vendor ID
|
---|
69 | ///
|
---|
70 | UINT16 Vid; // 0f00h
|
---|
71 | ///
|
---|
72 | /// Device ID
|
---|
73 | ///
|
---|
74 | UINT16 Did; // 0f02h
|
---|
75 | ///
|
---|
76 | /// Revision ID
|
---|
77 | ///
|
---|
78 | UINT8 Rid; // 0f04h
|
---|
79 | ///
|
---|
80 | /// TCG defined configuration registers.
|
---|
81 | ///
|
---|
82 | UINT8 TcgDefined[0x7b]; // 0f05h
|
---|
83 | ///
|
---|
84 | /// Alias to I/O legacy space.
|
---|
85 | ///
|
---|
86 | UINT32 LegacyAddress1; // 0f80h
|
---|
87 | ///
|
---|
88 | /// Additional 8 bits for I/O legacy space extension.
|
---|
89 | ///
|
---|
90 | UINT32 LegacyAddress1Ex; // 0f84h
|
---|
91 | ///
|
---|
92 | /// Alias to second I/O legacy space.
|
---|
93 | ///
|
---|
94 | UINT32 LegacyAddress2; // 0f88h
|
---|
95 | ///
|
---|
96 | /// Additional 8 bits for second I/O legacy space extension.
|
---|
97 | ///
|
---|
98 | UINT32 LegacyAddress2Ex; // 0f8ch
|
---|
99 | ///
|
---|
100 | /// Vendor-defined configuration registers.
|
---|
101 | ///
|
---|
102 | UINT8 VendorDefined[0x70]; // 0f90h
|
---|
103 | } TIS_PC_REGISTERS;
|
---|
104 |
|
---|
105 | //
|
---|
106 | // Restore original structure alignment
|
---|
107 | //
|
---|
108 | #pragma pack ()
|
---|
109 |
|
---|
110 | //
|
---|
111 | // Define pointer types used to access TIS registers on PC
|
---|
112 | //
|
---|
113 | typedef TIS_PC_REGISTERS *TIS_PC_REGISTERS_PTR;
|
---|
114 |
|
---|
115 | //
|
---|
116 | // TCG Platform Type based on TCG ACPI Specification Version 1.00
|
---|
117 | //
|
---|
118 | #define TCG_PLATFORM_TYPE_CLIENT 0
|
---|
119 | #define TCG_PLATFORM_TYPE_SERVER 1
|
---|
120 |
|
---|
121 | //
|
---|
122 | // Define bits of ACCESS and STATUS registers
|
---|
123 | //
|
---|
124 |
|
---|
125 | ///
|
---|
126 | /// This bit is a 1 to indicate that the other bits in this register are valid.
|
---|
127 | ///
|
---|
128 | #define TIS_PC_VALID BIT7
|
---|
129 | ///
|
---|
130 | /// Indicate that this locality is active.
|
---|
131 | ///
|
---|
132 | #define TIS_PC_ACC_ACTIVE BIT5
|
---|
133 | ///
|
---|
134 | /// Set to 1 to indicate that this locality had the TPM taken away while
|
---|
135 | /// this locality had the TIS_PC_ACC_ACTIVE bit set.
|
---|
136 | ///
|
---|
137 | #define TIS_PC_ACC_SEIZED BIT4
|
---|
138 | ///
|
---|
139 | /// Set to 1 to indicate that TPM MUST reset the
|
---|
140 | /// TIS_PC_ACC_ACTIVE bit and remove ownership for localities less than the
|
---|
141 | /// locality that is writing this bit.
|
---|
142 | ///
|
---|
143 | #define TIS_PC_ACC_SEIZE BIT3
|
---|
144 | ///
|
---|
145 | /// When this bit is 1, another locality is requesting usage of the TPM.
|
---|
146 | ///
|
---|
147 | #define TIS_PC_ACC_PENDIND BIT2
|
---|
148 | ///
|
---|
149 | /// Set to 1 to indicate that this locality is requesting to use TPM.
|
---|
150 | ///
|
---|
151 | #define TIS_PC_ACC_RQUUSE BIT1
|
---|
152 | ///
|
---|
153 | /// A value of 1 indicates that a T/OS has not been established on the platform
|
---|
154 | ///
|
---|
155 | #define TIS_PC_ACC_ESTABLISH BIT0
|
---|
156 |
|
---|
157 | ///
|
---|
158 | /// When this bit is 1, TPM is in the Ready state,
|
---|
159 | /// indicating it is ready to receive a new command.
|
---|
160 | ///
|
---|
161 | #define TIS_PC_STS_READY BIT6
|
---|
162 | ///
|
---|
163 | /// Write a 1 to this bit to cause the TPM to execute that command.
|
---|
164 | ///
|
---|
165 | #define TIS_PC_STS_GO BIT5
|
---|
166 | ///
|
---|
167 | /// This bit indicates that the TPM has data available as a response.
|
---|
168 | ///
|
---|
169 | #define TIS_PC_STS_DATA BIT4
|
---|
170 | ///
|
---|
171 | /// The TPM sets this bit to a value of 1 when it expects another byte of data for a command.
|
---|
172 | ///
|
---|
173 | #define TIS_PC_STS_EXPECT BIT3
|
---|
174 | ///
|
---|
175 | /// Writes a 1 to this bit to force the TPM to re-send the response.
|
---|
176 | ///
|
---|
177 | #define TIS_PC_STS_RETRY BIT1
|
---|
178 |
|
---|
179 | //
|
---|
180 | // Default TimeOut value
|
---|
181 | //
|
---|
182 | #define TIS_TIMEOUT_A 750 * 1000 // 750ms
|
---|
183 | #define TIS_TIMEOUT_B 2000 * 1000 // 2s
|
---|
184 | #define TIS_TIMEOUT_C 750 * 1000 // 750ms
|
---|
185 | #define TIS_TIMEOUT_D 750 * 1000 // 750ms
|
---|
186 |
|
---|
187 | //
|
---|
188 | // Max TPM command/response length
|
---|
189 | //
|
---|
190 | #define TPMCMDBUFLENGTH 1024
|
---|
191 |
|
---|
192 | /**
|
---|
193 | Check whether the value of a TPM chip register satisfies the input BIT setting.
|
---|
194 |
|
---|
195 | @param[in] Register Address port of register to be checked.
|
---|
196 | @param[in] BitSet Check these data bits are set.
|
---|
197 | @param[in] BitClear Check these data bits are clear.
|
---|
198 | @param[in] TimeOut The max wait time (unit MicroSecond) when checking register.
|
---|
199 |
|
---|
200 | @retval EFI_SUCCESS The register satisfies the check bit.
|
---|
201 | @retval EFI_TIMEOUT The register can't run into the expected status in time.
|
---|
202 | **/
|
---|
203 | EFI_STATUS
|
---|
204 | EFIAPI
|
---|
205 | TisPcWaitRegisterBits (
|
---|
206 | IN UINT8 *Register,
|
---|
207 | IN UINT8 BitSet,
|
---|
208 | IN UINT8 BitClear,
|
---|
209 | IN UINT32 TimeOut
|
---|
210 | );
|
---|
211 |
|
---|
212 | /**
|
---|
213 | Get BurstCount by reading the burstCount field of a TIS register
|
---|
214 | in the time of default TIS_TIMEOUT_D.
|
---|
215 |
|
---|
216 | @param[in] TisReg Pointer to TIS register.
|
---|
217 | @param[out] BurstCount Pointer to a buffer to store the got BurstCount.
|
---|
218 |
|
---|
219 | @retval EFI_SUCCESS Get BurstCount.
|
---|
220 | @retval EFI_INVALID_PARAMETER TisReg is NULL or BurstCount is NULL.
|
---|
221 | @retval EFI_TIMEOUT BurstCount can't be got in time.
|
---|
222 | **/
|
---|
223 | EFI_STATUS
|
---|
224 | EFIAPI
|
---|
225 | TisPcReadBurstCount (
|
---|
226 | IN TIS_PC_REGISTERS_PTR TisReg,
|
---|
227 | OUT UINT16 *BurstCount
|
---|
228 | );
|
---|
229 |
|
---|
230 | /**
|
---|
231 | Set TPM chip to ready state by sending ready command TIS_PC_STS_READY
|
---|
232 | to Status Register in time.
|
---|
233 |
|
---|
234 | @param[in] TisReg Pointer to TIS register.
|
---|
235 |
|
---|
236 | @retval EFI_SUCCESS TPM chip enters into ready state.
|
---|
237 | @retval EFI_INVALID_PARAMETER TisReg is NULL.
|
---|
238 | @retval EFI_TIMEOUT TPM chip can't be set to ready state in time.
|
---|
239 | **/
|
---|
240 | EFI_STATUS
|
---|
241 | EFIAPI
|
---|
242 | TisPcPrepareCommand (
|
---|
243 | IN TIS_PC_REGISTERS_PTR TisReg
|
---|
244 | );
|
---|
245 |
|
---|
246 | /**
|
---|
247 | Get the control of TPM chip by sending requestUse command TIS_PC_ACC_RQUUSE
|
---|
248 | to ACCESS Register in the time of default TIS_TIMEOUT_D.
|
---|
249 |
|
---|
250 | @param[in] TisReg Pointer to TIS register.
|
---|
251 |
|
---|
252 | @retval EFI_SUCCESS Get the control of TPM chip.
|
---|
253 | @retval EFI_INVALID_PARAMETER TisReg is NULL.
|
---|
254 | @retval EFI_NOT_FOUND TPM chip doesn't exit.
|
---|
255 | @retval EFI_TIMEOUT Can't get the TPM control in time.
|
---|
256 | **/
|
---|
257 | EFI_STATUS
|
---|
258 | EFIAPI
|
---|
259 | TisPcRequestUseTpm (
|
---|
260 | IN TIS_PC_REGISTERS_PTR TisReg
|
---|
261 | );
|
---|
262 |
|
---|
263 | /**
|
---|
264 | Single function calculates SHA1 digest value for all raw data. It
|
---|
265 | combines Sha1Init(), Sha1Update() and Sha1Final().
|
---|
266 |
|
---|
267 | @param[in] Data Raw data to be digested.
|
---|
268 | @param[in] DataLen Size of the raw data.
|
---|
269 | @param[out] Digest Pointer to a buffer that stores the final digest.
|
---|
270 |
|
---|
271 | @retval EFI_SUCCESS Always successfully calculate the final digest.
|
---|
272 | **/
|
---|
273 | EFI_STATUS
|
---|
274 | EFIAPI
|
---|
275 | TpmCommHashAll (
|
---|
276 | IN CONST UINT8 *Data,
|
---|
277 | IN UINTN DataLen,
|
---|
278 | OUT TPM_DIGEST *Digest
|
---|
279 | );
|
---|
280 |
|
---|
281 | #endif
|
---|