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