Skip to content

Commit

Permalink
OvmfPkg: Fix the failure of GCC Debug build
Browse files Browse the repository at this point in the history
  • Loading branch information
mxu9 committed Apr 19, 2021
1 parent 75d6d78 commit ba06323
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 218 deletions.
14 changes: 1 addition & 13 deletions MdePkg/Include/Protocol/Tdx.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,10 @@ SPDX-License-Identifier: BSD-2-Clause-Patent

#include <Uefi/UefiBaseType.h>

#define REG_TYPE_NA 0
#define REG_TYPE_MRTD 1
#define REG_TYPE_RTMR 2
#define PCR_COUNT 16

typedef struct {
UINT8 Pcr; // PCR index
UINT8 RegType; // RTMR or MRTD
UINT8 Index; // index in RTMR/MRTD
UINT8 EventlogIndex;// index in EventLog
} PCR_TDX_EXTEND_MAP;

#define EFI_TD_PROTOCOL_GUID \
{0x96751a3d, 0x72f4, 0x41a6, { 0xa7, 0x94, 0xed, 0x5d, 0x0e, 0x67, 0xae, 0x6b }}

extern EFI_GUID gTdTcg2ProtocolGuid;


#endif
#endif
18 changes: 18 additions & 0 deletions OvmfPkg/Include/Library/TdxStartupLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
#include <Pi/PiPeiCis.h>
#include <Library/DebugLib.h>
#include <Protocol/DebugSupport.h>
#include <IndustryStandard/Tpm20.h>

#pragma pack(1)

typedef struct {
UINT32 count;
TPMI_ALG_HASH hashAlg;
BYTE sha384[SHA384_DIGEST_SIZE];
} TDX_DIGEST_VALUE;

typedef struct {
UINT32 Signature;
UINT64 HashDataPtr;
UINT64 HashDataLen;
} TDX_EVENT;

#pragma pack()


typedef
VOID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#include <Library/TdxLib.h>
#include <Library/TdxProbeLib.h>
#include <Protocol/Tdx.h>
#include <Library/TdxStartupLib.h>
#include "HashLibBaseCryptoRouterCommon.h"

extern PCR_TDX_EXTEND_MAP mPcrTdxExtendMaps[PCR_COUNT];

HASH_INTERFACE mHashInterface[HASH_COUNT] = {{{0}, NULL, NULL, NULL}};
UINTN mHashInterfaceCount = 0;

Expand Down Expand Up @@ -127,6 +126,31 @@ HashUpdate (
return EFI_SUCCESS;
}

/**
MRTD => PCR[0]
RTMR[0] => PCR[1,7]
RTMR[1] => PCR[2,3,4,5,6]
RTMR[2] => PCR[8~15]
RTMR[3] => NA
**/
UINT8 GetMappedRtmrIndex(UINT32 PCRIndex)
{
UINT8 RtmrIndex;

ASSERT (PCRIndex <= 16 && PCRIndex >= 0);
RtmrIndex = 0;
if (PCRIndex == 1 || PCRIndex == 7) {
RtmrIndex = 0;
} else if (PCRIndex >= 2 && PCRIndex <= 6) {
RtmrIndex = 1;
} else if (PCRIndex >= 8 && PCRIndex <= 15) {
RtmrIndex = 2;
}

return RtmrIndex;
}

/**
Hash sequence complete and extend to PCR.
Expand All @@ -153,15 +177,11 @@ HashCompleteAndExtend (
UINTN Index;
EFI_STATUS Status;
UINT32 HashMask;
PCR_TDX_EXTEND_MAP PcrTdxExtendMap;

if (mHashInterfaceCount == 0) {
return EFI_UNSUPPORTED;
}

PcrTdxExtendMap = mPcrTdxExtendMaps[PcrIndex];
ASSERT(PcrTdxExtendMap.Pcr == PcrIndex);

CheckSupportedHashMaskMismatch ();

HashCtx = (HASH_HANDLE *)HashHandle;
Expand All @@ -183,7 +203,7 @@ HashCompleteAndExtend (
Status = TdExtendRtmr (
(UINT32*)DigestList->digests[0].digest.sha384,
SHA384_DIGEST_SIZE,
(UINT8)PcrTdxExtendMap.Index
GetMappedRtmrIndex(PcrIndex)
);
return Status;
}
Expand Down
32 changes: 18 additions & 14 deletions OvmfPkg/Library/TdxStartupLib/Tcg.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#include <Library/ReportStatusCodeLib.h>
#include <Library/ResetSystemLib.h>
#include <Library/PrintLib.h>

#include <Library/TdxStartupLib.h>
#include "TdxStartupInternal.h"

#pragma pack (1)
Expand Down Expand Up @@ -70,6 +70,7 @@ CreateTdxExtendEvent (
TDX_EVENT *TdxEvent;
UINT8 *DigestBuffer;
TDX_DIGEST_VALUE *TdxDigest;
UINT8 *Ptr;

DEBUG ((EFI_D_INFO, "Creating Tcg2PcrEvent PCR %d EventType 0x%x\n", PCRIndex, EventType));

Expand All @@ -91,40 +92,43 @@ CreateTdxExtendEvent (

DEBUG ((EFI_D_INFO, " Tcg2PcrEvent - data %p\n", EventHobData));

Ptr = (UINT8*)EventHobData;
//
// Initialize PcrEvent data now
//
TcgPcrEvent2 = EventHobData;
TcgPcrEvent2->PCRIndex = PCRIndex;
TcgPcrEvent2->EventType = EventType;
CopyMem(Ptr, &PCRIndex, sizeof(TCG_PCRINDEX));
Ptr += sizeof(TCG_PCRINDEX);
CopyMem(Ptr, &EventType, sizeof(TCG_EVENTTYPE));
Ptr += sizeof(TCG_EVENTTYPE);

//
// We don't have a digest to copy yet, but we can to copy the eventsize/data now
//
DigestBuffer = (UINT8 *)&TcgPcrEvent2->Digest;
DigestBuffer = Ptr;
DEBUG ((EFI_D_INFO, " Tcg2PcrEvent - digest %p\n", DigestBuffer));

TdxDigest = (TDX_DIGEST_VALUE *)DigestBuffer;
TdxDigest->count = 1;
TdxDigest->hashAlg = TPM_ALG_SHA384;
ZeroMem(TdxDigest->sha384, SHA384_DIGEST_SIZE);

DigestBuffer = DigestBuffer + sizeof(TDX_DIGEST_VALUE);
Ptr += sizeof(TDX_DIGEST_VALUE);
DEBUG ((EFI_D_INFO, " Tcg2PcrEvent - eventdata %p\n", DigestBuffer));

CopyMem (DigestBuffer, &EventSize, sizeof(TcgPcrEvent2->EventSize));
DigestBuffer = DigestBuffer + sizeof(TcgPcrEvent2->EventSize);
CopyMem (DigestBuffer, EventData, EventSize);
DigestBuffer = DigestBuffer + EventSize;
TdxEvent = (TDX_EVENT *)DigestBuffer;
CopyMem (Ptr, &EventSize, sizeof(UINT32));
Ptr += sizeof(UINT32);
CopyMem (Ptr, EventData, EventSize);
Ptr += EventSize;
TdxEvent = (TDX_EVENT *)Ptr;

//
// Initialize the TdxEvent so we can perform measurement in DXE.
// During early DXE, the gTcgEvent2EntryHobGuid will be parsed, the data hashed, and TcgEvent2 hobs
// updated with the updated hash
//
//TdxEvent->Signature = TCG_TDX_EVENT_DATA_SIGNATURE;
TdxEvent->HashData = HashData;
TdxEvent->HashDataLen = HashDataLen;
TdxEvent->Signature = SIGNATURE_32('T', 'D', 'E', 'T');
TdxEvent->HashDataPtr = (UINT64)(UINTN)HashData;
TdxEvent->HashDataLen = (UINT64)HashDataLen;

Status = EFI_SUCCESS;
return Status;
Expand Down
25 changes: 6 additions & 19 deletions OvmfPkg/Library/TdxStartupLib/TdxStartupInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,12 @@
} MP_RELOCATION_MAP;

#define HANDOFF_TABLE_DESC "TdxTable"
typedef struct {
UINT8 TableDescriptionSize;
UINT8 TableDescription[sizeof(HANDOFF_TABLE_DESC)];
UINT64 NumberOfTables;
EFI_CONFIGURATION_TABLE TableEntry[1];
} TDX_HANDOFF_TABLE_POINTERS2;

typedef struct {
UINT32 count;
TPMI_ALG_HASH hashAlg;
BYTE sha384[SHA384_DIGEST_SIZE];
} TDX_DIGEST_VALUE;

typedef struct {
UINT32 Signature;
UINT8 *HashData;
UINTN HashDataLen;
} TDX_EVENT;

typedef struct {
UINT8 TableDescriptionSize;
UINT8 TableDescription[sizeof (HANDOFF_TABLE_DESC)];
UINT64 NumberOfTables;
EFI_CONFIGURATION_TABLE TableEntry[1];
} TDX_HANDOFF_TABLE_POINTERS2;
#pragma pack()

#define LOOPIT(X) do { \
Expand Down
Loading

0 comments on commit ba06323

Please sign in to comment.