Skip to content

Commit

Permalink
TS: Use newer RPC APIs
Browse files Browse the repository at this point in the history
The 1.0.0 version of trusted services has RPC APIs changed. This
patch configures the provider to use the newer ones and updates
the submodule.

Signed-off-by: Gowtham Suresh Kumar <gowtham.sureshkumar@arm.com>
  • Loading branch information
gowthamsk-arm committed May 10, 2024
1 parent 2b8a571 commit 904535e
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions src/providers/trusted_service/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use error::{Error, WrapperError};
use log::{error, info, trace};
use prost::Message;
use std::convert::{TryFrom, TryInto};
use std::ffi::{c_void, CString};
use std::ffi::CString;
use std::io::{self};
use std::ptr::null_mut;
use std::slice;
Expand Down Expand Up @@ -61,9 +61,8 @@ mod ts_protobuf;
/// is required from the caller.
#[derive(Debug)]
pub struct Context {
rpc_caller: *mut rpc_caller,
rpc_caller_session: *mut rpc_caller_session,
service_context: *mut service_context,
rpc_session_handle: *mut c_void,
call_mutex: Mutex<()>,
}

Expand All @@ -75,43 +74,29 @@ impl Context {
unsafe { service_locator_init() };

info!("Obtaining a crypto Trusted Service context.");
let mut status = 0;
let service_name = CString::new("sn:trustedfirmware.org:crypto-protobuf:0").unwrap();
let service_context = unsafe { service_locator_query(service_name.as_ptr(), &mut status) };
let service_context = unsafe { service_locator_query(service_name.as_ptr()) };
if service_context.is_null() {
error!("Locating crypto Trusted Service failed, status: {}", status);
error!("Locating crypto Trusted Service failed");
return Err(io::Error::new(
io::ErrorKind::Other,
"Failed to obtain a Trusted Service context",
)
.into());
} else if status != 0 {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Failed to connect to Trusted Service; status code: {}",
status
),
)
.into());
}

info!("Starting crypto Trusted Service context");
let mut rpc_caller = null_mut();
let rpc_session_handle = unsafe {
service_context_open(service_context, TS_RPC_ENCODING_PROTOBUF, &mut rpc_caller)
};
if rpc_caller.is_null() || rpc_session_handle.is_null() {
let rpc_caller_session = unsafe { service_context_open(service_context) };
if rpc_caller_session.is_null() {
return Err(io::Error::new(
io::ErrorKind::Other,
"Failed to start Trusted Service context",
)
.into());
}
let ctx = Context {
rpc_caller,
rpc_caller_session,
service_context,
rpc_session_handle,
call_mutex: Mutex::new(()),
};

Expand All @@ -129,8 +114,19 @@ impl Context {
trace!("Beginning call to Trusted Service");

let mut buf_out = null_mut();
let call_handle =
unsafe { rpc_caller_begin(self.rpc_caller, &mut buf_out, req.encoded_len()) };
// The response buffer length is set to 4096 as a common buffer length
// for all operations. In case of the session memory policy being "alloc_for_session"
// which is dependant on the platform, this value doesnt impact but for
// platforms with memory policy "alloc_for_each_call" the buffer length should be
// sufficient enough to hold the entire response.
let call_handle = unsafe {
rpc_caller_session_begin(
self.rpc_caller_session,
&mut buf_out,
req.encoded_len(),
4096,
)
};
if call_handle.is_null() {
error!("Call handle was null");
return Err(WrapperError::CallHandleNull.into());
Expand All @@ -140,7 +136,9 @@ impl Context {
}
let mut buf_out = unsafe { slice::from_raw_parts_mut(buf_out, req.encoded_len()) };
req.encode(&mut buf_out).map_err(|e| {
unsafe { rpc_caller_end(self.rpc_caller, call_handle) };
unsafe {
let _ = rpc_caller_session_end(call_handle);
};
format_error!("Failed to serialize Protobuf request", e);
WrapperError::FailedPbConversion
})?;
Expand All @@ -151,38 +149,44 @@ impl Context {
let mut resp_buf = null_mut();
let mut resp_buf_size = 0;
let status = unsafe {
rpc_caller_invoke(
self.rpc_caller,
rpc_caller_session_invoke(
call_handle,
i32::from(req.opcode()).try_into().unwrap(),
&mut opstatus,
&mut resp_buf,
&mut resp_buf_size,
&mut opstatus,
)
};
Error::from_status_opstatus(
status,
i32::try_from(opstatus).map_err(|_| Error::Wrapper(WrapperError::InvalidOpStatus))?,
)
.map_err(|e| {
unsafe { rpc_caller_end(self.rpc_caller, call_handle) };
unsafe {
let _ = rpc_caller_session_end(call_handle);
};
e
})?;
let resp_buf = unsafe { slice::from_raw_parts_mut(resp_buf, resp_buf_size) };
resp.merge(&*resp_buf).map_err(|e| {
unsafe { rpc_caller_end(self.rpc_caller, call_handle) };
unsafe {
let _ = rpc_caller_session_end(call_handle);
};
format_error!("Failed to serialize Protobuf request", e);
WrapperError::FailedPbConversion
})?;
unsafe { rpc_caller_end(self.rpc_caller, call_handle) };
unsafe {
let status = rpc_caller_session_end(call_handle);
Error::from_status_opstatus(status, 0)?;
};

Ok(resp)
}
}

impl Drop for Context {
fn drop(&mut self) {
unsafe { service_context_close(self.service_context, self.rpc_session_handle) };
unsafe { service_context_close(self.service_context, self.rpc_caller_session) };

unsafe { service_context_relinquish(self.service_context) };
}
Expand Down

0 comments on commit 904535e

Please sign in to comment.