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> |
Tianjie Xu | 6af51a0 | 2017-04-19 14:00:52 -0700 | [diff] [blame] | 28 | #include <string> |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 29 | #include <string_view> |
Josh Gao | 2aa0d3a | 2017-04-13 12:45:55 -0700 | [diff] [blame] | 30 | #include <thread> |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 31 | #include <unordered_set> |
Josh Gao | 2aa0d3a | 2017-04-13 12:45:55 -0700 | [diff] [blame] | 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 "fdevent.h" |
| 45 | #include "fuse_adb_provider.h" |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 46 | #include "fuse_sideload.h" |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 47 | #include "minadbd_types.h" |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 48 | #include "services.h" |
Elliott Hughes | 24eb8a0 | 2016-06-15 15:12:17 -0700 | [diff] [blame] | 49 | #include "sysdeps.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 50 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 51 | static int minadbd_socket = -1; |
Tao Bao | c6dc325 | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 52 | static bool rescue_mode = false; |
| 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 | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 62 | static 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. |
| 76 | static 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 Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 94 | static MinadbdErrorCode RunAdbFuseSideload(int sfd, const std::string& args, |
| 95 | MinadbdCommandStatus* status) { |
| 96 | auto pieces = android::base::Split(args, ":"); |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 97 | int64_t file_size; |
| 98 | int block_size; |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 99 | 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) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 101 | LOG(ERROR) << "bad sideload-host arguments: " << args; |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 102 | return kMinadbdPackageSizeError; |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 103 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 104 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 105 | LOG(INFO) << "sideload-host file size " << file_size << ", block size " << block_size; |
| 106 | |
| 107 | if (!WriteCommandToFd(MinadbdCommands::kInstall, minadbd_socket)) { |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 108 | return kMinadbdSocketIOError; |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 109 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 110 | |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 111 | auto adb_data_reader = std::make_unique<FuseAdbDataProvider>(sfd, file_size, block_size); |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 112 | if (int result = run_fuse_sideload(std::move(adb_data_reader)); result != 0) { |
| 113 | LOG(ERROR) << "Failed to start fuse"; |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 114 | return kMinadbdFuseStartError; |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 115 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 116 | |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 117 | 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. |
| 140 | static void SideloadHostService(unique_fd sfd, const std::string& args) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 141 | MinadbdCommandStatus status; |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 142 | exit(RunAdbFuseSideload(sfd.get(), args, &status)); |
| 143 | } |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 144 | |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 145 | // Rescue service waits for the next command after an install command. |
| 146 | static 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 | |
| 153 | static 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 Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 172 | unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) { |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 173 | 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 Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 188 | 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). |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 191 | exit(kMinadbdAdbVersionError); |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 192 | } else if (ConsumePrefix(&name, "sideload-host:")) { |
| 193 | // sideload-host:<file-size>:<block-size> |
| 194 | std::string args(name); |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 195 | return create_service_thread("sideload-host", |
Tao Bao | ed717ca | 2019-04-04 18:37:58 -0700 | [diff] [blame] | 196 | std::bind(SideloadHostService, std::placeholders::_1, args)); |
Tao Bao | 99f0d9e | 2016-10-13 12:46:38 -0700 | [diff] [blame] | 197 | } |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 198 | return unique_fd{}; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 199 | } |