@@ -4,7 +4,7 @@ use error::{Error, WrapperError};
4
4
use log:: { error, info, trace} ;
5
5
use prost:: Message ;
6
6
use std:: convert:: { TryFrom , TryInto } ;
7
- use std:: ffi:: { c_void , CString } ;
7
+ use std:: ffi:: CString ;
8
8
use std:: io:: { self } ;
9
9
use std:: ptr:: null_mut;
10
10
use std:: slice;
@@ -61,9 +61,8 @@ mod ts_protobuf;
61
61
/// is required from the caller.
62
62
#[ derive( Debug ) ]
63
63
pub struct Context {
64
- rpc_caller : * mut rpc_caller ,
64
+ rpc_caller_session : * mut rpc_caller_session ,
65
65
service_context : * mut service_context ,
66
- rpc_session_handle : * mut c_void ,
67
66
call_mutex : Mutex < ( ) > ,
68
67
}
69
68
@@ -75,43 +74,29 @@ impl Context {
75
74
unsafe { service_locator_init ( ) } ;
76
75
77
76
info ! ( "Obtaining a crypto Trusted Service context." ) ;
78
- let mut status = 0 ;
79
77
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 ( ) ) } ;
81
79
if service_context. is_null ( ) {
82
- error ! ( "Locating crypto Trusted Service failed, status: {}" , status ) ;
80
+ error ! ( "Locating crypto Trusted Service failed" ) ;
83
81
return Err ( io:: Error :: new (
84
82
io:: ErrorKind :: Other ,
85
83
"Failed to obtain a Trusted Service context" ,
86
84
)
87
85
. 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 ( ) ) ;
97
86
}
98
87
99
88
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 ( ) {
105
91
return Err ( io:: Error :: new (
106
92
io:: ErrorKind :: Other ,
107
93
"Failed to start Trusted Service context" ,
108
94
)
109
95
. into ( ) ) ;
110
96
}
111
97
let ctx = Context {
112
- rpc_caller ,
98
+ rpc_caller_session ,
113
99
service_context,
114
- rpc_session_handle,
115
100
call_mutex : Mutex :: new ( ( ) ) ,
116
101
} ;
117
102
@@ -129,8 +114,19 @@ impl Context {
129
114
trace ! ( "Beginning call to Trusted Service" ) ;
130
115
131
116
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
+ } ;
134
130
if call_handle. is_null ( ) {
135
131
error ! ( "Call handle was null" ) ;
136
132
return Err ( WrapperError :: CallHandleNull . into ( ) ) ;
@@ -140,7 +136,9 @@ impl Context {
140
136
}
141
137
let mut buf_out = unsafe { slice:: from_raw_parts_mut ( buf_out, req. encoded_len ( ) ) } ;
142
138
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
+ } ;
144
142
format_error ! ( "Failed to serialize Protobuf request" , e) ;
145
143
WrapperError :: FailedPbConversion
146
144
} ) ?;
@@ -151,38 +149,44 @@ impl Context {
151
149
let mut resp_buf = null_mut ( ) ;
152
150
let mut resp_buf_size = 0 ;
153
151
let status = unsafe {
154
- rpc_caller_invoke (
155
- self . rpc_caller ,
152
+ rpc_caller_session_invoke (
156
153
call_handle,
157
154
i32:: from ( req. opcode ( ) ) . try_into ( ) . unwrap ( ) ,
158
- & mut opstatus,
159
155
& mut resp_buf,
160
156
& mut resp_buf_size,
157
+ & mut opstatus,
161
158
)
162
159
} ;
163
160
Error :: from_status_opstatus (
164
161
status,
165
162
i32:: try_from ( opstatus) . map_err ( |_| Error :: Wrapper ( WrapperError :: InvalidOpStatus ) ) ?,
166
163
)
167
164
. map_err ( |e| {
168
- unsafe { rpc_caller_end ( self . rpc_caller , call_handle) } ;
165
+ unsafe {
166
+ let _ = rpc_caller_session_end ( call_handle) ;
167
+ } ;
169
168
e
170
169
} ) ?;
171
170
let resp_buf = unsafe { slice:: from_raw_parts_mut ( resp_buf, resp_buf_size) } ;
172
171
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
+ } ;
174
175
format_error ! ( "Failed to serialize Protobuf request" , e) ;
175
176
WrapperError :: FailedPbConversion
176
177
} ) ?;
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
+ } ;
178
182
179
183
Ok ( resp)
180
184
}
181
185
}
182
186
183
187
impl Drop for Context {
184
188
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 ) } ;
186
190
187
191
unsafe { service_context_relinquish ( self . service_context ) } ;
188
192
}
0 commit comments