The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | |
| 2 | --- a replacement for aproto ------------------------------------------- |
| 3 | |
| 4 | When it comes down to it, aproto's primary purpose is to forward |
| 5 | various streams between the host computer and client device (in either |
| 6 | direction). |
| 7 | |
| 8 | This replacement further simplifies the concept, reducing the protocol |
| 9 | to an extremely straightforward model optimized to accomplish the |
| 10 | forwarding of these streams and removing additional state or |
| 11 | complexity. |
| 12 | |
| 13 | The host side becomes a simple comms bridge with no "UI", which will |
| 14 | be used by either commandline or interactive tools to communicate with |
| 15 | a device or emulator that is connected to the bridge. |
| 16 | |
| 17 | The protocol is designed to be straightforward and well-defined enough |
| 18 | that if it needs to be reimplemented in another environment (Java |
| 19 | perhaps), there should not problems ensuring perfect interoperability. |
| 20 | |
| 21 | The protocol discards the layering aproto has and should allow the |
| 22 | implementation to be much more robust. |
| 23 | |
| 24 | |
| 25 | --- protocol overview and basics --------------------------------------- |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 26 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | The transport layer deals in "messages", which consist of a 24 byte |
| 28 | header followed (optionally) by a payload. The header consists of 6 |
| 29 | 32 bit words which are sent across the wire in little endian format. |
| 30 | |
| 31 | struct message { |
Eyal Lezmy | 2ff70ab | 2016-08-30 00:53:08 +0200 | [diff] [blame] | 32 | unsigned command; /* command identifier constant (A_CNXN, ...) */ |
| 33 | unsigned arg0; /* first argument */ |
| 34 | unsigned arg1; /* second argument */ |
| 35 | unsigned data_length; /* length of payload (0 is allowed) */ |
| 36 | unsigned data_crc32; /* crc32 of data payload */ |
| 37 | unsigned magic; /* command ^ 0xffffffff */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | Receipt of an invalid message header, corrupt message payload, or an |
| 41 | unrecognized command MUST result in the closing of the remote |
| 42 | connection. The protocol depends on shared state and any break in the |
| 43 | message stream will result in state getting out of sync. |
| 44 | |
| 45 | The following sections describe the six defined message types in |
| 46 | detail. Their format is COMMAND(arg0, arg1, payload) where the payload |
| 47 | is represented by a quoted string or an empty string if none should be |
| 48 | sent. |
| 49 | |
| 50 | The identifiers "local-id" and "remote-id" are always relative to the |
| 51 | *sender* of the message, so for a receiver, the meanings are effectively |
| 52 | reversed. |
| 53 | |
| 54 | |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 55 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | --- CONNECT(version, maxdata, "system-identity-string") ---------------- |
| 57 | |
Eyal Lezmy | 2ff70ab | 2016-08-30 00:53:08 +0200 | [diff] [blame] | 58 | Command constant: A_CNXN |
| 59 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | The CONNECT message establishes the presence of a remote system. |
| 61 | The version is used to ensure protocol compatibility and maxdata |
| 62 | declares the maximum message body size that the remote system |
| 63 | is willing to accept. |
| 64 | |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 65 | Currently, version=0x01000000 and maxdata=256*1024. Older versions of adb |
| 66 | hard-coded maxdata=4096, so CONNECT and AUTH packets sent to a device must not |
| 67 | be larger than that because they're sent before the CONNECT from the device |
| 68 | that tells the adb server what maxdata the device can support. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 69 | |
| 70 | Both sides send a CONNECT message when the connection between them is |
| 71 | established. Until a CONNECT message is received no other messages may |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 72 | be sent. Any messages received before a CONNECT message MUST be ignored. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 73 | |
| 74 | If a CONNECT message is received with an unknown version or insufficiently |
| 75 | large maxdata value, the connection with the other side must be closed. |
| 76 | |
| 77 | The system identity string should be "<systemtype>:<serialno>:<banner>" |
| 78 | where systemtype is "bootloader", "device", or "host", serialno is some |
| 79 | kind of unique ID (or empty), and banner is a human-readable version |
Scott Anderson | fa02092 | 2012-05-25 14:10:02 -0700 | [diff] [blame] | 80 | or identifier string. The banner is used to transmit useful properties. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 82 | --- STLS(type, version, "") -------------------------------------------- |
| 83 | |
| 84 | Command constant: A_STLS |
| 85 | |
| 86 | The TLS message informs the recipient that the connection will be encrypted |
| 87 | and will need to perform a TLS handshake. version is the current version of |
| 88 | the protocol. |
| 89 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 90 | |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 91 | --- AUTH(type, 0, "data") ---------------------------------------------- |
| 92 | |
Eyal Lezmy | 2ff70ab | 2016-08-30 00:53:08 +0200 | [diff] [blame] | 93 | Command constant: A_AUTH |
| 94 | |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 95 | The AUTH message informs the recipient that authentication is required to |
| 96 | connect to the sender. If type is TOKEN(1), data is a random token that |
| 97 | the recipient can sign with a private key. The recipient replies with an |
| 98 | AUTH packet where type is SIGNATURE(2) and data is the signature. If the |
| 99 | signature verification succeeds, the sender replies with a CONNECT packet. |
| 100 | |
| 101 | If the signature verification fails, the sender replies with a new AUTH |
| 102 | packet and a new random token, so that the recipient can retry signing |
| 103 | with a different private key. |
| 104 | |
| 105 | Once the recipient has tried all its private keys, it can reply with an |
| 106 | AUTH packet where type is RSAPUBLICKEY(3) and data is the public key. If |
| 107 | possible, an on-screen confirmation may be displayed for the user to |
| 108 | confirm they want to install the public key on the device. |
| 109 | |
| 110 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 111 | --- OPEN(local-id, 0, "destination") ----------------------------------- |
| 112 | |
Eyal Lezmy | 2ff70ab | 2016-08-30 00:53:08 +0200 | [diff] [blame] | 113 | Command constant: A_OPEN |
| 114 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 115 | The OPEN message informs the recipient that the sender has a stream |
| 116 | identified by local-id that it wishes to connect to the named |
| 117 | destination in the message payload. The local-id may not be zero. |
| 118 | |
| 119 | The OPEN message MUST result in either a READY message indicating that |
| 120 | the connection has been established (and identifying the other end) or |
| 121 | a CLOSE message, indicating failure. An OPEN message also implies |
| 122 | a READY message sent at the same time. |
| 123 | |
| 124 | Common destination naming conventions include: |
| 125 | |
| 126 | * "tcp:<host>:<port>" - host may be omitted to indicate localhost |
| 127 | * "udp:<host>:<port>" - host may be omitted to indicate localhost |
Andrew Walbran | ebf09dd | 2021-03-03 18:06:12 +0000 | [diff] [blame] | 128 | * "vsock:<CID>:<port>" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | * "local-dgram:<identifier>" |
| 130 | * "local-stream:<identifier>" |
| 131 | * "shell" - local shell service |
| 132 | * "upload" - service for pushing files across (like aproto's /sync) |
| 133 | * "fs-bridge" - FUSE protocol filesystem bridge |
| 134 | |
| 135 | |
| 136 | --- READY(local-id, remote-id, "") ------------------------------------- |
| 137 | |
Eyal Lezmy | 2ff70ab | 2016-08-30 00:53:08 +0200 | [diff] [blame] | 138 | Command constant: A_OKAY |
| 139 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 | The READY message informs the recipient that the sender's stream |
| 141 | identified by local-id is ready for write messages and that it is |
| 142 | connected to the recipient's stream identified by remote-id. |
| 143 | |
Eyal Lezmy | 2ff70ab | 2016-08-30 00:53:08 +0200 | [diff] [blame] | 144 | Neither the local-id nor the remote-id may be zero. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 145 | |
| 146 | A READY message containing a remote-id which does not map to an open |
| 147 | stream on the recipient's side is ignored. The stream may have been |
| 148 | closed while this message was in-flight. |
| 149 | |
| 150 | The local-id is ignored on all but the first READY message (where it |
| 151 | is used to establish the connection). Nonetheless, the local-id MUST |
| 152 | not change on later READY messages sent to the same stream. |
| 153 | |
| 154 | |
Derrick Bonafilia | 65f76dd | 2015-07-06 10:19:28 -0700 | [diff] [blame] | 155 | --- WRITE(local-id, remote-id, "data") --------------------------------- |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 156 | |
Eyal Lezmy | 2ff70ab | 2016-08-30 00:53:08 +0200 | [diff] [blame] | 157 | Command constant: A_WRTE |
| 158 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | The WRITE message sends data to the recipient's stream identified by |
| 160 | remote-id. The payload MUST be <= maxdata in length. |
| 161 | |
| 162 | A WRITE message containing a remote-id which does not map to an open |
| 163 | stream on the recipient's side is ignored. The stream may have been |
| 164 | closed while this message was in-flight. |
| 165 | |
| 166 | A WRITE message may not be sent until a READY message is received. |
| 167 | Once a WRITE message is sent, an additional WRITE message may not be |
| 168 | sent until another READY message has been received. Recipients of |
| 169 | a WRITE message that is in violation of this requirement will CLOSE |
| 170 | the connection. |
| 171 | |
| 172 | |
| 173 | --- CLOSE(local-id, remote-id, "") ------------------------------------- |
| 174 | |
Eyal Lezmy | 2ff70ab | 2016-08-30 00:53:08 +0200 | [diff] [blame] | 175 | Command constant: A_CLSE |
| 176 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | The CLOSE message informs recipient that the connection between the |
| 178 | sender's stream (local-id) and the recipient's stream (remote-id) is |
| 179 | broken. The remote-id MUST not be zero, but the local-id MAY be zero |
| 180 | if this CLOSE indicates a failed OPEN. |
| 181 | |
| 182 | A CLOSE message containing a remote-id which does not map to an open |
| 183 | stream on the recipient's side is ignored. The stream may have |
| 184 | already been closed by the recipient while this message was in-flight. |
| 185 | |
| 186 | The recipient should not respond to a CLOSE message in any way. The |
| 187 | recipient should cancel pending WRITEs or CLOSEs, but this is not a |
| 188 | requirement, since they will be ignored. |
| 189 | |
| 190 | |
| 191 | --- SYNC(online, sequence, "") ----------------------------------------- |
| 192 | |
Eyal Lezmy | 2ff70ab | 2016-08-30 00:53:08 +0200 | [diff] [blame] | 193 | Command constant: A_SYNC |
| 194 | |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 195 | *** obsolete, no longer used *** |
| 196 | |
| 197 | The SYNC message was used by the io pump to make sure that stale |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 198 | outbound messages are discarded when the connection to the remote side |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 199 | is broken. It was only used internally to the bridge and never valid |
Eyal Lezmy | 2ff70ab | 2016-08-30 00:53:08 +0200 | [diff] [blame] | 200 | to send across the wire. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 201 | |
Eyal Lezmy | 2ff70ab | 2016-08-30 00:53:08 +0200 | [diff] [blame] | 202 | * when the connection to the remote side goes offline, the io pump |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 203 | sends a SYNC(0, 0) and starts discarding all messages |
| 204 | * when the connection to the remote side is established, the io pump |
| 205 | sends a SYNC(1, token) and continues to discard messages |
| 206 | * when the io pump receives a matching SYNC(1, token), it once again |
| 207 | starts accepting messages to forward to the remote side |
| 208 | |
| 209 | |
| 210 | --- message command constants ------------------------------------------ |
| 211 | |
| 212 | #define A_SYNC 0x434e5953 |
| 213 | #define A_CNXN 0x4e584e43 |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 214 | #define A_AUTH 0x48545541 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 215 | #define A_OPEN 0x4e45504f |
| 216 | #define A_OKAY 0x59414b4f |
| 217 | #define A_CLSE 0x45534c43 |
| 218 | #define A_WRTE 0x45545257 |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 219 | #define A_STLS 0x534C5453 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | |
| 221 | |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 222 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 223 | --- implementation details --------------------------------------------- |
| 224 | |
| 225 | The core of the bridge program will use three threads. One thread |
| 226 | will be a select/epoll loop to handle io between various inbound and |
| 227 | outbound connections and the connection to the remote side. |
| 228 | |
| 229 | The remote side connection will be implemented as two threads (one for |
| 230 | reading, one for writing) and a datagram socketpair to provide the |
| 231 | channel between the main select/epoll thread and the remote connection |
| 232 | threadpair. The reason for this is that for usb connections, the |
| 233 | kernel interface on linux and osx does not allow you to do meaningful |
| 234 | nonblocking IO. |
| 235 | |
| 236 | The endian swapping for the message headers will happen (as needed) in |
| 237 | the remote connection threadpair and that the rest of the program will |
| 238 | always treat message header values as native-endian. |
| 239 | |
| 240 | The bridge program will be able to have a number of mini-servers |
| 241 | compiled in. They will be published under known names (examples |
| 242 | "shell", "fs-bridge", etc) and upon receiving an OPEN() to such a |
| 243 | service, the bridge program will create a stream socketpair and spawn |
| 244 | a thread or subprocess to handle the io. |
| 245 | |
| 246 | |
| 247 | --- simplified / embedded implementation ------------------------------- |
| 248 | |
| 249 | For limited environments, like the bootloader, it is allowable to |
| 250 | support a smaller, fixed number of channels using pre-assigned channel |
| 251 | ID numbers such that only one stream may be connected to a bootloader |
| 252 | endpoint at any given time. The protocol remains unchanged, but the |
| 253 | "embedded" version of it is less dynamic. |
| 254 | |
| 255 | The bootloader will support two streams. A "bootloader:debug" stream, |
| 256 | which may be opened to get debug messages from the bootloader and a |
| 257 | "bootloader:control", stream which will support the set of basic |
| 258 | bootloader commands. |
| 259 | |
| 260 | Example command stream dialogues: |
| 261 | "flash_kernel,2515049,........\n" "okay\n" |
| 262 | "flash_ramdisk,5038,........\n" "fail,flash write error\n" |
| 263 | "bogus_command......" <CLOSE> |
| 264 | |
| 265 | |
| 266 | --- future expansion --------------------------------------------------- |
| 267 | |
| 268 | I plan on providing either a message or a special control stream so that |
| 269 | the client device could ask the host computer to setup inbound socket |
| 270 | translations on the fly on behalf of the client device. |
| 271 | |
| 272 | |
| 273 | The initial design does handshaking to provide flow control, with a |
| 274 | message flow that looks like: |
| 275 | |
| 276 | >OPEN <READY >WRITE <READY >WRITE <READY >WRITE <CLOSE |
| 277 | |
| 278 | The far side may choose to issue the READY message as soon as it receives |
| 279 | a WRITE or it may defer the READY until the write to the local stream |
| 280 | succeeds. A future version may want to do some level of windowing where |
| 281 | multiple WRITEs may be sent without requiring individual READY acks. |
| 282 | |
| 283 | ------------------------------------------------------------------------ |
| 284 | |
| 285 | --- smartsockets ------------------------------------------------------- |
| 286 | |
| 287 | Port 5037 is used for smart sockets which allow a client on the host |
| 288 | side to request access to a service in the host adb daemon or in the |
| 289 | remote (device) daemon. The service is requested by ascii name, |
| 290 | preceeded by a 4 digit hex length. Upon successful connection an |
| 291 | "OKAY" response is sent, otherwise a "FAIL" message is returned. Once |
| 292 | connected the client is talking to that (remote or local) service. |
| 293 | |
| 294 | client: <hex4> <service-name> |
| 295 | server: "OKAY" |
| 296 | |
| 297 | client: <hex4> <service-name> |
| 298 | server: "FAIL" <hex4> <reason> |
| 299 | |