]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Tis.c
Add TPM Physical Presence >=128 operation value support.
[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
4Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
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
226EFIAPI\r
227TisPcWaitRegisterBits (\r
228 IN UINT8 *Register,\r
229 IN UINT8 BitSet,\r
230 IN UINT8 BitClear,\r
231 IN UINT32 TimeOut\r
232 )\r
233{\r
234 UINT8 RegRead;\r
235 UINT32 WaitTime;\r
236\r
237 for (WaitTime = 0; WaitTime < TimeOut; WaitTime += 30){\r
238 RegRead = MmioRead8 ((UINTN)Register);\r
239 if ((RegRead & BitSet) == BitSet && (RegRead & BitClear) == 0)\r
240 return EFI_SUCCESS;\r
241 MicroSecondDelay (30);\r
242 }\r
243 return EFI_TIMEOUT;\r
244}\r
245\r
246/**\r
247 Get BurstCount by reading the burstCount field of a TIS regiger \r
248 in the time of default TIS_TIMEOUT_D.\r
249\r
250 @param[in] TisReg Pointer to TIS register.\r
251 @param[out] BurstCount Pointer to a buffer to store the got BurstConut.\r
252\r
253 @retval EFI_SUCCESS Get BurstCount.\r
254 @retval EFI_INVALID_PARAMETER TisReg is NULL or BurstCount is NULL.\r
255 @retval EFI_TIMEOUT BurstCount can't be got in time.\r
256**/\r
257EFI_STATUS\r
258EFIAPI\r
259TisPcReadBurstCount (\r
260 IN TIS_PC_REGISTERS_PTR TisReg,\r
261 OUT UINT16 *BurstCount\r
262 )\r
263{\r
264 UINT32 WaitTime;\r
265 UINT8 DataByte0;\r
266 UINT8 DataByte1;\r
267\r
268 if (BurstCount == NULL || TisReg == NULL) {\r
269 return EFI_INVALID_PARAMETER;\r
270 }\r
271\r
272 WaitTime = 0;\r
273 do {\r
274 //\r
275 // TIS_PC_REGISTERS_PTR->burstCount is UINT16, but it is not 2bytes aligned,\r
276 // so it needs to use MmioRead8 to read two times\r
277 //\r
278 DataByte0 = MmioRead8 ((UINTN)&TisReg->BurstCount);\r
279 DataByte1 = MmioRead8 ((UINTN)&TisReg->BurstCount + 1);\r
280 *BurstCount = (UINT16)((DataByte1 << 8) + DataByte0);\r
281 if (*BurstCount != 0) {\r
282 return EFI_SUCCESS;\r
283 }\r
284 MicroSecondDelay (30);\r
285 WaitTime += 30;\r
286 } while (WaitTime < TIS_TIMEOUT_D);\r
287\r
288 return EFI_TIMEOUT;\r
289}\r
290\r
291/**\r
292 Set TPM chip to ready state by sending ready command TIS_PC_STS_READY \r
293 to Status Register in time.\r
294\r
295 @param[in] TisReg Pointer to TIS register.\r
296\r
297 @retval EFI_SUCCESS TPM chip enters into ready state.\r
298 @retval EFI_INVALID_PARAMETER TisReg is NULL.\r
299 @retval EFI_TIMEOUT TPM chip can't be set to ready state in time.\r
300**/\r
301EFI_STATUS\r
302EFIAPI\r
303TisPcPrepareCommand (\r
304 IN TIS_PC_REGISTERS_PTR TisReg\r
305 )\r
306{\r
307 EFI_STATUS Status;\r
308\r
309 if (TisReg == NULL) {\r
310 return EFI_INVALID_PARAMETER;\r
311 }\r
312\r
313 MmioWrite8((UINTN)&TisReg->Status, TIS_PC_STS_READY);\r
314 Status = TisPcWaitRegisterBits (\r
315 &TisReg->Status,\r
316 TIS_PC_STS_READY,\r
317 0,\r
318 TIS_TIMEOUT_B\r
319 );\r
320 return Status;\r
321}\r
322\r
323/**\r
324 Get the control of TPM chip by sending requestUse command TIS_PC_ACC_RQUUSE \r
325 to ACCESS Register in the time of default TIS_TIMEOUT_A.\r
326\r
327 @param[in] TisReg Pointer to TIS register.\r
328\r
329 @retval EFI_SUCCESS Get the control of TPM chip.\r
330 @retval EFI_INVALID_PARAMETER TisReg is NULL.\r
331 @retval EFI_NOT_FOUND TPM chip doesn't exit.\r
332 @retval EFI_TIMEOUT Can't get the TPM control in time.\r
333**/\r
334EFI_STATUS\r
335EFIAPI\r
336TisPcRequestUseTpm (\r
337 IN TIS_PC_REGISTERS_PTR TisReg\r
338 )\r
339{\r
340 EFI_STATUS Status;\r
341 \r
342 if (TisReg == NULL) {\r
343 return EFI_INVALID_PARAMETER;\r
344 }\r
345 \r
346 if (!TisPcPresenceCheck (TisReg)) {\r
347 return EFI_NOT_FOUND;\r
348 }\r
349\r
350 MmioWrite8((UINTN)&TisReg->Access, TIS_PC_ACC_RQUUSE);\r
351 Status = TisPcWaitRegisterBits (\r
352 &TisReg->Access,\r
353 (UINT8)(TIS_PC_ACC_ACTIVE |TIS_PC_VALID),\r
354 0,\r
355 TIS_TIMEOUT_A\r
356 );\r
357 return Status;\r
358}\r
359\r
360/**\r
361 Send a command to TPM for execution and return response data.\r
362\r
363 @param[in] TisReg TPM register space base address. \r
364 @param[in] BufferIn Buffer for command data. \r
365 @param[in] SizeIn Size of command data. \r
366 @param[in, out] BufferOut Buffer for response data. \r
367 @param[in, out] SizeOut Size of response data. \r
368 \r
369 @retval EFI_SUCCESS Operation completed successfully.\r
370 @retval EFI_TIMEOUT The register can't run into the expected status in time.\r
371 @retval EFI_BUFFER_TOO_SMALL Response data buffer is too small.\r
372 @retval EFI_DEVICE_ERROR Unexpected device behavior.\r
373 @retval EFI_UNSUPPORTED Unsupported TPM version\r
374\r
375**/\r
376EFI_STATUS\r
377TisTpmCommand (\r
378 IN TIS_PC_REGISTERS_PTR TisReg,\r
379 IN UINT8 *BufferIn,\r
380 IN UINT32 SizeIn,\r
381 IN OUT UINT8 *BufferOut,\r
382 IN OUT UINT32 *SizeOut\r
383 )\r
384{\r
385 EFI_STATUS Status;\r
386 UINT16 BurstCount;\r
387 UINT32 Index;\r
388 UINT32 TpmOutSize;\r
389 UINT16 Data16;\r
390 UINT32 Data32;\r
391\r
392 DEBUG_CODE (\r
393 UINTN DebugSize;\r
394\r
395 DEBUG ((EFI_D_INFO, "TisTpmCommand Send - "));\r
396 if (SizeIn > 0x100) {\r
397 DebugSize = 0x40;\r
398 } else {\r
399 DebugSize = SizeIn;\r
400 }\r
401 for (Index = 0; Index < DebugSize; Index++) {\r
402 DEBUG ((EFI_D_INFO, "%02x ", BufferIn[Index]));\r
403 }\r
404 if (DebugSize != SizeIn) {\r
405 DEBUG ((EFI_D_INFO, "...... "));\r
406 for (Index = SizeIn - 0x20; Index < SizeIn; Index++) {\r
407 DEBUG ((EFI_D_INFO, "%02x ", BufferIn[Index]));\r
408 }\r
409 }\r
410 DEBUG ((EFI_D_INFO, "\n"));\r
411 );\r
412 TpmOutSize = 0;\r
413\r
414 Status = TisPcPrepareCommand (TisReg);\r
415 if (EFI_ERROR (Status)){\r
416 DEBUG ((DEBUG_ERROR, "Tpm is not ready for command!\n"));\r
417 return Status;\r
418 }\r
419 //\r
420 // Send the command data to Tpm\r
421 //\r
422 Index = 0;\r
423 while (Index < SizeIn) {\r
424 Status = TisPcReadBurstCount (TisReg, &BurstCount);\r
425 if (EFI_ERROR (Status)) {\r
426 Status = EFI_TIMEOUT;\r
427 goto Exit;\r
428 }\r
429 for (; BurstCount > 0 && Index < SizeIn; BurstCount--) {\r
430 MmioWrite8((UINTN)&TisReg->DataFifo, *(BufferIn + Index));\r
431 Index++;\r
432 }\r
433 }\r
434 //\r
435 // Check the Tpm status STS_EXPECT change from 1 to 0\r
436 //\r
437 Status = TisPcWaitRegisterBits (\r
438 &TisReg->Status,\r
439 (UINT8) TIS_PC_VALID,\r
440 TIS_PC_STS_EXPECT,\r
441 TIS_TIMEOUT_C\r
442 );\r
443 if (EFI_ERROR (Status)) {\r
444 DEBUG ((DEBUG_ERROR, "The send buffer too small!\n"));\r
445 Status = EFI_BUFFER_TOO_SMALL;\r
446 goto Exit;\r
447 }\r
448 //\r
449 // Executed the TPM command and waiting for the response data ready\r
450 //\r
451 MmioWrite8((UINTN)&TisReg->Status, TIS_PC_STS_GO);\r
452\r
453 //\r
454 // NOTE: That may take many seconds to minutes for certain commands, such as key generation.\r
455 //\r
456 Status = TisPcWaitRegisterBits (\r
457 &TisReg->Status,\r
458 (UINT8) (TIS_PC_VALID | TIS_PC_STS_DATA),\r
459 0,\r
460 TIS_TIMEOUT_MAX\r
461 );\r
462 if (EFI_ERROR (Status)) {\r
463 DEBUG ((DEBUG_ERROR, "Wait for Tpm response data time out!!\n"));\r
464 Status = EFI_TIMEOUT;\r
465 goto Exit;\r
466 }\r
467 //\r
468 // Get response data header\r
469 //\r
470 Index = 0;\r
471 BurstCount = 0;\r
472 while (Index < sizeof (TPM2_RESPONSE_HEADER)) {\r
473 Status = TisPcReadBurstCount (TisReg, &BurstCount);\r
474 if (EFI_ERROR (Status)) {\r
475 Status = EFI_TIMEOUT;\r
476 goto Exit;\r
477 }\r
478 for (; BurstCount > 0; BurstCount--) {\r
479 *(BufferOut + Index) = MmioRead8 ((UINTN)&TisReg->DataFifo);\r
480 Index++;\r
481 if (Index == sizeof (TPM2_RESPONSE_HEADER)) break;\r
482 }\r
483 }\r
484 DEBUG_CODE (\r
485 DEBUG ((EFI_D_INFO, "TisTpmCommand ReceiveHeader - "));\r
486 for (Index = 0; Index < sizeof (TPM2_RESPONSE_HEADER); Index++) {\r
487 DEBUG ((EFI_D_INFO, "%02x ", BufferOut[Index]));\r
488 }\r
489 DEBUG ((EFI_D_INFO, "\n"));\r
490 );\r
491 //\r
492 // Check the reponse data header (tag,parasize and returncode )\r
493 //\r
494 CopyMem (&Data16, BufferOut, sizeof (UINT16));\r
495 // TPM2 should not use this RSP_COMMAND\r
496 if (SwapBytes16 (Data16) == TPM_ST_RSP_COMMAND) {\r
497 DEBUG ((EFI_D_ERROR, "TPM_ST_RSP error - %x\n", TPM_ST_RSP_COMMAND));\r
498 Status = EFI_UNSUPPORTED;\r
499 goto Exit;\r
500 }\r
501\r
502 CopyMem (&Data32, (BufferOut + 2), sizeof (UINT32));\r
503 TpmOutSize = SwapBytes32 (Data32);\r
504 if (*SizeOut < TpmOutSize) {\r
505 Status = EFI_BUFFER_TOO_SMALL;\r
506 goto Exit;\r
507 }\r
508 *SizeOut = TpmOutSize;\r
509 //\r
510 // Continue reading the remaining data\r
511 //\r
512 while ( Index < TpmOutSize ) {\r
513 for (; BurstCount > 0; BurstCount--) {\r
514 *(BufferOut + Index) = MmioRead8 ((UINTN)&TisReg->DataFifo);\r
515 Index++;\r
516 if (Index == TpmOutSize) {\r
517 Status = EFI_SUCCESS;\r
518 goto Exit;\r
519 }\r
520 }\r
521 Status = TisPcReadBurstCount (TisReg, &BurstCount);\r
522 if (EFI_ERROR (Status)) {\r
523 Status = EFI_TIMEOUT;\r
524 goto Exit;\r
525 }\r
526 }\r
527Exit:\r
528 DEBUG_CODE (\r
529 DEBUG ((EFI_D_INFO, "TisTpmCommand Receive - "));\r
530 for (Index = 0; Index < TpmOutSize; Index++) {\r
531 DEBUG ((EFI_D_INFO, "%02x ", BufferOut[Index]));\r
532 }\r
533 DEBUG ((EFI_D_INFO, "\n"));\r
534 );\r
535 MmioWrite8((UINTN)&TisReg->Status, TIS_PC_STS_READY);\r
536 return Status;\r
537}\r
538\r
539/**\r
540 This service enables the sending of commands to the TPM2.\r
541\r
542 @param[in] InputParameterBlockSize Size of the TPM2 input parameter block.\r
543 @param[in] InputParameterBlock Pointer to the TPM2 input parameter block.\r
544 @param[in,out] OutputParameterBlockSize Size of the TPM2 output parameter block.\r
545 @param[in] OutputParameterBlock Pointer to the TPM2 output parameter block.\r
546\r
547 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.\r
548 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.\r
549 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small. \r
550**/\r
551EFI_STATUS\r
552EFIAPI\r
553DTpm2SubmitCommand (\r
554 IN UINT32 InputParameterBlockSize,\r
555 IN UINT8 *InputParameterBlock,\r
556 IN OUT UINT32 *OutputParameterBlockSize,\r
557 IN UINT8 *OutputParameterBlock\r
558 )\r
559{\r
560 return TisTpmCommand (\r
561 (TIS_PC_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress),\r
562 InputParameterBlock,\r
563 InputParameterBlockSize,\r
564 OutputParameterBlock,\r
565 OutputParameterBlockSize\r
566 );\r
567}\r
568\r
569/**\r
570 This service requests use TPM2.\r
571\r
572 @retval EFI_SUCCESS Get the control of TPM2 chip.\r
573 @retval EFI_NOT_FOUND TPM2 not found.\r
574 @retval EFI_DEVICE_ERROR Unexpected device behavior.\r
575**/\r
576EFI_STATUS\r
577EFIAPI\r
578DTpm2RequestUseTpm (\r
579 VOID\r
580 )\r
581{\r
582 return TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress));\r
583}\r