2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ADB_H
|
|
|
|
#define __ADB_H
|
|
|
|
|
|
|
|
#include <limits.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <sys/types.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 02:12:58 +02:00
|
|
|
#include <string>
|
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/macros.h>
|
2015-05-18 22:06:53 +02:00
|
|
|
|
2014-07-29 21:50:02 +02:00
|
|
|
#include "adb_trace.h"
|
2014-11-25 08:34:35 +01:00
|
|
|
#include "fdevent.h"
|
2015-09-16 01:27:09 +02:00
|
|
|
#include "socket.h"
|
2011-03-16 23:57:42 +01:00
|
|
|
|
2015-07-13 20:12:28 +02:00
|
|
|
constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
|
|
|
|
constexpr size_t MAX_PAYLOAD_V2 = 256 * 1024;
|
|
|
|
constexpr size_t MAX_PAYLOAD = MAX_PAYLOAD_V2;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#define A_SYNC 0x434e5953
|
|
|
|
#define A_CNXN 0x4e584e43
|
|
|
|
#define A_OPEN 0x4e45504f
|
|
|
|
#define A_OKAY 0x59414b4f
|
|
|
|
#define A_CLSE 0x45534c43
|
|
|
|
#define A_WRTE 0x45545257
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
#define A_AUTH 0x48545541
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
// ADB protocol version.
|
|
|
|
#define A_VERSION 0x01000000
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
// Used for help/version information.
|
|
|
|
#define ADB_VERSION_MAJOR 1
|
|
|
|
#define ADB_VERSION_MINOR 0
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-08-12 17:32:10 +02:00
|
|
|
std::string adb_version();
|
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
// Increment this when we want to force users to start a new adb server.
|
2016-04-07 20:25:48 +02:00
|
|
|
#define ADB_SERVER_VERSION 37
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-19 01:46:31 +02:00
|
|
|
class atransport;
|
2015-04-17 18:47:42 +02:00
|
|
|
struct usb_handle;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
struct amessage {
|
|
|
|
unsigned command; /* command identifier constant */
|
|
|
|
unsigned arg0; /* first argument */
|
|
|
|
unsigned arg1; /* second argument */
|
|
|
|
unsigned data_length; /* length of payload (0 is allowed) */
|
|
|
|
unsigned data_check; /* checksum of data payload */
|
|
|
|
unsigned magic; /* command ^ 0xffffffff */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct apacket
|
|
|
|
{
|
|
|
|
apacket *next;
|
|
|
|
|
|
|
|
unsigned len;
|
|
|
|
unsigned char *ptr;
|
|
|
|
|
|
|
|
amessage msg;
|
|
|
|
unsigned char data[MAX_PAYLOAD];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* the adisconnect structure is used to record a callback that
|
|
|
|
** will be called whenever a transport is disconnected (e.g. by the user)
|
|
|
|
** this should be used to cleanup objects that depend on the
|
|
|
|
** transport (e.g. remote sockets, listeners, etc...)
|
|
|
|
*/
|
|
|
|
struct adisconnect
|
|
|
|
{
|
|
|
|
void (*func)(void* opaque, atransport* t);
|
|
|
|
void* opaque;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-05-19 01:46:31 +02:00
|
|
|
// A transport object models the connection to a remote device or emulator there
|
|
|
|
// is one transport per connected device/emulator. A "local transport" connects
|
|
|
|
// through TCP (for the emulator), while a "usb transport" through USB (for real
|
|
|
|
// devices).
|
|
|
|
//
|
|
|
|
// Note that kTransportHost doesn't really correspond to a real transport
|
|
|
|
// object, it's a special value used to indicate that a client wants to connect
|
|
|
|
// to a service implemented within the ADB server itself.
|
2015-05-05 22:10:43 +02:00
|
|
|
enum TransportType {
|
2015-05-19 01:46:31 +02:00
|
|
|
kTransportUsb,
|
|
|
|
kTransportLocal,
|
|
|
|
kTransportAny,
|
|
|
|
kTransportHost,
|
2015-04-17 18:47:42 +02:00
|
|
|
};
|
2009-03-04 04:32:55 +01:00
|
|
|
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
#define TOKEN_SIZE 20
|
|
|
|
|
2015-05-19 01:43:57 +02:00
|
|
|
enum ConnectionState {
|
|
|
|
kCsAny = -1,
|
|
|
|
kCsOffline = 0,
|
|
|
|
kCsBootloader,
|
|
|
|
kCsDevice,
|
|
|
|
kCsHost,
|
|
|
|
kCsRecovery,
|
|
|
|
kCsNoPerm, // Insufficient permissions to communicate with the device.
|
|
|
|
kCsSideload,
|
|
|
|
kCsUnauthorized,
|
|
|
|
};
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
void print_packet(const char *label, apacket *p);
|
|
|
|
|
2016-01-16 00:17:37 +01:00
|
|
|
// These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they
|
|
|
|
// shouldn't be tagged with ADB_FORMAT_ARCHETYPE.
|
|
|
|
void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
|
|
|
|
void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
void handle_packet(apacket *p, atransport *t);
|
|
|
|
|
2009-10-21 17:55:00 +02:00
|
|
|
void get_my_path(char *s, size_t maxLen);
|
2010-04-19 13:21:12 +02:00
|
|
|
int launch_server(int server_port);
|
2015-11-19 03:07:48 +01:00
|
|
|
int adb_server_main(int is_daemon, int server_port, int ack_reply_fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
/* initialize a transport object's func pointers and state */
|
2010-04-26 11:17:43 +02:00
|
|
|
#if ADB_HOST
|
|
|
|
int get_available_local_transport_index();
|
|
|
|
#endif
|
2009-08-25 00:58:40 +02:00
|
|
|
int init_socket_transport(atransport *t, int s, int port, int local);
|
2015-05-19 01:43:57 +02:00
|
|
|
void init_usb_transport(atransport *t, usb_handle *usb, ConnectionState state);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-04-26 11:17:43 +02:00
|
|
|
#if ADB_HOST
|
|
|
|
atransport* find_emulator_transport_by_adb_port(int adb_port);
|
|
|
|
#endif
|
2009-10-12 05:04:18 +02:00
|
|
|
|
2015-08-31 19:42:13 +02:00
|
|
|
int service_to_fd(const char* name, const atransport* transport);
|
2009-03-04 04:32:55 +01:00
|
|
|
#if ADB_HOST
|
|
|
|
asocket *host_service_to_socket(const char* name, const char *serial);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !ADB_HOST
|
|
|
|
int init_jdwp(void);
|
|
|
|
asocket* create_jdwp_service_socket();
|
|
|
|
asocket* create_jdwp_tracker_service_socket();
|
|
|
|
int create_jdwp_connection_fd(int jdwp_pid);
|
|
|
|
#endif
|
|
|
|
|
2015-05-08 06:38:41 +02:00
|
|
|
int handle_forward_request(const char* service, TransportType type, const char* serial, int reply_fd);
|
2013-03-21 21:07:42 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#if !ADB_HOST
|
|
|
|
void framebuffer_service(int fd, void *cookie);
|
2014-12-04 00:31:57 +01:00
|
|
|
void set_verity_enabled_state_service(int fd, void* cookie);
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* packet allocator */
|
|
|
|
apacket *get_apacket(void);
|
|
|
|
void put_apacket(apacket *p);
|
|
|
|
|
2014-08-15 18:51:27 +02:00
|
|
|
// Define it if you want to dump packets.
|
|
|
|
#define DEBUG_PACKETS 0
|
|
|
|
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
#if !DEBUG_PACKETS
|
2009-03-04 04:32:55 +01:00
|
|
|
#define print_packet(tag,p) do {} while (0)
|
|
|
|
#endif
|
|
|
|
|
2010-09-24 00:08:34 +02:00
|
|
|
#if ADB_HOST_ON_TARGET
|
|
|
|
/* adb and adbd are coexisting on the target, so use 5038 for adb
|
|
|
|
* to avoid conflicting with adbd's usage of 5037
|
|
|
|
*/
|
|
|
|
# define DEFAULT_ADB_PORT 5038
|
|
|
|
#else
|
|
|
|
# define DEFAULT_ADB_PORT 5037
|
|
|
|
#endif
|
|
|
|
|
2010-04-19 13:21:12 +02:00
|
|
|
#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2009-05-22 02:47:43 +02:00
|
|
|
#define ADB_CLASS 0xff
|
|
|
|
#define ADB_SUBCLASS 0x42
|
|
|
|
#define ADB_PROTOCOL 0x1
|
2009-05-09 03:25:58 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2009-08-26 21:50:22 +02:00
|
|
|
void local_init(int port);
|
2015-07-24 02:12:58 +02:00
|
|
|
void local_connect(int port);
|
|
|
|
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-12-03 00:14:31 +01:00
|
|
|
// USB host/client interface.
|
2009-03-04 04:32:55 +01:00
|
|
|
void usb_init();
|
|
|
|
int usb_write(usb_handle *h, const void *data, int len);
|
|
|
|
int usb_read(usb_handle *h, void *data, int len);
|
|
|
|
int usb_close(usb_handle *h);
|
|
|
|
void usb_kick(usb_handle *h);
|
|
|
|
|
2015-12-03 00:14:31 +01:00
|
|
|
// USB device detection.
|
2009-05-21 02:33:53 +02:00
|
|
|
#if ADB_HOST
|
2009-03-04 04:32:55 +01:00
|
|
|
int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
|
2009-05-21 02:33:53 +02:00
|
|
|
#endif
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
int adb_commandline(int argc, const char **argv);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-19 01:43:57 +02:00
|
|
|
ConnectionState connection_state(atransport *t);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-18 22:06:53 +02:00
|
|
|
extern const char* adb_device_banner;
|
|
|
|
|
2015-07-17 21:30:59 +02:00
|
|
|
#if !ADB_HOST
|
2011-03-16 23:57:42 +01:00
|
|
|
extern int SHELL_EXIT_NOTIFY_FD;
|
2015-07-17 21:30:59 +02:00
|
|
|
#endif // !ADB_HOST
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#define CHUNK_SIZE (64*1024)
|
|
|
|
|
2012-01-13 15:13:46 +01:00
|
|
|
#if !ADB_HOST
|
|
|
|
#define USB_ADB_PATH "/dev/android_adb"
|
|
|
|
|
|
|
|
#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
|
|
|
|
#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
|
|
|
|
|
|
|
|
#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
|
|
|
|
#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
|
|
|
|
#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
|
|
|
|
#endif
|
|
|
|
|
2015-05-08 06:38:41 +02:00
|
|
|
int handle_host_request(const char* service, TransportType type, const char* serial, int reply_fd, asocket *s);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-02-19 02:47:33 +01:00
|
|
|
void handle_online(atransport *t);
|
|
|
|
void handle_offline(atransport *t);
|
|
|
|
|
|
|
|
void send_connect(atransport *t);
|
|
|
|
|
2015-05-18 22:06:53 +02:00
|
|
|
void parse_banner(const std::string&, atransport* t);
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|