]>
Commit | Line | Data |
---|---|---|
c1d93242 JY |
1 | /** @file\r |
2 | TIS (TPM Interface Specification) functions used by TPM1.2.\r | |
3 | \r | |
8e997ab8 | 4 | Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>\r |
6aaac383 | 5 | (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r |
c1d93242 JY |
6 | This program and the accompanying materials \r |
7 | are licensed and made available under the terms and conditions of the BSD License \r | |
8 | which accompanies this distribution. The full text of the license may be found at \r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include <Uefi.h>\r | |
17 | #include <IndustryStandard/Tpm12.h>\r | |
18 | #include <Library/BaseLib.h>\r | |
19 | #include <Library/BaseMemoryLib.h>\r | |
20 | #include <Library/IoLib.h>\r | |
21 | #include <Library/TimerLib.h>\r | |
22 | #include <Library/DebugLib.h>\r | |
23 | #include <Library/Tpm12CommandLib.h>\r | |
24 | #include <Library/PcdLib.h>\r | |
25 | \r | |
8e997ab8 JY |
26 | #include <IndustryStandard/TpmPtp.h>\r |
27 | #include <IndustryStandard/TpmTis.h>\r | |
c1d93242 | 28 | \r |
8e997ab8 JY |
29 | typedef enum {\r |
30 | PtpInterfaceTis,\r | |
31 | PtpInterfaceFifo,\r | |
32 | PtpInterfaceCrb,\r | |
33 | PtpInterfaceMax,\r | |
34 | } PTP_INTERFACE_TYPE;\r | |
c1d93242 JY |
35 | \r |
36 | //\r | |
37 | // Max TPM command/reponse length\r | |
38 | //\r | |
39 | #define TPMCMDBUFLENGTH 1024\r | |
40 | \r | |
41 | /**\r | |
42 | Check whether TPM chip exist.\r | |
43 | \r | |
44 | @param[in] TisReg Pointer to TIS register.\r | |
45 | \r | |
46 | @retval TRUE TPM chip exists.\r | |
47 | @retval FALSE TPM chip is not found.\r | |
48 | **/\r | |
49 | BOOLEAN\r | |
50 | Tpm12TisPcPresenceCheck (\r | |
51 | IN TIS_PC_REGISTERS_PTR TisReg\r | |
52 | )\r | |
53 | {\r | |
54 | UINT8 RegRead;\r | |
55 | \r | |
56 | RegRead = MmioRead8 ((UINTN)&TisReg->Access);\r | |
57 | return (BOOLEAN)(RegRead != (UINT8)-1);\r | |
58 | }\r | |
59 | \r | |
e4780913 JY |
60 | /**\r |
61 | Return PTP interface type.\r | |
62 | \r | |
63 | @param[in] Register Pointer to PTP register.\r | |
64 | \r | |
65 | @return PTP interface type.\r | |
66 | **/\r | |
67 | PTP_INTERFACE_TYPE\r | |
68 | Tpm12GetPtpInterface (\r | |
69 | IN VOID *Register\r | |
70 | )\r | |
71 | {\r | |
72 | PTP_CRB_INTERFACE_IDENTIFIER InterfaceId;\r | |
73 | PTP_FIFO_INTERFACE_CAPABILITY InterfaceCapability;\r | |
74 | \r | |
75 | if (!Tpm12TisPcPresenceCheck (Register)) {\r | |
76 | return PtpInterfaceMax;\r | |
77 | }\r | |
78 | //\r | |
79 | // Check interface id\r | |
80 | //\r | |
81 | InterfaceId.Uint32 = MmioRead32 ((UINTN)&((PTP_CRB_REGISTERS *)Register)->InterfaceId);\r | |
82 | InterfaceCapability.Uint32 = MmioRead32 ((UINTN)&((PTP_FIFO_REGISTERS *)Register)->InterfaceCapability);\r | |
83 | \r | |
84 | if ((InterfaceId.Bits.InterfaceType == PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_CRB) &&\r | |
85 | (InterfaceId.Bits.InterfaceVersion == PTP_INTERFACE_IDENTIFIER_INTERFACE_VERSION_CRB) &&\r | |
86 | (InterfaceId.Bits.CapCRB != 0)) {\r | |
87 | return PtpInterfaceCrb;\r | |
88 | }\r | |
89 | if ((InterfaceId.Bits.InterfaceType == PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_FIFO) &&\r | |
90 | (InterfaceId.Bits.InterfaceVersion == PTP_INTERFACE_IDENTIFIER_INTERFACE_VERSION_FIFO) &&\r | |
91 | (InterfaceId.Bits.CapFIFO != 0) &&\r | |
92 | (InterfaceCapability.Bits.InterfaceVersion == INTERFACE_CAPABILITY_INTERFACE_VERSION_PTP)) {\r | |
93 | return PtpInterfaceFifo;\r | |
94 | }\r | |
95 | return PtpInterfaceTis;\r | |
96 | }\r | |
97 | \r | |
c1d93242 JY |
98 | /**\r |
99 | Check whether the value of a TPM chip register satisfies the input BIT setting.\r | |
100 | \r | |
101 | @param[in] Register Address port of register to be checked.\r | |
102 | @param[in] BitSet Check these data bits are set.\r | |
103 | @param[in] BitClear Check these data bits are clear.\r | |
104 | @param[in] TimeOut The max wait time (unit MicroSecond) when checking register.\r | |
105 | \r | |
106 | @retval EFI_SUCCESS The register satisfies the check bit.\r | |
107 | @retval EFI_TIMEOUT The register can't run into the expected status in time.\r | |
108 | **/\r | |
109 | EFI_STATUS\r | |
c1d93242 JY |
110 | Tpm12TisPcWaitRegisterBits (\r |
111 | IN UINT8 *Register,\r | |
112 | IN UINT8 BitSet,\r | |
113 | IN UINT8 BitClear,\r | |
114 | IN UINT32 TimeOut\r | |
115 | )\r | |
116 | {\r | |
117 | UINT8 RegRead;\r | |
118 | UINT32 WaitTime;\r | |
119 | \r | |
120 | for (WaitTime = 0; WaitTime < TimeOut; WaitTime += 30){\r | |
121 | RegRead = MmioRead8 ((UINTN)Register);\r | |
122 | if ((RegRead & BitSet) == BitSet && (RegRead & BitClear) == 0)\r | |
123 | return EFI_SUCCESS;\r | |
124 | MicroSecondDelay (30);\r | |
125 | }\r | |
126 | return EFI_TIMEOUT;\r | |
127 | }\r | |
128 | \r | |
129 | /**\r | |
130 | Get BurstCount by reading the burstCount field of a TIS regiger \r | |
131 | in the time of default TIS_TIMEOUT_D.\r | |
132 | \r | |
133 | @param[in] TisReg Pointer to TIS register.\r | |
134 | @param[out] BurstCount Pointer to a buffer to store the got BurstConut.\r | |
135 | \r | |
136 | @retval EFI_SUCCESS Get BurstCount.\r | |
137 | @retval EFI_INVALID_PARAMETER TisReg is NULL or BurstCount is NULL.\r | |
138 | @retval EFI_TIMEOUT BurstCount can't be got in time.\r | |
139 | **/\r | |
140 | EFI_STATUS\r | |
c1d93242 JY |
141 | Tpm12TisPcReadBurstCount (\r |
142 | IN TIS_PC_REGISTERS_PTR TisReg,\r | |
143 | OUT UINT16 *BurstCount\r | |
144 | )\r | |
145 | {\r | |
146 | UINT32 WaitTime;\r | |
147 | UINT8 DataByte0;\r | |
148 | UINT8 DataByte1;\r | |
149 | \r | |
150 | if (BurstCount == NULL || TisReg == NULL) {\r | |
151 | return EFI_INVALID_PARAMETER;\r | |
152 | }\r | |
153 | \r | |
154 | WaitTime = 0;\r | |
155 | do {\r | |
156 | //\r | |
157 | // TIS_PC_REGISTERS_PTR->burstCount is UINT16, but it is not 2bytes aligned,\r | |
158 | // so it needs to use MmioRead8 to read two times\r | |
159 | //\r | |
160 | DataByte0 = MmioRead8 ((UINTN)&TisReg->BurstCount);\r | |
161 | DataByte1 = MmioRead8 ((UINTN)&TisReg->BurstCount + 1);\r | |
162 | *BurstCount = (UINT16)((DataByte1 << 8) + DataByte0);\r | |
163 | if (*BurstCount != 0) {\r | |
164 | return EFI_SUCCESS;\r | |
165 | }\r | |
166 | MicroSecondDelay (30);\r | |
167 | WaitTime += 30;\r | |
168 | } while (WaitTime < TIS_TIMEOUT_D);\r | |
169 | \r | |
170 | return EFI_TIMEOUT;\r | |
171 | }\r | |
172 | \r | |
173 | /**\r | |
174 | Set TPM chip to ready state by sending ready command TIS_PC_STS_READY \r | |
175 | to Status Register in time.\r | |
176 | \r | |
177 | @param[in] TisReg Pointer to TIS register.\r | |
178 | \r | |
179 | @retval EFI_SUCCESS TPM chip enters into ready state.\r | |
180 | @retval EFI_INVALID_PARAMETER TisReg is NULL.\r | |
181 | @retval EFI_TIMEOUT TPM chip can't be set to ready state in time.\r | |
182 | **/\r | |
183 | EFI_STATUS\r | |
c1d93242 JY |
184 | Tpm12TisPcPrepareCommand (\r |
185 | IN TIS_PC_REGISTERS_PTR TisReg\r | |
186 | )\r | |
187 | {\r | |
188 | EFI_STATUS Status;\r | |
189 | \r | |
190 | if (TisReg == NULL) {\r | |
191 | return EFI_INVALID_PARAMETER;\r | |
192 | }\r | |
193 | \r | |
194 | MmioWrite8((UINTN)&TisReg->Status, TIS_PC_STS_READY);\r | |
195 | Status = Tpm12TisPcWaitRegisterBits (\r | |
196 | &TisReg->Status,\r | |
197 | TIS_PC_STS_READY,\r | |
198 | 0,\r | |
199 | TIS_TIMEOUT_B\r | |
200 | );\r | |
201 | return Status;\r | |
202 | }\r | |
203 | \r | |
204 | /**\r | |
205 | Get the control of TPM chip by sending requestUse command TIS_PC_ACC_RQUUSE \r | |
206 | to ACCESS Register in the time of default TIS_TIMEOUT_A.\r | |
207 | \r | |
208 | @param[in] TisReg Pointer to TIS register.\r | |
209 | \r | |
210 | @retval EFI_SUCCESS Get the control of TPM chip.\r | |
211 | @retval EFI_INVALID_PARAMETER TisReg is NULL.\r | |
212 | @retval EFI_NOT_FOUND TPM chip doesn't exit.\r | |
213 | @retval EFI_TIMEOUT Can't get the TPM control in time.\r | |
214 | **/\r | |
215 | EFI_STATUS\r | |
c1d93242 JY |
216 | Tpm12TisPcRequestUseTpm (\r |
217 | IN TIS_PC_REGISTERS_PTR TisReg\r | |
218 | )\r | |
219 | {\r | |
220 | EFI_STATUS Status;\r | |
221 | \r | |
222 | if (TisReg == NULL) {\r | |
223 | return EFI_INVALID_PARAMETER;\r | |
224 | }\r | |
225 | \r | |
226 | if (!Tpm12TisPcPresenceCheck (TisReg)) {\r | |
227 | return EFI_NOT_FOUND;\r | |
228 | }\r | |
229 | \r | |
230 | MmioWrite8((UINTN)&TisReg->Access, TIS_PC_ACC_RQUUSE);\r | |
231 | Status = Tpm12TisPcWaitRegisterBits (\r | |
232 | &TisReg->Access,\r | |
233 | (UINT8)(TIS_PC_ACC_ACTIVE |TIS_PC_VALID),\r | |
234 | 0,\r | |
235 | TIS_TIMEOUT_A\r | |
236 | );\r | |
237 | return Status;\r | |
238 | }\r | |
239 | \r | |
240 | /**\r | |
241 | Send a command to TPM for execution and return response data.\r | |
242 | \r | |
243 | @param[in] TisReg TPM register space base address. \r | |
244 | @param[in] BufferIn Buffer for command data. \r | |
245 | @param[in] SizeIn Size of command data. \r | |
246 | @param[in, out] BufferOut Buffer for response data. \r | |
247 | @param[in, out] SizeOut Size of response data. \r | |
248 | \r | |
249 | @retval EFI_SUCCESS Operation completed successfully.\r | |
c1d93242 JY |
250 | @retval EFI_BUFFER_TOO_SMALL Response data buffer is too small.\r |
251 | @retval EFI_DEVICE_ERROR Unexpected device behavior.\r | |
252 | @retval EFI_UNSUPPORTED Unsupported TPM version\r | |
253 | \r | |
254 | **/\r | |
255 | EFI_STATUS\r | |
256 | Tpm12TisTpmCommand (\r | |
257 | IN TIS_PC_REGISTERS_PTR TisReg,\r | |
258 | IN UINT8 *BufferIn,\r | |
259 | IN UINT32 SizeIn,\r | |
260 | IN OUT UINT8 *BufferOut,\r | |
261 | IN OUT UINT32 *SizeOut\r | |
262 | )\r | |
263 | {\r | |
264 | EFI_STATUS Status;\r | |
265 | UINT16 BurstCount;\r | |
266 | UINT32 Index;\r | |
267 | UINT32 TpmOutSize;\r | |
268 | UINT16 Data16;\r | |
269 | UINT32 Data32;\r | |
270 | \r | |
271 | DEBUG_CODE (\r | |
272 | UINTN DebugSize;\r | |
273 | \r | |
6aaac383 | 274 | DEBUG ((EFI_D_VERBOSE, "Tpm12TisTpmCommand Send - "));\r |
c1d93242 JY |
275 | if (SizeIn > 0x100) {\r |
276 | DebugSize = 0x40;\r | |
277 | } else {\r | |
278 | DebugSize = SizeIn;\r | |
279 | }\r | |
280 | for (Index = 0; Index < DebugSize; Index++) {\r | |
6aaac383 | 281 | DEBUG ((EFI_D_VERBOSE, "%02x ", BufferIn[Index]));\r |
c1d93242 JY |
282 | }\r |
283 | if (DebugSize != SizeIn) {\r | |
6aaac383 | 284 | DEBUG ((EFI_D_VERBOSE, "...... "));\r |
c1d93242 | 285 | for (Index = SizeIn - 0x20; Index < SizeIn; Index++) {\r |
6aaac383 | 286 | DEBUG ((EFI_D_VERBOSE, "%02x ", BufferIn[Index]));\r |
c1d93242 JY |
287 | }\r |
288 | }\r | |
6aaac383 | 289 | DEBUG ((EFI_D_VERBOSE, "\n"));\r |
c1d93242 JY |
290 | );\r |
291 | TpmOutSize = 0;\r | |
292 | \r | |
293 | Status = Tpm12TisPcPrepareCommand (TisReg);\r | |
294 | if (EFI_ERROR (Status)){\r | |
295 | DEBUG ((DEBUG_ERROR, "Tpm12 is not ready for command!\n"));\r | |
6f785cfc | 296 | return EFI_DEVICE_ERROR;\r |
c1d93242 JY |
297 | }\r |
298 | //\r | |
299 | // Send the command data to Tpm\r | |
300 | //\r | |
301 | Index = 0;\r | |
302 | while (Index < SizeIn) {\r | |
303 | Status = Tpm12TisPcReadBurstCount (TisReg, &BurstCount);\r | |
304 | if (EFI_ERROR (Status)) {\r | |
6f785cfc | 305 | Status = EFI_DEVICE_ERROR;\r |
c1d93242 JY |
306 | goto Exit;\r |
307 | }\r | |
308 | for (; BurstCount > 0 && Index < SizeIn; BurstCount--) {\r | |
309 | MmioWrite8((UINTN)&TisReg->DataFifo, *(BufferIn + Index));\r | |
310 | Index++;\r | |
311 | }\r | |
312 | }\r | |
313 | //\r | |
314 | // Check the Tpm status STS_EXPECT change from 1 to 0\r | |
315 | //\r | |
316 | Status = Tpm12TisPcWaitRegisterBits (\r | |
317 | &TisReg->Status,\r | |
318 | (UINT8) TIS_PC_VALID,\r | |
319 | TIS_PC_STS_EXPECT,\r | |
320 | TIS_TIMEOUT_C\r | |
321 | );\r | |
322 | if (EFI_ERROR (Status)) {\r | |
323 | DEBUG ((DEBUG_ERROR, "Tpm12 The send buffer too small!\n"));\r | |
324 | Status = EFI_BUFFER_TOO_SMALL;\r | |
325 | goto Exit;\r | |
326 | }\r | |
327 | //\r | |
328 | // Executed the TPM command and waiting for the response data ready\r | |
329 | //\r | |
330 | MmioWrite8((UINTN)&TisReg->Status, TIS_PC_STS_GO);\r | |
331 | Status = Tpm12TisPcWaitRegisterBits (\r | |
332 | &TisReg->Status,\r | |
333 | (UINT8) (TIS_PC_VALID | TIS_PC_STS_DATA),\r | |
334 | 0,\r | |
335 | TIS_TIMEOUT_B\r | |
336 | );\r | |
337 | if (EFI_ERROR (Status)) {\r | |
338 | DEBUG ((DEBUG_ERROR, "Wait for Tpm12 response data time out!!\n"));\r | |
6f785cfc | 339 | Status = EFI_DEVICE_ERROR;\r |
c1d93242 JY |
340 | goto Exit;\r |
341 | }\r | |
342 | //\r | |
343 | // Get response data header\r | |
344 | //\r | |
345 | Index = 0;\r | |
346 | BurstCount = 0;\r | |
347 | while (Index < sizeof (TPM_RSP_COMMAND_HDR)) {\r | |
348 | Status = Tpm12TisPcReadBurstCount (TisReg, &BurstCount);\r | |
349 | if (EFI_ERROR (Status)) {\r | |
6f785cfc | 350 | Status = EFI_DEVICE_ERROR;\r |
c1d93242 JY |
351 | goto Exit;\r |
352 | }\r | |
353 | for (; BurstCount > 0; BurstCount--) {\r | |
354 | *(BufferOut + Index) = MmioRead8 ((UINTN)&TisReg->DataFifo);\r | |
355 | Index++;\r | |
356 | if (Index == sizeof (TPM_RSP_COMMAND_HDR)) break;\r | |
357 | }\r | |
358 | }\r | |
359 | DEBUG_CODE (\r | |
6aaac383 | 360 | DEBUG ((EFI_D_VERBOSE, "Tpm12TisTpmCommand ReceiveHeader - "));\r |
c1d93242 | 361 | for (Index = 0; Index < sizeof (TPM_RSP_COMMAND_HDR); Index++) {\r |
6aaac383 | 362 | DEBUG ((EFI_D_VERBOSE, "%02x ", BufferOut[Index]));\r |
c1d93242 | 363 | }\r |
6aaac383 | 364 | DEBUG ((EFI_D_VERBOSE, "\n"));\r |
c1d93242 JY |
365 | );\r |
366 | //\r | |
367 | // Check the reponse data header (tag,parasize and returncode )\r | |
368 | //\r | |
369 | CopyMem (&Data16, BufferOut, sizeof (UINT16));\r | |
370 | if (SwapBytes16 (Data16) != TPM_TAG_RSP_COMMAND) {\r | |
371 | DEBUG ((EFI_D_ERROR, "TPM12: TPM_ST_RSP error - %x\n", TPM_TAG_RSP_COMMAND));\r | |
372 | Status = EFI_UNSUPPORTED;\r | |
373 | goto Exit;\r | |
374 | }\r | |
375 | \r | |
376 | CopyMem (&Data32, (BufferOut + 2), sizeof (UINT32));\r | |
377 | TpmOutSize = SwapBytes32 (Data32);\r | |
378 | if (*SizeOut < TpmOutSize) {\r | |
379 | Status = EFI_BUFFER_TOO_SMALL;\r | |
380 | goto Exit;\r | |
381 | }\r | |
382 | *SizeOut = TpmOutSize;\r | |
383 | //\r | |
384 | // Continue reading the remaining data\r | |
385 | //\r | |
386 | while ( Index < TpmOutSize ) {\r | |
387 | for (; BurstCount > 0; BurstCount--) {\r | |
388 | *(BufferOut + Index) = MmioRead8 ((UINTN)&TisReg->DataFifo);\r | |
389 | Index++;\r | |
390 | if (Index == TpmOutSize) {\r | |
391 | Status = EFI_SUCCESS;\r | |
392 | goto Exit;\r | |
393 | }\r | |
394 | }\r | |
395 | Status = Tpm12TisPcReadBurstCount (TisReg, &BurstCount);\r | |
396 | if (EFI_ERROR (Status)) {\r | |
6f785cfc | 397 | Status = EFI_DEVICE_ERROR;\r |
c1d93242 JY |
398 | goto Exit;\r |
399 | }\r | |
400 | }\r | |
401 | Exit:\r | |
402 | DEBUG_CODE (\r | |
6aaac383 | 403 | DEBUG ((EFI_D_VERBOSE, "Tpm12TisTpmCommand Receive - "));\r |
c1d93242 | 404 | for (Index = 0; Index < TpmOutSize; Index++) {\r |
6aaac383 | 405 | DEBUG ((EFI_D_VERBOSE, "%02x ", BufferOut[Index]));\r |
c1d93242 | 406 | }\r |
6aaac383 | 407 | DEBUG ((EFI_D_VERBOSE, "\n"));\r |
c1d93242 JY |
408 | );\r |
409 | MmioWrite8((UINTN)&TisReg->Status, TIS_PC_STS_READY);\r | |
410 | return Status;\r | |
411 | }\r | |
412 | \r | |
413 | /**\r | |
414 | This service enables the sending of commands to the TPM12.\r | |
415 | \r | |
416 | @param[in] InputParameterBlockSize Size of the TPM12 input parameter block.\r | |
417 | @param[in] InputParameterBlock Pointer to the TPM12 input parameter block.\r | |
418 | @param[in,out] OutputParameterBlockSize Size of the TPM12 output parameter block.\r | |
419 | @param[in] OutputParameterBlock Pointer to the TPM12 output parameter block.\r | |
420 | \r | |
421 | @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.\r | |
422 | @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.\r | |
423 | @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small. \r | |
424 | **/\r | |
425 | EFI_STATUS\r | |
426 | EFIAPI\r | |
427 | Tpm12SubmitCommand (\r | |
428 | IN UINT32 InputParameterBlockSize,\r | |
429 | IN UINT8 *InputParameterBlock,\r | |
430 | IN OUT UINT32 *OutputParameterBlockSize,\r | |
431 | IN UINT8 *OutputParameterBlock\r | |
432 | )\r | |
433 | {\r | |
e4780913 | 434 | PTP_INTERFACE_TYPE PtpInterface;\r |
c1d93242 | 435 | \r |
8e997ab8 | 436 | //\r |
e4780913 | 437 | // Special handle for TPM1.2 to check PTP too, because PTP/TIS share same register address.\r |
8e997ab8 | 438 | //\r |
e4780913 JY |
439 | PtpInterface = Tpm12GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));\r |
440 | switch (PtpInterface) {\r | |
441 | case PtpInterfaceFifo:\r | |
442 | case PtpInterfaceTis:\r | |
443 | return Tpm12TisTpmCommand (\r | |
444 | (TIS_PC_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress),\r | |
445 | InputParameterBlock,\r | |
446 | InputParameterBlockSize,\r | |
447 | OutputParameterBlock,\r | |
448 | OutputParameterBlockSize\r | |
449 | );\r | |
450 | case PtpInterfaceCrb:\r | |
451 | //\r | |
452 | // No need to support CRB because it is only accept TPM2 command.\r | |
453 | //\r | |
454 | default:\r | |
455 | return EFI_DEVICE_ERROR;\r | |
8e997ab8 | 456 | }\r |
e4780913 | 457 | \r |
8e997ab8 JY |
458 | }\r |
459 | \r | |
460 | /**\r | |
461 | Check whether the value of a TPM chip register satisfies the input BIT setting.\r | |
462 | \r | |
463 | @param[in] Register Address port of register to be checked.\r | |
464 | @param[in] BitSet Check these data bits are set.\r | |
465 | @param[in] BitClear Check these data bits are clear.\r | |
466 | @param[in] TimeOut The max wait time (unit MicroSecond) when checking register.\r | |
467 | \r | |
468 | @retval EFI_SUCCESS The register satisfies the check bit.\r | |
469 | @retval EFI_TIMEOUT The register can't run into the expected status in time.\r | |
470 | **/\r | |
471 | EFI_STATUS\r | |
472 | Tpm12PtpCrbWaitRegisterBits (\r | |
473 | IN UINT32 *Register,\r | |
474 | IN UINT32 BitSet,\r | |
475 | IN UINT32 BitClear,\r | |
476 | IN UINT32 TimeOut\r | |
477 | )\r | |
478 | {\r | |
479 | UINT32 RegRead;\r | |
480 | UINT32 WaitTime;\r | |
481 | \r | |
482 | for (WaitTime = 0; WaitTime < TimeOut; WaitTime += 30){\r | |
483 | RegRead = MmioRead32 ((UINTN)Register);\r | |
484 | if ((RegRead & BitSet) == BitSet && (RegRead & BitClear) == 0) {\r | |
485 | return EFI_SUCCESS;\r | |
486 | }\r | |
487 | MicroSecondDelay (30);\r | |
488 | }\r | |
489 | return EFI_TIMEOUT;\r | |
490 | }\r | |
491 | \r | |
492 | /**\r | |
493 | Get the control of TPM chip.\r | |
494 | \r | |
495 | @param[in] CrbReg Pointer to CRB register.\r | |
496 | \r | |
497 | @retval EFI_SUCCESS Get the control of TPM chip.\r | |
498 | @retval EFI_INVALID_PARAMETER CrbReg is NULL.\r | |
499 | @retval EFI_NOT_FOUND TPM chip doesn't exit.\r | |
500 | @retval EFI_TIMEOUT Can't get the TPM control in time.\r | |
501 | **/\r | |
502 | EFI_STATUS\r | |
503 | Tpm12PtpCrbRequestUseTpm (\r | |
504 | IN PTP_CRB_REGISTERS_PTR CrbReg\r | |
505 | )\r | |
506 | {\r | |
507 | EFI_STATUS Status;\r | |
508 | \r | |
509 | MmioWrite32((UINTN)&CrbReg->LocalityControl, PTP_CRB_LOCALITY_CONTROL_REQUEST_ACCESS);\r | |
510 | Status = Tpm12PtpCrbWaitRegisterBits (\r | |
511 | &CrbReg->LocalityStatus,\r | |
512 | PTP_CRB_LOCALITY_STATUS_GRANTED,\r | |
513 | 0,\r | |
514 | PTP_TIMEOUT_A\r | |
515 | );\r | |
516 | return Status;\r | |
517 | }\r | |
518 | \r | |
c1d93242 JY |
519 | /**\r |
520 | This service requests use TPM12.\r | |
521 | \r | |
522 | @retval EFI_SUCCESS Get the control of TPM12 chip.\r | |
523 | @retval EFI_NOT_FOUND TPM12 not found.\r | |
524 | @retval EFI_DEVICE_ERROR Unexpected device behavior.\r | |
525 | **/\r | |
526 | EFI_STATUS\r | |
527 | EFIAPI\r | |
528 | Tpm12RequestUseTpm (\r | |
529 | VOID\r | |
530 | )\r | |
531 | {\r | |
8e997ab8 JY |
532 | PTP_INTERFACE_TYPE PtpInterface;\r |
533 | \r | |
534 | //\r | |
535 | // Special handle for TPM1.2 to check PTP too, because PTP/TIS share same register address.\r | |
536 | // Some other program might leverage this function to check the existence of TPM chip.\r | |
537 | //\r | |
538 | PtpInterface = Tpm12GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));\r | |
539 | switch (PtpInterface) {\r | |
540 | case PtpInterfaceCrb:\r | |
541 | return Tpm12PtpCrbRequestUseTpm ((PTP_CRB_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress));\r | |
542 | case PtpInterfaceFifo:\r | |
543 | case PtpInterfaceTis:\r | |
544 | return Tpm12TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress));\r | |
545 | default:\r | |
546 | return EFI_NOT_FOUND;\r | |
547 | }\r | |
c1d93242 | 548 | }\r |