Skip to content

Commit c28d00e

Browse files
committed
OvmfPkg/TdxDxe: setup TPM device instance for vTPM in TdxDxe
The TPM device instance is not set before DXE in peiless boot. Move the work of setting the device instance and Tpm2HashMask PCDs to `TdxDxe` if the `MeasurementType` is vTPM. Signed-off-by: Jiaqi Gao <[email protected]>
1 parent 32f6d36 commit c28d00e

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

OvmfPkg/IntelTdx/IntelTdxX64.dsc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
TpmMeasurementLib|SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf
211211

212212
!include OvmfPkg/Include/Dsc/ShellLibs.dsc.inc
213+
!include OvmfPkg/Include/Dsc/OvmfTpmLibs.dsc.inc
213214

214215
[LibraryClasses.common]
215216
AmdSvsmLib|UefiCpuPkg/Library/AmdSvsmLibNull/AmdSvsmLibNull.inf
@@ -529,6 +530,8 @@
529530

530531
gEfiMdePkgTokenSpaceGuid.PcdFSBClock|1000000000
531532

533+
!include OvmfPkg/Include/Dsc/OvmfTpmPcds.dsc.inc
534+
532535
################################################################################
533536
#
534537
# Components Section - list of all EDK II Modules needed by this Platform.
@@ -727,7 +730,10 @@
727730
OvmfPkg/PlatformDxe/Platform.inf
728731
OvmfPkg/IoMmuDxe/IoMmuDxe.inf
729732

730-
OvmfPkg/TdxDxe/TdxDxe.inf
733+
OvmfPkg/TdxDxe/TdxDxe.inf {
734+
<LibraryClasses>
735+
Tpm2DeviceLib|SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.inf
736+
}
731737

732738
#
733739
# Variable driver stack (non-SMM)
@@ -751,3 +757,8 @@
751757
HashLib|OvmfPkg/Library/HashLibTdx/HashLibTdx.inf
752758
NULL|SecurityPkg/Library/HashInstanceLibSha384/HashInstanceLibSha384.inf
753759
}
760+
761+
#
762+
# TPM support
763+
#
764+
!include OvmfPkg/Include/Dsc/OvmfTpmComponentsDxe.dsc.inc

OvmfPkg/IntelTdx/IntelTdxX64.fdf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ INF MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
257257
#
258258
INF OvmfPkg/Tcg/TdTcg2Dxe/TdTcg2Dxe.inf
259259

260+
#
261+
# TPM support
262+
#
263+
!include OvmfPkg/Include/Fdf/OvmfTpmDxe.fdf.inc
264+
260265
################################################################################
261266

262267
[FV.NCCFV]

OvmfPkg/TdxDxe/TdxDxe.c

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#include <Library/TdxLib.h>
3333
#include <TdxAcpiTable.h>
3434
#include <Library/MemEncryptTdxLib.h>
35+
#include <WorkArea.h>
36+
#include <Library/Tpm2CommandLib.h>
37+
#include <Library/Tpm2DeviceLib.h>
3538

3639
#define ALIGNED_2MB_MASK 0x1fffff
3740
EFI_HANDLE mTdxDxeHandle = NULL;
@@ -301,6 +304,58 @@ SetMmioSharedBit (
301304
return EFI_SUCCESS;
302305
}
303306

307+
#ifdef TDX_PEI_LESS_BOOT
308+
STATIC
309+
EFI_STATUS
310+
SetVtpmDeviceInstance (
311+
VOID
312+
)
313+
{
314+
EFI_STATUS Status;
315+
OVMF_WORK_AREA *WorkArea;
316+
UINTN Size;
317+
UINT32 TpmHashAlgorithmBitmap;
318+
UINT32 TpmActivePcrBanks;
319+
320+
DEBUG ((DEBUG_INFO, ">>%a\n", __func__));
321+
322+
WorkArea = (OVMF_WORK_AREA *)FixedPcdGet32 (PcdOvmfWorkAreaBase);
323+
if (WorkArea == NULL) {
324+
return EFI_INVALID_PARAMETER;
325+
}
326+
327+
if (WorkArea->TdxWorkArea.SecTdxWorkArea.MeasurementType == TDX_MEASUREMENT_TYPE_VTPM)
328+
{
329+
// Set PcdTpmInstanceGuid
330+
Size = sizeof (gEfiTpmDeviceInstanceTpm20DtpmGuid);
331+
Status = PcdSetPtrS (
332+
PcdTpmInstanceGuid,
333+
&Size,
334+
&gEfiTpmDeviceInstanceTpm20DtpmGuid
335+
);
336+
ASSERT_EFI_ERROR (Status);
337+
if (EFI_ERROR(Status)) {
338+
DEBUG((DEBUG_ERROR, "Set PcdTpmInstanceGuid failed with %r\n", Status));
339+
}
340+
341+
Status = Tpm2RequestUseTpm ();
342+
if (EFI_ERROR (Status)) {
343+
DEBUG ((DEBUG_ERROR, "TPM2 not detected!\n"));
344+
return Status;
345+
}
346+
347+
// Determine the current TPM support and the Platform PCR mask.
348+
Status = Tpm2GetCapabilitySupportedAndActivePcrs (&TpmHashAlgorithmBitmap, &TpmActivePcrBanks);
349+
ASSERT_EFI_ERROR (Status);
350+
// Set active pcr banks
351+
Status = PcdSet32S (PcdTpm2HashMask, TpmActivePcrBanks);
352+
ASSERT_RETURN_ERROR (Status);
353+
}
354+
355+
return EFI_SUCCESS;
356+
}
357+
#endif
358+
304359
EFI_STATUS
305360
EFIAPI
306361
TdxDxeEntryPoint (
@@ -339,9 +394,12 @@ TdxDxeEntryPoint (
339394
// need to set PCDs based on these information.
340395
//
341396
SetPcdSettings (PlatformInfo);
397+
// In Pei-less boot, the `TpmInstance` Pcd shall be set if virtual TPM
398+
// is detected.
399+
SetVtpmDeviceInstance();
342400
#endif
343401

344-
if (!TdIsEnabled () || TdpIsEnabled ()) {
402+
if (!TdIsEnabled () || TdpIsEnabled ()) {
345403
//
346404
// If it is Non-Td guest, we install gEfiMpInitLibMpDepProtocolGuid so that
347405
// MpInitLib will be used in CpuDxe driver.

OvmfPkg/TdxDxe/TdxDxe.inf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
MdePkg/MdePkg.dec
2727
UefiCpuPkg/UefiCpuPkg.dec
2828
OvmfPkg/OvmfPkg.dec
29+
SecurityPkg/SecurityPkg.dec
2930

3031
[LibraryClasses]
3132
BaseLib
@@ -39,12 +40,14 @@
3940
HobLib
4041
TdxMailboxLib
4142
MemEncryptTdxLib
43+
Tpm2CommandLib
4244

4345
[Depex]
4446
TRUE
4547

4648
[Guids]
4749
gUefiOvmfPkgPlatformInfoGuid ## CONSUMES
50+
gEfiTpmDeviceInstanceTpm20DtpmGuid ## CONSUMES
4851

4952
[Protocols]
5053
gQemuAcpiTableNotifyProtocolGuid ## CONSUMES
@@ -71,3 +74,6 @@
7174
gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack
7275
gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved
7376
gUefiOvmfPkgTokenSpaceGuid.PcdTdxAcceptPageSize
77+
gUefiOvmfPkgTokenSpaceGuid.PcdOvmfWorkAreaBase
78+
gEfiSecurityPkgTokenSpaceGuid.PcdTpmInstanceGuid
79+
gEfiSecurityPkgTokenSpaceGuid.PcdTpm2HashMask

0 commit comments

Comments
 (0)