1 /** @file
2   Ihis library is TPM2 device router. Platform can register multi TPM2 instance to it
3   via PcdTpmInstanceGuid. Platform need make choice that which one will be final one.
4   At most one TPM2 instance can be finally registered, and other will return unsupported.
5 
6 Copyright (c) 2013, Intel Corporation. All rights reserved. <BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution.  The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11 
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 
15 **/
16 
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/Tpm2DeviceLib.h>
22 
23 TPM2_DEVICE_INTERFACE  mInternalTpm2DeviceInterface;
24 
25 /**
26   This service enables the sending of commands to the TPM2.
27 
28   @param[in]      InputParameterBlockSize  Size of the TPM2 input parameter block.
29   @param[in]      InputParameterBlock      Pointer to the TPM2 input parameter block.
30   @param[in,out]  OutputParameterBlockSize Size of the TPM2 output parameter block.
31   @param[in]      OutputParameterBlock     Pointer to the TPM2 output parameter block.
32 
33   @retval EFI_SUCCESS            The command byte stream was successfully sent to the device and a response was successfully received.
34   @retval EFI_DEVICE_ERROR       The command was not successfully sent to the device or a response was not successfully received from the device.
35   @retval EFI_BUFFER_TOO_SMALL   The output parameter block is too small.
36 **/
37 EFI_STATUS
38 EFIAPI
Tpm2SubmitCommand(IN UINT32 InputParameterBlockSize,IN UINT8 * InputParameterBlock,IN OUT UINT32 * OutputParameterBlockSize,IN UINT8 * OutputParameterBlock)39 Tpm2SubmitCommand (
40   IN UINT32            InputParameterBlockSize,
41   IN UINT8             *InputParameterBlock,
42   IN OUT UINT32        *OutputParameterBlockSize,
43   IN UINT8             *OutputParameterBlock
44   )
45 {
46   if (mInternalTpm2DeviceInterface.Tpm2SubmitCommand == NULL) {
47     return EFI_UNSUPPORTED;
48   }
49   return mInternalTpm2DeviceInterface.Tpm2SubmitCommand (
50                                         InputParameterBlockSize,
51                                         InputParameterBlock,
52                                         OutputParameterBlockSize,
53                                         OutputParameterBlock
54                                         );
55 }
56 
57 /**
58   This service requests use TPM2.
59 
60   @retval EFI_SUCCESS      Get the control of TPM2 chip.
61   @retval EFI_NOT_FOUND    TPM2 not found.
62   @retval EFI_DEVICE_ERROR Unexpected device behavior.
63 **/
64 EFI_STATUS
65 EFIAPI
Tpm2RequestUseTpm(VOID)66 Tpm2RequestUseTpm (
67   VOID
68   )
69 {
70   if (mInternalTpm2DeviceInterface.Tpm2RequestUseTpm == NULL) {
71     return EFI_UNSUPPORTED;
72   }
73   return mInternalTpm2DeviceInterface.Tpm2RequestUseTpm ();
74 }
75 
76 /**
77   This service register TPM2 device.
78 
79   @param Tpm2Device  TPM2 device
80 
81   @retval EFI_SUCCESS          This TPM2 device is registered successfully.
82   @retval EFI_UNSUPPORTED      System does not support register this TPM2 device.
83   @retval EFI_ALREADY_STARTED  System already register this TPM2 device.
84 **/
85 EFI_STATUS
86 EFIAPI
Tpm2RegisterTpm2DeviceLib(IN TPM2_DEVICE_INTERFACE * Tpm2Device)87 Tpm2RegisterTpm2DeviceLib (
88   IN TPM2_DEVICE_INTERFACE   *Tpm2Device
89   )
90 {
91   if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &Tpm2Device->ProviderGuid)){
92     DEBUG ((EFI_D_ERROR, "WARNING: Tpm2RegisterTpm2DeviceLib - does not support %g registration\n", &Tpm2Device->ProviderGuid));
93     return EFI_UNSUPPORTED;
94   }
95 
96   CopyMem (&mInternalTpm2DeviceInterface, Tpm2Device, sizeof(mInternalTpm2DeviceInterface));
97   return EFI_SUCCESS;
98 }
99