1 /*
2  * Copyright (C) 2015-2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may 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 implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <stdint.h>
20 
21 /*
22  * Storage port names
23  * @STORAGE_CLIENT_TD_PORT:     Port used by clients that require tamper and
24  *                              rollback detection.
25  * @STORAGE_CLIENT_TDEA_PORT:   Port used by clients that require storage before
26  *                              the non-secure os has booted.
27  * @STORAGE_CLIENT_TP_PORT:     Port used by clients that require tamper proof
28  *                              storage. Note that non-secure code can prevent
29                                 read and write operations from succeeding, but
30                                 it cannot modify on-disk data.
31  * @STORAGE_DISK_PROXY_PORT:    Port used by non-secure proxy server
32  */
33 #define STORAGE_CLIENT_TD_PORT     "com.android.trusty.storage.client.td"
34 #define STORAGE_CLIENT_TDEA_PORT   "com.android.trusty.storage.client.tdea"
35 #define STORAGE_CLIENT_TP_PORT     "com.android.trusty.storage.client.tp"
36 #define STORAGE_DISK_PROXY_PORT    "com.android.trusty.storage.proxy"
37 
38 enum storage_cmd {
39 	STORAGE_REQ_SHIFT = 1,
40 	STORAGE_RESP_BIT  = 1,
41 
42 	STORAGE_RESP_MSG_ERR   = STORAGE_RESP_BIT,
43 
44 	STORAGE_FILE_DELETE    = 1 << STORAGE_REQ_SHIFT,
45 	STORAGE_FILE_OPEN      = 2 << STORAGE_REQ_SHIFT,
46 	STORAGE_FILE_CLOSE     = 3 << STORAGE_REQ_SHIFT,
47 	STORAGE_FILE_READ      = 4 << STORAGE_REQ_SHIFT,
48 	STORAGE_FILE_WRITE     = 5 << STORAGE_REQ_SHIFT,
49 	STORAGE_FILE_GET_SIZE  = 6 << STORAGE_REQ_SHIFT,
50 	STORAGE_FILE_SET_SIZE  = 7 << STORAGE_REQ_SHIFT,
51 
52 	STORAGE_RPMB_SEND      = 8 << STORAGE_REQ_SHIFT,
53 
54 	/* transaction support */
55 	STORAGE_END_TRANSACTION = 9 << STORAGE_REQ_SHIFT,
56 
57 	STORAGE_FILE_GET_MAX_SIZE = 12 << STORAGE_REQ_SHIFT,
58 };
59 
60 /**
61  * enum storage_err - error codes for storage protocol
62  * @STORAGE_NO_ERROR:           all OK
63  * @STORAGE_ERR_GENERIC:        unknown error. Can occur when there's an internal server
64  *                              error, e.g. the server runs out of memory or is in a bad state.
65  * @STORAGE_ERR_NOT_VALID:      input not valid. May occur if the arguments passed
66  *                              into the command are not valid, for example if the file handle
67  *                              passed in is not a valid one.
68  * @STORAGE_ERR_UNIMPLEMENTED:  the command passed in is not recognized
69  * @STORAGE_ERR_ACCESS:         the file is not accessible in the requested mode
70  * @STORAGE_ERR_NOT_FOUND:      the file was not found
71  * @STORAGE_ERR_EXIST           the file exists when it shouldn't as in with OPEN_CREATE | OPEN_EXCLUSIVE.
72  * @STORAGE_ERR_TRANSACT        returned by various operations to indicate that current transaction
73  *                              is in error state. Such state could be only cleared by sending
74  *                              STORAGE_END_TRANSACTION message.
75  * @STORAGE_ERR_SYNC_FAILURE    indicates that the current operation failed to sync
76  *                              to disk. Only returned if STORAGE_MSG_FLAG_PRE_COMMIT or
77  *                              STORAGE_MSG_FLAG_POST_COMMIT was set for the request.
78  */
79 enum storage_err {
80 	STORAGE_NO_ERROR          = 0,
81 	STORAGE_ERR_GENERIC       = 1,
82 	STORAGE_ERR_NOT_VALID     = 2,
83 	STORAGE_ERR_UNIMPLEMENTED = 3,
84 	STORAGE_ERR_ACCESS        = 4,
85 	STORAGE_ERR_NOT_FOUND     = 5,
86 	STORAGE_ERR_EXIST         = 6,
87 	STORAGE_ERR_TRANSACT      = 7,
88 	STORAGE_ERR_SYNC_FAILURE  = 8,
89 };
90 
91 /**
92  * storage_delete_flag - flags for controlling delete semantics
93  */
94 enum storage_file_delete_flag {
95 	STORAGE_FILE_DELETE_MASK = 0,
96 };
97 
98 /**
99  * storage_file_flag - Flags to control 'open' semantics.
100  * @STORAGE_FILE_OPEN_CREATE:           if this file does not exist, create it.
101  * @STORAGE_FILE_OPEN_CREATE_EXCLUSIVE: causes STORAGE_FILE_OPEN_CREATE to fail if the file
102  *                                      already exists. Only meaningful if used in combination
103  *                                      with STORAGE_FILE_OPEN_CREATE.
104  * @STORAGE_FILE_OPEN_TRUNCATE:         if this file already exists, discard existing content
105  *                                      and open it as a new file. No change in semantics if the
106  *                                      file does not exist.
107  * @STORAGE_FILE_OPEN_MASK:             mask for all open flags supported in current protocol.
108  *                                      All other bits must be set to 0.
109  */
110 enum storage_file_open_flag {
111 	STORAGE_FILE_OPEN_CREATE             = (1 << 0),
112 	STORAGE_FILE_OPEN_CREATE_EXCLUSIVE   = (1 << 1),
113 	STORAGE_FILE_OPEN_TRUNCATE           = (1 << 2),
114 	STORAGE_FILE_OPEN_MASK               = STORAGE_FILE_OPEN_CREATE |
115 					       STORAGE_FILE_OPEN_TRUNCATE |
116 					       STORAGE_FILE_OPEN_CREATE_EXCLUSIVE,
117 };
118 
119 /**
120  * enum storage_msg_flag - protocol-level flags in struct storage_msg
121  * @STORAGE_MSG_FLAG_BATCH:                 if set, command belongs to a batch transaction.
122  *                                          No response will be sent by the server until
123  *                                          it receives a command with this flag unset, at
124  *                                          which point a cumulative result for all messages
125  *                                          sent with STORAGE_MSG_FLAG_BATCH will be sent.
126  *                                          This is only supported by the non-secure disk proxy
127  *                                          server.
128  * @STORAGE_MSG_FLAG_PRE_COMMIT:            if set, indicates that server need to commit
129  *                                          pending changes before processing this message.
130  * @STORAGE_MSG_FLAG_POST_COMMIT:           if set, indicates that server need to commit
131  *                                          pending changes after processing this message.
132  * @STORAGE_MSG_FLAG_TRANSACT_COMPLETE:     if set, indicates that server need to commit
133  *                                          current transaction after processing this message.
134  *                                          It is an alias for STORAGE_MSG_FLAG_POST_COMMIT.
135  * @STORAGE_MSG_FLAG_PRE_COMMIT_CHECKPOINT: if set, indicates that server needs to ensure
136  *                                          that there is not a pending checkpoint for
137  *                                          userdata before processing this message.
138  */
139 enum storage_msg_flag {
140     STORAGE_MSG_FLAG_BATCH = 0x1,
141     STORAGE_MSG_FLAG_PRE_COMMIT = 0x2,
142     STORAGE_MSG_FLAG_POST_COMMIT = 0x4,
143     STORAGE_MSG_FLAG_TRANSACT_COMPLETE = STORAGE_MSG_FLAG_POST_COMMIT,
144     STORAGE_MSG_FLAG_PRE_COMMIT_CHECKPOINT = 0x8,
145 };
146 
147 /*
148  * The following declarations are the message-specific contents of
149  * the 'payload' element inside struct storage_msg.
150  */
151 
152 /**
153  * struct storage_file_delete_req - request format for STORAGE_FILE_DELETE
154  * @flags: currently unused, must be set to 0.
155  * @name:  the name of the file
156  */
157 struct storage_file_delete_req {
158 	uint32_t flags;
159 	char name[0];
160 };
161 
162 /**
163  * struct storage_file_open_req - request format for STORAGE_FILE_OPEN
164  * @flags: any of enum storage_file_flag or'ed together
165  * @name:  the name of the file
166  */
167 struct storage_file_open_req {
168 	uint32_t flags;
169 	char     name[0];
170 };
171 
172 /**
173  * struct storage_file_open_resp - response format for STORAGE_FILE_OPEN
174  * @handle: opaque handle to the opened file. Only present on success.
175  */
176 struct storage_file_open_resp {
177 	uint32_t handle;
178 };
179 
180 /**
181  * struct storage_file_close_req - request format for STORAGE_FILE_CLOSE
182  * @handle: the handle for the file to close
183  */
184 struct storage_file_close_req {
185 	uint32_t handle;
186 };
187 
188 /**
189  * struct storage_file_get_max_size_req - request format for
190  *                                        STORAGE_FILE_GET_MAX_SIZE
191  * @handle: the handle for the file whose max size is requested
192  */
193 struct storage_file_get_max_size_req {
194 	uint32_t handle;
195 };
196 
197 /**
198  * struct storage_file_get_max_size_resp - response format for
199  *                                         STORAGE_FILE_GET_MAX_SIZE
200  * @max_size:   the maximum size of the file
201  */
202 struct storage_file_get_max_size_resp {
203 	uint64_t max_size;
204 };
205 
206 /**
207  * struct storage_file_read_req - request format for STORAGE_FILE_READ
208  * @handle: the handle for the file from which to read
209  * @size:   the quantity of bytes to read from the file
210  * @offset: the offset in the file from whence to read
211  */
212 struct storage_file_read_req {
213 	uint32_t handle;
214 	uint32_t size;
215 	uint64_t offset;
216 };
217 
218 /**
219  * struct storage_file_read_resp - response format for STORAGE_FILE_READ
220  * @data: beginning of data retrieved from file
221  */
222 struct storage_file_read_resp {
223 	uint8_t data[0];
224 };
225 
226 /**
227  * struct storage_file_write_req - request format for STORAGE_FILE_WRITE
228  * @handle:     the handle for the file to write to
229  * @offset:     the offset in the file from whence to write
230  * @__reserved: unused, must be set to 0.
231  * @data:       beginning of the data to be written
232  */
233 struct storage_file_write_req {
234 	uint64_t offset;
235 	uint32_t handle;
236 	uint32_t __reserved;
237 	uint8_t  data[0];
238 };
239 
240 /**
241  * struct storage_file_get_size_req - request format for STORAGE_FILE_GET_SIZE
242  * @handle: handle for which the size is requested
243  */
244 struct storage_file_get_size_req {
245 	uint32_t handle;
246 };
247 
248 /**
249  * struct storage_file_get_size_resp - response format for STORAGE_FILE_GET_SIZE
250  * @size:   the size of the file
251  */
252 struct storage_file_get_size_resp {
253 	uint64_t size;
254 };
255 
256 /**
257  * struct storage_file_set_size_req - request format for STORAGE_FILE_SET_SIZE
258  * @handle: the file handle
259  * @size:   the desired size of the file
260  */
261 struct storage_file_set_size_req {
262 	uint64_t size;
263 	uint32_t handle;
264 };
265 
266 /**
267  * struct storage_rpmb_send_req - request format for STORAGE_RPMB_SEND
268  * @reliable_write_size:        size in bytes of reliable write region
269  * @write_size:                 size in bytes of write region
270  * @read_size:                  number of bytes to read for a read request
271  * @__reserved:                 unused, must be set to 0
272  * @payload:                    start of reliable write region, followed by
273  *                              write region.
274  *
275  * Only used in proxy<->server interface.
276  */
277 struct storage_rpmb_send_req {
278 	uint32_t reliable_write_size;
279 	uint32_t write_size;
280 	uint32_t read_size;
281 	uint32_t __reserved;
282 	uint8_t  payload[0];
283 };
284 
285 /**
286  * struct storage_rpmb_send_resp: response type for STORAGE_RPMB_SEND
287  * @data: the data frames frames retrieved from the MMC.
288  */
289 struct storage_rpmb_send_resp {
290 	uint8_t data[0];
291 };
292 
293 /**
294  * struct storage_msg - generic req/resp format for all storage commands
295  * @cmd:        one of enum storage_cmd
296  * @op_id:      client chosen operation identifier for an instance
297  *              of a command or atomic grouping of commands (transaction).
298  * @flags:      one or many of enum storage_msg_flag or'ed together.
299  * @size:       total size of the message including this header
300  * @result:     one of enum storage_err
301  * @__reserved: unused, must be set to 0.
302  * @payload:    beginning of command specific message format
303  */
304 struct storage_msg {
305 	uint32_t cmd;
306 	uint32_t op_id;
307 	uint32_t flags;
308 	uint32_t size;
309 	int32_t  result;
310 	uint32_t __reserved;
311 	uint8_t  payload[0];
312 };
313 
314