-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vtpm: Manufacture the Microsoft TPM 2.0
Initialize the TPM values in preparation for the TPM's first use. This follows the same steps that the Microsoft TPM Simulator uses to manufacture the TPM. Signed-off-by: Claudio Carvalho <[email protected]>
- Loading branch information
Showing
6 changed files
with
165 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
// | ||
// Copyright (C) 2023 IBM | ||
// | ||
// Authors: Claudio Carvalho <[email protected]> | ||
|
||
// This crate implements the MS TPM Simulator interface defined in | ||
// libvtpm/ms-tpm-20-ref/TPMCmd/Simulator/include/TpmTcpProtocol.h | ||
// | ||
// The same interface is also implemented in ms-tpm-20-ref/TPMCmd/Simulator/; we don't use it | ||
// because we are not compiling the TPM Simulator (-DSIMULATION=NO), as it brings in more | ||
// dependencies on libc and a higher memory footprint. | ||
|
||
use core::ffi::c_void; | ||
|
||
use crate::address::VirtAddr; | ||
use crate::protocols::errors::SvsmReqError; | ||
use crate::vtpm::bindings::{ | ||
_plat__NVDisable, _plat__NVEnable, _plat__SetNvAvail, _plat__Signal_PowerOn, | ||
_plat__Signal_Reset, | ||
}; | ||
use crate::vtpm::mstpm::{mstpm_manufacture, mstpm_teardown}; | ||
|
||
static mut VTPM_IS_POWERED_ON: bool = false; | ||
|
||
/// Initialize the vTPM | ||
pub fn mssim_vtpm_init() -> Result<(), SvsmReqError> { | ||
// Manufacture the MS TPM following the same steps done in the Simulator: | ||
// | ||
// 1. Manufacture it for the first time | ||
// 2. Make sure it does not fail if it is re-manufactured | ||
// 3. Teardown to indicate it needs to be manufactured | ||
// 4. Manufacture it for the first time | ||
// 5. Power it on indicating it requires startup. By default, OVMF will start | ||
// and selftest it. | ||
|
||
unsafe { _plat__NVEnable(VirtAddr::null().as_mut_ptr::<c_void>()) }; | ||
|
||
let mut rc = mstpm_manufacture(1)?; | ||
if rc != 0 { | ||
unsafe { _plat__NVDisable(1) }; | ||
return Err(SvsmReqError::incomplete()); | ||
} | ||
|
||
rc = mstpm_manufacture(0)?; | ||
if rc != 1 { | ||
return Err(SvsmReqError::incomplete()); | ||
} | ||
|
||
mstpm_teardown()?; | ||
rc = mstpm_manufacture(1)?; | ||
if rc != 0 { | ||
return Err(SvsmReqError::incomplete()); | ||
} | ||
|
||
mssim_signal_poweron(false)?; | ||
mssim_signal_nvon()?; | ||
|
||
log::info!("vTPM manufactured"); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Power on the vTPM, which also triggers a reset | ||
/// | ||
/// @only_reset: If enabled, it will only reset the vTPM; | ||
/// however, the vtPM has to be powered on previously. | ||
/// Otherwise, it will fail. | ||
pub fn mssim_signal_poweron(only_reset: bool) -> Result<(), SvsmReqError> { | ||
unsafe { | ||
if VTPM_IS_POWERED_ON && !only_reset { | ||
return Ok(()); | ||
} | ||
if only_reset && !VTPM_IS_POWERED_ON { | ||
return Err(SvsmReqError::invalid_request()); | ||
} | ||
if !only_reset { | ||
_plat__Signal_PowerOn(); | ||
} | ||
|
||
// It calls TPM_init() within to indicate that a TPM2_Startup is required. | ||
_plat__Signal_Reset(); | ||
|
||
VTPM_IS_POWERED_ON = true; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn mssim_signal_nvon() -> Result<(), SvsmReqError> { | ||
unsafe { | ||
if !VTPM_IS_POWERED_ON { | ||
return Err(SvsmReqError::invalid_request()); | ||
} | ||
_plat__SetNvAvail(); | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
// | ||
// Copyright (C) 2023 IBM | ||
// | ||
// Authors: Claudio Carvalho <[email protected]> | ||
|
||
// This crate make calls to the Microsoft libtpm.a built from ms-tpm-20-ref/TPMCmd/tpm. | ||
// The libtpm implements the commands defined in the "TCG TPM 2.0 - Part 3" specification. | ||
|
||
use crate::protocols::errors::SvsmReqError; | ||
use crate::vtpm::bindings::{TPM_Manufacture, TPM_TearDown}; | ||
|
||
/// Prepare the TPM for re-manufacture. | ||
pub fn mstpm_teardown() -> Result<(), SvsmReqError> { | ||
unsafe { | ||
match TPM_TearDown() { | ||
0 => Ok(()), | ||
rc => { | ||
log::error!("TPM_Teardown failed rc={}", rc); | ||
Err(SvsmReqError::incomplete()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Initialize the TPM values in preparation for the TPM's first use. | ||
/// This function will fail if previously called. The TPM can be re-manufactured | ||
/// by calling mstpm_teardown() first and then calling this function again. | ||
pub fn mstpm_manufacture(first_time: i32) -> Result<i32, SvsmReqError> { | ||
unsafe { | ||
// Return Type: int | ||
// -1 failure | ||
// 0 success | ||
// 1 manufacturing process previously performed | ||
match TPM_Manufacture(first_time) { | ||
// TPM manufactured successfully | ||
0 => Ok(0), | ||
// TPM already manufactured | ||
1 => Ok(1), | ||
// TPM failed to manufacture | ||
rc => { | ||
log::error!("TPM_Manufacture failed rc={}", rc); | ||
Err(SvsmReqError::incomplete()) | ||
} | ||
} | ||
} | ||
} |