tpm: rename established_flag to established_bit and match TCG semantics

The helper used to communicate the TPM Establishment bit between the
swtpm backend and the CRB device had inverted semantics:

    self.established_flag = est.resp.bit == 0;

so `established_flag == true` actually meant "*not* established". The
device-side call site then double-negated:

    if !self.emulator.get_established_flag() {
        val |= 0x1;  // tpmEstablished in TPM_LOC_STATE
    }

The end-to-end behaviour was correct but the boundary between the
swtpm-specific backend and the (TCG-spec defined) CRB device was hard
to follow and easy to misuse -- the now-removed pre-init check in
Emulator::new() was an example of that confusion (it errored out with
"TPM not in established state" precisely when the TPM *was*
established).

Per the TCG PC Client Platform TPM Profile (PTP) specification, bit 0
of TPM_LOC_STATE_x is `tpmEstablished`:

  * 0 = default state after a cold reset
  * 1 = a TPM2_Startup from Locality 3 or 4 has occurred

Rename the backend accessor to `get_established_bit()` and return the
bit value directly (true == 1, false == 0). The CRB device then simply
forwards the bit, with no inversion, which makes the spec mapping
obvious and removes swtpm-flavoured naming from the device layer.

No functional change.

Asissted-by: Copilot:GPT-5.5
Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
Wei Liu
2026-05-29 17:39:15 -07:00
parent 746b760f69
commit d834c85697
2 changed files with 20 additions and 11 deletions

View File

@@ -363,7 +363,10 @@ impl BusDevice for Tpm {
offset &= 0xff;
let mut val = self.regs[offset as usize];
if offset == CRB_LOC_STATE && !self.emulator.get_established_flag() {
// Per the TCG PC Client Platform TPM Profile (PTP) spec, bit 0
// of TPM_LOC_STATE_x is `tpmEstablished`. Reflect the live value
// from the backend rather than the (zero-initialised) shadow.
if offset == CRB_LOC_STATE && self.emulator.get_established_bit() {
val |= 0x1;
}

View File

@@ -75,8 +75,8 @@ pub struct Emulator {
caps: PtmCap, /* capabilities of the TPM */
control_socket: SocketDev,
data_fd: RawFd,
established_flag_cached: bool,
established_flag: bool,
established_bit_cached: bool,
established_bit: bool,
}
impl Emulator {
@@ -103,8 +103,8 @@ impl Emulator {
caps: 0,
control_socket: socket,
data_fd: -1,
established_flag_cached: false,
established_flag: false,
established_bit_cached: false,
established_bit: false,
};
emulator.prepare_data_fd()?;
@@ -322,11 +322,17 @@ impl Emulator {
Ok(())
}
pub fn get_established_flag(&mut self) -> bool {
/// Returns the value of the TPM Establishment bit as defined by the
/// TCG PC Client Platform TPM Profile (PTP) specification:
/// * `true` - a TPM2_Startup from Locality 3 or 4 has occurred
/// * `false` - default state after a cold reset
///
/// The value is cached on first successful query.
pub fn get_established_bit(&mut self) -> bool {
let mut est: PtmEst = PtmEst::new();
if self.established_flag_cached {
return self.established_flag;
if self.established_bit_cached {
return self.established_bit;
}
if let Err(e) = self.run_control_cmd(
@@ -339,10 +345,10 @@ impl Emulator {
return false;
}
self.established_flag_cached = true;
self.established_flag = est.resp.bit == 0;
self.established_bit_cached = true;
self.established_bit = est.resp.bit != 0;
self.established_flag
self.established_bit
}
/// Function to write to data socket and read the response from it