]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/Tpm2DeviceLibRouter/Tpm2DeviceLibRouterPei.c
SecurityPkg: Apply uncrustify changes
[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
39 return (TPM2_DEVICE_INTERFACE *)(Hob + 1);
40 }
41
42 /**
43 This service enables the sending of commands to the TPM2.
44
45 @param[in] InputParameterBlockSize Size of the TPM2 input parameter block.
46 @param[in] InputParameterBlock Pointer to the TPM2 input parameter block.
47 @param[in,out] OutputParameterBlockSize Size of the TPM2 output parameter block.
48 @param[in] OutputParameterBlock Pointer to the TPM2 output parameter block.
49
50 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
51 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
52 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
53 **/
54 EFI_STATUS
55 EFIAPI
56 Tpm2SubmitCommand (
57 IN UINT32 InputParameterBlockSize,
58 IN UINT8 *InputParameterBlock,
59 IN OUT UINT32 *OutputParameterBlockSize,
60 IN UINT8 *OutputParameterBlock
61 )
62 {
63 TPM2_DEVICE_INTERFACE *Tpm2DeviceInterface;
64
65 Tpm2DeviceInterface = InternalGetTpm2DeviceInterface ();
66 if (Tpm2DeviceInterface == NULL) {
67 return EFI_UNSUPPORTED;
68 }
69
70 return Tpm2DeviceInterface->Tpm2SubmitCommand (
71 InputParameterBlockSize,
72 InputParameterBlock,
73 OutputParameterBlockSize,
74 OutputParameterBlock
75 );
76 }
77
78 /**
79 This service requests use TPM2.
80
81 @retval EFI_SUCCESS Get the control of TPM2 chip.
82 @retval EFI_NOT_FOUND TPM2 not found.
83 @retval EFI_DEVICE_ERROR Unexpected device behavior.
84 **/
85 EFI_STATUS
86 EFIAPI
87 Tpm2RequestUseTpm (
88 VOID
89 )
90 {
91 TPM2_DEVICE_INTERFACE *Tpm2DeviceInterface;
92
93 Tpm2DeviceInterface = InternalGetTpm2DeviceInterface ();
94 if (Tpm2DeviceInterface == NULL) {
95 return EFI_UNSUPPORTED;
96 }
97
98 return Tpm2DeviceInterface->Tpm2RequestUseTpm ();
99 }
100
101 /**
102 This service register TPM2 device.
103
104 @param Tpm2Device TPM2 device
105
106 @retval EFI_SUCCESS This TPM2 device is registered successfully.
107 @retval EFI_UNSUPPORTED System does not support register this TPM2 device.
108 @retval EFI_ALREADY_STARTED System already register this TPM2 device.
109 **/
110 EFI_STATUS
111 EFIAPI
112 Tpm2RegisterTpm2DeviceLib (
113 IN TPM2_DEVICE_INTERFACE *Tpm2Device
114 )
115 {
116 TPM2_DEVICE_INTERFACE *Tpm2DeviceInterface;
117
118 if (!CompareGuid (PcdGetPtr (PcdTpmInstanceGuid), &Tpm2Device->ProviderGuid)) {
119 DEBUG ((DEBUG_WARN, "WARNING: Tpm2RegisterTpm2DeviceLib - does not support %g registration\n", &Tpm2Device->ProviderGuid));
120 return EFI_UNSUPPORTED;
121 }
122
123 Tpm2DeviceInterface = InternalGetTpm2DeviceInterface ();
124 if (Tpm2DeviceInterface != NULL) {
125 //
126 // In PEI phase, there will be shadow driver dispatched again.
127 //
128 DEBUG ((DEBUG_INFO, "Tpm2RegisterTpm2DeviceLib - Override\n"));
129 CopyMem (Tpm2DeviceInterface, Tpm2Device, sizeof (*Tpm2Device));
130 return EFI_SUCCESS;
131 } else {
132 Tpm2Device = BuildGuidDataHob (&mInternalTpm2DeviceInterfaceGuid, Tpm2Device, sizeof (*Tpm2Device));
133 if (Tpm2Device != NULL) {
134 return EFI_SUCCESS;
135 } else {
136 return EFI_OUT_OF_RESOURCES;
137 }
138 }
139 }