]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Tcg/Tcg2Config/Tcg2ConfigPeim.c
OvmfPkg/Tcg2ConfigPei: clean up some lib class dependencies
[mirror_edk2.git] / OvmfPkg / Tcg / Tcg2Config / Tcg2ConfigPeim.c
CommitLineData
6cf1880f 1/** @file\r
1affa1c0 2 Set TPM device type\r
6cf1880f 3\r
1affa1c0
LE
4 In SecurityPkg, this module initializes the TPM device type based on a UEFI\r
5 variable and/or hardware detection. In OvmfPkg, the module only performs TPM2\r
6 hardware detection.\r
6cf1880f 7\r
1affa1c0
LE
8 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
9 Copyright (C) 2018, Red Hat, Inc.\r
6cf1880f 10\r
b26f0cf9 11 SPDX-License-Identifier: BSD-2-Clause-Patent\r
6cf1880f
MAL
12**/\r
13\r
14\r
15#include <PiPei.h>\r
16\r
17#include <Guid/TpmInstance.h>\r
08c77cce 18#include <Library/BaseLib.h>\r
6cf1880f
MAL
19#include <Library/DebugLib.h>\r
20#include <Library/PeiServicesLib.h>\r
21#include <Library/Tpm2DeviceLib.h>\r
89236992 22#include <Library/Tpm12DeviceLib.h>\r
6cf1880f
MAL
23#include <Ppi/TpmInitialized.h>\r
24\r
25STATIC CONST EFI_PEI_PPI_DESCRIPTOR mTpmSelectedPpi = {\r
26 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
27 &gEfiTpmDeviceSelectedGuid,\r
28 NULL\r
29};\r
30\r
31STATIC CONST EFI_PEI_PPI_DESCRIPTOR mTpmInitializationDonePpiList = {\r
32 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
33 &gPeiTpmInitializationDonePpiGuid,\r
34 NULL\r
35};\r
36\r
89236992
MAL
37#pragma pack (1)\r
38\r
39typedef struct {\r
40 TPM_RSP_COMMAND_HDR Hdr;\r
41 TPM_CURRENT_TICKS CurrentTicks;\r
42} TPM_RSP_GET_TICKS;\r
43\r
44#pragma pack ()\r
45\r
46/**\r
47 Probe for the TPM for 1.2 version, by sending TPM1.2 GetTicks\r
48\r
49 Sending a TPM1.2 command to a TPM2 should return a TPM1.2\r
50 header (tag = 0xc4) and error code (TPM_BADTAG = 0x1e)\r
51**/\r
52static\r
53EFI_STATUS\r
54TestTpm12 (\r
55 )\r
56{\r
57 EFI_STATUS Status;\r
58 TPM_RQU_COMMAND_HDR Command;\r
59 TPM_RSP_GET_TICKS Response;\r
60 UINT32 Length;\r
61\r
62 Command.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
63 Command.paramSize = SwapBytes32 (sizeof (Command));\r
64 Command.ordinal = SwapBytes32 (TPM_ORD_GetTicks);\r
65\r
66 Length = sizeof (Response);\r
67 Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r
68 if (EFI_ERROR (Status)) {\r
69 return Status;\r
70 }\r
71\r
72 return EFI_SUCCESS;\r
73}\r
74\r
6cf1880f
MAL
75/**\r
76 The entry point for Tcg2 configuration driver.\r
77\r
78 @param FileHandle Handle of the file being invoked.\r
79 @param PeiServices Describes the list of possible PEI Services.\r
80**/\r
81EFI_STATUS\r
82EFIAPI\r
83Tcg2ConfigPeimEntryPoint (\r
84 IN EFI_PEI_FILE_HANDLE FileHandle,\r
85 IN CONST EFI_PEI_SERVICES **PeiServices\r
86 )\r
87{\r
88 UINTN Size;\r
89 EFI_STATUS Status;\r
90\r
91 DEBUG ((DEBUG_INFO, "%a\n", __FUNCTION__));\r
92\r
89236992
MAL
93 Status = Tpm12RequestUseTpm ();\r
94 if (!EFI_ERROR (Status) && !EFI_ERROR (TestTpm12 ())) {\r
95 DEBUG ((DEBUG_INFO, "%a: TPM1.2 detected\n", __FUNCTION__));\r
96 Size = sizeof (gEfiTpmDeviceInstanceTpm12Guid);\r
1affa1c0
LE
97 Status = PcdSetPtrS (\r
98 PcdTpmInstanceGuid,\r
99 &Size,\r
89236992 100 &gEfiTpmDeviceInstanceTpm12Guid\r
1affa1c0
LE
101 );\r
102 ASSERT_EFI_ERROR (Status);\r
6cf1880f 103 } else {\r
89236992
MAL
104 Status = Tpm2RequestUseTpm ();\r
105 if (!EFI_ERROR (Status)) {\r
106 DEBUG ((DEBUG_INFO, "%a: TPM2 detected\n", __FUNCTION__));\r
107 Size = sizeof (gEfiTpmDeviceInstanceTpm20DtpmGuid);\r
108 Status = PcdSetPtrS (\r
109 PcdTpmInstanceGuid,\r
110 &Size,\r
111 &gEfiTpmDeviceInstanceTpm20DtpmGuid\r
112 );\r
113 ASSERT_EFI_ERROR (Status);\r
114 } else {\r
115 DEBUG ((DEBUG_INFO, "%a: no TPM detected\n", __FUNCTION__));\r
116 //\r
117 // If no TPM2 was detected, we still need to install\r
118 // TpmInitializationDonePpi. Namely, Tcg2Pei will exit early upon seeing\r
119 // the default (all-bits-zero) contents of PcdTpmInstanceGuid, thus we have\r
120 // to install the PPI in its place, in order to unblock any dependent\r
121 // PEIMs.\r
122 //\r
123 Status = PeiServicesInstallPpi (&mTpmInitializationDonePpiList);\r
124 ASSERT_EFI_ERROR (Status);\r
125 }\r
6cf1880f
MAL
126 }\r
127\r
128 //\r
129 // Selection done\r
130 //\r
131 Status = PeiServicesInstallPpi (&mTpmSelectedPpi);\r
132 ASSERT_EFI_ERROR (Status);\r
133\r
134 return Status;\r
135}\r