blob: 5792c7a1762cff801542688daed5f0b52454e1aa [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 "fuse_adb_provider.h"
xunchang5e6832a2019-03-15 16:04:32 -070045#include "fuse_sideload.h"
xunchang34690ce2019-04-05 16:16:07 -070046#include "minadbd_types.h"
Josh Gao038c4a12018-07-27 11:18:30 -070047#include "services.h"
Elliott Hughes24eb8a02016-06-15 15:12:17 -070048#include "sysdeps.h"
Doug Zongker9270a202012-01-09 15:16:13 -080049
xunchang34690ce2019-04-05 16:16:07 -070050static int minadbd_socket = -1;
Tao Baoc6dc3252019-04-16 14:22:25 -070051static bool rescue_mode = false;
xunchang9c04eb42019-04-17 14:43:58 -070052static std::string sideload_mount_point = FUSE_SIDELOAD_HOST_MOUNTPOINT;
Tao Baoc6dc3252019-04-16 14:22:25 -070053
xunchang34690ce2019-04-05 16:16:07 -070054void SetMinadbdSocketFd(int socket_fd) {
55 minadbd_socket = socket_fd;
56}
57
Tao Baoc6dc3252019-04-16 14:22:25 -070058void SetMinadbdRescueMode(bool rescue) {
59 rescue_mode = rescue;
60}
61
xunchang9c04eb42019-04-17 14:43:58 -070062void SetSideloadMountPoint(const std::string& path) {
63 sideload_mount_point = path;
64}
65
Tao Bao10f441a2019-04-19 15:22:15 -070066static bool WriteCommandToFd(MinadbdCommand cmd, int fd) {
xunchang34690ce2019-04-05 16:16:07 -070067 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.
80static 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 Baoed717ca2019-04-04 18:37:58 -070098static MinadbdErrorCode RunAdbFuseSideload(int sfd, const std::string& args,
99 MinadbdCommandStatus* status) {
100 auto pieces = android::base::Split(args, ":");
katao77d61732018-09-20 20:34:05 +0800101 int64_t file_size;
102 int block_size;
Tao Baoed717ca2019-04-04 18:37:58 -0700103 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) {
xunchang34690ce2019-04-05 16:16:07 -0700105 LOG(ERROR) << "bad sideload-host arguments: " << args;
xunchangfedeef62019-04-22 12:18:14 -0700106 return kMinadbdHostCommandArgumentError;
katao77d61732018-09-20 20:34:05 +0800107 }
Doug Zongker9270a202012-01-09 15:16:13 -0800108
xunchang34690ce2019-04-05 16:16:07 -0700109 LOG(INFO) << "sideload-host file size " << file_size << ", block size " << block_size;
110
Tao Bao10f441a2019-04-19 15:22:15 -0700111 if (!WriteCommandToFd(MinadbdCommand::kInstall, minadbd_socket)) {
Tao Baoed717ca2019-04-04 18:37:58 -0700112 return kMinadbdSocketIOError;
xunchang34690ce2019-04-05 16:16:07 -0700113 }
Doug Zongker9270a202012-01-09 15:16:13 -0800114
Tao Bao2be97372019-04-15 12:45:50 -0700115 auto adb_data_reader = std::make_unique<FuseAdbDataProvider>(sfd, file_size, block_size);
xunchang9c04eb42019-04-17 14:43:58 -0700116 if (int result = run_fuse_sideload(std::move(adb_data_reader), sideload_mount_point.c_str());
117 result != 0) {
xunchang34690ce2019-04-05 16:16:07 -0700118 LOG(ERROR) << "Failed to start fuse";
Tao Baoed717ca2019-04-04 18:37:58 -0700119 return kMinadbdFuseStartError;
xunchang34690ce2019-04-05 16:16:07 -0700120 }
Doug Zongker9270a202012-01-09 15:16:13 -0800121
Tao Baoed717ca2019-04-04 18:37:58 -0700122 if (!WaitForCommandStatus(minadbd_socket, status)) {
123 return kMinadbdMessageFormatError;
124 }
125
xunchangfedeef62019-04-22 12:18:14 -0700126 // Signal host-side adb to stop. For sideload mode, we always send kMinadbdServicesExitSuccess
Tao Baoed717ca2019-04-04 18:37:58 -0700127 // (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) {
xunchangfedeef62019-04-22 12:18:14 -0700130 if (!android::base::WriteFully(sfd, kMinadbdServicesExitSuccess,
131 strlen(kMinadbdServicesExitSuccess))) {
Tao Baoed717ca2019-04-04 18:37:58 -0700132 return kMinadbdHostSocketIOError;
133 }
134 } else {
xunchangfedeef62019-04-22 12:18:14 -0700135 if (!android::base::WriteFully(sfd, kMinadbdServicesExitFailure,
136 strlen(kMinadbdServicesExitFailure))) {
Tao Baoed717ca2019-04-04 18:37:58 -0700137 return kMinadbdHostSocketIOError;
138 }
139 }
140
141 return kMinadbdSuccess;
142}
143
144// Sideload service always exits after serving an install command.
145static void SideloadHostService(unique_fd sfd, const std::string& args) {
xunchang34690ce2019-04-05 16:16:07 -0700146 MinadbdCommandStatus status;
Tao Baoed717ca2019-04-04 18:37:58 -0700147 exit(RunAdbFuseSideload(sfd.get(), args, &status));
148}
xunchang34690ce2019-04-05 16:16:07 -0700149
Tao Baoed717ca2019-04-04 18:37:58 -0700150// Rescue service waits for the next command after an install command.
151static 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 Bao57a27892019-06-10 12:41:44 -0700158// 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 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 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 Bao57a27892019-06-10 12:41:44 -0700183 } else if (kGetpropAllowedProps.find(prop) != kGetpropAllowedProps.end()) {
184 result = android::base::GetProperty(prop, "") + "\n";
Tao Baod8db81a2019-06-04 11:20:00 -0700185 }
Tao Baoed717ca2019-04-04 18:37:58 -0700186 if (result.empty()) {
Tao Bao57a27892019-06-10 12:41:44 -0700187 result = "\n";
Tao Baoed717ca2019-04-04 18:37:58 -0700188 }
189 if (!android::base::WriteFully(sfd, result.data(), result.size())) {
190 exit(kMinadbdHostSocketIOError);
191 }
Doug Zongker9270a202012-01-09 15:16:13 -0800192}
193
Tao Bao10f441a2019-04-19 15:22:15 -0700194// 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.
197static 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
xunchangfedeef62019-04-22 12:18:14 -0700219static 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 Bao5de19e22019-01-02 09:35:59 -0800247unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) {
Tao Bao10f441a2019-04-19 15:22:15 -0700248 // Common services that are supported both in sideload and rescue modes.
Elliott Hughes93838f62019-05-03 10:33:04 -0700249 if (android::base::ConsumePrefix(&name, "reboot:")) {
Tao Bao10f441a2019-04-19 15:22:15 -0700250 // "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 Baoed717ca2019-04-04 18:37:58 -0700261 if (rescue_mode) {
Elliott Hughes93838f62019-05-03 10:33:04 -0700262 if (android::base::ConsumePrefix(&name, "rescue-install:")) {
Tao Baoed717ca2019-04-04 18:37:58 -0700263 // 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 Hughes93838f62019-05-03 10:33:04 -0700267 } else if (android::base::ConsumePrefix(&name, "rescue-getprop:")) {
Tao Baoed717ca2019-04-04 18:37:58 -0700268 // rescue-getprop:<prop>
269 std::string args(name);
270 return create_service_thread(
271 "rescue-getprop", std::bind(RescueGetpropHostService, std::placeholders::_1, args));
Elliott Hughes93838f62019-05-03 10:33:04 -0700272 } else if (android::base::ConsumePrefix(&name, "rescue-wipe:")) {
xunchangfedeef62019-04-22 12:18:14 -0700273 // 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 Baoed717ca2019-04-04 18:37:58 -0700277 }
xunchangfedeef62019-04-22 12:18:14 -0700278
Tao Baoed717ca2019-04-04 18:37:58 -0700279 return unique_fd{};
280 }
281
Tao Bao10f441a2019-04-19 15:22:15 -0700282 // Sideload-specific services.
Tao Bao5de19e22019-01-02 09:35:59 -0800283 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).
xunchang34690ce2019-04-05 16:16:07 -0700286 exit(kMinadbdAdbVersionError);
Elliott Hughes93838f62019-05-03 10:33:04 -0700287 } else if (android::base::ConsumePrefix(&name, "sideload-host:")) {
Tao Baoed717ca2019-04-04 18:37:58 -0700288 // sideload-host:<file-size>:<block-size>
289 std::string args(name);
Josh Gao038c4a12018-07-27 11:18:30 -0700290 return create_service_thread("sideload-host",
Tao Baoed717ca2019-04-04 18:37:58 -0700291 std::bind(SideloadHostService, std::placeholders::_1, args));
Tao Bao99f0d9e2016-10-13 12:46:38 -0700292 }
Josh Gao038c4a12018-07-27 11:18:30 -0700293 return unique_fd{};
Doug Zongker9270a202012-01-09 15:16:13 -0800294}