diff --git a/ec/Cargo.lock b/ec/Cargo.lock index 36fa8f3..1a35313 100644 --- a/ec/Cargo.lock +++ b/ec/Cargo.lock @@ -491,6 +491,7 @@ dependencies = [ "num_enum", "scopeguard", "serialport", + "smbus-pec", "thermal-service-messages", "time-alarm-service-messages", "uuid", diff --git a/ec/test-lib/Cargo.toml b/ec/test-lib/Cargo.toml index 5be842f..195758c 100644 --- a/ec/test-lib/Cargo.toml +++ b/ec/test-lib/Cargo.toml @@ -29,6 +29,7 @@ scopeguard = { version = "1.2" } # Serial specific serialport = { version = "4.8.1" } +smbus-pec = "1.0.1" embedded-services = { git = "https://github.com/OpenDevicePartnership/embedded-services", branch = "v0.2.0" } thermal-service-messages = { git = "https://github.com/OpenDevicePartnership/embedded-services", branch = "v0.2.0" } diff --git a/ec/test-lib/src/serial.rs b/ec/test-lib/src/serial.rs index bca1d94..eb3cb92 100644 --- a/ec/test-lib/src/serial.rs +++ b/ec/test-lib/src/serial.rs @@ -184,6 +184,8 @@ impl Serial { .map_err(|e| Error::Io(format!("{e:?}")))?; port.write_all(&buffer[..HEADER_SZ + request_sz]) .map_err(|e| Error::Io(format!("{e:?}")))?; + let pec = smbus_pec::pec(&buffer[..HEADER_SZ + request_sz]); + port.write_all(&[pec]).map_err(|e| Error::Io(format!("{e:?}")))?; port.flush().map_err(|e| Error::Io(format!("{e:?}")))?; // Read response packets @@ -207,6 +209,16 @@ impl Serial { .ok_or_else(|| Error::Protocol("Response does not fit in buffer".into()))?; port.read_exact(packet_slice).map_err(|e| Error::Io(format!("{e:?}")))?; + let mut pec_buf = [0u8; 1]; + port.read_exact(&mut pec_buf).map_err(|e| Error::Io(format!("{e:?}")))?; + let computed = smbus_pec::pec(&buffer[..SMBUS_HEADER_SZ + len]); + if pec_buf[0] != computed { + return Err(Error::Protocol(format!( + "PEC mismatch: received {:#04x}, computed {:#04x}", + pec_buf[0], computed + ))); + } + let flags = buffer[MCTP_FLAGS_IDX]; // If this is a SOM packet, skip ODP header (we don't use it) and grab the command code/discriminant