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