]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/Tpm2DeviceLibRouter/Tpm2DeviceLibRouterPei.c
SecurityPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / SecurityPkg / Library / Tpm2DeviceLibRouter / Tpm2DeviceLibRouterPei.c
1 /** @file
2 This 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 - 2018, Intel Corporation. All rights reserved. <BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <Library/BaseLib.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/HobLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/PcdLib.h>
16 #include <Library/Tpm2DeviceLib.h>
17
18 EFI_GUID mInternalTpm2DeviceInterfaceGuid = {
19 0x349cf818, 0xc0ba, 0x4c43, { 0x92, 0x9a, 0xc8, 0xa1, 0xb1, 0xb3, 0xd2, 0x55 }
20 };
21
22 /**
23 This function get TPM2.0 interface.
24
25 @retval TPM2.0 interface.
26 **/
27 TPM2_DEVICE_INTERFACE *
28 InternalGetTpm2DeviceInterface (
29 VOID
30 )
31 {
32 EFI_HOB_GUID_TYPE *Hob;
33
34 Hob = GetFirstGuidHob (&mInternalTpm2DeviceInterfaceGuid);
35 if (Hob == NULL) {
36 return NULL;
37 }
38 return (TPM2_DEVICE_INTERFACE *)(Hob + 1);
39 }
40
41 /**
42 This service enables the sending of commands to the TPM2.
43
44 @param[in] InputParameterBlockSize Size of the TPM2 input parameter block.
45 @param[in] InputParameterBlock Pointer to the TPM2 input parameter block.
46 @param[in,out] OutputParameterBlockSize Size of the TPM2 output parameter block.
47 @param[in] OutputParameterBlock Pointer to the TPM2 output parameter block.
48
49 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
50 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
51 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
52 **/
53 EFI_STATUS
54 EFIAPI
55 Tpm2SubmitCommand (
56 IN UINT32 InputParameterBlockSize,
57 IN UINT8 *InputParameterBlock,
58 IN OUT UINT32 *OutputParameterBlockSize,
59 IN UINT8 *OutputParameterBlock
60 )
61 {
62 TPM2_DEVICE_INTERFACE *Tpm2DeviceInterface;
63
64 Tpm2DeviceInterface = InternalGetTpm2DeviceInterface ();
65 if (Tpm2DeviceInterface == NULL) {
66 return EFI_UNSUPPORTED;
67 }
68
69 return Tpm2DeviceInterface->Tpm2SubmitCommand (
70 InputParameterBlockSize,
71 InputParameterBlock,
72 OutputParameterBlockSize,
73 OutputParameterBlock
74 );
75 }
76
77 /**
78 This service requests use TPM2.
79
80 @retval EFI_SUCCESS Get the control of TPM2 chip.
81 @retval EFI_NOT_FOUND TPM2 not found.
82 @retval EFI_DEVICE_ERROR Unexpected device behavior.
83 **/
84 EFI_STATUS
85 EFIAPI
86 Tpm2RequestUseTpm (
87 VOID
88 )
89 {
90 TPM2_DEVICE_INTERFACE *Tpm2DeviceInterface;
91
92 Tpm2DeviceInterface = InternalGetTpm2DeviceInterface ();
93 if (Tpm2DeviceInterface == NULL) {
94 return EFI_UNSUPPORTED;
95 }
96 return Tpm2DeviceInterface->Tpm2RequestUseTpm ();
97 }
98
99 /**
100 This service register TPM2 device.
101
102 @param Tpm2Device TPM2 device
103
104 @retval EFI_SUCCESS This TPM2 device is registered successfully.
105 @retval EFI_UNSUPPORTED System does not support register this TPM2 device.
106 @retval EFI_ALREADY_STARTED System already register this TPM2 device.
107 **/
108 EFI_STATUS
109 EFIAPI
110 Tpm2RegisterTpm2DeviceLib (
111 IN TPM2_DEVICE_INTERFACE *Tpm2Device
112 )
113 {
114 TPM2_DEVICE_INTERFACE *Tpm2DeviceInterface;
115
116 if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &Tpm2Device->ProviderGuid)){
117 DEBUG ((DEBUG_WARN, "WARNING: Tpm2RegisterTpm2DeviceLib - does not support %g registration\n", &Tpm2Device->ProviderGuid));
118 return EFI_UNSUPPORTED;
119 }
120
121 Tpm2DeviceInterface = InternalGetTpm2DeviceInterface ();
122 if (Tpm2DeviceInterface != NULL) {
123 //
124 // In PEI phase, there will be shadow driver dispatched again.
125 //
126 DEBUG ((EFI_D_INFO, "Tpm2RegisterTpm2DeviceLib - Override\n"));
127 CopyMem (Tpm2DeviceInterface, Tpm2Device, sizeof(*Tpm2Device));
128 return EFI_SUCCESS;
129 } else {
130 Tpm2Device = BuildGuidDataHob (&mInternalTpm2DeviceInterfaceGuid, Tpm2Device, sizeof(*Tpm2Device));
131 if (Tpm2Device != NULL) {
132 return EFI_SUCCESS;
133 } else {
134 return EFI_OUT_OF_RESOURCES;
135 }
136 }
137 }