From 4d32890c97f08e90021909c41e18f051b5acaacb Mon Sep 17 00:00:00 2001 From: "Yang, Longlong" Date: Sun, 24 Dec 2023 18:16:10 -0500 Subject: [PATCH] optimize stack usage. fix #186 Signed-off-by: Yang, Longlong --- fuzz-target/requester/vendor_req/src/main.rs | 10 +- idekm/src/pci_ide_km_requester/mod.rs | 7 +- .../pci_ide_km_req_key_prog.rs | 30 ++- .../pci_ide_km_req_key_set_go.rs | 31 ++- .../pci_ide_km_req_key_set_stop.rs | 31 ++- .../pci_ide_km_req_query.rs | 26 +-- spdmlib/src/message/vendor.rs | 18 ++ spdmlib/src/requester/vendor_req.rs | 20 +- tdisp/src/pci_tdisp_requester/mod.rs | 6 + .../pci_tdisp_req_bind_p2p_stream_request.rs | 138 ++++++------ ...i_tdisp_req_get_device_interface_report.rs | 196 +++++++++--------- ...ci_tdisp_req_get_device_interface_state.rs | 118 +++++------ .../pci_tdisp_req_get_tdisp_capabilities.rs | 147 ++++++------- .../pci_tdisp_req_get_tdisp_version.rs | 145 ++++++------- .../pci_tdisp_req_lock_interface_request.rs | 158 +++++++------- ...ci_tdisp_req_set_mmio_attribute_request.rs | 138 ++++++------ .../pci_tdisp_req_start_interface_request.rs | 138 ++++++------ .../pci_tdisp_req_stop_interface_request.rs | 110 +++++----- ...pci_tdisp_req_unbind_p2p_stream_request.rs | 138 ++++++------ .../pci_tdisp_req_vdm_request.rs | 81 ++++---- test/spdm-requester-emu/src/main.rs | 184 ++++++++-------- 21 files changed, 976 insertions(+), 894 deletions(-) diff --git a/fuzz-target/requester/vendor_req/src/main.rs b/fuzz-target/requester/vendor_req/src/main.rs index 8c59f323..39476161 100644 --- a/fuzz-target/requester/vendor_req/src/main.rs +++ b/fuzz-target/requester/vendor_req/src/main.rs @@ -46,8 +46,16 @@ async fn fuzz_send_spdm_vendor_defined_request(fuzzdata: Arc>) { vendor_defined_req_payload: [0u8; config::MAX_SPDM_MSG_SIZE - 7 - 2], }; + let mut vendor_defined_rsp_payload_struct = + spdmlib::message::VendorDefinedRspPayloadStruct::default(); let _ = requester - .send_spdm_vendor_defined_request(None, standard_id, vendor_idstruct, req_payload_struct) + .send_spdm_vendor_defined_request( + None, + standard_id, + vendor_idstruct, + &req_payload_struct, + &mut vendor_defined_rsp_payload_struct, + ) .await .is_ok(); } diff --git a/idekm/src/pci_ide_km_requester/mod.rs b/idekm/src/pci_ide_km_requester/mod.rs index e5489f40..70a0816f 100644 --- a/idekm/src/pci_ide_km_requester/mod.rs +++ b/idekm/src/pci_ide_km_requester/mod.rs @@ -2,8 +2,11 @@ // // SPDX-License-Identifier: Apache-2.0 or MIT -#[derive(Debug, Default, Copy, Clone)] -pub struct IdekmReqContext; +#[derive(Debug, Default, Clone)] +pub struct IdekmReqContext { + pub vendor_defined_req_payload_struct: spdmlib::message::VendorDefinedReqPayloadStruct, + pub vendor_defined_rsp_payload_struct: spdmlib::message::VendorDefinedRspPayloadStruct, +} pub mod pci_ide_km_req_query; pub use pci_ide_km_req_query::*; diff --git a/idekm/src/pci_ide_km_requester/pci_ide_km_req_key_prog.rs b/idekm/src/pci_ide_km_requester/pci_ide_km_req_key_prog.rs index 105e6351..034bd2b0 100644 --- a/idekm/src/pci_ide_km_requester/pci_ide_km_req_key_prog.rs +++ b/idekm/src/pci_ide_km_requester/pci_ide_km_req_key_prog.rs @@ -7,10 +7,7 @@ use codec::Writer; use spdmlib::error::SpdmResult; use spdmlib::error::SPDM_STATUS_BUFFER_FULL; use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; +use spdmlib::requester::RequesterContext; use crate::pci_idekm::vendor_id; use crate::pci_idekm::KpAckDataObject; @@ -36,15 +33,13 @@ impl IdekmReqContext { // OUT kp_ack_status: &mut KpAckStatus, ) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); - - vendor_defined_req_payload_struct.req_length = KeyProgDataObject { + self.vendor_defined_req_payload_struct.req_length = KeyProgDataObject { stream_id, key_set, key_direction, @@ -56,18 +51,21 @@ impl IdekmReqContext { .map_err(|_| SPDM_STATUS_BUFFER_FULL)? as u16; - let vendor_defined_rsp_payload_struct = spdm_requester + spdm_requester .send_spdm_vendor_defined_request( Some(session_id), STANDARD_ID, vendor_id(), - vendor_defined_req_payload_struct, + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, ) .await?; let kp_ack_data_object = KpAckDataObject::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], ) .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; diff --git a/idekm/src/pci_ide_km_requester/pci_ide_km_req_key_set_go.rs b/idekm/src/pci_ide_km_requester/pci_ide_km_req_key_set_go.rs index 60ecd4ad..bd19c743 100644 --- a/idekm/src/pci_ide_km_requester/pci_ide_km_req_key_set_go.rs +++ b/idekm/src/pci_ide_km_requester/pci_ide_km_req_key_set_go.rs @@ -6,11 +6,7 @@ use codec::Codec; use codec::Writer; use spdmlib::error::SPDM_STATUS_BUFFER_FULL; use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - error::SpdmResult, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; +use spdmlib::{error::SpdmResult, requester::RequesterContext}; use crate::pci_idekm::vendor_id; use crate::pci_idekm::KGoStopAckDataObject; @@ -33,15 +29,13 @@ impl IdekmReqContext { key_sub_stream: u8, port_index: u8, ) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); - - vendor_defined_req_payload_struct.req_length = KSetGoDataObject { + self.vendor_defined_req_payload_struct.req_length = KSetGoDataObject { stream_id, key_set, key_direction, @@ -52,18 +46,21 @@ impl IdekmReqContext { .map_err(|_| SPDM_STATUS_BUFFER_FULL)? as u16; - let vendor_defined_rsp_payload_struct = spdm_requester + spdm_requester .send_spdm_vendor_defined_request( Some(session_id), STANDARD_ID, vendor_id(), - vendor_defined_req_payload_struct, + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, ) .await?; let kgo_stop_ack_data_object = KGoStopAckDataObject::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], ) .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; diff --git a/idekm/src/pci_ide_km_requester/pci_ide_km_req_key_set_stop.rs b/idekm/src/pci_ide_km_requester/pci_ide_km_req_key_set_stop.rs index 428f2346..04624b56 100644 --- a/idekm/src/pci_ide_km_requester/pci_ide_km_req_key_set_stop.rs +++ b/idekm/src/pci_ide_km_requester/pci_ide_km_req_key_set_stop.rs @@ -6,11 +6,7 @@ use codec::Codec; use codec::Writer; use spdmlib::error::SPDM_STATUS_BUFFER_FULL; use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - error::SpdmResult, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; +use spdmlib::{error::SpdmResult, requester::RequesterContext}; use crate::pci_idekm::vendor_id; use crate::pci_idekm::KGoStopAckDataObject; @@ -33,15 +29,13 @@ impl IdekmReqContext { key_sub_stream: u8, port_index: u8, ) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); - - vendor_defined_req_payload_struct.req_length = KSetStopDataObject { + self.vendor_defined_req_payload_struct.req_length = KSetStopDataObject { stream_id, key_set, key_direction, @@ -52,18 +46,21 @@ impl IdekmReqContext { .map_err(|_| SPDM_STATUS_BUFFER_FULL)? as u16; - let vendor_defined_rsp_payload_struct = spdm_requester + spdm_requester .send_spdm_vendor_defined_request( Some(session_id), STANDARD_ID, vendor_id(), - vendor_defined_req_payload_struct, + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, ) .await?; let kgo_stop_ack_data_object = KGoStopAckDataObject::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], ) .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; diff --git a/idekm/src/pci_ide_km_requester/pci_ide_km_req_query.rs b/idekm/src/pci_ide_km_requester/pci_ide_km_req_query.rs index cead6319..070f0de9 100644 --- a/idekm/src/pci_ide_km_requester/pci_ide_km_req_query.rs +++ b/idekm/src/pci_ide_km_requester/pci_ide_km_req_query.rs @@ -5,7 +5,6 @@ use codec::{Codec, Writer}; use spdmlib::{ error::{SpdmResult, SPDM_STATUS_BUFFER_FULL, SPDM_STATUS_INVALID_MSG_FIELD}, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, requester::RequesterContext, }; @@ -33,31 +32,32 @@ impl IdekmReqContext { ide_reg_block: &mut [u32; PCI_IDE_KM_IDE_REG_BLOCK_MAX_COUNT], ide_reg_block_cnt: &mut usize, ) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); - - vendor_defined_req_payload_struct.req_length = QueryDataObject { port_index } + self.vendor_defined_req_payload_struct.req_length = QueryDataObject { port_index } .encode(&mut writer) .map_err(|_| SPDM_STATUS_BUFFER_FULL)? as u16; - let vendor_defined_rsp_payload_struct = spdm_requester + spdm_requester .send_spdm_vendor_defined_request( Some(session_id), STANDARD_ID, vendor_id(), - vendor_defined_req_payload_struct, + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, ) .await?; let query_resp_data_object = QueryRespDataObject::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], ) .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; diff --git a/spdmlib/src/message/vendor.rs b/spdmlib/src/message/vendor.rs index adef2230..11b0bfe4 100644 --- a/spdmlib/src/message/vendor.rs +++ b/spdmlib/src/message/vendor.rs @@ -138,6 +138,15 @@ impl Codec for VendorDefinedReqPayloadStruct { } } +impl Default for VendorDefinedReqPayloadStruct { + fn default() -> Self { + Self { + req_length: 0, + vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], + } + } +} + #[derive(Debug, Clone, ZeroizeOnDrop)] pub struct VendorDefinedRspPayloadStruct { pub rsp_length: u16, @@ -174,6 +183,15 @@ impl Codec for VendorDefinedRspPayloadStruct { } } +impl Default for VendorDefinedRspPayloadStruct { + fn default() -> Self { + Self { + rsp_length: 0, + vendor_defined_rsp_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], + } + } +} + #[derive(Debug, Clone)] pub struct SpdmVendorDefinedRequestPayload { pub standard_id: RegistryOrStandardsBodyID, diff --git a/spdmlib/src/requester/vendor_req.rs b/spdmlib/src/requester/vendor_req.rs index f2b0202c..dfaa7107 100644 --- a/spdmlib/src/requester/vendor_req.rs +++ b/spdmlib/src/requester/vendor_req.rs @@ -13,8 +13,9 @@ impl RequesterContext { session_id: Option, standard_id: RegistryOrStandardsBodyID, vendor_id_struct: VendorIDStruct, - req_payload_struct: VendorDefinedReqPayloadStruct, - ) -> SpdmResult { + req_payload_struct: &VendorDefinedReqPayloadStruct, + rsp_payload_struct: &mut VendorDefinedRspPayloadStruct, + ) -> SpdmResult { info!("send vendor defined request\n"); self.common.reset_buffer_via_request_code( @@ -33,7 +34,7 @@ impl RequesterContext { SpdmVendorDefinedRequestPayload { standard_id, vendor_id: vendor_id_struct, - req_payload: req_payload_struct, + req_payload: req_payload_struct.clone(), }, ), }; @@ -48,14 +49,19 @@ impl RequesterContext { .receive_message(session_id, &mut receive_buffer, false) .await?; - self.handle_spdm_vendor_defined_respond(session_id, &receive_buffer[..receive_used]) + self.handle_spdm_vendor_defined_respond( + session_id, + &receive_buffer[..receive_used], + rsp_payload_struct, + ) } pub fn handle_spdm_vendor_defined_respond( &mut self, session_id: Option, receive_buffer: &[u8], - ) -> SpdmResult { + rsp_payload_struct: &mut VendorDefinedRspPayloadStruct, + ) -> SpdmResult { let mut reader = Reader::init(receive_buffer); match SpdmMessageHeader::read(&mut reader) { Some(message_header) => { @@ -69,7 +75,9 @@ impl RequesterContext { &mut reader, ) { Some(spdm_vendor_defined_response_payload) => { - Ok(spdm_vendor_defined_response_payload.rsp_payload) + *rsp_payload_struct = + spdm_vendor_defined_response_payload.rsp_payload; + Ok(()) } None => Err(SPDM_STATUS_INVALID_MSG_FIELD), } diff --git a/tdisp/src/pci_tdisp_requester/mod.rs b/tdisp/src/pci_tdisp_requester/mod.rs index ad5084c4..579de8b3 100644 --- a/tdisp/src/pci_tdisp_requester/mod.rs +++ b/tdisp/src/pci_tdisp_requester/mod.rs @@ -4,6 +4,12 @@ use crate::pci_tdisp::{InterfaceId, TdispVersion}; +#[derive(Debug, Default, Clone)] +pub struct TdispReqContext { + pub vendor_defined_req_payload_struct: spdmlib::message::VendorDefinedReqPayloadStruct, + pub vendor_defined_rsp_payload_struct: spdmlib::message::VendorDefinedRspPayloadStruct, +} + pub mod pci_tdisp_req_get_tdisp_version; pub use pci_tdisp_req_get_tdisp_version::*; diff --git a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_bind_p2p_stream_request.rs b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_bind_p2p_stream_request.rs index 4b8098a1..0668f77b 100644 --- a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_bind_p2p_stream_request.rs +++ b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_bind_p2p_stream_request.rs @@ -7,11 +7,7 @@ use codec::Writer; use spdmlib::error::SPDM_STATUS_BUFFER_FULL; use spdmlib::error::SPDM_STATUS_ERROR_PEER; use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - error::SpdmResult, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; +use spdmlib::{error::SpdmResult, requester::RequesterContext}; use crate::pci_tdisp::vendor_id; use crate::pci_tdisp::RspBindP2PStreamResponse; @@ -23,75 +19,83 @@ use crate::pci_tdisp::{ TdispRequestResponseCode, }; -#[maybe_async::maybe_async] -pub async fn pci_tdisp_req_bind_p2p_stream_request( - // IN - spdm_requester: &mut RequesterContext, - session_id: u32, - interface_id: InterfaceId, - p2p_stream_id: u8, - // OUT - tdisp_error_code: &mut Option, -) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; +use super::TdispReqContext; - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); +impl TdispReqContext { + #[maybe_async::maybe_async] + pub async fn pci_tdisp_req_bind_p2p_stream_request( + &mut self, + // IN + spdm_requester: &mut RequesterContext, + session_id: u32, + interface_id: InterfaceId, + p2p_stream_id: u8, + // OUT + tdisp_error_code: &mut Option, + ) -> SpdmResult { + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - vendor_defined_req_payload_struct.req_length = ReqBindP2PStreamRequest { - message_header: TdispMessageHeader { - interface_id, - message_type: TdispRequestResponseCode::BIND_P2P_STREAM_REQUEST, - tdisp_version: TdispVersion { - major_version: 1, - minor_version: 0, + self.vendor_defined_req_payload_struct.req_length = ReqBindP2PStreamRequest { + message_header: TdispMessageHeader { + interface_id, + message_type: TdispRequestResponseCode::BIND_P2P_STREAM_REQUEST, + tdisp_version: TdispVersion { + major_version: 1, + minor_version: 0, + }, }, - }, - p2p_stream_id, - } - .encode(&mut writer) - .map_err(|_| SPDM_STATUS_BUFFER_FULL)? - as u16; + p2p_stream_id, + } + .encode(&mut writer) + .map_err(|_| SPDM_STATUS_BUFFER_FULL)? + as u16; + + spdm_requester + .send_spdm_vendor_defined_request( + Some(session_id), + STANDARD_ID, + vendor_id(), + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, + ) + .await?; - let vendor_defined_rsp_payload_struct = spdm_requester - .send_spdm_vendor_defined_request( - Some(session_id), - STANDARD_ID, - vendor_id(), - vendor_defined_req_payload_struct, + if let Ok(tdisp_error) = RspTdispError::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], ) - .await?; + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD) + { + *tdisp_error_code = Some(tdisp_error.error_code); + return Err(SPDM_STATUS_ERROR_PEER); + } - if let Ok(tdisp_error) = RspTdispError::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD) - { - *tdisp_error_code = Some(tdisp_error.error_code); - return Err(SPDM_STATUS_ERROR_PEER); - } + let rsp_bind_p2_pstream_response = RspBindP2PStreamResponse::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], + ) + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; - let rsp_bind_p2_pstream_response = RspBindP2PStreamResponse::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; + if rsp_bind_p2_pstream_response.message_header.tdisp_version + != (TdispVersion { + major_version: 1, + minor_version: 0, + }) + || rsp_bind_p2_pstream_response.message_header.message_type + != TdispRequestResponseCode::BIND_P2P_STREAM_RESPONSE + || rsp_bind_p2_pstream_response.message_header.interface_id != interface_id + { + return Err(SPDM_STATUS_INVALID_MSG_FIELD); + } - if rsp_bind_p2_pstream_response.message_header.tdisp_version - != (TdispVersion { - major_version: 1, - minor_version: 0, - }) - || rsp_bind_p2_pstream_response.message_header.message_type - != TdispRequestResponseCode::BIND_P2P_STREAM_RESPONSE - || rsp_bind_p2_pstream_response.message_header.interface_id != interface_id - { - return Err(SPDM_STATUS_INVALID_MSG_FIELD); + Ok(()) } - - Ok(()) } diff --git a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_device_interface_report.rs b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_device_interface_report.rs index efa64944..94fc47e0 100644 --- a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_device_interface_report.rs +++ b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_device_interface_report.rs @@ -7,11 +7,7 @@ use codec::Writer; use spdmlib::error::SPDM_STATUS_BUFFER_FULL; use spdmlib::error::SPDM_STATUS_ERROR_PEER; use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - error::SpdmResult, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; +use spdmlib::{error::SpdmResult, requester::RequesterContext}; use crate::pci_tdisp::vendor_id; use crate::pci_tdisp::InterfaceId; @@ -26,108 +22,116 @@ use crate::pci_tdisp::MAX_PORTION_LENGTH; use crate::pci_tdisp::STANDARD_ID; use crate::pci_tdisp_requester::TdispVersion; -#[maybe_async::maybe_async] -pub async fn pci_tdisp_req_get_device_interface_report( - // IN - spdm_requester: &mut RequesterContext, - session_id: u32, - interface_id: InterfaceId, - // OUT - report: &mut [u8; MAX_DEVICE_REPORT_BUFFER], - report_size: &mut usize, - tdisp_error_code: &mut Option, -) -> SpdmResult { - let mut offset = 0u16; - let length = MAX_PORTION_LENGTH as u16; - let mut report_buffer_walker = 0usize; - *report_size = 0; +use super::TdispReqContext; - loop { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; +impl TdispReqContext { + #[maybe_async::maybe_async] + pub async fn pci_tdisp_req_get_device_interface_report( + &mut self, + // IN + spdm_requester: &mut RequesterContext, + session_id: u32, + interface_id: InterfaceId, + // OUT + report: &mut [u8; MAX_DEVICE_REPORT_BUFFER], + report_size: &mut usize, + tdisp_error_code: &mut Option, + ) -> SpdmResult { + let mut offset = 0u16; + let length = MAX_PORTION_LENGTH as u16; + let mut report_buffer_walker = 0usize; + *report_size = 0; - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); + loop { + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - vendor_defined_req_payload_struct.req_length = ReqGetDeviceInterfaceReport { - message_header: TdispMessageHeader { - interface_id, - message_type: TdispRequestResponseCode::GET_DEVICE_INTERFACE_REPORT, - tdisp_version: TdispVersion { - major_version: 1, - minor_version: 0, + self.vendor_defined_req_payload_struct.req_length = ReqGetDeviceInterfaceReport { + message_header: TdispMessageHeader { + interface_id, + message_type: TdispRequestResponseCode::GET_DEVICE_INTERFACE_REPORT, + tdisp_version: TdispVersion { + major_version: 1, + minor_version: 0, + }, }, - }, - offset, - length, - } - .encode(&mut writer) - .map_err(|_| SPDM_STATUS_BUFFER_FULL)? - as u16; + offset, + length, + } + .encode(&mut writer) + .map_err(|_| SPDM_STATUS_BUFFER_FULL)? + as u16; - let vendor_defined_rsp_payload_struct = spdm_requester - .send_spdm_vendor_defined_request( - Some(session_id), - STANDARD_ID, - vendor_id(), - vendor_defined_req_payload_struct, - ) - .await?; + spdm_requester + .send_spdm_vendor_defined_request( + Some(session_id), + STANDARD_ID, + vendor_id(), + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, + ) + .await?; - if let Ok(tdisp_error) = RspTdispError::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD) - { - *tdisp_error_code = Some(tdisp_error.error_code); - return Err(SPDM_STATUS_ERROR_PEER); - } + if let Ok(tdisp_error) = RspTdispError::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], + ) + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD) + { + *tdisp_error_code = Some(tdisp_error.error_code); + return Err(SPDM_STATUS_ERROR_PEER); + } - let rsp_device_interface_report = RspDeviceInterfaceReport::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; + let rsp_device_interface_report = RspDeviceInterfaceReport::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], + ) + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; - if rsp_device_interface_report.message_header.tdisp_version - != (TdispVersion { - major_version: 1, - minor_version: 0, - }) - || rsp_device_interface_report.message_header.message_type - != TdispRequestResponseCode::DEVICE_INTERFACE_REPORT - || rsp_device_interface_report.message_header.interface_id != interface_id - { - return Err(SPDM_STATUS_INVALID_MSG_FIELD); - } + if rsp_device_interface_report.message_header.tdisp_version + != (TdispVersion { + major_version: 1, + minor_version: 0, + }) + || rsp_device_interface_report.message_header.message_type + != TdispRequestResponseCode::DEVICE_INTERFACE_REPORT + || rsp_device_interface_report.message_header.interface_id != interface_id + { + return Err(SPDM_STATUS_INVALID_MSG_FIELD); + } - if report_buffer_walker - + rsp_device_interface_report.portion_length as usize - + rsp_device_interface_report.remainder_length as usize - > MAX_DEVICE_REPORT_BUFFER - { - return Err(SPDM_STATUS_BUFFER_FULL); - } + if report_buffer_walker + + rsp_device_interface_report.portion_length as usize + + rsp_device_interface_report.remainder_length as usize + > MAX_DEVICE_REPORT_BUFFER + { + return Err(SPDM_STATUS_BUFFER_FULL); + } - report[report_buffer_walker - ..report_buffer_walker + rsp_device_interface_report.portion_length as usize] - .copy_from_slice( - &rsp_device_interface_report.report - [..rsp_device_interface_report.portion_length as usize], - ); - report_buffer_walker += rsp_device_interface_report.portion_length as usize; + report[report_buffer_walker + ..report_buffer_walker + rsp_device_interface_report.portion_length as usize] + .copy_from_slice( + &rsp_device_interface_report.report + [..rsp_device_interface_report.portion_length as usize], + ); + report_buffer_walker += rsp_device_interface_report.portion_length as usize; - if rsp_device_interface_report.remainder_length != 0 { - offset += rsp_device_interface_report.portion_length; - continue; - } else { - *report_size = report_buffer_walker; - break; + if rsp_device_interface_report.remainder_length != 0 { + offset += rsp_device_interface_report.portion_length; + continue; + } else { + *report_size = report_buffer_walker; + break; + } } - } - Ok(()) + Ok(()) + } } diff --git a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_device_interface_state.rs b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_device_interface_state.rs index 8e97029f..5ffc34f6 100644 --- a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_device_interface_state.rs +++ b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_device_interface_state.rs @@ -6,11 +6,7 @@ use codec::Codec; use codec::Writer; use spdmlib::error::SPDM_STATUS_BUFFER_FULL; use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - error::SpdmResult, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; +use spdmlib::{error::SpdmResult, requester::RequesterContext}; use crate::pci_tdisp::vendor_id; use crate::pci_tdisp::InterfaceId; @@ -22,65 +18,71 @@ use crate::pci_tdisp::TdispRequestResponseCode; use crate::pci_tdisp::TdispVersion; use crate::pci_tdisp::STANDARD_ID; -#[maybe_async::maybe_async] -pub async fn pci_tdisp_req_get_device_interface_state( - // IN - spdm_requester: &mut RequesterContext, - session_id: u32, - interface_id: InterfaceId, - // OUT - tdi_state: &mut TdiState, -) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; +use super::TdispReqContext; - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); +impl TdispReqContext { + #[maybe_async::maybe_async] + pub async fn pci_tdisp_req_get_device_interface_state( + &mut self, + // IN + spdm_requester: &mut RequesterContext, + session_id: u32, + interface_id: InterfaceId, + // OUT + tdi_state: &mut TdiState, + ) -> SpdmResult { + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - vendor_defined_req_payload_struct.req_length = ReqGetDeviceInterfaceState { - message_header: TdispMessageHeader { - interface_id, - message_type: TdispRequestResponseCode::GET_DEVICE_INTERFACE_STATE, - tdisp_version: TdispVersion { - major_version: 1, - minor_version: 0, + self.vendor_defined_req_payload_struct.req_length = ReqGetDeviceInterfaceState { + message_header: TdispMessageHeader { + interface_id, + message_type: TdispRequestResponseCode::GET_DEVICE_INTERFACE_STATE, + tdisp_version: TdispVersion { + major_version: 1, + minor_version: 0, + }, }, - }, - } - .encode(&mut writer) - .map_err(|_| SPDM_STATUS_BUFFER_FULL)? - as u16; + } + .encode(&mut writer) + .map_err(|_| SPDM_STATUS_BUFFER_FULL)? + as u16; - let vendor_defined_rsp_payload_struct = spdm_requester - .send_spdm_vendor_defined_request( - Some(session_id), - STANDARD_ID, - vendor_id(), - vendor_defined_req_payload_struct, - ) - .await?; + spdm_requester + .send_spdm_vendor_defined_request( + Some(session_id), + STANDARD_ID, + vendor_id(), + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, + ) + .await?; - let rsp_device_interface_state = RspDeviceInterfaceState::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; + let rsp_device_interface_state = RspDeviceInterfaceState::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], + ) + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; - if rsp_device_interface_state.message_header.tdisp_version - != (TdispVersion { - major_version: 1, - minor_version: 0, - }) - || rsp_device_interface_state.message_header.message_type - != TdispRequestResponseCode::DEVICE_INTERFACE_STATE - || rsp_device_interface_state.message_header.interface_id != interface_id - { - return Err(SPDM_STATUS_INVALID_MSG_FIELD); - } + if rsp_device_interface_state.message_header.tdisp_version + != (TdispVersion { + major_version: 1, + minor_version: 0, + }) + || rsp_device_interface_state.message_header.message_type + != TdispRequestResponseCode::DEVICE_INTERFACE_STATE + || rsp_device_interface_state.message_header.interface_id != interface_id + { + return Err(SPDM_STATUS_INVALID_MSG_FIELD); + } - *tdi_state = rsp_device_interface_state.tdi_state; + *tdi_state = rsp_device_interface_state.tdi_state; - Ok(()) + Ok(()) + } } diff --git a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_tdisp_capabilities.rs b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_tdisp_capabilities.rs index 3b898382..0febeeb5 100644 --- a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_tdisp_capabilities.rs +++ b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_tdisp_capabilities.rs @@ -6,11 +6,7 @@ use codec::Codec; use codec::Writer; use spdmlib::error::SPDM_STATUS_BUFFER_FULL; use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - error::SpdmResult, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; +use spdmlib::{error::SpdmResult, requester::RequesterContext}; use crate::pci_tdisp::vendor_id; use crate::pci_tdisp::InterfaceId; @@ -22,78 +18,85 @@ use crate::pci_tdisp::TdispRequestResponseCode; use crate::pci_tdisp::TdispVersion; use crate::pci_tdisp::STANDARD_ID; -#[maybe_async::maybe_async] -#[allow(clippy::too_many_arguments)] -pub async fn pci_tdisp_req_get_tdisp_capabilities( - // IN - spdm_requester: &mut RequesterContext, - session_id: u32, - tsm_caps: u32, - interface_id: InterfaceId, - // OUT - dsm_caps: &mut u32, - lock_interface_flags_supported: &mut LockInterfaceFlag, - dev_addr_width: &mut u8, - num_req_this: &mut u8, - num_req_all: &mut u8, - req_msgs_supported: &mut [u8; 16], -) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; +use super::TdispReqContext; - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); +impl TdispReqContext { + #[maybe_async::maybe_async] + #[allow(clippy::too_many_arguments)] + pub async fn pci_tdisp_req_get_tdisp_capabilities( + &mut self, + // IN + spdm_requester: &mut RequesterContext, + session_id: u32, + tsm_caps: u32, + interface_id: InterfaceId, + // OUT + dsm_caps: &mut u32, + lock_interface_flags_supported: &mut LockInterfaceFlag, + dev_addr_width: &mut u8, + num_req_this: &mut u8, + num_req_all: &mut u8, + req_msgs_supported: &mut [u8; 16], + ) -> SpdmResult { + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - vendor_defined_req_payload_struct.req_length = ReqGetTdispCapabilities { - message_header: TdispMessageHeader { - interface_id, - message_type: TdispRequestResponseCode::GET_TDISP_CAPABILITIES, - tdisp_version: TdispVersion { - major_version: 1, - minor_version: 0, + self.vendor_defined_req_payload_struct.req_length = ReqGetTdispCapabilities { + message_header: TdispMessageHeader { + interface_id, + message_type: TdispRequestResponseCode::GET_TDISP_CAPABILITIES, + tdisp_version: TdispVersion { + major_version: 1, + minor_version: 0, + }, }, - }, - tsm_caps, - } - .encode(&mut writer) - .map_err(|_| SPDM_STATUS_BUFFER_FULL)? - as u16; - let vendor_defined_rsp_payload_struct = spdm_requester - .send_spdm_vendor_defined_request( - Some(session_id), - STANDARD_ID, - vendor_id(), - vendor_defined_req_payload_struct, - ) - .await?; + tsm_caps, + } + .encode(&mut writer) + .map_err(|_| SPDM_STATUS_BUFFER_FULL)? + as u16; - let rsp_tdisp_capabilities = RspTdispCapabilities::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; + spdm_requester + .send_spdm_vendor_defined_request( + Some(session_id), + STANDARD_ID, + vendor_id(), + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, + ) + .await?; - if rsp_tdisp_capabilities.message_header.tdisp_version - != (TdispVersion { - major_version: 1, - minor_version: 0, - }) - || rsp_tdisp_capabilities.message_header.message_type - != TdispRequestResponseCode::TDISP_CAPABILITIES - || rsp_tdisp_capabilities.message_header.interface_id != interface_id - { - return Err(SPDM_STATUS_INVALID_MSG_FIELD); - } + let rsp_tdisp_capabilities = RspTdispCapabilities::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], + ) + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; - *dsm_caps = rsp_tdisp_capabilities.dsm_caps; - req_msgs_supported.copy_from_slice(&rsp_tdisp_capabilities.req_msgs_supported); - *lock_interface_flags_supported = rsp_tdisp_capabilities.lock_interface_flags_supported; - *lock_interface_flags_supported = rsp_tdisp_capabilities.lock_interface_flags_supported; - *dev_addr_width = rsp_tdisp_capabilities.dev_addr_width; - *num_req_this = rsp_tdisp_capabilities.num_req_this; - *num_req_all = rsp_tdisp_capabilities.num_req_all; + if rsp_tdisp_capabilities.message_header.tdisp_version + != (TdispVersion { + major_version: 1, + minor_version: 0, + }) + || rsp_tdisp_capabilities.message_header.message_type + != TdispRequestResponseCode::TDISP_CAPABILITIES + || rsp_tdisp_capabilities.message_header.interface_id != interface_id + { + return Err(SPDM_STATUS_INVALID_MSG_FIELD); + } - Ok(()) + *dsm_caps = rsp_tdisp_capabilities.dsm_caps; + req_msgs_supported.copy_from_slice(&rsp_tdisp_capabilities.req_msgs_supported); + *lock_interface_flags_supported = rsp_tdisp_capabilities.lock_interface_flags_supported; + *lock_interface_flags_supported = rsp_tdisp_capabilities.lock_interface_flags_supported; + *dev_addr_width = rsp_tdisp_capabilities.dev_addr_width; + *num_req_this = rsp_tdisp_capabilities.num_req_this; + *num_req_all = rsp_tdisp_capabilities.num_req_all; + + Ok(()) + } } diff --git a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_tdisp_version.rs b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_tdisp_version.rs index 8dd6b87d..4917d0e2 100644 --- a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_tdisp_version.rs +++ b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_get_tdisp_version.rs @@ -1,71 +1,74 @@ -// Copyright (c) 2023 Intel Corporation -// -// SPDX-License-Identifier: Apache-2.0 or MIT - -use codec::Codec; -use codec::Writer; -use spdmlib::error::SPDM_STATUS_BUFFER_FULL; -use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - error::SpdmResult, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; - -use crate::pci_tdisp::vendor_id; -use crate::pci_tdisp::RspTdispVersion; -use crate::pci_tdisp::STANDARD_ID; -use crate::pci_tdisp::{ReqGetTdispVersion, TdispVersion}; -use crate::pci_tdisp_requester::InterfaceId; - -#[maybe_async::maybe_async] -pub async fn pci_tdisp_req_get_tdisp_version( - // IN - spdm_requester: &mut RequesterContext, - session_id: u32, - interface_id: InterfaceId, -) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; - - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); - - vendor_defined_req_payload_struct.req_length = ReqGetTdispVersion { interface_id } - .encode(&mut writer) - .map_err(|_| SPDM_STATUS_BUFFER_FULL)? - as u16; - - let vendor_defined_rsp_payload_struct = spdm_requester - .send_spdm_vendor_defined_request( - Some(session_id), - STANDARD_ID, - vendor_id(), - vendor_defined_req_payload_struct, - ) - .await?; - - let rsp_tdisp_version = RspTdispVersion::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; - - if rsp_tdisp_version.interface_id != interface_id { - return Err(SPDM_STATUS_INVALID_MSG_FIELD); - } - - if rsp_tdisp_version.version_num_count == 1 - && rsp_tdisp_version.version_num_entry[0] - == (TdispVersion { - major_version: 1, - minor_version: 0, - }) - { - Ok(()) - } else { - Err(SPDM_STATUS_INVALID_MSG_FIELD) - } -} +// Copyright (c) 2023 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 or MIT + +use codec::Codec; +use codec::Writer; +use spdmlib::error::SPDM_STATUS_BUFFER_FULL; +use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; +use spdmlib::{error::SpdmResult, requester::RequesterContext}; + +use crate::pci_tdisp::vendor_id; +use crate::pci_tdisp::RspTdispVersion; +use crate::pci_tdisp::STANDARD_ID; +use crate::pci_tdisp::{ReqGetTdispVersion, TdispVersion}; +use crate::pci_tdisp_requester::InterfaceId; + +use super::TdispReqContext; + +impl TdispReqContext { + #[maybe_async::maybe_async] + pub async fn pci_tdisp_req_get_tdisp_version( + &mut self, + // IN + spdm_requester: &mut RequesterContext, + session_id: u32, + interface_id: InterfaceId, + // OUT + ) -> SpdmResult { + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); + + self.vendor_defined_req_payload_struct.req_length = ReqGetTdispVersion { interface_id } + .encode(&mut writer) + .map_err(|_| SPDM_STATUS_BUFFER_FULL)? + as u16; + + spdm_requester + .send_spdm_vendor_defined_request( + Some(session_id), + STANDARD_ID, + vendor_id(), + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, + ) + .await?; + + let rsp_tdisp_version = RspTdispVersion::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], + ) + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; + + if rsp_tdisp_version.interface_id != interface_id { + return Err(SPDM_STATUS_INVALID_MSG_FIELD); + } + + if rsp_tdisp_version.version_num_count == 1 + && rsp_tdisp_version.version_num_entry[0] + == (TdispVersion { + major_version: 1, + minor_version: 0, + }) + { + Ok(()) + } else { + Err(SPDM_STATUS_INVALID_MSG_FIELD) + } + } +} diff --git a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_lock_interface_request.rs b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_lock_interface_request.rs index a5da8d98..e9670753 100644 --- a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_lock_interface_request.rs +++ b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_lock_interface_request.rs @@ -7,11 +7,7 @@ use codec::Writer; use spdmlib::error::SPDM_STATUS_BUFFER_FULL; use spdmlib::error::SPDM_STATUS_ERROR_PEER; use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - error::SpdmResult, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; +use spdmlib::{error::SpdmResult, requester::RequesterContext}; use crate::pci_tdisp::vendor_id; use crate::pci_tdisp::InterfaceId; @@ -26,85 +22,93 @@ use crate::pci_tdisp::STANDARD_ID; use crate::pci_tdisp::START_INTERFACE_NONCE_LEN; use crate::pci_tdisp_requester::TdispVersion; -#[allow(clippy::too_many_arguments)] -#[maybe_async::maybe_async] -pub async fn pci_tdisp_req_lock_interface_request( - // IN - spdm_requester: &mut RequesterContext, - session_id: u32, - interface_id: InterfaceId, - flags: LockInterfaceFlag, - default_stream_id: u8, - mmio_reporting_offset: u64, - bind_p2p_address_mask: u64, - // OUT - start_interface_nonce: &mut [u8; START_INTERFACE_NONCE_LEN], - tdisp_error_code: &mut Option, -) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; +use super::TdispReqContext; - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); +impl TdispReqContext { + #[allow(clippy::too_many_arguments)] + #[maybe_async::maybe_async] + pub async fn pci_tdisp_req_lock_interface_request( + &mut self, + // IN + spdm_requester: &mut RequesterContext, + session_id: u32, + interface_id: InterfaceId, + flags: LockInterfaceFlag, + default_stream_id: u8, + mmio_reporting_offset: u64, + bind_p2p_address_mask: u64, + // OUT + start_interface_nonce: &mut [u8; START_INTERFACE_NONCE_LEN], + tdisp_error_code: &mut Option, + ) -> SpdmResult { + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - vendor_defined_req_payload_struct.req_length = ReqLockInterfaceRequest { - message_header: TdispMessageHeader { - interface_id, - message_type: TdispRequestResponseCode::LOCK_INTERFACE_REQUEST, - tdisp_version: TdispVersion { - major_version: 1, - minor_version: 0, + self.vendor_defined_req_payload_struct.req_length = ReqLockInterfaceRequest { + message_header: TdispMessageHeader { + interface_id, + message_type: TdispRequestResponseCode::LOCK_INTERFACE_REQUEST, + tdisp_version: TdispVersion { + major_version: 1, + minor_version: 0, + }, }, - }, - flags, - default_stream_id, - mmio_reporting_offset, - bind_p2p_address_mask, - } - .encode(&mut writer) - .map_err(|_| SPDM_STATUS_BUFFER_FULL)? - as u16; + flags, + default_stream_id, + mmio_reporting_offset, + bind_p2p_address_mask, + } + .encode(&mut writer) + .map_err(|_| SPDM_STATUS_BUFFER_FULL)? + as u16; - let vendor_defined_rsp_payload_struct = spdm_requester - .send_spdm_vendor_defined_request( - Some(session_id), - STANDARD_ID, - vendor_id(), - vendor_defined_req_payload_struct, - ) - .await?; + spdm_requester + .send_spdm_vendor_defined_request( + Some(session_id), + STANDARD_ID, + vendor_id(), + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, + ) + .await?; - if let Ok(tdisp_error) = RspTdispError::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD) - { - *tdisp_error_code = Some(tdisp_error.error_code); - return Err(SPDM_STATUS_ERROR_PEER); - } + if let Ok(tdisp_error) = RspTdispError::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], + ) + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD) + { + *tdisp_error_code = Some(tdisp_error.error_code); + return Err(SPDM_STATUS_ERROR_PEER); + } - let rsp_lock_interface_response = RspLockInterfaceResponse::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; + let rsp_lock_interface_response = RspLockInterfaceResponse::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], + ) + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; - if rsp_lock_interface_response.message_header.tdisp_version - != (TdispVersion { - major_version: 1, - minor_version: 0, - }) - || rsp_lock_interface_response.message_header.message_type - != TdispRequestResponseCode::LOCK_INTERFACE_RESPONSE - || rsp_lock_interface_response.message_header.interface_id != interface_id - { - return Err(SPDM_STATUS_INVALID_MSG_FIELD); - } + if rsp_lock_interface_response.message_header.tdisp_version + != (TdispVersion { + major_version: 1, + minor_version: 0, + }) + || rsp_lock_interface_response.message_header.message_type + != TdispRequestResponseCode::LOCK_INTERFACE_RESPONSE + || rsp_lock_interface_response.message_header.interface_id != interface_id + { + return Err(SPDM_STATUS_INVALID_MSG_FIELD); + } - start_interface_nonce.copy_from_slice(&rsp_lock_interface_response.start_interface_nonce); + start_interface_nonce.copy_from_slice(&rsp_lock_interface_response.start_interface_nonce); - Ok(()) + Ok(()) + } } diff --git a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_set_mmio_attribute_request.rs b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_set_mmio_attribute_request.rs index 9aa6e609..1e51aa67 100644 --- a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_set_mmio_attribute_request.rs +++ b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_set_mmio_attribute_request.rs @@ -7,11 +7,7 @@ use codec::Writer; use spdmlib::error::SPDM_STATUS_BUFFER_FULL; use spdmlib::error::SPDM_STATUS_ERROR_PEER; use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - error::SpdmResult, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; +use spdmlib::{error::SpdmResult, requester::RequesterContext}; use crate::pci_tdisp::vendor_id; use crate::pci_tdisp::ReqSetMmioAttributeRequest; @@ -23,75 +19,83 @@ use crate::pci_tdisp::{ InterfaceId, TdispErrorCode, TdispMessageHeader, TdispRequestResponseCode, TdispVersion, }; -#[maybe_async::maybe_async] -pub async fn pci_tdisp_req_set_mmio_attribute_request( - // IN - spdm_requester: &mut RequesterContext, - session_id: u32, - interface_id: InterfaceId, - mmio_range: TdispMmioRange, - // OUT - tdisp_error_code: &mut Option, -) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; +use super::TdispReqContext; - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); +impl TdispReqContext { + #[maybe_async::maybe_async] + pub async fn pci_tdisp_req_set_mmio_attribute_request( + &mut self, + // IN + spdm_requester: &mut RequesterContext, + session_id: u32, + interface_id: InterfaceId, + mmio_range: TdispMmioRange, + // OUT + tdisp_error_code: &mut Option, + ) -> SpdmResult { + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - vendor_defined_req_payload_struct.req_length = ReqSetMmioAttributeRequest { - message_header: TdispMessageHeader { - interface_id, - message_type: TdispRequestResponseCode::SET_MMIO_ATTRIBUTE_REQUEST, - tdisp_version: TdispVersion { - major_version: 1, - minor_version: 0, + self.vendor_defined_req_payload_struct.req_length = ReqSetMmioAttributeRequest { + message_header: TdispMessageHeader { + interface_id, + message_type: TdispRequestResponseCode::SET_MMIO_ATTRIBUTE_REQUEST, + tdisp_version: TdispVersion { + major_version: 1, + minor_version: 0, + }, }, - }, - mmio_range, - } - .encode(&mut writer) - .map_err(|_| SPDM_STATUS_BUFFER_FULL)? - as u16; + mmio_range, + } + .encode(&mut writer) + .map_err(|_| SPDM_STATUS_BUFFER_FULL)? + as u16; + + spdm_requester + .send_spdm_vendor_defined_request( + Some(session_id), + STANDARD_ID, + vendor_id(), + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, + ) + .await?; - let vendor_defined_rsp_payload_struct = spdm_requester - .send_spdm_vendor_defined_request( - Some(session_id), - STANDARD_ID, - vendor_id(), - vendor_defined_req_payload_struct, + if let Ok(tdisp_error) = RspTdispError::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], ) - .await?; + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD) + { + *tdisp_error_code = Some(tdisp_error.error_code); + return Err(SPDM_STATUS_ERROR_PEER); + } - if let Ok(tdisp_error) = RspTdispError::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD) - { - *tdisp_error_code = Some(tdisp_error.error_code); - return Err(SPDM_STATUS_ERROR_PEER); - } + let rsp_set_mmio_attribute_response = RspSetMmioAttributeResponse::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], + ) + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; - let rsp_set_mmio_attribute_response = RspSetMmioAttributeResponse::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; + if rsp_set_mmio_attribute_response.message_header.tdisp_version + != (TdispVersion { + major_version: 1, + minor_version: 0, + }) + || rsp_set_mmio_attribute_response.message_header.message_type + != TdispRequestResponseCode::SET_MMIO_ATTRIBUTE_RESPONSE + || rsp_set_mmio_attribute_response.message_header.interface_id != interface_id + { + return Err(SPDM_STATUS_INVALID_MSG_FIELD); + } - if rsp_set_mmio_attribute_response.message_header.tdisp_version - != (TdispVersion { - major_version: 1, - minor_version: 0, - }) - || rsp_set_mmio_attribute_response.message_header.message_type - != TdispRequestResponseCode::SET_MMIO_ATTRIBUTE_RESPONSE - || rsp_set_mmio_attribute_response.message_header.interface_id != interface_id - { - return Err(SPDM_STATUS_INVALID_MSG_FIELD); + Ok(()) } - - Ok(()) } diff --git a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_start_interface_request.rs b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_start_interface_request.rs index deadcafa..ceeef1ef 100644 --- a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_start_interface_request.rs +++ b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_start_interface_request.rs @@ -7,11 +7,7 @@ use codec::Writer; use spdmlib::error::SPDM_STATUS_BUFFER_FULL; use spdmlib::error::SPDM_STATUS_ERROR_PEER; use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - error::SpdmResult, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; +use spdmlib::{error::SpdmResult, requester::RequesterContext}; use crate::pci_tdisp::vendor_id; use crate::pci_tdisp::InterfaceId; @@ -25,75 +21,83 @@ use crate::pci_tdisp::TdispVersion; use crate::pci_tdisp::STANDARD_ID; use crate::pci_tdisp::START_INTERFACE_NONCE_LEN; -#[maybe_async::maybe_async] -pub async fn pci_tdisp_req_start_interface_request( - // IN - spdm_requester: &mut RequesterContext, - session_id: u32, - interface_id: InterfaceId, - start_interface_nonce: &[u8; START_INTERFACE_NONCE_LEN], - // OUT - tdisp_error_code: &mut Option, -) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; +use super::TdispReqContext; - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); +impl TdispReqContext { + #[maybe_async::maybe_async] + pub async fn pci_tdisp_req_start_interface_request( + &mut self, + // IN + spdm_requester: &mut RequesterContext, + session_id: u32, + interface_id: InterfaceId, + start_interface_nonce: &[u8; START_INTERFACE_NONCE_LEN], + // OUT + tdisp_error_code: &mut Option, + ) -> SpdmResult { + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - vendor_defined_req_payload_struct.req_length = ReqStartInterfaceRequest { - message_header: TdispMessageHeader { - interface_id, - message_type: TdispRequestResponseCode::START_INTERFACE_REQUEST, - tdisp_version: TdispVersion { - major_version: 1, - minor_version: 0, + self.vendor_defined_req_payload_struct.req_length = ReqStartInterfaceRequest { + message_header: TdispMessageHeader { + interface_id, + message_type: TdispRequestResponseCode::START_INTERFACE_REQUEST, + tdisp_version: TdispVersion { + major_version: 1, + minor_version: 0, + }, }, - }, - start_interface_nonce: *start_interface_nonce, - } - .encode(&mut writer) - .map_err(|_| SPDM_STATUS_BUFFER_FULL)? - as u16; + start_interface_nonce: *start_interface_nonce, + } + .encode(&mut writer) + .map_err(|_| SPDM_STATUS_BUFFER_FULL)? + as u16; - let vendor_defined_rsp_payload_struct = spdm_requester - .send_spdm_vendor_defined_request( - Some(session_id), - STANDARD_ID, - vendor_id(), - vendor_defined_req_payload_struct, + spdm_requester + .send_spdm_vendor_defined_request( + Some(session_id), + STANDARD_ID, + vendor_id(), + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, + ) + .await?; + + if let Ok(tdisp_error) = RspTdispError::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], ) - .await?; + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD) + { + *tdisp_error_code = Some(tdisp_error.error_code); + return Err(SPDM_STATUS_ERROR_PEER); + } - if let Ok(tdisp_error) = RspTdispError::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD) - { - *tdisp_error_code = Some(tdisp_error.error_code); - return Err(SPDM_STATUS_ERROR_PEER); - } + let rsp_start_interface_response = RspStartInterfaceResponse::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], + ) + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; - let rsp_start_interface_response = RspStartInterfaceResponse::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; + if rsp_start_interface_response.message_header.tdisp_version + != (TdispVersion { + major_version: 1, + minor_version: 0, + }) + || rsp_start_interface_response.message_header.message_type + != TdispRequestResponseCode::START_INTERFACE_RESPONSE + || rsp_start_interface_response.message_header.interface_id != interface_id + { + return Err(SPDM_STATUS_INVALID_MSG_FIELD); + } - if rsp_start_interface_response.message_header.tdisp_version - != (TdispVersion { - major_version: 1, - minor_version: 0, - }) - || rsp_start_interface_response.message_header.message_type - != TdispRequestResponseCode::START_INTERFACE_RESPONSE - || rsp_start_interface_response.message_header.interface_id != interface_id - { - return Err(SPDM_STATUS_INVALID_MSG_FIELD); + Ok(()) } - - Ok(()) } diff --git a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_stop_interface_request.rs b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_stop_interface_request.rs index 1b804c52..411c28ba 100644 --- a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_stop_interface_request.rs +++ b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_stop_interface_request.rs @@ -6,11 +6,7 @@ use codec::Codec; use codec::Writer; use spdmlib::error::SPDM_STATUS_BUFFER_FULL; use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - error::SpdmResult, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; +use spdmlib::{error::SpdmResult, requester::RequesterContext}; use crate::pci_tdisp::vendor_id; use crate::pci_tdisp::InterfaceId; @@ -21,61 +17,67 @@ use crate::pci_tdisp::TdispRequestResponseCode; use crate::pci_tdisp::TdispVersion; use crate::pci_tdisp::STANDARD_ID; -#[maybe_async::maybe_async] -pub async fn pci_tdisp_req_stop_interface_request( - // IN - spdm_requester: &mut RequesterContext, - session_id: u32, - interface_id: InterfaceId, -) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; +use super::TdispReqContext; - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); +impl TdispReqContext { + #[maybe_async::maybe_async] + pub async fn pci_tdisp_req_stop_interface_request( + &mut self, + // IN + spdm_requester: &mut RequesterContext, + session_id: u32, + interface_id: InterfaceId, + ) -> SpdmResult { + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - vendor_defined_req_payload_struct.req_length = ReqStopInterfaceRequest { - message_header: TdispMessageHeader { - interface_id, - message_type: TdispRequestResponseCode::STOP_INTERFACE_REQUEST, - tdisp_version: TdispVersion { - major_version: 1, - minor_version: 0, + self.vendor_defined_req_payload_struct.req_length = ReqStopInterfaceRequest { + message_header: TdispMessageHeader { + interface_id, + message_type: TdispRequestResponseCode::STOP_INTERFACE_REQUEST, + tdisp_version: TdispVersion { + major_version: 1, + minor_version: 0, + }, }, - }, - } - .encode(&mut writer) - .map_err(|_| SPDM_STATUS_BUFFER_FULL)? - as u16; + } + .encode(&mut writer) + .map_err(|_| SPDM_STATUS_BUFFER_FULL)? + as u16; + + spdm_requester + .send_spdm_vendor_defined_request( + Some(session_id), + STANDARD_ID, + vendor_id(), + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, + ) + .await?; - let vendor_defined_rsp_payload_struct = spdm_requester - .send_spdm_vendor_defined_request( - Some(session_id), - STANDARD_ID, - vendor_id(), - vendor_defined_req_payload_struct, + let rsp_stop_interface_response = RspStopInterfaceResponse::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], ) - .await?; + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; - let rsp_stop_interface_response = RspStopInterfaceResponse::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; + if rsp_stop_interface_response.message_header.tdisp_version + != (TdispVersion { + major_version: 1, + minor_version: 0, + }) + || rsp_stop_interface_response.message_header.message_type + != TdispRequestResponseCode::STOP_INTERFACE_RESPONSE + || rsp_stop_interface_response.message_header.interface_id != interface_id + { + return Err(SPDM_STATUS_INVALID_MSG_FIELD); + } - if rsp_stop_interface_response.message_header.tdisp_version - != (TdispVersion { - major_version: 1, - minor_version: 0, - }) - || rsp_stop_interface_response.message_header.message_type - != TdispRequestResponseCode::STOP_INTERFACE_RESPONSE - || rsp_stop_interface_response.message_header.interface_id != interface_id - { - return Err(SPDM_STATUS_INVALID_MSG_FIELD); + Ok(()) } - - Ok(()) } diff --git a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_unbind_p2p_stream_request.rs b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_unbind_p2p_stream_request.rs index ce1e6d96..364bd1a1 100644 --- a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_unbind_p2p_stream_request.rs +++ b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_unbind_p2p_stream_request.rs @@ -7,11 +7,7 @@ use codec::Writer; use spdmlib::error::SPDM_STATUS_BUFFER_FULL; use spdmlib::error::SPDM_STATUS_ERROR_PEER; use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD; -use spdmlib::{ - error::SpdmResult, - message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE}, - requester::RequesterContext, -}; +use spdmlib::{error::SpdmResult, requester::RequesterContext}; use crate::pci_tdisp::vendor_id; use crate::pci_tdisp::ReqUnBindP2PStreamRequest; @@ -22,75 +18,83 @@ use crate::pci_tdisp::{ InterfaceId, TdispErrorCode, TdispMessageHeader, TdispRequestResponseCode, TdispVersion, }; -#[maybe_async::maybe_async] -pub async fn pci_tdisp_req_unbind_p2p_stream_request( - // IN - spdm_requester: &mut RequesterContext, - session_id: u32, - interface_id: InterfaceId, - p2p_stream_id: u8, - // OUT - tdisp_error_code: &mut Option, -) -> SpdmResult { - let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct { - req_length: 0, - vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE], - }; +use super::TdispReqContext; - let mut writer = - Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload); +impl TdispReqContext { + #[maybe_async::maybe_async] + pub async fn pci_tdisp_req_unbind_p2p_stream_request( + &mut self, + // IN + spdm_requester: &mut RequesterContext, + session_id: u32, + interface_id: InterfaceId, + p2p_stream_id: u8, + // OUT + tdisp_error_code: &mut Option, + ) -> SpdmResult { + let mut writer = Writer::init( + &mut self + .vendor_defined_req_payload_struct + .vendor_defined_req_payload, + ); - vendor_defined_req_payload_struct.req_length = ReqUnBindP2PStreamRequest { - message_header: TdispMessageHeader { - interface_id, - message_type: TdispRequestResponseCode::UNBIND_P2P_STREAM_REQUEST, - tdisp_version: TdispVersion { - major_version: 1, - minor_version: 0, + self.vendor_defined_req_payload_struct.req_length = ReqUnBindP2PStreamRequest { + message_header: TdispMessageHeader { + interface_id, + message_type: TdispRequestResponseCode::UNBIND_P2P_STREAM_REQUEST, + tdisp_version: TdispVersion { + major_version: 1, + minor_version: 0, + }, }, - }, - p2p_stream_id, - } - .encode(&mut writer) - .map_err(|_| SPDM_STATUS_BUFFER_FULL)? - as u16; + p2p_stream_id, + } + .encode(&mut writer) + .map_err(|_| SPDM_STATUS_BUFFER_FULL)? + as u16; + + spdm_requester + .send_spdm_vendor_defined_request( + Some(session_id), + STANDARD_ID, + vendor_id(), + &self.vendor_defined_req_payload_struct, + &mut self.vendor_defined_rsp_payload_struct, + ) + .await?; - let vendor_defined_rsp_payload_struct = spdm_requester - .send_spdm_vendor_defined_request( - Some(session_id), - STANDARD_ID, - vendor_id(), - vendor_defined_req_payload_struct, + if let Ok(tdisp_error) = RspTdispError::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], ) - .await?; + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD) + { + *tdisp_error_code = Some(tdisp_error.error_code); + return Err(SPDM_STATUS_ERROR_PEER); + } - if let Ok(tdisp_error) = RspTdispError::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD) - { - *tdisp_error_code = Some(tdisp_error.error_code); - return Err(SPDM_STATUS_ERROR_PEER); - } + let rsp_un_bind_p2_pstream_response = RspUnBindP2PStreamResponse::read_bytes( + &self + .vendor_defined_rsp_payload_struct + .vendor_defined_rsp_payload + [..self.vendor_defined_rsp_payload_struct.rsp_length as usize], + ) + .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; - let rsp_un_bind_p2_pstream_response = RspUnBindP2PStreamResponse::read_bytes( - &vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload - [..vendor_defined_rsp_payload_struct.rsp_length as usize], - ) - .ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?; + if rsp_un_bind_p2_pstream_response.message_header.tdisp_version + != (TdispVersion { + major_version: 1, + minor_version: 0, + }) + || rsp_un_bind_p2_pstream_response.message_header.message_type + != TdispRequestResponseCode::UNBIND_P2P_STREAM_RESPONSE + || rsp_un_bind_p2_pstream_response.message_header.interface_id != interface_id + { + return Err(SPDM_STATUS_INVALID_MSG_FIELD); + } - if rsp_un_bind_p2_pstream_response.message_header.tdisp_version - != (TdispVersion { - major_version: 1, - minor_version: 0, - }) - || rsp_un_bind_p2_pstream_response.message_header.message_type - != TdispRequestResponseCode::UNBIND_P2P_STREAM_RESPONSE - || rsp_un_bind_p2_pstream_response.message_header.interface_id != interface_id - { - return Err(SPDM_STATUS_INVALID_MSG_FIELD); + Ok(()) } - - Ok(()) } diff --git a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_vdm_request.rs b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_vdm_request.rs index ffd9eb7c..1d265877 100644 --- a/tdisp/src/pci_tdisp_requester/pci_tdisp_req_vdm_request.rs +++ b/tdisp/src/pci_tdisp_requester/pci_tdisp_req_vdm_request.rs @@ -1,37 +1,44 @@ -// Copyright (c) 2023 Intel Corporation -// -// SPDX-License-Identifier: Apache-2.0 or MIT - -use spdmlib::error::SPDM_STATUS_INVALID_PARAMETER; -use spdmlib::message::VendorDefinedRspPayloadStruct; -use spdmlib::{ - error::SpdmResult, message::VendorDefinedReqPayloadStruct, requester::RequesterContext, -}; - -use crate::pci_tdisp::vendor_id; -use crate::pci_tdisp::STANDARD_ID; -use crate::pci_tdisp::TDISP_PROTOCOL_ID; - -#[maybe_async::maybe_async] -pub async fn pci_tdisp_req_vdm_request( - // IN - spdm_requester: &mut RequesterContext, - session_id: u32, - vendor_defined_req_payload_struct: VendorDefinedReqPayloadStruct, - // OUT -) -> SpdmResult { - if vendor_defined_req_payload_struct.req_length < 1 - || vendor_defined_req_payload_struct.vendor_defined_req_payload[0] != TDISP_PROTOCOL_ID - { - Err(SPDM_STATUS_INVALID_PARAMETER) - } else { - spdm_requester - .send_spdm_vendor_defined_request( - Some(session_id), - STANDARD_ID, - vendor_id(), - vendor_defined_req_payload_struct, - ) - .await - } -} +// Copyright (c) 2023 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 or MIT + +use spdmlib::error::SPDM_STATUS_INVALID_PARAMETER; +use spdmlib::{ + error::SpdmResult, message::VendorDefinedReqPayloadStruct, requester::RequesterContext, +}; + +use crate::pci_tdisp::vendor_id; +use crate::pci_tdisp::STANDARD_ID; +use crate::pci_tdisp::TDISP_PROTOCOL_ID; + +use super::TdispReqContext; + +impl TdispReqContext { + #[maybe_async::maybe_async] + pub async fn pci_tdisp_req_vdm_request( + &mut self, + // IN + spdm_requester: &mut RequesterContext, + session_id: u32, + vendor_defined_req_payload_struct: &VendorDefinedReqPayloadStruct, + // OUT + vendor_defined_rsp_payload_struct: &mut spdmlib::message::VendorDefinedRspPayloadStruct, + ) -> SpdmResult { + if vendor_defined_req_payload_struct.req_length < 1 + || vendor_defined_req_payload_struct.vendor_defined_req_payload[0] != TDISP_PROTOCOL_ID + { + Err(SPDM_STATUS_INVALID_PARAMETER) + } else { + spdm_requester + .send_spdm_vendor_defined_request( + Some(session_id), + STANDARD_ID, + vendor_id(), + &vendor_defined_req_payload_struct, + vendor_defined_rsp_payload_struct, + ) + .await?; + Ok(()) + } + } +} diff --git a/test/spdm-requester-emu/src/main.rs b/test/spdm-requester-emu/src/main.rs index f105b1ca..1f8c6c61 100644 --- a/test/spdm-requester-emu/src/main.rs +++ b/test/spdm-requester-emu/src/main.rs @@ -21,6 +21,7 @@ use idekm::pci_idekm::PCI_IDE_KM_IDE_REG_BLOCK_MAX_COUNT; use log::LevelFilter; use log::*; use simple_logger::SimpleLogger; +use tdisp::pci_tdisp_requester::TdispReqContext; #[cfg(not(feature = "is_sync"))] use spdm_emu::async_runtime::block_on; @@ -51,13 +52,6 @@ use tdisp::pci_tdisp::TdiState; use tdisp::pci_tdisp::TdispMmioRange; use tdisp::pci_tdisp::MAX_DEVICE_REPORT_BUFFER; use tdisp::pci_tdisp::START_INTERFACE_NONCE_LEN; -use tdisp::pci_tdisp_requester::pci_tdisp_req_get_device_interface_report; -use tdisp::pci_tdisp_requester::pci_tdisp_req_get_device_interface_state; -use tdisp::pci_tdisp_requester::pci_tdisp_req_get_tdisp_capabilities; -use tdisp::pci_tdisp_requester::pci_tdisp_req_get_tdisp_version; -use tdisp::pci_tdisp_requester::pci_tdisp_req_lock_interface_request; -use tdisp::pci_tdisp_requester::pci_tdisp_req_start_interface_request; -use tdisp::pci_tdisp_requester::pci_tdisp_req_stop_interface_request; use spin::Mutex; extern crate alloc; @@ -627,7 +621,7 @@ async fn test_idekm_tdisp( .unwrap(); // ide_km test - let mut idekm_req_context = IdekmReqContext; + let mut idekm_req_context = IdekmReqContext::default(); // ide_km query let port_index = 0u8; let mut dev_func_num = 0u8; @@ -1116,6 +1110,7 @@ async fn test_idekm_tdisp( println!("Successful KEY_SET_STOP at Key Set 0 | TX | CPL!"); // tdisp test + let mut tdisp_req_context = TdispReqContext::default(); let interface_id = InterfaceId { function_id: FunctionId { requester_id: 0x1234, @@ -1124,7 +1119,8 @@ async fn test_idekm_tdisp( }, }; - pci_tdisp_req_get_tdisp_version(&mut context, session_id, interface_id) + tdisp_req_context + .pci_tdisp_req_get_tdisp_version(&mut context, session_id, interface_id) .await .unwrap(); println!("Successful Get Tdisp Version!"); @@ -1136,31 +1132,34 @@ async fn test_idekm_tdisp( let mut num_req_this = 0u8; let mut num_req_all = 0u8; let mut req_msgs_supported = [0u8; 16]; - pci_tdisp_req_get_tdisp_capabilities( - &mut context, - session_id, - tsm_caps, - interface_id, - &mut dsm_caps, - &mut lock_interface_flags_supported, - &mut dev_addr_width, - &mut num_req_this, - &mut num_req_all, - &mut req_msgs_supported, - ) - .await - .unwrap(); + + tdisp_req_context + .pci_tdisp_req_get_tdisp_capabilities( + &mut context, + session_id, + tsm_caps, + interface_id, + &mut dsm_caps, + &mut lock_interface_flags_supported, + &mut dev_addr_width, + &mut num_req_this, + &mut num_req_all, + &mut req_msgs_supported, + ) + .await + .unwrap(); println!("Successful Get Tdisp Capabilities!"); let mut tdi_state = TdiState::ERROR; - pci_tdisp_req_get_device_interface_state( - &mut context, - session_id, - interface_id, - &mut tdi_state, - ) - .await - .unwrap(); + tdisp_req_context + .pci_tdisp_req_get_device_interface_state( + &mut context, + session_id, + interface_id, + &mut tdi_state, + ) + .await + .unwrap(); assert_eq!(tdi_state, TdiState::CONFIG_UNLOCKED); println!("Successful Get Tdisp State: {:X?}!", tdi_state); @@ -1170,48 +1169,51 @@ async fn test_idekm_tdisp( let bind_p2p_address_mask = 0; let mut start_interface_nonce = [0u8; START_INTERFACE_NONCE_LEN]; let mut tdisp_error_code = None; - pci_tdisp_req_lock_interface_request( - &mut context, - session_id, - interface_id, - flags, - default_stream_id, - mmio_reporting_offset, - bind_p2p_address_mask, - &mut start_interface_nonce, - &mut tdisp_error_code, - ) - .await - .unwrap(); + tdisp_req_context + .pci_tdisp_req_lock_interface_request( + &mut context, + session_id, + interface_id, + flags, + default_stream_id, + mmio_reporting_offset, + bind_p2p_address_mask, + &mut start_interface_nonce, + &mut tdisp_error_code, + ) + .await + .unwrap(); assert!(tdisp_error_code.is_none()); println!( "Successful Lock Interface, start_interface_nonce: {:X?}!", start_interface_nonce ); - pci_tdisp_req_get_device_interface_state( - &mut context, - session_id, - interface_id, - &mut tdi_state, - ) - .await - .unwrap(); + tdisp_req_context + .pci_tdisp_req_get_device_interface_state( + &mut context, + session_id, + interface_id, + &mut tdi_state, + ) + .await + .unwrap(); assert_eq!(tdi_state, TdiState::CONFIG_LOCKED); println!("Successful Get Tdisp State: {:X?}!", tdi_state); let mut report = [0u8; MAX_DEVICE_REPORT_BUFFER]; let mut report_size = 0usize; - pci_tdisp_req_get_device_interface_report( - &mut context, - session_id, - interface_id, - &mut report, - &mut report_size, - &mut tdisp_error_code, - ) - .await - .unwrap(); + tdisp_req_context + .pci_tdisp_req_get_device_interface_report( + &mut context, + session_id, + interface_id, + &mut report, + &mut report_size, + &mut tdisp_error_code, + ) + .await + .unwrap(); assert!(tdisp_error_code.is_none()); let tdi_report = TdiReportStructure::read_bytes(&report).unwrap(); println!( @@ -1219,42 +1221,46 @@ async fn test_idekm_tdisp( tdi_report ); - pci_tdisp_req_start_interface_request( - &mut context, - session_id, - interface_id, - &start_interface_nonce, - &mut tdisp_error_code, - ) - .await - .unwrap(); + tdisp_req_context + .pci_tdisp_req_start_interface_request( + &mut context, + session_id, + interface_id, + &start_interface_nonce, + &mut tdisp_error_code, + ) + .await + .unwrap(); assert!(tdisp_error_code.is_none()); println!("Successful Start Interface!"); - pci_tdisp_req_get_device_interface_state( - &mut context, - session_id, - interface_id, - &mut tdi_state, - ) - .await - .unwrap(); + tdisp_req_context + .pci_tdisp_req_get_device_interface_state( + &mut context, + session_id, + interface_id, + &mut tdi_state, + ) + .await + .unwrap(); assert_eq!(tdi_state, TdiState::RUN); println!("Successful Get Tdisp State: {:X?}!", tdi_state); - pci_tdisp_req_stop_interface_request(&mut context, session_id, interface_id) + tdisp_req_context + .pci_tdisp_req_stop_interface_request(&mut context, session_id, interface_id) .await .unwrap(); println!("Successful Stop Interface!"); - pci_tdisp_req_get_device_interface_state( - &mut context, - session_id, - interface_id, - &mut tdi_state, - ) - .await - .unwrap(); + tdisp_req_context + .pci_tdisp_req_get_device_interface_state( + &mut context, + session_id, + interface_id, + &mut tdi_state, + ) + .await + .unwrap(); assert_eq!(tdi_state, TdiState::CONFIG_UNLOCKED); println!("Successful Get Tdisp State: {:X?}!", tdi_state); @@ -1379,7 +1385,7 @@ fn main() { use std::thread; thread::Builder::new() - .stack_size(EMU_STACK_SIZE) + .stack_size(EMU_STACK_SIZE * 2) .spawn(emu_main) .unwrap() .join()