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 | 95d6732 | 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 | |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 26 | #include <functional> |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 27 | #include <memory> |
Tao Bao | 5359d77 | 2019-06-04 11:20:00 -0700 | [diff] [blame] | 28 | #include <set> |
Tianjie Xu | 6af51a0 | 2017-04-19 14:00:52 -0700 | [diff] [blame] | 29 | #include <string> |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 30 | #include <string_view> |
Josh Gao | 2aa0d3a | 2017-04-13 12:45:55 -0700 | [diff] [blame] | 31 | #include <thread> |
| 32 | |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 33 | #include <android-base/file.h> |
| 34 | #include <android-base/logging.h> |
| 35 | #include <android-base/memory.h> |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 36 | #include <android-base/parseint.h> |
| 37 | #include <android-base/properties.h> |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 38 | #include <android-base/stringprintf.h> |
| 39 | #include <android-base/strings.h> |
| 40 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 41 | #include "adb.h" |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 42 | #include "adb_unique_fd.h" |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 43 | #include "adb_utils.h" |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 44 | #include "fdevent.h" |
| 45 | #include "fuse_adb_provider.h" |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 46 | #include "fuse_sideload.h" |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 47 | #include "minadbd_types.h" |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 48 | #include "services.h" |
Elliott Hughes | 24eb8a0 | 2016-06-15 15:12:17 -0700 | [diff] [blame] | 49 | #include "sysdeps.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 50 | |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 51 | static int minadbd_socket = -1; |
Tao Bao | 378bfbf | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 52 | static bool rescue_mode = false; |
xunchang | 23f15fc | 2019-04-17 14:43:58 -0700 | [diff] [blame] | 53 | static std::string sideload_mount_point = FUSE_SIDELOAD_HOST_MOUNTPOINT; |
Tao Bao | 378bfbf | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 54 | |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 55 | void SetMinadbdSocketFd(int socket_fd) { |
| 56 | minadbd_socket = socket_fd; |
| 57 | } |
| 58 | |
Tao Bao | 378bfbf | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 59 | void SetMinadbdRescueMode(bool rescue) { |
| 60 | rescue_mode = rescue; |
| 61 | } |
| 62 | |
xunchang | 23f15fc | 2019-04-17 14:43:58 -0700 | [diff] [blame] | 63 | void SetSideloadMountPoint(const std::string& path) { |
| 64 | sideload_mount_point = path; |
| 65 | } |
| 66 | |
Tao Bao | 7b9b7db | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 67 | static bool WriteCommandToFd(MinadbdCommand cmd, int fd) { |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 68 | char message[kMinadbdMessageSize]; |
| 69 | memcpy(message, kMinadbdCommandPrefix, strlen(kMinadbdStatusPrefix)); |
| 70 | android::base::put_unaligned(message + strlen(kMinadbdStatusPrefix), cmd); |
| 71 | |
| 72 | if (!android::base::WriteFully(fd, message, kMinadbdMessageSize)) { |
| 73 | PLOG(ERROR) << "Failed to write message " << message; |
| 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | // Blocks and reads the command status from |fd|. Returns false if the received message has a |
| 80 | // format error. |
| 81 | static bool WaitForCommandStatus(int fd, MinadbdCommandStatus* status) { |
| 82 | char buffer[kMinadbdMessageSize]; |
| 83 | if (!android::base::ReadFully(fd, buffer, kMinadbdMessageSize)) { |
| 84 | PLOG(ERROR) << "Failed to response status from socket"; |
| 85 | exit(kMinadbdSocketIOError); |
| 86 | } |
| 87 | |
| 88 | std::string message(buffer, buffer + kMinadbdMessageSize); |
| 89 | if (!android::base::StartsWith(message, kMinadbdStatusPrefix)) { |
| 90 | LOG(ERROR) << "Failed to parse status in " << message; |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | *status = android::base::get_unaligned<MinadbdCommandStatus>( |
| 95 | message.substr(strlen(kMinadbdStatusPrefix)).c_str()); |
| 96 | return true; |
| 97 | } |
| 98 | |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 99 | static MinadbdErrorCode RunAdbFuseSideload(int sfd, const std::string& args, |
| 100 | MinadbdCommandStatus* status) { |
| 101 | auto pieces = android::base::Split(args, ":"); |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 102 | int64_t file_size; |
| 103 | int block_size; |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 104 | if (pieces.size() != 2 || !android::base::ParseInt(pieces[0], &file_size) || file_size <= 0 || |
| 105 | !android::base::ParseInt(pieces[1], &block_size) || block_size <= 0) { |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 106 | LOG(ERROR) << "bad sideload-host arguments: " << args; |
xunchang | 5a1916b | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 107 | return kMinadbdHostCommandArgumentError; |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 108 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 109 | |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 110 | LOG(INFO) << "sideload-host file size " << file_size << ", block size " << block_size; |
| 111 | |
Tao Bao | 7b9b7db | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 112 | if (!WriteCommandToFd(MinadbdCommand::kInstall, minadbd_socket)) { |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 113 | return kMinadbdSocketIOError; |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 114 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 115 | |
Tao Bao | 178cdd4 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 116 | auto adb_data_reader = std::make_unique<FuseAdbDataProvider>(sfd, file_size, block_size); |
xunchang | 23f15fc | 2019-04-17 14:43:58 -0700 | [diff] [blame] | 117 | if (int result = run_fuse_sideload(std::move(adb_data_reader), sideload_mount_point.c_str()); |
| 118 | result != 0) { |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 119 | LOG(ERROR) << "Failed to start fuse"; |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 120 | return kMinadbdFuseStartError; |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 121 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 122 | |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 123 | if (!WaitForCommandStatus(minadbd_socket, status)) { |
| 124 | return kMinadbdMessageFormatError; |
| 125 | } |
| 126 | |
xunchang | 5a1916b | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 127 | // Signal host-side adb to stop. For sideload mode, we always send kMinadbdServicesExitSuccess |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 128 | // (i.e. "DONEDONE") regardless of the install result. For rescue mode, we send failure message on |
| 129 | // install error. |
| 130 | if (!rescue_mode || *status == MinadbdCommandStatus::kSuccess) { |
xunchang | 5a1916b | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 131 | if (!android::base::WriteFully(sfd, kMinadbdServicesExitSuccess, |
| 132 | strlen(kMinadbdServicesExitSuccess))) { |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 133 | return kMinadbdHostSocketIOError; |
| 134 | } |
| 135 | } else { |
xunchang | 5a1916b | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 136 | if (!android::base::WriteFully(sfd, kMinadbdServicesExitFailure, |
| 137 | strlen(kMinadbdServicesExitFailure))) { |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 138 | return kMinadbdHostSocketIOError; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return kMinadbdSuccess; |
| 143 | } |
| 144 | |
| 145 | // Sideload service always exits after serving an install command. |
| 146 | static void SideloadHostService(unique_fd sfd, const std::string& args) { |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 147 | MinadbdCommandStatus status; |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 148 | exit(RunAdbFuseSideload(sfd.get(), args, &status)); |
| 149 | } |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 150 | |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 151 | // Rescue service waits for the next command after an install command. |
| 152 | static void RescueInstallHostService(unique_fd sfd, const std::string& args) { |
| 153 | MinadbdCommandStatus status; |
| 154 | if (auto result = RunAdbFuseSideload(sfd.get(), args, &status); result != kMinadbdSuccess) { |
| 155 | exit(result); |
| 156 | } |
| 157 | } |
| 158 | |
Tao Bao | e3cc180 | 2019-06-10 12:41:44 -0700 | [diff] [blame] | 159 | // Answers the query on a given property |prop|, by writing the result to the given |sfd|. The |
| 160 | // result will be newline-terminated, so nonexistent or nonallowed query will be answered with "\n". |
| 161 | // If given an empty string, dumps all the supported properties (analogous to `adb shell getprop`) |
| 162 | // in lines, e.g. "[prop]: [value]". |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 163 | static void RescueGetpropHostService(unique_fd sfd, const std::string& prop) { |
Tao Bao | 5359d77 | 2019-06-04 11:20:00 -0700 | [diff] [blame] | 164 | static const std::set<std::string> kGetpropAllowedProps = { |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 165 | "ro.build.date.utc", |
Tao Bao | 7a31ab6 | 2019-06-03 12:10:54 -0700 | [diff] [blame] | 166 | "ro.build.fingerprint", |
| 167 | "ro.build.flavor", |
| 168 | "ro.build.id", |
| 169 | "ro.build.product", |
| 170 | "ro.build.tags", |
| 171 | "ro.build.version.incremental", |
| 172 | "ro.product.device", |
| 173 | "ro.product.vendor.device", |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 174 | }; |
Tao Bao | 5359d77 | 2019-06-04 11:20:00 -0700 | [diff] [blame] | 175 | std::string result; |
| 176 | if (prop.empty()) { |
| 177 | for (const auto& key : kGetpropAllowedProps) { |
| 178 | auto value = android::base::GetProperty(key, ""); |
| 179 | if (value.empty()) { |
| 180 | continue; |
| 181 | } |
| 182 | result += "[" + key + "]: [" + value + "]\n"; |
| 183 | } |
Tao Bao | e3cc180 | 2019-06-10 12:41:44 -0700 | [diff] [blame] | 184 | } else if (kGetpropAllowedProps.find(prop) != kGetpropAllowedProps.end()) { |
| 185 | result = android::base::GetProperty(prop, "") + "\n"; |
Tao Bao | 5359d77 | 2019-06-04 11:20:00 -0700 | [diff] [blame] | 186 | } |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 187 | if (result.empty()) { |
Tao Bao | e3cc180 | 2019-06-10 12:41:44 -0700 | [diff] [blame] | 188 | result = "\n"; |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 189 | } |
| 190 | if (!android::base::WriteFully(sfd, result.data(), result.size())) { |
| 191 | exit(kMinadbdHostSocketIOError); |
| 192 | } |
Tao Bao | 0bbb2ed | 2019-07-08 18:07:22 -0700 | [diff] [blame] | 193 | |
| 194 | // Send heartbeat signal to keep the rescue service alive. |
| 195 | if (!WriteCommandToFd(MinadbdCommand::kNoOp, minadbd_socket)) { |
| 196 | exit(kMinadbdSocketIOError); |
| 197 | } |
| 198 | if (MinadbdCommandStatus status; !WaitForCommandStatus(minadbd_socket, &status)) { |
| 199 | exit(kMinadbdMessageFormatError); |
| 200 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Tao Bao | 7b9b7db | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 203 | // Reboots into the given target. We don't reboot directly from minadbd, but going through recovery |
| 204 | // instead. This allows recovery to finish all the pending works (clear BCB, save logs etc) before |
| 205 | // the reboot. |
| 206 | static void RebootHostService(unique_fd /* sfd */, const std::string& target) { |
| 207 | MinadbdCommand command; |
| 208 | if (target == "bootloader") { |
| 209 | command = MinadbdCommand::kRebootBootloader; |
| 210 | } else if (target == "rescue") { |
| 211 | command = MinadbdCommand::kRebootRescue; |
| 212 | } else if (target == "recovery") { |
| 213 | command = MinadbdCommand::kRebootRecovery; |
| 214 | } else if (target == "fastboot") { |
| 215 | command = MinadbdCommand::kRebootFastboot; |
| 216 | } else { |
| 217 | command = MinadbdCommand::kRebootAndroid; |
| 218 | } |
| 219 | if (!WriteCommandToFd(command, minadbd_socket)) { |
| 220 | exit(kMinadbdSocketIOError); |
| 221 | } |
| 222 | MinadbdCommandStatus status; |
| 223 | if (!WaitForCommandStatus(minadbd_socket, &status)) { |
| 224 | exit(kMinadbdMessageFormatError); |
| 225 | } |
| 226 | } |
| 227 | |
xunchang | 5a1916b | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 228 | static void WipeDeviceService(unique_fd fd, const std::string& args) { |
| 229 | auto pieces = android::base::Split(args, ":"); |
| 230 | if (pieces.size() != 2 || pieces[0] != "userdata") { |
| 231 | LOG(ERROR) << "Failed to parse wipe device command arguments " << args; |
| 232 | exit(kMinadbdHostCommandArgumentError); |
| 233 | } |
| 234 | |
| 235 | size_t message_size; |
| 236 | if (!android::base::ParseUint(pieces[1], &message_size) || |
| 237 | message_size < strlen(kMinadbdServicesExitSuccess)) { |
| 238 | LOG(ERROR) << "Failed to parse wipe device message size in " << args; |
| 239 | exit(kMinadbdHostCommandArgumentError); |
| 240 | } |
| 241 | |
| 242 | WriteCommandToFd(MinadbdCommand::kWipeData, minadbd_socket); |
| 243 | MinadbdCommandStatus status; |
| 244 | if (!WaitForCommandStatus(minadbd_socket, &status)) { |
| 245 | exit(kMinadbdMessageFormatError); |
| 246 | } |
| 247 | |
| 248 | std::string response = (status == MinadbdCommandStatus::kSuccess) ? kMinadbdServicesExitSuccess |
| 249 | : kMinadbdServicesExitFailure; |
| 250 | response += std::string(message_size - response.size(), '\0'); |
| 251 | if (!android::base::WriteFully(fd, response.c_str(), response.size())) { |
| 252 | exit(kMinadbdHostSocketIOError); |
| 253 | } |
| 254 | } |
| 255 | |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 256 | unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) { |
Tao Bao | 7b9b7db | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 257 | // Common services that are supported both in sideload and rescue modes. |
| 258 | if (ConsumePrefix(&name, "reboot:")) { |
| 259 | // "reboot:<target>", where target must be one of the following. |
| 260 | std::string args(name); |
| 261 | if (args.empty() || args == "bootloader" || args == "rescue" || args == "recovery" || |
| 262 | args == "fastboot") { |
| 263 | return create_service_thread("reboot", |
| 264 | std::bind(RebootHostService, std::placeholders::_1, args)); |
| 265 | } |
| 266 | return unique_fd{}; |
| 267 | } |
| 268 | |
| 269 | // Rescue-specific services. |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 270 | if (rescue_mode) { |
| 271 | if (ConsumePrefix(&name, "rescue-install:")) { |
| 272 | // rescue-install:<file-size>:<block-size> |
| 273 | std::string args(name); |
| 274 | return create_service_thread( |
| 275 | "rescue-install", std::bind(RescueInstallHostService, std::placeholders::_1, args)); |
| 276 | } else if (ConsumePrefix(&name, "rescue-getprop:")) { |
| 277 | // rescue-getprop:<prop> |
| 278 | std::string args(name); |
| 279 | return create_service_thread( |
| 280 | "rescue-getprop", std::bind(RescueGetpropHostService, std::placeholders::_1, args)); |
xunchang | 5a1916b | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 281 | } else if (ConsumePrefix(&name, "rescue-wipe:")) { |
| 282 | // rescue-wipe:target:<message-size> |
| 283 | std::string args(name); |
| 284 | return create_service_thread("rescue-wipe", |
| 285 | std::bind(WipeDeviceService, std::placeholders::_1, args)); |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 286 | } |
xunchang | 5a1916b | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 287 | |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 288 | return unique_fd{}; |
| 289 | } |
| 290 | |
Tao Bao | 7b9b7db | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 291 | // Sideload-specific services. |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 292 | if (name.starts_with("sideload:")) { |
| 293 | // This exit status causes recovery to print a special error message saying to use a newer adb |
| 294 | // (that supports sideload-host). |
xunchang | 95d6732 | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 295 | exit(kMinadbdAdbVersionError); |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 296 | } else if (ConsumePrefix(&name, "sideload-host:")) { |
| 297 | // sideload-host:<file-size>:<block-size> |
| 298 | std::string args(name); |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 299 | return create_service_thread("sideload-host", |
Tao Bao | e5c6446 | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 300 | std::bind(SideloadHostService, std::placeholders::_1, args)); |
Tao Bao | 99f0d9e | 2016-10-13 12:46:38 -0700 | [diff] [blame] | 301 | } |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 302 | return unique_fd{}; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 303 | } |