blob: 0abe8675bd2ad640d0a16696802545e2afec2045 [file] [log] [blame]
Doug Zongker9270a202012-01-09 15:16:13 -08001/*
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
xunchang34690ce2019-04-05 16:16:07 -070017#include "minadbd_services.h"
18
Doug Zongker9270a202012-01-09 15:16:13 -080019#include <errno.h>
Dan Albert8f6eb5c2015-02-24 22:07:18 -080020#include <inttypes.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
Doug Zongker9270a202012-01-09 15:16:13 -080025
Kelvin Zhange32c9f72020-12-10 09:43:59 -050026#include <chrono>
Josh Gao038c4a12018-07-27 11:18:30 -070027#include <functional>
xunchang5e6832a2019-03-15 16:04:32 -070028#include <memory>
Tao Baod8db81a2019-06-04 11:20:00 -070029#include <set>
Tianjie Xu6af51a02017-04-19 14:00:52 -070030#include <string>
Tao Bao5de19e22019-01-02 09:35:59 -080031#include <string_view>
Josh Gao2aa0d3a2017-04-13 12:45:55 -070032#include <thread>
33
xunchang34690ce2019-04-05 16:16:07 -070034#include <android-base/file.h>
35#include <android-base/logging.h>
36#include <android-base/memory.h>
Tao Baoed717ca2019-04-04 18:37:58 -070037#include <android-base/parseint.h>
38#include <android-base/properties.h>
xunchang34690ce2019-04-05 16:16:07 -070039#include <android-base/stringprintf.h>
40#include <android-base/strings.h>
41
Doug Zongker9270a202012-01-09 15:16:13 -080042#include "adb.h"
Josh Gao038c4a12018-07-27 11:18:30 -070043#include "adb_unique_fd.h"
Tao Baoed717ca2019-04-04 18:37:58 -070044#include "adb_utils.h"
Dan Albert8f6eb5c2015-02-24 22:07:18 -080045#include "fuse_adb_provider.h"
xunchang5e6832a2019-03-15 16:04:32 -070046#include "fuse_sideload.h"
Tao Bao3305d482019-09-26 00:02:29 -070047#include "minadbd/types.h"
Tao Baob548bea2019-09-30 16:16:07 -070048#include "recovery_utils/battery_utils.h"
Josh Gao038c4a12018-07-27 11:18:30 -070049#include "services.h"
Elliott Hughes24eb8a02016-06-15 15:12:17 -070050#include "sysdeps.h"
Doug Zongker9270a202012-01-09 15:16:13 -080051
xunchang34690ce2019-04-05 16:16:07 -070052static int minadbd_socket = -1;
Tao Baoc6dc3252019-04-16 14:22:25 -070053static bool rescue_mode = false;
xunchang9c04eb42019-04-17 14:43:58 -070054static std::string sideload_mount_point = FUSE_SIDELOAD_HOST_MOUNTPOINT;
Tao Baoc6dc3252019-04-16 14:22:25 -070055
xunchang34690ce2019-04-05 16:16:07 -070056void SetMinadbdSocketFd(int socket_fd) {
57 minadbd_socket = socket_fd;
58}
59
Tao Baoc6dc3252019-04-16 14:22:25 -070060void SetMinadbdRescueMode(bool rescue) {
61 rescue_mode = rescue;
62}
63
xunchang9c04eb42019-04-17 14:43:58 -070064void SetSideloadMountPoint(const std::string& path) {
65 sideload_mount_point = path;
66}
67
Tao Bao10f441a2019-04-19 15:22:15 -070068static bool WriteCommandToFd(MinadbdCommand cmd, int fd) {
xunchang34690ce2019-04-05 16:16:07 -070069 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.
82static 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 Baoed717ca2019-04-04 18:37:58 -0700100static MinadbdErrorCode RunAdbFuseSideload(int sfd, const std::string& args,
101 MinadbdCommandStatus* status) {
102 auto pieces = android::base::Split(args, ":");
katao77d61732018-09-20 20:34:05 +0800103 int64_t file_size;
104 int block_size;
Tao Baoed717ca2019-04-04 18:37:58 -0700105 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) {
xunchang34690ce2019-04-05 16:16:07 -0700107 LOG(ERROR) << "bad sideload-host arguments: " << args;
xunchangfedeef62019-04-22 12:18:14 -0700108 return kMinadbdHostCommandArgumentError;
katao77d61732018-09-20 20:34:05 +0800109 }
Doug Zongker9270a202012-01-09 15:16:13 -0800110
xunchang34690ce2019-04-05 16:16:07 -0700111 LOG(INFO) << "sideload-host file size " << file_size << ", block size " << block_size;
112
Tao Bao10f441a2019-04-19 15:22:15 -0700113 if (!WriteCommandToFd(MinadbdCommand::kInstall, minadbd_socket)) {
Tao Baoed717ca2019-04-04 18:37:58 -0700114 return kMinadbdSocketIOError;
xunchang34690ce2019-04-05 16:16:07 -0700115 }
Doug Zongker9270a202012-01-09 15:16:13 -0800116
Tao Bao2be97372019-04-15 12:45:50 -0700117 auto adb_data_reader = std::make_unique<FuseAdbDataProvider>(sfd, file_size, block_size);
xunchang9c04eb42019-04-17 14:43:58 -0700118 if (int result = run_fuse_sideload(std::move(adb_data_reader), sideload_mount_point.c_str());
119 result != 0) {
xunchang34690ce2019-04-05 16:16:07 -0700120 LOG(ERROR) << "Failed to start fuse";
Tao Baoed717ca2019-04-04 18:37:58 -0700121 return kMinadbdFuseStartError;
xunchang34690ce2019-04-05 16:16:07 -0700122 }
Doug Zongker9270a202012-01-09 15:16:13 -0800123
Tao Baoed717ca2019-04-04 18:37:58 -0700124 if (!WaitForCommandStatus(minadbd_socket, status)) {
125 return kMinadbdMessageFormatError;
126 }
127
xunchangfedeef62019-04-22 12:18:14 -0700128 // Signal host-side adb to stop. For sideload mode, we always send kMinadbdServicesExitSuccess
Tao Baoed717ca2019-04-04 18:37:58 -0700129 // (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) {
xunchangfedeef62019-04-22 12:18:14 -0700132 if (!android::base::WriteFully(sfd, kMinadbdServicesExitSuccess,
133 strlen(kMinadbdServicesExitSuccess))) {
Tao Baoed717ca2019-04-04 18:37:58 -0700134 return kMinadbdHostSocketIOError;
135 }
136 } else {
xunchangfedeef62019-04-22 12:18:14 -0700137 if (!android::base::WriteFully(sfd, kMinadbdServicesExitFailure,
138 strlen(kMinadbdServicesExitFailure))) {
Tao Baoed717ca2019-04-04 18:37:58 -0700139 return kMinadbdHostSocketIOError;
140 }
141 }
142
143 return kMinadbdSuccess;
144}
145
Kelvin Zhange32c9f72020-12-10 09:43:59 -0500146static 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 Baoed717ca2019-04-04 18:37:58 -0700174// Sideload service always exits after serving an install command.
175static void SideloadHostService(unique_fd sfd, const std::string& args) {
Kelvin Zhange32c9f72020-12-10 09:43:59 -0500176 using namespace std::chrono_literals;
xunchang34690ce2019-04-05 16:16:07 -0700177 MinadbdCommandStatus status;
Kelvin Zhange32c9f72020-12-10 09:43:59 -0500178 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 Baoed717ca2019-04-04 18:37:58 -0700188}
xunchang34690ce2019-04-05 16:16:07 -0700189
Tao Baoed717ca2019-04-04 18:37:58 -0700190// Rescue service waits for the next command after an install command.
191static 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 Bao57a27892019-06-10 12:41:44 -0700198// 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 Baoed717ca2019-04-04 18:37:58 -0700202static void RescueGetpropHostService(unique_fd sfd, const std::string& prop) {
Tao Baob548bea2019-09-30 16:16:07 -0700203 constexpr const char* kRescueBatteryLevelProp = "rescue.battery_level";
Tao Baod8db81a2019-06-04 11:20:00 -0700204 static const std::set<std::string> kGetpropAllowedProps = {
Tao Baob548bea2019-09-30 16:16:07 -0700205 // clang-format off
206 kRescueBatteryLevelProp,
Tao Baoed717ca2019-04-04 18:37:58 -0700207 "ro.build.date.utc",
Tao Bao3b9ef342019-06-03 12:10:54 -0700208 "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 Baob548bea2019-09-30 16:16:07 -0700216 // clang-format on
Tao Baoed717ca2019-04-04 18:37:58 -0700217 };
Tao Baob548bea2019-09-30 16:16:07 -0700218
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 Baod8db81a2019-06-04 11:20:00 -0700227 std::string result;
228 if (prop.empty()) {
229 for (const auto& key : kGetpropAllowedProps) {
Tao Baob548bea2019-09-30 16:16:07 -0700230 auto value = query_prop(key);
Tao Baod8db81a2019-06-04 11:20:00 -0700231 if (value.empty()) {
232 continue;
233 }
234 result += "[" + key + "]: [" + value + "]\n";
235 }
Tao Bao57a27892019-06-10 12:41:44 -0700236 } else if (kGetpropAllowedProps.find(prop) != kGetpropAllowedProps.end()) {
Tao Baob548bea2019-09-30 16:16:07 -0700237 result = query_prop(prop) + "\n";
Tao Baod8db81a2019-06-04 11:20:00 -0700238 }
Tao Baoed717ca2019-04-04 18:37:58 -0700239 if (result.empty()) {
Tao Bao57a27892019-06-10 12:41:44 -0700240 result = "\n";
Tao Baoed717ca2019-04-04 18:37:58 -0700241 }
242 if (!android::base::WriteFully(sfd, result.data(), result.size())) {
243 exit(kMinadbdHostSocketIOError);
244 }
Tao Bao2223e6a2019-07-08 18:07:22 -0700245
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 Zongker9270a202012-01-09 15:16:13 -0800253}
254
Tao Bao10f441a2019-04-19 15:22:15 -0700255// 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.
258static 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
xunchangfedeef62019-04-22 12:18:14 -0700280static 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 Gao5ee3eba2020-03-27 20:37:07 -0700308asocket* daemon_service_to_socket(std::string_view) {
309 return nullptr;
310}
311
Tao Bao5de19e22019-01-02 09:35:59 -0800312unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) {
Tao Bao10f441a2019-04-19 15:22:15 -0700313 // Common services that are supported both in sideload and rescue modes.
Elliott Hughes93838f62019-05-03 10:33:04 -0700314 if (android::base::ConsumePrefix(&name, "reboot:")) {
Tao Bao10f441a2019-04-19 15:22:15 -0700315 // "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 Baoed717ca2019-04-04 18:37:58 -0700326 if (rescue_mode) {
Elliott Hughes93838f62019-05-03 10:33:04 -0700327 if (android::base::ConsumePrefix(&name, "rescue-install:")) {
Tao Baoed717ca2019-04-04 18:37:58 -0700328 // 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 Hughes93838f62019-05-03 10:33:04 -0700332 } else if (android::base::ConsumePrefix(&name, "rescue-getprop:")) {
Tao Baoed717ca2019-04-04 18:37:58 -0700333 // rescue-getprop:<prop>
334 std::string args(name);
335 return create_service_thread(
336 "rescue-getprop", std::bind(RescueGetpropHostService, std::placeholders::_1, args));
Elliott Hughes93838f62019-05-03 10:33:04 -0700337 } else if (android::base::ConsumePrefix(&name, "rescue-wipe:")) {
xunchangfedeef62019-04-22 12:18:14 -0700338 // 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 Baoed717ca2019-04-04 18:37:58 -0700342 }
xunchangfedeef62019-04-22 12:18:14 -0700343
Tao Baoed717ca2019-04-04 18:37:58 -0700344 return unique_fd{};
345 }
346
Tao Bao10f441a2019-04-19 15:22:15 -0700347 // Sideload-specific services.
Tao Bao5de19e22019-01-02 09:35:59 -0800348 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).
xunchang34690ce2019-04-05 16:16:07 -0700351 exit(kMinadbdAdbVersionError);
Elliott Hughes93838f62019-05-03 10:33:04 -0700352 } else if (android::base::ConsumePrefix(&name, "sideload-host:")) {
Tao Baoed717ca2019-04-04 18:37:58 -0700353 // sideload-host:<file-size>:<block-size>
354 std::string args(name);
Josh Gao038c4a12018-07-27 11:18:30 -0700355 return create_service_thread("sideload-host",
Tao Baoed717ca2019-04-04 18:37:58 -0700356 std::bind(SideloadHostService, std::placeholders::_1, args));
Tao Bao99f0d9e2016-10-13 12:46:38 -0700357 }
Josh Gao038c4a12018-07-27 11:18:30 -0700358 return unique_fd{};
Doug Zongker9270a202012-01-09 15:16:13 -0800359}