]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/Tpm2DeviceLibTcg2/Tpm2DeviceLibTcg2.c
SecurityPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / SecurityPkg / Library / Tpm2DeviceLibTcg2 / Tpm2DeviceLibTcg2.c
1 /** @file
2 This library is TPM2 TCG2 protocol lib.
3
4 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Library/BaseLib.h>
10 #include <Library/BaseMemoryLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/UefiBootServicesTableLib.h>
13 #include <Library/Tpm2DeviceLib.h>
14 #include <Protocol/Tcg2Protocol.h>
15 #include <IndustryStandard/Tpm20.h>
16
17 EFI_TCG2_PROTOCOL *mTcg2Protocol = NULL;
18
19 /**
20 This service enables the sending of commands to the TPM2.
21
22 @param[in] InputParameterBlockSize Size of the TPM2 input parameter block.
23 @param[in] InputParameterBlock Pointer to the TPM2 input parameter block.
24 @param[in,out] OutputParameterBlockSize Size of the TPM2 output parameter block.
25 @param[in] OutputParameterBlock Pointer to the TPM2 output parameter block.
26
27 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
28 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
29 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
30 **/
31 EFI_STATUS
32 EFIAPI
33 Tpm2SubmitCommand (
34 IN UINT32 InputParameterBlockSize,
35 IN UINT8 *InputParameterBlock,
36 IN OUT UINT32 *OutputParameterBlockSize,
37 IN UINT8 *OutputParameterBlock
38 )
39 {
40 EFI_STATUS Status;
41 TPM2_RESPONSE_HEADER *Header;
42
43 if (mTcg2Protocol == NULL) {
44 Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **) &mTcg2Protocol);
45 if (EFI_ERROR (Status)) {
46 //
47 // Tcg2 protocol is not installed. So, TPM2 is not present.
48 //
49 DEBUG ((EFI_D_ERROR, "Tpm2SubmitCommand - Tcg2 - %r\n", Status));
50 return EFI_NOT_FOUND;
51 }
52 }
53 //
54 // Assume when Tcg2 Protocol is ready, RequestUseTpm already done.
55 //
56 Status = mTcg2Protocol->SubmitCommand (
57 mTcg2Protocol,
58 InputParameterBlockSize,
59 InputParameterBlock,
60 *OutputParameterBlockSize,
61 OutputParameterBlock
62 );
63 if (EFI_ERROR (Status)) {
64 return Status;
65 }
66 Header = (TPM2_RESPONSE_HEADER *)OutputParameterBlock;
67 *OutputParameterBlockSize = SwapBytes32 (Header->paramSize);
68
69 return EFI_SUCCESS;
70 }
71
72 /**
73 This service requests use TPM2.
74
75 @retval EFI_SUCCESS Get the control of TPM2 chip.
76 @retval EFI_NOT_FOUND TPM2 not found.
77 @retval EFI_DEVICE_ERROR Unexpected device behavior.
78 **/
79 EFI_STATUS
80 EFIAPI
81 Tpm2RequestUseTpm (
82 VOID
83 )
84 {
85 EFI_STATUS Status;
86
87 if (mTcg2Protocol == NULL) {
88 Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **) &mTcg2Protocol);
89 if (EFI_ERROR (Status)) {
90 //
91 // Tcg2 protocol is not installed. So, TPM2 is not present.
92 //
93 DEBUG ((EFI_D_ERROR, "Tpm2RequestUseTpm - Tcg2 - %r\n", Status));
94 return EFI_NOT_FOUND;
95 }
96 }
97 //
98 // Assume when Tcg2 Protocol is ready, RequestUseTpm already done.
99 //
100 return EFI_SUCCESS;
101 }
102
103 /**
104 This service register TPM2 device.
105
106 @param Tpm2Device TPM2 device
107
108 @retval EFI_SUCCESS This TPM2 device is registered successfully.
109 @retval EFI_UNSUPPORTED System does not support register this TPM2 device.
110 @retval EFI_ALREADY_STARTED System already register this TPM2 device.
111 **/
112 EFI_STATUS
113 EFIAPI
114 Tpm2RegisterTpm2DeviceLib (
115 IN TPM2_DEVICE_INTERFACE *Tpm2Device
116 )
117 {
118 return EFI_UNSUPPORTED;
119 }