diff --git a/base/cvd/adb/BUILD.bazel b/base/cvd/adb/BUILD.bazel index 1b2a2fa0b6f..8627983eb97 100644 --- a/base/cvd/adb/BUILD.bazel +++ b/base/cvd/adb/BUILD.bazel @@ -42,7 +42,6 @@ cc_binary( "adb_unique_fd.h", "adb_utils.cpp", "adb_utils.h", - "adb_wifi.h", "apacket_reader.cpp", "apacket_reader.h", "client/adb_client.cpp", @@ -50,6 +49,7 @@ cc_binary( "client/adb_install.cpp", "client/adb_install.h", "client/adb_wifi.cpp", + "client/adb_wifi.h", "client/auth.cpp", "client/bugreport.cpp", "client/bugreport.h", @@ -76,17 +76,12 @@ cc_binary( "client/mdns_tracker.h", "client/mdns_utils.cpp", "client/mdns_utils.h", - "client/openscreen/mdns_service_info.cpp", - "client/openscreen/mdns_service_info.h", - "client/openscreen/platform/logging.cpp", - "client/openscreen/platform/task_runner.cpp", - "client/openscreen/platform/task_runner.h", - "client/openscreen/platform/udp_socket.cpp", "client/pairing/pairing_client.cpp", "client/pairing/pairing_client.h", "client/transport_client.h", "client/transport_emulator.cpp", "client/transport_mdns.cpp", + "client/transport_mdns.h", "client/transport_usb.cpp", "client/usb.h", "client/usb_libusb.cpp", @@ -98,6 +93,8 @@ cc_binary( "client/usb_libusb_inhouse_hotplug.cpp", "client/usb_libusb_inhouse_hotplug.h", "client/usb_linux.cpp", + "client/usb_linux_netlink.cpp", + "client/usb_linux_netlink.h", "compression_utils.h", "crypto/include/adb/crypto/key.h", "crypto/include/adb/crypto/rsa_2048_key.h", @@ -112,7 +109,6 @@ cc_binary( "fdevent/fdevent_epoll.cpp", "fdevent/fdevent_epoll.h", "file_sync_protocol.h", - "incfs/util/map_ptr.h", "libcrypto_utils/android_pubkey.cpp", "libcrypto_utils/include/crypto_utils/android_pubkey.h", "pairing_auth/aes_128_gcm.cpp", @@ -183,13 +179,13 @@ cc_binary( "@android_system_libbase//:no_destructor", "@android_system_libbase//:parsebool", "@android_system_libbase//:parsenetaddress", + "@android_system_libbase//:properties", "@android_system_libbase//:scopeguard", "@android_system_libbase//:thread_annotations", "@boringssl//:crypto", "@boringssl//:ssl", "@brotli//:brotlidec", "@brotli//:brotlienc", - "@libopenscreen", "@libusb", "@libzip", "@lz4//:lz4_frame", diff --git a/base/cvd/adb/adb.cpp b/base/cvd/adb/adb.cpp index 3f7d7ae33d8..a4794e95b3d 100644 --- a/base/cvd/adb/adb.cpp +++ b/base/cvd/adb/adb.cpp @@ -579,7 +579,7 @@ void handle_packet(apacket* p, atransport* t) { local_socket_ack(s, acked_bytes); } else { D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s", p->msg.arg0, - p->msg.arg1, s->peer->id, p->msg.arg1, t->serial.c_str()); + p->msg.arg1, s->peer->id, p->msg.arg1, t->name.c_str()); } } else { // When receiving A_OKAY from device for A_OPEN request, the host server may @@ -606,7 +606,7 @@ void handle_packet(apacket* p, atransport* t) { */ if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) { D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s", p->msg.arg1, - t->serial.c_str(), s->peer->transport->serial.c_str()); + t->name.c_str(), s->peer->transport->name.c_str()); } else { s->close(s); } @@ -1252,6 +1252,10 @@ void adb_set_reject_kill_server(bool value) { g_reject_kill_server = value; } +bool is_usb_enabled() { + return !getenv("ADB_USB") || strcmp(getenv("ADB_USB"), "0") != 0; +} + static bool handle_mdns_request(std::string_view service, int reply_fd) { if (!android::base::ConsumePrefix(&service, "mdns:")) { return false; @@ -1352,15 +1356,28 @@ HostRequestResult handle_host_request(std::string_view service, TransportType ty if (service == "server-status") { adb::proto::AdbServerStatus status; - if (is_libusb_enabled()) { - status.set_usb_backend(adb::proto::AdbServerStatus::LIBUSB); + + if (is_usb_enabled()) { + if (is_libusb_enabled()) { + status.set_usb_backend(adb::proto::AdbServerStatus::LIBUSB); + } else { + status.set_usb_backend(adb::proto::AdbServerStatus::NATIVE); + } } else { - status.set_usb_backend(adb::proto::AdbServerStatus::NATIVE); + status.set_usb_backend(adb::proto::AdbServerStatus::USB_DISABLED); } status.set_usb_backend_forced(getenv("ADB_LIBUSB") != nullptr); - status.set_mdns_backend(adb::proto::AdbServerStatus::OPENSCREEN); - status.set_mdns_backend_forced(false); + if (mdns::is_enabled()) { + if (mdns::should_use_openscreen()) { + status.set_mdns_backend(adb::proto::AdbServerStatus::OPENSCREEN); + } else { + status.set_mdns_backend(adb::proto::AdbServerStatus::LIBADBMDNS); + } + } else { + status.set_mdns_backend(adb::proto::AdbServerStatus::MDNS_DISABLED); + } + status.set_mdns_backend_forced(getenv("ADB_MDNS_OPENSCREEN") != nullptr); status.set_version(std::string(PLATFORM_TOOLS_VERSION)); status.set_build(android::build::GetBuildNumber()); @@ -1371,7 +1388,7 @@ HostRequestResult handle_host_request(std::string_view service, TransportType ty status.set_trace_level(get_trace_setting()); status.set_mdns_enabled(mdns::is_enabled()); status.set_keystore_path(adb_auth_get_userkey_path()); - status.set_known_hosts_path(get_user_known_hosts_path()); + status.set_known_hosts_path(known_wifi_hosts_file.KeyStorePath()); std::string server_status_string; status.SerializeToString(&server_status_string); @@ -1485,7 +1502,7 @@ HostRequestResult handle_host_request(std::string_view service, TransportType ty s->transport ? s->transport : acquire_one_transport(type, serial, transport_id, nullptr, &error); if (t) { - SendOkay(reply_fd, !t->serial.empty() ? t->serial : "unknown"); + SendOkay(reply_fd, !t->name.empty() ? t->name : "unknown"); } else { SendFail(reply_fd, error); } @@ -1522,7 +1539,10 @@ HostRequestResult handle_host_request(std::string_view service, TransportType ty if (!ParseUint(&port, service)) { LOG(ERROR) << "received invalid port for emulator: " << service; } else { - connect_emulator(port); + // We are running on the fdevent thread. Connect is a long operation which + // can potentially block 10s until connection is established. We must run + // it on another thread. + std::thread([=] { connect_emulator(port); }).detach(); } /* we don't even need to send a reply */ diff --git a/base/cvd/adb/adb.h b/base/cvd/adb/adb.h index 4516d3cbfb9..5c24ff83e1b 100644 --- a/base/cvd/adb/adb.h +++ b/base/cvd/adb/adb.h @@ -198,15 +198,11 @@ void put_apacket(apacket* p); #define ADB_DBC_CLASS 0xDC #define ADB_DBC_SUBCLASS 0x2 -bool connect_emulator(int port); -int connect_emulator_arbitrary_ports(int console_port, int adb_port, std::string* error); - extern const char* adb_device_banner; #define CHUNK_SIZE (64 * 1024) -// Argument delimeter for adb abb command. -#define ABB_ARG_DELIMETER ('\0') +#define ABB_ARG_DELIMITER ('\0') #if !ADB_HOST #define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/" @@ -254,6 +250,8 @@ void adb_wait_for_device_initialization(); // When ssh-forwarding to a remote adb server, kill-server is almost never what you actually want, // and unfortunately, many other tools issue it. This adds a knob to reject kill-servers. void adb_set_reject_kill_server(bool reject); + +bool is_usb_enabled(); #endif void usb_init(); diff --git a/base/cvd/adb/adb_auth.h b/base/cvd/adb/adb_auth.h index 46707b40be6..a6222d5fe2c 100644 --- a/base/cvd/adb/adb_auth.h +++ b/base/cvd/adb/adb_auth.h @@ -23,6 +23,7 @@ #include #include +#include /* AUTH packets first argument */ /* Request */ diff --git a/base/cvd/adb/adb_listeners.cpp b/base/cvd/adb/adb_listeners.cpp index c24016fb4db..32aa5352442 100644 --- a/base/cvd/adb/adb_listeners.cpp +++ b/base/cvd/adb/adb_listeners.cpp @@ -137,7 +137,7 @@ std::string format_listeners() EXCLUDES(listener_list_mutex) { // Entries from "adb reverse" have no serial. android::base::StringAppendF( &result, "%s %s %s\n", - !l->transport->serial.empty() ? l->transport->serial.c_str() : "(reverse)", + !l->transport->name.empty() ? l->transport->name.c_str() : "(reverse)", l->local_name.c_str(), l->connect_to.c_str()); } return result; diff --git a/base/cvd/adb/adb_mdns.cpp b/base/cvd/adb/adb_mdns.cpp index c5228036e64..dde08d7a069 100644 --- a/base/cvd/adb/adb_mdns.cpp +++ b/base/cvd/adb/adb_mdns.cpp @@ -89,35 +89,36 @@ void config_auto_connect_services() { } // namespace -std::optional adb_DNSServiceIndexByName(std::string_view reg_type) { - for (int i = 0; i < kNumADBDNSServices; ++i) { - if (!strncmp(reg_type.data(), kADBDNSServices[i], strlen(kADBDNSServices[i]))) { - return i; - } - } - return std::nullopt; -} - bool adb_DNSServiceShouldAutoConnect(std::string_view reg_type, std::string_view service_name) { config_auto_connect_services(); // Try to auto-connect to any "_adb" or "_adb-tls-connect" services excluding emulator services. std::optional index = adb_DNSServiceIndexByName(reg_type); - if (!index || - (index != kADBTransportServiceRefIndex && index != kADBSecureConnectServiceRefIndex)) { + if (!index) { + VLOG(MDNS) << "Unknown index for '" << reg_type << "'"; return false; } + if (!g_autoconn_allowedlist.contains(*index)) { - D("Auto-connect for reg_type '%s' disabled", reg_type.data()); + VLOG(MDNS) << "Auto-connect for reg_type '" << reg_type << "' disabled"; return false; } // Ignore adb-EMULATOR* service names, as it interferes with the // emulator ports that are already connected. if (android::base::StartsWith(service_name, "adb-EMULATOR")) { - LOG(INFO) << "Ignoring emulator transport service [" << service_name << "]"; + VLOG(MDNS) << "Ignoring emulator transport service [" << service_name << "]"; return false; } return true; } #endif // ADB_HOST + +std::optional adb_DNSServiceIndexByName(std::string_view reg_type) { + for (int i = 0; i < kNumADBDNSServices; ++i) { + if (!strncmp(reg_type.data(), kADBDNSServices[i], strlen(kADBDNSServices[i]))) { + return i; + } + } + return std::nullopt; +} diff --git a/base/cvd/adb/adb_mdns.h b/base/cvd/adb/adb_mdns.h index 48213c95211..ff9ccc4a779 100644 --- a/base/cvd/adb/adb_mdns.h +++ b/base/cvd/adb/adb_mdns.h @@ -29,7 +29,8 @@ #define ADB_MDNS_SERVICE_TYPE "adb" #define ADB_MDNS_TLS_PAIRING_TYPE "adb-tls-pairing" #define ADB_MDNS_TLS_CONNECT_TYPE "adb-tls-connect" -#define ADB_FULL_MDNS_SERVICE_TYPE(atype) ("_" atype "._tcp") +#define MDNS_SERVICE_PROTOCOL "_tcp" +#define ADB_FULL_MDNS_SERVICE_TYPE(atype) ("_" atype "." MDNS_SERVICE_PROTOCOL) #define ADB_SERVICE_TCP ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_SERVICE_TYPE) #define ADB_SERVICE_TLS ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_CONNECT_TYPE) @@ -47,21 +48,17 @@ constexpr int kADBSecureConnectServiceRefIndex = 2; constexpr int kNumADBDNSServices = 3; extern const char* _Nonnull kADBDNSServices[kNumADBDNSServices]; -extern const char* _Nonnull kADBDNSServiceTxtRecords[kNumADBDNSServices]; #if ADB_HOST -#include "client/openscreen/mdns_service_info.h" +#include "client/discovered_services.h" // ADB Secure DNS service interface. Used to query what ADB Secure DNS services have been // resolved, and to run some kind of callback for each one. -using adb_secure_foreach_service_callback = std::function; +using adb_secure_foreach_service_callback = std::function; // Tries to connect to a |service_name| if found. Returns true if found and // connected, false otherwise. bool adb_secure_connect_by_service_name(const std::string& instance_name); -// Returns the index in kADBDNSServices array if |reg_type| matches a service name, otherwise -// std::nullopt. -std::optional adb_DNSServiceIndexByName(std::string_view reg_type); // Returns true if auto-connect is allowed for |service_name| and |instance_name|. // See ADB_MDNS_AUTO_CONNECT environment variable for more info. bool adb_DNSServiceShouldAutoConnect(std::string_view service_name, std::string_view instance_name); @@ -69,9 +66,10 @@ bool adb_DNSServiceShouldAutoConnect(std::string_view service_name, std::string_ std::string mdns_check(); std::string mdns_list_discovered_services(); -std::optional mdns_get_connect_service_info(const std::string& name); -std::optional mdns_get_pairing_service_info(const std::string& name); - // Return the location where adb host stores paired devices std::string get_user_known_hosts_path(); #endif // ADB_HOST + +// Returns the index in kADBDNSServices array if |reg_type| matches a service name, otherwise +// std::nullopt. +std::optional adb_DNSServiceIndexByName(std::string_view reg_type); diff --git a/base/cvd/adb/adb_trace.cpp b/base/cvd/adb/adb_trace.cpp index 79829d7f7b5..0220d7d7b31 100644 --- a/base/cvd/adb/adb_trace.cpp +++ b/base/cvd/adb/adb_trace.cpp @@ -113,6 +113,7 @@ static void setup_trace_mask() { return; } + // LINT.IfChange std::unordered_map trace_flags = { {"1", -1}, {"all", -1}, @@ -133,6 +134,7 @@ static void setup_trace_mask() { {"mdns", MDNS}, {"mdns-stack", MDNS_STACK}, }; + // LINT.ThenChange(packages/modules/adb/docs/user/adb.1.md) // Make sure we check for ALL enum in AdbTrace. size_t num_flags = trace_flags.size() - 2; diff --git a/base/cvd/adb/adb_utils.cpp b/base/cvd/adb/adb_utils.cpp index 7f65ed88799..f3b641bf52a 100644 --- a/base/cvd/adb/adb_utils.cpp +++ b/base/cvd/adb/adb_utils.cpp @@ -312,7 +312,7 @@ std::string adb_get_android_dir_path() { std::string android_dir = user_dir + OS_PATH_SEPARATOR + ".android"; struct stat buf; if (stat(android_dir.c_str(), &buf) == -1) { - if (adb_mkdir(android_dir, 0750) == -1) { + if (adb_mkdir(android_dir, 0750) == -1 && errno != EEXIST) { PLOG(FATAL) << "Cannot mkdir '" << android_dir << "'"; } } diff --git a/base/cvd/adb/client/adb_install.cpp b/base/cvd/adb/client/adb_install.cpp index 7643c197553..f6a848cdb13 100644 --- a/base/cvd/adb/client/adb_install.cpp +++ b/base/cvd/adb/client/adb_install.cpp @@ -297,9 +297,9 @@ static int install_app_incremental(int argc, const char** argv, bool wait, bool auto files = incremental::Files{argv + first_apk, argv + last_apk + 1}; if (silent) { - // For a silent installation we want to do the lightweight check first and bail early and - // quietly if it fails. - if (!incremental::can_install(files)) { + // For a silent installation we want to do the lightweight check first and return early if + // we shouldn't use incremental install for the given files. + if (!incremental::should_use_incremental_by_default(files)) { return -1; } } diff --git a/base/cvd/adb/client/adb_wifi.cpp b/base/cvd/adb/client/adb_wifi.cpp index 7168c0eb58a..0f4231f5abc 100644 --- a/base/cvd/adb/client/adb_wifi.cpp +++ b/base/cvd/adb/client/adb_wifi.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "adb_wifi.h" +#include "client/adb_wifi.h" #include #include @@ -26,6 +26,7 @@ #include #include #include "client/pairing/pairing_client.h" +#include "client/transport_mdns.h" #include "adb_auth.h" #include "adb_known_hosts.pb.h" @@ -37,6 +38,8 @@ using adbwifi::pairing::PairingClient; using namespace adb::crypto; +KnownWifiHostsFile known_wifi_hosts_file [[clang::no_destroy]]; + struct PairingResultWaiter { std::mutex mutex_; std::condition_variable cv_; @@ -94,44 +97,39 @@ bool SafeReplaceFile(std::string_view old_file, std::string_view new_file) { return true; } -std::string get_user_known_hosts_path() { - return adb_get_android_dir_path() + OS_PATH_SEPARATOR + "adb_known_hosts.pb"; -} +adb::proto::AdbKnownHosts KnownWifiHostsFile::Load() { + adb::proto::AdbKnownHosts known_hosts; -bool load_known_hosts_from_file(const std::string& path, adb::proto::AdbKnownHosts& known_hosts) { // Check for file existence. struct stat buf; - if (stat(path.c_str(), &buf) == -1) { - LOG(INFO) << "Known hosts file [" << path << "] does not exist..."; - return false; + if (stat(keystore_path_.c_str(), &buf) == -1) { + return known_hosts; } - std::ifstream file(path, std::ios::binary); + std::ifstream file(keystore_path_, std::ios::binary); if (!file) { - PLOG(ERROR) << "Unable to open [" << path << "]."; - return false; + PLOG(ERROR) << "Unable to open [" << keystore_path_ << "]."; + return known_hosts; } if (!known_hosts.ParseFromIstream(&file)) { - PLOG(ERROR) << "Failed to parse [" << path << "]. Deleting it as it may be corrupted."; - adb_unlink(path.c_str()); - return false; + PLOG(ERROR) << "Failed to parse [" << keystore_path_ + << "]. Deleting it as it may be corrupted."; + adb_unlink(keystore_path_.c_str()); + return known_hosts; } - return true; + return known_hosts; } -static bool write_known_host_to_file(std::string& known_host) { - std::string path = get_user_known_hosts_path(); - if (path.empty()) { - PLOG(ERROR) << "Error getting user known hosts filename"; - return false; - } +void KnownWifiHostsFile::Clear() { + adb_unlink(keystore_path_.c_str()); +} - adb::proto::AdbKnownHosts known_hosts; - load_known_hosts_from_file(path, known_hosts); +bool KnownWifiHostsFile::AddKnownHost(const std::string& host) { + auto known_hosts = Load(); auto* host_info = known_hosts.add_host_infos(); - host_info->set_guid(known_host); + host_info->set_guid(host); std::unique_ptr temp_file(new TemporaryFile(adb_get_android_dir_path())); if (temp_file->fd == -1) { @@ -148,30 +146,29 @@ static bool write_known_host_to_file(std::string& known_host) { temp_file.reset(); // Replace the existing adb_known_hosts with the new one - if (!SafeReplaceFile(path, temp_file_name.c_str())) { + if (!SafeReplaceFile(keystore_path_, temp_file_name.c_str())) { LOG(ERROR) << "Failed to replace old adb_known_hosts"; adb_unlink(temp_file_name.c_str()); return false; } - chmod(path.c_str(), S_IRUSR | S_IWUSR | S_IRGRP); + chmod(keystore_path_.c_str(), S_IRUSR | S_IWUSR | S_IRGRP); return true; } -bool adb_wifi_is_known_host(const std::string& host) { - std::string path = get_user_known_hosts_path(); - if (path.empty()) { - PLOG(ERROR) << "Error getting user known hosts filename"; - return false; - } - - adb::proto::AdbKnownHosts known_hosts; - if (!load_known_hosts_from_file(path, known_hosts)) { - return false; - } +KnownWifiHostsFile::KnownWifiHostsFile() + : keystore_path_(adb_get_android_dir_path() + OS_PATH_SEPARATOR + "adb_known_hosts.pb") {} +bool KnownWifiHostsFile::IsKnownHost(const std::string& host) { + auto known_hosts = Load(); for (const auto& host_info : known_hosts.host_infos()) { - if (host == host_info.guid()) { + // Android devices have a bug where they can register a service instance before + // unregistering the previous one. This leads to mdns duplicates. We can mitigate this issue + // by detecting them (a duplicate is automatically created by the mDNS stack with a suffix + // " (X)" where X is an integer to dedup). Testing for start_with instead of == does the + // trick. This workaround can be deleted once b/432299214 and b/421363980 fixes have been + // out for a few years. + if (host.starts_with(host_info.guid())) { return true; } } @@ -200,7 +197,7 @@ void adb_wifi_pair_device(const std::string& host, const std::string& password, auto priv_key = adb_auth_get_user_privkey(); auto x509_cert = GenerateX509Certificate(priv_key.get()); if (!x509_cert) { - LOG(ERROR) << "Unable to create X509 certificate for pairing"; + response = "Unable to create X509 certificate for pairing"; return; } auto cert_str = X509ToPEMString(x509_cert.get()); @@ -249,7 +246,8 @@ void adb_wifi_pair_device(const std::string& host, const std::string& password, response = "Successfully paired to " + host + " [guid=" + device_guid + "]"; // Write to adb_known_hosts - write_known_host_to_file(device_guid); + known_wifi_hosts_file.AddKnownHost(device_guid); + // Try to auto-connect. adb_secure_connect_by_service_name(device_guid); } diff --git a/base/cvd/adb/client/adb_wifi.h b/base/cvd/adb/client/adb_wifi.h new file mode 100644 index 00000000000..b4d3e82361a --- /dev/null +++ b/base/cvd/adb/client/adb_wifi.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2019 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. + */ + +#pragma once + +#include + +#include "adb.h" +#include "adb_known_hosts.pb.h" + +void adb_wifi_pair_device(const std::string& host, const std::string& password, + std::string& response); + +// An accessor to the list of known hosts (known_host). Nothing is cached, every operation +// hits the disk. +// TODO: Convert this to a write-through cache. +class KnownWifiHostsFile { + public: + KnownWifiHostsFile(); + + // For testing, we allow a custom known_host location + explicit KnownWifiHostsFile(const std::string& keystore_path) : keystore_path_(keystore_path) {} + + // Location of the known_host file + std::string KeyStorePath() const { return keystore_path_; } + + // Add host to the known_host file + bool AddKnownHost(const std::string& host); + + // Load known_host file and return true if that host is considered paired. + bool IsKnownHost(const std::string& host); + + // Delete known_host file + void Clear(); + + private: + std::string keystore_path_; + adb::proto::AdbKnownHosts Load(); +}; + +extern KnownWifiHostsFile known_wifi_hosts_file; diff --git a/base/cvd/adb/client/commandline.cpp b/base/cvd/adb/client/commandline.cpp index 1f033b10bff..fa6d73629a9 100644 --- a/base/cvd/adb/client/commandline.cpp +++ b/base/cvd/adb/client/commandline.cpp @@ -60,6 +60,7 @@ using namespace std::string_literals; #include "adb_host.pb.h" #include "adb_install.h" #include "adb_io.h" +#include "adb_known_hosts.pb.h" #include "adb_unique_fd.h" #include "adb_utils.h" #include "app_processes.pb.h" @@ -807,7 +808,7 @@ static int adb_abb(int argc, const char** argv) { constexpr bool empty_command = false; std::vector args(argv + optind, argv + argc); - std::string service_string = "abb:" + android::base::Join(args, ABB_ARG_DELIMETER); + std::string service_string = "abb:" + android::base::Join(args, ABB_ARG_DELIMITER); D("abb -e 0x%x [%*.s]\n", escape_char, static_cast(service_string.size()), service_string.data()); @@ -1053,6 +1054,15 @@ static bool wait_for_device(const char* service, static bool adb_root(const char* command) { std::string error; + std::string state; + + // Query current state (e.g. device, recovery) for subsequent "wait-for" + // reference. + if (!adb_query(format_host_command("get-state"), &state, &error)) { + fprintf(stderr, "get-state error: %s\n", error.c_str()); + return false; + } + state = android::base::Trim(state); TransportId transport_id; unique_fd fd(adb_connect(&transport_id, android::base::StringPrintf("%s:", command), &error)); @@ -1100,8 +1110,10 @@ static bool adb_root(const char* command) { // Wait for the device to come back. // If we were using a specific transport ID, there's nothing we can wait for. if (previous_id == 0) { + std::string wait_for_command = "wait-for-" + state; + D("wait for device to come back (%s)", wait_for_command.c_str()); adb_set_transport(previous_type, previous_serial, 0); - wait_for_device("wait-for-device", 12000ms); + wait_for_device(wait_for_command.c_str(), 12000ms); } return true; @@ -1955,17 +1967,31 @@ int adb_commandline(int argc, const char** argv) { } printf("List of discovered mdns services\n"); return adb_query_command("host:mdns:services"); + } else if (!strcmp(argv[0], "list-known-hosts")) { + if (argc != 2) { + error_exit("mdns %s takes two arguments", argv[0]); + } + + std::string service = "host:"s + HostServices::kListMdnsKnownHosts; + if (!strcmp(argv[1], "--proto-binary")) { + return adb_connect_command(service); + } else if (!strcmp(argv[1], "--proto-text")) { + ProtoBinaryToText callback("\nKnown mDNS hosts:\n"); + return adb_connect_command(service, nullptr, &callback); + } else { + error_exit("unknown mdns command [%s] flag '%s'", argv[0], argv[1]); + } } else if (!strcmp(argv[0], "track-services")) { if (argc != 2) { - error_exit("mdns %s take two arguments", argv[0]); + error_exit("mdns %s takes two arguments", argv[0]); } std::string service = "host:"s + HostServices::kTrackMdnsServices; if (!strcmp(argv[1], "--proto-binary")) { - adb_connect_command(service); + return adb_connect_command(service); } else if (!strcmp(argv[1], "--proto-text")) { ProtoBinaryToText callback("\nServices:\n"); - adb_connect_command(service, nullptr, &callback); + return adb_connect_command(service, nullptr, &callback); } else { error_exit("unknown mdns command [%s] flag '%s'", argv[0], argv[1]); } @@ -2182,11 +2208,11 @@ int adb_commandline(int argc, const char** argv) { } } } else if (!strcmp(argv[0], "inc-server")) { - if (argc < 4) { + if (argc < 3) { #ifdef _WIN32 - error_exit("usage: adb inc-server CONNECTION_HANDLE OUTPUT_HANDLE FILE1 FILE2 ..."); + error_exit("usage: adb inc-server CONNECTION_HANDLE OUTPUT_HANDLE [FILE1 FILE2 ...]"); #else - error_exit("usage: adb inc-server CONNECTION_FD OUTPUT_FD FILE1 FILE2 ..."); + error_exit("usage: adb inc-server CONNECTION_FD OUTPUT_FD [FILE1 FILE2 ...]"); #endif } int connection_fd = atoi(argv[1]); diff --git a/base/cvd/adb/client/commandline.h b/base/cvd/adb/client/commandline.h index 5b3e97b11ab..0923b2a6d8e 100644 --- a/base/cvd/adb/client/commandline.h +++ b/base/cvd/adb/client/commandline.h @@ -210,7 +210,7 @@ int read_and_dump(borrowed_fd fd, bool use_shell_protocol = false, // Connects to the device "abb" service with |command| and returns the fd. template unique_fd send_abb_exec_command(const ContainerT& command_args, std::string* error) { - std::string service_string = "abb_exec:" + android::base::Join(command_args, ABB_ARG_DELIMETER); + std::string service_string = "abb_exec:" + android::base::Join(command_args, ABB_ARG_DELIMITER); unique_fd fd(adb_connect(service_string, error)); if (fd < 0) { diff --git a/base/cvd/adb/client/console.cpp b/base/cvd/adb/client/console.cpp index d10f4deec5f..56286b5f09d 100644 --- a/base/cvd/adb/client/console.cpp +++ b/base/cvd/adb/client/console.cpp @@ -29,22 +29,28 @@ #include "adb_utils.h" // Return the console authentication command for the emulator, if needed -static std::string adb_construct_auth_command() { +static std::string adb_construct_auth_command(const std::string& adb_homedir_path) { static const char auth_token_filename[] = ".emulator_console_auth_token"; - std::string auth_token_path = adb_get_homedir_path(); + std::string auth_token_path = adb_homedir_path; auth_token_path += OS_PATH_SEPARATOR; auth_token_path += auth_token_filename; // read the token std::string token; - if (!android::base::ReadFileToString(auth_token_path, &token) - || token.empty()) { - // we either can't read the file, or it doesn't exist, or it's empty - + if (!android::base::ReadFileToString(auth_token_path, &token)) { + // we either can't read the file, or it doesn't exist - // either way we won't add any authentication command. return {}; } + token = android::base::Trim(token); + if (token.empty()) { + // if the file is empty or just whitespace, we won't add any + // authentication command. + return {}; + } + // now construct and return the actual command: "auth \n" std::string command = "auth "; command += token; @@ -52,6 +58,10 @@ static std::string adb_construct_auth_command() { return command; } +static std::string adb_construct_auth_command() { + return adb_construct_auth_command(adb_get_homedir_path()); +} + // Return the console port of the currently connected emulator (if any) or -1 if // there is no emulator, and -2 if there is more than one. static int adb_get_emulator_console_port(const char* serial) { diff --git a/base/cvd/adb/client/discovered_services.cpp b/base/cvd/adb/client/discovered_services.cpp index b8f2591e8f3..a5fd0d5142b 100644 --- a/base/cvd/adb/client/discovered_services.cpp +++ b/base/cvd/adb/client/discovered_services.cpp @@ -18,6 +18,48 @@ #include "adb_trace.h" +#include + +std::ostream& operator<<(std::ostream& os, const ServiceInfo& info) { + os << info.instance << "." << info.service << ":" << info.port << ""; + return os; +} + +// Parse a key/value from a TXT record. Format expected is "key=value" +std::tuple ServiceInfo::ParseTxtKeyValue(const std::string& kv) { + auto split_loc = std::ranges::find(kv, static_cast('=')); + if (split_loc == kv.end()) { + return {false, "", ""}; + } + std::string key; + std::string value; + + key.assign(kv.begin(), split_loc); + if (split_loc + 1 != kv.end()) { + value.assign(split_loc + 1, kv.end()); + } + + if (key.empty()) { + return {false, key, value}; + } + return {true, key, value}; +} + +std::unordered_map ServiceInfo::ParseTxt( + const std::vector>& txt) { + std::unordered_map kv; + for (auto& in_kv : txt) { + std::string skv = std::string(in_kv.begin(), in_kv.end()); + auto [valid, key, value] = ParseTxtKeyValue(skv); + if (!valid) { + VLOG(MDNS) << "Bad TXT value '" << skv << "'"; + continue; + } + kv[key] = value; + } + return kv; +} + namespace mdns { DiscoveredServices discovered_services [[clang::no_destroy]]; @@ -31,10 +73,46 @@ void DiscoveredServices::ServiceCreated(const ServiceInfo& service_info) { services_[fq_name(service_info)] = service_info; } -void DiscoveredServices::ServiceUpdated(const ServiceInfo& service_info) { +bool DiscoveredServices::ServiceUpdated(const ServiceInfo& service_info) { std::lock_guard lock(services_mutex_); - VLOG(MDNS) << "Service update " << service_info; - services_[fq_name(service_info)] = service_info; + + const auto key = fq_name(service_info); + if (!services_.contains(key)) { + services_[key] = service_info; + return true; + } + + auto& current_service = services_[key]; + bool updated = false; + + if (service_info.v4_address.has_value() && + service_info.v4_address != current_service.v4_address) { + current_service.v4_address = service_info.v4_address; + updated = true; + } + + for (auto& new_address : service_info.v6_addresses) { + if (!current_service.v6_addresses.contains(new_address)) { + updated = true; + current_service.v6_addresses.insert(new_address); + } + } + + if (service_info.port != current_service.port) { + current_service.port = service_info.port; + updated = true; + } + + if (service_info.attributes != current_service.attributes) { + current_service.attributes = service_info.attributes; + updated = true; + } + + if (updated) { + VLOG(MDNS) << "Service update " << service_info; + } + + return updated; } void DiscoveredServices::ServiceDeleted(const ServiceInfo& service_info) { diff --git a/base/cvd/adb/client/discovered_services.h b/base/cvd/adb/client/discovered_services.h index c3c68152979..73fefd4fa69 100644 --- a/base/cvd/adb/client/discovered_services.h +++ b/base/cvd/adb/client/discovered_services.h @@ -16,16 +16,121 @@ #pragma once +#include + +#include +#include +#include +#include +#include #include +#include +#include #include +#include +#include +#include +#include + +struct IPv4Address { + uint8_t bytes[4]; + + bool operator==(const IPv4Address& other) const { + return std::memcmp(bytes, other.bytes, sizeof(bytes)) == 0; + } +}; + +struct IPv6Address { + uint8_t bytes[16]; + + std::string to_string() const { + std::string result; + result.reserve(39); + + for (size_t i = 0; i < 16; i += 2) { + uint16_t word = (static_cast(bytes[i]) << 8) | bytes[i + 1]; + result += std::format("{:x}", word); + + if (i < 14) { + result += ":"; + } + } + return result; + } + + // Allow the struct to be used in an unordered_set + bool operator<(const IPv6Address& other) const { + return std::memcmp(bytes, other.bytes, sizeof(bytes)) < 0; + } + + bool operator==(const IPv6Address& other) const { + return std::memcmp(bytes, other.bytes, sizeof(bytes)) == 0; + } +}; + +struct IPv6AddressHash { + std::size_t operator()(const IPv6Address& addr) const { + return std::hash{}( + std::string_view(reinterpret_cast(addr.bytes), sizeof(addr.bytes))); + } +}; + +struct ServiceInfo { + ServiceInfo() = default; + + ServiceInfo(const std::string& in_instance, const std::string& in_service, + const std::string& hostname, const std::optional& in_v4_address, + const std::unordered_set& in_v6_addresses, + const uint16_t in_port, const std::vector>& txt) + : instance(in_instance), + service(in_service), + hostname(hostname), + v4_address(in_v4_address), + v6_addresses(in_v6_addresses), + port(in_port), + attributes(ParseTxt(txt)) {} + + std::string instance; + std::string service; + std::string hostname; + std::optional v4_address; + std::unordered_set v6_addresses; + uint16_t port; + + // Store keys/values from TXT resource record + std::unordered_map attributes; + + std::string v4_address_string() const { + if (!v4_address.has_value()) { + return ""; + } + return std::format("{}.{}.{}.{}", v4_address->bytes[0], v4_address->bytes[1], + v4_address->bytes[2], v4_address->bytes[3]); + } -#include "client/openscreen/mdns_service_info.h" + // Parse a key/value from a TXT record. Format expected is "key=value" + static std::tuple ParseTxtKeyValue(const std::string& kv); + static std::unordered_map ParseTxt( + const std::vector>& txt); +}; + +std::ostream& operator<<(std::ostream& os, const ServiceInfo& info); + +enum ServiceInfoState { + Created, + Updated, + Deleted, +}; namespace mdns { class DiscoveredServices { public: void ServiceCreated(const ServiceInfo& service_info); - void ServiceUpdated(const ServiceInfo& service_info); + + // Return true if the provided service_info resulted in an update + // of the internal state of DiscoveredServices + bool ServiceUpdated(const ServiceInfo& service_info); + void ServiceDeleted(const ServiceInfo& service_info); std::optional FindInstance(const std::string& service, const std::string& instance); diff --git a/base/cvd/adb/client/host_services.h b/base/cvd/adb/client/host_services.h index 84718b1ab86..4eb0fe69467 100644 --- a/base/cvd/adb/client/host_services.h +++ b/base/cvd/adb/client/host_services.h @@ -20,4 +20,5 @@ namespace HostServices { constexpr const char* kTrackMdnsServices = "track-mdns-services"; +constexpr const char* kListMdnsKnownHosts = "list-mdns-known-hosts"; } \ No newline at end of file diff --git a/base/cvd/adb/client/incremental.cpp b/base/cvd/adb/client/incremental.cpp index 2319c2f895a..2a48fcc9e97 100644 --- a/base/cvd/adb/client/incremental.cpp +++ b/base/cvd/adb/client/incremental.cpp @@ -36,6 +36,7 @@ #include #include "adb_install.h" +#include "adb_io.h" #include "adb_unique_fd.h" #include "commandline.h" #include "incremental_utils.h" @@ -45,6 +46,12 @@ using namespace std::literals; namespace incremental { +enum class CheckPolicy { + NORMAL, + // Allows missing signatures for files that would normally require a v4 signature. + ALLOW_MISSING_SIGNATURES, +}; + // Used to be sent as arguments via install-incremental, to describe the IncrementalServer database. class ISDatabaseEntry { public: @@ -189,7 +196,7 @@ static std::optional> open_and_get_size(const std:: // - For unsigned files in the list, the caller is expected to send them through stdin before // streaming the signed ones, in the order specified by the list. static std::optional>> build_database( - const Files& files, std::string* error) { + const Files& files, CheckPolicy check_policy, std::string* error) { std::unordered_map, int32_t>> signatures_by_file; for (const std::string& file : files) { @@ -198,8 +205,13 @@ static std::optional>> build_databa return {}; } if (requires_v4_signature(file) && signature_and_tree_size->first.empty()) { - *error = std::format("V4 signature missing for '{}'", file); - return {}; + std::string error_msg = std::format("V4 signature missing for '{}'", file); + if (check_policy == CheckPolicy::ALLOW_MISSING_SIGNATURES) { + fprintf(stderr, "%s.\n", error_msg.c_str()); + } else { + *error = std::move(error_msg); + return {}; + } } signatures_by_file[file] = std::move(*signature_and_tree_size); } @@ -258,7 +270,7 @@ static std::optional>> build_databa } // Opens a connection and sends install-incremental to the device along with the database. -// Returns a socket FD connected to the `abb` deamon on device, where writes to it go to `pm` +// Returns a socket FD connected to the `abb` daemon on device, where writes to it go to `pm` // shell's stdin and reads from it come from `pm` shell's stdout. static std::optional connect_and_send_database( const std::vector>& database, const Args& passthrough_args, @@ -280,7 +292,7 @@ static std::optional connect_and_send_database( return connection_fd; } -bool can_install(const Files& files) { +bool should_use_incremental_by_default(const Files& files) { for (const auto& file : files) { struct stat st; if (stat(file.c_str(), &st)) { @@ -327,12 +339,23 @@ static bool wait_for_installation(int read_fd, std::string* error) { static constexpr int kMaxMessageSize = 256; std::string child_stdout; child_stdout.resize(kMaxMessageSize); - int bytes_read = adb_read(read_fd, child_stdout.data(), kMaxMessageSize); - if (bytes_read < 0) { + if (!ReadFdExactly(read_fd, child_stdout.data(), kMaxMessageSize) && errno != 0) { *error = std::format("Failed to read output: {}", strerror(errno)); return false; } - child_stdout.resize(bytes_read); + // Truncate to '\0'. `ReadFdExactly` reads until the end of the steam and leaves the remaining + // bytes in the buffer unchanged, which were default-initialized ('\0') by + // `std::string::resize`. + size_t len = child_stdout.find('\0'); + if (len != std::string::npos) { + child_stdout.resize(len); + } + + printf("%s", child_stdout.c_str()); + if (!child_stdout.ends_with('\n')) { + printf("\n"); + } + // wait till installation either succeeds or fails if (child_stdout.find("Success") != std::string::npos) { return true; @@ -342,17 +365,15 @@ static bool wait_for_installation(int read_fd, std::string* error) { if (begin_itr != std::string::npos) { auto end_itr = child_stdout.rfind("]"); if (end_itr != std::string::npos && end_itr >= begin_itr) { - *error = std::format( - "Install failed: {}", - std::string_view(child_stdout).substr(begin_itr, end_itr - begin_itr + 1)); + *error = "Install failed"; return false; } } - if (bytes_read == kMaxMessageSize) { - *error = std::format("Output too long: {}", child_stdout); + if (child_stdout.length() == kMaxMessageSize) { + *error = "Output too long"; return false; } - *error = std::format("Failed to parse output: {}", child_stdout); + *error = "Failed to parse output"; return false; } @@ -366,15 +387,15 @@ static std::optional start_inc_server_and_stream_signed_files( return {}; } auto [pipe_read_fd, pipe_write_fd] = print_fds; - auto fd_cleaner = android::base::make_scope_guard([&] { - adb_close(pipe_read_fd); - adb_close(pipe_write_fd); - }); + auto read_fd_cleaner = android::base::make_scope_guard([&] { adb_close(pipe_read_fd); }); + auto write_fd_cleaner = android::base::make_scope_guard([&] { adb_close(pipe_write_fd); }); close_on_exec(pipe_read_fd); // We spawn an incremental server that will be up until all blocks have been fed to the // Package Manager. This could take a long time depending on the size of the files to // stream so we use a process able to outlive adb. + // Note that there might not be any signed files in the database, in which case we still need + // to spawn the server to process the output from `pm`. std::vector args{ "inc-server", std::to_string(cast_handle_to_int(adb_get_os_handle(connection_fd.get()))), @@ -396,6 +417,9 @@ static std::optional start_inc_server_and_stream_signed_files( *error = "adb: failed to fork"; return {}; } + // Close the write FD, so that reads on the read FD returns as soon as the child process exits. + adb_close(pipe_write_fd); + write_fd_cleaner.Disable(); auto server_killer = android::base::make_scope_guard([&] { child.kill(); }); // Block until the Package Manager has received enough blocks to declare the installation @@ -410,10 +434,10 @@ static std::optional start_inc_server_and_stream_signed_files( return child; } -std::optional install(const Files& files, const Args& passthrough_args, - std::string* error) { +static std::optional install(const Files& files, const Args& passthrough_args, + CheckPolicy check_policy, std::string* error) { std::optional>> database = - build_database(files, error); + build_database(files, check_policy, error); if (!database.has_value()) { return {}; } @@ -430,7 +454,9 @@ std::optional install(const Files& files, const Args& passthrough_args, std::optional install(const Files& files, const Args& passthrough_args, bool silent) { std::string error; - std::optional res = install(files, passthrough_args, &error); + std::optional res = + install(files, passthrough_args, + silent ? CheckPolicy::NORMAL : CheckPolicy::ALLOW_MISSING_SIGNATURES, &error); if (!res.has_value() && !silent) { fprintf(stderr, "%s.\n", error.c_str()); } diff --git a/base/cvd/adb/client/incremental.h b/base/cvd/adb/client/incremental.h index 7861cdce448..d2f863c762f 100644 --- a/base/cvd/adb/client/incremental.h +++ b/base/cvd/adb/client/incremental.h @@ -27,7 +27,9 @@ namespace incremental { using Files = std::vector; using Args = std::vector; -bool can_install(const Files& files); +// Returns true if incremental installation should be used by default for the given set of files. +bool should_use_incremental_by_default(const Files& files); + std::optional install(const Files& files, const Args& passthrough_args, bool silent); } // namespace incremental diff --git a/base/cvd/adb/client/incremental_server.cpp b/base/cvd/adb/client/incremental_server.cpp index 6c95d8b38f7..1ab1b4ab355 100644 --- a/base/cvd/adb/client/incremental_server.cpp +++ b/base/cvd/adb/client/incremental_server.cpp @@ -327,7 +327,13 @@ bool IncrementalServer::SkipToRequest(void* buffer, size_t* size, bool blocking) continue; } - D("Failed to read from fd %d: %d. Exit", adb_fd_.get(), errno); + // Nothing was read. Remove the padding added before the read. + buffer_.resize(bsize); + if (r == 0) { + D("Disconnected from fd %d. Exit", adb_fd_.get()); + } else { + D("Failed to read from fd %d: %d. Exit", adb_fd_.get(), errno); + } break; } // socket is closed. print remaining messages @@ -705,9 +711,6 @@ static std::pair open_signature(int64_t file_size, const cha bool serve(int connection_fd, int output_fd, int argc, const char** argv) { auto connection_ufd = unique_fd(connection_fd); auto output_ufd = unique_fd(output_fd); - if (argc <= 0) { - error_exit("inc-server: must specify at least one file."); - } std::vector files; files.reserve(argc); diff --git a/base/cvd/adb/client/incremental_utils.cpp b/base/cvd/adb/client/incremental_utils.cpp index 083078461c8..ece192aa0b1 100644 --- a/base/cvd/adb/client/incremental_utils.cpp +++ b/base/cvd/adb/client/incremental_utils.cpp @@ -123,7 +123,7 @@ static inline bool skip_bytes_with_size(borrowed_fd fd, std::string* error) { } int32_t size = int32_t(le32toh(*le_size)); if (size < 0) { - *error = std::format("Invalid sze {}", size); + *error = std::format("Invalid size {}", size); return false; } if (adb_lseek(fd, size, SEEK_CUR) < 0) { diff --git a/base/cvd/adb/client/main.cpp b/base/cvd/adb/client/main.cpp index ac733cc960a..d8186c36417 100644 --- a/base/cvd/adb/client/main.cpp +++ b/base/cvd/adb/client/main.cpp @@ -36,7 +36,7 @@ #include "adb_listeners.h" #include "adb_mdns.h" #include "adb_utils.h" -#include "adb_wifi.h" +#include "client/adb_wifi.h" #include "client/mdns_utils.h" #include "client/transport_client.h" #include "client/usb.h" @@ -137,7 +137,7 @@ int adb_server_main(int is_daemon, const std::string& socket_spec, const char* o init_mdns_transport_discovery(); } - if (!getenv("ADB_USB") || strcmp(getenv("ADB_USB"), "0") != 0) { + if (is_usb_enabled()) { if (is_libusb_enabled()) { libusb::usb_init(); } else { diff --git a/base/cvd/adb/client/mdns_tracker.cpp b/base/cvd/adb/client/mdns_tracker.cpp index db535fef194..597d66cf18b 100644 --- a/base/cvd/adb/client/mdns_tracker.cpp +++ b/base/cvd/adb/client/mdns_tracker.cpp @@ -38,7 +38,7 @@ static std::list mdns_trackers [[clang::no_destroy]]; static std::string list_mdns_services() { adb::proto::MdnsServices services; - mdns::discovered_services.ForAllServices([&](const mdns::ServiceInfo& service) { + mdns::discovered_services.ForAllServices([&](const ServiceInfo& service) { adb::proto::MdnsService* s = nullptr; if (service.service == ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_SERVICE_TYPE)) { @@ -49,7 +49,7 @@ static std::string list_mdns_services() { s = pair->mutable_service(); } else if (service.service == ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_CONNECT_TYPE)) { auto* tls = services.add_tls(); - tls->set_known_device(adb_wifi_is_known_host(service.instance)); + tls->set_known_device(known_wifi_hosts_file.IsKnownHost(service.instance)); s = tls->mutable_service(); } else { LOG(WARNING) << "Unknown service type: " << service; @@ -60,9 +60,19 @@ static std::string list_mdns_services() { s->set_service(service.service); s->set_port(service.port); - s->set_ipv4(service.v4_address_string()); - auto ipv6 = s->add_ipv6(); - ipv6->append(service.v6_address_string()); + if (service.v4_address.has_value()) { + s->set_ipv4(service.v4_address_string()); + } + + for (auto& address : service.v6_addresses) { + auto ipv6 = s->add_ipv6(); + ipv6->append(address.to_string()); + } + + s->set_mdns_service_version(service.attributes.contains("v") ? service.attributes.at("v") + : "1"); + + s->set_hostname(service.hostname); if (service.attributes.contains("name")) { s->set_product_model(service.attributes.at("name")); @@ -70,6 +80,12 @@ static std::string list_mdns_services() { if (service.attributes.contains("api")) { s->set_build_version_sdk_full(service.attributes.at("api")); } + if (service.attributes.contains("given_name")) { + s->set_given_name(service.attributes.at("given_name")); + } + if (service.attributes.contains("serial")) { + s->set_serial(service.attributes.at("serial")); + } }); std::string proto; @@ -78,7 +94,7 @@ static std::string list_mdns_services() { } static void mdns_tracker_close(asocket* socket) { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); auto* tracker = reinterpret_cast(socket); asocket* peer = socket->peer; @@ -92,14 +108,14 @@ static void mdns_tracker_close(asocket* socket) { } static int device_tracker_enqueue(asocket* socket, apacket::payload_type) { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); /* you can't read from a device tracker, close immediately */ mdns_tracker_close(socket); return -1; } static int mdns_tracker_send(const MdnsTracker* tracker, const std::string& string) { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); asocket* peer = tracker->socket_.peer; apacket::payload_type data; @@ -112,7 +128,7 @@ static int mdns_tracker_send(const MdnsTracker* tracker, const std::string& stri } static void mdns_tracker_ready(asocket* socket) { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); auto* tracker = reinterpret_cast(socket); // We want to send the service list when the tracker connects @@ -124,7 +140,7 @@ static void mdns_tracker_ready(asocket* socket) { } asocket* create_mdns_tracker() { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); auto* tracker = new MdnsTracker(); VLOG(MDNS) << "mdns tracker created"; diff --git a/base/cvd/adb/client/mdns_utils.cpp b/base/cvd/adb/client/mdns_utils.cpp index 74a90366423..9f08f85777c 100644 --- a/base/cvd/adb/client/mdns_utils.cpp +++ b/base/cvd/adb/client/mdns_utils.cpp @@ -75,7 +75,11 @@ std::optional mdns_parse_instance_name(std::string_view name) { } bool is_enabled() { - return !getenv("ADB_MDNS") || strcmp(getenv("ADB_MDNS"), "0") != 0; + return false; +} + +bool should_use_openscreen() { + return false; } } // namespace mdns diff --git a/base/cvd/adb/client/mdns_utils.h b/base/cvd/adb/client/mdns_utils.h index 86b8fe9871b..d10613312c8 100644 --- a/base/cvd/adb/client/mdns_utils.h +++ b/base/cvd/adb/client/mdns_utils.h @@ -53,4 +53,6 @@ std::optional mdns_parse_instance_name(std::string_view name); // Return true if mdns backend is enabled bool is_enabled(); + +bool should_use_openscreen(); } // namespace mdns diff --git a/base/cvd/adb/client/openscreen/mdns_service_info.cpp b/base/cvd/adb/client/openscreen/mdns_service_info.cpp deleted file mode 100644 index 0df2caea471..00000000000 --- a/base/cvd/adb/client/openscreen/mdns_service_info.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (C) 2020 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. - */ - -#include "client/openscreen/mdns_service_info.h" - -#include "adb_mdns.h" - -using namespace openscreen; - -namespace mdns { - -std::string ServiceInfo::v4_address_string() const { - std::stringstream ss; - ss << v4_address; - return ss.str(); -} - -std::string ServiceInfo::v6_address_string() const { - std::stringstream ss; - ss << v6_address; - return ss.str(); -} - -// Parse a key/value from a TXT record. Format expected is "key=value" -std::tuple ParseTxtKeyValue(const std::string& kv) { - auto split_loc = std::ranges::find(kv, static_cast('=')); - if (split_loc == kv.end()) { - return {false, "", ""}; - } - std::string key; - std::string value; - - key.assign(kv.begin(), split_loc); - if (split_loc + 1 != kv.end()) { - value.assign(split_loc + 1, kv.end()); - } - - if (key.empty()) { - return {false, key, value}; - } - return {true, key, value}; -} - -static std::unordered_map ParseTxt( - std::vector>& txt) { - std::unordered_map kv; - for (auto& in_kv : txt) { - std::string skv = std::string(in_kv.begin(), in_kv.end()); - auto [valid, key, value] = ParseTxtKeyValue(skv); - if (!valid) { - VLOG(MDNS) << "Bad TXT value '" << skv << "'"; - continue; - } - VLOG(MDNS) << "Parsed TXT key='" << key << "', value='" << value << "'"; - kv[key] = value; - } - return kv; -} - -ErrorOr DnsSdInstanceEndpointToServiceInfo( - const discovery::DnsSdInstanceEndpoint& endpoint) { - ServiceInfo service_info; - - service_info.instance = endpoint.instance_id(); - service_info.service = endpoint.service_id(); - service_info.port = endpoint.port(); - for (const IPAddress& address : endpoint.addresses()) { - if (!service_info.v4_address && address.IsV4()) { - service_info.v4_address = address; - } else if (!service_info.v6_address && address.IsV6()) { - service_info.v6_address = address; - } - } - CHECK(service_info.v4_address || service_info.v6_address); - - auto txt = endpoint.txt().GetData(); - service_info.attributes = ParseTxt(txt); - - return service_info; -} - -} // namespace mdns diff --git a/base/cvd/adb/client/openscreen/mdns_service_info.h b/base/cvd/adb/client/openscreen/mdns_service_info.h deleted file mode 100644 index 1cfd3498634..00000000000 --- a/base/cvd/adb/client/openscreen/mdns_service_info.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2020 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. - */ - -#pragma once - -#include -#include - -#include -#include - -#include "client/mdns_utils.h" - -namespace mdns { - -struct ServiceInfo { - std::string instance; - std::string service; - openscreen::IPAddress v4_address; - openscreen::IPAddress v6_address; - uint16_t port; - - std::string v4_address_string() const; - std::string v6_address_string() const; - - // Store keys/values from TXT resource record - std::unordered_map attributes; -}; // ServiceInfo - -inline std::ostream& operator<<(std::ostream& os, const ServiceInfo& service_info) { - os << "Instance: " << service_info.instance << ", Service: " << service_info.service - << ", Port: " << service_info.port << ", IPv4: " << service_info.v4_address - << ", IPv6: " << service_info.v6_address; - return os; -} - -openscreen::ErrorOr DnsSdInstanceEndpointToServiceInfo( - const openscreen::discovery::DnsSdInstanceEndpoint& endpoint); - -std::tuple ParseTxtKeyValue(const std::string& kv); - -} // namespace mdns diff --git a/base/cvd/adb/client/openscreen/platform/logging.cpp b/base/cvd/adb/client/openscreen/platform/logging.cpp deleted file mode 100644 index 3b9be16a1a9..00000000000 --- a/base/cvd/adb/client/openscreen/platform/logging.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2020 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. - */ - -#include - -#include - -#include "adb_trace.h" - -namespace openscreen { - -bool IsLoggingOn(LogLevel level, const char* file) { - return true; -} - -static android::base::LogSeverity OpenScreenLogLevelToAndroid(LogLevel level) { - switch (level) { - case LogLevel::kVerbose: - return android::base::VERBOSE; - case LogLevel::kInfo: - return android::base::INFO; - case LogLevel::kWarning: - return android::base::WARNING; - case LogLevel::kError: - return android::base::ERROR; - case LogLevel::kFatal: - return android::base::FATAL; - } -} - -void LogWithLevel(LogLevel level, const char* file, int line, std::stringstream desc) { - auto severity = OpenScreenLogLevelToAndroid(level); - std::string msg = std::string("(") + file + ":" + std::to_string(line) + ") " + desc.str(); - - // We never ignore a warning or worse (error and fatals). - if (severity >= android::base::WARNING) { - LOG(severity) << msg; - } else { - VLOG(MDNS_STACK) << msg; - } -} - -[[noreturn]] void Break() { - LOG(FATAL) << "openscreen Break() called"; - abort(); // LOG(FATAL) isn't [[noreturn]]. -} - -} // namespace openscreen diff --git a/base/cvd/adb/client/openscreen/platform/task_runner.cpp b/base/cvd/adb/client/openscreen/platform/task_runner.cpp deleted file mode 100644 index a7d15483884..00000000000 --- a/base/cvd/adb/client/openscreen/platform/task_runner.cpp +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (C) 2020 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. - */ - -#include "client/openscreen/platform/task_runner.h" - -#include -#include - -#include -#include -#include - -#include "fdevent/fdevent.h" - -using android::base::ScopedLockAssertion; -using namespace openscreen; - -namespace mdns { - -AdbOspTaskRunner::AdbOspTaskRunner() { - fdevent_check_looper(); - thread_id_ = android::base::GetThreadId(); - task_handler_ = std::thread([this]() { TaskExecutorWorker(); }); -} - -AdbOspTaskRunner::~AdbOspTaskRunner() { - if (task_handler_.joinable()) { - terminate_loop_ = true; - cv_.notify_one(); - task_handler_.join(); - } -} - -void AdbOspTaskRunner::PostPackagedTask(Task task) { - PostPackagedTaskWithDelay(std::move(task), openscreen::Clock::duration::zero()); -} - -void AdbOspTaskRunner::PostPackagedTaskWithDelay(Task task, Clock::duration delay) { - auto now = std::chrono::steady_clock::now(); - { - std::lock_guard lock(mutex_); - tasks_.emplace(now + delay, std::move(task)); - } - cv_.notify_one(); -} - -bool AdbOspTaskRunner::IsRunningOnTaskRunner() { - return (thread_id_ == android::base::GetThreadId()); -} - -void AdbOspTaskRunner::TaskExecutorWorker() { - for (;;) { - { - // Wait until there's a task available. - std::unique_lock lock(mutex_); - ScopedLockAssertion assume_locked(mutex_); - while (!terminate_loop_ && tasks_.empty()) { - cv_.wait(lock); - } - if (terminate_loop_) { - return; - } - - // Wait until the task with the closest time point is ready to run. - auto timepoint = tasks_.begin()->first; - while (timepoint > std::chrono::steady_clock::now()) { - cv_.wait_until(lock, timepoint); - // It's possible that another task with an earlier time was added - // while waiting for |timepoint|. - timepoint = tasks_.begin()->first; - - if (terminate_loop_) { - return; - } - } - } - - // Execute all tasks whose time points have passed. - std::vector running_tasks; - { - std::lock_guard lock(mutex_); - - while (!tasks_.empty()) { - auto task_with_delay = tasks_.begin(); - if (task_with_delay->first > std::chrono::steady_clock::now()) { - break; - } else { - running_tasks.emplace_back(std::move(task_with_delay->second)); - tasks_.erase(task_with_delay); - } - } - } - - CHECK(!running_tasks.empty()); - std::packaged_task waitable_task([&] { - for (Task& task : running_tasks) { - task(); - } - return 0; - }); - - fdevent_run_on_looper([&]() { waitable_task(); }); - - waitable_task.get_future().wait(); - } -} -} // namespace mdns diff --git a/base/cvd/adb/client/openscreen/platform/task_runner.h b/base/cvd/adb/client/openscreen/platform/task_runner.h deleted file mode 100644 index 814d2505bac..00000000000 --- a/base/cvd/adb/client/openscreen/platform/task_runner.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2020 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. - */ - -#pragma once - -#include - -#include -#include -#include -#include -#include -#include - -#include - -namespace mdns { - -// Openscreen API surface that allows for posting tasks. The underlying -// implementation may be single or multi-threaded, and all complication should -// be handled by the implementation class. The implementation must guarantee: -// (1) Tasks shall not overlap in time/CPU. -// (2) Tasks shall run sequentially, e.g. posting task A then B implies -// that A shall run before B. -// (3) If task A is posted before task B, then any mutation in A happens-before -// B runs (even if A and B run on different threads). -// -// Adb implementation: The PostPackagedTask* APIs are thread-safe. -// Another thread will handle dequeuing each item and calling it on the fdevent thread. -// Thus, the task runner thread is the fdevent thread, and IsRunningOnTaskRunner() shall always -// return true if calling from within the running Task. -class AdbOspTaskRunner final : public openscreen::TaskRunner { - public: - using Task = openscreen::TaskRunner::Task; - - // Must be called on the fdevent thread. - explicit AdbOspTaskRunner(); - ~AdbOspTaskRunner() final; - void PostPackagedTask(Task task) final; - void PostPackagedTaskWithDelay(Task task, openscreen::Clock::duration delay) final; - bool IsRunningOnTaskRunner() final; - - // The task executor thread. - void TaskExecutorWorker(); - - private: - uint64_t thread_id_; - std::mutex mutex_; - std::multimap, Task> tasks_ - GUARDED_BY(mutex_); - std::atomic terminate_loop_ = false; - std::condition_variable cv_; - std::thread task_handler_; -}; - -} // namespace mdns diff --git a/base/cvd/adb/client/openscreen/platform/udp_socket.cpp b/base/cvd/adb/client/openscreen/platform/udp_socket.cpp deleted file mode 100644 index 4ea9fab7862..00000000000 --- a/base/cvd/adb/client/openscreen/platform/udp_socket.cpp +++ /dev/null @@ -1,674 +0,0 @@ -/* - * Copyright (C) 2020 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. - */ - -#include - -#include -#include - -#include -#include - -#include "adb_unique_fd.h" -#include "adb_utils.h" -#include "fdevent/fdevent.h" -#include "sysdeps.h" - -//#define DEBUG_UDP - -constexpr bool IsPowerOf2(uint32_t x) { - return (x > 0) && ((x & (x - 1)) == 0); -} - -static_assert(IsPowerOf2(alignof(adb_cmsghdr)), "std::align requires power-of-2 alignment"); - -// For windows, winuser.h defines a SendMessage macro that causes compilation issues. -#ifdef _WIN32 -#ifdef SendMessage -#undef SendMessage -#endif -#endif -namespace openscreen { - -namespace { - -#ifdef _WIN32 -using IPv4NetworkInterfaceIndex = decltype(ip_mreq().imr_interface.s_addr); -#else -using IPv4NetworkInterfaceIndex = decltype(ip_mreqn().imr_ifindex); -#endif -using IPv6NetworkInterfaceIndex = decltype(ipv6_mreq().ipv6mr_interface); - -// Examine |posix_errno| to determine whether the specific cause of a failure -// was transient or hard, and return the appropriate error response. -Error ChooseError(decltype(errno) posix_errno, Error::Code hard_error_code) { - if (posix_errno == EAGAIN || posix_errno == EWOULDBLOCK || posix_errno == ENOBUFS) { - return Error(Error::Code::kAgain, strerror(errno)); - } - return Error(hard_error_code, strerror(errno)); -} - -IPAddress GetIPAddressFromSockAddr(const sockaddr_in& sa) { - static_assert(IPAddress::kV4Size == sizeof(sa.sin_addr.s_addr), "IPv4 address size mismatch."); - return IPAddress(IPAddress::Version::kV4, - reinterpret_cast(&sa.sin_addr.s_addr)); -} - -IPAddress GetIPAddressFromPktInfo(const in_pktinfo& pktinfo) { - static_assert(IPAddress::kV4Size == sizeof(pktinfo.ipi_addr), "IPv4 address size mismatch."); - return IPAddress(IPAddress::Version::kV4, reinterpret_cast(&pktinfo.ipi_addr)); -} - -uint16_t GetPortFromFromSockAddr(const sockaddr_in& sa) { - return ntohs(sa.sin_port); -} - -IPAddress GetIPAddressFromSockAddr(const sockaddr_in6& sa) { - return IPAddress(IPAddress::Version::kV6, sa.sin6_addr.s6_addr); -} - -IPAddress GetIPAddressFromPktInfo(const in6_pktinfo& pktinfo) { - return IPAddress(IPAddress::Version::kV6, pktinfo.ipi6_addr.s6_addr); -} - -uint16_t GetPortFromFromSockAddr(const sockaddr_in6& sa) { - return ntohs(sa.sin6_port); -} - -template -bool IsPacketInfo(adb_cmsghdr* cmh); - -template <> -bool IsPacketInfo(adb_cmsghdr* cmh) { - return cmh->cmsg_level == IPPROTO_IP && cmh->cmsg_type == IP_PKTINFO; -} - -template <> -bool IsPacketInfo(adb_cmsghdr* cmh) { - return cmh->cmsg_level == IPPROTO_IPV6 && cmh->cmsg_type == IPV6_PKTINFO; -} - -template -Error ReceiveMessageInternal(borrowed_fd fd, UdpPacket* packet) { - SockAddrType sa; - adb_iovec iov; - iov.iov_len = packet->size(); - iov.iov_base = packet->data(); - alignas(alignof(adb_cmsghdr)) uint8_t control_buffer[1024]; - adb_msghdr msg; - msg.msg_name = &sa; - msg.msg_namelen = sizeof(sa); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - msg.msg_control = control_buffer; - msg.msg_controllen = sizeof(control_buffer); - msg.msg_flags = 0; - - ssize_t bytes_received = adb_recvmsg(fd, &msg, 0); - if (bytes_received == -1) { - return ChooseError(errno, Error::Code::kSocketReadFailure); - } - - CHECK_EQ(static_cast(bytes_received), packet->size()); - - IPEndpoint source_endpoint = {.address = GetIPAddressFromSockAddr(sa), - .port = GetPortFromFromSockAddr(sa)}; - packet->set_source(std::move(source_endpoint)); - - // For multicast sockets, the packet's original destination address may be - // the host address (since we called bind()) but it may also be a - // multicast address. This may be relevant for handling multicast data; - // specifically, mDNSResponder requires this information to work properly. - - socklen_t sa_len = sizeof(sa); - if (((msg.msg_flags & MSG_CTRUNC) != 0) || - (adb_getsockname(fd, reinterpret_cast(&sa), &sa_len) == -1)) { - return Error::Code::kNone; - } - for (adb_cmsghdr* cmh = adb_CMSG_FIRSTHDR(&msg); cmh; cmh = adb_CMSG_NXTHDR(&msg, cmh)) { - if (IsPacketInfo(cmh)) { - PktInfoType* pktinfo = reinterpret_cast(adb_CMSG_DATA(cmh)); - IPEndpoint destination_endpoint = {.address = GetIPAddressFromPktInfo(*pktinfo), - .port = GetPortFromFromSockAddr(sa)}; - packet->set_destination(destination_endpoint); - break; - } - } - return Error::Code::kNone; -} -// An open UDP socket for sending/receiving datagrams to/from either specific -// endpoints or over IP multicast. -// -// Usage: The socket is created and opened by calling the Create() method. This -// returns a unique pointer that auto-closes/destroys the socket when it goes -// out-of-scope. -class AdbUdpSocket : public UdpSocket { - public: - explicit AdbUdpSocket(UdpSocket::Client* client, const IPEndpoint& local_endpoint, unique_fd fd) - : client_(client), local_endpoint_(local_endpoint), fd_(std::move(fd)) { - CHECK(client_); - CHECK(local_endpoint_.address.IsV4() || local_endpoint_.address.IsV6()); - fde_ = fdevent_create(fd_.get(), OnFdeventResult, this); - if (!fde_) { - LOG(FATAL) << "Unable to create fdevent"; - return; - } - fdevent_set(fde_, FDE_READ); - VLOG(MDNS) << " fd=" << fd_.get(); - } - - ~AdbUdpSocket() override { - if (fde_) { - fdevent_destroy(fde_); - } - } - - // Returns true if |socket| belongs to the IPv4/IPv6 address family. - bool IsIPv4() const override { return local_endpoint_.address.IsV4(); } - bool IsIPv6() const override { return local_endpoint_.address.IsV6(); } - - // Returns the current local endpoint's address and port. Initially, this will - // be the same as the value that was passed into Create(). However, it can - // later change after certain operations, such as Bind(), are executed. - IPEndpoint GetLocalEndpoint() const override { - if (local_endpoint_.port == 0) { - // Note: If the getsockname() call fails, just assume that's because the - // socket isn't bound yet. In this case, leave the original value in-place. - switch (local_endpoint_.address.version()) { - case UdpSocket::Version::kV4: { - struct sockaddr_in address; - socklen_t address_len = sizeof(address); - if (adb_getsockname(fd_, reinterpret_cast(&address), - &address_len) == 0) { - CHECK_EQ(address.sin_family, AF_INET); - local_endpoint_.address = - IPAddress(IPAddress::Version::kV4, - reinterpret_cast(&address.sin_addr.s_addr)); - local_endpoint_.port = ntohs(address.sin_port); - } - break; - } - - case UdpSocket::Version::kV6: { - struct sockaddr_in6 address; - socklen_t address_len = sizeof(address); - if (adb_getsockname(fd_, reinterpret_cast(&address), - &address_len) == 0) { - CHECK_EQ(address.sin6_family, AF_INET6); - local_endpoint_.address = - IPAddress(IPAddress::Version::kV6, - reinterpret_cast(&address.sin6_addr)); - local_endpoint_.port = ntohs(address.sin6_port); - } - break; - } - } - } - - return local_endpoint_; - } - - // Binds to the address specified in the constructor. If the local endpoint's - // address is zero, the operating system will bind to all interfaces. If the - // local endpoint's port is zero, the operating system will automatically find - // a free local port and bind to it. Future calls to GetLocalEndpoint() will - // reflect the resolved port. - // - // TODO: openscreen does some chromium compat thing where it calls Bind() before - // SetMulticastOutboundInterface(), because chromium alreadys sets IP_MULTICAST_IF internally - // before calling Bind(). So we currently wait for the SetMulticastOutboundInterface() call from - // the osp-discovery code before actually binding. Note that this means AdbUdpSocket is not for - // the general use-case of udp sockets. - void Bind() override { - if (mdns_ifindex_) { - // TODO: move MdnsBind() code back into here once osp-discovery calls Bind() after - // SetMulticastOutboundInterface(). - LOG(FATAL) << "osp-discovery called Bind() after SetMulticastOutboundInterface()!"; - } else { - // mdns impl will only call SetMulticastOutboundInterface and JoinMulticastGroup after - // bind is successful. - client_->OnBound(this); - } - } - - static void SetIPV4MulticastProperties(std::optional local_ipv4, - std::optional multiaddr_ipv4, - ip_mreq* result) { - CHECK(result); - static_assert(sizeof(result->imr_multiaddr) == 4u, "IPv4 address requires exactly 4 bytes"); - static_assert(sizeof(result->imr_interface) == 4u, "IPv4 address requires exactly 4 bytes"); - - *result = {}; - if (local_ipv4) { - local_ipv4->CopyToV4(reinterpret_cast(&result->imr_interface.s_addr)); - } - - if (multiaddr_ipv4) { - multiaddr_ipv4->CopyToV4(reinterpret_cast(&result->imr_multiaddr)); - } - } - - // Sets the device to use for outgoing multicast packets on the socket. - void SetMulticastOutboundInterface(NetworkInterfaceIndex ifindex) override { - if (!fd_.ok()) { - OnError(Error::Code::kSocketClosedFailure); - return; - } - - // TODO: remove once osp-discovery calls Bind() after SetMulticastOutboundInterface(). - *mdns_ifindex_ = ifindex; - - VLOG(MDNS) << "SetMulticastOutboundInterface for index=" << ifindex; - switch (local_endpoint_.address.version()) { - case UdpSocket::Version::kV4: { - struct ip_mreq multicast_properties = {}; - SetIPV4MulticastProperties(local_endpoint_.address, std::nullopt, - &multicast_properties); -#ifdef DEBUG_UDP - struct in_addr default_addr; - unsigned int buf_size = sizeof(default_addr); - if (getsockopt(fd_.get(), IPPROTO_IP, IP_MULTICAST_IF, &default_addr, &buf_size) != - -1) { - const auto default_ip = - IPAddress(IPAddress::Version::kV4, - reinterpret_cast(&default_addr.s_addr)); - VLOG(MDNS) << "BEFORE IP_MULTICAST_IF: default multicast addr=" << default_ip; - } -#endif // DEBUG_UDP - if (adb_setsockopt(fd_, IPPROTO_IP, IP_MULTICAST_IF, &multicast_properties, - sizeof(multicast_properties)) == -1) { - OnError(Error::Code::kSocketOptionSettingFailure); - PLOG(ERROR) << "adb_setsockopt() failed"; - return; - } -#ifdef DEBUG_UDP -#ifndef _WIN32 - buf_size = sizeof(default_addr); - if (getsockopt(fd_.get(), IPPROTO_IP, IP_MULTICAST_IF, &default_addr, &buf_size) != - -1) { - const auto default_ip = - IPAddress(IPAddress::Version::kV4, - reinterpret_cast(&default_addr.s_addr)); - LOG(INFO) << "AFTER IP_MULTICAST_IF: default multicast addr=" << default_ip; - } -#endif // !_WIN32 -#endif // DEBUG_UDP - break; - } - case UdpSocket::Version::kV6: { - const auto index = static_cast(ifindex); - if (adb_setsockopt(fd_, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof(index)) == - -1) { - OnError(Error::Code::kSocketOptionSettingFailure); - PLOG(ERROR) << "adb_setsockopt() failed"; - return; - } - break; - } - } - - // TODO: remove once osp-discovery calls Bind() after SetMulticastOutboundInterface(). - MdnsBind(ifindex); - } - - // Joins to the multicast group at the given address, using the specified - // interface. - void JoinMulticastGroup(const IPAddress& address, NetworkInterfaceIndex ifindex) override { - if (!fd_.ok()) { - OnError(Error::Code::kSocketClosedFailure); - return; - } - - switch (local_endpoint_.address.version()) { - case UdpSocket::Version::kV4: { - // Passed as data to setsockopt(). 1 means return IP_PKTINFO control data - // in recvmsg() calls. - const int enable_pktinfo = 1; - if (adb_setsockopt(fd_, IPPROTO_IP, IP_PKTINFO, &enable_pktinfo, - sizeof(enable_pktinfo)) == -1) { - OnError(Error::Code::kSocketOptionSettingFailure); - LOG(ERROR) << "adb_setsockopt failed"; - return; - } - struct ip_mreq multicast_properties; - SetIPV4MulticastProperties(local_endpoint_.address, address, &multicast_properties); - if (adb_setsockopt(fd_, IPPROTO_IP, IP_ADD_MEMBERSHIP, &multicast_properties, - sizeof(multicast_properties)) == -1) { - OnError(Error::Code::kSocketOptionSettingFailure); - LOG(ERROR) << "adb_setsockopt failed"; - return; - } - return; - } - - case UdpSocket::Version::kV6: { - // Passed as data to setsockopt(). 1 means return IPV6_PKTINFO control - // data in recvmsg() calls. - const int enable_pktinfo = 1; -#ifdef _WIN32 - if (adb_setsockopt(fd_, IPPROTO_IPV6, IPV6_PKTINFO, &enable_pktinfo, - sizeof(enable_pktinfo)) == -1) { -#else - if (adb_setsockopt(fd_, IPPROTO_IPV6, IPV6_RECVPKTINFO, &enable_pktinfo, - sizeof(enable_pktinfo)) == -1) { -#endif - OnError(Error::Code::kSocketOptionSettingFailure); - LOG(ERROR) << "adb_setsockopt failed"; - return; - } - struct ipv6_mreq multicast_properties = { - .ipv6mr_multiaddr = {}, - .ipv6mr_interface = static_cast(ifindex), - }; - static_assert(sizeof(multicast_properties.ipv6mr_multiaddr) == 16u, - "IPv6 address requires exactly 16 bytes"); - address.CopyToV6( - reinterpret_cast(&multicast_properties.ipv6mr_multiaddr)); - // Portability note: All platforms support IPV6_JOIN_GROUP, which is - // synonymous with IPV6_ADD_MEMBERSHIP. - if (adb_setsockopt(fd_, IPPROTO_IPV6, IPV6_JOIN_GROUP, &multicast_properties, - sizeof(multicast_properties)) == -1) { - OnError(Error::Code::kSocketOptionSettingFailure); - LOG(ERROR) << "adb_setsockopt failed"; - return; - } - return; - } - } - } - - // Sends a message. If the message is not sent, Client::OnSendError() will be - // called to indicate this. Error::Code::kAgain indicates the operation would - // block, which can be expected during normal operation. - virtual void SendMessage(const void* data, size_t length, const IPEndpoint& dest) override { - if (!fd_.ok()) { - client_->OnSendError(this, Error::Code::kSocketClosedFailure); - return; - } - - adb_iovec iov; - iov.iov_len = length; - iov.iov_base = const_cast(data); - - adb_msghdr msg; - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - msg.msg_control = nullptr; - msg.msg_controllen = 0; - msg.msg_flags = 0; - - ssize_t num_bytes_sent = -2; - switch (local_endpoint_.address.version()) { - case UdpSocket::Version::kV4: { - struct sockaddr_in sa = {}; - sa.sin_family = AF_INET; - sa.sin_port = htons(dest.port); - dest.address.CopyToV4(reinterpret_cast(&sa.sin_addr.s_addr)); - msg.msg_name = &sa; - msg.msg_namelen = sizeof(sa); - num_bytes_sent = adb_sendmsg(fd_, &msg, 0); - break; - } - - case UdpSocket::Version::kV6: { - struct sockaddr_in6 sa = {}; - sa.sin6_family = AF_INET6; - sa.sin6_flowinfo = 0; - sa.sin6_scope_id = 0; - sa.sin6_port = htons(dest.port); - dest.address.CopyToV6(reinterpret_cast(&sa.sin6_addr.s6_addr)); - msg.msg_name = &sa; - msg.msg_namelen = sizeof(sa); - num_bytes_sent = adb_sendmsg(fd_, &msg, 0); - break; - } - } - - // Some VPN result in "short send" where less than the full datagram is reported sent. We - // shield ourselves from these and hypothetical "long send" and plain errors by reporting - // any unexpected return value. - if (num_bytes_sent != (ssize_t)length) { - LOG(WARNING) << "Error: sendmsg datagram size=" << length << " sent=" << num_bytes_sent; - client_->OnSendError(this, ChooseError(errno, Error::Code::kSocketSendFailure)); - return; - } - } - - // Sets the DSCP value to use for all messages sent from this socket. - void SetDscp(DscpMode state) override { -#ifdef _WIN32 - // TODO: this method doesn't seem to be used anywhere in the openscreen code, so - // ignoring implementation for now. - // Windows 10 seems to ignore setsockopt IP_TOS, so need to use Win APIs instead. - LOG(FATAL) << "NOT IMPLEMENTED"; -#else // !_WIN32 - if (!fd_.ok()) { - OnError(Error::Code::kSocketClosedFailure); - return; - } - - constexpr auto kSettingLevel = IPPROTO_IP; - uint8_t code_array[1] = {static_cast(state)}; - auto code = adb_setsockopt(fd_, kSettingLevel, IP_TOS, code_array, sizeof(uint8_t)); - - if (code == EBADF || code == ENOTSOCK || code == EFAULT) { - OnError(Error::Code::kSocketOptionSettingFailure); - LOG(WARNING) << "BAD SOCKET PROVIDED. CODE: " << code; - return; - } else if (code == EINVAL) { - OnError(Error::Code::kSocketOptionSettingFailure); - LOG(WARNING) << "INVALID DSCP INFO PROVIDED"; - return; - } else if (code == ENOPROTOOPT) { - OnError(Error::Code::kSocketOptionSettingFailure); - LOG(WARNING) << "INVALID DSCP SETTING LEVEL PROVIDED: " << kSettingLevel; - return; - } -#endif // _WIN32 - } - - private: - // TODO: Move back into public Bind() call once osp-discovery code calls Bind() after - // SetMulticastOutboundInterface(). - void MdnsBind(NetworkInterfaceIndex ifindex) { - if (!fd_.ok()) { - OnError(Error::Code::kSocketClosedFailure); - LOG(ERROR) << "Bind() failed. Socket is closed."; - return; - } - - // This is effectively a boolean passed to setsockopt() to allow a future - // bind() on the same socket to succeed, even if the address is already in - // use. This is pretty much universally the desired behavior. - const int reuse = 1; - if (adb_setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) { - OnError(Error::Code::kSocketOptionSettingFailure); - LOG(WARNING) << "Failed to set SO_REUSEADDR"; - return; - } - -#if defined(__APPLE__) - // On Mac, SO_REUSEADDR is not enough to allow a bind() on a reusable multicast socket. - // We need to also set the option SO_REUSEPORT. - if (adb_setsockopt(fd_, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse)) == -1) { - OnError(Error::Code::kSocketOptionSettingFailure); - LOG(WARNING) << "Failed to set SO_REUSEPORT"; - return; - } -#endif - - switch (local_endpoint_.address.version()) { - case UdpSocket::Version::kV4: { - struct sockaddr_in address = {}; - address.sin_family = AF_INET; - address.sin_port = htons(local_endpoint_.port); - // MUST bind to ADDR_ANY to send and receive multicast messages. - address.sin_addr.s_addr = INADDR_ANY; - if (adb_bind(fd_, reinterpret_cast(&address), sizeof(address)) == - -1) { - OnError(Error::Code::kSocketBindFailure); - PLOG(ERROR) << "adb_bind failed"; - return; - } - - // Get the resolved address/port - sockaddr_in sa; - socklen_t sa_len = sizeof(sa); - if (adb_getsockname(fd_, reinterpret_cast(&sa), &sa_len) != -1) { - local_endpoint_.address = GetIPAddressFromSockAddr(sa); - local_endpoint_.port = GetPortFromFromSockAddr(sa); - VLOG(MDNS) << "bind endpoint=" << local_endpoint_; - } - return; - } - case UdpSocket::Version::kV6: { - struct sockaddr_in6 address = {}; - address.sin6_family = AF_INET6; - address.sin6_flowinfo = 0; - address.sin6_port = htons(local_endpoint_.port); - // MUST bind to ADDR_ANY and scope_id unset to send and receive multicast messages. - address.sin6_addr = in6addr_any; - address.sin6_scope_id = 0; - if (adb_bind(fd_, reinterpret_cast(&address), sizeof(address)) == - -1) { - OnError(Error::Code::kSocketBindFailure); - PLOG(ERROR) << "adb_bind failed"; - return; - } - - // Get the resolved address/port - sockaddr_in6 sa; - socklen_t sa_len = sizeof(sa); - if (adb_getsockname(fd_, reinterpret_cast(&sa), &sa_len) != -1) { - local_endpoint_.address = GetIPAddressFromSockAddr(sa); - local_endpoint_.port = GetPortFromFromSockAddr(sa); - VLOG(MDNS) << "bind endpoint=" << local_endpoint_ - << " scope_id=" << sa.sin6_scope_id; - } - return; - } - default: - LOG(FATAL) << "Invalid domain"; - break; - } - } - - // Called by fdevent handler when data is available. - void ReceiveMessage() { - if (!fd_.ok()) { - client_->OnRead(this, Error::Code::kSocketClosedFailure); - return; - } - - const auto bytes_available = network_peek(fd_); - if (!bytes_available.has_value()) { - client_->OnRead(this, ChooseError(errno, Error::Code::kSocketReadFailure)); - return; - } - - UdpPacket packet(*bytes_available); - packet.set_socket(this); - Error result = Error::Code::kUnknownError; - switch (local_endpoint_.address.version()) { - case UdpSocket::Version::kV4: { - result = ReceiveMessageInternal(fd_, &packet); - break; - } - case UdpSocket::Version::kV6: { - result = ReceiveMessageInternal(fd_, &packet); - break; - } - default: { - LOG(FATAL) << "Invalid domain"; - break; - } - } - - client_->OnRead(this, result.ok() ? ErrorOr(std::move(packet)) - : ErrorOr(std::move(result))); - } - - void OnError(Error::Code error_code) { - // Close the socket unless the error code represents a transient condition. - if (error_code != Error::Code::kNone && error_code != Error::Code::kAgain) { - if (fde_) { - fdevent_destroy(fde_); - fde_ = nullptr; - } - } - - std::stringstream stream; - stream << "endpoint: " << local_endpoint_; - client_->OnError(this, Error(error_code, stream.str())); - } - - static void OnFdeventResult(int fd, unsigned ev, void* opaque) { - AdbUdpSocket* s = reinterpret_cast(opaque); - if (ev & FDE_READ) { - s->ReceiveMessage(); - } - } - - Client* const client_; - mutable IPEndpoint local_endpoint_; - unique_fd fd_; - fdevent* fde_ = nullptr; - std::optional mdns_ifindex_; -}; - -} // namespace - -// Implementation of openscreen's platform APIs for udp_socket.h -// static -ErrorOr> UdpSocket::Create(TaskRunner* task_runner, - UdpSocket::Client* client, - const IPEndpoint& local_endpoint) { - // |task_runner| is not used in adb's udp implementation because everything is going through the - // fdevent thread when we register the fd. - std::string err; - std::stringstream ip_addr_ss; - ip_addr_ss << local_endpoint.address; - - int domain; - switch (local_endpoint.address.version()) { - case Version::kV4: - domain = AF_INET; - break; - case Version::kV6: - domain = AF_INET6; - break; - default: - LOG(FATAL) << "Invalid domain"; - return Error::Code::kInitializationFailure; - } - - unique_fd fd(adb_socket(domain, SOCK_DGRAM, 0)); - if (!fd.ok()) { - PLOG(ERROR) << "Failed to create udp socket"; - return Error::Code::kInitializationFailure; - } - - if (!set_file_block_mode(fd, false)) { - PLOG(ERROR) << "Failed to set non-block mode on fd"; - return Error::Code::kInitializationFailure; - } - - VLOG(MDNS) << "UDP socket created for " << local_endpoint; - std::unique_ptr udp_socket(new AdbUdpSocket(client, local_endpoint, std::move(fd))); - return udp_socket; -} - -} // namespace openscreen diff --git a/base/cvd/adb/client/transport_emulator.cpp b/base/cvd/adb/client/transport_emulator.cpp index 3b747565b1b..83fb1e41d8f 100644 --- a/base/cvd/adb/client/transport_emulator.cpp +++ b/base/cvd/adb/client/transport_emulator.cpp @@ -40,6 +40,7 @@ #include "adb.h" #include "adb_io.h" +#include "adb_mdns.h" #include "adb_unique_fd.h" #include "adb_utils.h" #include "fdevent/fdevent.h" @@ -74,13 +75,18 @@ static std::unordered_map emulator_transports [[clang::no_destroy]] GUARDED_BY(emulator_transports_lock); bool connect_emulator(int port) { - fdevent_check_not_looper(); + CHECK_NOT_LOOPER_THREAD(); std::string dummy; return connect_emulator_arbitrary_ports(port - 1, port, &dummy) == 0; } +void init_socket_transport_tcp(atransport* t, unique_fd fd) { + auto fd_connection = std::make_unique(std::move(fd)); + t->SetConnection(std::make_unique(std::move(fd_connection))); +} + void connect_device(const std::string& address, std::string* response) { - fdevent_check_not_looper(); + CHECK_NOT_LOOPER_THREAD(); if (address.empty()) { *response = "empty address"; return; @@ -89,7 +95,7 @@ void connect_device(const std::string& address, std::string* response) { D("connection requested to '%s'", address.c_str()); unique_fd fd; int port; - std::string serial, prefix_addr; + std::string transport_name, prefix_addr; // If address does not match any socket type, it should default to TCP. if (address.starts_with("vsock:") || address.starts_with("localfilesystem:")) { @@ -98,44 +104,52 @@ void connect_device(const std::string& address, std::string* response) { prefix_addr = "tcp:" + address; } - socket_spec_connect(&fd, prefix_addr, &port, &serial, response); + socket_spec_connect(&fd, prefix_addr, &port, &transport_name, response); if (fd.get() == -1) { return; } auto reconnect = [prefix_addr](atransport* t) { + if (prefix_addr.ends_with(MDNS_SERVICE_PROTOCOL)) { + return ReconnectResult::Abort; + } std::string response; unique_fd fd; int port; std::string serial; socket_spec_connect(&fd, prefix_addr, &port, &serial, &response); if (fd == -1) { - D("reconnect failed: %s", response.c_str()); + LOG(INFO) << "reconnect " << serial << " failed: " << response; return ReconnectResult::Retry; } // This invokes the part of register_socket_transport() that needs to be // invoked if the atransport* has already been setup. This eventually // calls atransport->SetConnection() with a newly created Connection* // that will in turn send the CNXN packet. - return init_socket_transport(t, std::move(fd), port, false) >= 0 ? ReconnectResult::Success - : ReconnectResult::Retry; + init_socket_transport_tcp(t, std::move(fd)); + return ReconnectResult::Success; }; int error; - if (!register_socket_transport(std::move(fd), serial, port, false, std::move(reconnect), false, - &error)) { + if (!register_socket_transport(std::move(fd), transport_name, port, false, std::move(reconnect), + false, &error)) { if (error == EALREADY) { - *response = android::base::StringPrintf("already connected to %s", serial.c_str()); + *response = + android::base::StringPrintf("already connected to %s", transport_name.c_str()); } else if (error == EPERM) { - *response = android::base::StringPrintf("failed to authenticate to %s", serial.c_str()); + *response = android::base::StringPrintf("failed to authenticate to %s", + transport_name.c_str()); } else { - *response = android::base::StringPrintf("failed to connect to %s", serial.c_str()); + *response = + android::base::StringPrintf("failed to connect to %s", transport_name.c_str()); } } else { - *response = android::base::StringPrintf("connected to %s", serial.c_str()); + *response = android::base::StringPrintf("connected to %s", transport_name.c_str()); } } int connect_emulator_arbitrary_ports(int console_port, int adb_port, std::string* error) { + CHECK_NOT_LOOPER_THREAD(); + unique_fd fd; if (find_emulator_transport_by_adb_port(adb_port) != nullptr || @@ -282,28 +296,25 @@ std::string getEmulatorSerialString(int console_port) { return android::base::StringPrintf("emulator-%d", console_port); } -int init_socket_transport(atransport* t, unique_fd fd, int adb_port, bool is_emulator) { - int fail = 0; +int init_socket_transport_emulator(atransport* t, unique_fd fd, int adb_port) { + auto emulator_connection = std::make_unique(std::move(fd), adb_port); + t->SetConnection(std::make_unique(std::move(emulator_connection))); + std::lock_guard lock(emulator_transports_lock); + atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port); + if (existing_transport != nullptr) { + D("is_emulator transport for port %d already registered (%p)?", adb_port, + existing_transport); + return -1; + } + emulator_transports[adb_port] = t; - if (is_emulator) { - auto emulator_connection = std::make_unique(std::move(fd), adb_port); - t->SetConnection( - std::make_unique(std::move(emulator_connection))); - std::lock_guard lock(emulator_transports_lock); - atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port); - if (existing_transport != nullptr) { - D("is_emulator transport for port %d already registered (%p)?", adb_port, - existing_transport); - fail = -1; - } else { - emulator_transports[adb_port] = t; - } + return 0; +} - return fail; +int init_socket_transport(atransport* t, unique_fd fd, int adb_port, bool is_emulator) { + if (is_emulator) { + return init_socket_transport_emulator(t, std::move(fd), adb_port); } - - // Regular tcp connection. - auto fd_connection = std::make_unique(std::move(fd)); - t->SetConnection(std::make_unique(std::move(fd_connection))); - return fail; + init_socket_transport_tcp(t, std::move(fd)); + return 0; } diff --git a/base/cvd/adb/client/transport_mdns.cpp b/base/cvd/adb/client/transport_mdns.cpp index 515222ebc42..03eeaa9ef11 100644 --- a/base/cvd/adb/client/transport_mdns.cpp +++ b/base/cvd/adb/client/transport_mdns.cpp @@ -32,15 +32,6 @@ #include #include -#include -#include -#include -#include -#include -#include -#include -#include - #include "adb_client.h" #include "adb_mdns.h" #include "adb_trace.h" @@ -48,55 +39,18 @@ #include "adb_wifi.h" #include "client/discovered_services.h" #include "client/mdns_utils.h" -#include "client/openscreen/platform/task_runner.h" #include "fdevent/fdevent.h" #include "mdns_tracker.h" #include "sysdeps.h" namespace { -using namespace mdns; -using namespace openscreen; -using ServiceWatcher = discovery::DnsSdServiceWatcher; -using ServicesUpdatedState = ServiceWatcher::ServicesUpdatedState; - -struct DiscoveryState; -DiscoveryState* g_state = nullptr; - -class DiscoveryReportingClient : public discovery::ReportingClient { - public: - void OnFatalError(Error error) override { - LOG(ERROR) << "Encountered fatal discovery error: " << error; - got_fatal_ = true; - } - - void OnRecoverableError(Error error) override { - LOG(ERROR) << "Encountered recoverable discovery error: " << error; - } - - bool GotFatalError() const { return got_fatal_; } - - private: - std::atomic got_fatal_{false}; -}; - -struct DiscoveryState { - std::optional config; - SerialDeletePtr service; - std::unique_ptr reporting_client; - std::unique_ptr task_runner; - std::vector> watchers; - InterfaceInfo interface_info; -}; - static void RequestConnectToDevice(const ServiceInfo& info) { // Connecting to a device does not happen often. We spawn a new thread each time. // Let's re-evaluate if we need a thread-pool or a background thread if this ever becomes // a perf bottleneck. std::thread([=] { - VLOG(MDNS) << "Attempting to secure connect to instance=" << info.instance - << " service=" << info.service << " addr4=%s" << info.v4_address << ":" - << info.port; + VLOG(MDNS) << "Attempting to secure connect to instance '" << info << "'"; std::string response; connect_device(std::format("{}.{}", info.instance, info.service), &response); VLOG(MDNS) << std::format("secure connect to {} regtype {} ({}:{}) : {}", info.instance, @@ -104,117 +58,34 @@ static void RequestConnectToDevice(const ServiceInfo& info) { }).detach(); } -// Callback provided to service receiver for updates. -void OnServiceReceiverResult(std::vector>, - std::reference_wrapper info, - ServicesUpdatedState state) { - switch (state) { - case ServicesUpdatedState::EndpointCreated: { - discovered_services.ServiceCreated(info); - break; - } - case ServicesUpdatedState::EndpointUpdated: { - discovered_services.ServiceUpdated(info); - break; - } - case ServicesUpdatedState::EndpointDeleted: { - discovered_services.ServiceDeleted(info); - break; - } +void AttemptAutoConnect(const std::reference_wrapper info) { + if (!adb_DNSServiceShouldAutoConnect(info.get().service, info.get().instance)) { + VLOG(MDNS) << "Don't auto-connect to " << info.get().instance << "." << info.get().service; + return; } - update_mdns_trackers(); - - switch (state) { - case ServicesUpdatedState::EndpointCreated: - case ServicesUpdatedState::EndpointUpdated: - if (adb_DNSServiceShouldAutoConnect(info.get().service, info.get().instance) && - info.get().v4_address) { - auto index = adb_DNSServiceIndexByName(info.get().service); - if (!index) { - return; - } - - // Don't try to auto-connect if not in the keystore. - if (*index == kADBSecureConnectServiceRefIndex && - !adb_wifi_is_known_host(info.get().instance)) { - VLOG(MDNS) << "instance_name=" << info.get().instance << " not in keystore"; - return; - } - - RequestConnectToDevice(info.get()); - } - break; - default: - break; + if (!info.get().v4_address.has_value()) { + return; } -} - -std::optional GetConfigForAllInterfaces() { - auto interface_infos = GetNetworkInterfaces(); - - discovery::Config config; - // The host only consumes mDNS traffic. It doesn't publish anything. - // Avoid creating an mDNSResponder that will listen with authority - // to answer over no domain. - config.enable_publication = false; - - for (const auto& interface : interface_infos) { - if (interface.GetIpAddressV4() || interface.GetIpAddressV6()) { - config.network_info.push_back({interface}); - VLOG(MDNS) << "Listening on interface [" << interface << "]"; - } + const auto index = adb_DNSServiceIndexByName(info.get().service); + if (!index) { + VLOG(MDNS) << "Unknown service" << info.get().service; + return; } - if (config.network_info.empty()) { - VLOG(MDNS) << "No available network interfaces for mDNS discovery"; - return std::nullopt; + // Don't try to auto-connect if not in the keystore. + if (*index == kADBSecureConnectServiceRefIndex && + !known_wifi_hosts_file.IsKnownHost(info.get().instance)) { + VLOG(MDNS) << "instance_name=" << info.get().instance << " not in keystore"; + return; } - return config; -} - -void StartDiscovery() { - CHECK(!g_state); - g_state = new DiscoveryState(); - g_state->task_runner = std::make_unique(); - g_state->reporting_client = std::make_unique(); - - g_state->task_runner->PostTask([]() { - g_state->config = GetConfigForAllInterfaces(); - if (!g_state->config) { - VLOG(MDNS) << "No mDNS config. Aborting StartDiscovery()"; - return; - } - - VLOG(MDNS) << "Starting discovery on " << (*g_state->config).network_info.size() - << " interfaces"; - - g_state->service = discovery::CreateDnsSdService( - g_state->task_runner.get(), g_state->reporting_client.get(), *g_state->config); - // Register a receiver for each service type - for (int i = 0; i < kNumADBDNSServices; ++i) { - auto watcher = std::make_unique( - g_state->service.get(), kADBDNSServices[i], DnsSdInstanceEndpointToServiceInfo, - OnServiceReceiverResult); - watcher->StartDiscovery(); - g_state->watchers.push_back(std::move(watcher)); - - if (g_state->reporting_client->GotFatalError()) { - for (auto& w : g_state->watchers) { - if (w->is_running()) { - w->StopDiscovery(); - } - } - break; - } - } - }); + RequestConnectToDevice(info.get()); } bool ConnectAdbSecureDevice(const ServiceInfo& info) { - if (!adb_wifi_is_known_host(info.instance)) { + if (!known_wifi_hosts_file.IsKnownHost(info.instance)) { VLOG(MDNS) << "serviceName=" << info.instance << " not in keystore"; return false; } @@ -225,25 +96,44 @@ bool ConnectAdbSecureDevice(const ServiceInfo& info) { } // namespace +// Callback provided to service receiver for updates. +void OnServiceReceiverResult(const ServiceInfo& info, ServiceInfoState state) { + bool updated = true; + switch (state) { + case Created: { + VLOG(MDNS) << "mdns stack reported service created " << info; + mdns::discovered_services.ServiceCreated(info); + AttemptAutoConnect(info); + break; + } + case Updated: { + VLOG(MDNS) << "mdns stack reported service updated " << info; + updated = mdns::discovered_services.ServiceUpdated(info); + if (updated) { + AttemptAutoConnect(info); + } + break; + } + case Deleted: { + VLOG(MDNS) << "mdns stack reported service deleted " << info; + mdns::discovered_services.ServiceDeleted(info); + break; + } + } + + if (updated) { + update_mdns_trackers(); + } +} + ///////////////////////////////////////////////////////////////////////////////// void init_mdns_transport_discovery() { - const char* mdns_osp = getenv("ADB_MDNS_OPENSCREEN"); - if (mdns_osp && strcmp(mdns_osp, "0") == 0) { - LOG(WARNING) << "Environment variable ADB_MDNS_OPENSCREEN disregarded"; - } else { - VLOG(MDNS) << "Openscreen mdns discovery enabled"; - StartDiscovery(); - } + VLOG(MDNS) << "mdns discovery is disabled in this build"; } bool adb_secure_connect_by_service_name(const std::string& instance_name) { - if (!g_state || g_state->watchers.empty()) { - VLOG(MDNS) << "Mdns not enabled"; - return false; - } - - auto info = discovered_services.FindInstance(ADB_SERVICE_TLS, instance_name); + auto info = mdns::discovered_services.FindInstance(ADB_SERVICE_TLS, instance_name); if (info.has_value()) { return ConnectAdbSecureDevice(*info); } @@ -251,24 +141,16 @@ bool adb_secure_connect_by_service_name(const std::string& instance_name) { } std::string mdns_check() { - if (!g_state) { - return "ERROR: mdns discovery disabled"; - } - - return "mdns daemon version [Openscreen discovery 0.0.0]"; + return "ERROR: mdns discovery disabled"; } std::string mdns_list_discovered_services() { - if (!g_state || g_state->watchers.empty()) { - return ""; - } - std::string result; - auto cb = [&](const mdns::ServiceInfo& si) { + auto cb = [&](const ServiceInfo& si) { result += std::format("{}\t{}\t{}:{}\n", si.instance, si.service, si.v4_address_string(), si.port); }; - discovered_services.ForAllServices(cb); + mdns::discovered_services.ForAllServices(cb); return result; } @@ -283,7 +165,7 @@ std::optional mdns_get_connect_service_info(const std::string& name std::string fq_service = std::format("{}.{}", mdns_instance->service_name, mdns_instance->transport_type); - return discovered_services.FindInstance(fq_service, mdns_instance->instance_name); + return mdns::discovered_services.FindInstance(fq_service, mdns_instance->instance_name); } std::optional mdns_get_pairing_service_info(const std::string& name) { @@ -295,5 +177,5 @@ std::optional mdns_get_pairing_service_info(const std::string& name return {}; } - return discovered_services.FindInstance(ADB_SERVICE_PAIR, mdns_instance->instance_name); + return mdns::discovered_services.FindInstance(ADB_SERVICE_PAIR, mdns_instance->instance_name); } diff --git a/base/cvd/adb/adb_wifi.h b/base/cvd/adb/client/transport_mdns.h similarity index 57% rename from base/cvd/adb/adb_wifi.h rename to base/cvd/adb/client/transport_mdns.h index bc19d36cd0e..7f32697b61d 100644 --- a/base/cvd/adb/adb_wifi.h +++ b/base/cvd/adb/client/transport_mdns.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 The Android Open Source Project + * Copyright (C) 2025 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. @@ -19,20 +19,10 @@ #include #include -#include "adb.h" +#include "discovered_services.h" -#if ADB_HOST +// Whatever mdns engine is used, this is the sole entry point into ADB. +void OnServiceReceiverResult(const ServiceInfo& info, ServiceInfoState state); -void adb_wifi_pair_device(const std::string& host, const std::string& password, - std::string& response); -bool adb_wifi_is_known_host(const std::string& host); - -#else // !ADB_HOST - -struct AdbdAuthContext; - -void adbd_wifi_init(AdbdAuthContext* ctx); -void adbd_wifi_secure_connect(atransport* t); -void adbd_send_tls_server_port(uint16_t port); - -#endif +std::optional mdns_get_connect_service_info(const std::string& name); +std::optional mdns_get_pairing_service_info(const std::string& name); diff --git a/base/cvd/adb/client/transport_usb.cpp b/base/cvd/adb/client/transport_usb.cpp index 3b0b49064f8..d3b4929cd60 100644 --- a/base/cvd/adb/client/transport_usb.cpp +++ b/base/cvd/adb/client/transport_usb.cpp @@ -177,9 +177,9 @@ bool is_adb_interface(int usb_class, int usb_subclass, int usb_protocol) { } bool is_libusb_enabled() { - bool enable = true; -#if defined(_WIN32) - enable = false; + bool enable = false; +#if defined(__APPLE__) + enable = true; #endif char* env = getenv("ADB_LIBUSB"); if (env) { diff --git a/base/cvd/adb/client/usb_libusb.cpp b/base/cvd/adb/client/usb_libusb.cpp index 27499e0d2a9..34dc5c48856 100644 --- a/base/cvd/adb/client/usb_libusb.cpp +++ b/base/cvd/adb/client/usb_libusb.cpp @@ -65,7 +65,7 @@ void LibUsbConnection::HandleStop(const std::string& reason) { // layer. If a connection is detached it has merely been requested to stop transmitting and // release its resources. if (detached_) { - VLOG(USB) << "Not reporting error '" << reason << "' because device " << transport_->serial + VLOG(USB) << "Not reporting error '" << reason << "' because device " << transport_->name << " is detached"; } else { OnError(reason); diff --git a/base/cvd/adb/client/usb_libusb_device.cpp b/base/cvd/adb/client/usb_libusb_device.cpp index 49ca829d7e1..2bb270e78f4 100644 --- a/base/cvd/adb/client/usb_libusb_device.cpp +++ b/base/cvd/adb/client/usb_libusb_device.cpp @@ -203,10 +203,12 @@ bool LibUsbDevice::FindAdbInterface() { return false; } - if (device_desc->bDeviceClass != LIBUSB_CLASS_PER_INTERFACE) { - // Assume that all Android devices have the device class set to per interface. - // TODO: Is this assumption valid? - VLOG(USB) << "skipping device with incorrect class at " << device_address_; + if (device_desc->bDeviceClass != LIBUSB_CLASS_PER_INTERFACE && + device_desc->bDeviceClass != LIBUSB_CLASS_MISCELLANEOUS) { + // Assume that all Android devices have the device class set to per interface or + // miscellaneous. + VLOG(USB) << "skipping device with incorrect class '" << device_desc->bDeviceClass + << "' at " << device_address_; return false; } diff --git a/base/cvd/adb/client/usb_libusb_hotplug.cpp b/base/cvd/adb/client/usb_libusb_hotplug.cpp index 9619ea7b8d7..5efc4c00a74 100644 --- a/base/cvd/adb/client/usb_libusb_hotplug.cpp +++ b/base/cvd/adb/client/usb_libusb_hotplug.cpp @@ -222,7 +222,7 @@ static void usb_init_libusb_hotplug() { static_cast(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT), LIBUSB_HOTPLUG_ENUMERATE, LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY, - LIBUSB_CLASS_PER_INTERFACE, hotplug_callback, nullptr, nullptr); + LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, nullptr, nullptr); if (rc != LIBUSB_SUCCESS) { LOG(FATAL) << "failed to register libusb hotplug callback"; diff --git a/base/cvd/adb/client/usb_linux.cpp b/base/cvd/adb/client/usb_linux.cpp index 96d7a8a32ce..6e2148f3506 100644 --- a/base/cvd/adb/client/usb_linux.cpp +++ b/base/cvd/adb/client/usb_linux.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -31,8 +32,8 @@ #include #include #include -#include #include +#include #include #include @@ -49,7 +50,9 @@ #include #include "adb.h" +#include "adb_unique_fd.h" #include "transport.h" +#include "usb_linux_netlink.h" using namespace std::chrono_literals; using namespace std::literals; @@ -109,18 +112,6 @@ static int is_known_device(std::string_view dev_name) { return 0; } -static void kick_disconnected_devices() { - std::lock_guard lock(g_usb_handles_mutex); - // kick any devices in the device list that were not found in the device scan - for (usb_handle* usb : g_usb_handles) { - if (!usb->mark) { - usb_kick(usb); - } else { - usb->mark = false; - } - } -} - static inline bool contains_non_digit(const char* name) { while (*name) { if (!isdigit(*name++)) return true; @@ -128,188 +119,196 @@ static inline bool contains_non_digit(const char* name) { return false; } -static void find_usb_device(const std::string& base, - void (*register_device_callback)(const char*, const char*, - unsigned char, unsigned char, int, int, - unsigned, size_t)) { - std::unique_ptr bus_dir(opendir(base.c_str()), closedir); - if (!bus_dir) return; +// USB device processing can come from the initial scan but also from the netlink event, +// when that USB device has been plugged. The former is not a problem but there is a +// race condition with the later. udev will create an entry in /dev/bus/usb that is owned +// by root before being given permission to other users to read it. If we try to open too fast +// we will get a EACCES (permission denied) error. To solve this problem we retry a few time +// to open before giving up +static int unix_open_retry(const std::string& path, int flags) { + const int max_attempts = 3; + int fd = -1; - dirent* de; - while ((de = readdir(bus_dir.get())) != nullptr) { - if (contains_non_digit(de->d_name)) continue; + for (int i = 0; i < max_attempts; ++i) { + fd = unix_open(path, flags); - std::string bus_name = base + "/" + de->d_name; + if (fd != -1) { + VLOG(USB) << "USB device opened:'" << path << "' (" << (i + 1) << " attempts)"; + return fd; // Success + } - std::unique_ptr dev_dir(opendir(bus_name.c_str()), closedir); - if (!dev_dir) continue; + // Retry only if permission is denied (udev hasn't chmod'd yet) + if (errno == EACCES && i < max_attempts - 1) { + std::this_thread::sleep_for(200ms); + continue; + } - while ((de = readdir(dev_dir.get()))) { - unsigned char devdesc[4096]; - unsigned char* bufptr = devdesc; - unsigned char* bufend; - struct usb_device_descriptor* device; - struct usb_config_descriptor* config; - struct usb_interface_descriptor* interface; - struct usb_endpoint_descriptor *ep1, *ep2; - unsigned zero_mask = 0; - size_t max_packet_size = 0; + // If it's a different error (like ENOENT), don't bother retrying + break; + } - if (contains_non_digit(de->d_name)) continue; + return -1; +} - std::string dev_name = bus_name + "/" + de->d_name; - if (is_known_device(dev_name)) { - continue; - } +static void process_usb_device(const std::string& dev_name, + void (*register_device_callback)(const char*, const char*, + unsigned char, unsigned char, int, + int, unsigned, size_t)) { + VLOG(USB) << "USB device found: " << dev_name; + if (is_known_device(dev_name)) { + VLOG(USB) << "USB device already known: " << dev_name; + return; + } - int fd = unix_open(dev_name, O_RDONLY | O_CLOEXEC); - if (fd == -1) { - continue; - } + int fd = unix_open_retry(dev_name, O_RDONLY | O_CLOEXEC); + if (fd == -1) { + VLOG(USB) << "Failed to open usb device: " << dev_name << ": " << strerror(errno); + return; + } - size_t desclength = unix_read(fd, devdesc, sizeof(devdesc)); - bufend = bufptr + desclength; + unsigned char devdesc[4096]; + size_t desclength = unix_read(fd, devdesc, sizeof(devdesc)); + unsigned char* bufptr = devdesc; + unsigned char* bufend = bufptr + desclength; - // should have device and configuration descriptors, and atleast two endpoints - if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) { - D("desclength %zu is too small", desclength); - unix_close(fd); - continue; - } + bufend = bufptr + desclength; - device = (struct usb_device_descriptor*)bufptr; - bufptr += USB_DT_DEVICE_SIZE; + // should have device and configuration descriptors, and at least two endpoints + if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) { + VLOG(USB) << "USB device descriptor too small"; + unix_close(fd); + return; + } - if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) { - unix_close(fd); - continue; - } + struct usb_device_descriptor* device = (struct usb_device_descriptor*)bufptr; + bufptr += USB_DT_DEVICE_SIZE; - DBGX("[ %s is V:%04x P:%04x ]\n", dev_name.c_str(), device->idVendor, - device->idProduct); + if ((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) { + unix_close(fd); + return; + } - // should have config descriptor next - config = (struct usb_config_descriptor *)bufptr; - bufptr += USB_DT_CONFIG_SIZE; - if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) { - D("usb_config_descriptor not found"); - unix_close(fd); - continue; - } + DBGX("[ %s is V:%04x P:%04x ]\n", dev_name.c_str(), device->idVendor, device->idProduct); - // loop through all the descriptors and look for the ADB interface - while (bufptr < bufend) { - unsigned char length = bufptr[0]; - unsigned char type = bufptr[1]; + // should have config descriptor next + struct usb_config_descriptor* config = (struct usb_config_descriptor*)bufptr; + bufptr += USB_DT_CONFIG_SIZE; + if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) { + VLOG(USB) << "usb_config_descriptor not found"; + unix_close(fd); + return; + } - if (type == USB_DT_INTERFACE) { - interface = (struct usb_interface_descriptor *)bufptr; - bufptr += length; + // loop through all the descriptors and look for the ADB interface + while (bufptr < bufend) { + unsigned char length = bufptr[0]; + unsigned char type = bufptr[1]; - if (length != USB_DT_INTERFACE_SIZE) { - D("interface descriptor has wrong size"); - break; - } + if (type == USB_DT_INTERFACE) { + struct usb_interface_descriptor* interface = (struct usb_interface_descriptor*)bufptr; + bufptr += length; - DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d," - "bInterfaceProtocol: %d, bNumEndpoints: %d\n", - interface->bInterfaceClass, interface->bInterfaceSubClass, - interface->bInterfaceProtocol, interface->bNumEndpoints); - - if (interface->bNumEndpoints == 2 && - is_adb_interface(interface->bInterfaceClass, interface->bInterfaceSubClass, - interface->bInterfaceProtocol)) { - struct stat st; - char pathbuf[128]; - char link[256]; - char *devpath = nullptr; - - DBGX("looking for bulk endpoints\n"); - // looks like ADB... - ep1 = (struct usb_endpoint_descriptor *)bufptr; - bufptr += USB_DT_ENDPOINT_SIZE; - // For USB 3.0 SuperSpeed devices, skip potential - // USB 3.0 SuperSpeed Endpoint Companion descriptor - if (bufptr+2 <= devdesc + desclength && - bufptr[0] == USB_DT_SS_EP_COMP_SIZE && - bufptr[1] == USB_DT_SS_ENDPOINT_COMP) { - bufptr += USB_DT_SS_EP_COMP_SIZE; - } - ep2 = (struct usb_endpoint_descriptor *)bufptr; - bufptr += USB_DT_ENDPOINT_SIZE; - if (bufptr+2 <= devdesc + desclength && - bufptr[0] == USB_DT_SS_EP_COMP_SIZE && - bufptr[1] == USB_DT_SS_ENDPOINT_COMP) { - bufptr += USB_DT_SS_EP_COMP_SIZE; - } + if (length != USB_DT_INTERFACE_SIZE) { + D("interface descriptor has wrong size"); + break; + } - if (bufptr > devdesc + desclength || - ep1->bLength != USB_DT_ENDPOINT_SIZE || - ep1->bDescriptorType != USB_DT_ENDPOINT || - ep2->bLength != USB_DT_ENDPOINT_SIZE || - ep2->bDescriptorType != USB_DT_ENDPOINT) { - D("endpoints not found"); - break; - } + DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d," + "bInterfaceProtocol: %d, bNumEndpoints: %d\n", + interface->bInterfaceClass, interface->bInterfaceSubClass, + interface->bInterfaceProtocol, interface->bNumEndpoints); + + if (interface->bNumEndpoints == 2 && + is_adb_interface(interface->bInterfaceClass, interface->bInterfaceSubClass, + interface->bInterfaceProtocol)) { + struct stat st; + char pathbuf[128]; + char link[256]; + char* devpath = nullptr; + + DBGX("looking for bulk endpoints\n"); + // looks like ADB... + struct usb_endpoint_descriptor* ep1 = (struct usb_endpoint_descriptor*)bufptr; + bufptr += USB_DT_ENDPOINT_SIZE; + // For USB 3.0 SuperSpeed devices, skip potential + // USB 3.0 SuperSpeed Endpoint Companion descriptor + if (bufptr + 2 <= devdesc + desclength && bufptr[0] == USB_DT_SS_EP_COMP_SIZE && + bufptr[1] == USB_DT_SS_ENDPOINT_COMP) { + bufptr += USB_DT_SS_EP_COMP_SIZE; + } + struct usb_endpoint_descriptor* ep2 = (struct usb_endpoint_descriptor*)bufptr; + bufptr += USB_DT_ENDPOINT_SIZE; + if (bufptr + 2 <= devdesc + desclength && bufptr[0] == USB_DT_SS_EP_COMP_SIZE && + bufptr[1] == USB_DT_SS_ENDPOINT_COMP) { + bufptr += USB_DT_SS_EP_COMP_SIZE; + } - // both endpoints should be bulk - if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK || - ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) { - D("bulk endpoints not found"); - continue; - } - /* aproto 01 needs 0 termination */ - if (interface->bInterfaceProtocol == ADB_PROTOCOL) { - max_packet_size = ep1->wMaxPacketSize; - zero_mask = ep1->wMaxPacketSize - 1; - } + if (bufptr > devdesc + desclength || ep1->bLength != USB_DT_ENDPOINT_SIZE || + ep1->bDescriptorType != USB_DT_ENDPOINT || + ep2->bLength != USB_DT_ENDPOINT_SIZE || + ep2->bDescriptorType != USB_DT_ENDPOINT) { + D("endpoints not found"); + break; + } - // we have a match. now we just need to figure out which is in and which is out. - unsigned char local_ep_in, local_ep_out; - if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) { - local_ep_in = ep1->bEndpointAddress; - local_ep_out = ep2->bEndpointAddress; - } else { - local_ep_in = ep2->bEndpointAddress; - local_ep_out = ep1->bEndpointAddress; - } + // both endpoints should be bulk + if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK || + ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) { + D("bulk endpoints not found"); + continue; + } + /* aproto 01 needs 0 termination */ + size_t max_packet_size = 0; + unsigned zero_mask = 0; - // Determine the device path - if (!fstat(fd, &st) && S_ISCHR(st.st_mode)) { - snprintf(pathbuf, sizeof(pathbuf), "/sys/dev/char/%d:%d", - major(st.st_rdev), minor(st.st_rdev)); - ssize_t link_len = readlink(pathbuf, link, sizeof(link) - 1); - if (link_len > 0) { - link[link_len] = '\0'; - const char* slash = strrchr(link, '/'); - if (slash) { - snprintf(pathbuf, sizeof(pathbuf), - "usb:%s", slash + 1); - devpath = pathbuf; - } - } - } + if (interface->bInterfaceProtocol == ADB_PROTOCOL) { + max_packet_size = ep1->wMaxPacketSize; + zero_mask = ep1->wMaxPacketSize - 1; + } - register_device_callback(dev_name.c_str(), devpath, local_ep_in, - local_ep_out, interface->bInterfaceNumber, - device->iSerialNumber, zero_mask, max_packet_size); - break; - } - } else if (!length) { - // Specific Corsair USB hubs seen in the wild report a zero-length - // descriptor (resulting in an uninitialized descriptor - // type: https://issuetracker.google.com/302212871). - // If we don't give up here, we'll never make progress. - D("interface descriptor has 0 length. type: %d", type); - break; + // we have a match. now we just need to figure out which is in and which is out. + unsigned char local_ep_in, local_ep_out; + if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) { + local_ep_in = ep1->bEndpointAddress; + local_ep_out = ep2->bEndpointAddress; } else { - bufptr += length; + local_ep_in = ep2->bEndpointAddress; + local_ep_out = ep1->bEndpointAddress; } - } // end of while - unix_close(fd); + // Determine the device path + if (!fstat(fd, &st) && S_ISCHR(st.st_mode)) { + snprintf(pathbuf, sizeof(pathbuf), "/sys/dev/char/%d:%d", major(st.st_rdev), + minor(st.st_rdev)); + ssize_t link_len = readlink(pathbuf, link, sizeof(link) - 1); + if (link_len > 0) { + link[link_len] = '\0'; + const char* slash = strrchr(link, '/'); + if (slash) { + snprintf(pathbuf, sizeof(pathbuf), "usb:%s", slash + 1); + devpath = pathbuf; + } + } + } + + register_device_callback(dev_name.c_str(), devpath, local_ep_in, local_ep_out, + interface->bInterfaceNumber, device->iSerialNumber, + zero_mask, max_packet_size); + break; + } + } else if (!length) { + // Specific Corsair USB hubs seen in the wild report a zero-length + // descriptor (resulting in an uninitialized descriptor + // type: https://issuetracker.google.com/302212871). + // If we don't give up here, we'll never make progress. + D("interface descriptor has 0 length. type: %d", type); + break; + } else { + bufptr += length; } - } + } // end of while + + unix_close(fd); } static int usb_bulk_write(usb_handle* h, const void* data, int len) { @@ -619,16 +618,104 @@ static void register_device(const char* dev_name, const char* dev_path, unsigned register_usb_transport(done_usb, serial.c_str(), dev_path, done_usb->writeable); } +// Perform one manual scan of /dev/bus/usb to find devices already plugged in +static void scan_usb_devices(void (*register_device_callback)(const char*, const char*, + unsigned char, unsigned char, int, + int, unsigned, size_t)) { + VLOG(USB) << "Scanning for USB devices..."; + std::string base = "/dev/bus/usb"; + std::unique_ptr bus_dir(opendir(base.c_str()), closedir); + if (!bus_dir) { + LOG(WARNING) << "Failed to open bus directory '" << base << "': " << strerror(errno); + return; + } + + dirent* de; + while ((de = readdir(bus_dir.get())) != nullptr) { + if (contains_non_digit(de->d_name)) { + continue; + } + std::string bus_name = base + "/" + de->d_name; + std::unique_ptr dev_dir(opendir(bus_name.c_str()), closedir); + if (!dev_dir) { + continue; + } + dirent* de2; + while ((de2 = readdir(dev_dir.get()))) { + if (contains_non_digit(de2->d_name)) { + continue; + } + process_usb_device(bus_name + "/" + de2->d_name, register_device_callback); + } + } +} + static void device_poll_thread() { adb_thread_setname("device poll"); - D("Created device thread"); + VLOG(USB) << "USB device poll thread started"; + + // Initial Scan + scan_usb_devices(register_device); + adb_notify_device_scan_complete(); + + // Set up netlink to get add/remove events + unique_fd fd{socket(AF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT)}; + if (fd == -1) { + LOG(WARNING) << "Failed to open netlink socket: " << strerror(errno); + return; + } + + sockaddr_nl sa = {.nl_family = AF_NETLINK, .nl_groups = 1}; + if (bind(fd.get(), (sockaddr*)&sa, sizeof(sa)) < 0) { + LOG(WARNING) << "Failed to bind netlink socket: " << strerror(errno); + return; + } + + char buffer[4096]; while (true) { - // TODO: Use inotify. - find_usb_device("/dev/bus/usb", register_device); - adb_notify_device_scan_complete(); - kick_disconnected_devices(); - std::this_thread::sleep_for(1s); + ssize_t len = TEMP_FAILURE_RETRY(recv(fd.get(), buffer, sizeof(buffer) - 1, 0)); + if (len < 0) { + LOG(ERROR) << "Netlink socket recv has failed: " << strerror(errno); + break; + } + NetlinkMessage msg(buffer, len); + + if (!msg.has_attr("SUBSYSTEM", "usb")) { + LOG(WARNING) << "Netlink: SUBSYSTEM not found"; + continue; + } + + // We also receive "add" for "usb_interface" but we only look for whole device. + if (!msg.has_attr("DEVTYPE", "usb_device")) { + LOG(WARNING) << "Netlink: DEVTYPE not found"; + continue; + } + + std::string dev_name = "/dev/" + msg.attr("DEVNAME"); + if (dev_name.empty()) { + LOG(WARNING) << "Netlink: usb_device DEVNAME not found"; + continue; + } + + if (msg.has_attr("ACTION", "add")) { + VLOG(USB) << "Netlink: USB Device Added: " << dev_name; + process_usb_device(dev_name, register_device); + continue; + } + + if (msg.has_attr("ACTION", "remove")) { + VLOG(USB) << "Netlink: USB Device Removed: " << dev_name; + std::lock_guard lock(g_usb_handles_mutex); + for (usb_handle* usb : g_usb_handles) { + if (usb->path == dev_name) { + usb_kick(usb); + break; + } + } + continue; + } } + VLOG(USB) << "USB device poll thread stopped"; } void usb_init() { diff --git a/base/cvd/adb/client/usb_linux_netlink.cpp b/base/cvd/adb/client/usb_linux_netlink.cpp new file mode 100644 index 00000000000..5cfae270c19 --- /dev/null +++ b/base/cvd/adb/client/usb_linux_netlink.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2026 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. + */ + +#include "usb_linux_netlink.h" + +#include "adb_trace.h" + +NetlinkMessage::NetlinkMessage(const char* buffer, ssize_t len) { + VLOG(USB) << "\nNetlink: NEW MESSAGE"; + if (len <= 0) { + return; + } + + const char* current_ptr = buffer; + const char* const end_ptr = buffer + len; + + while (current_ptr < end_ptr) { + std::string_view entry(current_ptr); + + current_ptr += entry.size() + 1; + + // Skip if no '=' delimiter is found + size_t sep = entry.find('='); + if (sep == std::string_view::npos) { + continue; + } + + std::string key(entry.substr(0, sep)); + std::string value(entry.substr(sep + 1)); + + if (key.empty()) { + continue; + } + + VLOG(USB) << "Netlink: '" << key << "' = '" << value << "'"; + attrs_[std::move(key)] = std::move(value); + } +} + +std::string NetlinkMessage::attr(const std::string& key) const { + auto it = attrs_.find(key); + return it != attrs_.end() ? it->second : ""; +} + +bool NetlinkMessage::has_attr(const std::string& key, std::string_view value) const { + auto it = attrs_.find(key); + return it != attrs_.end() && it->second == value; +} \ No newline at end of file diff --git a/base/cvd/adb/client/usb_linux_netlink.h b/base/cvd/adb/client/usb_linux_netlink.h new file mode 100644 index 00000000000..a2f6a4d7f9a --- /dev/null +++ b/base/cvd/adb/client/usb_linux_netlink.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2026 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. + */ + +#pragma once + +#include +#include +#include + +#include + +// Helper class to parse netlink messages. + +// A message is a sequence of null-terminated strings as follows. +// add@/devices/pci0000:00/0000:00:03.1/0000:02:00.0/0000:03:08.0/0000:04:00.1/usb4/4-1 +// ACTION=add +// DEVPATH=/devices/pci0000:00/0000:00:03.1/0000:02:00.0/0000:03:08.0/0000:04:00.1/usb4/4-1 +// SUBSYSTEM=usb +// MAJOR=189 +// MINOR=509 +// DEVNAME=bus/usb/004/126 +// DEVTYPE=usb_device +// PRODUCT=18d1/4ee7/601 +// TYPE=0/0/0 +// BUSNUM=004 +// DEVNUM=126 +// SEQNUM=10453 +// +// This class only capture key/value pairs, the "header@" is ignored. +class NetlinkMessage { + public: + NetlinkMessage(const char* buffer, ssize_t len); + + // Return the value for the given key. If the key is not found, return an empty string. + std::string attr(const std::string& key) const; + + // Return true if the message has the given attribute with the given value. + bool has_attr(const std::string& key, std::string_view value) const; + + private: + std::unordered_map attrs_; +}; \ No newline at end of file diff --git a/base/cvd/adb/fdevent/fdevent.cpp b/base/cvd/adb/fdevent/fdevent.cpp index a5eb20cccce..6ae120b89cd 100644 --- a/base/cvd/adb/fdevent/fdevent.cpp +++ b/base/cvd/adb/fdevent/fdevent.cpp @@ -186,12 +186,6 @@ void fdevent_context::CheckLooperThread() const { } } -void fdevent_context::CheckNotLooperThread() const { - if (looper_thread_id_) { - CHECK_NE(*looper_thread_id_, android::base::GetThreadId()); - } -} - void fdevent_context::Run(std::function fn) { { std::lock_guard lock(run_queue_mutex_); @@ -219,7 +213,7 @@ static auto& g_ambient_fdevent_context() { return context; } -static fdevent_context* fdevent_get_ambient() { +fdevent_context* fdevent_get_ambient() { return g_ambient_fdevent_context(); } @@ -265,14 +259,6 @@ void fdevent_loop() { fdevent_get_ambient()->Loop(); } -void fdevent_check_looper() { - fdevent_get_ambient()->CheckLooperThread(); -} - -void fdevent_check_not_looper() { - fdevent_get_ambient()->CheckNotLooperThread(); -} - void fdevent_terminate_loop() { fdevent_get_ambient()->TerminateLoop(); } diff --git a/base/cvd/adb/fdevent/fdevent.h b/base/cvd/adb/fdevent/fdevent.h index cb5e010257e..61d56988447 100644 --- a/base/cvd/adb/fdevent/fdevent.h +++ b/base/cvd/adb/fdevent/fdevent.h @@ -31,6 +31,7 @@ #include #include +#include #include "adb_unique_fd.h" @@ -109,14 +110,6 @@ struct fdevent_context { // terminate_loop_ to determine whether to stop. virtual void Loop() = 0; - // Assert that the caller is executing in the context of the execution - // thread that invoked Loop(). - void CheckLooperThread() const; - - // Assert that the caller is NOT executing in the context of the execution - // thread that invoked Loop(). - void CheckNotLooperThread() const; - // Queue an operation to be run on the looper thread. void Run(std::function fn); @@ -124,11 +117,16 @@ struct fdevent_context { void TerminateLoop(); virtual size_t InstalledCount() = 0; + std::optional looper_thread_id_ = std::nullopt; + protected: // Interrupt the run loop. virtual void Interrupt() = 0; - std::optional looper_thread_id_ = std::nullopt; + // Assert that the caller is executing in the context of the execution + // thread that invoked Loop(). + void CheckLooperThread() const; + std::atomic terminate_loop_ = false; std::map installed_fdevents_; @@ -154,12 +152,6 @@ void fdevent_del(fdevent *fde, unsigned events); void fdevent_set_timeout(fdevent* fde, std::optional timeout); void fdevent_loop(); -// Delegates to the member function that checks for the initialization -// of Loop() so that fdevent_context requests can be serially processed -// by the global instance robustly. -void fdevent_check_looper(); -void fdevent_check_not_looper(); - // Queue an operation to run on the looper event thread. void fdevent_run_on_looper(std::function fn); @@ -168,4 +160,20 @@ void fdevent_terminate_loop(); size_t fdevent_installed_count(); void fdevent_reset(); +fdevent_context* fdevent_get_ambient(); + +// We use macro so _FILE_ and _FUNCTION_ get the proper value if the check fails. + +// Assert that the caller is executing in the context of the execution +// thread that invoked Loop(). +#define CHECK_LOOPER_THREAD() \ + if (fdevent_get_ambient()->looper_thread_id_) \ + CHECK_EQ(*fdevent_get_ambient()->looper_thread_id_, android::base::GetThreadId()) + +// Assert that the caller is NOT executing in the context of the execution +// thread that invoked Loop(). +#define CHECK_NOT_LOOPER_THREAD() \ + if (fdevent_get_ambient()->looper_thread_id_) \ + CHECK_NE(*fdevent_get_ambient()->looper_thread_id_, android::base::GetThreadId()) + #endif diff --git a/base/cvd/adb/incfs/util/map_ptr.h b/base/cvd/adb/incfs/util/map_ptr.h deleted file mode 100644 index 9a6aebf2932..00000000000 --- a/base/cvd/adb/incfs/util/map_ptr.h +++ /dev/null @@ -1,413 +0,0 @@ -/* - * Copyright (C) 2020 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. - */ - -#pragma once - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#ifdef __ANDROID__ -#include -#endif - -namespace android { - -class FileMap; - -namespace incfs { - -// Controls whether not verifying the presence of data before de-referencing the pointer aborts -// program execution. -#define LIBINCFS_MAP_PTR_DEBUG false -#if LIBINCFS_MAP_PTR_DEBUG -#define LIBINCFS_MAP_PTR_DEBUG_CODE(x) x -#else -#define LIBINCFS_MAP_PTR_DEBUG_CODE(x) -#endif - -template -struct map_ptr; - -// This class represents a memory-mapped, read-only file that may exist on an IncFs file system. -// -// Files stored on IncFs may not be fully present. This class is able to return a smart pointer -// (map_ptr) that is able to verify whether the contents of the pointer are fully present on -// IncFs. -// -// This always uses MAP_SHARED. -class IncFsFileMap final { -public: - IncFsFileMap() noexcept; - IncFsFileMap(IncFsFileMap&&) noexcept; - IncFsFileMap& operator =(IncFsFileMap&&) noexcept; - ~IncFsFileMap() noexcept; - - // Initializes the map. Does not take ownership of the file descriptor. - // Returns whether or not the file was able to be memory-mapped. - bool Create(int fd, off64_t offset, size_t length, const char* file_name); - - // Same thing, but allows verification to be disabled when `verify` is `false`, and enabled when - // `verify` is true and the file resides on IncFs. - bool Create(int fd, off64_t offset, size_t length, const char* file_name, bool verify); - - // Same thing, but allows verification to be disabled when `verify` is `false`, and enabled when - // `verify` is true regardless of whether the file resides on IncFs (used for benchmarks and - // testing). - bool CreateForceVerification(int fd, off64_t offset, size_t length, const char* file_name, - bool verify); - - template - map_ptr data() const { - return map_ptr(verification_enabled_ ? this : nullptr, - reinterpret_cast(unsafe_data())); - } - - const void* unsafe_data() const; - size_t length() const; - off64_t offset() const; - const char* file_name() const; - -public: - // Returns whether the data range is entirely present on IncFs. - bool Verify(const uint8_t* const& data_start, const uint8_t* const& data_end, - const uint8_t** prev_verified_block) const; - -private: - DISALLOW_COPY_AND_ASSIGN(IncFsFileMap); - - using bucket_t = uint8_t; - static constexpr size_t kBucketBits = sizeof(bucket_t) * 8U; - - // File descriptor of the memory-mapped file (not owned). - int fd_ = -1; - bool verification_enabled_ = false; - size_t start_block_offset_ = 0; - const uint8_t* start_block_ptr_ = nullptr; - - std::unique_ptr map_; - - // Bitwise cache for storing whether a block has already been verified. This cache relies on - // IncFs not deleting blocks of a file that is currently memory mapped. - mutable std::vector> loaded_blocks_; - - template - friend struct map_ptr; -}; - -// Variant of map_ptr that statically guarantees that the pointed to data is fully present and -// reading data will not result in IncFs raising a SIGBUS. -template -using verified_map_ptr = map_ptr; - -// Smart pointer that is able to verify whether the contents of the pointer are fully present on -// the file system before using the pointer. Files residing on IncFs may not be fully present. -// -// Before attempting to use the data represented by the smart pointer, the caller should always use -// the bool operator to verify the presence of the data. The bool operator is not thread-safe. If -// this pointer must be used in multiple threads concurrently, use verified_map_ptr instead. -// -// map_ptr created from raw pointers have less overhead than when created from IncFsFileMap. -template -struct map_ptr final { -private: - friend class IncFsFileMap; - - // To access internals of map_ptr with a different type - template - friend struct map_ptr; - - template - using IsVoid = typename std::enable_if_t::value, int>; - - template - using NotVoid = typename std::enable_if_t::value, int>; - - template - using IsVerified = typename std::enable_if_t; - - template - using IsUnverified = typename std::enable_if_t; - -public: - class const_iterator final { - public: - friend struct map_ptr; - using iterator_category = std::random_access_iterator_tag; - using value_type = const map_ptr; - using difference_type = std::ptrdiff_t; - using pointer = void; - using reference = value_type; - - const_iterator() = default; - const_iterator(const const_iterator& it) = default; - - bool operator==(const const_iterator& other) const { return safe_ptr_ == other.safe_ptr_; } - bool operator!=(const const_iterator& other) const { return safe_ptr_ != other.safe_ptr_; } - std::ptrdiff_t operator-(const const_iterator& other) const { - return safe_ptr_ - other.safe_ptr_; - } - - const_iterator operator+(int n) const { - const_iterator other = *this; - other += n; - return other; - } - - reference operator*() const { return safe_ptr_; } - - const const_iterator& operator++() { - safe_ptr_++; - return *this; - } - - const_iterator& operator+=(int n) { - safe_ptr_ = safe_ptr_ + n; - return *this; - } - - const const_iterator operator++(int) { - const_iterator temp(*this); - safe_ptr_++; - return temp; - } - - private: - explicit const_iterator(const map_ptr& ptr) : safe_ptr_(ptr) {} - map_ptr safe_ptr_; - }; - - // Default constructor - map_ptr() = default; - - // Implicit conversion from raw pointer - map_ptr(const T* ptr) : map_ptr(nullptr, ptr, nullptr) {} - - // Copy constructor - map_ptr(const map_ptr& other) = default; - - // Implicit copy conversion from verified to unverified map_ptr - template = 0, IsVerified = 0> - map_ptr(const map_ptr& other) : map_ptr(other.map_, other.ptr_, other.verified_block_) {} - - // Move constructor - map_ptr(map_ptr&& other) noexcept = default; - - // Implicit move conversion from verified to unverified map_ptr - template = 0, IsVerified = 0> - map_ptr(map_ptr&& other) : map_ptr(other.map_, other.ptr_, other.verified_block_) {} - - // Implicit conversion to unverified map_ptr - template = 0, - NotVoid = 0, IsUnverified = 0> - map_ptr(const map_ptr& other) - : map_ptr(other.map_, reinterpret_cast(other.ptr_), other.verified_block_) {} - - // Implicit conversion from regular raw pointer - map_ptr& operator=(const T* ptr) { - ptr_ = ptr; - map_ = nullptr; - verified_block_ = nullptr; - LIBINCFS_MAP_PTR_DEBUG_CODE(verified_ = Verified); - return *this; - } - - // Copy assignment operator - map_ptr& operator=(const map_ptr& other) = default; - - // Copy assignment operator - template = 0, IsVerified = 0> - map_ptr& operator=(const map_ptr& other) { - ptr_ = other.ptr_; - map_ = other.map_; - verified_block_ = other.verified_block_; - LIBINCFS_MAP_PTR_DEBUG_CODE(verified_ = other.verified_); - return *this; - } - - template - bool operator==(const map_ptr& other) const { - return ptr_ == other.ptr_; - } - - template - bool operator!=(const map_ptr& other) const { - return ptr_ != other.ptr_; - } - - template - bool operator<(const map_ptr& other) const { - return ptr_ < other.ptr_; - } - - template - std::ptrdiff_t operator-(const map_ptr& other) const { - return ptr_ - other.ptr_; - } - - template - map_ptr convert() const { - return map_ptr(map_, reinterpret_cast(ptr_), verified_block_); - } - - // Retrieves a map_ptr offset from an original map_ptr by the specified number of `offset` - // bytes. - map_ptr offset(std::ptrdiff_t offset) const { - return map_ptr(map_, - reinterpret_cast(reinterpret_cast(ptr_) + - offset), - verified_block_); - } - - // Returns a raw pointer to the value of this pointer. - const T* unsafe_ptr() const { return ptr_; } - - // Start T == void methods - - template = 0> - operator bool() const { - return ptr_ != nullptr; - } - - // End T == void methods - // Start T != void methods - - template = 0, bool V1 = Verified, IsUnverified = 0> - operator bool() const { - return verify(); - } - - template = 0, bool V1 = Verified, IsVerified = 0> - operator bool() const { - return ptr_ != nullptr; - } - - template = 0> - const_iterator iterator() const { - return const_iterator(*this); - } - - template = 0> - const map_ptr& operator++() { - LIBINCFS_MAP_PTR_DEBUG_CODE(verified_ = false); - ++ptr_; - return *this; - } - - template = 0> - const map_ptr operator++(int) { - map_ptr temp = *this; - LIBINCFS_MAP_PTR_DEBUG_CODE(verified_ = false); - ++ptr_; - return temp; - } - - template = 0> - map_ptr operator+(const S n) const { - return map_ptr(map_, ptr_ + n, verified_block_); - } - - template = 0> - map_ptr operator-(const S n) const { - return map_ptr(map_, ptr_ - n, verified_block_); - } - - // Returns the value of the pointer. - // The caller should verify the presence of the pointer data before calling this method. - template = 0> - const T1& value() const { - LIBINCFS_MAP_PTR_DEBUG_CODE( - CHECK(verified_) << "Did not verify presence before de-referencing safe pointer"); - return *ptr_; - } - - // Returns a raw pointer to the value this pointer. - // The caller should verify the presence of the pointer data before calling this method. - template = 0> - const T1* operator->() const { - LIBINCFS_MAP_PTR_DEBUG_CODE( - CHECK(verified_) << "Did not verify presence before de-referencing safe pointer"); - return ptr_; - } - - // Verifies the presence of `n` elements of `T`. - // - // Returns true if the elements are completely present; otherwise, returns false. - template = 0, bool V1 = Verified, IsUnverified = 0> - bool verify(size_t n = 1) const { - if (ptr_ == nullptr) { - return false; - } - -#ifdef __ANDROID__ - if (LIKELY(map_ == nullptr)) { - return ptr_ != nullptr; - } - - const size_t verify_size = sizeof(T) * n; - LIBINCFS_MAP_PTR_DEBUG_CODE(if (sizeof(T) <= verify_size) verified_ = true;); - - const auto data_start = reinterpret_cast(ptr_); - const auto data_end = reinterpret_cast(ptr_) + verify_size; - - // If the data is entirely within the block beginning at the previous verified block - // pointer, then the data can safely be used. - if (LIKELY(data_start >= verified_block_ && - data_end <= verified_block_ + INCFS_DATA_FILE_BLOCK_SIZE)) { - return true; - } - - if (LIKELY(map_->Verify(data_start, data_end, &verified_block_))) { - return true; - } - - LIBINCFS_MAP_PTR_DEBUG_CODE(verified_ = false); - return false; -#else - (void)n; - return true; -#endif - } - - // Returns a verified version of this pointer. - // The caller should verify the presence of the pointer data before calling this method. - template = 0> - verified_map_ptr verified() const { - return verified_map_ptr(map_, ptr_, verified_block_); - } - -private: - map_ptr(const IncFsFileMap* map, const T* ptr) - : ptr_(ptr), map_(map), verified_block_(nullptr) {} - map_ptr(const IncFsFileMap* map, const T* ptr, const uint8_t* verified_block) - : ptr_(ptr), map_(map), verified_block_(verified_block) {} - - const T* ptr_ = nullptr; - mutable const IncFsFileMap* map_ = nullptr; - mutable const uint8_t* verified_block_; - LIBINCFS_MAP_PTR_DEBUG_CODE(mutable bool verified_ = Verified); -}; - -} // namespace incfs - -} // namespace android \ No newline at end of file diff --git a/base/cvd/adb/patches/0001-drop-fastdeploy.patch b/base/cvd/adb/patches/0001-drop-fastdeploy.patch new file mode 100644 index 00000000000..2ba023374ce --- /dev/null +++ b/base/cvd/adb/patches/0001-drop-fastdeploy.patch @@ -0,0 +1,205 @@ +--- b/base/cvd/adb/client/adb_install.cpp ++++ a/base/cvd/adb/client/adb_install.cpp +@@ -37,14 +37,11 @@ + #include "adb_utils.h" + #include "client/file_sync_client.h" + #include "commandline.h" +-#include "fastdeploy.h" + #include "incremental.h" + #include "sysdeps.h" + + using namespace std::literals; + +-static constexpr int kFastDeployMinApi = 24; +- + namespace { + + enum InstallMode { +@@ -158,7 +155,7 @@ static unique_fd send_command(const std::vector& cmd_args, std::str + } + } + +-static int install_app_streamed(int argc, const char** argv, bool use_fastdeploy) { ++static int install_app_streamed(int argc, const char** argv) { + printf("Performing Streamed Install\n"); + + // The last argument must be the APK file +@@ -176,21 +173,6 @@ static int install_app_streamed(int argc, const char** argv, bool use_fastdeploy + error_exit(".apex is not supported on the target device"); + } + +- if (is_apex && use_fastdeploy) { +- error_exit("--fastdeploy doesn't support .apex files"); +- } +- +- if (use_fastdeploy) { +- auto metadata = extract_metadata(file); +- if (metadata.has_value()) { +- // pass all but 1st (command) and last (apk path) parameters through to pm for +- // session creation +- std::vector pm_args{argv + 1, argv + argc - 1}; +- auto patchFd = install_patch(pm_args.size(), pm_args.data()); +- return stream_patch(file, std::move(metadata.value()), std::move(patchFd)); +- } +- } +- + struct stat sb; + if (stat(file, &sb) == -1) { + perror_exit("failed to stat %s", file); +@@ -247,7 +229,7 @@ static int install_app_streamed(int argc, const char** argv, bool use_fastdeploy + return 0; + } + +-static int install_app_legacy(int argc, const char** argv, bool use_fastdeploy) { ++static int install_app_legacy(int argc, const char** argv) { + printf("Performing Push Install\n"); + + // Find last APK argument. +@@ -270,19 +252,6 @@ static int install_app_legacy(int argc, const char** argv, bool use_fastdeploy) + std::string apk_dest = "/data/local/tmp/" + android::base::Basename(argv[last_apk]); + argv[last_apk] = apk_dest.c_str(); /* destination name, not source location */ + +- if (use_fastdeploy) { +- auto metadata = extract_metadata(apk_file[0]); +- if (metadata.has_value()) { +- auto patchFd = apply_patch_on_device(apk_dest.c_str()); +- int status = stream_patch(apk_file[0], std::move(metadata.value()), std::move(patchFd)); +- +- result = pm_command(argc, argv); +- delete_device_file(apk_dest); +- +- return status; +- } +- } +- + if (do_sync_push(apk_file, apk_dest.c_str(), false, CompressionType::Any, false, false)) { + result = pm_command(argc, argv); + delete_device_file(apk_dest); +@@ -352,14 +321,7 @@ static int install_app_incremental(int argc, const char** argv, bool wait, bool + } + + static std::pair> calculate_install_mode( +- InstallMode modeFromArgs, bool fastdeploy, CmdlineOption incremental_request) { +- if (incremental_request == CmdlineOption::Enable) { +- if (fastdeploy) { +- error_exit( +- "--incremental and --fast-deploy options are incompatible. " +- "Please choose one"); +- } +- } ++ InstallMode modeFromArgs, CmdlineOption incremental_request) { + + if (modeFromArgs != INSTALL_DEFAULT) { + if (incremental_request == CmdlineOption::Enable) { +@@ -446,61 +408,23 @@ static std::vector parse_install_mode(std::vector argv + return passthrough; + } + +-static std::vector parse_fast_deploy_mode( +- std::vector argv, bool* use_fastdeploy, +- FastDeploy_AgentUpdateStrategy* agent_update_strategy) { +- *use_fastdeploy = false; +- *agent_update_strategy = FastDeploy_AgentUpdateDifferentVersion; +- +- std::vector passthrough; +- for (auto&& arg : argv) { +- if (arg == "--fastdeploy"sv) { +- *use_fastdeploy = true; +- } else if (arg == "--no-fastdeploy"sv) { +- *use_fastdeploy = false; +- } else if (arg == "--force-agent"sv) { +- *agent_update_strategy = FastDeploy_AgentUpdateAlways; +- } else if (arg == "--date-check-agent"sv) { +- *agent_update_strategy = FastDeploy_AgentUpdateNewerTimeStamp; +- } else if (arg == "--version-check-agent"sv) { +- *agent_update_strategy = FastDeploy_AgentUpdateDifferentVersion; +- } else { +- passthrough.push_back(arg); +- } +- } +- return passthrough; +-} +- + int install_app(int argc, const char** argv) { + InstallMode install_mode = INSTALL_DEFAULT; + auto incremental_request = CmdlineOption::None; + bool incremental_wait = false; + +- bool use_fastdeploy = false; +- FastDeploy_AgentUpdateStrategy agent_update_strategy = FastDeploy_AgentUpdateDifferentVersion; +- + auto unused_argv = parse_install_mode({argv, argv + argc}, &install_mode, &incremental_request, + &incremental_wait); +- auto passthrough_argv = +- parse_fast_deploy_mode(std::move(unused_argv), &use_fastdeploy, &agent_update_strategy); ++ auto passthrough_argv = std::move(unused_argv); + + auto [primary_mode, fallback_mode] = +- calculate_install_mode(install_mode, use_fastdeploy, incremental_request); ++ calculate_install_mode(install_mode, incremental_request); + if ((primary_mode == INSTALL_STREAM || + fallback_mode.value_or(INSTALL_PUSH) == INSTALL_STREAM) && + best_install_mode() == INSTALL_PUSH) { + error_exit("Attempting to use streaming install on unsupported device"); + } + +- if (use_fastdeploy && get_device_api_level() < kFastDeployMinApi) { +- fprintf(stderr, +- "Fast Deploy is only compatible with devices of API version %d or higher, " +- "ignoring.\n", +- kFastDeployMinApi); +- use_fastdeploy = false; +- } +- fastdeploy_set_agent_update_strategy(agent_update_strategy); +- + if (passthrough_argv.size() < 2) { + error_exit("install requires an apk argument"); + } +@@ -508,11 +432,9 @@ int install_app(int argc, const char** argv) { + auto run_install_mode = [&](InstallMode install_mode, bool silent) { + switch (install_mode) { + case INSTALL_PUSH: +- return install_app_legacy(passthrough_argv.size(), passthrough_argv.data(), +- use_fastdeploy); ++ return install_app_legacy(passthrough_argv.size(), passthrough_argv.data()); + case INSTALL_STREAM: +- return install_app_streamed(passthrough_argv.size(), passthrough_argv.data(), +- use_fastdeploy); ++ return install_app_streamed(passthrough_argv.size(), passthrough_argv.data()); + case INSTALL_INCREMENTAL: + return install_app_incremental(passthrough_argv.size(), passthrough_argv.data(), + incremental_wait, silent); +@@ -681,13 +603,12 @@ int install_multiple_app(int argc, const char** argv) { + InstallMode install_mode = INSTALL_DEFAULT; + auto incremental_request = CmdlineOption::None; + bool incremental_wait = false; +- bool use_fastdeploy = false; + + auto passthrough_argv = parse_install_mode({argv + 1, argv + argc}, &install_mode, + &incremental_request, &incremental_wait); + + auto [primary_mode, fallback_mode] = +- calculate_install_mode(install_mode, use_fastdeploy, incremental_request); ++ calculate_install_mode(install_mode, incremental_request); + if ((primary_mode == INSTALL_STREAM || + fallback_mode.value_or(INSTALL_PUSH) == INSTALL_STREAM) && + best_install_mode() == INSTALL_PUSH) { +--- b/base/cvd/adb/client/commandline.cpp ++++ a/base/cvd/adb/client/commandline.cpp +@@ -66,7 +66,6 @@ using namespace std::string_literals; + #include "bugreport.h" + #include "client/file_sync_client.h" + #include "commandline.h" +-#include "fastdeploy.h" + #include "incremental_server.h" + #include "services.h" + #include "shell_protocol.h" +@@ -189,8 +188,6 @@ static void help() { + " --instant: cause the app to be installed as an ephemeral install app\n" + " --no-streaming: always push APK to device and invoke Package Manager as separate steps\n" + " --streaming: force streaming APK directly into Package Manager\n" +- " --fastdeploy: use fast deploy\n" +- " --no-fastdeploy: prevent use of fast deploy\n" + " --force-agent: force update of deployment agent when using fast deploy\n" + " --date-check-agent: update deployment agent when local version is newer and using fast deploy\n" + " --version-check-agent: update deployment agent when local version has different version code and using fast deploy\n" diff --git a/base/cvd/adb/patches/0002-drop-incremental-update.patch b/base/cvd/adb/patches/0002-drop-incremental-update.patch new file mode 100644 index 00000000000..1bef14cc2ca --- /dev/null +++ b/base/cvd/adb/patches/0002-drop-incremental-update.patch @@ -0,0 +1,273 @@ +--- a/base/cvd/adb/client/incremental_utils.cpp ++++ b/base/cvd/adb/client/incremental_utils.cpp +@@ -30,8 +30,6 @@ + #include + #include + #include +-#include +-#include + + #include "adb_io.h" + #include "adb_trace.h" +@@ -41,10 +39,6 @@ + + namespace incremental { + +-static constexpr inline int32_t offsetToBlockIndex(int64_t offset) { +- return (offset & ~(kBlockSize - 1)) >> 12; +-} +- + Size verity_tree_blocks_for_file(Size fileSize) { + if (fileSize == 0) { + return 0; +@@ -194,16 +188,6 @@ + return t; + } + +-static void appendBlocks(int32_t start, int count, std::vector* blocks) { +- if (count == 1) { +- blocks->push_back(start); +- } else { +- auto oldSize = blocks->size(); +- blocks->resize(oldSize + count); +- std::iota(blocks->begin() + oldSize, blocks->end(), start); +- } +-} +- + template + static void unduplicate(std::vector& v) { + std::unordered_set uniques(v.size()); +@@ -212,230 +196,9 @@ + v.end()); + } + +-static off64_t CentralDirOffset(borrowed_fd fd, Size fileSize) { +- static constexpr int kZipEocdRecMinSize = 22; +- static constexpr int32_t kZipEocdRecSig = 0x06054b50; +- static constexpr int kZipEocdCentralDirSizeFieldOffset = 12; +- static constexpr int kZipEocdCommentLengthFieldOffset = 20; +- +- int32_t sigBuf = 0; +- off64_t eocdOffset = -1; +- off64_t maxEocdOffset = fileSize - kZipEocdRecMinSize; +- int16_t commentLenBuf = 0; +- +- // Search from the end of zip, backward to find beginning of EOCD +- for (int16_t commentLen = 0; commentLen < fileSize; ++commentLen) { +- sigBuf = valueAt(fd, maxEocdOffset - commentLen); +- if (sigBuf == kZipEocdRecSig) { +- commentLenBuf = valueAt( +- fd, maxEocdOffset - commentLen + kZipEocdCommentLengthFieldOffset); +- if (commentLenBuf == commentLen) { +- eocdOffset = maxEocdOffset - commentLen; +- break; +- } +- } +- } +- +- if (eocdOffset < 0) { +- return -1; +- } +- +- off64_t cdLen = static_cast( +- valueAt(fd, eocdOffset + kZipEocdCentralDirSizeFieldOffset)); +- +- return eocdOffset - cdLen; +-} +- +-// Does not support APKs larger than 4GB +-static off64_t SignerBlockOffset(borrowed_fd fd, Size fileSize) { +- static constexpr int kApkSigBlockMinSize = 32; +- static constexpr int kApkSigBlockFooterSize = 24; +- static constexpr int64_t APK_SIG_BLOCK_MAGIC_HI = 0x3234206b636f6c42l; +- static constexpr int64_t APK_SIG_BLOCK_MAGIC_LO = 0x20676953204b5041l; +- +- off64_t cdOffset = CentralDirOffset(fd, fileSize); +- if (cdOffset < 0) { +- return -1; +- } +- // CD offset is where original signer block ends. Search backwards for magic and footer. +- if (cdOffset < kApkSigBlockMinSize || +- valueAt(fd, cdOffset - 2 * sizeof(int64_t)) != APK_SIG_BLOCK_MAGIC_LO || +- valueAt(fd, cdOffset - sizeof(int64_t)) != APK_SIG_BLOCK_MAGIC_HI) { +- return -1; +- } +- int32_t signerSizeInFooter = valueAt(fd, cdOffset - kApkSigBlockFooterSize); +- off64_t signerBlockOffset = cdOffset - signerSizeInFooter - sizeof(int64_t); +- if (signerBlockOffset < 0) { +- return -1; +- } +- int32_t signerSizeInHeader = valueAt(fd, signerBlockOffset); +- if (signerSizeInFooter != signerSizeInHeader) { +- return -1; +- } +- +- return signerBlockOffset; +-} +- +-static std::vector ZipPriorityBlocks(off64_t signerBlockOffset, Size fileSize) { +- int32_t signerBlockIndex = offsetToBlockIndex(signerBlockOffset); +- int32_t lastBlockIndex = offsetToBlockIndex(fileSize); +- const auto numPriorityBlocks = lastBlockIndex - signerBlockIndex + 1; +- +- std::vector zipPriorityBlocks; +- +- // Some magic here: most of zip libraries perform a scan for EOCD record starting at the offset +- // of a maximum comment size from the end of the file. This means the last 65-ish KBs will be +- // accessed first, followed by the rest of the central directory blocks. Make sure we +- // send the data in the proper order, as central directory can be quite big by itself. +- static constexpr auto kMaxZipCommentSize = 64 * 1024; +- static constexpr auto kNumBlocksInEocdSearch = kMaxZipCommentSize / kBlockSize + 1; +- if (numPriorityBlocks > kNumBlocksInEocdSearch) { +- appendBlocks(lastBlockIndex - kNumBlocksInEocdSearch + 1, kNumBlocksInEocdSearch, +- &zipPriorityBlocks); +- appendBlocks(signerBlockIndex, numPriorityBlocks - kNumBlocksInEocdSearch, +- &zipPriorityBlocks); +- } else { +- appendBlocks(signerBlockIndex, numPriorityBlocks, &zipPriorityBlocks); +- } +- +- // Somehow someone keeps accessing the start of the archive, even if there's nothing really +- // interesting there... +- appendBlocks(0, 1, &zipPriorityBlocks); +- return zipPriorityBlocks; +-} +- +-[[maybe_unused]] static ZipArchiveHandle openZipArchiveFd(borrowed_fd fd) { +- bool transferFdOwnership = false; +-#ifdef _WIN32 +- // +- // Need to create a special CRT FD here as the current one is not compatible with +- // normal read()/write() calls that libziparchive uses. +- // To make this work we have to create a copy of the file handle, as CRT doesn't care +- // and closes it together with the new descriptor. +- // +- // Note: don't move this into a helper function, it's better to be hard to reuse because +- // the code is ugly and won't work unless it's a last resort. +- // +- auto handle = adb_get_os_handle(fd); +- HANDLE dupedHandle; +- if (!::DuplicateHandle(::GetCurrentProcess(), handle, ::GetCurrentProcess(), &dupedHandle, 0, +- false, DUPLICATE_SAME_ACCESS)) { +- D("%s failed at DuplicateHandle: %d", __func__, (int)::GetLastError()); +- return {}; +- } +- int osfd = _open_osfhandle((intptr_t)dupedHandle, _O_RDONLY | _O_BINARY); +- if (osfd < 0) { +- D("%s failed at _open_osfhandle: %d", __func__, errno); +- ::CloseHandle(handle); +- return {}; +- } +- transferFdOwnership = true; +-#else +- int osfd = fd.get(); +-#endif +- ZipArchiveHandle zip; +- if (OpenArchiveFd(osfd, "apk_fd", &zip, transferFdOwnership) != 0) { +- D("%s failed at OpenArchiveFd: %d", __func__, errno); +-#ifdef _WIN32 +- // "_close()" is a secret WinCRT name for the regular close() function. +- _close(osfd); +-#endif +- return {}; +- } +- return zip; +-} +- +-static std::pair> openZipArchive( +- borrowed_fd fd, Size fileSize) { +-#ifndef __LP64__ +- if (fileSize >= INT_MAX) { +- return {openZipArchiveFd(fd), std::nullopt}; +- } +-#endif +- auto mapping = android::base::MappedFile::Create(adb_get_os_handle(fd), 0, fileSize, PROT_READ); +- if (!mapping) { +- D("%s failed at MappedFile::Create: %d", __func__, errno); +- return {}; +- } +- ZipArchiveHandle zip; +- if (OpenArchiveFromMemory(mapping->data(), mapping->size(), "apk_mapping", &zip) != 0) { +- D("%s failed at OpenArchiveFromMemory: %d", __func__, errno); +- return {}; +- } +- return {zip, std::move(mapping)}; +-} +- +-static std::vector InstallationPriorityBlocks(borrowed_fd fd, Size fileSize) { +- static constexpr std::array additional_matches = { +- "resources.arsc"sv, "AndroidManifest.xml"sv, "classes.dex"sv}; +- auto [zip, _] = openZipArchive(fd, fileSize); +- if (!zip) { +- return {}; +- } +- +- auto matcher = [](std::string_view entry_name) { +- if (entry_name.starts_with("lib/"sv) && entry_name.ends_with(".so"sv)) { +- return true; +- } +- return std::any_of(additional_matches.begin(), additional_matches.end(), +- [entry_name](std::string_view i) { return i == entry_name; }); +- }; +- +- void* cookie = nullptr; +- if (StartIteration(zip, &cookie, std::move(matcher)) != 0) { +- D("%s failed at StartIteration: %d", __func__, errno); +- return {}; +- } +- +- std::vector installationPriorityBlocks; +- ZipEntry64 entry; +- std::string_view entryName; +- while (Next(cookie, &entry, &entryName) == 0) { +- if (entryName == "classes.dex"sv) { +- // Only the head is needed for installation +- int32_t startBlockIndex = offsetToBlockIndex(entry.offset); +- appendBlocks(startBlockIndex, 2, &installationPriorityBlocks); +- D("\tadding to priority blocks: '%.*s' (%d)", (int)entryName.size(), entryName.data(), +- 2); +- } else { +- // Full entries are needed for installation +- off64_t entryStartOffset = entry.offset; +- off64_t entryEndOffset = +- entryStartOffset + +- (entry.method == kCompressStored ? entry.uncompressed_length +- : entry.compressed_length) + +- (entry.has_data_descriptor ? 16 /* sizeof(DataDescriptor) */ : 0); +- int32_t startBlockIndex = offsetToBlockIndex(entryStartOffset); +- int32_t endBlockIndex = offsetToBlockIndex(entryEndOffset); +- int32_t numNewBlocks = endBlockIndex - startBlockIndex + 1; +- appendBlocks(startBlockIndex, numNewBlocks, &installationPriorityBlocks); +- D("\tadding to priority blocks: '%.*s' (%d)", (int)entryName.size(), entryName.data(), +- numNewBlocks); +- } +- } +- +- EndIteration(cookie); +- CloseArchive(zip); +- return installationPriorityBlocks; +-} +- +-std::vector PriorityBlocksForFile(const std::string& filepath, borrowed_fd fd, +- Size fileSize) { +- if (!android::base::EndsWithIgnoreCase(filepath, ".apk"sv)) { +- return {}; +- } +- off64_t signerOffset = SignerBlockOffset(fd, fileSize); +- if (signerOffset < 0) { +- // No signer block? not a valid APK +- return {}; +- } +- std::vector priorityBlocks = ZipPriorityBlocks(signerOffset, fileSize); +- std::vector installationPriorityBlocks = InstallationPriorityBlocks(fd, fileSize); +- +- priorityBlocks.insert(priorityBlocks.end(), installationPriorityBlocks.begin(), +- installationPriorityBlocks.end()); +- unduplicate(priorityBlocks); +- return priorityBlocks; ++std::vector PriorityBlocksForFile(const std::string&, borrowed_fd, ++ Size) { ++ return {}; + } + + } // namespace incremental diff --git a/base/cvd/adb/patches/0003-drop-mdns.patch b/base/cvd/adb/patches/0003-drop-mdns.patch new file mode 100644 index 00000000000..063689f6d14 --- /dev/null +++ b/base/cvd/adb/patches/0003-drop-mdns.patch @@ -0,0 +1,66 @@ +--- a/base/cvd/adb/client/mdns_utils.cpp ++++ b/base/cvd/adb/client/mdns_utils.cpp +@@ -75,17 +75,11 @@ + } + + bool is_enabled() { +- return !getenv("ADB_MDNS") || strcmp(getenv("ADB_MDNS"), "0") != 0; ++ return false; + } + +-// Historically, this env variable was a boolean used to disable openscreen. +-// It has evolved into an enum to select the mdns backend +-// 0 = Bonjour (deprecated) +-// 1 = Openscreen +-// 2 = Adbmdns + bool should_use_openscreen() { +- const char* mdns_osp = getenv("ADB_MDNS_OPENSCREEN"); +- return mdns_osp != nullptr && strcmp(mdns_osp, "1") == 0; ++ return false; + } + + } // namespace mdns +--- a/base/cvd/adb/client/transport_mdns.cpp ++++ b/base/cvd/adb/client/transport_mdns.cpp +@@ -37,11 +37,8 @@ + #include "adb_trace.h" + #include "adb_utils.h" + #include "adb_wifi.h" +-#include "client/adbmdns/adbmdns.h" +-#include "client/adbmdns/adbmdns_bridge.h" + #include "client/discovered_services.h" + #include "client/mdns_utils.h" +-#include "client/openscreen/mdns_service.h" + #include "fdevent/fdevent.h" + #include "mdns_tracker.h" + #include "sysdeps.h" +@@ -132,11 +129,7 @@ + ///////////////////////////////////////////////////////////////////////////////// + + void init_mdns_transport_discovery() { +- if (mdns::should_use_openscreen()) { +- StartOpenScreenDiscovery(); +- } else { +- StartAdbMdnsDiscovery(); +- } ++ VLOG(MDNS) << "mdns discovery is disabled in this build"; + } + + bool adb_secure_connect_by_service_name(const std::string& instance_name) { +@@ -148,15 +141,7 @@ + } + + std::string mdns_check() { +- if (!mdns::is_enabled()) { +- return "ERROR: mdns discovery disabled"; +- } +- +- if (mdns::should_use_openscreen()) { +- return "mdns daemon version [Openscreen discovery 0.0.0]"; +- } +- +- return "mdns daemon version [adb discovery 0.0.0]"; ++ return "ERROR: mdns discovery disabled"; + } + + std::string mdns_list_discovered_services() { diff --git a/base/cvd/adb/patches/0004-drop-socket-local-client-timeout.patch b/base/cvd/adb/patches/0004-drop-socket-local-client-timeout.patch new file mode 100644 index 00000000000..ec1c6e9fece --- /dev/null +++ b/base/cvd/adb/patches/0004-drop-socket-local-client-timeout.patch @@ -0,0 +1,28 @@ +--- a/base/cvd/adb/sysdeps.h ++++ b/base/cvd/adb/sysdeps.h +@@ -606,7 +606,7 @@ + return _fd_set_error_str(socket_inaddr_any_server(port, type), error); + } + +-static int get_adb_connect_timeout() { ++[[maybe_unused]] static int get_adb_connect_timeout() { + // Problematic pull services encountered (e.g.: chrome://inspect/#devices) tend to default + // to pulling every 1s. Defaulting to 500ms gives enough time to fdevent to process packets + // between the time these services hit us. +@@ -614,15 +614,7 @@ + } + + inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) { +- int timeout = get_adb_connect_timeout(); +- // As a failsafe for the new socket_local_client_timeout, we revert to the legacy version if +- // no timeout is requested. +- if (timeout == -1) { +- return _fd_set_error_str(socket_local_client(name, namespace_id, type), error); +- } else { +- return _fd_set_error_str(socket_local_client_timeout(name, namespace_id, type, timeout), +- error); +- } ++ return _fd_set_error_str(socket_local_client(name, namespace_id, type), error); + } + + inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) { diff --git a/base/cvd/adb/proto/adb_host.proto b/base/cvd/adb/proto/adb_host.proto index f2c907ec353..3a9b3949b39 100644 --- a/base/cvd/adb/proto/adb_host.proto +++ b/base/cvd/adb/proto/adb_host.proto @@ -66,12 +66,15 @@ message AdbServerStatus { UNKNOWN_USB = 0; NATIVE = 1; LIBUSB = 2; + USB_DISABLED = 3; } enum MdnsBackend { UNKNOWN_MDNS = 0; BONJOUR = 1; OPENSCREEN = 2; + LIBADBMDNS = 3; + MDNS_DISABLED = 4; } UsbBackend usb_backend = 1; @@ -134,6 +137,18 @@ message MdnsService { // Comes from device property "ro.build.version.sdk_full" optional string build_version_sdk_full = 8; + + // The name of the device, e.g. "Foo's Pixel 10". + optional string given_name = 9; + + // Comes from device property "ro.serialno" + optional string serial = 10; + + // Used by clients to manage changes to the MdnsService. + optional string mdns_service_version = 11; + + // The local hostname the service is advertised under. + optional string hostname = 12; } diff --git a/base/cvd/adb/services.cpp b/base/cvd/adb/services.cpp index 2fc5aba69dd..de9f6287575 100644 --- a/base/cvd/adb/services.cpp +++ b/base/cvd/adb/services.cpp @@ -30,12 +30,15 @@ #include #include #include +#include "android-base/file.h" #include "adb.h" #include "adb_io.h" #include "adb_unique_fd.h" #include "adb_utils.h" -#include "adb_wifi.h" +#if ADB_HOST +#include "client/adb_wifi.h" +#endif #include "services.h" #include "socket_spec.h" #include "sysdeps.h" @@ -140,6 +143,8 @@ void connect_emulator(const std::string& port_spec, std::string* response) { } static void connect_service(unique_fd fd, std::string host) { + CHECK_NOT_LOOPER_THREAD(); + std::string response; if (!strncmp(host.c_str(), "emu:", 4)) { connect_emulator(host.c_str() + 4, &response); @@ -154,6 +159,7 @@ static void connect_service(unique_fd fd, std::string host) { static void pair_service(unique_fd fd, std::string host, std::string password) { std::string response; adb_wifi_pair_device(host, password, response); + VLOG(MDNS) << "Pairing response: '" << response << "'"; if (android::base::StartsWith(response, "Successful")) { SendProtocolString(fd.get(), response); } else { @@ -252,6 +258,13 @@ static void wait_service(unique_fd fd, std::string serial, TransportId transport #endif #if ADB_HOST + +static void list_mdns_known_hosts(const unique_fd& fd) { + std::string response; + android::base::ReadFileToString(known_wifi_hosts_file.KeyStorePath(), &response); + SendProtocolString(fd.get(), response); +} + asocket* host_service_to_socket(std::string_view name, std::string_view serial, TransportId transport_id) { if (name == "track-devices") { @@ -285,6 +298,10 @@ asocket* host_service_to_socket(std::string_view name, std::string_view serial, return create_local_socket(std::move(fd)); } else if (android::base::ConsumePrefix(&name, HostServices::kTrackMdnsServices)) { return create_mdns_tracker(); + } else if (android::base::ConsumePrefix(&name, HostServices::kListMdnsKnownHosts)) { + unique_fd fd = create_service_thread( + "list-mdns-known-hosts", std::bind(list_mdns_known_hosts, std::placeholders::_1)); + return create_local_socket(std::move(fd)); } return nullptr; } diff --git a/base/cvd/adb/socket_spec.cpp b/base/cvd/adb/socket_spec.cpp index eb72ed55be6..391bfdb8644 100644 --- a/base/cvd/adb/socket_spec.cpp +++ b/base/cvd/adb/socket_spec.cpp @@ -56,6 +56,10 @@ using android::base::StringPrintf; #include #endif +#if ADB_HOST +#include "client/transport_mdns.h" +#endif + bool gListenAll = false; struct LocalSocketType { @@ -76,7 +80,7 @@ static auto& kLocalSocketTypes = *new std::unordered_map, parse it ourselves. // Otherwise, delegate to android::base::ParseNetAddress. - if (android::base::ParseInt(&spec[4], &port_value)) { + if (android::base::ParseInt(&after_tcp[0], &port_value)) { // Do the range checking ourselves, because ParseInt rejects 'tcp:65536' and 'tcp:foo:1234' // identically. if (port_value < 0 || port_value > 65535) { @@ -96,12 +106,12 @@ bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* po return false; } } else { - std::string addr(spec.substr(4)); port_value = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening // on an address that isn't 'localhost' is unsupported. - if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) { + if (!android::base::ParseNetAddress(after_tcp, &hostname_value, &port_value, + canonical_address, error)) { return false; } } @@ -183,8 +193,22 @@ bool is_local_socket_spec(std::string_view spec) { return tcp_host_is_local(hostname); } -bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial, - std::string* error) { +bool check_adb_vsock_cid_port(int cid, int port, std::string* error) { +#if !ADB_HOST + if (cid != VMADDR_CID_HOST && port != DEFAULT_ADB_LOCAL_TRANSPORT_PORT) { + *error = android::base::StringPrintf( + "Only port %d is supported for vsock connections to any CID other than %d. Got %d:%d.", + DEFAULT_ADB_LOCAL_TRANSPORT_PORT, VMADDR_CID_HOST, cid, port); + return false; + } + return true; +#else + return true; +#endif +} + +bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, + std::string* transport_name, std::string* error) { #if !ADB_HOST if (!socket_access_allowed) { // Check whether this security suppression is // active (initiated from minadbd), and if so disable socket communications @@ -197,7 +221,7 @@ bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std if (address.starts_with("tcp:")) { std::string hostname; int port_value = port ? *port : 0; - if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) { + if (!parse_tcp_socket_spec(address, &hostname, &port_value, transport_name, error)) { return false; } @@ -215,8 +239,9 @@ bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std // use the mdns instance name, so we can adjust to address changes on // reconnects. port_value = mdns_info->port; - if (serial) { - *serial = std::format("{}.{}", mdns_info->instance, mdns_info->service); + if (transport_name) { + *transport_name = + std::format("{}.{}", mdns_info->instance, mdns_info->service); } } } else { @@ -272,6 +297,10 @@ bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std errno = EINVAL; return false; } + if (!check_adb_vsock_cid_port(cid, port_value, error)) { + errno = EINVAL; + return false; + } fd->reset(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0)); if (fd->get() == -1) { *error = "could not open vsock socket"; @@ -281,8 +310,8 @@ bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std addr.svm_family = AF_VSOCK; addr.svm_port = port_value; addr.svm_cid = cid; - if (serial) { - *serial = android::base::StringPrintf("vsock:%u:%d", cid, port_value); + if (transport_name) { + *transport_name = android::base::StringPrintf("vsock:%u:%d", cid, port_value); } if (connect(fd->get(), reinterpret_cast(&addr), sizeof(addr))) { int error_num = errno; @@ -317,14 +346,14 @@ bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std SOCK_STREAM, error)); if (fd->get() < 0) { - *error = - android::base::StringPrintf("could not connect to %s address '%s'", - it.first.c_str(), std::string(address).c_str()); + *error = android::base::StringPrintf("could not connect to %s address '%s' (%s)", + it.first.c_str(), std::string(address).c_str(), + error->c_str()); return false; } - if (serial) { - *serial = address; + if (transport_name) { + *transport_name = address; } return true; } diff --git a/base/cvd/adb/socket_spec.h b/base/cvd/adb/socket_spec.h index 8fbb1e3ff12..dbbf17bf7a4 100644 --- a/base/cvd/adb/socket_spec.h +++ b/base/cvd/adb/socket_spec.h @@ -27,11 +27,18 @@ extern bool gListenAll; bool is_socket_spec(std::string_view spec); bool is_local_socket_spec(std::string_view spec); -bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial, - std::string* error); +// Connect to 'address'. +// - fd (out): The file descriptor of the connection. +// - address (in) : Can be IP:PORT, or a HOSTNAME (even .local) to resolve. +// - port (out): port the connection was established on. +// - transport_name (out): name of the transport_name (see Transport::name). +// - error (out): error string if the function returns false. +bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, + std::string* transport_name, std::string* error); + int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_tcp_port = nullptr); bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port, - std::string* serial, std::string* error); + std::string* canonical_address, std::string* error); int get_host_socket_spec_port(std::string_view spec, std::string* error); diff --git a/base/cvd/adb/sockets.cpp b/base/cvd/adb/sockets.cpp index 9a24ff08449..acb3de1c8ca 100644 --- a/base/cvd/adb/sockets.cpp +++ b/base/cvd/adb/sockets.cpp @@ -470,7 +470,7 @@ asocket* create_local_service_socket(std::string_view name, atransport* transpor int fd_value = fd.get(); asocket* s = create_local_socket(std::move(fd)); s->transport = transport; - VLOG(SERVICES) << "LS(" << s->id << "): bound to '" << name << "' via " << fd_value; + VLOG(SOCKETS) << "LS(" << s->id << "): bound to '" << name << "' via " << fd_value; #if !ADB_HOST if ((name.starts_with("root:") && getuid() != 0 && __android_log_is_debuggable()) || @@ -567,7 +567,7 @@ void connect_to_remote(asocket* s, std::string_view destination) { D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd); apacket* p = get_apacket(); - LOG(VERBOSE) << "LS(" << s->id << ": connect(" << destination << ")"; + VLOG(ADB) << "LS(" << s->id << ": connect(" << destination << ")"; p->msg.command = A_OPEN; p->msg.arg0 = s->id; @@ -859,7 +859,7 @@ static int smart_socket_enqueue(asocket* s, apacket::payload_type data) { switch (host_request_result) { case HostRequestResult::Handled: - LOG(VERBOSE) << "SS(" << s->id << "): handled host service '" << service << "'"; + VLOG(SERVICES) << "SS(" << s->id << "): handled host service '" << service << "'"; goto fail; case HostRequestResult::SwitchedTransport: @@ -878,7 +878,8 @@ static int smart_socket_enqueue(asocket* s, apacket::payload_type data) { // TODO: Convert to string_view. s2 = host_service_to_socket(service, serial, transport_id); if (s2 == nullptr) { - LOG(VERBOSE) << "SS(" << s->id << "): couldn't create host service '" << service << "'"; + VLOG(SERVICES) << "SS(" << s->id << "): couldn't create host service '" << service + << "'"; std::string msg = std::string("unknown host service '") + std::string(service) + "'"; SendFail(s->peer->fd, msg); goto fail; diff --git a/base/cvd/adb/sysdeps.h b/base/cvd/adb/sysdeps.h index e973e0ef17e..f0d63bdaf05 100644 --- a/base/cvd/adb/sysdeps.h +++ b/base/cvd/adb/sysdeps.h @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #if __has_include() @@ -605,6 +606,13 @@ inline int network_inaddr_any_server(int port, int type, std::string* error) { return _fd_set_error_str(socket_inaddr_any_server(port, type), error); } +[[maybe_unused]] static int get_adb_connect_timeout() { + // Problematic pull services encountered (e.g.: chrome://inspect/#devices) tend to default + // to pulling every 1s. Defaulting to 500ms gives enough time to fdevent to process packets + // between the time these services hit us. + return android::base::GetIntProperty("persist.adb.local_connect_timeout_ms", 500); +} + inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) { return _fd_set_error_str(socket_local_client(name, namespace_id, type), error); } diff --git a/base/cvd/adb/tls/tls_connection.cpp b/base/cvd/adb/tls/tls_connection.cpp index d87993d0174..0ea3605fc62 100644 --- a/base/cvd/adb/tls/tls_connection.cpp +++ b/base/cvd/adb/tls/tls_connection.cpp @@ -27,6 +27,7 @@ #include using android::base::borrowed_fd; +using namespace std::string_literals; namespace adb { namespace tls { @@ -61,6 +62,7 @@ class TlsConnectionImpl : public TlsConnection { static bssl::UniquePtr X509FromBuffer(bssl::UniquePtr buffer); static const char* SSLErrorString(); + static std::string SSL_IO_Error(int error); void Invalidate(); TlsError GetFailureReason(int err); const char* RoleToString() { return role_ == Role::Server ? kServerRoleStr : kClientRoleStr; } @@ -100,19 +102,16 @@ TlsConnectionImpl::~TlsConnectionImpl() { } } -// static const char* TlsConnectionImpl::SSLErrorString() { auto sslerr = ERR_peek_last_error(); return ERR_reason_error_string(sslerr); } -// static bssl::UniquePtr TlsConnectionImpl::EvpPkeyFromPEM(std::string_view pem) { bssl::UniquePtr bio(BIO_new_mem_buf(pem.data(), pem.size())); return bssl::UniquePtr(PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr)); } -// static bssl::UniquePtr TlsConnectionImpl::BufferFromPEM(std::string_view pem) { bssl::UniquePtr bio(BIO_new_mem_buf(pem.data(), pem.size())); char* name = nullptr; @@ -132,7 +131,6 @@ bssl::UniquePtr TlsConnectionImpl::BufferFromPEM(std::string_view return ret; } -// static bssl::UniquePtr TlsConnectionImpl::X509FromBuffer(bssl::UniquePtr buffer) { if (!buffer) { return nullptr; @@ -140,13 +138,11 @@ bssl::UniquePtr TlsConnectionImpl::X509FromBuffer(bssl::UniquePtr(X509_parse_from_buffer(buffer.get())); } -// static int TlsConnectionImpl::SSLSetCertVerifyCb(X509_STORE_CTX* ctx, void* opaque) { auto* p = reinterpret_cast(opaque); return p->cert_verify_cb_(ctx); } -// static int TlsConnectionImpl::SSLSetCertCb(SSL* ssl, void* opaque) { auto* p = reinterpret_cast(opaque); return p->set_cert_cb_(ssl); @@ -317,6 +313,62 @@ std::vector TlsConnectionImpl::ReadFully(size_t size) { return buf; } +std::string TlsConnectionImpl::SSL_IO_Error(int error) { + switch (error) { + case SSL_ERROR_NONE: { + return "SSL_ERROR_NONE: nothing read but not an error?"; + } + case SSL_ERROR_SSL: { + return "SSL_ERROR_SSL: '"s + SSLErrorString() + "'"; + } + case SSL_ERROR_WANT_READ: { + return "SSL_ERROR_WANT_READ"; + } + case SSL_ERROR_WANT_WRITE: { + return "SSL_ERROR_WANT_WRITE"; + } + case SSL_ERROR_WANT_X509_LOOKUP: { + return "SSL_ERROR_WANT_X509_LOOKUP"; + } + case SSL_ERROR_SYSCALL: { + return "SSL_ERROR_SYSCALL: '"s + strerror(errno) + "'"; + } + case SSL_ERROR_ZERO_RETURN: { + return "SSL_ERROR_ZERO_RETURN"; + } + case SSL_ERROR_WANT_CONNECT: { + return "SSL_ERROR_WANT_CONNECT"; + } + case SSL_ERROR_WANT_ACCEPT: { + return "SSL_ERROR_WANT_ACCEPT"; + } + case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: { + return "SSL_ERROR_WANT_CHANNEL_ID_LOOKUP"; + } + case SSL_ERROR_PENDING_SESSION: { + return "SSL_ERROR_PENDING_SESSION"; + } + case SSL_ERROR_PENDING_CERTIFICATE: { + return "SSL_ERROR_PENDING_CERTIFICATE"; + } + case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION: { + return "SSL_ERROR_WANT_PRIVATE_KEY_OPERATION"; + } + case SSL_ERROR_PENDING_TICKET: { + return "SSL_ERROR_PENDING_TICKET"; + } + case SSL_ERROR_EARLY_DATA_REJECTED: { + return "SSL_ERROR_EARLY_DATA_REJECTED"; + } + case SSL_ERROR_WANT_CERTIFICATE_VERIFY: { + return "SSL_ERROR_WANT_CERTIFICATE_VERIFY"; + } + default: { + return "Unknown case: "s + std::to_string(error); + } + } +} + bool TlsConnectionImpl::ReadFully(void* buf, size_t size) { CHECK_GT(size, 0U); if (!ssl_) { @@ -330,7 +382,10 @@ bool TlsConnectionImpl::ReadFully(void* buf, size_t size) { int bytes_read = SSL_read(ssl_.get(), p8 + offset, std::min(static_cast(INT_MAX), size)); if (bytes_read <= 0) { - LOG(ERROR) << RoleToString() << "SSL_read failed [" << SSLErrorString() << "]"; + int error = SSL_get_error(ssl_.get(), bytes_read); + LOG(ERROR) << RoleToString() << "SSL_read failed [bytes_read=" << bytes_read + << ", io_error=" << SSL_IO_Error(error) << "]"; + return false; } size -= bytes_read; @@ -350,7 +405,9 @@ bool TlsConnectionImpl::WriteFully(std::string_view data) { int bytes_out = SSL_write(ssl_.get(), data.data(), std::min(static_cast(INT_MAX), data.size())); if (bytes_out <= 0) { - LOG(ERROR) << RoleToString() << "SSL_write failed [" << SSLErrorString() << "]"; + int error = SSL_get_error(ssl_.get(), bytes_out); + LOG(ERROR) << RoleToString() << "SSL_write failed [bytes_out=" << bytes_out + << ", io_error=" << SSL_IO_Error(error) << "]"; return false; } data = data.substr(bytes_out); @@ -359,7 +416,6 @@ bool TlsConnectionImpl::WriteFully(std::string_view data) { } } // namespace -// static std::unique_ptr TlsConnection::Create(TlsConnection::Role role, std::string_view cert, std::string_view priv_key, borrowed_fd fd) { @@ -369,7 +425,6 @@ std::unique_ptr TlsConnection::Create(TlsConnection::Role role, return std::make_unique(role, cert, priv_key, fd); } -// static bool TlsConnection::SetCertAndKey(SSL* ssl, std::string_view cert, std::string_view priv_key) { CHECK(ssl); // Note: declaring these in local scope is okay because diff --git a/base/cvd/adb/transport.cpp b/base/cvd/adb/transport.cpp index 07fbfb5bba0..09d7d20c37b 100644 --- a/base/cvd/adb/transport.cpp +++ b/base/cvd/adb/transport.cpp @@ -161,12 +161,12 @@ class ReconnectHandler { }; void ReconnectHandler::Start() { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); handler_thread_ = std::thread(&ReconnectHandler::Run, this); } void ReconnectHandler::Stop() { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); { std::lock_guard lock(reconnect_mutex_); running_ = false; @@ -184,7 +184,7 @@ void ReconnectHandler::Stop() { } void ReconnectHandler::TrackTransport(atransport* transport) { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); { std::lock_guard lock(reconnect_mutex_); if (!running_) return; @@ -223,7 +223,7 @@ void ReconnectHandler::Run() { // disconnect request. for (auto it = reconnect_queue_.begin(); it != reconnect_queue_.end();) { if (it->transport->kicked()) { - D("transport %s was kicked. giving up on it.", it->transport->serial.c_str()); + D("transport %s was kicked. giving up on it.", it->transport->name.c_str()); remove_transport(it->transport); it = reconnect_queue_.erase(it); } else { @@ -243,14 +243,14 @@ void ReconnectHandler::Run() { attempt = *reconnect_queue_.begin(); reconnect_queue_.erase(reconnect_queue_.begin()); } - D("attempting to reconnect %s", attempt.transport->serial.c_str()); + D("attempting to reconnect %s", attempt.transport->name.c_str()); switch (attempt.transport->Reconnect()) { case ReconnectResult::Retry: { - D("attempting to reconnect %s failed.", attempt.transport->serial.c_str()); + D("attempting to reconnect %s failed.", attempt.transport->name.c_str()); if (attempt.attempts_left == 0) { D("transport %s exceeded the number of retry attempts. giving up on it.", - attempt.transport->serial.c_str()); + attempt.transport->name.c_str()); remove_transport(attempt.transport); continue; } @@ -264,12 +264,12 @@ void ReconnectHandler::Run() { } case ReconnectResult::Success: - D("reconnection to %s succeeded.", attempt.transport->serial.c_str()); + D("reconnection to %s succeeded.", attempt.transport->name.c_str()); register_transport(attempt.transport); continue; case ReconnectResult::Abort: - D("cancelling reconnection attempt to %s.", attempt.transport->serial.c_str()); + D("cancelling reconnection attempt to %s.", attempt.transport->name.c_str()); remove_transport(attempt.transport); continue; } @@ -300,7 +300,7 @@ BlockingConnectionAdapter::BlockingConnectionAdapter(std::unique_ptr lock(mutex_); ScopedLockAssertion assume_locked(mutex_); @@ -342,11 +342,11 @@ bool BlockingConnectionAdapter::Start() { void BlockingConnectionAdapter::StartReadThread() { read_thread_ = std::thread([this]() { - LOG(INFO) << Serial() << ": read thread spawning"; + VLOG(ADB) << Serial() << ": read thread spawning"; while (true) { auto packet = std::make_unique(); if (!underlying_->Read(packet.get())) { - PLOG(INFO) << Serial() << ": read failed"; + VLOG(ADB) << Serial() << ": read failed"; break; } @@ -361,7 +361,7 @@ void BlockingConnectionAdapter::StartReadThread() { // handshake. So this read thread must stop and resume after the // handshake completes otherwise this will interfere in the process. if (got_stls_cmd) { - LOG(INFO) << Serial() << ": Received STLS packet. Stopping read thread."; + VLOG(ADB) << Serial() << ": Received STLS packet. Stopping read thread."; return; } } @@ -383,17 +383,17 @@ void BlockingConnectionAdapter::Reset() { { std::lock_guard lock(mutex_); if (!started_) { - LOG(INFO) << "BlockingConnectionAdapter(" << Serial() << "): not started"; + VLOG(ADB) << "BlockingConnectionAdapter(" << Serial() << "): not started"; return; } if (stopped_) { - LOG(INFO) << "BlockingConnectionAdapter(" << Serial() << "): already stopped"; + VLOG(ADB) << "BlockingConnectionAdapter(" << Serial() << "): already stopped"; return; } } - LOG(INFO) << "BlockingConnectionAdapter(" << Serial() << "): resetting"; + VLOG(ADB) << "BlockingConnectionAdapter(" << Serial() << "): resetting"; this->underlying_->Reset(); Stop(); } @@ -402,19 +402,19 @@ void BlockingConnectionAdapter::Stop() { { std::lock_guard lock(mutex_); if (!started_) { - LOG(INFO) << "BlockingConnectionAdapter(" << Serial() << "): not started"; + VLOG(ADB) << "BlockingConnectionAdapter(" << Serial() << "): not started"; return; } if (stopped_) { - LOG(INFO) << "BlockingConnectionAdapter(" << Serial() << "): already stopped"; + VLOG(ADB) << "BlockingConnectionAdapter(" << Serial() << "): already stopped"; return; } stopped_ = true; } - LOG(INFO) << "BlockingConnectionAdapter(" << Serial() << "): stopping"; + VLOG(ADB) << "BlockingConnectionAdapter(" << Serial() << "): stopping"; this->underlying_->Close(); this->cv_.notify_one(); @@ -432,7 +432,7 @@ void BlockingConnectionAdapter::Stop() { read_thread.join(); write_thread.join(); - LOG(INFO) << "BlockingConnectionAdapter(" << Serial() << "): stopped"; + VLOG(ADB) << "BlockingConnectionAdapter(" << Serial() << "): stopped"; std::call_once(this->error_flag_, [this]() { transport_->HandleError("requested stop"); }); } @@ -575,14 +575,14 @@ void send_packet(apacket* p, atransport* t) { p->msg.data_check = calculate_apacket_checksum(p); } - VLOG(TRANSPORT) << dump_packet(t->serial.c_str(), "to remote", p); + VLOG(TRANSPORT) << dump_packet(t->name.c_str(), "to remote", p); if (t == nullptr) { LOG(FATAL) << "Transport is null"; } if (t->Write(p) != 0) { - D("%s: failed to enqueue packet, closing transport", t->serial.c_str()); + D("%s: failed to enqueue packet, closing transport", t->name.c_str()); t->Kick(); } } @@ -737,7 +737,7 @@ void update_transports() { #endif // ADB_HOST static void fdevent_unregister_transport(atransport* t) { - VLOG(TRANSPORT) << "unregistering transport: " << t->serial; + VLOG(TRANSPORT) << "unregistering transport: " << t->name; { std::lock_guard lock(transport_lock); @@ -753,8 +753,7 @@ static void fdevent_unregister_transport(atransport* t) { static void fdevent_register_transport(atransport* t) { auto state = to_string(t->GetConnectionState()); - VLOG(TRANSPORT) << "registering: " << t->serial.c_str() << " state=" << state - << " type=" << t->type; + VLOG(TRANSPORT) << "registering: " << t->name << " state=" << state << " type=" << t->type; /* don't create transport threads for inaccessible devices */ if (t->GetConnectionState() != kCsNoPerm) { @@ -763,22 +762,22 @@ static void fdevent_register_transport(atransport* t) { #if ADB_HOST if (t->type == kTransportUsb && attached_devices.ShouldStartDetached(*t->connection().get())) { - VLOG(TRANSPORT) << "Force-detaching transport:" << t->serial; + VLOG(TRANSPORT) << "Force-detaching transport:" << t->name; t->SetConnectionState(kCsDetached); } - VLOG(TRANSPORT) << "transport:" << t->serial << "(" << state << ")"; + VLOG(TRANSPORT) << "transport:" << t->name << "(" << state << ")"; if (t->GetConnectionState() != kCsDetached) { - VLOG(TRANSPORT) << "Starting transport:" << t->serial; + VLOG(TRANSPORT) << "Starting transport:" << t->name; if (t->connection()->Start()) { send_connect(t); } else { - VLOG(TRANSPORT) << "transport:" << t->serial << " failed to start."; + VLOG(TRANSPORT) << "transport:" << t->name << " failed to start."; return; } } #else - VLOG(TRANSPORT) << "Starting transport:" << t->serial; + VLOG(TRANSPORT) << "Starting transport:" << t->name; t->connection()->Start(); #endif } @@ -841,7 +840,7 @@ static void remove_transport(atransport* transport) { } static void transport_destroy(atransport* t) { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); CHECK(t != nullptr); std::lock_guard lock(transport_lock); @@ -849,7 +848,7 @@ static void transport_destroy(atransport* t) { t->connection()->Stop(); #if ADB_HOST if (t->IsTcpDevice() && !t->kicked()) { - D("transport: %s destroy (attempting reconnection)", t->serial.c_str()); + D("transport: %s destroy (attempting reconnection)", t->name.c_str()); // We need to clear the transport's keys, so that on the next connection, it tries // again from the beginning. @@ -859,7 +858,7 @@ static void transport_destroy(atransport* t) { } #endif - D("transport: %s destroy (kicking and closing)", t->serial.c_str()); + D("transport: %s destroy (kicking and closing)", t->name.c_str()); remove_transport(t); } @@ -915,16 +914,16 @@ bool transport_server_owns_device(std::string_view dev_path, std::string_view se dev_path.compare(__transport_server_one_device) == 0; } -atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id, - bool* is_ambiguous, std::string* error_out, - bool accept_any_state) { +atransport* acquire_one_transport(TransportType type, const char* transport_name, + TransportId transport_id, bool* is_ambiguous, + std::string* error_out, bool accept_any_state) { atransport* result = nullptr; if (transport_id != 0) { *error_out = android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id); - } else if (serial) { - *error_out = android::base::StringPrintf("device '%s' not found", serial); + } else if (transport_name) { + *error_out = android::base::StringPrintf("device '%s' not found", transport_name); } else if (type == kTransportLocal) { *error_out = "no emulators found"; } else if (type == kTransportAny) { @@ -945,10 +944,10 @@ atransport* acquire_one_transport(TransportType type, const char* serial, Transp result = t; break; } - } else if (serial) { - if (t->MatchesTarget(serial)) { + } else if (transport_name) { + if (t->MatchesTarget(transport_name)) { if (result) { - *error_out = "more than one device with serial "s + serial; + *error_out = "more than one device with serial "s + transport_name; if (is_ambiguous) *is_ambiguous = true; result = nullptr; break; @@ -1062,14 +1061,14 @@ int atransport::Write(apacket* p) { void atransport::Reset() { if (!kicked_.exchange(true)) { - LOG(INFO) << "resetting transport " << this << " " << this->serial; + LOG(INFO) << "resetting transport " << this << " " << this->name; this->connection()->Reset(); } } void atransport::Kick() { if (!kicked_.exchange(true)) { - LOG(INFO) << "kicking transport " << this << " " << this->serial; + LOG(INFO) << "kicking transport " << this << " " << this->name; this->connection()->Stop(); } } @@ -1079,15 +1078,15 @@ ConnectionState atransport::GetConnectionState() const { } void atransport::SetConnectionState(ConnectionState state) { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); connection_state_ = state; update_transports(); } #if ADB_HOST bool atransport::Attach(std::string* error) { - D("%s: attach", serial.c_str()); - fdevent_check_looper(); + D("%s: attach", name.c_str()); + CHECK_LOOPER_THREAD(); { std::lock_guard lock(mutex_); @@ -1098,7 +1097,7 @@ bool atransport::Attach(std::string* error) { } if (GetConnectionState() != ConnectionState::kCsDetached) { - *error = android::base::StringPrintf("transport %s is not detached", serial.c_str()); + *error = android::base::StringPrintf("transport %s is not detached", name.c_str()); return false; } @@ -1116,8 +1115,8 @@ bool atransport::Attach(std::string* error) { } bool atransport::Detach(std::string* error) { - D("%s: detach", serial.c_str()); - fdevent_check_looper(); + D("%s: detach", name.c_str()); + CHECK_LOOPER_THREAD(); { std::lock_guard lock(mutex_); @@ -1128,7 +1127,7 @@ bool atransport::Detach(std::string* error) { } if (GetConnectionState() == ConnectionState::kCsDetached) { - *error = android::base::StringPrintf("transport %s is already detached", serial.c_str()); + *error = android::base::StringPrintf("transport %s is already detached", name.c_str()); return false; } @@ -1153,11 +1152,11 @@ void atransport::SetConnection(std::shared_ptr connection) { bool atransport::HandleRead(std::unique_ptr p) { if (!check_header(p.get(), this)) { - D("%s: remote read: bad header", serial.c_str()); + D("%s: remote read: bad header", name.c_str()); return false; } - VLOG(TRANSPORT) << dump_packet(serial.c_str(), "from remote", p.get()); + VLOG(TRANSPORT) << dump_packet(name.c_str(), "from remote", p.get()); apacket* packet = p.release(); // This needs to run on the looper thread since the associated fdevent @@ -1294,8 +1293,8 @@ void atransport::RunDisconnects() { #if ADB_HOST bool atransport::MatchesTarget(const std::string& target) const { - if (!serial.empty()) { - if (target == serial) { + if (!name.empty()) { + if (target == name) { return true; } else if (type == kTransportLocal) { // Local transports can match [tcp:|udp:][:port]. @@ -1310,8 +1309,7 @@ bool atransport::MatchesTarget(const std::string& target) const { // Parse our |serial| and the given |target| to check if the hostnames and ports match. std::string serial_host, error; int serial_port = -1; - if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, - &error)) { + if (android::base::ParseNetAddress(name, &serial_host, &serial_port, nullptr, &error)) { // |target| may omit the port to default to ours. std::string target_host; int target_port = serial_port; @@ -1381,7 +1379,7 @@ static std::string transportListToProto(const std::list& sorted_tra adb::proto::Devices devices; for (const auto& t : sorted_transport_list) { auto* device = devices.add_device(); - device->set_serial(t->serial.c_str()); + device->set_serial(t->name.c_str()); device->set_connection_type(t->type == kTransportUsb ? adb::proto::ConnectionType::USB : adb::proto::ConnectionType::SOCKET); device->set_state(adbStateFromProto(t->GetConnectionState())); @@ -1415,7 +1413,7 @@ static void append_transport_info(std::string* result, const char* key, const st } static void append_transport(const atransport* t, std::string* result, bool long_listing) { - std::string serial = t->serial; + std::string serial = t->name; if (serial.empty()) { serial = "(no serial number)"; } @@ -1458,7 +1456,7 @@ std::string list_transports(TrackerOutputType outputType) { if (x->type != y->type) { return x->type < y->type; } - return x->serial < y->serial; + return x->name < y->name; }); switch (outputType) { @@ -1492,46 +1490,55 @@ void close_usb_devices(bool reset) { } #endif -bool validate_transport_list(const std::list& list, const std::string& serial, - atransport* t, int* error) { +// Return true is the transport_name is not part of the transport list +static bool validate_transport_list(const std::list& list, + const std::string& transport_name, atransport* t, int* error) { for (const auto& transport : list) { - if (serial == transport->serial) { - const std::string list_name(&list == &pending_list ? "pending" : "transport"); - VLOG(TRANSPORT) << "socket transport " << transport->serial << " is already in the " - << list_name << " list and fails to register"; - delete t; - if (error) *error = EALREADY; - return false; + if (transport_name != transport->name) { + continue; } + + const std::string list_name(&list == &pending_list ? "pending" : "transport"); + VLOG(TRANSPORT) << "socket transport " << transport->name << " is already in the " + << list_name << " list and fails to register"; + delete t; + if (error) { + *error = EALREADY; + } + return false; } return true; } -bool register_socket_transport(unique_fd s, std::string serial, int port, bool is_emulator, +bool register_socket_transport(unique_fd s, std::string transport_name, int port, bool is_emulator, atransport::ReconnectCallback reconnect, bool use_tls, int* error) { #if ADB_HOST // Below in this method, we block up to 10s on the waitable. This should never run on the // fdevent thread. - fdevent_check_not_looper(); + CHECK_NOT_LOOPER_THREAD(); #endif atransport* t = new atransport(kTransportLocal, std::move(reconnect), kCsOffline); t->use_tls = use_tls; - t->serial = std::move(serial); + t->name = std::move(transport_name); - D("transport: %s init'ing for socket %d, on port %d", t->serial.c_str(), s.get(), port); + D("transport: %s init'ing for socket %d, on port %d", t->name.c_str(), s.get(), port); if (init_socket_transport(t, std::move(s), port, is_emulator) < 0) { delete t; if (error) *error = errno; return false; } + // Before proceeding with this transport, make sure it is not already present in our established + // or pending list of transports. This is done to reject attempts to connect to a device we are + // already connected to (e.g.: An adb device with a tcp port 5555 should only be connected to + // once. std::unique_lock lock(transport_lock); - if (!validate_transport_list(pending_list, t->serial, t, error)) { + if (!validate_transport_list(pending_list, t->name, t, error)) { return false; } - if (!validate_transport_list(transport_list, t->serial, t, error)) { + if (!validate_transport_list(transport_list, t->name, t, error)) { return false; } @@ -1564,12 +1571,12 @@ bool register_socket_transport(unique_fd s, std::string serial, int port, bool i } #if ADB_HOST -atransport* find_transport(const char* serial) { +atransport* find_transport(const char* transport_name) { atransport* result = nullptr; std::lock_guard lock(transport_lock); for (auto& t : transport_list) { - if (strcmp(serial, t->serial.c_str()) == 0) { + if (strcmp(transport_name, t->name.c_str()) == 0) { result = t; break; } @@ -1593,11 +1600,11 @@ void kick_all_tcp_devices() { } #if ADB_HOST -void register_libusb_transport(std::shared_ptr connection, const char* serial, +void register_libusb_transport(std::shared_ptr connection, const char* transport_name, const char* devpath, unsigned writeable) { atransport* t = new atransport(kTransportUsb, writeable ? kCsOffline : kCsNoPerm); - if (serial) { - t->serial = serial; + if (transport_name) { + t->name = transport_name; } if (devpath) { t->devpath = devpath; @@ -1620,7 +1627,7 @@ void register_usb_transport(usb_handle* usb, const char* serial, const char* dev D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : ""); init_usb_transport(t, usb); if (serial) { - t->serial = serial; + t->name = serial; } if (devpath) { @@ -1651,7 +1658,7 @@ void unregister_usb_transport(usb_handle* usb) { // - adb reverse --remove tcp: : responds OKAY // - adb reverse --remove-all : responds OKAY void atransport::UpdateReverseConfig(std::string_view service_addr) { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); if (!android::base::ConsumePrefix(&service_addr, "reverse:")) { return; } @@ -1691,7 +1698,7 @@ void atransport::UpdateReverseConfig(std::string_view service_addr) { // Is this an authorized :connect request? bool atransport::IsReverseConfigured(const std::string& local_addr) { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); for (const auto& [remote, local] : reverse_forwards_) { if (local == local_addr) { return true; diff --git a/base/cvd/adb/transport.h b/base/cvd/adb/transport.h index 08f034cd5ea..365f3c4987b 100644 --- a/base/cvd/adb/transport.h +++ b/base/cvd/adb/transport.h @@ -319,8 +319,12 @@ class atransport : public enable_weak_from_this { bool online = false; TransportType type = kTransportAny; - // Used to identify transports for clients. - std::string serial; + // Uniquely identify a transport, the name can be freely picked. Default values: + // USB: The serial-number of the device (43081FDAS000ST) + // TCP: IP:PORT (192.168.86.43:5555) + // TLS: Domain Name (adb-43081FDAS000ST-XKzA7F._adb-tls-connect._tcp) + std::string name; + std::string product; std::string model; std::string device; @@ -350,7 +354,7 @@ class atransport : public enable_weak_from_this { char token[TOKEN_SIZE] = {}; size_t failed_auth_attempts = 0; - std::string serial_name() const { return !serial.empty() ? serial : ""; } + std::string serial_name() const { return !name.empty() ? name : ""; } void update_version(int version, size_t payload); int get_protocol_version() const; @@ -465,15 +469,15 @@ bool transport_server_owns_device(std::string_view dev_path, std::string_view se /* * Obtain a transport from the available transports. - * If serial is non-null then only the device with that serial will be chosen. + * If transport_name is non-null then only the device with that name will be chosen. * If transport_id is non-zero then only the device with that transport ID will be chosen. * If multiple devices/emulators would match, *is_ambiguous (if non-null) * is set to true and nullptr returned. * If no suitable transport is found, error is set and nullptr returned. */ -atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id, - bool* is_ambiguous, std::string* error_out, - bool accept_any_state = false); +atransport* acquire_one_transport(TransportType type, const char* transport_name, + TransportId transport_id, bool* is_ambiguous, + std::string* error_out, bool accept_any_state = false); void kick_transport(atransport* t, bool reset = false); void update_transports(); @@ -485,7 +489,7 @@ void init_reconnect_handler(); void init_mdns_transport_discovery(); #if ADB_HOST -atransport* find_transport(const char* serial); +atransport* find_transport(const char* transport_name); void kick_all_tcp_devices(); #endif @@ -503,7 +507,7 @@ void register_transport(atransport* transport); #if ADB_HOST void init_usb_transport(atransport* t, usb_handle* usb); -void register_libusb_transport(std::shared_ptr connection, const char* serial, +void register_libusb_transport(std::shared_ptr connection, const char* transport_name, const char* devpath, unsigned writable); void register_usb_transport(usb_handle* h, const char* serial, const char* devpath, @@ -511,16 +515,21 @@ void register_usb_transport(usb_handle* h, const char* serial, const char* devpa // This should only be used for transports with connection_state == kCsNoPerm. void unregister_usb_transport(usb_handle* usb); -#endif +bool connect_emulator(int port); +int connect_emulator_arbitrary_ports(int console_port, int adb_port, std::string* error); /* Connect to a network address and register it as a device */ void connect_device(const std::string& address, std::string* response); +#endif + /* initialize a transport object's func pointers and state */ -int init_socket_transport(atransport* t, unique_fd s, int port, bool is_emulator); +int init_socket_transport(atransport* t, unique_fd fd, int port, bool is_emulator); -/* cause new transports to be init'd and added to the list */ -bool register_socket_transport(unique_fd s, std::string serial, int port, bool is_emulator, +// Cause new transports to be init'd and added to the list. +// The transport name uniquely identify a transport. An attempt to register a transport with a name +// already present in our pending/established list will be rejected. +bool register_socket_transport(unique_fd s, std::string transport_name, int port, bool is_emulator, atransport::ReconnectCallback reconnect, bool use_tls, int* error = nullptr); diff --git a/base/cvd/adb/types.h b/base/cvd/adb/types.h index 6b8bbfdd5c0..ce88118ed15 100644 --- a/base/cvd/adb/types.h +++ b/base/cvd/adb/types.h @@ -333,12 +333,12 @@ struct weak_ptr { } T* get() const { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); return ptr_; } void reset(T* ptr = nullptr) { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); if (ptr == ptr_) { return; @@ -364,7 +364,7 @@ template struct enable_weak_from_this { ~enable_weak_from_this() { if (!weak_ptrs_.empty()) { - fdevent_check_looper(); + CHECK_LOOPER_THREAD(); for (auto& weak : weak_ptrs_) { weak->ptr_ = nullptr; } diff --git a/base/cvd/build_external/build_external.MODULE.bazel b/base/cvd/build_external/build_external.MODULE.bazel index 0795d46ebd6..6795c5b2feb 100644 --- a/base/cvd/build_external/build_external.MODULE.bazel +++ b/base/cvd/build_external/build_external.MODULE.bazel @@ -34,7 +34,6 @@ include("//build_external/libeigen:libeigen.MODULE.bazel") include("//build_external/libffi:libffi.MODULE.bazel") include("//build_external/libjpeg_turbo:libjpeg_turbo.MODULE.bazel") include("//build_external/libnl:libnl.MODULE.bazel") -include("//build_external/libopenscreen:libopenscreen.MODULE.bazel") include("//build_external/libpffft:libpffft.MODULE.bazel") include("//build_external/libsrtp2:libsrtp2.MODULE.bazel") include("//build_external/libusb:libusb.MODULE.bazel") diff --git a/base/cvd/build_external/libopenscreen/BUILD.bazel b/base/cvd/build_external/libopenscreen/BUILD.bazel deleted file mode 100644 index 287419ab07b..00000000000 --- a/base/cvd/build_external/libopenscreen/BUILD.bazel +++ /dev/null @@ -1,5 +0,0 @@ -package( - default_visibility = [ - "@libopenscreen//:__subpackages__", - ], -) diff --git a/base/cvd/build_external/libopenscreen/BUILD.libopenscreen.bazel b/base/cvd/build_external/libopenscreen/BUILD.libopenscreen.bazel deleted file mode 100644 index d2cf0e0ed1b..00000000000 --- a/base/cvd/build_external/libopenscreen/BUILD.libopenscreen.bazel +++ /dev/null @@ -1,200 +0,0 @@ -load("@rules_cc//cc:cc_library.bzl", "cc_library") -load("@//:build_variables.bzl", "COPTS") - -package( - default_visibility = [ - "@//:android_cuttlefish", - ], -) - -cc_library( - name = "libopenscreen", - srcs = [ - "discovery/dnssd/impl/constants.h", - "discovery/dnssd/impl/conversion_layer.cc", - "discovery/dnssd/impl/conversion_layer.h", - "discovery/dnssd/impl/dns_data_graph.cc", - "discovery/dnssd/impl/dns_data_graph.h", - "discovery/dnssd/impl/instance_key.cc", - "discovery/dnssd/impl/instance_key.h", - "discovery/dnssd/impl/network_interface_config.cc", - "discovery/dnssd/impl/network_interface_config.h", - "discovery/dnssd/impl/publisher_impl.cc", - "discovery/dnssd/impl/publisher_impl.h", - "discovery/dnssd/impl/querier_impl.cc", - "discovery/dnssd/impl/querier_impl.h", - "discovery/dnssd/impl/service_dispatcher.cc", - "discovery/dnssd/impl/service_dispatcher.h", - "discovery/dnssd/impl/service_instance.cc", - "discovery/dnssd/impl/service_instance.h", - "discovery/dnssd/impl/service_key.cc", - "discovery/dnssd/impl/service_key.h", - "discovery/dnssd/public/dns_sd_instance.cc", - "discovery/dnssd/public/dns_sd_instance_endpoint.cc", - "discovery/dnssd/public/dns_sd_txt_record.cc", - "discovery/mdns/mdns_domain_confirmed_provider.h", - "discovery/mdns/mdns_probe.cc", - "discovery/mdns/mdns_probe.h", - "discovery/mdns/mdns_probe_manager.cc", - "discovery/mdns/mdns_probe_manager.h", - "discovery/mdns/mdns_publisher.cc", - "discovery/mdns/mdns_publisher.h", - "discovery/mdns/mdns_querier.cc", - "discovery/mdns/mdns_querier.h", - "discovery/mdns/mdns_random.h", - "discovery/mdns/mdns_reader.cc", - "discovery/mdns/mdns_reader.h", - "discovery/mdns/mdns_receiver.cc", - "discovery/mdns/mdns_receiver.h", - "discovery/mdns/mdns_record_changed_callback.h", - "discovery/mdns/mdns_records.cc", - "discovery/mdns/mdns_records.h", - "discovery/mdns/mdns_responder.cc", - "discovery/mdns/mdns_responder.h", - "discovery/mdns/mdns_sender.cc", - "discovery/mdns/mdns_sender.h", - "discovery/mdns/mdns_service_impl.cc", - "discovery/mdns/mdns_service_impl.h", - "discovery/mdns/mdns_trackers.cc", - "discovery/mdns/mdns_trackers.h", - "discovery/mdns/mdns_writer.cc", - "discovery/mdns/mdns_writer.h", - "discovery/mdns/public/mdns_service.cc", - "platform/api/udp_socket.cc", - "platform/base/error.cc", - "platform/base/interface_info.cc", - "platform/base/ip_address.cc", - "platform/base/udp_packet.cc", - "platform/impl/scoped_pipe.h", - "platform/impl/time.cc", - "platform/impl/network_interface.cc", - "platform/impl/network_interface.h", - "platform/impl/network_interface_linux.cc", - "third_party/abseil/src/absl/algorithm/algorithm.h", - "third_party/abseil/src/absl/base/attributes.h", - "third_party/abseil/src/absl/base/casts.h", - "third_party/abseil/src/absl/base/config.h", - "third_party/abseil/src/absl/base/dynamic_annotations.h", - "third_party/abseil/src/absl/base/log_severity.h", - "third_party/abseil/src/absl/base/macros.h", - "third_party/abseil/src/absl/base/optimization.h", - "third_party/abseil/src/absl/base/options.h", - "third_party/abseil/src/absl/base/policy_checks.h", - "third_party/abseil/src/absl/base/port.h", - "third_party/abseil/src/absl/base/internal/atomic_hook.h", - "third_party/abseil/src/absl/base/internal/bits.h", - "third_party/abseil/src/absl/base/internal/dynamic_annotations.h", - "third_party/abseil/src/absl/base/internal/endian.h", - "third_party/abseil/src/absl/base/internal/identity.h", - "third_party/abseil/src/absl/base/internal/inline_variable.h", - "third_party/abseil/src/absl/base/internal/invoke.h", - "third_party/abseil/src/absl/base/internal/raw_logging.cc", - "third_party/abseil/src/absl/base/internal/raw_logging.h", - "third_party/abseil/src/absl/base/internal/throw_delegate.cc", - "third_party/abseil/src/absl/base/internal/throw_delegate.h", - "third_party/abseil/src/absl/base/internal/unaligned_access.h", - "third_party/abseil/src/absl/container/fixed_array.h", - "third_party/abseil/src/absl/container/internal/compressed_tuple.h", - "third_party/abseil/src/absl/hash/hash.h", - "third_party/abseil/src/absl/hash/internal/city.cc", - "third_party/abseil/src/absl/hash/internal/city.h", - "third_party/abseil/src/absl/hash/internal/hash.cc", - "third_party/abseil/src/absl/hash/internal/hash.h", - "third_party/abseil/src/absl/memory/memory.h", - "third_party/abseil/src/absl/meta/type_traits.h", - "third_party/abseil/src/absl/numeric/int128.cc", - "third_party/abseil/src/absl/numeric/int128.h", - "third_party/abseil/src/absl/numeric/int128_have_intrinsic.inc", - "third_party/abseil/src/absl/strings/ascii.cc", - "third_party/abseil/src/absl/strings/ascii.h", - "third_party/abseil/src/absl/strings/charconv.cc", - "third_party/abseil/src/absl/strings/charconv.h", - "third_party/abseil/src/absl/strings/escaping.cc", - "third_party/abseil/src/absl/strings/escaping.h", - "third_party/abseil/src/absl/strings/internal/char_map.h", - "third_party/abseil/src/absl/strings/internal/charconv_bigint.cc", - "third_party/abseil/src/absl/strings/internal/charconv_bigint.h", - "third_party/abseil/src/absl/strings/internal/charconv_parse.cc", - "third_party/abseil/src/absl/strings/internal/charconv_parse.h", - "third_party/abseil/src/absl/strings/internal/escaping.cc", - "third_party/abseil/src/absl/strings/internal/escaping.h", - "third_party/abseil/src/absl/strings/internal/memutil.cc", - "third_party/abseil/src/absl/strings/internal/memutil.h", - "third_party/abseil/src/absl/strings/internal/ostringstream.h", - "third_party/abseil/src/absl/strings/internal/resize_uninitialized.h", - "third_party/abseil/src/absl/strings/internal/str_join_internal.h", - "third_party/abseil/src/absl/strings/internal/str_split_internal.h", - "third_party/abseil/src/absl/strings/internal/utf8.cc", - "third_party/abseil/src/absl/strings/internal/utf8.h", - "third_party/abseil/src/absl/strings/match.cc", - "third_party/abseil/src/absl/strings/match.h", - "third_party/abseil/src/absl/strings/numbers.cc", - "third_party/abseil/src/absl/strings/numbers.h", - "third_party/abseil/src/absl/strings/str_cat.cc", - "third_party/abseil/src/absl/strings/str_cat.h", - "third_party/abseil/src/absl/strings/str_join.h", - "third_party/abseil/src/absl/strings/str_replace.cc", - "third_party/abseil/src/absl/strings/str_replace.h", - "third_party/abseil/src/absl/strings/str_split.cc", - "third_party/abseil/src/absl/strings/str_split.h", - "third_party/abseil/src/absl/strings/string_view.cc", - "third_party/abseil/src/absl/strings/string_view.h", - "third_party/abseil/src/absl/strings/strip.h", - "third_party/abseil/src/absl/strings/substitute.cc", - "third_party/abseil/src/absl/strings/substitute.h", - "third_party/abseil/src/absl/types/bad_optional_access.cc", - "third_party/abseil/src/absl/types/bad_optional_access.h", - "third_party/abseil/src/absl/types/bad_variant_access.cc", - "third_party/abseil/src/absl/types/bad_variant_access.h", - "third_party/abseil/src/absl/types/internal/span.h", - "third_party/abseil/src/absl/types/optional.h", - "third_party/abseil/src/absl/types/span.h", - "third_party/abseil/src/absl/types/variant.h", - "third_party/abseil/src/absl/utility/utility.h", - "util/alarm.cc", - "util/big_endian.cc", - ], - includes = [ - ".", - "third_party/abseil/src", - ], - hdrs = [ - "discovery/common/config.h", - "discovery/common/reporting_client.h", - "discovery/dnssd/public/dns_sd_instance.h", - "discovery/dnssd/public/dns_sd_instance_endpoint.h", - "discovery/dnssd/public/dns_sd_publisher.h", - "discovery/dnssd/public/dns_sd_querier.h", - "discovery/dnssd/public/dns_sd_service.h", - "discovery/dnssd/public/dns_sd_txt_record.h", - "discovery/mdns/public/mdns_constants.h", - "discovery/mdns/public/mdns_service.h", - "discovery/public/dns_sd_service_factory.h", - "discovery/public/dns_sd_service_watcher.h", - "platform/api/logging.h", - "platform/api/network_interface.h", - "platform/api/serial_delete_ptr.h", - "platform/api/task_runner.h", - "platform/api/time.h", - "platform/api/udp_socket.h", - "platform/base/error.h", - "platform/base/interface_info.h", - "platform/base/ip_address.h", - "platform/base/macros.h", - "platform/base/trace_logging_types.h", - "platform/base/trivial_clock_traits.h", - "platform/base/udp_packet.h", - "util/alarm.h", - "util/big_endian.h", - "util/chrono_helpers.h", - "util/hashing.h", - "util/osp_logging.h", - "util/std_util.h", - "util/stringprintf.h", - "util/trace_logging.h", - ], - cxxopts = COPTS + [ - "-fno-exceptions", - "-Wno-vla-cxx-extension", - ], -) diff --git a/base/cvd/build_external/libopenscreen/PATCH.bazelignore.patch b/base/cvd/build_external/libopenscreen/PATCH.bazelignore.patch deleted file mode 100644 index 25d3081a501..00000000000 --- a/base/cvd/build_external/libopenscreen/PATCH.bazelignore.patch +++ /dev/null @@ -1,5 +0,0 @@ -diff '--color=auto' -Nudr -U3 a/.bazelignore b/.bazelignore ---- a/.bazelignore 1969-12-31 16:00:00.000000000 -0800 -+++ b/.bazelignore 2025-05-09 11:21:50.404669685 -0700 -@@ -0,0 +1 @@ -+third_party/abseil/src/ diff --git a/base/cvd/build_external/libopenscreen/libopenscreen.MODULE.bazel b/base/cvd/build_external/libopenscreen/libopenscreen.MODULE.bazel deleted file mode 100644 index c5ca326cbf0..00000000000 --- a/base/cvd/build_external/libopenscreen/libopenscreen.MODULE.bazel +++ /dev/null @@ -1,13 +0,0 @@ -git_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") - -# Android version is slightly forked from the chromium upstream -git_repository( - name = "libopenscreen", - build_file = "@//build_external/libopenscreen:BUILD.libopenscreen.bazel", - commit = "3f982cf4871df8771c9d4abe6e9a6f8d829b2736", - patch_args = ["-p1"], - patches = [ - "@//build_external/libopenscreen:PATCH.bazelignore.patch", - ], - remote = "https://android.googlesource.com/platform/external/openscreen", -) diff --git a/base/cvd/fastboot/BUILD.bazel b/base/cvd/fastboot/BUILD.bazel index 9de958d514e..bf1df8da4b0 100644 --- a/base/cvd/fastboot/BUILD.bazel +++ b/base/cvd/fastboot/BUILD.bazel @@ -22,6 +22,8 @@ cc_binary( "filesystem.h", "fs.cpp", "fs.h", + "h2h_workaround.cpp", + "h2h_workaround.h", "libstorage_literals/storage_literals/storage_literals.h", "main.cpp", "result.h", diff --git a/base/cvd/fastboot/fastboot.cpp b/base/cvd/fastboot/fastboot.cpp index b22ba03fcae..0f6e5d69b60 100644 --- a/base/cvd/fastboot/fastboot.cpp +++ b/base/cvd/fastboot/fastboot.cpp @@ -76,6 +76,7 @@ #include "fastboot_driver.h" #include "fastboot_driver_interface.h" #include "fs.h" +#include "h2h_workaround.h" #include "storage.h" #include "task.h" #include "tcp.h" @@ -90,6 +91,8 @@ using android::base::ReadFully; using android::base::Split; using android::base::Trim; using android::base::unique_fd; +using fastboot::IFastBootDriver; +using fastboot::SUCCESS; using namespace std::placeholders; #define FASTBOOT_INFO_VERSION 1 @@ -111,8 +114,6 @@ static std::string g_dtb_path; static bool g_disable_verity = false; static bool g_disable_verification = false; -fastboot::FastBootDriver* fb = nullptr; - static std::vector images = { // clang-format off { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical }, @@ -211,10 +212,12 @@ static void Status(const std::string& message) { last_start_time = now(); } -static void Epilog(int status) { +static void Epilog(IFastBootDriver* fb, int status, bool crash_on_error) { if (status) { fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str()); - die("Command failed"); + if (crash_on_error) { + die("Command failed"); + } } else { double split = now(); fprintf(stderr, "OKAY [%7.3fs]\n", (split - last_start_time)); @@ -288,6 +291,11 @@ static void PrintDevice(const char* local_serial, const char* status = nullptr, static int list_devices_callback(usb_ifc_info* info) { if (match_fastboot_with_serial(info, nullptr) == 0) { + // As a temporary workaround, we disregard any h2h device. + if (is_h2h_device(info)) { + return -1; + } + std::string serial = info->serial_number; std::string interface = info->interface; if (interface.empty()) { @@ -320,7 +328,7 @@ Result ParseNetworkSerial(const std::string& seria net_address = serial.c_str() + strlen("udp:"); port = udp::kDefaultPort; } else { - return Error(FastbootError::Type::NETWORK_SERIAL_WRONG_PREFIX) + return Error(FastbootError::NETWORK_SERIAL_WRONG_PREFIX) << "protocol prefix ('tcp:' or 'udp:') is missed: " << serial << ". " << "Expected address format:\n" << ":
: (tcp:localhost:5554)"; @@ -329,7 +337,7 @@ Result ParseNetworkSerial(const std::string& seria std::string error; std::string host; if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) { - return Error(FastbootError::Type::NETWORK_SERIAL_WRONG_ADDRESS) + return Error(FastbootError::NETWORK_SERIAL_WRONG_ADDRESS) << "invalid network address '" << net_address << "': " << error; } @@ -363,8 +371,7 @@ static std::unique_ptr open_device(const char* local_serial, bool wai if (!transport && announce) { LOG(ERROR) << "error: " << error; } - } else if (network_serial.error().code() == - FastbootError::Type::NETWORK_SERIAL_WRONG_PREFIX) { + } else if (network_serial.error().code() == FastbootError::NETWORK_SERIAL_WRONG_PREFIX) { // WRONG_PREFIX is special because it happens when user wants to communicate with USB // device transport = usb_open(match_fastboot(local_serial)); @@ -855,8 +862,8 @@ static unique_fd UnzipToFile(unique_zip_t& zip, const char* entry_name) { return fd; } -static bool CheckRequirement(const std::string& cur_product, const std::string& var, - const std::string& product, bool invert, +static bool CheckRequirement(IFastBootDriver* fb, const std::string& cur_product, + const std::string& var, const std::string& product, bool invert, const std::vector& options) { Status("Checking '" + var + "'"); @@ -949,7 +956,7 @@ bool ParseRequirementLine(const std::string& line, std::string* name, std::strin // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them, // missing out new partitions. A device with new partitions can use "partition-exists" to // override the fields `optional_if_no_image` in the `images` array. -static void HandlePartitionExists(const std::vector& options) { +static void HandlePartitionExists(IFastBootDriver* fb, const std::vector& options) { const std::string& partition_name = options[0]; std::string has_slot; if (fb->GetVar("has-slot:" + partition_name, &has_slot) != fastboot::SUCCESS || @@ -969,7 +976,7 @@ static void HandlePartitionExists(const std::vector& options) { } } -static void CheckRequirements(const std::string& data, bool force_flash) { +static void CheckRequirements(IFastBootDriver* fb, const std::string& data, bool force_flash) { std::string cur_product; if (fb->GetVar("product", &cur_product) != fastboot::SUCCESS) { fprintf(stderr, "getvar:product FAILED (%s)\n", fb->Error().c_str()); @@ -991,9 +998,9 @@ static void CheckRequirements(const std::string& data, bool force_flash) { continue; } if (name == "partition-exists") { - HandlePartitionExists(options); + HandlePartitionExists(fb, options); } else { - bool met = CheckRequirement(cur_product, name, product, invert, options); + bool met = CheckRequirement(fb, cur_product, name, product, invert, options); if (!met) { if (!force_flash) { die("requirements not met!"); @@ -1005,7 +1012,8 @@ static void CheckRequirements(const std::string& data, bool force_flash) { } } -static void DisplayVarOrError(const std::string& label, const std::string& var) { +static void DisplayVarOrError(IFastBootDriver* fb, const std::string& label, + const std::string& var) { std::string value; if (fb->GetVar(var, &value) != fastboot::SUCCESS) { @@ -1016,40 +1024,14 @@ static void DisplayVarOrError(const std::string& label, const std::string& var) fprintf(stderr, "%s: %s\n", label.c_str(), value.c_str()); } -static void DumpInfo() { +void FlashAllTool::DumpInfo() { fprintf(stderr, "--------------------------------------------\n"); - DisplayVarOrError("Bootloader Version...", "version-bootloader"); - DisplayVarOrError("Baseband Version.....", "version-baseband"); - DisplayVarOrError("Serial Number........", "serialno"); + DisplayVarOrError(fp_->fb, "Bootloader Version...", "version-bootloader"); + DisplayVarOrError(fp_->fb, "Baseband Version.....", "version-baseband"); + DisplayVarOrError(fp_->fb, "Serial Number........", "serialno"); fprintf(stderr, "--------------------------------------------\n"); } -std::vector resparse_file(sparse_file* s, int64_t max_size) { - if (max_size <= 0 || max_size > std::numeric_limits::max()) { - die("invalid max size %" PRId64, max_size); - } - - const int files = sparse_file_resparse(s, max_size, nullptr, 0); - if (files < 0) die("Failed to compute resparse boundaries"); - - auto temp = std::make_unique(files); - const int rv = sparse_file_resparse(s, max_size, temp.get(), files); - if (rv < 0) die("Failed to resparse"); - - std::vector out_s; - for (int i = 0; i < files; i++) { - out_s.emplace_back(temp[i], sparse_file_destroy); - } - return out_s; -} - -static std::vector load_sparse_files(int fd, int64_t max_size) { - SparsePtr s(sparse_file_import_auto(fd, false, true), sparse_file_destroy); - if (!s) die("cannot sparse read file"); - - return resparse_file(s.get(), max_size); -} - static uint64_t get_uint_var(const char* var_name, fastboot::IFastBootDriver* fb) { std::string value_str; if (fb->GetVar(var_name, &value_str) != fastboot::SUCCESS || value_str.empty()) { @@ -1093,43 +1075,30 @@ int64_t get_sparse_limit(int64_t size, const FlashingPlan* fp) { return 0; } -static bool load_buf_fd(unique_fd fd, struct fastboot_buffer* buf, const FlashingPlan* fp) { - int64_t sz = get_file_size(fd); - if (sz == -1) { +static bool load_buf_fd(unique_fd fd, struct fastboot_buffer* buf) { + buf->sz = get_file_size(fd); + if (buf->sz == -1) { return false; } - if (sparse_file* s = sparse_file_import(fd.get(), false, false)) { - buf->image_size = sparse_file_len(s, false, false); + if (SparsePtr s(sparse_file_import(fd.get(), false, false), sparse_file_destroy); s) { + buf->image_size = sparse_file_len(s.get(), false, false); if (buf->image_size < 0) { LOG(ERROR) << "Could not compute length of sparse file"; return false; } - sparse_file_destroy(s); buf->file_type = FB_BUFFER_SPARSE; } else { - buf->image_size = sz; + buf->image_size = buf->sz; buf->file_type = FB_BUFFER_FD; } lseek(fd.get(), 0, SEEK_SET); - int64_t limit = get_sparse_limit(sz, fp); buf->fd = std::move(fd); - if (limit) { - buf->files = load_sparse_files(buf->fd.get(), limit); - if (buf->files.empty()) { - return false; - } - buf->type = FB_BUFFER_SPARSE; - } else { - buf->type = FB_BUFFER_FD; - buf->sz = sz; - } - return true; } -static bool load_buf(const char* fname, struct fastboot_buffer* buf, const FlashingPlan* fp) { +static bool load_buf(const char* fname, struct fastboot_buffer* buf) { unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY))); if (fd == -1) { @@ -1145,7 +1114,7 @@ static bool load_buf(const char* fname, struct fastboot_buffer* buf, const Flash return false; } - return load_buf_fd(std::move(fd), buf, fp); + return load_buf_fd(std::move(fd), buf); } static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf, bool vbmeta_in_boot) { @@ -1200,7 +1169,7 @@ static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf, bool vbmeta_in_bo lseek(buf->fd.get(), 0, SEEK_SET); } -static bool has_vbmeta_partition() { +static bool has_vbmeta_partition(IFastBootDriver* fb) { std::string partition_type; return fb->GetVar("partition-type:vbmeta", &partition_type) == fastboot::SUCCESS || fb->GetVar("partition-type:vbmeta_a", &partition_type) == fastboot::SUCCESS || @@ -1215,15 +1184,15 @@ static bool is_vbmeta_partition(const std::string& partition) { // Note: this only works in userspace fastboot. In the bootloader, use // should_flash_in_userspace(). -bool is_logical(const std::string& partition) { +bool is_logical(IFastBootDriver* fb, const std::string& partition) { std::string value; return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes"; } -static uint64_t get_partition_size(const std::string& partition) { +static uint64_t get_partition_size(IFastBootDriver* fb, const std::string& partition) { std::string partition_size_str; if (fb->GetVar("partition-size:" + partition, &partition_size_str) != fastboot::SUCCESS) { - if (!is_logical(partition)) { + if (!is_logical(fb, partition)) { return 0; } die("cannot get partition size for %s", partition.c_str()); @@ -1232,7 +1201,7 @@ static uint64_t get_partition_size(const std::string& partition) { partition_size_str = fb_fix_numeric_var(partition_size_str); uint64_t partition_size; if (!android::base::ParseUint(partition_size_str, &partition_size)) { - if (!is_logical(partition)) { + if (!is_logical(fb, partition)) { return 0; } die("Couldn't parse partition size '%s'.", partition_size_str.c_str()); @@ -1240,10 +1209,10 @@ static uint64_t get_partition_size(const std::string& partition) { return partition_size; } -static void copy_avb_footer(const ImageSource* source, const std::string& partition, +static void copy_avb_footer(const FlashingPlan* fp, const std::string& partition, struct fastboot_buffer* buf) { - if (buf->sz < AVB_FOOTER_SIZE || is_logical(partition) || - should_flash_in_userspace(source, partition)) { + if (buf->sz < AVB_FOOTER_SIZE || is_logical(fp->fb, partition) || + should_flash_in_userspace(fp->source.get(), partition)) { return; } @@ -1256,7 +1225,7 @@ static void copy_avb_footer(const ImageSource* source, const std::string& partit } // If overflows and negative, it should be < buf->sz. - int64_t partition_size = static_cast(get_partition_size(partition)); + int64_t partition_size = static_cast(get_partition_size(fp->fb, partition)); if (partition_size == buf->sz) { return; @@ -1280,7 +1249,6 @@ static void copy_avb_footer(const ImageSource* source, const std::string& partit uint64_t footer_offset = buf->sz - AVB_FOOTER_SIZE; if (0 != data.compare(footer_offset, AVB_FOOTER_MAGIC_LEN, AVB_FOOTER_MAGIC)) { - lseek(buf->fd.get(), 0, SEEK_SET); // IMPORTANT: resets buf->fd before return. return; } @@ -1295,23 +1263,38 @@ static void copy_avb_footer(const ImageSource* source, const std::string& partit } buf->fd = std::move(fd); buf->sz = partition_size; - lseek(buf->fd.get(), 0, SEEK_SET); } -void flash_partition_files(const std::string& partition, const std::vector& files) { +void flash_partition(const FlashingPlan* fp, const std::string& partition, SparsePtr sparse_file) { + std::vector files; + if (int limit = get_sparse_limit(sparse_file_len(sparse_file.get(), true, false), fp)) { + if (!split_file(sparse_file.get(), limit, &files)) { + LOG(FATAL) << "Failed to resparse data for partition " << partition << " sparse limit " + << limit << " bytes"; + } + } else { + files.emplace_back(std::move(sparse_file)); + } + flash_partition_files(fp->fb, partition, files); +} + +void flash_partition_files(IFastBootDriver* fb, const std::string& partition, + const std::vector& files) { for (size_t i = 0; i < files.size(); i++) { sparse_file* s = files[i].get(); int64_t sz = sparse_file_len(s, true, false); if (sz < 0) { LOG(FATAL) << "Could not compute length of sparse image for " << partition; } - fb->FlashPartition(partition, s, sz, i + 1, files.size()); + if (fb->Download(partition, s, sz, i + 1, files.size(), false) == SUCCESS) { + fb->Flash(partition); + } } } -static void flash_buf(const ImageSource* source, const std::string& partition, +static void flash_buf(const FlashingPlan* fp, const std::string& partition, struct fastboot_buffer* buf, const bool apply_vbmeta) { - copy_avb_footer(source, partition, buf); + copy_avb_footer(fp, partition, buf); // Rewrite vbmeta if that's what we're flashing and modification has been requested. if (g_disable_verity || g_disable_verification) { @@ -1319,26 +1302,28 @@ static void flash_buf(const ImageSource* source, const std::string& partition, // e.g., guest_vbmeta_a. if (apply_vbmeta) { rewrite_vbmeta_buffer(buf, false /* vbmeta_in_boot */); - } else if (!has_vbmeta_partition() && + } else if (!has_vbmeta_partition(fp->fb) && (partition == "boot" || partition == "boot_a" || partition == "boot_b")) { rewrite_vbmeta_buffer(buf, true /* vbmeta_in_boot */); } } - switch (buf->type) { - case FB_BUFFER_SPARSE: { - flash_partition_files(partition, buf->files); - break; + if (lseek(buf->fd.get(), 0, SEEK_SET) < 0) { + PLOG(FATAL) << "Failed to reset seek position before flashing"; + } + + if (int64_t limit = get_sparse_limit(buf->sz, fp); limit > 0) { + std::vector files; + if (!split_file(buf->fd.get(), limit, &files)) { + LOG(FATAL) << "Could not resparse file for partition: " << partition; } - case FB_BUFFER_FD: - fb->FlashPartition(partition, buf->fd, buf->sz); - break; - default: - die("unknown buffer type: %d", buf->type); + flash_partition_files(fp->fb, partition, files); + } else { + fp->fb->FlashPartition(partition, buf->fd, buf->sz); } } -std::string get_current_slot() { +std::string get_current_slot(IFastBootDriver* fb) { std::string current_slot; if (fb->GetVar("current-slot", ¤t_slot) != fastboot::SUCCESS) return ""; if (current_slot[0] == '_') current_slot.erase(0, 1); @@ -1367,19 +1352,19 @@ static std::string get_other_slot(const std::string& current_slot, int count) { return std::string(1, next); } -static std::string get_other_slot(const std::string& current_slot) { +static std::string get_other_slot(IFastBootDriver* fb, const std::string& current_slot) { return get_other_slot(current_slot, get_slot_count(fb)); } -static std::string get_other_slot(int count) { - return get_other_slot(get_current_slot(), count); +static std::string get_other_slot(IFastBootDriver* fb, int count) { + return get_other_slot(get_current_slot(fb), count); } -static std::string get_other_slot() { - return get_other_slot(get_current_slot(), get_slot_count(fb)); +static std::string get_other_slot(IFastBootDriver* fb) { + return get_other_slot(get_current_slot(fb), get_slot_count(fb)); } -static std::string verify_slot(const std::string& slot_name, bool allow_all) { +static std::string verify_slot(IFastBootDriver* fb, const std::string& slot_name, bool allow_all) { std::string slot = slot_name; if (slot == "all") { if (allow_all) { @@ -1398,7 +1383,7 @@ static std::string verify_slot(const std::string& slot_name, bool allow_all) { if (count == 0) die("Device does not support slots"); if (slot == "other") { - std::string other = get_other_slot(count); + std::string other = get_other_slot(fb, count); if (other == "") { die("No known slots"); } @@ -1415,11 +1400,11 @@ static std::string verify_slot(const std::string& slot_name, bool allow_all) { exit(1); } -static std::string verify_slot(const std::string& slot) { - return verify_slot(slot, true); +static std::string verify_slot(IFastBootDriver* fb, const std::string& slot) { + return verify_slot(fb, slot, true); } -static void do_for_partition(const std::string& part, const std::string& slot, +static void do_for_partition(IFastBootDriver* fb, const std::string& part, const std::string& slot, const std::function& func, bool force_slot) { std::string has_slot; std::string current_slot; @@ -1432,7 +1417,7 @@ static void do_for_partition(const std::string& part, const std::string& slot, } if (has_slot == "yes") { if (slot == "") { - current_slot = get_current_slot(); + current_slot = get_current_slot(fb); if (current_slot == "") { die("Failed to identify current slot"); } @@ -1455,7 +1440,7 @@ static void do_for_partition(const std::string& part, const std::string& slot, * partition names. If force_slot is true, it will fail if a slot is specified, and the given * partition does not support slots. */ -void do_for_partitions(const std::string& part, const std::string& slot, +void do_for_partitions(IFastBootDriver* fb, const std::string& part, const std::string& slot, const std::function& func, bool force_slot) { std::string has_slot; // |part| can be vendor_boot:default. Query has-slot on the first token only. @@ -1468,13 +1453,13 @@ void do_for_partitions(const std::string& part, const std::string& slot, } if (has_slot == "yes") { for (int i = 0; i < get_slot_count(fb); i++) { - do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot); + do_for_partition(fb, part, std::string(1, (char)(i + 'a')), func, force_slot); } } else { - do_for_partition(part, "", func, force_slot); + do_for_partition(fb, part, "", func, force_slot); } } else { - do_for_partition(part, slot, func, force_slot); + do_for_partition(fb, part, slot, func, force_slot); } } @@ -1486,7 +1471,7 @@ static uint64_t fetch_partition(const std::string& partition, borrowed_fd fd, if (fetch_size == 0) { die("Unable to get %s. Device does not support fetch command.", FB_VAR_MAX_FETCH_SIZE); } - uint64_t partition_size = get_partition_size(partition); + uint64_t partition_size = get_partition_size(fb, partition); if (partition_size <= 0) { die("Invalid partition size for partition %s: %" PRId64, partition.c_str(), partition_size); } @@ -1508,7 +1493,7 @@ static void do_fetch(const std::string& partition, const std::string& slot_overr unique_fd fd(TEMP_FAILURE_RETRY( open(outfile.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY, 0644))); auto fetch = std::bind(fetch_partition, _1, borrowed_fd(fd), fb); - do_for_partitions(partition, slot_override, fetch, false /* force slot */); + do_for_partitions(fb, partition, slot_override, fetch, false /* force slot */); } // Return immediately if not flashing a vendor boot image. If flashing a vendor boot image, @@ -1524,7 +1509,7 @@ static std::string repack_ramdisk(const char* pname, struct fastboot_buffer* buf !android::base::StartsWith(pname_sv, "vendor_boot_b:")) { return std::string(pname_sv); } - if (buf->type != FB_BUFFER_FD) { + if (buf->file_type != FB_BUFFER_FD) { die("Flashing sparse vendor ramdisk image is not supported."); } if (buf->sz <= 0) { @@ -1534,11 +1519,11 @@ static std::string repack_ramdisk(const char* pname, struct fastboot_buffer* buf std::string ramdisk(pname_sv.substr(pname_sv.find(':') + 1)); if (!g_dtb_path.empty()) { - if (!load_buf(g_dtb_path.c_str(), &dtb_buf, nullptr)) { + if (!load_buf(g_dtb_path.c_str(), &dtb_buf)) { die("cannot load '%s': %s", g_dtb_path.c_str(), strerror(errno)); } - if (dtb_buf.type != FB_BUFFER_FD) { + if (dtb_buf.file_type != FB_BUFFER_FD) { die("Flashing sparse vendor ramdisk image with dtb is not supported."); } if (dtb_buf.sz <= 0) { @@ -1572,48 +1557,48 @@ void do_flash(const char* pname, const char* fname, const bool apply_vbmeta, if (fp->source) { unique_fd fd = fp->source->OpenFile(fname); - if (fd < 0 || !load_buf_fd(std::move(fd), &buf, fp)) { + if (fd < 0 || !load_buf_fd(std::move(fd), &buf)) { die("could not load '%s': %s", fname, strerror(errno)); } std::vector signature_data; std::string file_string(fname); if (fp->source->ReadFile(file_string.substr(0, file_string.find('.')) + ".sig", &signature_data)) { - fb->Download("signature", signature_data); - fb->RawCommand("signature", "installing signature"); + fp->fb->Download("signature", signature_data); + fp->fb->RawCommand("signature", "installing signature"); } - } else if (!load_buf(fname, &buf, fp)) { + } else if (!load_buf(fname, &buf)) { die("cannot load '%s': %s", fname, strerror(errno)); } - if (is_logical(pname)) { - fb->ResizePartition(pname, std::to_string(buf.image_size)); + if (is_logical(fp->fb, pname)) { + fp->fb->ResizePartition(pname, std::to_string(buf.image_size)); } std::string flash_pname = repack_ramdisk(pname, &buf, fp->fb); - flash_buf(fp->source.get(), flash_pname, &buf, apply_vbmeta); + flash_buf(fp, flash_pname, &buf, apply_vbmeta); } // Sets slot_override as the active slot. If slot_override is blank, // set current slot as active instead. This clears slot-unbootable. -static void set_active(const std::string& slot_override) { +static void set_active(IFastBootDriver* fb, const std::string& slot_override) { if (!supports_AB(fb)) return; if (slot_override != "") { fb->SetActive(slot_override); } else { - std::string current_slot = get_current_slot(); + std::string current_slot = get_current_slot(fb); if (current_slot != "") { fb->SetActive(current_slot); } } } -bool is_userspace_fastboot() { +bool is_userspace_fastboot(IFastBootDriver* fb) { std::string value; return fb->GetVar("is-userspace", &value) == fastboot::SUCCESS && value == "yes"; } -void reboot_to_userspace_fastboot() { +void reboot_to_userspace_fastboot(IFastBootDriver* fb) { fb->RebootTo("fastboot"); if (fb->WaitForDisconnect() != fastboot::SUCCESS) { die("Error waiting for USB disconnect."); @@ -1627,7 +1612,7 @@ void reboot_to_userspace_fastboot() { fb->set_transport(open_device()); - if (!is_userspace_fastboot()) { + if (!is_userspace_fastboot(fb)) { die("Failed to boot into userspace fastboot; one or more components might be unbootable."); } @@ -1636,7 +1621,7 @@ void reboot_to_userspace_fastboot() { target_sparse_limit = -1; } -static void CancelSnapshotIfNeeded() { +static void CancelSnapshotIfNeeded(IFastBootDriver* fb) { std::string merge_status = "none"; if (fb->GetVar(FB_VAR_SNAPSHOT_UPDATE_STATUS, &merge_status) == fastboot::SUCCESS && !merge_status.empty() && merge_status != "none") { @@ -1852,14 +1837,14 @@ void FlashAllTool::Flash() { // Change the slot first, so we boot into the correct recovery image when // using fastbootd. if (fp_->slot_override == "all") { - set_active("a"); + set_active(fp_->fb, "a"); } else { - set_active(fp_->slot_override); + set_active(fp_->fb, fp_->slot_override); } DetermineSlot(); - CancelSnapshotIfNeeded(); + CancelSnapshotIfNeeded(fp_->fb); tasks_ = CollectTasks(); @@ -1898,12 +1883,12 @@ void FlashAllTool::CheckRequirements() { if (!fp_->source->ReadFile("android-info.txt", &contents)) { die("could not read android-info.txt"); } - ::CheckRequirements({contents.data(), contents.size()}, fp_->force_flash); + ::CheckRequirements(fp_->fb, {contents.data(), contents.size()}, fp_->force_flash); } void FlashAllTool::DetermineSlot() { if (fp_->slot_override.empty()) { - fp_->current_slot = get_current_slot(); + fp_->current_slot = get_current_slot(fp_->fb); } else { fp_->current_slot = fp_->slot_override; } @@ -1912,12 +1897,12 @@ void FlashAllTool::DetermineSlot() { return; } if (fp_->slot_override != "" && fp_->slot_override != "all") { - fp_->secondary_slot = get_other_slot(fp_->slot_override); + fp_->secondary_slot = get_other_slot(fp_->fb, fp_->slot_override); } else { - fp_->secondary_slot = get_other_slot(); + fp_->secondary_slot = get_other_slot(fp_->fb); } if (fp_->secondary_slot == "") { - if (supports_AB(fb)) { + if (supports_AB(fp_->fb)) { fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n"); } fp_->skip_secondary = true; @@ -1983,7 +1968,7 @@ void FlashAllTool::AddFlashTasks(const std::vectorsource->OpenFile(image->img_name); - if (fd < 0 || !load_buf_fd(std::move(fd), &buf, fp_)) { + if (fd < 0 || !load_buf_fd(std::move(fd), &buf)) { if (image->optional_if_no_image) { continue; } @@ -2041,7 +2026,8 @@ static std::string next_arg(std::vector* args) { return result; } -static void do_oem_command(const std::string& cmd, std::vector* args) { +static void do_oem_command(IFastBootDriver* fb, const std::string& cmd, + std::vector* args) { if (args->empty()) syntax_error("empty oem command"); std::string command(cmd); @@ -2051,7 +2037,7 @@ static void do_oem_command(const std::string& cmd, std::vector* arg fb->RawCommand(command, ""); } -static unsigned fb_get_flash_block_size(std::string name) { +static unsigned fb_get_flash_block_size(IFastBootDriver* fb, std::string name) { std::string sizeString; if (fb->GetVar(name, &sizeString) != fastboot::SUCCESS || sizeString.empty()) { // This device does not report flash block sizes, so return 0. @@ -2090,7 +2076,7 @@ void fb_perform_format(const std::string& partition, int skip_if_not_supported, limit = fp->sparse_limit; } - if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) { + if (fp->fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) { errMsg = "Can't determine partition type.\n"; goto failed; } @@ -2102,7 +2088,7 @@ void fb_perform_format(const std::string& partition, int skip_if_not_supported, partition_type = type_override; } - if (fb->GetVar("partition-size:" + partition, &partition_size) != fastboot::SUCCESS) { + if (fp->fb->GetVar("partition-size:" + partition, &partition_size) != fastboot::SUCCESS) { errMsg = "Unable to get partition size\n"; goto failed; } @@ -2131,8 +2117,8 @@ void fb_perform_format(const std::string& partition, int skip_if_not_supported, } unsigned eraseBlkSize, logicalBlkSize; - eraseBlkSize = fb_get_flash_block_size("erase-block-size"); - logicalBlkSize = fb_get_flash_block_size("logical-block-size"); + eraseBlkSize = fb_get_flash_block_size(fp->fb, "erase-block-size"); + logicalBlkSize = fb_get_flash_block_size(fp->fb, "logical-block-size"); if (fs_generator_generate(gen, output.path, size, eraseBlkSize, logicalBlkSize, fs_options)) { die("Cannot generate image for %s", partition.c_str()); @@ -2142,11 +2128,11 @@ void fb_perform_format(const std::string& partition, int skip_if_not_supported, if (fd == -1) { die("Cannot open generated image: %s", strerror(errno)); } - if (!load_buf_fd(std::move(fd), &buf, fp)) { + if (!load_buf_fd(std::move(fd), &buf)) { die("Cannot read image: %s", strerror(errno)); } - flash_buf(fp->source.get(), partition, &buf, is_vbmeta_partition(partition)); + flash_buf(fp, partition, &buf, is_vbmeta_partition(partition)); return; failed: @@ -2154,7 +2140,7 @@ void fb_perform_format(const std::string& partition, int skip_if_not_supported, fprintf(stderr, "Erase successful, but not automatically formatting.\n"); if (errMsg) fprintf(stderr, "%s", errMsg); } - fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str()); + fprintf(stderr, "FAILED (%s)\n", fp->fb->Error().c_str()); if (!skip_if_not_supported) { die("Command failed"); } @@ -2192,7 +2178,7 @@ static bool wipe_super(const android::fs_mgr::LpMetadata& metadata, const std::s if (super_bdev_name != "super") { // retrofit devices do not allow flashing to the retrofit partitions, // so enable it if we can. - fb->RawCommand("oem allow-flash-super"); + fp->fb->RawCommand("oem allow-flash-super"); } // Note: do not use die() in here, since we want TemporaryDir's destructor @@ -2226,7 +2212,7 @@ static bool wipe_super(const android::fs_mgr::LpMetadata& metadata, const std::s auto flash = [&](const std::string& partition_name) { do_flash(partition_name.c_str(), image_path.c_str(), false, fp); }; - do_for_partitions(partition, slot, flash, force_slot); + do_for_partitions(fp->fb, partition, slot, flash, force_slot); unlink(image_path.c_str()); } @@ -2245,7 +2231,7 @@ static void do_wipe_super(const std::string& image, const std::string& slot_over auto slot = slot_override; if (slot.empty()) { - slot = get_current_slot(); + slot = get_current_slot(fp->fb); } std::string message; @@ -2444,19 +2430,20 @@ int FastBootTool::Main(int argc, char* argv[]) { } fastboot::DriverCallbacks driver_callbacks = { .prolog = Status, - .epilog = Epilog, + .epilog = [&fp](int status, + bool crash_on_error) { Epilog(fp->fb, status, crash_on_error); }, .info = InfoMessage, .text = TextMessage, }; fastboot::FastBootDriver fastboot_driver(std::move(transport), driver_callbacks, false); - fb = &fastboot_driver; - fp->fb = &fastboot_driver; + fastboot::IFastBootDriver* fb = &fastboot_driver; + fp->fb = fb; const double start = now(); - if (fp->slot_override != "") fp->slot_override = verify_slot(fp->slot_override); - if (next_active != "") next_active = verify_slot(next_active, false); + if (fp->slot_override != "") fp->slot_override = verify_slot(fp->fb, fp->slot_override); + if (next_active != "") next_active = verify_slot(fp->fb, next_active, false); if (fp->wants_set_active) { if (next_active == "") { @@ -2464,12 +2451,12 @@ int FastBootTool::Main(int argc, char* argv[]) { std::string current_slot; if (fb->GetVar("current-slot", ¤t_slot) == fastboot::SUCCESS) { if (current_slot[0] == '_') current_slot.erase(0, 1); - next_active = verify_slot(current_slot, false); + next_active = verify_slot(fp->fb, current_slot, false); } else { fp->wants_set_active = false; } } else { - next_active = verify_slot(fp->slot_override, false); + next_active = verify_slot(fp->fb, fp->slot_override, false); } } } @@ -2480,7 +2467,7 @@ int FastBootTool::Main(int argc, char* argv[]) { if (command == FB_CMD_GETVAR) { std::string variable = next_arg(&args); - DisplayVarOrError(variable, variable); + DisplayVarOrError(fb, variable, variable); } else if (command == FB_CMD_ERASE) { std::string partition = next_arg(&args); auto erase = [&](const std::string& partition) { @@ -2494,7 +2481,7 @@ int FastBootTool::Main(int argc, char* argv[]) { fb->Erase(partition); }; - do_for_partitions(partition, fp->slot_override, erase, true); + do_for_partitions(fp->fb, partition, fp->slot_override, erase, true); } else if (android::base::StartsWith(command, "format")) { // Parsing for: "format[:[type][:[size]]]" // Some valid things: @@ -2515,7 +2502,7 @@ int FastBootTool::Main(int argc, char* argv[]) { fb_perform_format(partition, 0, type_override, size_override, fp->fs_options, fp.get()); }; - do_for_partitions(partition, fp->slot_override, format, true); + do_for_partitions(fb, partition, fp->slot_override, format, true); } else if (command == "signature") { std::string filename = next_arg(&args); std::vector data; @@ -2571,10 +2558,10 @@ int FastBootTool::Main(int argc, char* argv[]) { if (!args.empty()) second_stage = next_arg(&args); auto data = LoadBootableImage(kernel, ramdisk, second_stage); - auto flashraw = [&data](const std::string& partition) { + auto flashraw = [&fb, &data](const std::string& partition) { fb->FlashPartition(partition, data); }; - do_for_partitions(partition, fp->slot_override, flashraw, true); + do_for_partitions(fp->fb, partition, fp->slot_override, flashraw, true); } else if (command == "flashall") { if (fp->slot_override == "all") { fprintf(stderr, @@ -2601,13 +2588,13 @@ int FastBootTool::Main(int argc, char* argv[]) { tasks.emplace_back(std::make_unique(fp.get())); } } else if (command == FB_CMD_SET_ACTIVE) { - std::string slot = verify_slot(next_arg(&args), false); + std::string slot = verify_slot(fb, next_arg(&args), false); fb->SetActive(slot); } else if (command == "stage") { std::string filename = next_arg(&args); struct fastboot_buffer buf; - if (!load_buf(filename.c_str(), &buf, fp.get()) || buf.type != FB_BUFFER_FD) { + if (!load_buf(filename.c_str(), &buf) || buf.file_type != FB_BUFFER_FD) { die("cannot load '%s'", filename.c_str()); } fb->Download(filename, buf.fd.get(), buf.sz); @@ -2615,14 +2602,14 @@ int FastBootTool::Main(int argc, char* argv[]) { std::string filename = next_arg(&args); fb->Upload(filename); } else if (command == FB_CMD_OEM) { - do_oem_command(FB_CMD_OEM, &args); + do_oem_command(fp->fb, FB_CMD_OEM, &args); } else if (command == "flashing") { if (args.empty()) { syntax_error("missing 'flashing' command"); } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" || args[0] == "unlock_critical" || args[0] == "lock_critical" || args[0] == "get_unlock_ability")) { - do_oem_command("flashing", &args); + do_oem_command(fp->fb, "flashing", &args); } else { syntax_error("unknown 'flashing' command %s", args[0].c_str()); } @@ -2674,7 +2661,7 @@ int FastBootTool::Main(int argc, char* argv[]) { if (fp->wants_wipe) { if (fp->force_flash) { - CancelSnapshotIfNeeded(); + CancelSnapshotIfNeeded(fp->fb); } std::vector> wipe_tasks; std::vector partitions = {"userdata", "cache", "metadata"}; diff --git a/base/cvd/fastboot/fastboot.h b/base/cvd/fastboot/fastboot.h index f68f7010009..f02dd9ff767 100644 --- a/base/cvd/fastboot/fastboot.h +++ b/base/cvd/fastboot/fastboot.h @@ -59,9 +59,7 @@ enum fb_buffer_type { }; struct fastboot_buffer { - fb_buffer_type type; fb_buffer_type file_type; - std::vector files; int64_t sz; unique_fd fd; int64_t image_size; @@ -119,6 +117,7 @@ class FlashAllTool { std::vector> CollectTasks(); private: + void DumpInfo(); void CheckRequirements(); void DetermineSlot(); void CollectImages(); @@ -153,15 +152,16 @@ class LocalImageSource final : public ImageSource { char* get_android_product_out(); bool should_flash_in_userspace(const ImageSource* source, const std::string& partition_name); -bool is_userspace_fastboot(); +bool is_userspace_fastboot(fastboot::IFastBootDriver* fb); void do_flash(const char* pname, const char* fname, const bool apply_vbmeta, const FlashingPlan* fp); -void do_for_partitions(const std::string& part, const std::string& slot, - const std::function& func, bool force_slot); +void do_for_partitions(fastboot::IFastBootDriver* fb, const std::string& part, + const std::string& slot, const std::function& func, + bool force_slot); std::string find_item(const std::string& item); -void reboot_to_userspace_fastboot(); +void reboot_to_userspace_fastboot(fastboot::IFastBootDriver* fb); void syntax_error(const char* fmt, ...); -std::string get_current_slot(); +std::string get_current_slot(fastboot::IFastBootDriver* fb); // Code for Parsing fastboot-info.txt bool CheckFastbootInfoRequirements(const std::vector& command, @@ -186,12 +186,16 @@ struct NetworkSerial { Result ParseNetworkSerial(const std::string& serial); std::string GetPartitionName(const ImageEntry& entry, const std::string& current_slot_); -void flash_partition_files(const std::string& partition, const std::vector& files); +// Flash a single large sparse file to specified partition +void flash_partition(const FlashingPlan* fp, const std::string& partition, SparsePtr sparse_file); +// Flash a set of sparse files to specified partition, each sparse file should +// not exceed sparse limit of the device +void flash_partition_files(fastboot::IFastBootDriver* fb, const std::string& partition, + const std::vector& files); int64_t get_sparse_limit(int64_t size, const FlashingPlan* fp); -std::vector resparse_file(sparse_file* s, int64_t max_size); bool supports_AB(fastboot::IFastBootDriver* fb); -bool is_logical(const std::string& partition); +bool is_logical(fastboot::IFastBootDriver* fb, const std::string& partition); void fb_perform_format(const std::string& partition, int skip_if_not_supported, const std::string& type_override, const std::string& size_override, const unsigned fs_options, const FlashingPlan* fp); diff --git a/base/cvd/fastboot/fastboot_driver.cpp b/base/cvd/fastboot/fastboot_driver.cpp index e5ef66bafe9..0cdd6d51275 100644 --- a/base/cvd/fastboot/fastboot_driver.cpp +++ b/base/cvd/fastboot/fastboot_driver.cpp @@ -59,8 +59,7 @@ namespace fastboot { /*************************** PUBLIC *******************************/ FastBootDriver::FastBootDriver(std::unique_ptr transport, - DriverCallbacks driver_callbacks, - bool no_checks) + DriverCallbacks driver_callbacks, bool no_checks) : transport_(std::move(transport)), prolog_(std::move(driver_callbacks.prolog)), epilog_(std::move(driver_callbacks.epilog)), @@ -68,8 +67,7 @@ FastBootDriver::FastBootDriver(std::unique_ptr transport, text_(std::move(driver_callbacks.text)), disable_checks_(no_checks) {} -FastBootDriver::~FastBootDriver() { -} +FastBootDriver::~FastBootDriver() {} RetCode FastBootDriver::Boot(std::string* response, std::vector* info) { return RawCommand(FB_CMD_BOOT, "Booting", response, info); @@ -133,7 +131,7 @@ RetCode FastBootDriver::SnapshotUpdateCommand(const std::string& command, std::s prolog_(StringPrintf("Snapshot %s", command.c_str())); std::string raw = FB_CMD_SNAPSHOT_UPDATE ":" + command; auto result = RawCommand(raw, response, info); - epilog_(result); + epilog_(result, crash_on_error_); return result; } @@ -155,15 +153,6 @@ RetCode FastBootDriver::FlashPartition(const std::string& partition, android::ba return Flash(partition); } -RetCode FastBootDriver::FlashPartition(const std::string& partition, sparse_file* s, uint32_t size, - size_t current, size_t total) { - RetCode ret; - if ((ret = Download(partition, s, size, current, total, false))) { - return ret; - } - return Flash(partition); -} - RetCode FastBootDriver::Partitions(std::vector>* partitions) { std::vector all; RetCode ret; @@ -190,7 +179,7 @@ RetCode FastBootDriver::Download(const std::string& name, android::base::borrowe std::vector* info) { prolog_(StringPrintf("Sending '%s' (%zu KB)", name.c_str(), size / 1024)); auto result = Download(fd, size, response, info); - epilog_(result); + epilog_(result, crash_on_error_); return result; } @@ -221,7 +210,7 @@ RetCode FastBootDriver::Download(const std::string& name, const std::vector* info) { prolog_(StringPrintf("Sending '%s' (%zu KB)", name.c_str(), buf.size() / 1024)); auto result = Download(buf, response, info); - epilog_(result); + epilog_(result, crash_on_error_); return result; } @@ -253,7 +242,7 @@ RetCode FastBootDriver::Download(const std::string& partition, struct sparse_fil prolog_(StringPrintf("Sending sparse '%s' %zu/%zu (%u KB)", partition.c_str(), current, total, size / 1024)); auto result = Download(s, use_crc, response, info); - epilog_(result); + epilog_(result, crash_on_error_); return result; } @@ -301,7 +290,7 @@ RetCode FastBootDriver::Upload(const std::string& outfile, std::string* response std::vector* info) { prolog_("Uploading '" + outfile + "'"); auto result = UploadInner(outfile, response, info); - epilog_(result); + epilog_(result, crash_on_error_); return result; } @@ -331,7 +320,7 @@ RetCode FastBootDriver::RunAndReadBuffer( while (current_offset < total_size) { uint64_t remaining = total_size - current_offset; uint64_t chunk_size = std::min(buf_size, remaining); - if ((ret = ReadBuffer(data.data(), chunk_size)) != SUCCESS) { + if ((ret = ReadFully(data.data(), chunk_size)) != SUCCESS) { return ret; } if ((ret = write_fn(data.data(), chunk_size)) != SUCCESS) { @@ -366,6 +355,25 @@ RetCode FastBootDriver::UploadInner(const std::string& outfile, std::string* res RetCode FastBootDriver::FetchToFd(const std::string& partition, android::base::borrowed_fd fd, int64_t offset, int64_t size, std::string* response, std::vector* info) { + if (fd.get() < 0) { + return BAD_ARG; + } + return Fetch( + partition, + [&](const char* data, uint64_t size) { + if (!android::base::WriteFully(fd, data, size)) { + error_ = android::base::StringPrintf("Cannot write: %s", strerror(errno)); + return IO_ERROR; + } + return SUCCESS; + }, + offset, size, response, info); +} + +RetCode FastBootDriver::Fetch(const std::string& partition, + const std::function& write_fn, + int64_t offset, int64_t size, std::string* response, + std::vector* info) { prolog_(android::base::StringPrintf("Fetching %s (offset=%" PRIx64 ", size=%" PRIx64 ")", partition.c_str(), offset, size)); std::string cmd = FB_CMD_FETCH ":" + partition; @@ -375,14 +383,8 @@ RetCode FastBootDriver::FetchToFd(const std::string& partition, android::base::b cmd += android::base::StringPrintf(":0x%08" PRIx64, size); } } - RetCode ret = RunAndReadBuffer(cmd, response, info, [&](const char* data, uint64_t size) { - if (!android::base::WriteFully(fd, data, size)) { - error_ = android::base::StringPrintf("Cannot write: %s", strerror(errno)); - return IO_ERROR; - } - return SUCCESS; - }); - epilog_(ret); + RetCode ret = RunAndReadBuffer(cmd, response, info, write_fn); + epilog_(ret, crash_on_error_); return ret; } @@ -430,7 +432,7 @@ RetCode FastBootDriver::RawCommand(const std::string& cmd, const std::string& me int* dsize) { prolog_(message); auto result = RawCommand(cmd, response, info, dsize); - epilog_(result); + epilog_(result, crash_on_error_); return result; } @@ -538,7 +540,7 @@ RetCode FastBootDriver::SendBuffer(android::base::borrowed_fd fd, size_t size) { while (remaining) { // Memory map the file size_t len = std::min(remaining, MAX_MAP_SIZE); - auto mapping{android::base::MappedFile::FromFd(fd, offset, len, PROT_READ)}; + auto mapping{android::base::MappedFile::FromOsHandle(fd.get(), offset, len, PROT_READ)}; if (!mapping) { error_ = "Creating filemap failed"; return IO_ERROR; @@ -580,16 +582,19 @@ RetCode FastBootDriver::SendBuffer(const void* buf, size_t size) { return SUCCESS; } -RetCode FastBootDriver::ReadBuffer(void* buf, size_t size) { - // Read the buffer - ssize_t tmp = transport_->Read(buf, size); - - if (tmp < 0) { - error_ = ErrnoStr("Read from device failed in ReadBuffer()"); - return IO_ERROR; - } else if (static_cast(tmp) != size) { - error_ = android::base::StringPrintf("Failed to read all %zu bytes", size); - return IO_ERROR; +RetCode FastBootDriver::ReadFully(void* buf, size_t size) { + size_t left = size; + while (left > 0) { + ssize_t n = transport_->Read(buf, left); + if (n < 0) { + error_ = ErrnoStr("Read from device failed in ReadFully()"); + return IO_ERROR; + } else if (n == 0) { + error_ = android::base::StringPrintf("Failed to read all %zu bytes", size); + return IO_ERROR; + } + left -= n; + buf = static_cast(buf) + n; } return SUCCESS; diff --git a/base/cvd/fastboot/fastboot_driver.h b/base/cvd/fastboot/fastboot_driver.h index 49cebd97a1c..c7987bd5c5f 100644 --- a/base/cvd/fastboot/fastboot_driver.h +++ b/base/cvd/fastboot/fastboot_driver.h @@ -48,7 +48,7 @@ namespace fastboot { struct DriverCallbacks { std::function prolog = [](const std::string&) {}; - std::function epilog = [](int) {}; + std::function epilog = [](int, bool) {}; std::function info = [](const std::string&) {}; std::function text = [](const std::string&) {}; }; @@ -65,28 +65,29 @@ class FastBootDriver : public IFastBootDriver { bool no_checks = false); ~FastBootDriver(); - RetCode Boot(std::string* response = nullptr, std::vector* info = nullptr); - RetCode Continue(std::string* response = nullptr, std::vector* info = nullptr); - RetCode CreatePartition(const std::string& partition, const std::string& size); + RetCode Boot(std::string* response = nullptr, + std::vector* info = nullptr) override; + RetCode Continue(std::string* response = nullptr, + std::vector* info = nullptr) override; + RetCode CreatePartition(const std::string& partition, const std::string& size) override; RetCode DeletePartition(const std::string& partition) override; RetCode Download(const std::string& name, android::base::borrowed_fd fd, size_t size, std::string* response = nullptr, std::vector* info = nullptr) override; RetCode Download(android::base::borrowed_fd fd, size_t size, std::string* response = nullptr, - std::vector* info = nullptr); + std::vector* info = nullptr) override; RetCode Download(const std::string& name, const std::vector& buf, - std::string* response = nullptr, std::vector* info = nullptr); + std::string* response = nullptr, + std::vector* info = nullptr) override; RetCode Download(const std::vector& buf, std::string* response = nullptr, - std::vector* info = nullptr); + std::vector* info = nullptr) override; RetCode Download(const std::string& partition, struct sparse_file* s, uint32_t sz, size_t current, size_t total, bool use_crc, std::string* response = nullptr, - std::vector* info = nullptr); - RetCode Download(sparse_file* s, bool use_crc = false, std::string* response = nullptr, - std::vector* info = nullptr); + std::vector* info = nullptr) override; RetCode Erase(const std::string& partition, std::string* response = nullptr, std::vector* info = nullptr) override; RetCode Flash(const std::string& partition, std::string* response = nullptr, - std::vector* info = nullptr); + std::vector* info = nullptr) override; RetCode GetVar(const std::string& key, std::string* val, std::vector* info = nullptr) override; RetCode GetVarAll(std::vector* response); @@ -96,21 +97,23 @@ class FastBootDriver : public IFastBootDriver { std::vector* info = nullptr) override; RetCode ResizePartition(const std::string& partition, const std::string& size) override; RetCode SetActive(const std::string& slot, std::string* response = nullptr, - std::vector* info = nullptr); + std::vector* info = nullptr) override; RetCode Upload(const std::string& outfile, std::string* response = nullptr, - std::vector* info = nullptr); + std::vector* info = nullptr) override; RetCode SnapshotUpdateCommand(const std::string& command, std::string* response = nullptr, - std::vector* info = nullptr); + std::vector* info = nullptr) override; RetCode FetchToFd(const std::string& partition, android::base::borrowed_fd fd, int64_t offset = -1, int64_t size = -1, std::string* response = nullptr, std::vector* info = nullptr) override; + RetCode Fetch(const std::string& partition, + const std::function& write_fn, + int64_t offset = -1, int64_t size = -1, std::string* response = nullptr, + std::vector* info = nullptr) override; /* HIGHER LEVEL COMMANDS -- Composed of the commands above */ - RetCode FlashPartition(const std::string& partition, const std::vector& data); + RetCode FlashPartition(const std::string& partition, const std::vector& data) override; RetCode FlashPartition(const std::string& partition, android::base::borrowed_fd fd, uint32_t sz) override; - RetCode FlashPartition(const std::string& partition, sparse_file* s, uint32_t sz, - size_t current, size_t total); RetCode Partitions(std::vector>* partitions); RetCode Require(const std::string& var, const std::vector& allowed, bool* reqmet, @@ -119,19 +122,24 @@ class FastBootDriver : public IFastBootDriver { /* HELPERS */ void SetInfoCallback(std::function info); static const std::string RCString(RetCode rc); - std::string Error(); + std::string Error() override; RetCode WaitForDisconnect() override; - void set_transport(std::unique_ptr transport); + void set_transport(std::unique_ptr transport) override; RetCode RawCommand(const std::string& cmd, const std::string& message, std::string* response = nullptr, std::vector* info = nullptr, - int* dsize = nullptr); + int* dsize = nullptr) override; RetCode RawCommand(const std::string& cmd, std::string* response = nullptr, - std::vector* info = nullptr, int* dsize = nullptr); + std::vector* info = nullptr, int* dsize = nullptr) override; + + RetCode Download(struct sparse_file* s, bool use_crc = false, std::string* response = nullptr, + std::vector* info = nullptr); protected: + void SetCrashOnError(bool crash) override { crash_on_error_ = crash; } + bool GetCrashOnError() const override { return crash_on_error_; } RetCode DownloadCommand(uint32_t size, std::string* response = nullptr, std::vector* info = nullptr); RetCode HandleResponse(std::string* response = nullptr, @@ -146,7 +154,7 @@ class FastBootDriver : public IFastBootDriver { RetCode SendBuffer(const std::vector& buf); RetCode SendBuffer(const void* buf, size_t size); - RetCode ReadBuffer(void* buf, size_t size); + RetCode ReadFully(void* buf, size_t size); RetCode UploadInner(const std::string& outfile, std::string* response = nullptr, std::vector* info = nullptr); @@ -158,10 +166,11 @@ class FastBootDriver : public IFastBootDriver { std::string error_; std::function prolog_; - std::function epilog_; + std::function epilog_; std::function info_; std::function text_; bool disable_checks_; + bool crash_on_error_ = true; }; } // namespace fastboot diff --git a/base/cvd/fastboot/fastboot_driver_interface.h b/base/cvd/fastboot/fastboot_driver_interface.h index 7cb8a6b6acd..93ca86f9480 100644 --- a/base/cvd/fastboot/fastboot_driver_interface.h +++ b/base/cvd/fastboot/fastboot_driver_interface.h @@ -15,8 +15,11 @@ // #pragma once +#include #include +#include +#include #include "android-base/unique_fd.h" class Transport; @@ -34,6 +37,15 @@ enum RetCode : int { class IFastBootDriver { public: + template + auto RunWithNoCrash(F&& functor) { + const auto old_setting = GetCrashOnError(); + SetCrashOnError(false); + auto ret = functor(); + SetCrashOnError(old_setting); + return ret; + } + RetCode virtual FlashPartition(const std::string& partition, const std::vector& data) = 0; RetCode virtual FlashPartition(const std::string& partition, android::base::borrowed_fd fd, uint32_t sz) = 0; RetCode virtual DeletePartition(const std::string& partition) = 0; @@ -49,15 +61,101 @@ class IFastBootDriver { int64_t offset = -1, int64_t size = -1, std::string* response = nullptr, std::vector* info = nullptr) = 0; + + RetCode virtual Fetch(const std::string& partition, + const std::function& write_fn, + int64_t offset = -1, int64_t size = -1, std::string* response = nullptr, + std::vector* info = nullptr) = 0; + + // Fetch data to containers such as std::vector, container must support + // insertion at end of container. + template + RetCode Fetch(const std::string& partition, T* container, int64_t offset = -1, + int64_t size = -1, std::string* response = nullptr, + std::vector* info = nullptr) { + return Fetch( + partition, + [container](const char* data, uint64_t size) { + container->insert(container->end(), data, data + size); + return fastboot::SUCCESS; + }, + offset, size, response, info); + } + RetCode FetchAtOffset(const std::string& partition, void* data, size_t size, int64_t offset) { + auto bytes = reinterpret_cast(data); + return Fetch( + partition, + [bytes](const char* chunk, uint64_t chunk_size) mutable { + memcpy(bytes, chunk, chunk_size); + bytes += chunk_size; + return SUCCESS; + }, + offset, size); + } RetCode virtual Download(const std::string& name, android::base::borrowed_fd fd, size_t size, std::string* response = nullptr, std::vector* info = nullptr) = 0; + RetCode virtual Download(android::base::borrowed_fd fd, size_t size, + std::string* response = nullptr, + std::vector* info = nullptr) = 0; + RetCode virtual Download(const std::string& name, const std::vector& buf, + std::string* response = nullptr, + std::vector* info = nullptr) = 0; + RetCode virtual Download(const std::vector& buf, std::string* response = nullptr, + std::vector* info = nullptr) = 0; + RetCode virtual Download(const std::string& partition, struct sparse_file* s, uint32_t sz, + size_t current, size_t total, bool use_crc, + std::string* response = nullptr, + std::vector* info = nullptr) = 0; RetCode virtual RawCommand(const std::string& cmd, const std::string& message, std::string* response = nullptr, std::vector* info = nullptr, int* dsize = nullptr) = 0; + virtual RetCode RawCommand(const std::string& cmd, std::string* response = nullptr, + std::vector* info = nullptr, int* dsize = nullptr) = 0; RetCode virtual ResizePartition(const std::string& partition, const std::string& size) = 0; RetCode virtual Erase(const std::string& partition, std::string* response = nullptr, std::vector* info = nullptr) = 0; + RetCode virtual Flash(const std::string& partition, std::string* response = nullptr, + std::vector* info = nullptr) = 0; virtual ~IFastBootDriver() = default; + virtual std::string Error() = 0; + virtual RetCode SetActive(const std::string& slot, std::string* response = nullptr, + std::vector* info = nullptr) = 0; + virtual void set_transport(std::unique_ptr transport) = 0; + virtual RetCode SnapshotUpdateCommand(const std::string& command, + std::string* response = nullptr, + std::vector* info = nullptr) = 0; + virtual RetCode Boot(std::string* response = nullptr, + std::vector* info = nullptr) = 0; + virtual RetCode Continue(std::string* response = nullptr, + std::vector* info = nullptr) = 0; + virtual RetCode CreatePartition(const std::string& partition, const std::string& size) = 0; + virtual RetCode Upload(const std::string& outfile, std::string* response = nullptr, + std::vector* info = nullptr) = 0; + + protected: + friend struct NoCrashGuard; + virtual void SetCrashOnError(bool) = 0; + virtual bool GetCrashOnError() const = 0; +}; + +// By default, IFastBootDriver's various methods would crash if a device reports +// failure status for a command. While |NoCrashGuard| is in scope, the specified +// IFastBootDriver instance would no longer crash on error. +struct NoCrashGuard { + [[nodiscard("RAII guard should not be ignored")]] constexpr explicit NoCrashGuard( + IFastBootDriver* fb) + : driver(fb), old_setting(fb->GetCrashOnError()) { + fb->SetCrashOnError(false); + } + constexpr ~NoCrashGuard() { + if (driver) { + driver->SetCrashOnError(old_setting); + } + } + + private: + IFastBootDriver* driver; + const bool old_setting; }; -} // namespace fastboot \ No newline at end of file +} // namespace fastboot diff --git a/base/cvd/fastboot/h2h_workaround.cpp b/base/cvd/fastboot/h2h_workaround.cpp new file mode 100644 index 00000000000..c03706fa8a3 --- /dev/null +++ b/base/cvd/fastboot/h2h_workaround.cpp @@ -0,0 +1,27 @@ +// +// Copyright (C) 2025 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. +// + +#include "h2h_workaround.h" +#include "usb.h" + +#define GOOGLE_VID 0x18d1 +#define H2H_PID1 0x506d +#define H2H_PID2 0x27a7 + +bool is_h2h_device(usb_ifc_info* info) { + return info->dev_vendor == GOOGLE_VID && (info->dev_product == H2H_PID1 || + info->dev_product == H2H_PID2); +} diff --git a/base/cvd/fastboot/h2h_workaround.h b/base/cvd/fastboot/h2h_workaround.h new file mode 100644 index 00000000000..06d5534192d --- /dev/null +++ b/base/cvd/fastboot/h2h_workaround.h @@ -0,0 +1,25 @@ +// +// Copyright (C) 2025 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. +// + +#pragma once + +#include "usb.h" + +// TODO(b/441807482): Remove this workaround when h2h cable FW is updated +// The function returns true when workaround should be applied for given USB +// interface. The workaround is to not display h2h entries in +// "fastboot devices" output. +bool is_h2h_device(usb_ifc_info* info); diff --git a/base/cvd/fastboot/patches/0001-restore-license-banner.patch b/base/cvd/fastboot/patches/0001-restore-license-banner.patch new file mode 100644 index 00000000000..005661f8910 --- /dev/null +++ b/base/cvd/fastboot/patches/0001-restore-license-banner.patch @@ -0,0 +1,72 @@ +diff --git b/base/cvd/fastboot/fs.cpp a/base/cvd/fastboot/fs.cpp +index c8d1b59c9..3e47171b4 100644 +--- b/base/cvd/fastboot/fs.cpp ++++ a/base/cvd/fastboot/fs.cpp +@@ -1,3 +1,19 @@ ++/* ++ * Copyright (C) 2014 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. ++ */ ++ + #include "fs.h" + + +diff --git b/base/cvd/fastboot/fs.h a/base/cvd/fastboot/fs.h +index 5ae473be5..efee6878f 100644 +--- b/base/cvd/fastboot/fs.h ++++ a/base/cvd/fastboot/fs.h +@@ -1,3 +1,19 @@ ++/* ++ * Copyright (C) 2014 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. ++ */ ++ + #pragma once + + #include +diff --git b/base/cvd/fastboot/util.h a/base/cvd/fastboot/util.h +index fdbc1d699..e737726dd 100644 +--- b/base/cvd/fastboot/util.h ++++ a/base/cvd/fastboot/util.h +@@ -1,3 +1,19 @@ ++/* ++ * Copyright (C) 2018 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. ++ */ ++ + #pragma once + + #include diff --git a/base/cvd/fastboot/patches/0002-drop-libziparchive-dependency.patch b/base/cvd/fastboot/patches/0002-drop-libziparchive-dependency.patch new file mode 100644 index 00000000000..e113e2d4220 --- /dev/null +++ b/base/cvd/fastboot/patches/0002-drop-libziparchive-dependency.patch @@ -0,0 +1,182 @@ +diff --git b/base/cvd/fastboot/fastboot.cpp a/base/cvd/fastboot/fastboot.cpp +index 1c52da238..41472c286 100644 +--- b/base/cvd/fastboot/fastboot.cpp ++++ a/base/cvd/fastboot/fastboot.cpp +@@ -67,7 +67,7 @@ + #include + #include + #include +-#include ++#include + + #include "bootimg_utils.h" + #include "constants.h" +@@ -713,25 +713,40 @@ static std::vector LoadBootableImage(const std::string& kernel, const std: + return out; + } + +-static bool UnzipToMemory(ZipArchiveHandle zip, const std::string& entry_name, ++static zip_int64_t ExtractToBuffer(zip_file_t *zip_file, std::vector& out) { ++ zip_int64_t bytes_read; ++ zip_uint64_t pos = 0; ++ while ((bytes_read = zip_fread(zip_file, &out[pos], out.size() - pos)) > 0) { ++ pos += bytes_read; ++ } ++ return bytes_read; ++} ++ ++static bool UnzipToMemory(unique_zip_t& zip, const char *entry_name, + std::vector* out) { +- ZipEntry64 zip_entry; +- if (FindEntry(zip, entry_name, &zip_entry) != 0) { +- fprintf(stderr, "archive does not contain '%s'\n", entry_name.c_str()); ++ auto zip_file = zip_fopen(zip.get(), entry_name, 0); ++ if (!zip_file) { ++ fprintf(stderr, "archive does not contain '%s'\n", entry_name); + return false; + } + +- if (zip_entry.uncompressed_length > std::numeric_limits::max()) { +- die("entry '%s' is too large: %" PRIu64, entry_name.c_str(), zip_entry.uncompressed_length); ++ zip_stat_t zstat; ++ zip_stat_init(&zstat); ++ if (zip_stat(zip.get(), entry_name, 0, &zstat) < 0 || !(zstat.valid & ZIP_STAT_SIZE)) { ++ die("size of entry '%s' could not be determined\n", entry_name); + } +- out->resize(zip_entry.uncompressed_length); ++ if (zstat.size > std::numeric_limits::max()) { ++ die("entry '%s' is too large: %" PRIu64, entry_name, zstat.size); ++ } ++ out->resize(zstat.size); + +- fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(), ++ fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name, + out->size() / 1024 / 1024); + +- int error = +- ExtractToMemory(zip, &zip_entry, reinterpret_cast(out->data()), out->size()); +- if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error)); ++ if (ExtractToBuffer(zip_file, *out) < 0) { ++ auto error = zip_file_get_error(zip_file); ++ die("failed to extract '%s': %s", entry_name, zip_error_strerror(error)); ++ } + + return true; + } +@@ -787,22 +802,47 @@ static int make_temporary_fd(const char* what) { + + #endif + +-static unique_fd UnzipToFile(ZipArchiveHandle zip, const char* entry_name) { ++static unique_fd UnzipToFile(unique_zip_t& zip, const char* entry_name) { + unique_fd fd(make_temporary_fd(entry_name)); + +- ZipEntry64 zip_entry; +- if (FindEntry(zip, entry_name, &zip_entry) != 0) { ++ auto zip_file = zip_fopen(zip.get(), entry_name, 0); ++ if (!zip_file) { + fprintf(stderr, "archive does not contain '%s'\n", entry_name); + errno = ENOENT; + return unique_fd(); + } + ++ zip_stat_t zstat; ++ zip_stat_init(&zstat); ++ if (zip_stat(zip.get(), entry_name, 0, &zstat) < 0 || !(zstat.valid & ZIP_STAT_SIZE)) { ++ die("size of entry '%s' could not be determined\n", entry_name); ++ } ++ + fprintf(stderr, "extracting %s (%" PRIu64 " MB) to disk...", entry_name, +- zip_entry.uncompressed_length / 1024 / 1024); ++ zstat.size / 1024 / 1024); ++ + double start = now(); +- int error = ExtractEntryToFile(zip, &zip_entry, fd.get()); +- if (error != 0) { +- die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error)); ++ while (true) { ++ // Emulate libziparchive, which uses a 32kB read/write buffer ++ const size_t kBufSize = 32768; ++ std::vector out(kBufSize); ++ auto bytes_read = ExtractToBuffer(zip_file, out); ++ if (bytes_read < 0) { ++ auto error = zip_file_get_error(zip_file); ++ die("\nfailed to extract '%s': %s", entry_name, zip_error_strerror(error)); ++ } ++ if (bytes_read == 0) { ++ break; ++ } ++ ++ ssize_t bytes_written; ++ size_t pos = 0; ++ while ((bytes_written = write(fd.get(), &out[pos], out.size() - pos)) > 0) { ++ pos += bytes_written; ++ } ++ if (bytes_written != 0) { ++ die("\nfailed to write '%s': %s", entry_name, strerror(errno)); ++ } + } + + if (lseek(fd.get(), 0, SEEK_SET) != 0) { +@@ -1954,7 +1994,7 @@ void FlashAllTool::AddFlashTasks(const std::vector* out) const { +- return UnzipToMemory(zip_, name, out); ++ return UnzipToMemory(zip_, name.c_str(), out); + } + + unique_fd ZipImageSource::OpenFile(const std::string& name) const { +@@ -1962,16 +2002,16 @@ unique_fd ZipImageSource::OpenFile(const std::string& name) const { + } + + static void do_update(const char* filename, FlashingPlan* fp) { +- ZipArchiveHandle zip; +- int error = OpenArchive(filename, &zip); +- if (error != 0) { +- die("failed to open zip file '%s': %s", filename, ErrorCodeString(error)); ++ int err; ++ unique_zip_t zip(zip_open(filename, ZIP_RDONLY, &err), zip_close); ++ if (err != 0) { ++ zip_error_t error; ++ zip_error_init_with_code(&error, err); ++ die("failed to open zip file '%s': %s", filename, zip_error_strerror(&error)); + } + fp->source.reset(new ZipImageSource(zip)); + FlashAllTool tool(fp); + tool.Flash(); +- +- CloseArchive(zip); + } + + bool LocalImageSource::ReadFile(const std::string& name, std::vector* out) const { +diff --git b/base/cvd/fastboot/fastboot.h a/base/cvd/fastboot/fastboot.h +index 6a4997049..f68f70100 100644 +--- b/base/cvd/fastboot/fastboot.h ++++ a/base/cvd/fastboot/fastboot.h +@@ -40,7 +40,9 @@ + #include "result.h" + #include "socket.h" + #include "util.h" +-#include "ziparchive/zip_archive.h" ++#include ++ ++using unique_zip_t = std::unique_ptr; + + class FastBootTool { + public: +@@ -135,12 +137,12 @@ class FlashAllTool { + + class ZipImageSource final : public ImageSource { + public: +- explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {} ++ explicit ZipImageSource(unique_zip_t& zip) : zip_(zip) {} + bool ReadFile(const std::string& name, std::vector* out) const override; + unique_fd OpenFile(const std::string& name) const override; + + private: +- ZipArchiveHandle zip_; ++ unique_zip_t& zip_; + }; + + class LocalImageSource final : public ImageSource { diff --git a/base/cvd/fastboot/patches/0003-spell-string-view-the-absl-way.patch b/base/cvd/fastboot/patches/0003-spell-string-view-the-absl-way.patch new file mode 100644 index 00000000000..052abc9694c --- /dev/null +++ b/base/cvd/fastboot/patches/0003-spell-string-view-the-absl-way.patch @@ -0,0 +1,43 @@ +diff --git a/base/cvd/fastboot/fastboot.cpp b/base/cvd/fastboot/fastboot.cpp +index 41472c286..b22ba03fc 100644 +--- a/base/cvd/fastboot/fastboot.cpp ++++ b/base/cvd/fastboot/fastboot.cpp +@@ -69,6 +69,7 @@ + #include + #include + ++#include "absl/strings/string_view.h" + #include "bootimg_utils.h" + #include "constants.h" + #include "diagnose_usb.h" +@@ -1515,7 +1516,7 @@ static void do_fetch(const std::string& partition, const std::string& slot_overr + // to the new image to flash, and return value is the real partition name to flash. + static std::string repack_ramdisk(const char* pname, struct fastboot_buffer* buf, + fastboot::IFastBootDriver* fb) { +- std::string_view pname_sv{pname}; ++ absl::string_view pname_sv{pname}; + struct fastboot_buffer dtb_buf = {.sz = 0, .fd = unique_fd(-1)}; + + if (!android::base::StartsWith(pname_sv, "vendor_boot:") && +diff --git a/base/cvd/fastboot/vendor_boot_img_utils.cpp b/base/cvd/fastboot/vendor_boot_img_utils.cpp +index da547f1bf..1a411a900 100644 +--- a/base/cvd/fastboot/vendor_boot_img_utils.cpp ++++ b/base/cvd/fastboot/vendor_boot_img_utils.cpp +@@ -23,6 +23,8 @@ + #include + #include + ++#include "absl/strings/string_view.h" ++ + namespace { + + using android::base::Result; +@@ -296,7 +298,7 @@ inline uint32_t round_up(uint32_t value, uint32_t page_size) { + for (; idx < size; idx++, entry++) { + auto entry_name_c_str = reinterpret_cast(entry->ramdisk_name); + auto entry_name_len = strnlen(entry_name_c_str, VENDOR_RAMDISK_NAME_SIZE); +- std::string_view entry_name(entry_name_c_str, entry_name_len); ++ absl::string_view entry_name(entry_name_c_str, entry_name_len); + if (entry_name == ramdisk_name) { + if (ret != nullptr) { + return Errorf("Multiple vendor ramdisk '{}' found, name should be unique", diff --git a/base/cvd/fastboot/patches/0004-use-pinned-libbase-and-liblp.patch b/base/cvd/fastboot/patches/0004-use-pinned-libbase-and-liblp.patch new file mode 100644 index 00000000000..618be47a2a5 --- /dev/null +++ b/base/cvd/fastboot/patches/0004-use-pinned-libbase-and-liblp.patch @@ -0,0 +1,243 @@ +--- a/base/cvd/fastboot/fastboot_driver.cpp ++++ b/base/cvd/fastboot/fastboot_driver.cpp +@@ -540,7 +540,7 @@ + while (remaining) { + // Memory map the file + size_t len = std::min(remaining, MAX_MAP_SIZE); +- auto mapping{android::base::MappedFile::Create(fd, offset, len, PROT_READ)}; ++ auto mapping{android::base::MappedFile::FromOsHandle(fd.get(), offset, len, PROT_READ)}; + if (!mapping) { + error_ = "Creating filemap failed"; + return IO_ERROR; +--- a/base/cvd/fastboot/result.h ++++ b/base/cvd/fastboot/result.h +@@ -27,9 +27,18 @@ + using android::base::Result; + using android::base::ResultError; + +-enum class FastbootError { +- NETWORK_SERIAL_WRONG_PREFIX = 1, +- NETWORK_SERIAL_WRONG_ADDRESS = 2, ++class FastbootError { ++ public: ++ enum Type { NETWORK_SERIAL_WRONG_PREFIX = 1, NETWORK_SERIAL_WRONG_ADDRESS = 2 }; ++ ++ FastbootError(Type&& type) : type_(std::forward(type)) {} ++ ++ Type value() const { return type_; } ++ operator Type() const { return value(); } ++ std::string print() const { return ""; } ++ ++ private: ++ Type type_; + }; + + template +--- a/base/cvd/fastboot/task.cpp ++++ b/base/cvd/fastboot/task.cpp +@@ -36,11 +36,6 @@ + // clang-format on + + using namespace std::string_literals; +-using android::fs_mgr::GetPrimaryMetadataOffset; +-using android::fs_mgr::MetadataBuilder; +-using android::fs_mgr::ParseSuperPartition; +-using android::fs_mgr::SlotNumberForSlotSuffix; +-using android::fs_mgr::ValidateAndSerializeMetadata; + + FlashTask::FlashTask(const std::string& slot, const std::string& pname, const std::string& fname, + const bool apply_vbmeta, const FlashingPlan* fp) +@@ -55,107 +50,8 @@ + return should_flash_in_userspace(*metadata.get(), task->GetPartitionAndSlot()); + } + +-fastboot::RetCode FlashPartition(fastboot::IFastBootDriver* fb, const std::string& partition_name, +- void* data, size_t size, off64_t offset) { +- // We use LP_PARTITION_RESERVED_BYTES as block size because everything in +- // liblp is aligned with this granularity +- std::unique_ptr sparse_file( +- sparse_file_new(LP_PARTITION_RESERVED_BYTES, offset + size), sparse_file_destroy); +- // Fastboot's flash protocol does not have a way to specify offset we want +- // to write to, so we use sparse file as a way around it. Blocks which +- // have no data will be skipped by bootloader, similar to seeking forward +- // on a file. +- sparse_file_add_data(sparse_file.get(), data, size, offset / LP_PARTITION_RESERVED_BYTES); +- auto ret = fb->Download(partition_name, sparse_file.get(), +- sparse_file_len(sparse_file.get(), true, false), 1, 1, false); +- if (ret != fastboot::SUCCESS) { +- return ret; +- } +- return fb->Flash(partition_name); +-} +- +-fastboot::RetCode FlashMetadata(MetadataBuilder* builder, uint32_t slot_number, +- fastboot::IFastBootDriver* fb) { +- auto metadata = builder->Export(); +- auto serialized = ValidateAndSerializeMetadata(*metadata); +- return FlashPartition(fb, "super", serialized.data(), serialized.size(), +- GetPrimaryMetadataOffset(metadata->geometry, slot_number)); +-} +- +-std::unique_ptr FetchAndParseMetadata(fastboot::IFastBootDriver* fb, +- uint32_t slot_number) { +- // For typical use cases(3 slot + 65536 max metadata size), 512K is enough +- // We reserve 1MB just to get some headroom +- constexpr size_t kMaxMetadataSize = 1024 * 1024; +- std::vector buffer; +- buffer.reserve(kMaxMetadataSize); +- // So that fb->Fetch() commands do not just crash on failure +- fastboot::NoCrashGuard guard(fb); +- auto ret = fb->Fetch("super", &buffer, 0, buffer.capacity()); +- if (ret != fastboot::SUCCESS) { +- LOG(ERROR) << "Failed to fetch first " << kMaxMetadataSize << " bytes of super partition"; +- return nullptr; +- } +- LpMetadataGeometry geometry; +- if (!android::fs_mgr::ParseGeometryFromSuperPartition(buffer.data(), buffer.size(), +- &geometry)) { +- return nullptr; +- } +- const size_t total_metadata_size = android::fs_mgr::GetTotalMetadataSize( +- geometry.metadata_max_size, geometry.metadata_slot_count); +- if (total_metadata_size > kMaxMetadataSize) { +- LOG(ERROR) << "Total metadata size " << total_metadata_size +- << " exceeds maximum supported metadata size " << kMaxMetadataSize; +- return nullptr; +- } +- auto metadata = ParseSuperPartition(buffer.data(), buffer.size(), slot_number); +- if (metadata == nullptr) { +- return nullptr; +- } +- return MetadataBuilder::New(*metadata); +-} +- +-constexpr uint64_t SectorToBlock(uint64_t sectors) { +- constexpr auto kSectorPerBlock = android::fs_mgr::kDefaultBlockSize / LP_SECTOR_SIZE; +- return sectors / kSectorPerBlock; +-} +- +-android::base::unique_fd OpenFile(const FlashingPlan* fp, const char* fname) { +- if (fp->source) { +- return fp->source->OpenFile(fname); +- } else { +- return unique_fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY))); +- } +-} +- +-// Generate a sparse image where extents from LpMetadata partition |p| is +-// filled with data from |fname| . This sparse image can then be sent over +-// fastboot protocol to flash super partition, end result is flashing a single +-// partition inside super. +-SparsePtr SparseForSuper(const char* fname, int64_t file_size, android::fs_mgr::Partition* p) { +- struct fastboot_buffer buf; +- +- SparsePtr sparse_file = SparsePtr( +- sparse_file_new(android::fs_mgr::kDefaultBlockSize, file_size), sparse_file_destroy); +- uint64_t offset = 0; +- for (const auto& extent : p->extents()) { +- auto e = extent->AsLinearExtent(); +- if (!e) { +- die("Partition on device contains a non-linear extent. unsupported."); +- } +- sparse_file_add_file(sparse_file.get(), fname, offset, +- extent->num_sectors() * LP_SECTOR_SIZE, +- SectorToBlock(e->physical_sector())); +- offset += extent->num_sectors() * LP_SECTOR_SIZE; +- } +- +- LOG(INFO) << "Transfer size for " << p->name() << " is " << offset; +- return sparse_file; +-} +- + void FlashTask::Run() { + const auto is_fastbootd = is_userspace_fastboot(fp_->fb); +- const auto slot_number = SlotNumberForSlotSuffix(fp_->current_slot); + + auto flash = [&](const std::string& partition) { + const bool is_dynamic_partition = should_flash_in_userspace(fp_->source.get(), partition); +@@ -169,48 +65,13 @@ + do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_, fp_); + return; + } +- // is_dynamic && !force_flash +- // Make last attempt to save everything by fetching super partition +- // metadata +- auto builder = FetchAndParseMetadata(fp_->fb, slot_number); +- if (builder == nullptr) { +- die("The partition you are trying to flash is dynamic, and " +- "should be flashed via fastbootd. Please run:\n" +- "\n" +- " fastboot reboot fastboot\n" +- "\n" +- "And try again. If you are intentionally trying to " +- "overwrite a fixed partition, use --force."); +- } +- auto part = builder->FindPartition(partition); +- if (part == nullptr) { +- die("The partition you are trying to flash is not a static partition, and we cannot " +- "find it in super partition either"); +- } +- auto fd = OpenFile(fp_, fname_.c_str()); +- if (fd < 0) { +- die("Failed to open input file %s : %s", fname_.c_str(), strerror(errno)); +- } +- if (is_sparse_file(fd)) { +- die("Flashing sparse images to dynamic partitions is currently not supported"); +- } +- auto file_size = get_file_size(fd); +- if (file_size <= 0) { +- die("Failed to get file size for %s : %s", fname_.c_str(), strerror(errno)); +- } +- if ((uint64_t)file_size != part->size()) { +- part->RemoveExtents(); +- builder->ResizePartition(part, file_size); +- } +- SparsePtr sparse_file = SparseForSuper(fname_.c_str(), file_size, part); +- +- auto metadata = builder->Export(); +- auto serialized = ValidateAndSerializeMetadata(*metadata); +- sparse_file_add_data(sparse_file.get(), serialized.data(), serialized.size(), +- GetPrimaryMetadataOffset(metadata->geometry, slot_number) / +- android::fs_mgr::kDefaultBlockSize); +- +- flash_partition(fp_, "super", std::move(sparse_file)); ++ die("The partition you are trying to flash is dynamic, and " ++ "should be flashed via fastbootd. Please run:\n" ++ "\n" ++ " fastboot reboot fastboot\n" ++ "\n" ++ "And try again. If you are intentionally trying to " ++ "overwrite a fixed partition, use --force."); + }; + do_for_partitions(fp_->fb, pname_, slot_, flash, true); + } +@@ -433,26 +294,12 @@ + : fp_(fp), pname_(pname), size_(size), slot_(slot) {} + + void ResizeTask::Run() { +- if (is_userspace_fastboot(fp_->fb)) { +- auto resize_partition = [this](const std::string& partition) -> void { +- if (is_logical(fp_->fb, partition)) { +- fp_->fb->ResizePartition(partition, size_); +- } +- }; +- do_for_partitions(fp_->fb, pname_, slot_, resize_partition, false); +- return; +- } +- auto slot_number = SlotNumberForSlotSuffix(fp_->current_slot); +- auto builder = FetchAndParseMetadata(fp_->fb, slot_number); +- auto resize_partition = [this, +- builder(builder.get())](const std::string& partition) mutable -> void { +- auto p = builder->FindPartition(partition); +- if (p) { +- builder->ResizePartition(p, atoll(size_.c_str())); ++ auto resize_partition = [this](const std::string& partition) -> void { ++ if (is_logical(fp_->fb, partition)) { ++ fp_->fb->ResizePartition(partition, size_); + } + }; + do_for_partitions(fp_->fb, pname_, slot_, resize_partition, false); +- FlashMetadata(builder.get(), slot_number, fp_->fb); + } + + std::string ResizeTask::ToString() const { diff --git a/base/cvd/fastboot/task.cpp b/base/cvd/fastboot/task.cpp index ea78a01b583..600ca3bd41f 100644 --- a/base/cvd/fastboot/task.cpp +++ b/base/cvd/fastboot/task.cpp @@ -13,19 +13,30 @@ // See the License for the specific language governing permissions and // limitations under the License. // -#include "task.h" -#include "fastboot_driver.h" +#include "task.h" -#include +#include #include +#include #include "fastboot.h" +#include "fastboot_driver_interface.h" #include "filesystem.h" +#include "liblp/builder.h" +#include "liblp/liblp.h" +#include "liblp/metadata_format.h" +#include "sparse/sparse.h" #include "super_flash_helper.h" #include "util.h" +// Need to include logging.h after fastboot.h, due to ERROR macro conflict +// clang-format off +#include +// clang-format on + using namespace std::string_literals; + FlashTask::FlashTask(const std::string& slot, const std::string& pname, const std::string& fname, const bool apply_vbmeta, const FlashingPlan* fp) : pname_(pname), fname_(fname), slot_(slot), apply_vbmeta_(apply_vbmeta), fp_(fp) {} @@ -40,20 +51,29 @@ bool FlashTask::IsDynamicPartition(const ImageSource* source, const FlashTask* t } void FlashTask::Run() { + const auto is_fastbootd = is_userspace_fastboot(fp_->fb); + auto flash = [&](const std::string& partition) { - if (should_flash_in_userspace(fp_->source.get(), partition) && !is_userspace_fastboot() && - !fp_->force_flash) { - die("The partition you are trying to flash is dynamic, and " - "should be flashed via fastbootd. Please run:\n" - "\n" - " fastboot reboot fastboot\n" - "\n" - "And try again. If you are intentionally trying to " - "overwrite a fixed partition, use --force."); + const bool is_dynamic_partition = should_flash_in_userspace(fp_->source.get(), partition); + // fastbootd should be able to flash both static and dynamic + // partitions, just flash it + if (is_fastbootd) { + do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_, fp_); + return; } - do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_, fp_); + if (!is_dynamic_partition || fp_->force_flash) { + do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_, fp_); + return; + } + die("The partition you are trying to flash is dynamic, and " + "should be flashed via fastbootd. Please run:\n" + "\n" + " fastboot reboot fastboot\n" + "\n" + "And try again. If you are intentionally trying to " + "overwrite a fixed partition, use --force."); }; - do_for_partitions(pname_, slot_, flash, true); + do_for_partitions(fp_->fb, pname_, slot_, flash, true); } std::string FlashTask::ToString() const { @@ -67,7 +87,7 @@ std::string FlashTask::ToString() const { std::string FlashTask::GetPartitionAndSlot() const { auto slot = slot_; if (slot.empty()) { - slot = get_current_slot(); + slot = get_current_slot(fp_->fb); } if (slot.empty()) { return pname_; @@ -84,8 +104,8 @@ RebootTask::RebootTask(const FlashingPlan* fp, const std::string& reboot_target) void RebootTask::Run() { if (reboot_target_ == "fastboot") { - if (!is_userspace_fastboot()) { - reboot_to_userspace_fastboot(); + if (!is_userspace_fastboot(fp_->fb)) { + reboot_to_userspace_fastboot(fp_->fb); fp_->fb->WaitForDisconnect(); } } else if (reboot_target_ == "recovery") { @@ -122,13 +142,15 @@ void OptimizedFlashSuperTask::Run() { // it will map in all of the embedded fds. std::vector files; if (int limit = get_sparse_limit(super_size_, fp_)) { - files = resparse_file(sparse_layout_.get(), limit); + if (!split_file(sparse_layout_.get(), limit, &files)) { + LOG(FATAL) << "Failed to resparse super partition"; + } } else { files.emplace_back(std::move(sparse_layout_)); } // Send the data to the device. - flash_partition_files(super_name_, files); + flash_partition_files(fp_->fb, super_name_, files); } std::string OptimizedFlashSuperTask::ToString() const { @@ -247,8 +269,8 @@ void UpdateSuperTask::Run() { if (fd < 0) { return; } - if (!is_userspace_fastboot()) { - reboot_to_userspace_fastboot(); + if (!is_userspace_fastboot(fp_->fb)) { + reboot_to_userspace_fastboot(fp_->fb); } std::string super_name; @@ -273,11 +295,11 @@ ResizeTask::ResizeTask(const FlashingPlan* fp, const std::string& pname, const s void ResizeTask::Run() { auto resize_partition = [this](const std::string& partition) -> void { - if (is_logical(partition)) { + if (is_logical(fp_->fb, partition)) { fp_->fb->ResizePartition(partition, size_); } }; - do_for_partitions(pname_, slot_, resize_partition, false); + do_for_partitions(fp_->fb, pname_, slot_, resize_partition, false); } std::string ResizeTask::ToString() const { diff --git a/base/cvd/fastboot/usb_linux.cpp b/base/cvd/fastboot/usb_linux.cpp index 03af8f76b4f..f82c81f54b1 100644 --- a/base/cvd/fastboot/usb_linux.cpp +++ b/base/cvd/fastboot/usb_linux.cpp @@ -152,10 +152,50 @@ static int check(void *_desc, int len, unsigned type, int size) return 0; } +static int read_sysfs_string(const char *sysfs_name, const char *sysfs_node, + char* buf, int bufsize) +{ + char path[80]; + int fd, n; + + snprintf(path, sizeof(path), + "/sys/bus/usb/devices/%s/%s", sysfs_name, sysfs_node); + path[sizeof(path) - 1] = '\0'; + + fd = open(path, O_RDONLY); + if (fd < 0) + return -1; + + n = read(fd, buf, bufsize - 1); + close(fd); + + if (n < 0) + return -1; + + buf[n] = '\0'; + + return n; +} + +static int read_sysfs_number(const char *sysfs_name, const char *sysfs_node) +{ + char buf[16]; + int value; + + if (read_sysfs_string(sysfs_name, sysfs_node, buf, sizeof(buf)) < 0) + return -1; + + if (sscanf(buf, "%d", &value) != 1) + return -1; + + return value; +} + static int filter_usb_device(char* sysfs_name, char *ptr, int len, int writable, ifc_match_func callback, - int *ept_in_id, int *ept_out_id, int *ifc_id) + int *ept_in_id, int *ept_out_id, int *ifc_id, + int *cfg_id, int *alt_ifc_id) { struct usb_device_descriptor *dev; struct usb_config_descriptor *cfg; @@ -164,7 +204,6 @@ static int filter_usb_device(char* sysfs_name, struct usb_ifc_info info; int in, out; - unsigned i; unsigned e; if (check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE)) @@ -173,11 +212,35 @@ static int filter_usb_device(char* sysfs_name, len -= dev->bLength; ptr += dev->bLength; - if (check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE)) + *cfg_id = read_sysfs_number(sysfs_name, "bConfigurationValue"); + if (*cfg_id == -1) + return -1; + + // Find the active configuration + do { + if (check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE)) + return -1; + cfg = (struct usb_config_descriptor *)ptr; + if (cfg->bConfigurationValue == *cfg_id) { + // This is the configuration we are looking for + len -= cfg->bLength; + ptr += cfg->bLength; + break; + } + // Skip this configuration + len -= cfg->wTotalLength; + ptr += cfg->wTotalLength; + } while (1); + + // There is less data then it should be based on config descriptor. + if (len < cfg->wTotalLength - cfg->bLength) { + DBG("Config %d has 0x%x bytes length, but only 0x%x bytes are left\n", + cfg->bConfigurationValue, cfg->wTotalLength, len + cfg->bLength); return -1; - cfg = (struct usb_config_descriptor *)ptr; - len -= cfg->bLength; - ptr += cfg->bLength; + } + + // At this point, only interfaces within active configuration are interesting. + len = cfg->wTotalLength - cfg->bLength; info.dev_vendor = dev->idVendor; info.dev_product = dev->idProduct; @@ -196,41 +259,26 @@ static int filter_usb_device(char* sysfs_name, */ info.serial_number[0] = '\0'; if (dev->iSerialNumber) { - char path[80]; - int fd; - - snprintf(path, sizeof(path), - "/sys/bus/usb/devices/%s/serial", sysfs_name); - path[sizeof(path) - 1] = '\0'; - - fd = open(path, O_RDONLY); - if (fd >= 0) { - int chars_read = read(fd, info.serial_number, - sizeof(info.serial_number) - 1); - close(fd); - - if (chars_read <= 0) - info.serial_number[0] = '\0'; - else if (info.serial_number[chars_read - 1] == '\n') { - // strip trailing newline - info.serial_number[chars_read - 1] = '\0'; - } + int chars_read = read_sysfs_string(sysfs_name, "serial", info.serial_number, + sizeof(info.serial_number) - 1); + + if (chars_read > 0 && info.serial_number[chars_read - 1] == '\n') { + // strip trailing newline + info.serial_number[chars_read - 1] = '\0'; } } - for(i = 0; i < cfg->bNumInterfaces; i++) { - - while (len > 0) { - struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr; - if (check(hdr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE) == 0) - break; + while (len > 0) { + struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr; + if (check(hdr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE)) { + // This code parses only standard interfaces, skip everything else + // (e.g interface association descriptor). len -= hdr->bLength; ptr += hdr->bLength; + DBG("Skip interface of type 0x%02x\n", hdr->bDescriptorType); + continue; } - if (len <= 0) - return -1; - ifc = (struct usb_interface_descriptor *)ptr; len -= ifc->bLength; ptr += ifc->bLength; @@ -291,6 +339,7 @@ static int filter_usb_device(char* sysfs_name, *ept_in_id = in; *ept_out_id = out; *ifc_id = ifc->bInterfaceNumber; + *alt_ifc_id = ifc->bAlternateSetting; return 0; } } @@ -298,45 +347,6 @@ static int filter_usb_device(char* sysfs_name, return -1; } -static int read_sysfs_string(const char *sysfs_name, const char *sysfs_node, - char* buf, int bufsize) -{ - char path[80]; - int fd, n; - - snprintf(path, sizeof(path), - "/sys/bus/usb/devices/%s/%s", sysfs_name, sysfs_node); - path[sizeof(path) - 1] = '\0'; - - fd = open(path, O_RDONLY); - if (fd < 0) - return -1; - - n = read(fd, buf, bufsize - 1); - close(fd); - - if (n < 0) - return -1; - - buf[n] = '\0'; - - return n; -} - -static int read_sysfs_number(const char *sysfs_name, const char *sysfs_node) -{ - char buf[16]; - int value; - - if (read_sysfs_string(sysfs_name, sysfs_node, buf, sizeof(buf)) < 0) - return -1; - - if (sscanf(buf, "%d", &value) != 1) - return -1; - - return value; -} - /* Given the name of a USB device in sysfs, get the name for the same * device in devfs. Returns 0 for success, -1 for failure. */ @@ -362,7 +372,7 @@ static std::unique_ptr find_usb_device(const char* base, ifc_match_f std::unique_ptr usb; char devname[64]; char desc[1024]; - int n, in, out, ifc; + int n, in, out, ifc, cfg, alt_ifc; struct dirent *de; int fd; @@ -389,7 +399,8 @@ static std::unique_ptr find_usb_device(const char* base, ifc_match_f n = read(fd, desc, sizeof(desc)); - if (filter_usb_device(de->d_name, desc, n, writable, callback, &in, &out, &ifc) == 0) { + if (filter_usb_device(de->d_name, desc, n, writable, callback, + &in, &out, &ifc, &cfg, &alt_ifc) == 0) { usb.reset(new usb_handle()); strcpy(usb->fname, devname); usb->ep_in = in; @@ -402,6 +413,26 @@ static std::unique_ptr find_usb_device(const char* base, ifc_match_f usb.reset(); continue; } + if (read_sysfs_number(de->d_name, "bConfigurationValue") != cfg) { + // Someone changed configuration before we claimed the interface + close(fd); + usb.reset(); + continue; + } + // Select alternate setting if it is not default. + if (alt_ifc != 0) { + struct usbdevfs_setinterface set_ifc = { + .interface = (unsigned int)ifc, + .altsetting = (unsigned int)alt_ifc, + }; + + n = ioctl(fd, USBDEVFS_SETINTERFACE, &set_ifc); + if (n != 0) { + close(fd); + usb.reset(); + continue; + } + } } else { close(fd); } diff --git a/base/cvd/fastboot/util.cpp b/base/cvd/fastboot/util.cpp index 5966aead6a1..980f8535c05 100644 --- a/base/cvd/fastboot/util.cpp +++ b/base/cvd/fastboot/util.cpp @@ -29,10 +29,11 @@ #include #include #include -#include #include #include +#include +#include #include #include @@ -118,3 +119,68 @@ std::string fb_fix_numeric_var(std::string var) { if (!android::base::StartsWith(var, "0x")) var = "0x" + var; return var; } + +bool split_file(int fd, int64_t max_size, std::vector* out) { + if (max_size < 0 || max_size > std::numeric_limits::max()) { + LOG(ERROR) << "invalid max size: " << max_size; + return false; + } + + SparsePtr s(sparse_file_import(fd, true, false), sparse_file_destroy); + if (!s) { + int64_t size = get_file_size(fd); + int block_size; + if (size % 4096 == 0) { + block_size = 4096; + } else if (size % 512 == 0) { + block_size = 512; + } else { + // Resparsing this file will fail, since it is unaligned. libsparse + // will either infinite loop or error out. + LOG(ERROR) << "File size " << size + << " is unaligned, and greater than the maximum download size " << max_size + << ", unable to resparse for transfer."; + return false; + } + + if (lseek(fd, 0, SEEK_SET) < 0) { + PLOG(ERROR) << "lseek failed"; + return false; + } + + s = SparsePtr(sparse_file_new(block_size, size), sparse_file_destroy); + if (!s || sparse_file_read(s.get(), fd, SPARSE_READ_MODE_NORMAL, false) < 0) { + LOG(ERROR) << "Failed to read file into sparse buffer"; + return false; + } + } + + // We're now guaranteed to have a sparse file. If the sparse len < download size, don't split. + int64_t sparse_len = sparse_file_len(s.get(), false, false); + if (sparse_len <= max_size) { + out->emplace_back(std::move(s)); + return true; + } + + return split_file(s.get(), max_size, out); +} + +bool split_file(sparse_file* s, int64_t max_size, std::vector* out) { + const int files = sparse_file_resparse(s, max_size, nullptr, 0); + if (files < 0) { + LOG(ERROR) << "Failed to compute resparse boundaries"; + return false; + } + + auto temp = std::make_unique(files); + const int rv = sparse_file_resparse(s, max_size, temp.get(), files); + if (rv < 0) { + LOG(ERROR) << "Failed to resparse"; + return false; + } + + for (int i = 0; i < files; i++) { + out->emplace_back(temp[i], sparse_file_destroy); + } + return true; +} diff --git a/base/cvd/fastboot/util.h b/base/cvd/fastboot/util.h index e737726dd7d..b83fa96111b 100644 --- a/base/cvd/fastboot/util.h +++ b/base/cvd/fastboot/util.h @@ -16,7 +16,6 @@ #pragma once -#include #include #include @@ -47,6 +46,8 @@ bool should_flash_in_userspace(const android::fs_mgr::LpMetadata& metadata, bool is_sparse_file(android::base::borrowed_fd fd); int64_t get_file_size(android::base::borrowed_fd fd); std::string fb_fix_numeric_var(std::string var); +bool split_file(int fd, int64_t max_size, std::vector* out); +bool split_file(sparse_file* s, int64_t max_size, std::vector* out); class ImageSource { public: diff --git a/base/cvd/fec/libfec/fec_open.cpp b/base/cvd/fec/libfec/fec_open.cpp index 6825942b618..8affa4edc79 100644 --- a/base/cvd/fec/libfec/fec_open.cpp +++ b/base/cvd/fec/libfec/fec_open.cpp @@ -19,7 +19,6 @@ #include #include -#include #if defined(__linux__) #include @@ -48,7 +47,7 @@ static int find_offset(uint64_t file_size, int roots, uint64_t *offset, if (file_size % FEC_BLOCKSIZE) { /* must be a multiple of block size */ - error("file size not multiple of " stringify(FEC_BLOCKSIZE)); + error("file size not multiple of " STRINGIFY(FEC_BLOCKSIZE)); errno = EINVAL; return -1; } @@ -215,34 +214,6 @@ static int parse_ecc(fec_handle *f, uint64_t offset) return -1; } -/* reads the squashfs superblock and returns the size of the file system in - `offset' */ -static int get_squashfs_size(fec_handle *f, uint64_t *offset) -{ - check(f); - check(offset); - - size_t sb_size = squashfs_get_sb_size(); - check(sb_size <= SSIZE_MAX); - - uint8_t buffer[sb_size]; - - if (fec_pread(f, buffer, sizeof(buffer), 0) != (ssize_t)sb_size) { - error("failed to read superblock: %s", strerror(errno)); - return -1; - } - - squashfs_info sq; - - if (squashfs_parse_sb_buffer(buffer, &sq) < 0) { - error("failed to parse superblock: %s", strerror(errno)); - return -1; - } - - *offset = sq.bytes_used_4K_padded; - return 0; -} - /* reads the ext4 superblock and returns the size of the file system in `offset' */ static int get_ext4_size(fec_handle *f, uint64_t *offset) @@ -279,8 +250,6 @@ static int get_fs_size(fec_handle *f, uint64_t *offset) if (f->flags & FEC_FS_EXT4) { return get_ext4_size(f, offset); - } else if (f->flags & FEC_FS_SQUASH) { - return get_squashfs_size(f, offset); } else { /* try all alternatives */ int rc = get_ext4_size(f, offset); @@ -289,13 +258,6 @@ static int get_fs_size(fec_handle *f, uint64_t *offset) debug("found ext4fs"); return rc; } - - rc = get_squashfs_size(f, offset); - - if (rc == 0) { - debug("found squashfs"); - } - return rc; } } diff --git a/base/cvd/fec/libfec/fec_private.h b/base/cvd/fec/libfec/fec_private.h index 0c633223e1e..14697a9ac4b 100644 --- a/base/cvd/fec/libfec/fec_private.h +++ b/base/cvd/fec/libfec/fec_private.h @@ -30,6 +30,9 @@ #include #include + +#define STRINGIFY_INNER(x) #x +#define STRINGIFY(x) STRINGIFY_INNER(x) #include #include #include @@ -173,11 +176,6 @@ extern int verity_parse_header(fec_handle *f, uint64_t offset); #define likely(x) __builtin_expect(!!(x), 1) #endif -#ifndef stringify - #define __stringify(x) #x - #define stringify(x) __stringify(x) -#endif - /* warnings, errors, debug output */ #ifdef FEC_NO_KLOG #define __log(func, type, format, args...) \ diff --git a/base/cvd/fec/libfec/fec_verity.cpp b/base/cvd/fec/libfec/fec_verity.cpp index 45c197ff4af..8d208c06bb3 100644 --- a/base/cvd/fec/libfec/fec_verity.cpp +++ b/base/cvd/fec/libfec/fec_verity.cpp @@ -373,7 +373,7 @@ static int parse_table(fec_handle *f, uint64_t offset, uint32_t size, bool useec for (const auto& token : tokens) { switch (i++) { case 0: /* version */ - if (token != stringify(VERITY_TABLE_VERSION)) { + if (token != STRINGIFY(VERITY_TABLE_VERSION)) { error("unsupported verity table version: %s", token.c_str()); return -1; } @@ -381,7 +381,7 @@ static int parse_table(fec_handle *f, uint64_t offset, uint32_t size, bool useec case 3: /* data_block_size */ case 4: /* hash_block_size */ /* assume 4 KiB block sizes for everything */ - if (token != stringify(FEC_BLOCKSIZE)) { + if (token != STRINGIFY(FEC_BLOCKSIZE)) { error("unsupported verity block size: %s", token.c_str()); return -1; } @@ -436,7 +436,7 @@ static int parse_table(fec_handle *f, uint64_t offset, uint32_t size, bool useec if (i < VERITY_TABLE_ARGS) { error("not enough arguments in verity table: %d; expected at least " - stringify(VERITY_TABLE_ARGS), i); + STRINGIFY(VERITY_TABLE_ARGS), i); return -1; } @@ -512,8 +512,8 @@ static int validate_header(const fec_handle *f, const verity_header *header, if (header->length < VERITY_MIN_TABLE_SIZE || header->length > VERITY_MAX_TABLE_SIZE) { error("invalid verity table size: %u; expected [" - stringify(VERITY_MIN_TABLE_SIZE) ", " - stringify(VERITY_MAX_TABLE_SIZE) ")", header->length); + STRINGIFY(VERITY_MIN_TABLE_SIZE) ", " + STRINGIFY(VERITY_MAX_TABLE_SIZE) ")", header->length); return -1; } diff --git a/base/cvd/fec/patches/0001-define-stringify-locally.patch b/base/cvd/fec/patches/0001-define-stringify-locally.patch new file mode 100644 index 00000000000..29b882146a0 --- /dev/null +++ b/base/cvd/fec/patches/0001-define-stringify-locally.patch @@ -0,0 +1,14 @@ +--- a/base/cvd/fec/libfec/fec_private.h ++++ b/base/cvd/fec/libfec/fec_private.h +@@ -29,8 +29,10 @@ + #include + #include + +-#include + #include ++ ++#define STRINGIFY_INNER(x) #x ++#define STRINGIFY(x) STRINGIFY_INNER(x) + #include + #include + #include diff --git a/base/cvd/mkbootfs/patches/0001-restore-license-banner.patch b/base/cvd/mkbootfs/patches/0001-restore-license-banner.patch new file mode 100644 index 00000000000..4f3a622ebc4 --- /dev/null +++ b/base/cvd/mkbootfs/patches/0001-restore-license-banner.patch @@ -0,0 +1,21 @@ +--- b/base/cvd/mkbootfs/mkbootfs.cpp ++++ a/base/cvd/mkbootfs/mkbootfs.cpp +@@ -1,3 +1,18 @@ ++/* ++ * Copyright (C) 2008 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. ++ */ + + #include + #include diff --git a/base/cvd/update_aosp_sources.sh b/base/cvd/update_aosp_sources.sh new file mode 100755 index 00000000000..1b7092bdf5f --- /dev/null +++ b/base/cvd/update_aosp_sources.sh @@ -0,0 +1,309 @@ +#!/bin/bash + +set -euo pipefail + +usage() { + echo "usage: $0 " >&2 + exit 1 +} + +if [[ $# -ne 1 || -z "$1" ]]; then + usage +fi + +android_root="$1" +if [[ ! -d "${android_root}" ]]; then + echo "ERROR: ${android_root} is not a directory!" >&2 + exit 1 +fi + +# Guard against accidentally running against an internal Android tree. +if [[ -d "${android_root}/vendor/google_devices/cuttlefish_common" ]]; then + echo "ERROR: ${android_root} appears to be an internal Android tree (found vendor/google_devices/cuttlefish_common)." >&2 + echo "This script must only be run against an AOSP / publicly released Android tree." >&2 + exit 1 +fi + +check_not_internal_repo() { + local dir="$1" + if command -v git >/dev/null 2>&1 && git -C "${dir}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then + if git -C "${dir}" remote -v 2>/dev/null | grep -qi "googleplex-android"; then + echo "ERROR: ${dir} has a 'googleplex-android' git remote, which indicates an internal Android tree." >&2 + echo "This script must only be run against an AOSP / publicly released Android tree." >&2 + exit 1 + fi + fi +} + +script_dir="$(dirname "$(readlink -f "$0")")" +cd "${script_dir}" + +declare -A SOURCE_DIRS +register_source_dir() { + local key="$1" + local path="$2" + if [[ ! -d "${path}" ]]; then + echo "ERROR: ${path} does not exist!" >&2 + exit 1 + fi + check_not_internal_repo "${path}" + SOURCE_DIRS["${key}"]="${path}" +} + +register_source_dir adb "${android_root}/packages/modules/adb" +register_source_dir diagnose_usb "${android_root}/system/core/diagnose_usb" +register_source_dir fastboot "${android_root}/system/core/fastboot" +register_source_dir libstorage_literals "${android_root}/system/fs/fs_mgr/libstorage_literals" +register_source_dir libcrypto_utils "${android_root}/system/core/libcrypto_utils" +register_source_dir mkbootfs "${android_root}/system/core/mkbootfs" +register_source_dir ext4_utils "${android_root}/system/extras/ext4_utils" +register_source_dir libfec "${android_root}/system/extras/libfec" +register_source_dir verity "${android_root}/system/extras/verity" + +DEST_DIRS=("adb" "fastboot" "fec" "libext4_utils" "mkbootfs") +for dir in "${DEST_DIRS[@]}"; do + if [[ ! -d "${dir}" ]]; then + echo "ERROR: ${dir} does not exist!" >&2 + exit 1 + fi +done + +echo "WARNING: This script MUST be run against an AOSP / publicly released Android tree." +echo "WARNING: Do not run it against an internal Android tree under any circumstance!" +echo +while true; do + read -r -p "Do you understand the above instructions? " yn + case "${yn}" in + [Yy]* ) break;; + [Nn]* ) exit 1;; + * ) echo "Please answer yes or no.";; + esac +done +echo + +work_dir="$(mktemp -d)" +cleanup() { + rm -rf "${work_dir}" +} +trap cleanup EXIT + +preserved_files=() + +preserve_common() { + local dest="$1" + local staging="$2" + if [[ -f "${dest}/BUILD.bazel" ]]; then + cp -a "${dest}/BUILD.bazel" "${staging}/BUILD.bazel" + fi + if [[ -d "${dest}/patches" ]]; then + cp -a "${dest}/patches" "${staging}/patches" + fi +} + +preserve_file() { + local dest="$1" + local staging="$2" + local rel_path="$3" + local src_path="${dest}/${rel_path}" + if [[ ! -e "${src_path}" ]]; then + echo "ERROR: Expected preserved file ${src_path} does not exist!" >&2 + exit 1 + fi + mkdir -p "${staging}/$(dirname "${rel_path}")" + cp -a "${src_path}" "${staging}/${rel_path}" + preserved_files+=("${src_path}") +} + +update_adb() { + local staging="$1" + + mkdir -p "${staging}/diagnose_usb" "${staging}/libcrypto_utils" + cp -a "${SOURCE_DIRS[adb]}/." "${staging}/" + cp -a "${SOURCE_DIRS[diagnose_usb]}/." "${staging}/diagnose_usb/" + cp -a "${SOURCE_DIRS[libcrypto_utils]}/." "${staging}/libcrypto_utils/" + + ( + cd "${staging}" + + # Remove Android build system and metadata + find . \( -name Android.bp -o -name OWNERS \) -delete + rm -f adbd_flags.aconfig METADATA MODULE_LICENSE_* NOTICE PREUPLOAD.cfg rustfmt.toml TEST_MAPPING + rm -rf apex + + # Remove coverage instrumentation + rm -rf coverage + + # Remove documentation + rm -rf docs/ README.md sockets.dia + + # Remove tools + rm -rf adb.bash tools/ + + # Remove symbol map text files, as the modules using them are not built + find . -name '*.map.txt' -delete + + # Remove osx and windows support + find . \( -name '*_osx*' -o -name 'fdevent_poll*' -o -name '*_windows*' -o -name '*win32*' \) -exec rm -rf {} + + + # Remove all tests, as they are not built and have additional dependencies + find . \( -name '*_test.*' -o -name tests -o -name 'test_*' \) -exec rm -rf {} + + rm -f adb_integration_test_* run-device-tests.sh + + # Remove all benchmarks, as they are not built and have additional dependencies + find . \( -name '*_benchmark.*' -o -name 'benchmark_*' -o -name trace.sh \) -exec rm -rf {} + + + # Remove fastdeploy + rm -rf client/fastdeploy.* fastdeploy/ proto/jarjar-rules.txt + + # Remove adbmdns and openscreen mDNS backends + rm -rf client/adbmdns/ client/openscreen/ + + # Remove adb device-side daemon sources + rm -rf daemon/ libs/ security_log_tags.h transfer_id.h + if [[ -d pairing_connection ]]; then + find pairing_connection \( -name 'pairing_server*' -o -name internal \) -exec rm -rf {} + + fi + ) + + preserve_common "adb" "${staging}" + # version.h and platform_tools_version.h are generated files in AOSP + preserve_file "adb" "${staging}" "version/build/version.h" + preserve_file "adb" "${staging}" "version/platform_tools_version.h" +} + +update_fastboot() { + local staging="$1" + + mkdir -p "${staging}/diagnose_usb" "${staging}/libstorage_literals" + cp -a "${SOURCE_DIRS[fastboot]}/." "${staging}/" + cp -a "${SOURCE_DIRS[diagnose_usb]}/." "${staging}/diagnose_usb/" + cp -a "${SOURCE_DIRS[libstorage_literals]}/." "${staging}/libstorage_literals/" + + ( + cd "${staging}" + + # Remove fuzzers + rm -rf fuzzer fuzzy_fastboot + + # Remove osx and windows support + find . \( -name '*_osx*' -o -name '*_windows*' \) -exec rm -rf {} + + + # Remove Android build system and metadata + find . \( -name Android.bp -o -name OWNERS \) -delete + rm -f LICENSE README.md TEST_MAPPING + + # Remove device-side implementation + rm -rf device + + # Remove tools + rm -rf fastboot.bash + + # Remove all tests, as they are not built and have additional dependencies + find . \( \ + -name '*_test.*' -o -name 'test_*' -o -name tests -o \ + -name '*_mock.*' -o -name 'mock_*' \ + \) -exec rm -rf {} + + rm -rf testdata + ) + + preserve_common "fastboot" "${staging}" + # version.h and platform_tools_version.h are generated files in AOSP + preserve_file "fastboot" "${staging}" "version/build/version.h" + preserve_file "fastboot" "${staging}" "version/platform_tools_version.h" +} + +update_fec() { + local staging="$1" + + mkdir -p "${staging}/libcrypto_utils/include" "${staging}/libfec" "${staging}/verity/fec" + cp -a "${SOURCE_DIRS[libcrypto_utils]}/include/." "${staging}/libcrypto_utils/include/" + cp -a "${SOURCE_DIRS[libfec]}/." "${staging}/libfec/" + cp -a "${SOURCE_DIRS[verity]}/fec/." "${staging}/verity/fec/" + + ( + cd "${staging}" + + # Remove Android build system and metadata + find . \( -name Android.bp -o -name OWNERS -o -name NOTICE \) -delete + + # Remove all tests, as they are not built and have additional dependencies + find . \( -name test -o -name tests \) -exec rm -rf {} + + ) + + preserve_common "fec" "${staging}" + # Compat.h is stubbed as it is not used + preserve_file "fec" "${staging}" "libutils/include/utils/Compat.h" + # squashfs_utils.h is stubbed to avoid a libsquashfs_utils dependency + preserve_file "fec" "${staging}" "squashfs_utils/squashfs_utils.h" +} + +update_libext4_utils() { + local staging="$1" + + cp -a "${SOURCE_DIRS[ext4_utils]}/." "${staging}/" + + ( + cd "${staging}" + chmod a+x mkuserimg_mke2fs.py + + # Remove Android build system and metadata + rm -f Android.bp MODULE_LICENSE_* NOTICE OWNERS + + # Remove unused blk_alloc_to_base_fs and "wipe" APIs + rm -f blk_alloc_to_base_fs.cpp include/ext4_utils/wipe.h wipe.cpp + + # Remove all tests, as they are not built and have additional dependencies + find . -name 'test_*' -delete + ) + + preserve_common "libext4_utils" "${staging}" +} + +update_mkbootfs() { + local staging="$1" + + cp -a "${SOURCE_DIRS[mkbootfs]}/." "${staging}/" + + ( + cd "${staging}" + + # Remove Android build system + rm -f Android.bp + ) + + preserve_common "mkbootfs" "${staging}" +} + +# Stage, prune, replace, and patch the upstream sources +for dir in "${DEST_DIRS[@]}"; do + staging="${work_dir}/${dir}" + mkdir -p "${staging}" + "update_${dir}" "${staging}" + rm -rf "${dir}" + mv "${staging}" "${dir}" + + if [[ -d "${dir}/patches" ]]; then + ( + cd "${dir}" + shopt -s nullglob + slash_count="${dir//[^\/]/}" + level=$((4 + ${#slash_count})) + for patch_file in patches/*.patch; do + echo "Applying patch ${dir}/${patch_file}..." + if ! patch --no-backup-if-mismatch -Np"${level}" -i "${patch_file}"; then + echo "WARNING: Patch ${dir}/${patch_file} did not apply cleanly." >&2 + fi + echo + done + ) + fi +done + +if [[ ${#preserved_files[@]} -gt 0 ]]; then + echo "WARNING: If necessary, please update:" + echo + printf '%s\n' "${preserved_files[@]}" + echo +fi + +echo "Done!"