diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2019-04-30 05:15:13 +0200 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2019-04-30 05:15:13 +0200 |
commit | e1299d94095e5fc018472b27cc8d980234f2ffb2 (patch) | |
tree | b0e3acf9305aec92ab4f388b6948caeccb05a8f7 /minadbd/minadbd_services.cpp | |
parent | Snap for 5480912 from d0052c30cef1202fff6bb0f87d64eddb98cfbc99 to qt-release (diff) | |
parent | Parse BCB command to enter rescue mode. (diff) | |
download | android_bootable_recovery-e1299d94095e5fc018472b27cc8d980234f2ffb2.tar android_bootable_recovery-e1299d94095e5fc018472b27cc8d980234f2ffb2.tar.gz android_bootable_recovery-e1299d94095e5fc018472b27cc8d980234f2ffb2.tar.bz2 android_bootable_recovery-e1299d94095e5fc018472b27cc8d980234f2ffb2.tar.lz android_bootable_recovery-e1299d94095e5fc018472b27cc8d980234f2ffb2.tar.xz android_bootable_recovery-e1299d94095e5fc018472b27cc8d980234f2ffb2.tar.zst android_bootable_recovery-e1299d94095e5fc018472b27cc8d980234f2ffb2.zip |
Diffstat (limited to 'minadbd/minadbd_services.cpp')
-rw-r--r-- | minadbd/minadbd_services.cpp | 153 |
1 files changed, 135 insertions, 18 deletions
diff --git a/minadbd/minadbd_services.cpp b/minadbd/minadbd_services.cpp index 79e6fc4e0..9b1999d90 100644 --- a/minadbd/minadbd_services.cpp +++ b/minadbd/minadbd_services.cpp @@ -28,15 +28,19 @@ #include <string> #include <string_view> #include <thread> +#include <unordered_set> #include <android-base/file.h> #include <android-base/logging.h> #include <android-base/memory.h> +#include <android-base/parseint.h> +#include <android-base/properties.h> #include <android-base/stringprintf.h> #include <android-base/strings.h> #include "adb.h" #include "adb_unique_fd.h" +#include "adb_utils.h" #include "fdevent.h" #include "fuse_adb_provider.h" #include "fuse_sideload.h" @@ -45,11 +49,22 @@ #include "sysdeps.h" static int minadbd_socket = -1; +static bool rescue_mode = false; +static std::string sideload_mount_point = FUSE_SIDELOAD_HOST_MOUNTPOINT; + void SetMinadbdSocketFd(int socket_fd) { minadbd_socket = socket_fd; } -static bool WriteCommandToFd(MinadbdCommands cmd, int fd) { +void SetMinadbdRescueMode(bool rescue) { + rescue_mode = rescue; +} + +void SetSideloadMountPoint(const std::string& path) { + sideload_mount_point = path; +} + +static bool WriteCommandToFd(MinadbdCommand cmd, int fd) { char message[kMinadbdMessageSize]; memcpy(message, kMinadbdCommandPrefix, strlen(kMinadbdStatusPrefix)); android::base::put_unaligned(message + strlen(kMinadbdStatusPrefix), cmd); @@ -81,47 +96,149 @@ static bool WaitForCommandStatus(int fd, MinadbdCommandStatus* status) { return true; } -static void sideload_host_service(unique_fd sfd, const std::string& args) { +static MinadbdErrorCode RunAdbFuseSideload(int sfd, const std::string& args, + MinadbdCommandStatus* status) { + auto pieces = android::base::Split(args, ":"); int64_t file_size; int block_size; - if ((sscanf(args.c_str(), "%" SCNd64 ":%d", &file_size, &block_size) != 2) || file_size <= 0 || - block_size <= 0) { + if (pieces.size() != 2 || !android::base::ParseInt(pieces[0], &file_size) || file_size <= 0 || + !android::base::ParseInt(pieces[1], &block_size) || block_size <= 0) { LOG(ERROR) << "bad sideload-host arguments: " << args; - exit(kMinadbdPackageSizeError); + return kMinadbdPackageSizeError; } LOG(INFO) << "sideload-host file size " << file_size << ", block size " << block_size; - if (!WriteCommandToFd(MinadbdCommands::kInstall, minadbd_socket)) { - exit(kMinadbdSocketIOError); + if (!WriteCommandToFd(MinadbdCommand::kInstall, minadbd_socket)) { + return kMinadbdSocketIOError; } - auto adb_data_reader = - std::make_unique<FuseAdbDataProvider>(std::move(sfd), file_size, block_size); - if (int result = run_fuse_sideload(std::move(adb_data_reader)); result != 0) { + auto adb_data_reader = std::make_unique<FuseAdbDataProvider>(sfd, file_size, block_size); + if (int result = run_fuse_sideload(std::move(adb_data_reader), sideload_mount_point.c_str()); + result != 0) { LOG(ERROR) << "Failed to start fuse"; - exit(kMinadbdFuseStartError); + return kMinadbdFuseStartError; + } + + if (!WaitForCommandStatus(minadbd_socket, status)) { + return kMinadbdMessageFormatError; } + // Signal host-side adb to stop. For sideload mode, we always send kSideloadServiceExitSuccess + // (i.e. "DONEDONE") regardless of the install result. For rescue mode, we send failure message on + // install error. + if (!rescue_mode || *status == MinadbdCommandStatus::kSuccess) { + if (!android::base::WriteFully(sfd, kSideloadServiceExitSuccess, + strlen(kSideloadServiceExitSuccess))) { + return kMinadbdHostSocketIOError; + } + } else { + if (!android::base::WriteFully(sfd, kSideloadServiceExitFailure, + strlen(kSideloadServiceExitFailure))) { + return kMinadbdHostSocketIOError; + } + } + + return kMinadbdSuccess; +} + +// Sideload service always exits after serving an install command. +static void SideloadHostService(unique_fd sfd, const std::string& args) { + MinadbdCommandStatus status; + exit(RunAdbFuseSideload(sfd.get(), args, &status)); +} + +// Rescue service waits for the next command after an install command. +static void RescueInstallHostService(unique_fd sfd, const std::string& args) { + MinadbdCommandStatus status; + if (auto result = RunAdbFuseSideload(sfd.get(), args, &status); result != kMinadbdSuccess) { + exit(result); + } +} + +static void RescueGetpropHostService(unique_fd sfd, const std::string& prop) { + static const std::unordered_set<std::string> kGetpropAllowedProps = { + "ro.build.fingerprint", + "ro.build.date.utc", + }; + auto allowed = kGetpropAllowedProps.find(prop) != kGetpropAllowedProps.end(); + if (!allowed) { + return; + } + + auto result = android::base::GetProperty(prop, ""); + if (result.empty()) { + return; + } + if (!android::base::WriteFully(sfd, result.data(), result.size())) { + exit(kMinadbdHostSocketIOError); + } +} + +// Reboots into the given target. We don't reboot directly from minadbd, but going through recovery +// instead. This allows recovery to finish all the pending works (clear BCB, save logs etc) before +// the reboot. +static void RebootHostService(unique_fd /* sfd */, const std::string& target) { + MinadbdCommand command; + if (target == "bootloader") { + command = MinadbdCommand::kRebootBootloader; + } else if (target == "rescue") { + command = MinadbdCommand::kRebootRescue; + } else if (target == "recovery") { + command = MinadbdCommand::kRebootRecovery; + } else if (target == "fastboot") { + command = MinadbdCommand::kRebootFastboot; + } else { + command = MinadbdCommand::kRebootAndroid; + } + if (!WriteCommandToFd(command, minadbd_socket)) { + exit(kMinadbdSocketIOError); + } MinadbdCommandStatus status; if (!WaitForCommandStatus(minadbd_socket, &status)) { exit(kMinadbdMessageFormatError); } - LOG(INFO) << "Got command status: " << static_cast<unsigned int>(status); - - LOG(INFO) << "sideload_host finished"; - exit(kMinadbdSuccess); } unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) { + // Common services that are supported both in sideload and rescue modes. + if (ConsumePrefix(&name, "reboot:")) { + // "reboot:<target>", where target must be one of the following. + std::string args(name); + if (args.empty() || args == "bootloader" || args == "rescue" || args == "recovery" || + args == "fastboot") { + return create_service_thread("reboot", + std::bind(RebootHostService, std::placeholders::_1, args)); + } + return unique_fd{}; + } + + // Rescue-specific services. + if (rescue_mode) { + if (ConsumePrefix(&name, "rescue-install:")) { + // rescue-install:<file-size>:<block-size> + std::string args(name); + return create_service_thread( + "rescue-install", std::bind(RescueInstallHostService, std::placeholders::_1, args)); + } else if (ConsumePrefix(&name, "rescue-getprop:")) { + // rescue-getprop:<prop> + std::string args(name); + return create_service_thread( + "rescue-getprop", std::bind(RescueGetpropHostService, std::placeholders::_1, args)); + } + return unique_fd{}; + } + + // Sideload-specific services. if (name.starts_with("sideload:")) { // This exit status causes recovery to print a special error message saying to use a newer adb // (that supports sideload-host). exit(kMinadbdAdbVersionError); - } else if (name.starts_with("sideload-host:")) { - std::string arg(name.substr(strlen("sideload-host:"))); + } else if (ConsumePrefix(&name, "sideload-host:")) { + // sideload-host:<file-size>:<block-size> + std::string args(name); return create_service_thread("sideload-host", - std::bind(sideload_host_service, std::placeholders::_1, arg)); + std::bind(SideloadHostService, std::placeholders::_1, args)); } return unique_fd{}; } |