Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
pass vendor_id to vendor defined response handler.
Browse files Browse the repository at this point in the history
currently, just vendor defined request buffer is passed
to vendor defined response handler, the vendor id info
is missed.
This patch is to add vendor_id parameter to vendor
defined response handler.

Signed-off-by: Yang, Longlong <[email protected]>
  • Loading branch information
longlongyang authored and jyao1 committed Nov 6, 2023
1 parent 0b3ca29 commit 0f17765
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 36 deletions.
30 changes: 17 additions & 13 deletions fuzz-target/responder/vendor_rsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use fuzzlib::spdmlib::error::SpdmResult;
use fuzzlib::spdmlib::message::{
register_vendor_defined_struct, VendorDefinedReqPayloadStruct, VendorDefinedRspPayloadStruct,
VendorDefinedStruct,
VendorDefinedStruct, VendorIDStruct,
};
use fuzzlib::*;
use spdmlib::common::SpdmConnectionState;
Expand Down Expand Up @@ -54,19 +54,23 @@ async fn fuzz_handle_spdm_vendor_defined_request(data: Arc<Vec<u8>>) {
.runtime_info
.set_connection_state(SpdmConnectionState::SpdmConnectionNegotiated);

let vendor_defined_func: for<'r> fn(usize, &'r VendorDefinedReqPayloadStruct) -> Result<_, _> =
|_: usize,
_vendor_defined_req_payload_struct|
-> SpdmResult<VendorDefinedRspPayloadStruct> {
let mut vendor_defined_res_payload_struct = VendorDefinedRspPayloadStruct {
rsp_length: 0,
vendor_defined_rsp_payload: [0; config::MAX_SPDM_MSG_SIZE - 7 - 2],
};
vendor_defined_res_payload_struct.rsp_length = 8;
vendor_defined_res_payload_struct.vendor_defined_rsp_payload[0..8]
.clone_from_slice(b"deadbeef");
Ok(vendor_defined_res_payload_struct)
let vendor_defined_func: for<'r> fn(
usize,
&VendorIDStruct,
&'r VendorDefinedReqPayloadStruct,
) -> Result<_, _> = |_: usize,
_: &VendorIDStruct,
_vendor_defined_req_payload_struct|
-> SpdmResult<VendorDefinedRspPayloadStruct> {
let mut vendor_defined_res_payload_struct = VendorDefinedRspPayloadStruct {
rsp_length: 0,
vendor_defined_rsp_payload: [0; config::MAX_SPDM_MSG_SIZE - 7 - 2],
};
vendor_defined_res_payload_struct.rsp_length = 8;
vendor_defined_res_payload_struct.vendor_defined_rsp_payload[0..8]
.clone_from_slice(b"deadbeef");
Ok(vendor_defined_res_payload_struct)
};

register_vendor_defined_struct(VendorDefinedStruct {
vendor_defined_request_handler: vendor_defined_func,
Expand Down
14 changes: 11 additions & 3 deletions idekm/src/pci_ide_km_responder/pci_ide_km_rsp_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ use super::{
pci_ide_km_rsp_query,
};
use crate::pci_idekm::{
KEY_PROG_OBJECT_ID, K_SET_GO_OBJECT_ID, K_SET_STOP_OBJECT_ID, QUERY_OBJECT_ID,
vendor_id, IDEKM_PROTOCOL_ID, KEY_PROG_OBJECT_ID, K_SET_GO_OBJECT_ID, K_SET_STOP_OBJECT_ID,
QUERY_OBJECT_ID,
};
use spdmlib::{
error::{SpdmResult, SPDM_STATUS_INVALID_MSG_FIELD},
message::{VendorDefinedReqPayloadStruct, VendorDefinedRspPayloadStruct, VendorDefinedStruct},
message::{
VendorDefinedReqPayloadStruct, VendorDefinedRspPayloadStruct, VendorDefinedStruct,
VendorIDStruct,
},
};

pub const PCI_IDE_KM_INSTANCE: VendorDefinedStruct = VendorDefinedStruct {
Expand All @@ -21,9 +25,13 @@ pub const PCI_IDE_KM_INSTANCE: VendorDefinedStruct = VendorDefinedStruct {

pub fn pci_ide_km_rsp_dispatcher(
_vdm_handle: usize,
vendor_id_struct: &VendorIDStruct,
vendor_defined_req_payload_struct: &VendorDefinedReqPayloadStruct,
) -> SpdmResult<VendorDefinedRspPayloadStruct> {
if vendor_defined_req_payload_struct.req_length < 2 {
if vendor_defined_req_payload_struct.req_length < 2
|| vendor_id_struct != &vendor_id()
|| vendor_defined_req_payload_struct.vendor_defined_req_payload[0] != IDEKM_PROTOCOL_ID
{
return Err(SPDM_STATUS_INVALID_MSG_FIELD);
}

Expand Down
15 changes: 12 additions & 3 deletions spdmlib/src/message/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,11 @@ impl SpdmCodec for SpdmVendorDefinedResponsePayload {

#[derive(Clone, Copy)]
pub struct VendorDefinedStruct {
pub vendor_defined_request_handler:
fn(usize, &VendorDefinedReqPayloadStruct) -> SpdmResult<VendorDefinedRspPayloadStruct>,
pub vendor_defined_request_handler: fn(
usize,
&VendorIDStruct,
&VendorDefinedReqPayloadStruct,
) -> SpdmResult<VendorDefinedRspPayloadStruct>,
pub vdm_handle: usize, // interpreted/managed by User
}

Expand All @@ -284,6 +287,7 @@ static VENDOR_DEFNIED: OnceCell<VendorDefinedStruct> = OnceCell::uninit();
static VENDOR_DEFNIED_DEFAULT: VendorDefinedStruct = VendorDefinedStruct {
vendor_defined_request_handler:
|_vdm_handle: usize,
_vendor_id_struct: &VendorIDStruct,
_vendor_defined_req_payload_struct: &VendorDefinedReqPayloadStruct|
-> SpdmResult<VendorDefinedRspPayloadStruct> {
log::info!("not implement vendor defined struct!!!\n");
Expand All @@ -297,10 +301,15 @@ pub fn register_vendor_defined_struct(context: VendorDefinedStruct) -> bool {
}

pub fn vendor_defined_request_handler(
vendor_id_struct: &VendorIDStruct,
vendor_defined_req_payload_struct: &VendorDefinedReqPayloadStruct,
) -> SpdmResult<VendorDefinedRspPayloadStruct> {
if let Ok(vds) = VENDOR_DEFNIED.try_get_or_init(|| VENDOR_DEFNIED_DEFAULT) {
(vds.vendor_defined_request_handler)(vds.vdm_handle, vendor_defined_req_payload_struct)
(vds.vendor_defined_request_handler)(
vds.vdm_handle,
vendor_id_struct,
vendor_defined_req_payload_struct,
)
} else {
Err(SPDM_STATUS_INVALID_STATE_LOCAL)
}
Expand Down
15 changes: 11 additions & 4 deletions spdmlib/src/responder/vendor_rsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ impl ResponderContext {
let standard_id = vendor_defined_request_payload.standard_id;
let vendor_id = vendor_defined_request_payload.vendor_id;
let req_payload = vendor_defined_request_payload.req_payload;
let rsp_payload =
self.respond_to_vendor_defined_request(&req_payload, vendor_defined_request_handler);
let rsp_payload = self.respond_to_vendor_defined_request(
&req_payload,
&vendor_id,
vendor_defined_request_handler,
);
if let Err(e) = rsp_payload {
self.write_spdm_error(SpdmErrorCode::SpdmErrorUnspecified, 0, writer);
return (Err(e), Some(writer.used_slice()));
Expand Down Expand Up @@ -100,11 +103,15 @@ impl ResponderContext {
pub fn respond_to_vendor_defined_request<F>(
&mut self,
req: &VendorDefinedReqPayloadStruct,
vendor_id_struct: &VendorIDStruct,
verdor_defined_func: F,
) -> SpdmResult<VendorDefinedRspPayloadStruct>
where
F: Fn(&VendorDefinedReqPayloadStruct) -> SpdmResult<VendorDefinedRspPayloadStruct>,
F: Fn(
&VendorIDStruct,
&VendorDefinedReqPayloadStruct,
) -> SpdmResult<VendorDefinedRspPayloadStruct>,
{
verdor_defined_func(req)
verdor_defined_func(vendor_id_struct, req)
}
}
6 changes: 4 additions & 2 deletions tdisp/src/pci_tdisp_responder/pci_tdisp_rsp_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use core::convert::TryFrom;
use spdmlib::{
error::{SpdmResult, SPDM_STATUS_INVALID_MSG_FIELD},
message::{
VendorDefinedReqPayloadStruct, VendorDefinedRspPayloadStruct,
VendorDefinedReqPayloadStruct, VendorDefinedRspPayloadStruct, VendorIDStruct,
MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE,
},
};

use crate::pci_tdisp::{TdispErrorCode, TdispRequestResponseCode, TDISP_PROTOCOL_ID};
use crate::pci_tdisp::{vendor_id, TdispErrorCode, TdispRequestResponseCode, TDISP_PROTOCOL_ID};

use super::{
pci_tdisp_rsp_bind_p2p_stream_request::pci_tdisp_rsp_bind_p2p_stream,
Expand All @@ -29,9 +29,11 @@ use super::{

pub fn pci_tdisp_rsp_dispatcher(
vdm_handle: usize,
vendor_id_struct: &VendorIDStruct,
vendor_defined_req_payload_struct: &VendorDefinedReqPayloadStruct,
) -> SpdmResult<VendorDefinedRspPayloadStruct> {
if vendor_defined_req_payload_struct.req_length < 3
|| vendor_id_struct != &vendor_id()
|| vendor_defined_req_payload_struct.vendor_defined_req_payload[0] != TDISP_PROTOCOL_ID
{
return Err(SPDM_STATUS_INVALID_MSG_FIELD);
Expand Down
22 changes: 14 additions & 8 deletions test/spdm-responder-emu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

mod spdm_device_idekm_example;
use idekm::pci_ide_km_responder::pci_ide_km_rsp_dispatcher;
use idekm::pci_idekm::IDEKM_PROTOCOL_ID;
use idekm::pci_idekm::{vendor_id, IDEKM_PROTOCOL_ID};
use spdm_device_idekm_example::init_device_idekm_instance;

mod spdm_device_tdisp_example;
Expand All @@ -18,6 +18,7 @@ use spdmlib::config::{MAX_ROOT_CERT_SUPPORT, RECEIVER_BUFFER_SIZE};
use spdmlib::error::{SpdmResult, SPDM_STATUS_INVALID_MSG_FIELD};
use spdmlib::message::{
VendorDefinedReqPayloadStruct, VendorDefinedRspPayloadStruct, VendorDefinedStruct,
VendorIDStruct,
};
use tdisp::pci_tdisp::{
FunctionId, InterfaceId, LockInterfaceFlag, TdiState, START_INTERFACE_NONCE_LEN,
Expand Down Expand Up @@ -465,19 +466,24 @@ pub async fn send_pci_discovery(

fn pci_idekm_tdisp_rsp_dispatcher(
vdm_handle: usize,
vendor_id_struct: &VendorIDStruct,
vendor_defined_req_payload_struct: &VendorDefinedReqPayloadStruct,
) -> SpdmResult<VendorDefinedRspPayloadStruct> {
if vendor_defined_req_payload_struct.req_length < 1 {
if vendor_defined_req_payload_struct.req_length < 1 || vendor_id_struct != &vendor_id() {
return Err(SPDM_STATUS_INVALID_MSG_FIELD);
}

match vendor_defined_req_payload_struct.vendor_defined_req_payload[0] {
IDEKM_PROTOCOL_ID => {
pci_ide_km_rsp_dispatcher(vdm_handle, vendor_defined_req_payload_struct)
}
TDISP_PROTOCOL_ID => {
pci_tdisp_rsp_dispatcher(vdm_handle, vendor_defined_req_payload_struct)
}
IDEKM_PROTOCOL_ID => pci_ide_km_rsp_dispatcher(
vdm_handle,
vendor_id_struct,
vendor_defined_req_payload_struct,
),
TDISP_PROTOCOL_ID => pci_tdisp_rsp_dispatcher(
vdm_handle,
vendor_id_struct,
vendor_defined_req_payload_struct,
),
_ => Err(SPDM_STATUS_INVALID_MSG_FIELD),
}
}
Expand Down
10 changes: 7 additions & 3 deletions test/spdmlib-test/src/responder_tests/vendor_rsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ fn test_case0_handle_spdm_vendor_defined_request() {

let vendor_defined_func: for<'r> fn(
usize,
&VendorIDStruct,
&'r vendor::VendorDefinedReqPayloadStruct,
) -> Result<_, _> = |_: usize,
_: &VendorIDStruct,
_vendor_defined_req_payload_struct|
-> SpdmResult<VendorDefinedRspPayloadStruct> {
let mut vendor_defined_res_payload_struct = VendorDefinedRspPayloadStruct {
Expand All @@ -60,9 +62,11 @@ fn test_case0_handle_spdm_vendor_defined_request() {
vdm_handle: 0,
});

if let Ok(vendor_defined_res_payload_struct) =
responder.respond_to_vendor_defined_request(&req, vendor_defined_request_handler)
{
if let Ok(vendor_defined_res_payload_struct) = responder.respond_to_vendor_defined_request(
&req,
&VendorIDStruct::default(),
vendor_defined_request_handler,
) {
assert_eq!(vendor_defined_res_payload_struct.rsp_length, 8);
assert_eq!(
vendor_defined_res_payload_struct.vendor_defined_rsp_payload[0],
Expand Down

0 comments on commit 0f17765

Please sign in to comment.