Skip to main content

tss_esapi/context/tpm_commands/
startup.rs

1// Copyright 2021 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::{
4    constants::StartupType,
5    tss2_esys::{Esys_Shutdown, Esys_Startup},
6    Context, Error, Result,
7};
8use log::error;
9
10impl Context {
11    /// Send a TPM2_STARTUP command to the TPM
12    pub fn startup(&mut self, startup_type: StartupType) -> Result<()> {
13        let ret = unsafe { Esys_Startup(self.mut_context(), startup_type.into()) };
14        let ret = Error::from_tss_rc(ret);
15        if ret.is_success() {
16            Ok(())
17        } else {
18            error!("Error while starting up TPM: {}", ret);
19            Err(ret)
20        }
21    }
22
23    /// Send a TPM2_SHUTDOWN command to the TPM
24    pub fn shutdown(&mut self, shutdown_type: StartupType) -> Result<()> {
25        let ret = unsafe {
26            Esys_Shutdown(
27                self.mut_context(),
28                self.optional_session_1(),
29                self.optional_session_2(),
30                self.optional_session_3(),
31                shutdown_type.into(),
32            )
33        };
34        let ret = Error::from_tss_rc(ret);
35        if ret.is_success() {
36            Ok(())
37        } else {
38            error!("Error while shutting down TPM: {}", ret);
39            Err(ret)
40        }
41    }
42}