1 /** @file
2   TIS (TPM Interface Specification) functions used by dTPM2.0 library.
3 
4 Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution.  The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10 
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 #include <IndustryStandard/Tpm20.h>
17 
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/TimerLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/Tpm2DeviceLib.h>
24 #include <Library/PcdLib.h>
25 
26 #include <IndustryStandard/TpmTis.h>
27 
28 #define TIS_TIMEOUT_MAX             (90000 * 1000)  // 90s
29 
30 //
31 // Max TPM command/reponse length
32 //
33 #define TPMCMDBUFLENGTH             0x500
34 
35 /**
36   Check whether TPM chip exist.
37 
38   @param[in] TisReg  Pointer to TIS register.
39 
40   @retval    TRUE    TPM chip exists.
41   @retval    FALSE   TPM chip is not found.
42 **/
43 BOOLEAN
TisPcPresenceCheck(IN TIS_PC_REGISTERS_PTR TisReg)44 TisPcPresenceCheck (
45   IN      TIS_PC_REGISTERS_PTR      TisReg
46   )
47 {
48   UINT8                             RegRead;
49 
50   RegRead = MmioRead8 ((UINTN)&TisReg->Access);
51   return (BOOLEAN)(RegRead != (UINT8)-1);
52 }
53 
54 /**
55   Check whether the value of a TPM chip register satisfies the input BIT setting.
56 
57   @param[in]  Register     Address port of register to be checked.
58   @param[in]  BitSet       Check these data bits are set.
59   @param[in]  BitClear     Check these data bits are clear.
60   @param[in]  TimeOut      The max wait time (unit MicroSecond) when checking register.
61 
62   @retval     EFI_SUCCESS  The register satisfies the check bit.
63   @retval     EFI_TIMEOUT  The register can't run into the expected status in time.
64 **/
65 EFI_STATUS
TisPcWaitRegisterBits(IN UINT8 * Register,IN UINT8 BitSet,IN UINT8 BitClear,IN UINT32 TimeOut)66 TisPcWaitRegisterBits (
67   IN      UINT8                     *Register,
68   IN      UINT8                     BitSet,
69   IN      UINT8                     BitClear,
70   IN      UINT32                    TimeOut
71   )
72 {
73   UINT8                             RegRead;
74   UINT32                            WaitTime;
75 
76   for (WaitTime = 0; WaitTime < TimeOut; WaitTime += 30){
77     RegRead = MmioRead8 ((UINTN)Register);
78     if ((RegRead & BitSet) == BitSet && (RegRead & BitClear) == 0)
79       return EFI_SUCCESS;
80     MicroSecondDelay (30);
81   }
82   return EFI_TIMEOUT;
83 }
84 
85 /**
86   Get BurstCount by reading the burstCount field of a TIS regiger
87   in the time of default TIS_TIMEOUT_D.
88 
89   @param[in]  TisReg                Pointer to TIS register.
90   @param[out] BurstCount            Pointer to a buffer to store the got BurstConut.
91 
92   @retval     EFI_SUCCESS           Get BurstCount.
93   @retval     EFI_INVALID_PARAMETER TisReg is NULL or BurstCount is NULL.
94   @retval     EFI_TIMEOUT           BurstCount can't be got in time.
95 **/
96 EFI_STATUS
TisPcReadBurstCount(IN TIS_PC_REGISTERS_PTR TisReg,OUT UINT16 * BurstCount)97 TisPcReadBurstCount (
98   IN      TIS_PC_REGISTERS_PTR      TisReg,
99      OUT  UINT16                    *BurstCount
100   )
101 {
102   UINT32                            WaitTime;
103   UINT8                             DataByte0;
104   UINT8                             DataByte1;
105 
106   if (BurstCount == NULL || TisReg == NULL) {
107     return EFI_INVALID_PARAMETER;
108   }
109 
110   WaitTime = 0;
111   do {
112     //
113     // TIS_PC_REGISTERS_PTR->burstCount is UINT16, but it is not 2bytes aligned,
114     // so it needs to use MmioRead8 to read two times
115     //
116     DataByte0   = MmioRead8 ((UINTN)&TisReg->BurstCount);
117     DataByte1   = MmioRead8 ((UINTN)&TisReg->BurstCount + 1);
118     *BurstCount = (UINT16)((DataByte1 << 8) + DataByte0);
119     if (*BurstCount != 0) {
120       return EFI_SUCCESS;
121     }
122     MicroSecondDelay (30);
123     WaitTime += 30;
124   } while (WaitTime < TIS_TIMEOUT_D);
125 
126   return EFI_TIMEOUT;
127 }
128 
129 /**
130   Set TPM chip to ready state by sending ready command TIS_PC_STS_READY
131   to Status Register in time.
132 
133   @param[in] TisReg                Pointer to TIS register.
134 
135   @retval    EFI_SUCCESS           TPM chip enters into ready state.
136   @retval    EFI_INVALID_PARAMETER TisReg is NULL.
137   @retval    EFI_TIMEOUT           TPM chip can't be set to ready state in time.
138 **/
139 EFI_STATUS
TisPcPrepareCommand(IN TIS_PC_REGISTERS_PTR TisReg)140 TisPcPrepareCommand (
141   IN      TIS_PC_REGISTERS_PTR      TisReg
142   )
143 {
144   EFI_STATUS                        Status;
145 
146   if (TisReg == NULL) {
147     return EFI_INVALID_PARAMETER;
148   }
149 
150   MmioWrite8((UINTN)&TisReg->Status, TIS_PC_STS_READY);
151   Status = TisPcWaitRegisterBits (
152              &TisReg->Status,
153              TIS_PC_STS_READY,
154              0,
155              TIS_TIMEOUT_B
156              );
157   return Status;
158 }
159 
160 /**
161   Get the control of TPM chip by sending requestUse command TIS_PC_ACC_RQUUSE
162   to ACCESS Register in the time of default TIS_TIMEOUT_A.
163 
164   @param[in] TisReg                Pointer to TIS register.
165 
166   @retval    EFI_SUCCESS           Get the control of TPM chip.
167   @retval    EFI_INVALID_PARAMETER TisReg is NULL.
168   @retval    EFI_NOT_FOUND         TPM chip doesn't exit.
169   @retval    EFI_TIMEOUT           Can't get the TPM control in time.
170 **/
171 EFI_STATUS
TisPcRequestUseTpm(IN TIS_PC_REGISTERS_PTR TisReg)172 TisPcRequestUseTpm (
173   IN      TIS_PC_REGISTERS_PTR      TisReg
174   )
175 {
176   EFI_STATUS                        Status;
177 
178   if (TisReg == NULL) {
179     return EFI_INVALID_PARAMETER;
180   }
181 
182   if (!TisPcPresenceCheck (TisReg)) {
183     return EFI_NOT_FOUND;
184   }
185 
186   MmioWrite8((UINTN)&TisReg->Access, TIS_PC_ACC_RQUUSE);
187   Status = TisPcWaitRegisterBits (
188              &TisReg->Access,
189              (UINT8)(TIS_PC_ACC_ACTIVE |TIS_PC_VALID),
190              0,
191              TIS_TIMEOUT_A
192              );
193   return Status;
194 }
195 
196 /**
197   Send a command to TPM for execution and return response data.
198 
199   @param[in]      TisReg        TPM register space base address.
200   @param[in]      BufferIn      Buffer for command data.
201   @param[in]      SizeIn        Size of command data.
202   @param[in, out] BufferOut     Buffer for response data.
203   @param[in, out] SizeOut       Size of response data.
204 
205   @retval EFI_SUCCESS           Operation completed successfully.
206   @retval EFI_BUFFER_TOO_SMALL  Response data buffer is too small.
207   @retval EFI_DEVICE_ERROR      Unexpected device behavior.
208   @retval EFI_UNSUPPORTED       Unsupported TPM version
209 
210 **/
211 EFI_STATUS
Tpm2TisTpmCommand(IN TIS_PC_REGISTERS_PTR TisReg,IN UINT8 * BufferIn,IN UINT32 SizeIn,IN OUT UINT8 * BufferOut,IN OUT UINT32 * SizeOut)212 Tpm2TisTpmCommand (
213   IN     TIS_PC_REGISTERS_PTR       TisReg,
214   IN     UINT8                      *BufferIn,
215   IN     UINT32                     SizeIn,
216   IN OUT UINT8                      *BufferOut,
217   IN OUT UINT32                     *SizeOut
218   )
219 {
220   EFI_STATUS                        Status;
221   UINT16                            BurstCount;
222   UINT32                            Index;
223   UINT32                            TpmOutSize;
224   UINT16                            Data16;
225   UINT32                            Data32;
226 
227   DEBUG_CODE (
228     UINTN  DebugSize;
229 
230     DEBUG ((EFI_D_VERBOSE, "Tpm2TisTpmCommand Send - "));
231     if (SizeIn > 0x100) {
232       DebugSize = 0x40;
233     } else {
234       DebugSize = SizeIn;
235     }
236     for (Index = 0; Index < DebugSize; Index++) {
237       DEBUG ((EFI_D_VERBOSE, "%02x ", BufferIn[Index]));
238     }
239     if (DebugSize != SizeIn) {
240       DEBUG ((EFI_D_VERBOSE, "...... "));
241       for (Index = SizeIn - 0x20; Index < SizeIn; Index++) {
242         DEBUG ((EFI_D_VERBOSE, "%02x ", BufferIn[Index]));
243       }
244     }
245     DEBUG ((EFI_D_VERBOSE, "\n"));
246   );
247   TpmOutSize = 0;
248 
249   Status = TisPcPrepareCommand (TisReg);
250   if (EFI_ERROR (Status)){
251     DEBUG ((DEBUG_ERROR, "Tpm2 is not ready for command!\n"));
252     return EFI_DEVICE_ERROR;
253   }
254   //
255   // Send the command data to Tpm
256   //
257   Index = 0;
258   while (Index < SizeIn) {
259     Status = TisPcReadBurstCount (TisReg, &BurstCount);
260     if (EFI_ERROR (Status)) {
261       Status = EFI_DEVICE_ERROR;
262       goto Exit;
263     }
264     for (; BurstCount > 0 && Index < SizeIn; BurstCount--) {
265       MmioWrite8((UINTN)&TisReg->DataFifo, *(BufferIn + Index));
266       Index++;
267     }
268   }
269   //
270   // Check the Tpm status STS_EXPECT change from 1 to 0
271   //
272   Status = TisPcWaitRegisterBits (
273              &TisReg->Status,
274              (UINT8) TIS_PC_VALID,
275              TIS_PC_STS_EXPECT,
276              TIS_TIMEOUT_C
277              );
278   if (EFI_ERROR (Status)) {
279     DEBUG ((DEBUG_ERROR, "Tpm2 The send buffer too small!\n"));
280     Status = EFI_BUFFER_TOO_SMALL;
281     goto Exit;
282   }
283   //
284   // Executed the TPM command and waiting for the response data ready
285   //
286   MmioWrite8((UINTN)&TisReg->Status, TIS_PC_STS_GO);
287 
288   //
289   // NOTE: That may take many seconds to minutes for certain commands, such as key generation.
290   //
291   Status = TisPcWaitRegisterBits (
292              &TisReg->Status,
293              (UINT8) (TIS_PC_VALID | TIS_PC_STS_DATA),
294              0,
295              TIS_TIMEOUT_MAX
296              );
297   if (EFI_ERROR (Status)) {
298     DEBUG ((DEBUG_ERROR, "Wait for Tpm2 response data time out!!\n"));
299     Status = EFI_DEVICE_ERROR;
300     goto Exit;
301   }
302   //
303   // Get response data header
304   //
305   Index = 0;
306   BurstCount = 0;
307   while (Index < sizeof (TPM2_RESPONSE_HEADER)) {
308     Status = TisPcReadBurstCount (TisReg, &BurstCount);
309     if (EFI_ERROR (Status)) {
310       Status = EFI_DEVICE_ERROR;
311       goto Exit;
312     }
313     for (; BurstCount > 0; BurstCount--) {
314       *(BufferOut + Index) = MmioRead8 ((UINTN)&TisReg->DataFifo);
315       Index++;
316       if (Index == sizeof (TPM2_RESPONSE_HEADER)) break;
317     }
318   }
319   DEBUG_CODE (
320     DEBUG ((EFI_D_VERBOSE, "Tpm2TisTpmCommand ReceiveHeader - "));
321     for (Index = 0; Index < sizeof (TPM2_RESPONSE_HEADER); Index++) {
322       DEBUG ((EFI_D_VERBOSE, "%02x ", BufferOut[Index]));
323     }
324     DEBUG ((EFI_D_VERBOSE, "\n"));
325   );
326   //
327   // Check the reponse data header (tag,parasize and returncode )
328   //
329   CopyMem (&Data16, BufferOut, sizeof (UINT16));
330   // TPM2 should not use this RSP_COMMAND
331   if (SwapBytes16 (Data16) == TPM_ST_RSP_COMMAND) {
332     DEBUG ((EFI_D_ERROR, "TPM2: TPM_ST_RSP error - %x\n", TPM_ST_RSP_COMMAND));
333     Status = EFI_UNSUPPORTED;
334     goto Exit;
335   }
336 
337   CopyMem (&Data32, (BufferOut + 2), sizeof (UINT32));
338   TpmOutSize  = SwapBytes32 (Data32);
339   if (*SizeOut < TpmOutSize) {
340     Status = EFI_BUFFER_TOO_SMALL;
341     goto Exit;
342   }
343   *SizeOut = TpmOutSize;
344   //
345   // Continue reading the remaining data
346   //
347   while ( Index < TpmOutSize ) {
348     for (; BurstCount > 0; BurstCount--) {
349       *(BufferOut + Index) = MmioRead8 ((UINTN)&TisReg->DataFifo);
350       Index++;
351       if (Index == TpmOutSize) {
352         Status = EFI_SUCCESS;
353         goto Exit;
354       }
355     }
356     Status = TisPcReadBurstCount (TisReg, &BurstCount);
357     if (EFI_ERROR (Status)) {
358       Status = EFI_DEVICE_ERROR;
359       goto Exit;
360     }
361   }
362 Exit:
363   DEBUG_CODE (
364     DEBUG ((EFI_D_VERBOSE, "Tpm2TisTpmCommand Receive - "));
365     for (Index = 0; Index < TpmOutSize; Index++) {
366       DEBUG ((EFI_D_VERBOSE, "%02x ", BufferOut[Index]));
367     }
368     DEBUG ((EFI_D_VERBOSE, "\n"));
369   );
370   MmioWrite8((UINTN)&TisReg->Status, TIS_PC_STS_READY);
371   return Status;
372 }
373 
374 /**
375   This service enables the sending of commands to the TPM2.
376 
377   @param[in]      InputParameterBlockSize  Size of the TPM2 input parameter block.
378   @param[in]      InputParameterBlock      Pointer to the TPM2 input parameter block.
379   @param[in,out]  OutputParameterBlockSize Size of the TPM2 output parameter block.
380   @param[in]      OutputParameterBlock     Pointer to the TPM2 output parameter block.
381 
382   @retval EFI_SUCCESS            The command byte stream was successfully sent to the device and a response was successfully received.
383   @retval EFI_DEVICE_ERROR       The command was not successfully sent to the device or a response was not successfully received from the device.
384   @retval EFI_BUFFER_TOO_SMALL   The output parameter block is too small.
385 **/
386 EFI_STATUS
387 EFIAPI
DTpm2TisSubmitCommand(IN UINT32 InputParameterBlockSize,IN UINT8 * InputParameterBlock,IN OUT UINT32 * OutputParameterBlockSize,IN UINT8 * OutputParameterBlock)388 DTpm2TisSubmitCommand (
389   IN UINT32            InputParameterBlockSize,
390   IN UINT8             *InputParameterBlock,
391   IN OUT UINT32        *OutputParameterBlockSize,
392   IN UINT8             *OutputParameterBlock
393   )
394 {
395   return Tpm2TisTpmCommand (
396            (TIS_PC_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress),
397            InputParameterBlock,
398            InputParameterBlockSize,
399            OutputParameterBlock,
400            OutputParameterBlockSize
401            );
402 }
403 
404 /**
405   This service requests use TPM2.
406 
407   @retval EFI_SUCCESS      Get the control of TPM2 chip.
408   @retval EFI_NOT_FOUND    TPM2 not found.
409   @retval EFI_DEVICE_ERROR Unexpected device behavior.
410 **/
411 EFI_STATUS
412 EFIAPI
DTpm2TisRequestUseTpm(VOID)413 DTpm2TisRequestUseTpm (
414   VOID
415   )
416 {
417   return TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress));
418 }
419