Skip to content

SecurityPkg: Introduce Tpm2StartupLib V2 - #1974

Open
Raymond Diaz (Raymond-MS) wants to merge 3 commits into
microsoft:release/202608from
Raymond-MS:personal/rdiaz/Tpm2StartupLib_202608
Open

Raymond Diaz (Raymond-MS) wants to merge 3 commits into
microsoft:release/202608from
Raymond-MS:personal/rdiaz/Tpm2StartupLib_202608

Conversation

@Raymond-MS

Copy link
Copy Markdown
Contributor

Description

Updated Tpm2StartupLib to no longer just handle calling Tpm2Startup. The library is now responsible for validating TPM is available and working, initializing the TPM via Tpm2Startup, and syncing the active PCR banks. Sycning PCR banks happens in two phases: Security and Intent. Security makes sure that there are no currently active PCR banks that the platform doesn't support. Platform support is indicated by a PCD: PcdTcg2HashLibSupportMask. Intent makes sure that the active banks are defaulted to an intent value provided by the caller; this can be platform or user intent. Platform intent is indicated by a PCD: PcdTpm2HashMask. PcdTpm2InitializationPolicy now indicates which phase is responsible for calling Tpm2StartupInit. A new PCD: PcdTpm2SyncPolicy, determines which phase is responsible for syncing to intent. The security sync is called in every phase as there are no pre-requisites to calling it and calling it as soon as possible is preferred. PcdTcg2HashAlgorithmBitmap indicates which hash algorithms were registered via HashLib. This value is checked against PcdTcg2HashLibSupportMask to make sure that what the platform says it supports is actually what gets registered. If there is a mismatch then it means the platform is misconfigured.

Tpm2PcrAllocate takes in a value "AllocationSuccess" which indicates whether or not the call to enable/disable banks was successful in the TPM. It is possible for the Tpm2PcrAllocate function to return EFI_SUCCESS with "AllocationSuccess" being "NO". This means the command was sent successfully but the command was not successful. Updated the call to return an error if "AllocationSuccess" is "NO" rather than silently allowing the error through.

HashLibBaseCryptoRouterPei and HashLibBaseCryptoRouterDxe both used PcdTpm2HashMask in order to mask which algorithms get registered via the platform. It also used PcdTpm2HashMask in order to know which hash algorithms to use as PcdTpm2HashMask after syncing PCR banks would match the active PCR banks. This update uses the active banks, queried on HashStart, to know which banks to hash and extend to. It also removes the gating behind PcdTpm2HashMask allowing the platform to register as many algorithms as it would like. This allows the platform to have support for more algorithms than what is indicated by PcdTpm2HashMask while still using it to indicate what the platform would like to default to.

For details on how to complete these options and their meaning refer to CONTRIBUTING.md.

  • Impacts functionality?
  • Impacts security?
  • Breaking change?
  • Includes tests?
  • Includes documentation?

How This Was Tested

Tested both Q35 and ARM VIRT with TPM enabled and TpmShellApp included. Verified TPM functionality.

Integration Instructions

Include Tpm2StartupLib to your platform.dsc.

@mu-automation

mu-automation Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

✅ QEMU Validation Passed

Source Dependencies

Repository Commit
mu_basecore 1e5a980
mu_tiano_platforms 9a5b7f8

Results

Platform Target Build Boot Overall Boot Time Build Logs Boot Logs
Q35 DEBUG ✅ success ✅ success 0m 13s Build Logs Boot Logs
ArmVirt DEBUG ✅ success ✅ success 0m 18s Build Logs Boot Logs

Workflow run: https://github.com/microsoft/mu_basecore/actions/runs/40781194993

This comment was automatically generated by the Mu QEMU PR Validation workflow.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

S3 restore failures are discarded in two initialization paths, and the public API break is not identified or migrated.

4 open findings
What changed in this PR

Expands Tpm2StartupLib to manage TPM availability, startup, PCR-bank synchronization, and active-bank-based hashing.

Changes:

  • Adds phased TPM startup and PCR synchronization APIs and policies.
  • Uses active PCR banks for hashing and event logging.
  • Reports unsuccessful PCR allocation and updates Peiless SEC integration.
File Description
SecurityPkg/​Tcg/​Tcg2Pei/​Tcg2Pei.inf Adds startup library and policy dependencies.
SecurityPkg/​Tcg/​Tcg2Pei/​Tcg2Pei.c Integrates startup synchronization and active-bank logging.
SecurityPkg/​Tcg/​Tcg2Dxe/​Tcg2Dxe.inf Adds startup library and PCD dependencies.
SecurityPkg/​Tcg/​Tcg2Dxe/​Tcg2Dxe.c Integrates startup synchronization in DXE.
SecurityPkg/​SecurityPkg.dsc Configures new TPM policies and hash support.
SecurityPkg/​SecurityPkg.dec Defines new policy and support PCDs.
SecurityPkg/​Library/​Tpm2StartupLibNull/​Tpm2StartupLibNull.c Implements null versions of expanded APIs.
SecurityPkg/​Library/​Tpm2StartupLib/​Tpm2StartupLib.inf Updates library dependencies.
SecurityPkg/​Library/​Tpm2StartupLib/​Tpm2StartupLib.c Implements startup validation and PCR synchronization.
SecurityPkg/​Library/​Tpm2StartupLib/​README.md Documents configuration and APIs.
SecurityPkg/​Library/​Tpm2CommandLib/​Tpm2Integrity.c Rejects unsuccessful PCR allocation responses.
SecurityPkg/​Library/​HashLibBaseCryptoRouter/​HashLibBaseCryptoRouterPei.uni Updates PEI router description.
SecurityPkg/​Library/​HashLibBaseCryptoRouter/​HashLibBaseCryptoRouterPei.inf Removes intent-mask dependency.
SecurityPkg/​Library/​HashLibBaseCryptoRouter/​HashLibBaseCryptoRouterPei.c Hashes using active PCR banks.
SecurityPkg/​Library/​HashLibBaseCryptoRouter/​HashLibBaseCryptoRouterDxe.uni Updates DXE router description.
SecurityPkg/​Library/​HashLibBaseCryptoRouter/​HashLibBaseCryptoRouterDxe.inf Removes intent-mask dependency.
SecurityPkg/​Library/​HashLibBaseCryptoRouter/​HashLibBaseCryptoRouterDxe.c Hashes using active PCR banks.
SecurityPkg/​Include/​Library/​Tpm2StartupLib.h Exposes expanded startup APIs.
ArmPlatformPkg/​PeilessSec/​PeilessSec.inf Adds TPM policy dependencies.
ArmPlatformPkg/​PeilessSec/​PeilessSec.h Adds startup-related declarations.
ArmPlatformPkg/​PeilessSec/​PeilessSec.c Integrates phased TPM startup and measurement.
ArmPlatformPkg/​ArmPlatformPkg.dsc Adds supporting library configuration.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +226 to +233
if (PcdGet8 (PcdTpm2InitializationPolicy) == 1) {
// TODO: Need to figure out what to do if S3ErrorReport returns TRUE.
Status = Tpm2StartupInit (GetBootMode () == BOOT_ON_S3_RESUME, NULL);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "%a - TPM failed StartupInit: %r\n", __func__, Status));
goto Error;
}
}
Comment on lines 61 to +63
Tpm2StartupInit (
IN BOOLEAN IsS3Resume,
OUT BOOLEAN *S3ErrorReport
Comment on lines +3273 to +3280
if (PcdGet8 (PcdTpm2InitializationPolicy) == 1) {
// TODO: Need to figure out what to do if S3ErrorReport returns TRUE.
Status = Tpm2StartupInit (GetBootModeHob () == BOOT_ON_S3_RESUME, NULL);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "%a - TPM failed StartupInit!\n", __func__));
return Status;
}
}

[PcdsDynamic, PcdsDynamicEx]

## MU_CHANGE
@codecov-commenter

Codecov Comments Bot (codecov-commenter) commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 165 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (release/202608@003a16c). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...ecurityPkg/Library/Tpm2StartupLib/Tpm2StartupLib.c 0.00% 100 Missing ⚠️
...shLibBaseCryptoRouter/HashLibBaseCryptoRouterDxe.c 0.00% 25 Missing ⚠️
...shLibBaseCryptoRouter/HashLibBaseCryptoRouterPei.c 0.00% 20 Missing ⚠️
SecurityPkg/Tcg/Tcg2Pei/Tcg2Pei.c 0.00% 19 Missing ⚠️
SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.c 0.00% 1 Missing ⚠️
Additional details and impacted files
@@               Coverage Diff                @@
##             release/202608   #1974   +/-   ##
================================================
  Coverage                  ?   1.54%           
================================================
  Files                     ?     164           
  Lines                     ?   38861           
  Branches                  ?     172           
================================================
  Hits                      ?     601           
  Misses                    ?   38260           
  Partials                  ?       0           
Flag Coverage Δ
SecurityPkg 1.54% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Raymond-MS
Raymond Diaz (Raymond-MS) force-pushed the personal/rdiaz/Tpm2StartupLib_202608 branch from e21b88c to 2fc1900 Compare October 13, 2026 18:22
@cfernald

Chris Fernald (cfernald) commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Raymond Diaz (@Raymond-MS), is there significant difference between this is and #1922 we should be aware of?

@Raymond-MS

Copy link
Copy Markdown
Contributor Author

Raymond Diaz (Raymond Diaz (@Raymond-MS)), is there significant difference between this is and #1922 we should be aware of?

No, there is nothing different. I just moved this to 202608. I addressed your comments from the previous review so there shouldn't be anything new that you need to look at.

HashLibBaseCryptoRouterPei and HashLibBaseCryptoRouterDxe both used
PcdTpm2HashMask in order to mask which algorithms get registered via
the platform. It also used PcdTpm2HashMask in order to know which
hash algorithms to use as PcdTpm2HashMask after syncing PCR banks
would match the active PCR banks. This update uses the active banks,
queried on HashStart, to know which banks to hash and extend to. It
also removes the gating behind PcdTpm2HashMask allowing the platform
to register as many algorithms as it would like. This allows the
platform to have support for more algorithms than what is indicated
by PcdTpm2HashMask while still using it to indicate what the platform
would like to default to.

Signed-off-by: Raymond Diaz <raymond.diaz@microsoft.com>
Tpm2PcrAllocate takes in a value "AllocationSuccess" which
indicates whether or not the call to enable/disable banks was
successful in the TPM. It is possible for the Tpm2PcrAllocate
function to return EFI_SUCCESS with "AllocationSuccess" being
"NO". This means the command was sent successfully but the
command was not successful. Updated the call to return an error
if "AllocationSuccess" is "NO" rather than silently allowing the
error through.

Signed-off-by: Raymond Diaz <raymond.diaz@microsoft.com>
Updated Tpm2StartupLib to no longer just handle calling Tpm2Startup.
The library is now responsible for validating TPM is available and
working, initializing the TPM via Tpm2Startup, and syncing the
active PCR banks. Sycning PCR banks happens in two phases: Security
and Intent. Security makes sure that there are no currently active
PCR banks that the platform doesn't support. Platform support is
indicated by a PCD: PcdTcg2HashLibSupportMask. Intent makes sure
that the active banks are defaulted to an intent value provided by
the caller; this can be platform or user intent. Platform intent is
indicated by a PCD: PcdTpm2HashMask. PcdTpm2InitializationPolicy now
indicates which phase is responsible for calling Tpm2StartupInit.
A new PCD: PcdTpm2SyncPolicy, determines which phase is responsible
for syncing to intent. The security sync is called in every phase
as there are no pre-requisites to calling it and calling it as soon
as possible is preferred. PcdTcg2HashAlgorithmBitmap indicates which
hash algorithms were registered via HashLib. This value is checked
against PcdTcg2HashLibSupportMask to make sure that what the platform
says it supports is actually what gets registered. If there is a
mismatch then it means the platform is misconfigured.

Signed-off-by: Raymond Diaz <raymond.diaz@microsoft.com>
@Raymond-MS
Raymond Diaz (Raymond-MS) force-pushed the personal/rdiaz/Tpm2StartupLib_202608 branch from 2fc1900 to 1e5a980 Compare October 28, 2026 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants