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