Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 17 | #include "minadbd_services.h" |
| 18 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 25 | |
Kelvin Zhang | e32c9f7 | 2020-12-10 09:43:59 -0500 | [diff] [blame] | 26 | #include <chrono> |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 27 | #include <functional> |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 28 | #include <memory> |
Tao Bao | d8db81a | 2019-06-04 11:20:00 -0700 | [diff] [blame] | 29 | #include <set> |
Tianjie Xu | 6af51a0 | 2017-04-19 14:00:52 -0700 | [diff] [blame] | 30 | #include <string> |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 31 | #include <string_view> |
Josh Gao | 2aa0d3a | 2017-04-13 12:45:55 -0700 | [diff] [blame] | 32 | #include <thread> |
| 33 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 34 | #include <android-base/file.h> |
| 35 | #include <android-base/logging.h> |
| 36 | #include <android-base/memory.h> |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 37 | #include <android-base/parseint.h> |
| 38 | #include <android-base/properties.h> |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 39 | #include <android-base/stringprintf.h> |
| 40 | #include <android-base/strings.h> |
| 41 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 42 | #include "adb.h" |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 43 | #include "adb_unique_fd.h" |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 44 | #include "adb_utils.h" |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 45 | #include "fuse_adb_provider.h" |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 46 | #include "fuse_sideload.h" |
Tao Bao | 3305d48 | 2019-09-26 00:02:29 -0700 | [diff] [blame] | 47 | #include "minadbd/types.h" |
Tao Bao | b548bea | 2019-09-30 16:16:07 -0700 | [diff] [blame] | 48 | #include "recovery_utils/battery_utils.h" |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 49 | #include "services.h" |
Elliott Hughes | 24eb8a0 | 2016-06-15 15:12:17 -0700 | [diff] [blame] | 50 | #include "sysdeps.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 51 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 52 | static int minadbd_socket = -1; |
Tao Bao | c6dc325 | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 53 | static bool rescue_mode = false; |
xunchang | 9c04eb4 | 2019-04-17 14:43:58 -0700 | [diff] [blame] | 54 | static std::string sideload_mount_point = FUSE_SIDELOAD_HOST_MOUNTPOINT; |
Tao Bao | c6dc325 | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 55 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 56 | void SetMinadbdSocketFd(int socket_fd) { |
| 57 | minadbd_socket = socket_fd; |
| 58 | } |
| 59 | |
Tao Bao | c6dc325 | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 60 | void SetMinadbdRescueMode(bool rescue) { |
| 61 | rescue_mode = rescue; |
| 62 | } |
| 63 | |
xunchang | 9c04eb4 | 2019-04-17 14:43:58 -0700 | [diff] [blame] | 64 | void SetSideloadMountPoint(const std::string& path) { |
| 65 | sideload_mount_point = path; |
| 66 | } |
| 67 | |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 68 | static bool WriteCommandToFd(MinadbdCommand cmd, int fd) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 69 | char message[kMinadbdMessageSize]; |
| 70 | memcpy(message, kMinadbdCommandPrefix, strlen(kMinadbdStatusPrefix)); |
| 71 | android::base::put_unaligned(message + strlen(kMinadbdStatusPrefix), cmd); |
| 72 | |
| 73 | if (!android::base::WriteFully(fd, message, kMinadbdMessageSize)) { |
| 74 | PLOG(ERROR) << "Failed to write message " << message; |
| 75 | return false; |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | // Blocks and reads the command status from |fd|. Returns false if the received message has a |
| 81 | // format error. |
| 82 | static bool WaitForCommandStatus(int fd, MinadbdCommandStatus* status) { |
| 83 | char buffer[kMinadbdMessageSize]; |
| 84 | if (!android::base::ReadFully(fd, buffer, kMinadbdMessageSize)) { |
| 85 | PLOG(ERROR) << "Failed to response status from socket"; |
| 86 | exit(kMinadbdSocketIOError); |
| 87 | } |
| 88 | |
| 89 | std::string message(buffer, buffer + kMinadbdMessageSize); |
| 90 | if (!android::base::StartsWith(message, kMinadbdStatusPrefix)) { |
| 91 | LOG(ERROR) << "Failed to parse status in " << message; |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | *status = android::base::get_unaligned<MinadbdCommandStatus>( |
| 96 | message.substr(strlen(kMinadbdStatusPrefix)).c_str()); |
| 97 | return true; |
| 98 | } |
| 99 | |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 100 | static MinadbdErrorCode RunAdbFuseSideload(int sfd, const std::string& args, |
| 101 | MinadbdCommandStatus* status) { |
| 102 | auto pieces = android::base::Split(args, ":"); |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 103 | int64_t file_size; |
| 104 | int block_size; |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 105 | if (pieces.size() != 2 || !android::base::ParseInt(pieces[0], &file_size) || file_size <= 0 || |
| 106 | !android::base::ParseInt(pieces[1], &block_size) || block_size <= 0) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 107 | LOG(ERROR) << "bad sideload-host arguments: " << args; |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 108 | return kMinadbdHostCommandArgumentError; |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 109 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 110 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 111 | LOG(INFO) << "sideload-host file size " << file_size << ", block size " << block_size; |
| 112 | |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 113 | if (!WriteCommandToFd(MinadbdCommand::kInstall, minadbd_socket)) { |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 114 | return kMinadbdSocketIOError; |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 115 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 116 | |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 117 | auto adb_data_reader = std::make_unique<FuseAdbDataProvider>(sfd, file_size, block_size); |
xunchang | 9c04eb4 | 2019-04-17 14:43:58 -0700 | [diff] [blame] | 118 | if (int result = run_fuse_sideload(std::move(adb_data_reader), sideload_mount_point.c_str()); |
| 119 | result != 0) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 120 | LOG(ERROR) << "Failed to start fuse"; |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 121 | return kMinadbdFuseStartError; |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 122 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 123 | |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 124 | if (!WaitForCommandStatus(minadbd_socket, status)) { |
| 125 | return kMinadbdMessageFormatError; |
| 126 | } |
| 127 | |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 128 | // Signal host-side adb to stop. For sideload mode, we always send kMinadbdServicesExitSuccess |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 129 | // (i.e. "DONEDONE") regardless of the install result. For rescue mode, we send failure message on |
| 130 | // install error. |
| 131 | if (!rescue_mode || *status == MinadbdCommandStatus::kSuccess) { |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 132 | if (!android::base::WriteFully(sfd, kMinadbdServicesExitSuccess, |
| 133 | strlen(kMinadbdServicesExitSuccess))) { |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 134 | return kMinadbdHostSocketIOError; |
| 135 | } |
| 136 | } else { |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 137 | if (!android::base::WriteFully(sfd, kMinadbdServicesExitFailure, |
| 138 | strlen(kMinadbdServicesExitFailure))) { |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 139 | return kMinadbdHostSocketIOError; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return kMinadbdSuccess; |
| 144 | } |
| 145 | |
Kelvin Zhang | e32c9f7 | 2020-12-10 09:43:59 -0500 | [diff] [blame] | 146 | static bool WaitForSocketClose(int fd, std::chrono::milliseconds timeout) { |
| 147 | const auto begin = std::chrono::steady_clock::now(); |
| 148 | const auto end = begin + timeout; |
| 149 | while (std::chrono::steady_clock::now() < end) { |
| 150 | // We don't care about reading the socket, we just want to wait until |
| 151 | // socket closes. In this case .events = 0 will tell the kernel to wait |
| 152 | // for close events. |
| 153 | struct pollfd pfd = { .fd = fd, .events = 0 }; |
| 154 | auto timeout_ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 155 | end - std::chrono::steady_clock::now()) |
| 156 | .count(); |
| 157 | int rc = TEMP_FAILURE_RETRY(adb_poll(&pfd, 1, timeout_ms)); |
| 158 | if (rc == 1) { |
| 159 | LOG(INFO) << "revents: " << pfd.revents; |
| 160 | if (pfd.revents & (POLLHUP | POLLRDHUP)) { |
| 161 | return true; |
| 162 | } |
| 163 | } else { |
| 164 | PLOG(ERROR) << "poll() failed"; |
| 165 | // poll failed, almost definitely due to timeout |
| 166 | // If not, you're screwed anyway, because it probably means the kernel ran |
| 167 | // out of memory. |
| 168 | return false; |
| 169 | } |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 174 | // Sideload service always exits after serving an install command. |
| 175 | static void SideloadHostService(unique_fd sfd, const std::string& args) { |
Kelvin Zhang | e32c9f7 | 2020-12-10 09:43:59 -0500 | [diff] [blame] | 176 | using namespace std::chrono_literals; |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 177 | MinadbdCommandStatus status; |
Kelvin Zhang | e32c9f7 | 2020-12-10 09:43:59 -0500 | [diff] [blame] | 178 | auto error = RunAdbFuseSideload(sfd.get(), args, &status); |
| 179 | // No need to wait if the socket is already closed, meaning the other end |
| 180 | // already exited for some reason. |
| 181 | if (error != kMinadbdHostSocketIOError) { |
| 182 | // We sleep for a little bit just to wait for the host to receive last |
| 183 | // "DONEDONE" message. However minadbd process is likely to get terminated |
| 184 | // early due to exit_on_close |
| 185 | WaitForSocketClose(sfd, 3000ms); |
| 186 | } |
| 187 | exit(error); |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 188 | } |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 189 | |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 190 | // Rescue service waits for the next command after an install command. |
| 191 | static void RescueInstallHostService(unique_fd sfd, const std::string& args) { |
| 192 | MinadbdCommandStatus status; |
| 193 | if (auto result = RunAdbFuseSideload(sfd.get(), args, &status); result != kMinadbdSuccess) { |
| 194 | exit(result); |
| 195 | } |
| 196 | } |
| 197 | |
Tao Bao | 57a2789 | 2019-06-10 12:41:44 -0700 | [diff] [blame] | 198 | // Answers the query on a given property |prop|, by writing the result to the given |sfd|. The |
| 199 | // result will be newline-terminated, so nonexistent or nonallowed query will be answered with "\n". |
| 200 | // If given an empty string, dumps all the supported properties (analogous to `adb shell getprop`) |
| 201 | // in lines, e.g. "[prop]: [value]". |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 202 | static void RescueGetpropHostService(unique_fd sfd, const std::string& prop) { |
Tao Bao | b548bea | 2019-09-30 16:16:07 -0700 | [diff] [blame] | 203 | constexpr const char* kRescueBatteryLevelProp = "rescue.battery_level"; |
Tao Bao | d8db81a | 2019-06-04 11:20:00 -0700 | [diff] [blame] | 204 | static const std::set<std::string> kGetpropAllowedProps = { |
Tao Bao | b548bea | 2019-09-30 16:16:07 -0700 | [diff] [blame] | 205 | // clang-format off |
| 206 | kRescueBatteryLevelProp, |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 207 | "ro.build.date.utc", |
Tao Bao | 3b9ef34 | 2019-06-03 12:10:54 -0700 | [diff] [blame] | 208 | "ro.build.fingerprint", |
| 209 | "ro.build.flavor", |
| 210 | "ro.build.id", |
| 211 | "ro.build.product", |
| 212 | "ro.build.tags", |
| 213 | "ro.build.version.incremental", |
| 214 | "ro.product.device", |
| 215 | "ro.product.vendor.device", |
Tao Bao | b548bea | 2019-09-30 16:16:07 -0700 | [diff] [blame] | 216 | // clang-format on |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 217 | }; |
Tao Bao | b548bea | 2019-09-30 16:16:07 -0700 | [diff] [blame] | 218 | |
| 219 | auto query_prop = [](const std::string& key) { |
| 220 | if (key == kRescueBatteryLevelProp) { |
| 221 | auto battery_info = GetBatteryInfo(); |
| 222 | return std::to_string(battery_info.capacity); |
| 223 | } |
| 224 | return android::base::GetProperty(key, ""); |
| 225 | }; |
| 226 | |
Tao Bao | d8db81a | 2019-06-04 11:20:00 -0700 | [diff] [blame] | 227 | std::string result; |
| 228 | if (prop.empty()) { |
| 229 | for (const auto& key : kGetpropAllowedProps) { |
Tao Bao | b548bea | 2019-09-30 16:16:07 -0700 | [diff] [blame] | 230 | auto value = query_prop(key); |
Tao Bao | d8db81a | 2019-06-04 11:20:00 -0700 | [diff] [blame] | 231 | if (value.empty()) { |
| 232 | continue; |
| 233 | } |
| 234 | result += "[" + key + "]: [" + value + "]\n"; |
| 235 | } |
Tao Bao | 57a2789 | 2019-06-10 12:41:44 -0700 | [diff] [blame] | 236 | } else if (kGetpropAllowedProps.find(prop) != kGetpropAllowedProps.end()) { |
Tao Bao | b548bea | 2019-09-30 16:16:07 -0700 | [diff] [blame] | 237 | result = query_prop(prop) + "\n"; |
Tao Bao | d8db81a | 2019-06-04 11:20:00 -0700 | [diff] [blame] | 238 | } |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 239 | if (result.empty()) { |
Tao Bao | 57a2789 | 2019-06-10 12:41:44 -0700 | [diff] [blame] | 240 | result = "\n"; |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 241 | } |
| 242 | if (!android::base::WriteFully(sfd, result.data(), result.size())) { |
| 243 | exit(kMinadbdHostSocketIOError); |
| 244 | } |
Tao Bao | 2223e6a | 2019-07-08 18:07:22 -0700 | [diff] [blame] | 245 | |
| 246 | // Send heartbeat signal to keep the rescue service alive. |
| 247 | if (!WriteCommandToFd(MinadbdCommand::kNoOp, minadbd_socket)) { |
| 248 | exit(kMinadbdSocketIOError); |
| 249 | } |
| 250 | if (MinadbdCommandStatus status; !WaitForCommandStatus(minadbd_socket, &status)) { |
| 251 | exit(kMinadbdMessageFormatError); |
| 252 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 255 | // Reboots into the given target. We don't reboot directly from minadbd, but going through recovery |
| 256 | // instead. This allows recovery to finish all the pending works (clear BCB, save logs etc) before |
| 257 | // the reboot. |
| 258 | static void RebootHostService(unique_fd /* sfd */, const std::string& target) { |
| 259 | MinadbdCommand command; |
| 260 | if (target == "bootloader") { |
| 261 | command = MinadbdCommand::kRebootBootloader; |
| 262 | } else if (target == "rescue") { |
| 263 | command = MinadbdCommand::kRebootRescue; |
| 264 | } else if (target == "recovery") { |
| 265 | command = MinadbdCommand::kRebootRecovery; |
| 266 | } else if (target == "fastboot") { |
| 267 | command = MinadbdCommand::kRebootFastboot; |
| 268 | } else { |
| 269 | command = MinadbdCommand::kRebootAndroid; |
| 270 | } |
| 271 | if (!WriteCommandToFd(command, minadbd_socket)) { |
| 272 | exit(kMinadbdSocketIOError); |
| 273 | } |
| 274 | MinadbdCommandStatus status; |
| 275 | if (!WaitForCommandStatus(minadbd_socket, &status)) { |
| 276 | exit(kMinadbdMessageFormatError); |
| 277 | } |
| 278 | } |
| 279 | |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 280 | static void WipeDeviceService(unique_fd fd, const std::string& args) { |
| 281 | auto pieces = android::base::Split(args, ":"); |
| 282 | if (pieces.size() != 2 || pieces[0] != "userdata") { |
| 283 | LOG(ERROR) << "Failed to parse wipe device command arguments " << args; |
| 284 | exit(kMinadbdHostCommandArgumentError); |
| 285 | } |
| 286 | |
| 287 | size_t message_size; |
| 288 | if (!android::base::ParseUint(pieces[1], &message_size) || |
| 289 | message_size < strlen(kMinadbdServicesExitSuccess)) { |
| 290 | LOG(ERROR) << "Failed to parse wipe device message size in " << args; |
| 291 | exit(kMinadbdHostCommandArgumentError); |
| 292 | } |
| 293 | |
| 294 | WriteCommandToFd(MinadbdCommand::kWipeData, minadbd_socket); |
| 295 | MinadbdCommandStatus status; |
| 296 | if (!WaitForCommandStatus(minadbd_socket, &status)) { |
| 297 | exit(kMinadbdMessageFormatError); |
| 298 | } |
| 299 | |
| 300 | std::string response = (status == MinadbdCommandStatus::kSuccess) ? kMinadbdServicesExitSuccess |
| 301 | : kMinadbdServicesExitFailure; |
| 302 | response += std::string(message_size - response.size(), '\0'); |
| 303 | if (!android::base::WriteFully(fd, response.c_str(), response.size())) { |
| 304 | exit(kMinadbdHostSocketIOError); |
| 305 | } |
| 306 | } |
| 307 | |
Josh Gao | 5ee3eba | 2020-03-27 20:37:07 -0700 | [diff] [blame] | 308 | asocket* daemon_service_to_socket(std::string_view) { |
| 309 | return nullptr; |
| 310 | } |
| 311 | |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 312 | unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) { |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 313 | // Common services that are supported both in sideload and rescue modes. |
Elliott Hughes | 93838f6 | 2019-05-03 10:33:04 -0700 | [diff] [blame] | 314 | if (android::base::ConsumePrefix(&name, "reboot:")) { |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 315 | // "reboot:<target>", where target must be one of the following. |
| 316 | std::string args(name); |
| 317 | if (args.empty() || args == "bootloader" || args == "rescue" || args == "recovery" || |
| 318 | args == "fastboot") { |
| 319 | return create_service_thread("reboot", |
| 320 | std::bind(RebootHostService, std::placeholders::_1, args)); |
| 321 | } |
| 322 | return unique_fd{}; |
| 323 | } |
| 324 | |
| 325 | // Rescue-specific services. |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 326 | if (rescue_mode) { |
Elliott Hughes | 93838f6 | 2019-05-03 10:33:04 -0700 | [diff] [blame] | 327 | if (android::base::ConsumePrefix(&name, "rescue-install:")) { |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 328 | // rescue-install:<file-size>:<block-size> |
| 329 | std::string args(name); |
| 330 | return create_service_thread( |
| 331 | "rescue-install", std::bind(RescueInstallHostService, std::placeholders::_1, args)); |
Elliott Hughes | 93838f6 | 2019-05-03 10:33:04 -0700 | [diff] [blame] | 332 | } else if (android::base::ConsumePrefix(&name, "rescue-getprop:")) { |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 333 | // rescue-getprop:<prop> |
| 334 | std::string args(name); |
| 335 | return create_service_thread( |
| 336 | "rescue-getprop", std::bind(RescueGetpropHostService, std::placeholders::_1, args)); |
Elliott Hughes | 93838f6 | 2019-05-03 10:33:04 -0700 | [diff] [blame] | 337 | } else if (android::base::ConsumePrefix(&name, "rescue-wipe:")) { |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 338 | // rescue-wipe:target:<message-size> |
| 339 | std::string args(name); |
| 340 | return create_service_thread("rescue-wipe", |
| 341 | std::bind(WipeDeviceService, std::placeholders::_1, args)); |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 342 | } |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 343 | |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 344 | return unique_fd{}; |
| 345 | } |
| 346 | |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 347 | // Sideload-specific services. |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 348 | if (name.starts_with("sideload:")) { |
| 349 | // This exit status causes recovery to print a special error message saying to use a newer adb |
| 350 | // (that supports sideload-host). |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 351 | exit(kMinadbdAdbVersionError); |
Elliott Hughes | 93838f6 | 2019-05-03 10:33:04 -0700 | [diff] [blame] | 352 | } else if (android::base::ConsumePrefix(&name, "sideload-host:")) { |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 353 | // sideload-host:<file-size>:<block-size> |
| 354 | std::string args(name); |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 355 | return create_service_thread("sideload-host", |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 356 | std::bind(SideloadHostService, std::placeholders::_1, args)); |
Tao Bao | 99f0d9e | 2016-10-13 12:46:38 -0700 | [diff] [blame] | 357 | } |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 358 | return unique_fd{}; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 359 | } |