blob: d2b824cfea920e9326922f657906b33172ab77ea [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
Josh Gao038c4a12018-07-27 11:18:30 -070026#include <functional>
xunchang5e6832a2019-03-15 16:04:32 -070027#include <memory>
Tao Baod8db81a2019-06-04 11:20:00 -070028#include <set>
Tianjie Xu6af51a02017-04-19 14:00:52 -070029#include <string>
Tao Bao5de19e22019-01-02 09:35:59 -080030#include <string_view>
Josh Gao2aa0d3a2017-04-13 12:45:55 -070031#include <thread>
32
xunchang34690ce2019-04-05 16:16:07 -070033#include <android-base/file.h>
34#include <android-base/logging.h>
35#include <android-base/memory.h>
Tao Baoed717ca2019-04-04 18:37:58 -070036#include <android-base/parseint.h>
37#include <android-base/properties.h>
xunchang34690ce2019-04-05 16:16:07 -070038#include <android-base/stringprintf.h>
39#include <android-base/strings.h>
40
Doug Zongker9270a202012-01-09 15:16:13 -080041#include "adb.h"
Josh Gao038c4a12018-07-27 11:18:30 -070042#include "adb_unique_fd.h"
Tao Baoed717ca2019-04-04 18:37:58 -070043#include "adb_utils.h"
Dan Albert8f6eb5c2015-02-24 22:07:18 -080044#include "fdevent.h"
45#include "fuse_adb_provider.h"
xunchang5e6832a2019-03-15 16:04:32 -070046#include "fuse_sideload.h"
xunchang34690ce2019-04-05 16:16:07 -070047#include "minadbd_types.h"
Josh Gao038c4a12018-07-27 11:18:30 -070048#include "services.h"
Elliott Hughes24eb8a02016-06-15 15:12:17 -070049#include "sysdeps.h"
Doug Zongker9270a202012-01-09 15:16:13 -080050
xunchang34690ce2019-04-05 16:16:07 -070051static int minadbd_socket = -1;
Tao Baoc6dc3252019-04-16 14:22:25 -070052static bool rescue_mode = false;
xunchang9c04eb42019-04-17 14:43:58 -070053static std::string sideload_mount_point = FUSE_SIDELOAD_HOST_MOUNTPOINT;
Tao Baoc6dc3252019-04-16 14:22:25 -070054
xunchang34690ce2019-04-05 16:16:07 -070055void SetMinadbdSocketFd(int socket_fd) {
56 minadbd_socket = socket_fd;
57}
58
Tao Baoc6dc3252019-04-16 14:22:25 -070059void SetMinadbdRescueMode(bool rescue) {
60 rescue_mode = rescue;
61}
62
xunchang9c04eb42019-04-17 14:43:58 -070063void SetSideloadMountPoint(const std::string& path) {
64 sideload_mount_point = path;
65}
66
Tao Bao10f441a2019-04-19 15:22:15 -070067static bool WriteCommandToFd(MinadbdCommand cmd, int fd) {
xunchang34690ce2019-04-05 16:16:07 -070068 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.
81static 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 Baoed717ca2019-04-04 18:37:58 -070099static MinadbdErrorCode RunAdbFuseSideload(int sfd, const std::string& args,
100 MinadbdCommandStatus* status) {
101 auto pieces = android::base::Split(args, ":");
katao77d61732018-09-20 20:34:05 +0800102 int64_t file_size;
103 int block_size;
Tao Baoed717ca2019-04-04 18:37:58 -0700104 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) {
xunchang34690ce2019-04-05 16:16:07 -0700106 LOG(ERROR) << "bad sideload-host arguments: " << args;
xunchangfedeef62019-04-22 12:18:14 -0700107 return kMinadbdHostCommandArgumentError;
katao77d61732018-09-20 20:34:05 +0800108 }
Doug Zongker9270a202012-01-09 15:16:13 -0800109
xunchang34690ce2019-04-05 16:16:07 -0700110 LOG(INFO) << "sideload-host file size " << file_size << ", block size " << block_size;
111
Tao Bao10f441a2019-04-19 15:22:15 -0700112 if (!WriteCommandToFd(MinadbdCommand::kInstall, minadbd_socket)) {
Tao Baoed717ca2019-04-04 18:37:58 -0700113 return kMinadbdSocketIOError;
xunchang34690ce2019-04-05 16:16:07 -0700114 }
Doug Zongker9270a202012-01-09 15:16:13 -0800115
Tao Bao2be97372019-04-15 12:45:50 -0700116 auto adb_data_reader = std::make_unique<FuseAdbDataProvider>(sfd, file_size, block_size);
xunchang9c04eb42019-04-17 14:43:58 -0700117 if (int result = run_fuse_sideload(std::move(adb_data_reader), sideload_mount_point.c_str());
118 result != 0) {
xunchang34690ce2019-04-05 16:16:07 -0700119 LOG(ERROR) << "Failed to start fuse";
Tao Baoed717ca2019-04-04 18:37:58 -0700120 return kMinadbdFuseStartError;
xunchang34690ce2019-04-05 16:16:07 -0700121 }
Doug Zongker9270a202012-01-09 15:16:13 -0800122
Tao Baoed717ca2019-04-04 18:37:58 -0700123 if (!WaitForCommandStatus(minadbd_socket, status)) {
124 return kMinadbdMessageFormatError;
125 }
126
xunchangfedeef62019-04-22 12:18:14 -0700127 // Signal host-side adb to stop. For sideload mode, we always send kMinadbdServicesExitSuccess
Tao Baoed717ca2019-04-04 18:37:58 -0700128 // (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) {
xunchangfedeef62019-04-22 12:18:14 -0700131 if (!android::base::WriteFully(sfd, kMinadbdServicesExitSuccess,
132 strlen(kMinadbdServicesExitSuccess))) {
Tao Baoed717ca2019-04-04 18:37:58 -0700133 return kMinadbdHostSocketIOError;
134 }
135 } else {
xunchangfedeef62019-04-22 12:18:14 -0700136 if (!android::base::WriteFully(sfd, kMinadbdServicesExitFailure,
137 strlen(kMinadbdServicesExitFailure))) {
Tao Baoed717ca2019-04-04 18:37:58 -0700138 return kMinadbdHostSocketIOError;
139 }
140 }
141
142 return kMinadbdSuccess;
143}
144
145// Sideload service always exits after serving an install command.
146static void SideloadHostService(unique_fd sfd, const std::string& args) {
xunchang34690ce2019-04-05 16:16:07 -0700147 MinadbdCommandStatus status;
Tao Baoed717ca2019-04-04 18:37:58 -0700148 exit(RunAdbFuseSideload(sfd.get(), args, &status));
149}
xunchang34690ce2019-04-05 16:16:07 -0700150
Tao Baoed717ca2019-04-04 18:37:58 -0700151// Rescue service waits for the next command after an install command.
152static 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 Baod8db81a2019-06-04 11:20:00 -0700159// Answers the query on a given property. The result will be written to the given sfd. If given an
160// empty string, dumps all the supported properties (similar to `adb shell getprop`) in lines, e.g.
161// "[prop]: [value]".
Tao Baoed717ca2019-04-04 18:37:58 -0700162static void RescueGetpropHostService(unique_fd sfd, const std::string& prop) {
Tao Baod8db81a2019-06-04 11:20:00 -0700163 static const std::set<std::string> kGetpropAllowedProps = {
Tao Baoed717ca2019-04-04 18:37:58 -0700164 "ro.build.date.utc",
Tao Bao3b9ef342019-06-03 12:10:54 -0700165 "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 Baoed717ca2019-04-04 18:37:58 -0700173 };
Tao Baod8db81a2019-06-04 11:20:00 -0700174 if (!prop.empty() && kGetpropAllowedProps.find(prop) == kGetpropAllowedProps.end()) {
Tao Baoed717ca2019-04-04 18:37:58 -0700175 return;
176 }
177
Tao Baod8db81a2019-06-04 11:20:00 -0700178 std::string result;
179 if (prop.empty()) {
180 for (const auto& key : kGetpropAllowedProps) {
181 auto value = android::base::GetProperty(key, "");
182 if (value.empty()) {
183 continue;
184 }
185 result += "[" + key + "]: [" + value + "]\n";
186 }
187 } else {
188 result = android::base::GetProperty(prop, "");
189 }
Tao Baoed717ca2019-04-04 18:37:58 -0700190 if (result.empty()) {
191 return;
192 }
193 if (!android::base::WriteFully(sfd, result.data(), result.size())) {
194 exit(kMinadbdHostSocketIOError);
195 }
Doug Zongker9270a202012-01-09 15:16:13 -0800196}
197
Tao Bao10f441a2019-04-19 15:22:15 -0700198// Reboots into the given target. We don't reboot directly from minadbd, but going through recovery
199// instead. This allows recovery to finish all the pending works (clear BCB, save logs etc) before
200// the reboot.
201static void RebootHostService(unique_fd /* sfd */, const std::string& target) {
202 MinadbdCommand command;
203 if (target == "bootloader") {
204 command = MinadbdCommand::kRebootBootloader;
205 } else if (target == "rescue") {
206 command = MinadbdCommand::kRebootRescue;
207 } else if (target == "recovery") {
208 command = MinadbdCommand::kRebootRecovery;
209 } else if (target == "fastboot") {
210 command = MinadbdCommand::kRebootFastboot;
211 } else {
212 command = MinadbdCommand::kRebootAndroid;
213 }
214 if (!WriteCommandToFd(command, minadbd_socket)) {
215 exit(kMinadbdSocketIOError);
216 }
217 MinadbdCommandStatus status;
218 if (!WaitForCommandStatus(minadbd_socket, &status)) {
219 exit(kMinadbdMessageFormatError);
220 }
221}
222
xunchangfedeef62019-04-22 12:18:14 -0700223static void WipeDeviceService(unique_fd fd, const std::string& args) {
224 auto pieces = android::base::Split(args, ":");
225 if (pieces.size() != 2 || pieces[0] != "userdata") {
226 LOG(ERROR) << "Failed to parse wipe device command arguments " << args;
227 exit(kMinadbdHostCommandArgumentError);
228 }
229
230 size_t message_size;
231 if (!android::base::ParseUint(pieces[1], &message_size) ||
232 message_size < strlen(kMinadbdServicesExitSuccess)) {
233 LOG(ERROR) << "Failed to parse wipe device message size in " << args;
234 exit(kMinadbdHostCommandArgumentError);
235 }
236
237 WriteCommandToFd(MinadbdCommand::kWipeData, minadbd_socket);
238 MinadbdCommandStatus status;
239 if (!WaitForCommandStatus(minadbd_socket, &status)) {
240 exit(kMinadbdMessageFormatError);
241 }
242
243 std::string response = (status == MinadbdCommandStatus::kSuccess) ? kMinadbdServicesExitSuccess
244 : kMinadbdServicesExitFailure;
245 response += std::string(message_size - response.size(), '\0');
246 if (!android::base::WriteFully(fd, response.c_str(), response.size())) {
247 exit(kMinadbdHostSocketIOError);
248 }
249}
250
Tao Bao5de19e22019-01-02 09:35:59 -0800251unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) {
Tao Bao10f441a2019-04-19 15:22:15 -0700252 // Common services that are supported both in sideload and rescue modes.
Elliott Hughes93838f62019-05-03 10:33:04 -0700253 if (android::base::ConsumePrefix(&name, "reboot:")) {
Tao Bao10f441a2019-04-19 15:22:15 -0700254 // "reboot:<target>", where target must be one of the following.
255 std::string args(name);
256 if (args.empty() || args == "bootloader" || args == "rescue" || args == "recovery" ||
257 args == "fastboot") {
258 return create_service_thread("reboot",
259 std::bind(RebootHostService, std::placeholders::_1, args));
260 }
261 return unique_fd{};
262 }
263
264 // Rescue-specific services.
Tao Baoed717ca2019-04-04 18:37:58 -0700265 if (rescue_mode) {
Elliott Hughes93838f62019-05-03 10:33:04 -0700266 if (android::base::ConsumePrefix(&name, "rescue-install:")) {
Tao Baoed717ca2019-04-04 18:37:58 -0700267 // rescue-install:<file-size>:<block-size>
268 std::string args(name);
269 return create_service_thread(
270 "rescue-install", std::bind(RescueInstallHostService, std::placeholders::_1, args));
Elliott Hughes93838f62019-05-03 10:33:04 -0700271 } else if (android::base::ConsumePrefix(&name, "rescue-getprop:")) {
Tao Baoed717ca2019-04-04 18:37:58 -0700272 // rescue-getprop:<prop>
273 std::string args(name);
274 return create_service_thread(
275 "rescue-getprop", std::bind(RescueGetpropHostService, std::placeholders::_1, args));
Elliott Hughes93838f62019-05-03 10:33:04 -0700276 } else if (android::base::ConsumePrefix(&name, "rescue-wipe:")) {
xunchangfedeef62019-04-22 12:18:14 -0700277 // rescue-wipe:target:<message-size>
278 std::string args(name);
279 return create_service_thread("rescue-wipe",
280 std::bind(WipeDeviceService, std::placeholders::_1, args));
Tao Baoed717ca2019-04-04 18:37:58 -0700281 }
xunchangfedeef62019-04-22 12:18:14 -0700282
Tao Baoed717ca2019-04-04 18:37:58 -0700283 return unique_fd{};
284 }
285
Tao Bao10f441a2019-04-19 15:22:15 -0700286 // Sideload-specific services.
Tao Bao5de19e22019-01-02 09:35:59 -0800287 if (name.starts_with("sideload:")) {
288 // This exit status causes recovery to print a special error message saying to use a newer adb
289 // (that supports sideload-host).
xunchang34690ce2019-04-05 16:16:07 -0700290 exit(kMinadbdAdbVersionError);
Elliott Hughes93838f62019-05-03 10:33:04 -0700291 } else if (android::base::ConsumePrefix(&name, "sideload-host:")) {
Tao Baoed717ca2019-04-04 18:37:58 -0700292 // sideload-host:<file-size>:<block-size>
293 std::string args(name);
Josh Gao038c4a12018-07-27 11:18:30 -0700294 return create_service_thread("sideload-host",
Tao Baoed717ca2019-04-04 18:37:58 -0700295 std::bind(SideloadHostService, std::placeholders::_1, args));
Tao Bao99f0d9e2016-10-13 12:46:38 -0700296 }
Josh Gao038c4a12018-07-27 11:18:30 -0700297 return unique_fd{};
Doug Zongker9270a202012-01-09 15:16:13 -0800298}