]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Tis.c
Handle TPM device error and avoid deadloop in BDS.
[mirror_edk2.git] / SecurityPkg / Library / Tpm2DeviceLibDTpm / Tpm2Tis.c
CommitLineData
c1d93242
JY
1/** @file\r
2 TIS (TPM Interface Specification) functions used by dTPM2.0 library.\r
3 \r
6f785cfc 4Copyright (c) 2013 - 2015, Intel Corporation. All rights reserved.<BR>\r
c1d93242
JY
5This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <IndustryStandard/Tpm20.h>\r
16\r
17#include <Library/BaseLib.h>\r
18#include <Library/BaseMemoryLib.h>\r
19#include <Library/IoLib.h>\r
20#include <Library/TimerLib.h>\r
21#include <Library/DebugLib.h>\r
22#include <Library/Tpm2DeviceLib.h>\r
23#include <Library/PcdLib.h>\r
24\r
25//\r
26// Set structure alignment to 1-byte\r
27//\r
28#pragma pack (1)\r
29\r
30//\r
31// Register set map as specified in TIS specification Chapter 10\r
32//\r
33typedef struct {\r
34 ///\r
35 /// Used to gain ownership for this particular port.\r
36 ///\r
37 UINT8 Access; // 0\r
38 UINT8 Reserved1[7]; // 1\r
39 ///\r
40 /// Controls interrupts.\r
41 ///\r
42 UINT32 IntEnable; // 8\r
43 ///\r
44 /// SIRQ vector to be used by the TPM.\r
45 ///\r
46 UINT8 IntVector; // 0ch\r
47 UINT8 Reserved2[3]; // 0dh\r
48 ///\r
49 /// What caused interrupt.\r
50 ///\r
51 UINT32 IntSts; // 10h\r
52 ///\r
53 /// Shows which interrupts are supported by that particular TPM.\r
54 ///\r
55 UINT32 IntfCapability; // 14h\r
56 ///\r
57 /// Status Register. Provides status of the TPM.\r
58 ///\r
59 UINT8 Status; // 18h\r
60 ///\r
61 /// Number of consecutive writes that can be done to the TPM.\r
62 ///\r
63 UINT16 BurstCount; // 19h\r
64 ///\r
65 /// TPM2 support CANCEL at BIT[24] of STATUS register (WO)\r
66 ///\r
67 UINT8 StatusEx; // 1Bh\r
68 UINT8 Reserved3[8];\r
69 ///\r
70 /// Read or write FIFO, depending on transaction.\r
71 ///\r
72 UINT32 DataFifo; // 24h\r
73 UINT8 Reserved4[0xed8]; // 28h\r
74 ///\r
75 /// Vendor ID\r
76 ///\r
77 UINT16 Vid; // 0f00h\r
78 ///\r
79 /// Device ID\r
80 ///\r
81 UINT16 Did; // 0f02h\r
82 ///\r
83 /// Revision ID\r
84 ///\r
85 UINT8 Rid; // 0f04h\r
86 ///\r
87 /// TCG defined configuration registers.\r
88 ///\r
89 UINT8 TcgDefined[0x7b]; // 0f05h\r
90 ///\r
91 /// Alias to I/O legacy space.\r
92 ///\r
93 UINT32 LegacyAddress1; // 0f80h\r
94 ///\r
95 /// Additional 8 bits for I/O legacy space extension.\r
96 ///\r
97 UINT32 LegacyAddress1Ex; // 0f84h\r
98 ///\r
99 /// Alias to second I/O legacy space.\r
100 ///\r
101 UINT32 LegacyAddress2; // 0f88h\r
102 ///\r
103 /// Additional 8 bits for second I/O legacy space extension.\r
104 ///\r
105 UINT32 LegacyAddress2Ex; // 0f8ch\r
106 ///\r
107 /// Vendor-defined configuration registers.\r
108 ///\r
109 UINT8 VendorDefined[0x70];// 0f90h\r
110} TIS_PC_REGISTERS;\r
111\r
112//\r
113// Restore original structure alignment\r
114//\r
115#pragma pack ()\r
116\r
117//\r
118// Define pointer types used to access TIS registers on PC\r
119//\r
120typedef TIS_PC_REGISTERS *TIS_PC_REGISTERS_PTR;\r
121\r
122//\r
123// Define bits of ACCESS and STATUS registers\r
124//\r
125\r
126///\r
127/// This bit is a 1 to indicate that the other bits in this register are valid.\r
128///\r
129#define TIS_PC_VALID BIT7\r
130///\r
131/// Indicate that this locality is active.\r
132///\r
133#define TIS_PC_ACC_ACTIVE BIT5\r
134///\r
135/// Set to 1 to indicate that this locality had the TPM taken away while\r
136/// this locality had the TIS_PC_ACC_ACTIVE bit set.\r
137///\r
138#define TIS_PC_ACC_SEIZED BIT4\r
139///\r
140/// Set to 1 to indicate that TPM MUST reset the\r
141/// TIS_PC_ACC_ACTIVE bit and remove ownership for localities less than the\r
142/// locality that is writing this bit.\r
143///\r
144#define TIS_PC_ACC_SEIZE BIT3\r
145///\r
146/// When this bit is 1, another locality is requesting usage of the TPM.\r
147///\r
148#define TIS_PC_ACC_PENDIND BIT2\r
149///\r
150/// Set to 1 to indicate that this locality is requesting to use TPM.\r
151///\r
152#define TIS_PC_ACC_RQUUSE BIT1\r
153///\r
154/// A value of 1 indicates that a T/OS has not been established on the platform\r
155///\r
156#define TIS_PC_ACC_ESTABLISH BIT0\r
157\r
158///\r
159/// When this bit is 1, TPM is in the Ready state, \r
160/// indicating it is ready to receive a new command.\r
161///\r
162#define TIS_PC_STS_READY BIT6\r
163///\r
164/// Write a 1 to this bit to cause the TPM to execute that command.\r
165///\r
166#define TIS_PC_STS_GO BIT5\r
167///\r
168/// This bit indicates that the TPM has data available as a response.\r
169///\r
170#define TIS_PC_STS_DATA BIT4\r
171///\r
172/// The TPM sets this bit to a value of 1 when it expects another byte of data for a command.\r
173///\r
174#define TIS_PC_STS_EXPECT BIT3\r
175///\r
176/// Writes a 1 to this bit to force the TPM to re-send the response.\r
177///\r
178#define TIS_PC_STS_RETRY BIT1\r
179\r
180//\r
181// Default TimeOut value\r
182//\r
183#define TIS_TIMEOUT_A (1000 * 1000) // 1s\r
184#define TIS_TIMEOUT_B (2000 * 1000) // 2s\r
185#define TIS_TIMEOUT_C (1000 * 1000) // 1s\r
186#define TIS_TIMEOUT_D (1000 * 1000) // 1s\r
187\r
188#define TIS_TIMEOUT_MAX (90000 * 1000) // 90s\r
189\r
190//\r
191// Max TPM command/reponse length\r
192//\r
193#define TPMCMDBUFLENGTH 0x500\r
194\r
195/**\r
196 Check whether TPM chip exist.\r
197\r
198 @param[in] TisReg Pointer to TIS register.\r
199\r
200 @retval TRUE TPM chip exists.\r
201 @retval FALSE TPM chip is not found.\r
202**/\r
203BOOLEAN\r
204TisPcPresenceCheck (\r
205 IN TIS_PC_REGISTERS_PTR TisReg\r
206 )\r
207{\r
208 UINT8 RegRead;\r
209 \r
210 RegRead = MmioRead8 ((UINTN)&TisReg->Access);\r
211 return (BOOLEAN)(RegRead != (UINT8)-1);\r
212}\r
213\r
214/**\r
215 Check whether the value of a TPM chip register satisfies the input BIT setting.\r
216\r
217 @param[in] Register Address port of register to be checked.\r
218 @param[in] BitSet Check these data bits are set.\r
219 @param[in] BitClear Check these data bits are clear.\r
220 @param[in] TimeOut The max wait time (unit MicroSecond) when checking register.\r
221\r
222 @retval EFI_SUCCESS The register satisfies the check bit.\r
223 @retval EFI_TIMEOUT The register can't run into the expected status in time.\r
224**/\r
225EFI_STATUS\r
c1d93242
JY
226TisPcWaitRegisterBits (\r
227 IN UINT8 *Register,\r
228 IN UINT8 BitSet,\r
229 IN UINT8 BitClear,\r
230 IN UINT32 TimeOut\r
231 )\r
232{\r
233 UINT8 RegRead;\r
234 UINT32 WaitTime;\r
235\r
236 for (WaitTime = 0; WaitTime < TimeOut; WaitTime += 30){\r
237 RegRead = MmioRead8 ((UINTN)Register);\r
238 if ((RegRead & BitSet) == BitSet && (RegRead & BitClear) == 0)\r
239 return EFI_SUCCESS;\r
240 MicroSecondDelay (30);\r
241 }\r
242 return EFI_TIMEOUT;\r
243}\r
244\r
245/**\r
246 Get BurstCount by reading the burstCount field of a TIS regiger \r
247 in the time of default TIS_TIMEOUT_D.\r
248\r
249 @param[in] TisReg Pointer to TIS register.\r
250 @param[out] BurstCount Pointer to a buffer to store the got BurstConut.\r
251\r
252 @retval EFI_SUCCESS Get BurstCount.\r
253 @retval EFI_INVALID_PARAMETER TisReg is NULL or BurstCount is NULL.\r
254 @retval EFI_TIMEOUT BurstCount can't be got in time.\r
255**/\r
256EFI_STATUS\r
c1d93242
JY
257TisPcReadBurstCount (\r
258 IN TIS_PC_REGISTERS_PTR TisReg,\r
259 OUT UINT16 *BurstCount\r
260 )\r
261{\r
262 UINT32 WaitTime;\r
263 UINT8 DataByte0;\r
264 UINT8 DataByte1;\r
265\r
266 if (BurstCount == NULL || TisReg == NULL) {\r
267 return EFI_INVALID_PARAMETER;\r
268 }\r
269\r
270 WaitTime = 0;\r
271 do {\r
272 //\r
273 // TIS_PC_REGISTERS_PTR->burstCount is UINT16, but it is not 2bytes aligned,\r
274 // so it needs to use MmioRead8 to read two times\r
275 //\r
276 DataByte0 = MmioRead8 ((UINTN)&TisReg->BurstCount);\r
277 DataByte1 = MmioRead8 ((UINTN)&TisReg->BurstCount + 1);\r
278 *BurstCount = (UINT16)((DataByte1 << 8) + DataByte0);\r
279 if (*BurstCount != 0) {\r
280 return EFI_SUCCESS;\r
281 }\r
282 MicroSecondDelay (30);\r
283 WaitTime += 30;\r
284 } while (WaitTime < TIS_TIMEOUT_D);\r
285\r
286 return EFI_TIMEOUT;\r
287}\r
288\r
289/**\r
290 Set TPM chip to ready state by sending ready command TIS_PC_STS_READY \r
291 to Status Register in time.\r
292\r
293 @param[in] TisReg Pointer to TIS register.\r
294\r
295 @retval EFI_SUCCESS TPM chip enters into ready state.\r
296 @retval EFI_INVALID_PARAMETER TisReg is NULL.\r
297 @retval EFI_TIMEOUT TPM chip can't be set to ready state in time.\r
298**/\r
299EFI_STATUS\r
c1d93242
JY
300TisPcPrepareCommand (\r
301 IN TIS_PC_REGISTERS_PTR TisReg\r
302 )\r
303{\r
304 EFI_STATUS Status;\r
305\r
306 if (TisReg == NULL) {\r
307 return EFI_INVALID_PARAMETER;\r
308 }\r
309\r
310 MmioWrite8((UINTN)&TisReg->Status, TIS_PC_STS_READY);\r
311 Status = TisPcWaitRegisterBits (\r
312 &TisReg->Status,\r
313 TIS_PC_STS_READY,\r
314 0,\r
315 TIS_TIMEOUT_B\r
316 );\r
317 return Status;\r
318}\r
319\r
320/**\r
321 Get the control of TPM chip by sending requestUse command TIS_PC_ACC_RQUUSE \r
322 to ACCESS Register in the time of default TIS_TIMEOUT_A.\r
323\r
324 @param[in] TisReg Pointer to TIS register.\r
325\r
326 @retval EFI_SUCCESS Get the control of TPM chip.\r
327 @retval EFI_INVALID_PARAMETER TisReg is NULL.\r
328 @retval EFI_NOT_FOUND TPM chip doesn't exit.\r
329 @retval EFI_TIMEOUT Can't get the TPM control in time.\r
330**/\r
331EFI_STATUS\r
c1d93242
JY
332TisPcRequestUseTpm (\r
333 IN TIS_PC_REGISTERS_PTR TisReg\r
334 )\r
335{\r
336 EFI_STATUS Status;\r
337 \r
338 if (TisReg == NULL) {\r
339 return EFI_INVALID_PARAMETER;\r
340 }\r
341 \r
342 if (!TisPcPresenceCheck (TisReg)) {\r
343 return EFI_NOT_FOUND;\r
344 }\r
345\r
346 MmioWrite8((UINTN)&TisReg->Access, TIS_PC_ACC_RQUUSE);\r
347 Status = TisPcWaitRegisterBits (\r
348 &TisReg->Access,\r
349 (UINT8)(TIS_PC_ACC_ACTIVE |TIS_PC_VALID),\r
350 0,\r
351 TIS_TIMEOUT_A\r
352 );\r
353 return Status;\r
354}\r
355\r
356/**\r
357 Send a command to TPM for execution and return response data.\r
358\r
359 @param[in] TisReg TPM register space base address. \r
360 @param[in] BufferIn Buffer for command data. \r
361 @param[in] SizeIn Size of command data. \r
362 @param[in, out] BufferOut Buffer for response data. \r
363 @param[in, out] SizeOut Size of response data. \r
364 \r
365 @retval EFI_SUCCESS Operation completed successfully.\r
c1d93242
JY
366 @retval EFI_BUFFER_TOO_SMALL Response data buffer is too small.\r
367 @retval EFI_DEVICE_ERROR Unexpected device behavior.\r
368 @retval EFI_UNSUPPORTED Unsupported TPM version\r
369\r
370**/\r
371EFI_STATUS\r
372TisTpmCommand (\r
373 IN TIS_PC_REGISTERS_PTR TisReg,\r
374 IN UINT8 *BufferIn,\r
375 IN UINT32 SizeIn,\r
376 IN OUT UINT8 *BufferOut,\r
377 IN OUT UINT32 *SizeOut\r
378 )\r
379{\r
380 EFI_STATUS Status;\r
381 UINT16 BurstCount;\r
382 UINT32 Index;\r
383 UINT32 TpmOutSize;\r
384 UINT16 Data16;\r
385 UINT32 Data32;\r
386\r
387 DEBUG_CODE (\r
388 UINTN DebugSize;\r
389\r
6f785cfc 390 DEBUG ((EFI_D_INFO, "Tpm2TisTpmCommand Send - "));\r
c1d93242
JY
391 if (SizeIn > 0x100) {\r
392 DebugSize = 0x40;\r
393 } else {\r
394 DebugSize = SizeIn;\r
395 }\r
396 for (Index = 0; Index < DebugSize; Index++) {\r
397 DEBUG ((EFI_D_INFO, "%02x ", BufferIn[Index]));\r
398 }\r
399 if (DebugSize != SizeIn) {\r
400 DEBUG ((EFI_D_INFO, "...... "));\r
401 for (Index = SizeIn - 0x20; Index < SizeIn; Index++) {\r
402 DEBUG ((EFI_D_INFO, "%02x ", BufferIn[Index]));\r
403 }\r
404 }\r
405 DEBUG ((EFI_D_INFO, "\n"));\r
406 );\r
407 TpmOutSize = 0;\r
408\r
409 Status = TisPcPrepareCommand (TisReg);\r
410 if (EFI_ERROR (Status)){\r
6f785cfc
JY
411 DEBUG ((DEBUG_ERROR, "Tpm2 is not ready for command!\n"));\r
412 return EFI_DEVICE_ERROR;\r
c1d93242
JY
413 }\r
414 //\r
415 // Send the command data to Tpm\r
416 //\r
417 Index = 0;\r
418 while (Index < SizeIn) {\r
419 Status = TisPcReadBurstCount (TisReg, &BurstCount);\r
420 if (EFI_ERROR (Status)) {\r
6f785cfc 421 Status = EFI_DEVICE_ERROR;\r
c1d93242
JY
422 goto Exit;\r
423 }\r
424 for (; BurstCount > 0 && Index < SizeIn; BurstCount--) {\r
425 MmioWrite8((UINTN)&TisReg->DataFifo, *(BufferIn + Index));\r
426 Index++;\r
427 }\r
428 }\r
429 //\r
430 // Check the Tpm status STS_EXPECT change from 1 to 0\r
431 //\r
432 Status = TisPcWaitRegisterBits (\r
433 &TisReg->Status,\r
434 (UINT8) TIS_PC_VALID,\r
435 TIS_PC_STS_EXPECT,\r
436 TIS_TIMEOUT_C\r
437 );\r
438 if (EFI_ERROR (Status)) {\r
6f785cfc 439 DEBUG ((DEBUG_ERROR, "Tpm2 The send buffer too small!\n"));\r
c1d93242
JY
440 Status = EFI_BUFFER_TOO_SMALL;\r
441 goto Exit;\r
442 }\r
443 //\r
444 // Executed the TPM command and waiting for the response data ready\r
445 //\r
446 MmioWrite8((UINTN)&TisReg->Status, TIS_PC_STS_GO);\r
447\r
448 //\r
449 // NOTE: That may take many seconds to minutes for certain commands, such as key generation.\r
450 //\r
451 Status = TisPcWaitRegisterBits (\r
452 &TisReg->Status,\r
453 (UINT8) (TIS_PC_VALID | TIS_PC_STS_DATA),\r
454 0,\r
455 TIS_TIMEOUT_MAX\r
456 );\r
457 if (EFI_ERROR (Status)) {\r
6f785cfc
JY
458 DEBUG ((DEBUG_ERROR, "Wait for Tpm2 response data time out!!\n"));\r
459 Status = EFI_DEVICE_ERROR;\r
c1d93242
JY
460 goto Exit;\r
461 }\r
462 //\r
463 // Get response data header\r
464 //\r
465 Index = 0;\r
466 BurstCount = 0;\r
467 while (Index < sizeof (TPM2_RESPONSE_HEADER)) {\r
468 Status = TisPcReadBurstCount (TisReg, &BurstCount);\r
469 if (EFI_ERROR (Status)) {\r
6f785cfc 470 Status = EFI_DEVICE_ERROR;\r
c1d93242
JY
471 goto Exit;\r
472 }\r
473 for (; BurstCount > 0; BurstCount--) {\r
474 *(BufferOut + Index) = MmioRead8 ((UINTN)&TisReg->DataFifo);\r
475 Index++;\r
476 if (Index == sizeof (TPM2_RESPONSE_HEADER)) break;\r
477 }\r
478 }\r
479 DEBUG_CODE (\r
480 DEBUG ((EFI_D_INFO, "TisTpmCommand ReceiveHeader - "));\r
481 for (Index = 0; Index < sizeof (TPM2_RESPONSE_HEADER); Index++) {\r
482 DEBUG ((EFI_D_INFO, "%02x ", BufferOut[Index]));\r
483 }\r
484 DEBUG ((EFI_D_INFO, "\n"));\r
485 );\r
486 //\r
487 // Check the reponse data header (tag,parasize and returncode )\r
488 //\r
489 CopyMem (&Data16, BufferOut, sizeof (UINT16));\r
490 // TPM2 should not use this RSP_COMMAND\r
491 if (SwapBytes16 (Data16) == TPM_ST_RSP_COMMAND) {\r
6f785cfc 492 DEBUG ((EFI_D_ERROR, "TPM2: TPM_ST_RSP error - %x\n", TPM_ST_RSP_COMMAND));\r
c1d93242
JY
493 Status = EFI_UNSUPPORTED;\r
494 goto Exit;\r
495 }\r
496\r
497 CopyMem (&Data32, (BufferOut + 2), sizeof (UINT32));\r
498 TpmOutSize = SwapBytes32 (Data32);\r
499 if (*SizeOut < TpmOutSize) {\r
500 Status = EFI_BUFFER_TOO_SMALL;\r
501 goto Exit;\r
502 }\r
503 *SizeOut = TpmOutSize;\r
504 //\r
505 // Continue reading the remaining data\r
506 //\r
507 while ( Index < TpmOutSize ) {\r
508 for (; BurstCount > 0; BurstCount--) {\r
509 *(BufferOut + Index) = MmioRead8 ((UINTN)&TisReg->DataFifo);\r
510 Index++;\r
511 if (Index == TpmOutSize) {\r
512 Status = EFI_SUCCESS;\r
513 goto Exit;\r
514 }\r
515 }\r
516 Status = TisPcReadBurstCount (TisReg, &BurstCount);\r
517 if (EFI_ERROR (Status)) {\r
6f785cfc 518 Status = EFI_DEVICE_ERROR;\r
c1d93242
JY
519 goto Exit;\r
520 }\r
521 }\r
522Exit:\r
523 DEBUG_CODE (\r
6f785cfc 524 DEBUG ((EFI_D_INFO, "Tpm2TisTpmCommand Receive - "));\r
c1d93242
JY
525 for (Index = 0; Index < TpmOutSize; Index++) {\r
526 DEBUG ((EFI_D_INFO, "%02x ", BufferOut[Index]));\r
527 }\r
528 DEBUG ((EFI_D_INFO, "\n"));\r
529 );\r
530 MmioWrite8((UINTN)&TisReg->Status, TIS_PC_STS_READY);\r
531 return Status;\r
532}\r
533\r
534/**\r
535 This service enables the sending of commands to the TPM2.\r
536\r
537 @param[in] InputParameterBlockSize Size of the TPM2 input parameter block.\r
538 @param[in] InputParameterBlock Pointer to the TPM2 input parameter block.\r
539 @param[in,out] OutputParameterBlockSize Size of the TPM2 output parameter block.\r
540 @param[in] OutputParameterBlock Pointer to the TPM2 output parameter block.\r
541\r
542 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.\r
543 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.\r
544 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small. \r
545**/\r
546EFI_STATUS\r
547EFIAPI\r
548DTpm2SubmitCommand (\r
549 IN UINT32 InputParameterBlockSize,\r
550 IN UINT8 *InputParameterBlock,\r
551 IN OUT UINT32 *OutputParameterBlockSize,\r
552 IN UINT8 *OutputParameterBlock\r
553 )\r
554{\r
555 return TisTpmCommand (\r
556 (TIS_PC_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress),\r
557 InputParameterBlock,\r
558 InputParameterBlockSize,\r
559 OutputParameterBlock,\r
560 OutputParameterBlockSize\r
561 );\r
562}\r
563\r
564/**\r
565 This service requests use TPM2.\r
566\r
567 @retval EFI_SUCCESS Get the control of TPM2 chip.\r
568 @retval EFI_NOT_FOUND TPM2 not found.\r
569 @retval EFI_DEVICE_ERROR Unexpected device behavior.\r
570**/\r
571EFI_STATUS\r
572EFIAPI\r
573DTpm2RequestUseTpm (\r
574 VOID\r
575 )\r
576{\r
577 return TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress));\r
578}\r