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