blob: 136392a64cba069a8c8e4e7e393f08ffd4c7337e [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>
Tianjie Xu6af51a02017-04-19 14:00:52 -070028#include <string>
Tao Bao5de19e22019-01-02 09:35:59 -080029#include <string_view>
Josh Gao2aa0d3a2017-04-13 12:45:55 -070030#include <thread>
Tao Baoed717ca2019-04-04 18:37:58 -070031#include <unordered_set>
Josh Gao2aa0d3a2017-04-13 12:45:55 -070032
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;
53
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
xunchang34690ce2019-04-05 16:16:07 -070062static bool WriteCommandToFd(MinadbdCommands cmd, int fd) {
63 char message[kMinadbdMessageSize];
64 memcpy(message, kMinadbdCommandPrefix, strlen(kMinadbdStatusPrefix));
65 android::base::put_unaligned(message + strlen(kMinadbdStatusPrefix), cmd);
66
67 if (!android::base::WriteFully(fd, message, kMinadbdMessageSize)) {
68 PLOG(ERROR) << "Failed to write message " << message;
69 return false;
70 }
71 return true;
72}
73
74// Blocks and reads the command status from |fd|. Returns false if the received message has a
75// format error.
76static bool WaitForCommandStatus(int fd, MinadbdCommandStatus* status) {
77 char buffer[kMinadbdMessageSize];
78 if (!android::base::ReadFully(fd, buffer, kMinadbdMessageSize)) {
79 PLOG(ERROR) << "Failed to response status from socket";
80 exit(kMinadbdSocketIOError);
81 }
82
83 std::string message(buffer, buffer + kMinadbdMessageSize);
84 if (!android::base::StartsWith(message, kMinadbdStatusPrefix)) {
85 LOG(ERROR) << "Failed to parse status in " << message;
86 return false;
87 }
88
89 *status = android::base::get_unaligned<MinadbdCommandStatus>(
90 message.substr(strlen(kMinadbdStatusPrefix)).c_str());
91 return true;
92}
93
Tao Baoed717ca2019-04-04 18:37:58 -070094static MinadbdErrorCode RunAdbFuseSideload(int sfd, const std::string& args,
95 MinadbdCommandStatus* status) {
96 auto pieces = android::base::Split(args, ":");
katao77d61732018-09-20 20:34:05 +080097 int64_t file_size;
98 int block_size;
Tao Baoed717ca2019-04-04 18:37:58 -070099 if (pieces.size() != 2 || !android::base::ParseInt(pieces[0], &file_size) || file_size <= 0 ||
100 !android::base::ParseInt(pieces[1], &block_size) || block_size <= 0) {
xunchang34690ce2019-04-05 16:16:07 -0700101 LOG(ERROR) << "bad sideload-host arguments: " << args;
Tao Baoed717ca2019-04-04 18:37:58 -0700102 return kMinadbdPackageSizeError;
katao77d61732018-09-20 20:34:05 +0800103 }
Doug Zongker9270a202012-01-09 15:16:13 -0800104
xunchang34690ce2019-04-05 16:16:07 -0700105 LOG(INFO) << "sideload-host file size " << file_size << ", block size " << block_size;
106
107 if (!WriteCommandToFd(MinadbdCommands::kInstall, minadbd_socket)) {
Tao Baoed717ca2019-04-04 18:37:58 -0700108 return kMinadbdSocketIOError;
xunchang34690ce2019-04-05 16:16:07 -0700109 }
Doug Zongker9270a202012-01-09 15:16:13 -0800110
Tao Bao2be97372019-04-15 12:45:50 -0700111 auto adb_data_reader = std::make_unique<FuseAdbDataProvider>(sfd, file_size, block_size);
xunchang34690ce2019-04-05 16:16:07 -0700112 if (int result = run_fuse_sideload(std::move(adb_data_reader)); result != 0) {
113 LOG(ERROR) << "Failed to start fuse";
Tao Baoed717ca2019-04-04 18:37:58 -0700114 return kMinadbdFuseStartError;
xunchang34690ce2019-04-05 16:16:07 -0700115 }
Doug Zongker9270a202012-01-09 15:16:13 -0800116
Tao Baoed717ca2019-04-04 18:37:58 -0700117 if (!WaitForCommandStatus(minadbd_socket, status)) {
118 return kMinadbdMessageFormatError;
119 }
120
121 // Signal host-side adb to stop. For sideload mode, we always send kSideloadServiceExitSuccess
122 // (i.e. "DONEDONE") regardless of the install result. For rescue mode, we send failure message on
123 // install error.
124 if (!rescue_mode || *status == MinadbdCommandStatus::kSuccess) {
125 if (!android::base::WriteFully(sfd, kSideloadServiceExitSuccess,
126 strlen(kSideloadServiceExitSuccess))) {
127 return kMinadbdHostSocketIOError;
128 }
129 } else {
130 if (!android::base::WriteFully(sfd, kSideloadServiceExitFailure,
131 strlen(kSideloadServiceExitFailure))) {
132 return kMinadbdHostSocketIOError;
133 }
134 }
135
136 return kMinadbdSuccess;
137}
138
139// Sideload service always exits after serving an install command.
140static void SideloadHostService(unique_fd sfd, const std::string& args) {
xunchang34690ce2019-04-05 16:16:07 -0700141 MinadbdCommandStatus status;
Tao Baoed717ca2019-04-04 18:37:58 -0700142 exit(RunAdbFuseSideload(sfd.get(), args, &status));
143}
xunchang34690ce2019-04-05 16:16:07 -0700144
Tao Baoed717ca2019-04-04 18:37:58 -0700145// Rescue service waits for the next command after an install command.
146static void RescueInstallHostService(unique_fd sfd, const std::string& args) {
147 MinadbdCommandStatus status;
148 if (auto result = RunAdbFuseSideload(sfd.get(), args, &status); result != kMinadbdSuccess) {
149 exit(result);
150 }
151}
152
153static void RescueGetpropHostService(unique_fd sfd, const std::string& prop) {
154 static const std::unordered_set<std::string> kGetpropAllowedProps = {
155 "ro.build.fingerprint",
156 "ro.build.date.utc",
157 };
158 auto allowed = kGetpropAllowedProps.find(prop) != kGetpropAllowedProps.end();
159 if (!allowed) {
160 return;
161 }
162
163 auto result = android::base::GetProperty(prop, "");
164 if (result.empty()) {
165 return;
166 }
167 if (!android::base::WriteFully(sfd, result.data(), result.size())) {
168 exit(kMinadbdHostSocketIOError);
169 }
Doug Zongker9270a202012-01-09 15:16:13 -0800170}
171
Tao Bao5de19e22019-01-02 09:35:59 -0800172unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) {
Tao Baoed717ca2019-04-04 18:37:58 -0700173 if (rescue_mode) {
174 if (ConsumePrefix(&name, "rescue-install:")) {
175 // rescue-install:<file-size>:<block-size>
176 std::string args(name);
177 return create_service_thread(
178 "rescue-install", std::bind(RescueInstallHostService, std::placeholders::_1, args));
179 } else if (ConsumePrefix(&name, "rescue-getprop:")) {
180 // rescue-getprop:<prop>
181 std::string args(name);
182 return create_service_thread(
183 "rescue-getprop", std::bind(RescueGetpropHostService, std::placeholders::_1, args));
184 }
185 return unique_fd{};
186 }
187
Tao Bao5de19e22019-01-02 09:35:59 -0800188 if (name.starts_with("sideload:")) {
189 // This exit status causes recovery to print a special error message saying to use a newer adb
190 // (that supports sideload-host).
xunchang34690ce2019-04-05 16:16:07 -0700191 exit(kMinadbdAdbVersionError);
Tao Baoed717ca2019-04-04 18:37:58 -0700192 } else if (ConsumePrefix(&name, "sideload-host:")) {
193 // sideload-host:<file-size>:<block-size>
194 std::string args(name);
Josh Gao038c4a12018-07-27 11:18:30 -0700195 return create_service_thread("sideload-host",
Tao Baoed717ca2019-04-04 18:37:58 -0700196 std::bind(SideloadHostService, std::placeholders::_1, args));
Tao Bao99f0d9e2016-10-13 12:46:38 -0700197 }
Josh Gao038c4a12018-07-27 11:18:30 -0700198 return unique_fd{};
Doug Zongker9270a202012-01-09 15:16:13 -0800199}