Skip to content

Commit

Permalink
SecurityPkg: Cache TPM interface type info
Browse files Browse the repository at this point in the history
Cache TPM interface type info to avoid excessive interface ID register read

Cc: Long Qin <[email protected]>
Cc: Yao Jiewen <[email protected]>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Chao Zhang <[email protected]>
Reviewed-by: Long Qin <[email protected]>
  • Loading branch information
zhangchaointel committed Jun 25, 2018
1 parent 796ef9d commit f15cb99
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 145 deletions.
12 changes: 11 additions & 1 deletion SecurityPkg/Include/Library/Tpm2DeviceLib.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @file
This library abstract how to access TPM2 hardware device.
Copyright (c) 2013, Intel Corporation. All rights reserved. <BR>
Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
Expand All @@ -17,6 +17,16 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

#include <Uefi.h>

//
// Used in PcdActiveTpmInterfaceType to identify TPM interface type
//
typedef enum {
Tpm2PtpInterfaceTis,
Tpm2PtpInterfaceFifo,
Tpm2PtpInterfaceCrb,
Tpm2PtpInterfaceMax,
} TPM2_PTP_INTERFACE_TYPE;

/**
This service enables the sending of commands to the TPM2.
Expand Down
38 changes: 37 additions & 1 deletion SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This library is TPM2 DTPM device lib.
Choosing this library means platform uses and only uses DTPM device as TPM2 engine.
Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved. <BR>
Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
Expand All @@ -17,6 +17,19 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/Tpm2DeviceLib.h>
#include <Library/PcdLib.h>

/**
Return PTP interface type.
@param[in] Register Pointer to PTP register.
@return PTP interface type.
**/
TPM2_PTP_INTERFACE_TYPE
Tpm2GetPtpInterface (
IN VOID *Register
);

/**
This service enables the sending of commands to the TPM2.
Expand Down Expand Up @@ -114,3 +127,26 @@ Tpm2RegisterTpm2DeviceLib (
{
return EFI_UNSUPPORTED;
}

/**
The function caches current active TPM interface type.
@retval EFI_SUCCESS DTPM2.0 instance is registered, or system dose not surpport registr DTPM2.0 instance
**/
EFI_STATUS
EFIAPI
Tpm2DeviceLibConstructor (
VOID
)
{
TPM2_PTP_INTERFACE_TYPE PtpInterface;

//
// Cache current active TpmInterfaceType only when needed
//
if (PcdGet8(PcdActiveTpmInterfaceType) == 0xFF) {
PtpInterface = Tpm2GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
PcdSet8S(PcdActiveTpmInterfaceType, PtpInterface);
}
return EFI_SUCCESS;
}
8 changes: 5 additions & 3 deletions SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.inf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# used for every TPM 2.0 command. Choosing this library means platform uses and
# only uses TPM 2.0 DTPM device.
#
# Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
Expand All @@ -27,8 +27,8 @@
FILE_GUID = E54A3327-A345-4068-8842-70AC0D519855
MODULE_TYPE = BASE
VERSION_STRING = 1.0
LIBRARY_CLASS = Tpm2DeviceLib

LIBRARY_CLASS = Tpm2DeviceLib|PEIM DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER
Constructor = Tpm2DeviceLibConstructor
#
# The following information is for reference only and not required by the build tools.
#
Expand All @@ -50,6 +50,8 @@
IoLib
TimerLib
DebugLib
PcdLib

[Pcd]
gEfiSecurityPkgTokenSpaceGuid.PcdTpmBaseAddress ## CONSUMES
gEfiSecurityPkgTokenSpaceGuid.PcdActiveTpmInterfaceType ## PRODUCES
27 changes: 24 additions & 3 deletions SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
It can be registered to Tpm2 Device router, to be active TPM2 engine,
based on platform setting.
Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved. <BR>
Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
Expand All @@ -18,9 +18,22 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/Tpm2DeviceLib.h>
#include <Library/PcdLib.h>

#include <Guid/TpmInstance.h>

/**
Return PTP interface type.
@param[in] Register Pointer to PTP register.
@return PTP interface type.
**/
TPM2_PTP_INTERFACE_TYPE
Tpm2GetPtpInterface (
IN VOID *Register
);

/**
Dump PTP register information.
Expand Down Expand Up @@ -72,7 +85,7 @@ TPM2_DEVICE_INTERFACE mDTpm2InternalTpm2Device = {
};

/**
The function register DTPM2.0 instance.
The function register DTPM2.0 instance and caches current active TPM interface type.
@retval EFI_SUCCESS DTPM2.0 instance is registered, or system dose not surpport registr DTPM2.0 instance
**/
Expand All @@ -82,14 +95,22 @@ Tpm2InstanceLibDTpmConstructor (
VOID
)
{
EFI_STATUS Status;
EFI_STATUS Status;
TPM2_PTP_INTERFACE_TYPE PtpInterface;

Status = Tpm2RegisterTpm2DeviceLib (&mDTpm2InternalTpm2Device);
if ((Status == EFI_SUCCESS) || (Status == EFI_UNSUPPORTED)) {
//
// Unsupported means platform policy does not need this instance enabled.
//
if (Status == EFI_SUCCESS) {
//
// Cache current active TpmInterfaceType only when needed
//
if (PcdGet8(PcdActiveTpmInterfaceType) == 0xFF) {
PtpInterface = Tpm2GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
PcdSet8S(PcdActiveTpmInterfaceType, PtpInterface);
}
DumpPtpInfo ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
}
return EFI_SUCCESS;
Expand Down
6 changes: 4 additions & 2 deletions SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.inf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# engine, based on platform setting. It supports both TIS (TPM Interface Specification)
# and PTP (Platform TPM Profile) functions.
#
# Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
Expand Down Expand Up @@ -46,6 +46,8 @@
IoLib
TimerLib
DebugLib
PcdLib

[Pcd]
gEfiSecurityPkgTokenSpaceGuid.PcdTpmBaseAddress ## CONSUMES
gEfiSecurityPkgTokenSpaceGuid.PcdTpmBaseAddress ## CONSUMES
gEfiSecurityPkgTokenSpaceGuid.PcdActiveTpmInterfaceType ## PRODUCES
47 changes: 20 additions & 27 deletions SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <IndustryStandard/TpmPtp.h>
#include <IndustryStandard/TpmTis.h>

typedef enum {
PtpInterfaceTis,
PtpInterfaceFifo,
PtpInterfaceCrb,
PtpInterfaceMax,
} PTP_INTERFACE_TYPE;

//
// Execution of the command may take from several seconds to minutes for certain
// commands, such as key generation.
Expand Down Expand Up @@ -370,7 +363,7 @@ TisPcRequestUseTpm (
@return PTP interface type.
**/
PTP_INTERFACE_TYPE
TPM2_PTP_INTERFACE_TYPE
Tpm2GetPtpInterface (
IN VOID *Register
)
Expand All @@ -379,7 +372,7 @@ Tpm2GetPtpInterface (
PTP_FIFO_INTERFACE_CAPABILITY InterfaceCapability;

if (!Tpm2IsPtpPresence (Register)) {
return PtpInterfaceMax;
return Tpm2PtpInterfaceMax;
}
//
// Check interface id
Expand All @@ -390,15 +383,15 @@ Tpm2GetPtpInterface (
if ((InterfaceId.Bits.InterfaceType == PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_CRB) &&
(InterfaceId.Bits.InterfaceVersion == PTP_INTERFACE_IDENTIFIER_INTERFACE_VERSION_CRB) &&
(InterfaceId.Bits.CapCRB != 0)) {
return PtpInterfaceCrb;
return Tpm2PtpInterfaceCrb;
}
if ((InterfaceId.Bits.InterfaceType == PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_FIFO) &&
(InterfaceId.Bits.InterfaceVersion == PTP_INTERFACE_IDENTIFIER_INTERFACE_VERSION_FIFO) &&
(InterfaceId.Bits.CapFIFO != 0) &&
(InterfaceCapability.Bits.InterfaceVersion == INTERFACE_CAPABILITY_INTERFACE_VERSION_PTP)) {
return PtpInterfaceFifo;
return Tpm2PtpInterfaceFifo;
}
return PtpInterfaceTis;
return Tpm2PtpInterfaceTis;
}

/**
Expand All @@ -417,7 +410,7 @@ DumpPtpInfo (
UINT16 Vid;
UINT16 Did;
UINT8 Rid;
PTP_INTERFACE_TYPE PtpInterface;
TPM2_PTP_INTERFACE_TYPE PtpInterface;

if (!Tpm2IsPtpPresence (Register)) {
return ;
Expand Down Expand Up @@ -458,16 +451,16 @@ DumpPtpInfo (
Vid = 0xFFFF;
Did = 0xFFFF;
Rid = 0xFF;
PtpInterface = Tpm2GetPtpInterface (Register);
PtpInterface = PcdGet8(PcdActiveTpmInterfaceType);
DEBUG ((EFI_D_INFO, "PtpInterface - %x\n", PtpInterface));
switch (PtpInterface) {
case PtpInterfaceCrb:
case Tpm2PtpInterfaceCrb:
Vid = MmioRead16 ((UINTN)&((PTP_CRB_REGISTERS *)Register)->Vid);
Did = MmioRead16 ((UINTN)&((PTP_CRB_REGISTERS *)Register)->Did);
Rid = (UINT8)InterfaceId.Bits.Rid;
break;
case PtpInterfaceFifo:
case PtpInterfaceTis:
case Tpm2PtpInterfaceFifo:
case Tpm2PtpInterfaceTis:
Vid = MmioRead16 ((UINTN)&((PTP_FIFO_REGISTERS *)Register)->Vid);
Did = MmioRead16 ((UINTN)&((PTP_FIFO_REGISTERS *)Register)->Did);
Rid = MmioRead8 ((UINTN)&((PTP_FIFO_REGISTERS *)Register)->Rid);
Expand Down Expand Up @@ -501,20 +494,20 @@ DTpm2SubmitCommand (
IN UINT8 *OutputParameterBlock
)
{
PTP_INTERFACE_TYPE PtpInterface;
TPM2_PTP_INTERFACE_TYPE PtpInterface;

PtpInterface = Tpm2GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
PtpInterface = PcdGet8(PcdActiveTpmInterfaceType);
switch (PtpInterface) {
case PtpInterfaceCrb:
case Tpm2PtpInterfaceCrb:
return PtpCrbTpmCommand (
(PTP_CRB_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress),
InputParameterBlock,
InputParameterBlockSize,
OutputParameterBlock,
OutputParameterBlockSize
);
case PtpInterfaceFifo:
case PtpInterfaceTis:
case Tpm2PtpInterfaceFifo:
case Tpm2PtpInterfaceTis:
return Tpm2TisTpmCommand (
(TIS_PC_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress),
InputParameterBlock,
Expand All @@ -540,14 +533,14 @@ DTpm2RequestUseTpm (
VOID
)
{
PTP_INTERFACE_TYPE PtpInterface;
TPM2_PTP_INTERFACE_TYPE PtpInterface;

PtpInterface = Tpm2GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
PtpInterface = PcdGet8(PcdActiveTpmInterfaceType);
switch (PtpInterface) {
case PtpInterfaceCrb:
case Tpm2PtpInterfaceCrb:
return PtpCrbRequestUseTpm ((PTP_CRB_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress));
case PtpInterfaceFifo:
case PtpInterfaceTis:
case Tpm2PtpInterfaceFifo:
case Tpm2PtpInterfaceTis:
return TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress));
default:
return EFI_NOT_FOUND;
Expand Down
12 changes: 11 additions & 1 deletion SecurityPkg/SecurityPkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,16 @@
# linked, and the value of this PCD should be got in module entrypoint.
# @Prompt Hash Algorithm bitmap.
gEfiSecurityPkgTokenSpaceGuid.PcdTcg2HashAlgorithmBitmap|0xFFFFFFFF|UINT32|0x00010016


## This PCD indicates current active TPM interface type.
# Accodingt to TCG PTP spec 1.3, there are 3 types defined in TPM2_PTP_INTERFACE_TYPE.<BR>
# 0x00 - FIFO interface as defined in TIS 1.3 is active.<BR>
# 0x01 - FIFO interface as defined in PTP for TPM 2.0 is active.<BR>
# 0x02 - CRB interface is active.<BR>
# 0xFF - Contains no current active TPM interface type.<BR>
#
# @Prompt current active TPM interface type.
gEfiSecurityPkgTokenSpaceGuid.PcdActiveTpmInterfaceType|0xFF|UINT8|0x0001001E

[UserExtensions.TianoCore."ExtraFiles"]
SecurityPkgExtra.uni
10 changes: 9 additions & 1 deletion SecurityPkg/SecurityPkg.uni
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,12 @@
#string STR_gEfiSecurityPkgTokenSpaceGuid_PcdTpm2PossibleIrqNumBuf_PROMPT #language en-US "Possible TPM2 device interrupt number buffer"

#string STR_gEfiSecurityPkgTokenSpaceGuid_PcdTpm2PossibleIrqNumBuf_HELP #language en-US "This PCD defines possible TPM2 interrupt number in a platform reported by _PRS control method.\n"
"If PcdTpm2CurrentIrqNum set to 0, _PRS will not report any possible TPM2 interrupt numbers."
"If PcdTpm2CurrentIrqNum set to 0, _PRS will not report any possible TPM2 interrupt numbers."

#string STR_gEfiSecurityPkgTokenSpaceGuid_PcdActiveTpmInterfaceType_PROMPT #language en-US "Current active TPM interface type"

#string STR_gEfiSecurityPkgTokenSpaceGuid_PcdActiveTpmInterfaceType_HELP #language en-US "This PCD indicates current active TPM interface type.\n"
"0x00 - FIFO interface as defined in TIS 1.3 is active.<BR>\n"
"0x01 - FIFO interface as defined in PTP for TPM 2.0 is active.<BR>\n"
"0x02 - CRB interface is active.<BR>\n"
"0xFF - Contains no current active TPM interface type<BR>"
3 changes: 2 additions & 1 deletion SecurityPkg/Tcg/Tcg2Config/Tcg2ConfigDxe.inf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# By this module, user may select TPM device, clear TPM state, etc.
# NOTE: This module is only for reference only, each platform should have its own setup page.
#
# Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
Expand Down Expand Up @@ -79,6 +79,7 @@
gEfiSecurityPkgTokenSpaceGuid.PcdTpmBaseAddress ## CONSUMES
gEfiSecurityPkgTokenSpaceGuid.PcdTcgPhysicalPresenceInterfaceVer ## CONSUMES
gEfiSecurityPkgTokenSpaceGuid.PcdTpm2AcpiTableRev ## CONSUMES
gEfiSecurityPkgTokenSpaceGuid.PcdActiveTpmInterfaceType ## CONSUMES

[Depex]
gEfiTcg2ProtocolGuid AND
Expand Down
Loading

0 comments on commit f15cb99

Please sign in to comment.