• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
58 /**
59  * enum storage_err - error codes for storage protocol
60  * @STORAGE_NO_ERROR:           all OK
61  * @STORAGE_ERR_GENERIC:        unknown error. Can occur when there's an internal server
62  *                              error, e.g. the server runs out of memory or is in a bad state.
63  * @STORAGE_ERR_NOT_VALID:      input not valid. May occur if the arguments passed
64  *                              into the command are not valid, for example if the file handle
65  *                              passed in is not a valid one.
66  * @STORAGE_ERR_UNIMPLEMENTED:  the command passed in is not recognized
67  * @STORAGE_ERR_ACCESS:         the file is not accessible in the requested mode
68  * @STORAGE_ERR_NOT_FOUND:      the file was not found
69  * @STORAGE_ERR_EXIST           the file exists when it shouldn't as in with OPEN_CREATE | OPEN_EXCLUSIVE.
70  * @STORAGE_ERR_TRANSACT        returned by various operations to indicate that current transaction
71  *                              is in error state. Such state could be only cleared by sending
72  *                              STORAGE_END_TRANSACTION message.
73  * @STORAGE_ERR_SYNC_FAILURE    indicates that the current operation failed to sync
74  *                              to disk. Only returned if STORAGE_MSG_FLAG_PRE_COMMIT or
75  *                              STORAGE_MSG_FLAG_POST_COMMIT was set for the request.
76  */
77 enum storage_err {
78 	STORAGE_NO_ERROR          = 0,
79 	STORAGE_ERR_GENERIC       = 1,
80 	STORAGE_ERR_NOT_VALID     = 2,
81 	STORAGE_ERR_UNIMPLEMENTED = 3,
82 	STORAGE_ERR_ACCESS        = 4,
83 	STORAGE_ERR_NOT_FOUND     = 5,
84 	STORAGE_ERR_EXIST         = 6,
85 	STORAGE_ERR_TRANSACT      = 7,
86 	STORAGE_ERR_SYNC_FAILURE  = 8,
87 };
88 
89 /**
90  * storage_delete_flag - flags for controlling delete semantics
91  */
92 enum storage_file_delete_flag {
93 	STORAGE_FILE_DELETE_MASK = 0,
94 };
95 
96 /**
97  * storage_file_flag - Flags to control 'open' semantics.
98  * @STORAGE_FILE_OPEN_CREATE:           if this file does not exist, create it.
99  * @STORAGE_FILE_OPEN_CREATE_EXCLUSIVE: causes STORAGE_FILE_OPEN_CREATE to fail if the file
100  *                                      already exists. Only meaningful if used in combination
101  *                                      with STORAGE_FILE_OPEN_CREATE.
102  * @STORAGE_FILE_OPEN_TRUNCATE:         if this file already exists, discard existing content
103  *                                      and open it as a new file. No change in semantics if the
104  *                                      file does not exist.
105  * @STORAGE_FILE_OPEN_MASK:             mask for all open flags supported in current protocol.
106  *                                      All other bits must be set to 0.
107  */
108 enum storage_file_open_flag {
109 	STORAGE_FILE_OPEN_CREATE             = (1 << 0),
110 	STORAGE_FILE_OPEN_CREATE_EXCLUSIVE   = (1 << 1),
111 	STORAGE_FILE_OPEN_TRUNCATE           = (1 << 2),
112 	STORAGE_FILE_OPEN_MASK               = STORAGE_FILE_OPEN_CREATE |
113 					       STORAGE_FILE_OPEN_TRUNCATE |
114 					       STORAGE_FILE_OPEN_CREATE_EXCLUSIVE,
115 };
116 
117 /**
118  * enum storage_msg_flag - protocol-level flags in struct storage_msg
119  * @STORAGE_MSG_FLAG_BATCH:                 if set, command belongs to a batch transaction.
120  *                                          No response will be sent by the server until
121  *                                          it receives a command with this flag unset, at
122  *                                          which point a cumulative result for all messages
123  *                                          sent with STORAGE_MSG_FLAG_BATCH will be sent.
124  *                                          This is only supported by the non-secure disk proxy
125  *                                          server.
126  * @STORAGE_MSG_FLAG_PRE_COMMIT:            if set, indicates that server need to commit
127  *                                          pending changes before processing this message.
128  * @STORAGE_MSG_FLAG_POST_COMMIT:           if set, indicates that server need to commit
129  *                                          pending changes after processing this message.
130  * @STORAGE_MSG_FLAG_TRANSACT_COMPLETE:     if set, indicates that server need to commit
131  *                                          current transaction after processing this message.
132  *                                          It is an alias for STORAGE_MSG_FLAG_POST_COMMIT.
133  * @STORAGE_MSG_FLAG_PRE_COMMIT_CHECKPOINT: if set, indicates that server needs to ensure
134  *                                          that there is not a pending checkpoint for
135  *                                          userdata before processing this message.
136  */
137 enum storage_msg_flag {
138     STORAGE_MSG_FLAG_BATCH = 0x1,
139     STORAGE_MSG_FLAG_PRE_COMMIT = 0x2,
140     STORAGE_MSG_FLAG_POST_COMMIT = 0x4,
141     STORAGE_MSG_FLAG_TRANSACT_COMPLETE = STORAGE_MSG_FLAG_POST_COMMIT,
142     STORAGE_MSG_FLAG_PRE_COMMIT_CHECKPOINT = 0x8,
143 };
144 
145 /*
146  * The following declarations are the message-specific contents of
147  * the 'payload' element inside struct storage_msg.
148  */
149 
150 /**
151  * struct storage_file_delete_req - request format for STORAGE_FILE_DELETE
152  * @flags: currently unused, must be set to 0.
153  * @name:  the name of the file
154  */
155 struct storage_file_delete_req {
156 	uint32_t flags;
157 	char name[0];
158 };
159 
160 /**
161  * struct storage_file_open_req - request format for STORAGE_FILE_OPEN
162  * @flags: any of enum storage_file_flag or'ed together
163  * @name:  the name of the file
164  */
165 struct storage_file_open_req {
166 	uint32_t flags;
167 	char     name[0];
168 };
169 
170 /**
171  * struct storage_file_open_resp - response format for STORAGE_FILE_OPEN
172  * @handle: opaque handle to the opened file. Only present on success.
173  */
174 struct storage_file_open_resp {
175 	uint32_t handle;
176 };
177 
178 /**
179  * struct storage_file_close_req - request format for STORAGE_FILE_CLOSE
180  * @handle: the handle for the file to close
181  */
182 struct storage_file_close_req {
183 	uint32_t handle;
184 };
185 
186 /**
187  * struct storage_file_read_req - request format for STORAGE_FILE_READ
188  * @handle: the handle for the file from which to read
189  * @size:   the quantity of bytes to read from the file
190  * @offset: the offset in the file from whence to read
191  */
192 struct storage_file_read_req {
193 	uint32_t handle;
194 	uint32_t size;
195 	uint64_t offset;
196 };
197 
198 /**
199  * struct storage_file_read_resp - response format for STORAGE_FILE_READ
200  * @data: beginning of data retrieved from file
201  */
202 struct storage_file_read_resp {
203 	uint8_t data[0];
204 };
205 
206 /**
207  * struct storage_file_write_req - request format for STORAGE_FILE_WRITE
208  * @handle:     the handle for the file to write to
209  * @offset:     the offset in the file from whence to write
210  * @__reserved: unused, must be set to 0.
211  * @data:       beginning of the data to be written
212  */
213 struct storage_file_write_req {
214 	uint64_t offset;
215 	uint32_t handle;
216 	uint32_t __reserved;
217 	uint8_t  data[0];
218 };
219 
220 /**
221  * struct storage_file_get_size_req - request format for STORAGE_FILE_GET_SIZE
222  * @handle: handle for which the size is requested
223  */
224 struct storage_file_get_size_req {
225 	uint32_t handle;
226 };
227 
228 /**
229  * struct storage_file_get_size_resp - response format for STORAGE_FILE_GET_SIZE
230  * @size:   the size of the file
231  */
232 struct storage_file_get_size_resp {
233 	uint64_t size;
234 };
235 
236 /**
237  * struct storage_file_set_size_req - request format for STORAGE_FILE_SET_SIZE
238  * @handle: the file handle
239  * @size:   the desired size of the file
240  */
241 struct storage_file_set_size_req {
242 	uint64_t size;
243 	uint32_t handle;
244 };
245 
246 /**
247  * struct storage_rpmb_send_req - request format for STORAGE_RPMB_SEND
248  * @reliable_write_size:        size in bytes of reliable write region
249  * @write_size:                 size in bytes of write region
250  * @read_size:                  number of bytes to read for a read request
251  * @__reserved:                 unused, must be set to 0
252  * @payload:                    start of reliable write region, followed by
253  *                              write region.
254  *
255  * Only used in proxy<->server interface.
256  */
257 struct storage_rpmb_send_req {
258 	uint32_t reliable_write_size;
259 	uint32_t write_size;
260 	uint32_t read_size;
261 	uint32_t __reserved;
262 	uint8_t  payload[0];
263 };
264 
265 /**
266  * struct storage_rpmb_send_resp: response type for STORAGE_RPMB_SEND
267  * @data: the data frames frames retrieved from the MMC.
268  */
269 struct storage_rpmb_send_resp {
270 	uint8_t data[0];
271 };
272 
273 /**
274  * struct storage_msg - generic req/resp format for all storage commands
275  * @cmd:        one of enum storage_cmd
276  * @op_id:      client chosen operation identifier for an instance
277  *              of a command or atomic grouping of commands (transaction).
278  * @flags:      one or many of enum storage_msg_flag or'ed together.
279  * @size:       total size of the message including this header
280  * @result:     one of enum storage_err
281  * @__reserved: unused, must be set to 0.
282  * @payload:    beginning of command specific message format
283  */
284 struct storage_msg {
285 	uint32_t cmd;
286 	uint32_t op_id;
287 	uint32_t flags;
288 	uint32_t size;
289 	int32_t  result;
290 	uint32_t __reserved;
291 	uint8_t  payload[0];
292 };
293 
294