1 /* 2 * Copyright (C) 2017 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 #ifndef __CROS_EC_INCLUDE_APPLICATION_H 17 #define __CROS_EC_INCLUDE_APPLICATION_H 18 #include <stdint.h> 19 20 #ifdef __cplusplus 21 extern "C" { 22 #endif 23 24 #ifndef __packed 25 #define __packed __attribute__((packed)) 26 #endif 27 28 typedef const void * const __private; 29 30 /* 31 * Typical applications are independent tasks which are directed (or at least 32 * influenced) by some off-chip program. Communications with the applications 33 * are initiated by that off-chip Master and are routed to the application 34 * using a variety of methods. 35 */ 36 37 /****************************************************************************/ 38 /* 39 * Datagram API: 40 * 41 * Nugget OS abstracts the bus protocol (SPI, USB, whatever) into two 42 * unidirectional "datagram" transactions: 43 * 44 * - Read (the master wants data from the application) 45 * - Write (the master sends data to the application) 46 * 47 * Each transaction consists of a four-byte Command from the Master, plus zero 48 * or more data bytes either to (Read) or from (Write) the Master. 49 * 50 * The Command indicates the direction of data transfer, the application it 51 * addresses, and various other parameters. The application is responsible for 52 * providing (Read) or accepting (Write) the data bytes. 53 * 54 * Note: This interface was first used on the SPI bus, which allows for 55 * simultaneous bidirectional data transfer. We limit this interface to 56 * unidirectional transfers, because none of the other buses support that 57 * feature. 58 */ 59 60 /****************************************************************************/ 61 /* Application IDs */ 62 63 /* These two App IDs shouldn't be changed or used for other purposes */ 64 #define APP_ID_NUGGET 0x00 /* because we're selfish */ 65 #define APP_ID_TPM_REGISTER_API 0xD4 /* mandated by the TCG */ 66 /* 67 * Other App IDs are defined here. It will help avoid confusion if you use only 68 * the values from here and don't change them once they're set. But it's up to 69 * you. I'm a comment, not a cop. 70 */ 71 #define APP_ID_AVB 0x01 72 #define APP_ID_KEYMASTER 0x02 73 #define APP_ID_WEAVER 0x03 74 #define APP_ID_PROTOBUF 0x04 75 #define APP_ID_IDENTITY 0x05 76 77 /* Fake apps used only for testing */ 78 #define APP_ID_AVB_TEST 0x11 79 #define APP_ID_TRANSPORT_TEST 0x12 80 #define APP_ID_FACEAUTH_TEST 0x13 81 82 /* This app ID should only be used by tests. */ 83 #define APP_ID_TEST 0xff 84 85 /****************************************************************************/ 86 /* Other command fields */ 87 88 /* 89 * The Command encoding is: 90 * 91 * Bits 31-24 Control flags (reserved) 92 * Bits 23-16 Application ID 93 * Bits 15-0 Parameters (application-specific) 94 */ 95 96 /* Control flag bits */ 97 #define CMD_IS_READ 0x80000000 /* 1=Read, 0=Write */ 98 /* All other control flags bits are reserved */ 99 100 /* Extracting fields from a command */ 101 #define GET_APP_ID(cmd) (((cmd) & 0x00ff0000) >> 16) 102 #define GET_APP_PARAM(cmd) ((cmd) & 0x0000ffff) 103 104 /* Specifying command fields */ 105 #define CMD_ID(id) (((id) & 0x000000ff) << 16) 106 #define CMD_PARAM(p) ((p) & 0x0000ffff) 107 #define CMD_SET_PARAM(cmd, p) cmd = ((cmd & 0xffff0000) | (p & 0x0000ffff)) 108 109 /****************************************************************************/ 110 /* Data transfer */ 111 112 /* 113 * Functions of this type are invoked when the Master wants to read bytes from 114 * an application. The app should parse the Command, copy up to max_tx_size 115 * bytes into the tx_buffer provided, and return the number of bytes to send 116 * back to the Master. 117 * 118 * This is called in interrupt context, so act quickly. 119 * 120 * The last arg is for internal use. Just ignore it. 121 */ 122 typedef uint32_t (read_from_app_fn_t)(uint32_t command, 123 uint8_t *tx_buffer, 124 uint32_t max_tx_bytes, 125 __private priv); 126 127 /* 128 * Functions of this type are invoked when the Master has sent bytes to the 129 * application. The app should parse the Command and copy or process the 130 * expected number of bytes in the rx_buffer that the master has sent, up to 131 * rx_num_bytes. 132 * 133 * NOTE: Due to a quirk of the Citadel hardware, up to four extra bytes from 134 * the *next* transaction may be at the end of the rx_buffer. The application 135 * should only poke at the bytes it expects to see and ignore any extras. 136 * 137 * This is called in interrupt context, so act quickly. 138 * 139 * The last arg is for internal use. Just ignore it. 140 */ 141 typedef void (write_to_app_fn_t)(uint32_t command, 142 const uint8_t *rx_buffer, 143 uint32_t num_rx_bytes, 144 __private priv); 145 146 /* 147 * For apps that run asynchronously with little oversight, occasional 148 * Read/Write operations may be all that's necessary. An app that intercepts 149 * button presses, an accelerometer, or a fingerprint scanner can simply be 150 * told to start or stop and will send interrupts to the Master when its 151 * attention is required. 152 * 153 * Applications are free to define their own protcols and APIs using only the 154 * functions and constants above (and at least one app does just that). 155 * 156 * An app that wishes to handle its messaging using only the components 157 * described to this point would use the following macro to declare itself. 158 */ 159 160 /** 161 * This registers an application that communicates using the Datagram API, 162 * which deals only with the raw byte streams between Master (AP) and Slave 163 * (application). 164 * 165 * The name and version values may be exported to the Master by Nugget OS, so 166 * the Master can query what applications are available without blindly trying 167 * them all to see what works. 168 * 169 * @param Id The Application ID, defined above 170 * @param Name A human-readable string identifying the application 171 * @param Version An app-specific uint32_t number, for compability purposes 172 * @param From_fn A pointer to the app's read_from_app_fn_t handler 173 * @param To_fn A pointer to the app's write_to_app_fn_t handler 174 * @param Data App's private data 175 */ 176 #define DECLARE_APPLICATION_DATAGRAM(Id, Name, Version, From_fn, To_fn, Data) \ 177 const struct app_info __keep CONCAT2(app_, Id) \ 178 __attribute__((section(".rodata.app_info"))) \ 179 = { .api = { .id = Id, \ 180 .from_fn = From_fn, .to_fn = To_fn, \ 181 .data = Data}, \ 182 .version = Version, .name = Name } 183 184 /****************************************************************************/ 185 /* Transport API */ 186 /* 187 * Rather than handle unidirectonal datagrams themselves, many applications 188 * want to implement a request/response behavior, where the Master tells the 189 * app to do something and waits for it to finish and return the result. 190 * 191 * Seen from the AP's side, the application would be invoked using a blocking 192 * function something like this: 193 * 194 * uint32_t call_application(uint8_t app_id, uint16_t app_param, 195 * const uint8_t *args, uint16_t arg_len, 196 * uint8_t *reply, uint16_t *reply_len); 197 * 198 * The request or response may be larger than one bus transaction, and the AP 199 * may poll until the app finishes or wait for an interrupt before retrieving 200 * the reply (there's no difference from app's point of view). 201 * 202 * With this API, the application is initially idle. Nugget OS will marshall 203 * all the input from the Master before waking the application. The Application 204 * then performs the requested operation and transititions to a "done" state. 205 * The Master will retrieve the application status and any reply data from 206 * Nugget OS, after which the application is ready to handle the next command. 207 */ 208 209 #define TRANSPORT_V0 0x0000 210 #define TRANSPORT_V1 0x0001 211 212 /* Command information for the transport protocol. */ 213 struct transport_command_info { 214 /* v1 fields */ 215 uint16_t length; /* length of this message */ 216 uint16_t version; /* max version used by master */ 217 uint16_t crc; /* CRC of some command fields */ 218 uint16_t reply_len_hint; /* max that the master will read */ 219 } __packed; 220 221 #define COMMAND_INFO_MIN_LENGTH 8 222 #define COMMAND_INFO_MAX_LENGTH 32 223 /* If more data needs to be sent, chain a new struct to the end of this one. It 224 * will require its own CRC for data integrity and something to signify the 225 * presence of the extra data. */ 226 227 struct transport_status { 228 /* v0 fields */ 229 uint32_t status; /* status of the app */ 230 uint16_t reply_len; /* length of available response data */ 231 /* v1 fields */ 232 uint16_t length; /* length of this message */ 233 uint16_t version; /* max version used by slave */ 234 uint16_t flags; /* space for more protocol state flags */ 235 uint16_t crc; /* CRC of this status with crc set to 0 */ 236 uint16_t reply_crc; /* CRC of the reply data */ 237 } __packed; 238 239 /* Valid range of lengths for the status message */ 240 #define STATUS_MIN_LENGTH 0x10 241 #define STATUS_MAX_LENGTH 0xff 242 243 /* Flags used in the status message */ 244 #define STATUS_FLAG_WORKING 0x0001 /* added in v1 */ 245 246 /* Pre-calculated CRCs for different status responses set in the interrupt 247 * context where the CRC would otherwise not be calculated. */ 248 #define STATUS_CRC_FOR_IDLE 0x54c1 249 #define STATUS_CRC_FOR_WORKING 0x2101 250 #define STATUS_CRC_FOR_ERROR_TOO_MUCH 0x97c0 251 252 /* 253 * Applications that wish to use this transport API will need to declare a 254 * private struct app_transport which Nugget OS can use to maintain the state: 255 */ 256 struct app_transport { 257 void (*done_fn)(struct app_transport *); /* optional cleanup function */ 258 /* Note: Any done_fn() is called in interrupt context. Be quick. */ 259 uint8_t *const request; /* input data buffer */ 260 uint8_t *const response; /* output data buffer */ 261 const uint16_t max_request_len; /* input data buffer size */ 262 const uint16_t max_response_len; /* output data buffer size */ 263 /* The following are used for the incoming command. */ 264 uint32_t command; /* from master */ 265 union { 266 struct transport_command_info info; 267 uint8_t data[COMMAND_INFO_MAX_LENGTH]; /* space for future growth */ 268 } command_info; /* extra info about the command */ 269 uint16_t request_len; /* command data buffer size */ 270 uint16_t response_idx; /* current index into response */ 271 struct transport_status status[2]; /* current transport_status */ 272 volatile uint8_t status_idx; /* index of active status */ 273 }; 274 275 /* 276 * Note that request and response buffers are transferred as byte streams. 277 * However, if they will eventually represent structs, the usual ABI alignment 278 * requirements will be required. Until we've declared all applications structs 279 * in a union, we will need to align the buffers manually. Use this to declare 280 * the uint8_t buffers until then: 281 */ 282 #define __TRANSPORT_ALIGNED__ __attribute__((aligned(8))) 283 284 /* 285 * The application will need to provide a write_to_app_fn_t function that will 286 * be invoked when a new request is ready to be processed. All command and data 287 * parameters will already be present in the app's struct app_transport, so it 288 * just needs to awaken the application task to do the work. 289 * 290 * When awakened, the application task must check that there were no errors in 291 * the transmission of the request by calling this function. If it returns 292 * true, the task should go back to sleep until the next request arrives. 293 */ 294 int request_is_invalid(struct app_transport *s); 295 /* 296 * When processing is finished, the app should call the app_reply() function to 297 * return its status code and specify the length of any data it has placed into 298 * the response buffer, and then it can go back to sleep until its next 299 * invocation. CAUTION: The Master polls for app completion independently, so 300 * it may immediately begin retrieving the results as soon as this function 301 * is called *without* waiting for the Nugget OS app to go to sleep. 302 */ 303 void app_reply(struct app_transport *st, uint32_t status, uint16_t reply_len); 304 305 /* Application status codes are uint32_t, but an enum is easier to read. */ 306 enum app_status { 307 /* A few values are common to all applications */ 308 APP_SUCCESS = 0, 309 APP_ERROR_BOGUS_ARGS, /* caller being stupid */ 310 APP_ERROR_INTERNAL, /* application being stupid */ 311 APP_ERROR_TOO_MUCH, /* caller sent too much data */ 312 APP_ERROR_IO, /* problem sending or receiving data */ 313 APP_ERROR_RPC, /* problem during RPC communication */ 314 APP_ERROR_CHECKSUM, /* checksum failed, only used within protocol */ 315 APP_ERROR_BUSY, /* the app is already working on a commnad */ 316 APP_ERROR_TIMEOUT, /* the app took too long to respond */ 317 APP_ERROR_NOT_READY, /* some required condition is not satisfied */ 318 /* more? */ 319 320 /* 321 * Applications can define their own app-specific error codes. For example, 322 * app_foobar.h can do: 323 * 324 * #define APP_ERROR_FOOBAR_BAZ (APP_SPECIFIC_ERROR + 0) 325 * 326 * Do not use (APP_SPECIFIC_ERROR + N) directly in your code, because the 327 * error definition, firmware which generates it, and host code which 328 * interprets it are all in different repos. You'll never be able to keep 329 * the constants straight without using a #define or enum in your app's 330 * header file that everyone can share. 331 */ 332 APP_SPECIFIC_ERROR = 0x20, /* "should be enough for anybody" */ 333 334 /* For debugging, returning a line number might be helpful */ 335 APP_LINE_NUMBER_BASE = 0x70000000, 336 #define APP_ERROR_LINENO (APP_LINE_NUMBER_BASE + __LINE__) 337 338 /* Bit 31 is reserved for internal use */ 339 MAX_APP_STATUS = 0x7fffffff, 340 }; 341 342 /** 343 * This registers an application that communicates using the Transport API. 344 * 345 * The name and version values may be exported to the Master by Nugget OS, so 346 * the Master can query what applications are available without blindly trying 347 * them all to see what works. 348 * 349 * @param Id The Application ID, defined above 350 * @param Name A human-readable string identifying the application 351 * @param Version An app-specific uint32_t number, for compability purposes 352 * @param State A pointer to the app's struct app_transport 353 * @param To_fn A pointer to the app's write_to_app_fn_t handler 354 */ 355 #define DECLARE_APPLICATION_TRANSPORT(Id, Name, Version, State, To_fn) \ 356 const struct app_info __keep CONCAT2(app_, Id) \ 357 __attribute__((section(".rodata.app_info"))) \ 358 = { .api = { .id = Id, \ 359 .from_fn = transaction_api_from_fn, \ 360 .to_fn = transaction_api_to_fn, \ 361 .data = &(const struct datagram_api) \ 362 { .id = Id, .to_fn = To_fn, \ 363 .data = State } }, \ 364 .version = Version, .name = Name } 365 366 /****************************************************************************/ 367 /* Pay no attention to that man behind the curtain */ 368 369 /* We'll allow 31 bits of application status. We need one bit for transport. */ 370 #define APP_STATUS_IDLE 0x00000000 /* waiting for instructions */ 371 #define APP_STATUS_DONE 0x80000000 /* finished, reply is ready */ 372 #define APP_STATUS_CODE(res) ((res) & 0x7fffffff) /* actual status */ 373 374 /* Datagram API needs this info */ 375 struct datagram_api { 376 uint8_t id; 377 read_from_app_fn_t * const from_fn; 378 write_to_app_fn_t * const to_fn; 379 const void * const data; 380 }; 381 382 /* Here's the struct that keeps track of registered applications */ 383 struct app_info { 384 struct datagram_api api; 385 uint32_t version; 386 const char * const name; 387 }; 388 389 /* These handle the Transport API */ 390 extern read_from_app_fn_t transaction_api_from_fn; 391 extern write_to_app_fn_t transaction_api_to_fn; 392 393 /* Command flags used internally by Transport API messages */ 394 #define CMD_TRANSPORT 0x40000000 /* 1=Transport API message */ 395 /* When CMD_TRANSPORT is set, the following bits have meaning */ 396 #define CMD_IS_DATA 0x20000000 /* 1=data msg 0=status msg */ 397 #define CMD_MORE_TO_COME 0x10000000 /* 1=continued 0=new */ 398 399 #ifdef __cplusplus 400 } 401 #endif 402 403 #endif /* __CROS_EC_INCLUDE_APPLICATION_H */ 404