tss_esapi/context/tpm_commands/testing.rs
1// Copyright 2021 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::{
4 interface_types::YesNo,
5 structures::MaxBuffer,
6 tss2_esys::{Esys_GetTestResult, Esys_SelfTest},
7 Context, Result, ReturnCode,
8};
9use log::error;
10use std::convert::TryFrom;
11use std::ptr::null_mut;
12
13impl Context {
14 /// Execute the TPM self test and returns the result
15 pub fn self_test(&mut self, full_test: bool) -> Result<()> {
16 ReturnCode::ensure_success(
17 unsafe {
18 Esys_SelfTest(
19 self.mut_context(),
20 self.optional_session_1(),
21 self.optional_session_2(),
22 self.optional_session_3(),
23 YesNo::from(full_test).into(),
24 )
25 },
26 |ret| {
27 error!("Error in self-test: {:#010X}", ret);
28 },
29 )
30 }
31
32 // Missing function: incremental_self_test
33
34 /// Get the TPM self test result
35 ///
36 /// # Details
37 /// The first parameter returned is a buffer with manufacturer-specific information.
38 ///
39 /// The second parameter returned by the method is an indicator of how the
40 /// test went in the form a [Result].
41 ///
42 /// If testing of all functions is complete without functional failures then Ok(())
43 /// or else a `TssError` (see [Error](crate::error::Error)) is returned.
44 ///
45 /// - A [TpmFormatZeroWarningResponseCode](crate::error::TpmFormatZeroWarningResponseCode) with a `Testing`
46 /// [TpmFormatZeroWarning](crate::constants::return_code::TpmFormatZeroWarning) indicates that the test
47 /// are not complete.
48 ///
49 /// - A [TpmFormatZeroErrorResponseCode](crate::error::TpmFormatZeroErrorResponseCode) with a `NeedsTest`
50 /// [TpmFormatZeroError](crate::constants::return_code::TpmFormatZeroError) indicates that no self test
51 /// has been performed and testable function has not been tested.
52 ///
53 /// - A [TpmFormatZeroErrorResponseCode](crate::error::TpmFormatZeroErrorResponseCode) with a `Failure`
54 /// [TpmFormatZeroError](crate::constants::return_code::TpmFormatZeroError) indicates that there was
55 /// a failure.
56 ///
57 /// See [Part 3, Commands](https://trustedcomputinggroup.org/wp-content/uploads/TCG_TPM2_r1p59_Part3_Commands_pub.pdf).
58 pub fn get_test_result(&mut self) -> Result<(MaxBuffer, Result<()>)> {
59 let mut out_data_ptr = null_mut();
60 let mut test_result: u32 = 0;
61
62 ReturnCode::ensure_success(
63 unsafe {
64 Esys_GetTestResult(
65 self.mut_context(),
66 self.optional_session_1(),
67 self.optional_session_2(),
68 self.optional_session_3(),
69 &mut out_data_ptr,
70 &mut test_result,
71 )
72 },
73 |ret| {
74 error!("Error getting test result: {:#010X}", ret);
75 },
76 )?;
77 Ok((
78 MaxBuffer::try_from(Context::ffi_data_to_owned(out_data_ptr)?)?,
79 ReturnCode::ensure_success(test_result, |_| {}),
80 ))
81 }
82}