1 /*
2  *  Copyright (c) 2012, The Android Open Source Project
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you
5  *  may not use this file except in compliance with the License.  You may
6  *  obtain a copy of the License at
7  *
8  *  http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  *  implied.  See the License for the specific language governing
14  *  permissions and limitations under the License.
15  */
16 
17 #ifndef __QSEECOMAPI_H_
18 #define __QSEECOMAPI_H_
19 
20 
21 /*----------------------------------------------------------------------------
22  * Include Files
23 * -------------------------------------------------------------------------*/
24 #include <stdint.h>
25 #include <stdbool.h>
26 
27 #define QSEECOM_ALIGN_SIZE	0x40
28 #define QSEECOM_ALIGN_MASK	(QSEECOM_ALIGN_SIZE - 1)
29 #define QSEECOM_ALIGN(x)	\
30 	((x + QSEECOM_ALIGN_SIZE) & (~QSEECOM_ALIGN_MASK))
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 /*----------------------------------------------------------------------------
37  * Preprocessor Definitions and Constants
38  * -------------------------------------------------------------------------*/
39 /** The memory is locked and non-pageable */
40 #define MEM_LOCKED         0x00000001
41 /** The memory is marked non-cacheable */
42 #define MEM_NON_CACHED     0x00000002
43 
44 #define QSEECOM_APP_QUERY_FAILED       -6
45 #define QSEECOM_APP_NOT_LOADED         -5
46 #define QSEECOM_APP_ALREADY_LOADED     -4
47 #define QSEECOM_LISTENER_UNREGISTERED	-3
48 #define QSEECOM_LISTENER_ALREADY_REGISTERED	-2
49 #define QSEECOM_LISTENER_REGISTER_FAIL		-1
50 
51 /*----------------------------------------------------------------------------
52  * Type Declarations
53  * -------------------------------------------------------------------------*/
54 struct QSEECom_handle {
55 	unsigned char *ion_sbuffer;
56 };
57 
58 struct QSEECom_ion_fd_data {
59 	int32_t fd;
60 	uint32_t cmd_buf_offset;
61 };
62 
63 struct QSEECom_ion_fd_info {
64 	struct QSEECom_ion_fd_data data[4];
65 };
66 
67 /*----------------------------------------------------------------------------
68  * Function Declarations and Documentation
69  * -------------------------------------------------------------------------*/
70 /**
71  * @brief Open a handle to the  QSEECom device.
72  *
73  * - Load a secure application. The application will be verified that it is
74  *    secure by digital signature verification.
75  * Allocate memory for sending requests to the QSAPP
76  *
77  * Note/Comments:
78  * There is a one-to-one relation for a HLOS client and a QSAPP;
79  * meaning that only one app can communicate to a QSAPP at a time.
80  *
81  * Please note that there is difference between an application and a listener
82  * service. A QSAPP must be loaded at the request of the HLOS,
83  * and all requests are orginated by the HLOS client.
84  * A listener service on the otherhand is started during start-up by a
85  * daemon, qseecomd.
86  *
87  * A HLOS application may create mutiple handles to the QSAPP
88  *
89  * @param[in/out] handle The device handle
90  * @param[in] fname The directory and filename to load.
91  * @param[in] sb_size Size of the shared buffer memory  for sending requests.
92  * @return Zero on success, negative on failure. errno will be set on
93  *  error.
94  */
95 int QSEECom_start_app(struct QSEECom_handle **clnt_handle, const char *path,
96 			const char *fname, uint32_t sb_size);
97 
98 /**
99  * @brief Close the application associated with the handle.
100  *
101  * - Unload a secure application. The driver will verify if there exists
102  *   any other applications that are communicating with the QSAPP to which
103  *   the "handle" is tied.
104  * - De-allocate memory for sending requests to QSAPP.
105  *
106  * @param[in] handle The device handle
107  * @return Zero on success, negative on failure. errno will be set on
108  *  error.
109  */
110 int QSEECom_shutdown_app(struct QSEECom_handle **handle);
111 
112 /**
113  * @brief Open a handle to the  QSEECom device.
114  *
115  * - Load an external elf. The elf will be verified that it is
116  *    secure by digital signature verification.
117  *
118  * A HLOS application may create mutiple opens (only one is permitted for the
119  * app, but each listener service can open a unique device in the same HLOS app
120  * /executable.
121  * @param[in/out] handle The device handle
122  * @param[in] fname The directory and filename to load.
123  * @return Zero on success, negative on failure. errno will be set on
124  *  error.
125  */
126 int QSEECom_load_external_elf(struct QSEECom_handle **clnt_handle, const char *path,
127 			const char *fname);
128 
129 /**
130  * @brief Close the external elf
131  *
132  * - Unload an external elf.
133  *
134  * @param[in] handle The device handle
135  *
136  * @return Zero on success, negative on failure. errno will be set on
137  *  error.
138  */
139 int QSEECom_unload_external_elf(struct QSEECom_handle **handle);
140 
141 /**
142  * @brief Register an HLOS listener service. This allows messages from QSAPP
143  * to be received.
144  *
145  * @param[in] handle The device handle
146  * @param[in] lstnr_id The listener service identifier. This ID must be uniquely
147  * assigned to avoid any collisions.
148  * @param[in] sb_length Shared memory buffer between OS and QSE.
149  * @param[in] flags Provide the shared memory flags attributes.
150  *
151  * @return Zero on success, negative on failure. errno will be set on
152  *  error.
153  *
154  */
155 int QSEECom_register_listener(struct QSEECom_handle **handle,
156 			uint32_t lstnr_id, uint32_t sb_length, uint32_t flags);
157 
158 /**
159  * @brief Unregister a listener service.
160  *
161  * @param[in] handle The device handle
162  *
163  * @return Zero on success, negative on failure. errno will be set on
164  *  error.
165  */
166 int QSEECom_unregister_listener(struct QSEECom_handle *handle);
167 
168 
169 /**
170  * @brief Send QSAPP a "user" defined buffer (may contain some message/
171  * command request) and receives a response from QSAPP in receive buffer.
172  * The HLOS client writes to the send_buf, where QSAPP writes to the rcv_buf.
173  * This is a blocking call.
174  *
175  * @param[in] handle    The device handle
176  * @param[in] send_buf  The buffer to be sent.
177  *                      If using ion_sbuffer, ensure this
178  *                      QSEECOM_BUFFER_ALIGN'ed.
179  * @param[in] sbuf_len  The send buffer length
180  *                      If using ion_sbuffer, ensure length is
181  *                      multiple of QSEECOM_BUFFER_ALIGN.
182  * @param[in] rcv_buf   The QSEOS returned buffer.
183  *                      If using ion_sbuffer, ensure this is
184  *                      QSEECOM_BUFFER_ALIGN'ed.
185  * @param[in] rbuf_len  The returned buffer length.
186  *                      If using ion_sbuffer, ensure length is
187  *                      multiple of QSEECOM_BUFFER_ALIGN.
188  * @param[in] rbuf_len  The returned buffer length.
189  *
190  * @return Zero on success, negative on failure. errno will be set on
191  *  error.
192  */
193 int QSEECom_send_cmd(struct QSEECom_handle *handle, void *send_buf,
194 			uint32_t sbuf_len, void *rcv_buf, uint32_t rbuf_len);
195 
196 
197 /**
198  * @brief Send QSAPP a "user" defined buffer (may contain some message/
199  * command request) and receives a response from QSAPP in receive buffer.
200  * This API is same as send_cmd except it takes in addition parameter,
201  * "ifd_data".  This "ifd_data" holds information (ion fd handle and
202  * cmd_buf_offset) used for modifying data in the message in send_buf
203  * at an offset.  Essentailly, it has the ion fd handle information to
204  * retrieve physical address and modify the message in send_buf at the
205  * mentioned offset.
206  *
207  * The HLOS client writes to the send_buf, where QSAPP writes to the rcv_buf.
208  * This is a blocking call.
209  *
210  * @param[in] handle    The device handle
211  * @param[in] send_buf  The buffer to be sent.
212  *                      If using ion_sbuffer, ensure this
213  *                      QSEECOM_BUFFER_ALIGN'ed.
214  * @param[in] sbuf_len  The send buffer length
215  *                      If using ion_sbuffer, ensure length is
216  *                      multiple of QSEECOM_BUFFER_ALIGN.
217  * @param[in] rcv_buf   The QSEOS returned buffer.
218  *                      If using ion_sbuffer, ensure this is
219  *                      QSEECOM_BUFFER_ALIGN'ed.
220  * @param[in] rbuf_len  The returned buffer length.
221  *                      If using ion_sbuffer, ensure length is
222  *                      multiple of QSEECOM_BUFFER_ALIGN.
223  * @param[in] QSEECom_ion_fd_info  data related to memory allocated by ion.
224  *
225  * @return Zero on success, negative on failure. errno will be set on
226  *  error.
227  */
228 int QSEECom_send_modified_cmd(struct QSEECom_handle *handle, void *send_buf,
229 			uint32_t sbuf_len, void *resp_buf, uint32_t rbuf_len,
230 			struct QSEECom_ion_fd_info  *ifd_data);
231 
232 /**
233  * @brief Receive a service defined buffer.
234  *
235  * @param[in] handle    The device handle
236  * @param[out] buf      The buffer that is received
237  * @param[in] len       The receive buffer length
238  *
239  * @return Zero on success, negative on failure. errno will be set on
240  *  error.
241  */
242 int QSEECom_receive_req(struct QSEECom_handle *handle,
243 			void *buf, uint32_t len);
244 
245 /**
246  * @brief Send a response based on the previous QSEECom_receive_req.
247  *
248  * This allows a listener service to receive a command (e.g. read file abc).
249  * The service can then handle the request from QSEECom_receive_req, and provide
250  * that information back to QSAPP.
251  *
252  * This allows the HLOS to act as the server and QSAPP to behave as the client.
253  *
254  * @param[in] handle    The device handle
255  * @param[out] send_buf  The buffer to be returned back to QSAPP
256  * @param[in] len       The send buffer length
257  *
258  * @return Zero on success, negative on failure. errno will be set on
259  *  error.
260  */
261 int QSEECom_send_resp(struct QSEECom_handle *handle,
262 			void *send_buf, uint32_t len);
263 
264 /**
265  * @brief Set the bandwidth for QSEE.
266  *
267  * This API resulst in improving the performance on the Crypto hardware
268  * in QSEE. It should be called before issuing send_cmd/send_modified_cmd
269  * for commands that requires using the crypto hardware on the QSEE.
270  * Typically this API should be called before issuing the send request to
271  * enable high performance mode and after completion of the send_cmd to
272  * resume to low performance and hence to low power mode.
273  *
274  * This allows the clients of QSEECom to set the QSEE cyptpo HW bus
275  * bandwidth to high/low.
276  *
277  * @param[in] high    Set to 1 to enable bandwidth.
278  *
279  * @return Zero on success, negative on failure. errno will be set on
280  *  error.
281  */
282 int QSEECom_set_bandwidth(struct QSEECom_handle *handle, bool high);
283 
284 /**
285  * @brief Query QSEE to check if app is loaded.
286  *
287  * This API queries QSEE to see if the app is loaded or not.
288  *
289  * @param[in] app_name  Name of the app.
290  *
291  * @return QSEECOM_APP_QUERY_FAILED/QSEECOM_APP_NOT_LOADED/QSEECOM_APP_LOADED.
292  */
293 int QSEECom_app_load_query(struct QSEECom_handle *handle, char *app_name);
294 
295 #ifdef __cplusplus
296 }
297 #endif
298 
299 #endif
300