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