From 934910b94dad3a8372206542f085b55f0f101336 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Sat, 30 May 2026 15:37:59 -0700 Subject: [PATCH] tpm: refactor startup response check Extract the TPM2_Startup(CLEAR) response-code check so the accepted swtpm reset results can be covered directly. Assisted-by: Copilot:GPT-5.5 Signed-off-by: Wei Liu --- tpm/src/emulator.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tpm/src/emulator.rs b/tpm/src/emulator.rs index 5bea5a0bf..3b4fbf08f 100644 --- a/tpm/src/emulator.rs +++ b/tpm/src/emulator.rs @@ -27,6 +27,10 @@ const TPM2_STARTUP_CLEAR: [u8; 12] = [ 0x00, 0x00, // TPM_SU_CLEAR ]; +fn startup_response_is_ok(response_code: u32) -> bool { + response_code == TPM_SUCCESS || response_code == TPM_RC_INITIALIZE +} + /* capability flags returned by PTM_GET_CAPABILITY */ const PTM_CAP_INIT: u64 = 1; const PTM_CAP_SHUTDOWN: u64 = 1 << 1; @@ -520,7 +524,7 @@ impl Emulator { self.deliver_request(&mut cmd)?; let response_code = u32::from_be_bytes(buffer[6..10].try_into().unwrap()); - if response_code != TPM_SUCCESS && response_code != TPM_RC_INITIALIZE { + if !startup_response_is_ok(response_code) { return Err(Error::DeliverRequest(anyhow!( "TPM2_Startup(CLEAR) returned error code: {response_code:#X}" )));