Skip to content

Commit 57fb4a1

Browse files
committed
TS: Use newer RPC APIs
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 <[email protected]>
1 parent 599f0e1 commit 57fb4a1

File tree

1 file changed

+36
-32
lines changed
  • src/providers/trusted_service/context

1 file changed

+36
-32
lines changed

src/providers/trusted_service/context/mod.rs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use error::{Error, WrapperError};
44
use log::{error, info, trace};
55
use prost::Message;
66
use std::convert::{TryFrom, TryInto};
7-
use std::ffi::{c_void, CString};
7+
use std::ffi::CString;
88
use std::io::{self};
99
use std::ptr::null_mut;
1010
use std::slice;
@@ -61,9 +61,8 @@ mod ts_protobuf;
6161
/// is required from the caller.
6262
#[derive(Debug)]
6363
pub struct Context {
64-
rpc_caller: *mut rpc_caller,
64+
rpc_caller_session: *mut rpc_caller_session,
6565
service_context: *mut service_context,
66-
rpc_session_handle: *mut c_void,
6766
call_mutex: Mutex<()>,
6867
}
6968

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

7776
info!("Obtaining a crypto Trusted Service context.");
78-
let mut status = 0;
7977
let service_name = CString::new("sn:trustedfirmware.org:crypto-protobuf:0").unwrap();
80-
let service_context = unsafe { service_locator_query(service_name.as_ptr(), &mut status) };
78+
let service_context = unsafe { service_locator_query(service_name.as_ptr()) };
8179
if service_context.is_null() {
82-
error!("Locating crypto Trusted Service failed, status: {}", status);
80+
error!("Locating crypto Trusted Service failed");
8381
return Err(io::Error::new(
8482
io::ErrorKind::Other,
8583
"Failed to obtain a Trusted Service context",
8684
)
8785
.into());
88-
} else if status != 0 {
89-
return Err(io::Error::new(
90-
io::ErrorKind::Other,
91-
format!(
92-
"Failed to connect to Trusted Service; status code: {}",
93-
status
94-
),
95-
)
96-
.into());
9786
}
9887

9988
info!("Starting crypto Trusted Service context");
100-
let mut rpc_caller = null_mut();
101-
let rpc_session_handle = unsafe {
102-
service_context_open(service_context, TS_RPC_ENCODING_PROTOBUF, &mut rpc_caller)
103-
};
104-
if rpc_caller.is_null() || rpc_session_handle.is_null() {
89+
let rpc_caller_session = unsafe { service_context_open(service_context) };
90+
if rpc_caller_session.is_null() {
10591
return Err(io::Error::new(
10692
io::ErrorKind::Other,
10793
"Failed to start Trusted Service context",
10894
)
10995
.into());
11096
}
11197
let ctx = Context {
112-
rpc_caller,
98+
rpc_caller_session,
11399
service_context,
114-
rpc_session_handle,
115100
call_mutex: Mutex::new(()),
116101
};
117102

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

131116
let mut buf_out = null_mut();
132-
let call_handle =
133-
unsafe { rpc_caller_begin(self.rpc_caller, &mut buf_out, req.encoded_len()) };
117+
// The response buffer length is set to 4096 as a common buffer length
118+
// for all operations. In case of the session memory policy being "alloc_for_session"
119+
// which is dependant on the platform, this value doesnt impact but for
120+
// platforms with memory policy "alloc_for_each_call" the buffer length should be
121+
// sufficient enough to hold the entire response.
122+
let call_handle = unsafe {
123+
rpc_caller_session_begin(
124+
self.rpc_caller_session,
125+
&mut buf_out,
126+
req.encoded_len(),
127+
4096,
128+
)
129+
};
134130
if call_handle.is_null() {
135131
error!("Call handle was null");
136132
return Err(WrapperError::CallHandleNull.into());
@@ -140,7 +136,9 @@ impl Context {
140136
}
141137
let mut buf_out = unsafe { slice::from_raw_parts_mut(buf_out, req.encoded_len()) };
142138
req.encode(&mut buf_out).map_err(|e| {
143-
unsafe { rpc_caller_end(self.rpc_caller, call_handle) };
139+
unsafe {
140+
let _ = rpc_caller_session_end(call_handle);
141+
};
144142
format_error!("Failed to serialize Protobuf request", e);
145143
WrapperError::FailedPbConversion
146144
})?;
@@ -151,38 +149,44 @@ impl Context {
151149
let mut resp_buf = null_mut();
152150
let mut resp_buf_size = 0;
153151
let status = unsafe {
154-
rpc_caller_invoke(
155-
self.rpc_caller,
152+
rpc_caller_session_invoke(
156153
call_handle,
157154
i32::from(req.opcode()).try_into().unwrap(),
158-
&mut opstatus,
159155
&mut resp_buf,
160156
&mut resp_buf_size,
157+
&mut opstatus,
161158
)
162159
};
163160
Error::from_status_opstatus(
164161
status,
165162
i32::try_from(opstatus).map_err(|_| Error::Wrapper(WrapperError::InvalidOpStatus))?,
166163
)
167164
.map_err(|e| {
168-
unsafe { rpc_caller_end(self.rpc_caller, call_handle) };
165+
unsafe {
166+
let _ = rpc_caller_session_end(call_handle);
167+
};
169168
e
170169
})?;
171170
let resp_buf = unsafe { slice::from_raw_parts_mut(resp_buf, resp_buf_size) };
172171
resp.merge(&*resp_buf).map_err(|e| {
173-
unsafe { rpc_caller_end(self.rpc_caller, call_handle) };
172+
unsafe {
173+
let _ = rpc_caller_session_end(call_handle);
174+
};
174175
format_error!("Failed to serialize Protobuf request", e);
175176
WrapperError::FailedPbConversion
176177
})?;
177-
unsafe { rpc_caller_end(self.rpc_caller, call_handle) };
178+
unsafe {
179+
let status = rpc_caller_session_end(call_handle);
180+
Error::from_status_opstatus(status, 0)?;
181+
};
178182

179183
Ok(resp)
180184
}
181185
}
182186

183187
impl Drop for Context {
184188
fn drop(&mut self) {
185-
unsafe { service_context_close(self.service_context, self.rpc_session_handle) };
189+
unsafe { service_context_close(self.service_context, self.rpc_caller_session) };
186190

187191
unsafe { service_context_relinquish(self.service_context) };
188192
}

0 commit comments

Comments
 (0)