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>
|
2016-10-06 22:31:44 +02:00
|
|
|
#include <stdint.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"
|
2018-03-08 01:52:28 +01:00
|
|
|
#include "types.h"
|
2016-09-28 21:32:45 +02:00
|
|
|
#include "usb.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;
|
2017-07-18 23:07:57 +02:00
|
|
|
constexpr size_t MAX_PAYLOAD = 1024 * 1024;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-11 02:45:27 +01:00
|
|
|
constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304;
|
|
|
|
|
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.
|
2017-12-07 20:40:00 +01:00
|
|
|
// Version revision:
|
|
|
|
// 0x01000000: original
|
|
|
|
// 0x01000001: skip checksum (Dec 2017)
|
|
|
|
#define A_VERSION_MIN 0x01000000
|
|
|
|
#define A_VERSION_SKIP_CHECKSUM 0x01000001
|
|
|
|
#define A_VERSION 0x01000001
|
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.
|
2019-02-23 00:26:15 +01:00
|
|
|
#define ADB_SERVER_VERSION 41
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-08-17 01:57:01 +02:00
|
|
|
using TransportId = uint64_t;
|
2015-05-19 01:46:31 +02:00
|
|
|
class atransport;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-10-06 22:31:44 +02:00
|
|
|
uint32_t calculate_apacket_checksum(const apacket* packet);
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* 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...)
|
|
|
|
*/
|
2017-10-05 00:05:40 +02:00
|
|
|
struct adisconnect {
|
|
|
|
void (*func)(void* opaque, atransport* t);
|
|
|
|
void* opaque;
|
2009-03-04 04:32:55 +01:00
|
|
|
};
|
|
|
|
|
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,
|
2018-05-05 01:04:49 +02:00
|
|
|
|
|
|
|
kCsConnecting = 0, // Haven't received a response from the device yet.
|
|
|
|
kCsAuthorizing, // Authorizing with keys from ADB_VENDOR_KEYS.
|
|
|
|
kCsUnauthorized, // ADB_VENDOR_KEYS exhausted, fell back to user prompt.
|
|
|
|
kCsNoPerm, // Insufficient permissions to communicate with the device.
|
|
|
|
kCsOffline,
|
|
|
|
|
2015-05-19 01:43:57 +02:00
|
|
|
kCsBootloader,
|
|
|
|
kCsDevice,
|
|
|
|
kCsHost,
|
|
|
|
kCsRecovery,
|
|
|
|
kCsSideload,
|
|
|
|
};
|
|
|
|
|
2018-05-05 01:04:49 +02:00
|
|
|
inline bool ConnectionStateIsOnline(ConnectionState state) {
|
|
|
|
switch (state) {
|
|
|
|
case kCsBootloader:
|
|
|
|
case kCsDevice:
|
|
|
|
case kCsHost:
|
|
|
|
case kCsRecovery:
|
|
|
|
case kCsSideload:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 00:05:40 +02:00
|
|
|
void print_packet(const char* label, apacket* p);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-10-05 00:05:40 +02:00
|
|
|
void handle_packet(apacket* p, atransport* t);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-08-26 01:00:22 +02:00
|
|
|
int launch_server(const std::string& socket_spec);
|
|
|
|
int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
/* initialize a transport object's func pointers and state */
|
2018-07-26 02:21:49 +02:00
|
|
|
int init_socket_transport(atransport* t, unique_fd s, int port, int local);
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
void init_usb_transport(atransport* t, usb_handle* usb);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-10-06 21:22:55 +02:00
|
|
|
std::string getEmulatorSerialString(int console_port);
|
2010-04-26 11:17:43 +02:00
|
|
|
#if ADB_HOST
|
|
|
|
atransport* find_emulator_transport_by_adb_port(int adb_port);
|
2016-10-06 21:22:55 +02:00
|
|
|
atransport* find_emulator_transport_by_console_port(int console_port);
|
2010-04-26 11:17:43 +02:00
|
|
|
#endif
|
2009-10-12 05:04:18 +02:00
|
|
|
|
2019-01-24 00:36:42 +01:00
|
|
|
unique_fd service_to_fd(std::string_view name, atransport* transport);
|
2018-07-26 03:15:52 +02:00
|
|
|
#if !ADB_HOST
|
2018-12-13 22:06:03 +01:00
|
|
|
unique_fd daemon_service_to_fd(std::string_view name, atransport* transport);
|
2018-07-26 03:15:52 +02:00
|
|
|
#endif
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#if ADB_HOST
|
2019-02-22 22:41:55 +01:00
|
|
|
asocket* host_service_to_socket(std::string_view name, std::string_view serial,
|
|
|
|
TransportId transport_id);
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|
|
|
|
|
2018-11-17 00:40:16 +01:00
|
|
|
#if !ADB_HOST
|
|
|
|
asocket* daemon_service_to_socket(std::string_view name);
|
|
|
|
#endif
|
|
|
|
|
2018-12-12 19:48:50 +01:00
|
|
|
#if !ADB_HOST
|
2019-02-21 23:22:51 +01:00
|
|
|
unique_fd execute_abb_command(std::string_view command);
|
2018-12-12 19:48:50 +01:00
|
|
|
#endif
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#if !ADB_HOST
|
2017-10-05 00:05:40 +02:00
|
|
|
int init_jdwp(void);
|
|
|
|
asocket* create_jdwp_service_socket();
|
|
|
|
asocket* create_jdwp_tracker_service_socket();
|
2018-07-26 03:15:52 +02:00
|
|
|
unique_fd create_jdwp_connection_fd(int jdwp_pid);
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|
|
|
|
|
2018-07-31 03:49:03 +02:00
|
|
|
bool handle_forward_request(const char* service, atransport* transport, int reply_fd);
|
|
|
|
bool handle_forward_request(const char* service,
|
|
|
|
std::function<atransport*(std::string* error)> transport_acquirer,
|
|
|
|
int reply_fd);
|
2013-03-21 21:07:42 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* packet allocator */
|
2017-10-05 00:05:40 +02:00
|
|
|
apacket* get_apacket(void);
|
|
|
|
void put_apacket(apacket* p);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
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
|
2017-10-05 00:05:40 +02:00
|
|
|
#define print_packet(tag, p) \
|
|
|
|
do { \
|
|
|
|
} while (0)
|
2009-03-04 04:32:55 +01:00
|
|
|
#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
|
|
|
|
*/
|
2017-10-05 00:05:40 +02:00
|
|
|
#define DEFAULT_ADB_PORT 5038
|
2010-09-24 00:08:34 +02:00
|
|
|
#else
|
2017-10-05 00:05:40 +02:00
|
|
|
#define DEFAULT_ADB_PORT 5037
|
2010-09-24 00:08:34 +02:00
|
|
|
#endif
|
|
|
|
|
2010-04-19 13:21:12 +02:00
|
|
|
#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-10-05 00:05:40 +02:00
|
|
|
#define ADB_CLASS 0xff
|
|
|
|
#define ADB_SUBCLASS 0x42
|
|
|
|
#define ADB_PROTOCOL 0x1
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2009-08-26 21:50:22 +02:00
|
|
|
void local_init(int port);
|
2016-04-30 01:53:52 +02:00
|
|
|
bool local_connect(int port);
|
2017-10-05 00:05:40 +02:00
|
|
|
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-10-05 00:05:40 +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;
|
|
|
|
|
2017-10-05 00:05:40 +02:00
|
|
|
#define CHUNK_SIZE (64 * 1024)
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-12-12 19:48:50 +01:00
|
|
|
// Argument delimeter for adb abb command.
|
|
|
|
#define ABB_ARG_DELIMETER ('\0')
|
|
|
|
|
2012-01-13 15:13:46 +01:00
|
|
|
#if !ADB_HOST
|
2017-10-05 00:05:40 +02:00
|
|
|
#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
|
|
|
|
#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH #x
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2017-10-05 00:05:40 +02:00
|
|
|
#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)
|
2012-01-13 15:13:46 +01:00
|
|
|
#endif
|
|
|
|
|
2019-02-21 05:37:26 +01:00
|
|
|
enum class HostRequestResult {
|
|
|
|
Handled,
|
|
|
|
SwitchedTransport,
|
|
|
|
Unhandled,
|
|
|
|
};
|
|
|
|
|
|
|
|
HostRequestResult handle_host_request(std::string_view service, TransportType type,
|
|
|
|
const char* serial, TransportId transport_id, int reply_fd,
|
|
|
|
asocket* s);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-10-05 00:05:40 +02:00
|
|
|
void handle_online(atransport* t);
|
|
|
|
void handle_offline(atransport* t);
|
2015-02-19 02:47:33 +01:00
|
|
|
|
2017-10-05 00:05:40 +02:00
|
|
|
void send_connect(atransport* t);
|
2015-02-19 02:47:33 +01:00
|
|
|
|
2015-05-18 22:06:53 +02:00
|
|
|
void parse_banner(const std::string&, atransport* t);
|
|
|
|
|
2017-05-04 07:37:10 +02:00
|
|
|
// On startup, the adb server needs to wait until all of the connected devices are ready.
|
|
|
|
// To do this, we need to know when the scan has identified all of the potential new transports, and
|
|
|
|
// when each transport becomes ready.
|
|
|
|
// TODO: Do this for mDNS as well, instead of just USB?
|
|
|
|
|
|
|
|
// We've found all of the transports we potentially care about.
|
|
|
|
void adb_notify_device_scan_complete();
|
|
|
|
|
|
|
|
// One or more transports have changed status, check to see if we're ready.
|
|
|
|
void update_transport_status();
|
|
|
|
|
|
|
|
// Wait until device scan has completed and every transport is ready, or a timeout elapses.
|
|
|
|
void adb_wait_for_device_initialization();
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|