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