Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 17 | #include "install/adb_install.h" |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 18 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <signal.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 24 | #include <sys/epoll.h> |
| 25 | #include <sys/socket.h> |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 26 | #include <sys/stat.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <sys/wait.h> |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 29 | #include <unistd.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 30 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 31 | #include <atomic> |
| 32 | #include <functional> |
| 33 | #include <map> |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 34 | #include <utility> |
Tao Bao | c6dc325 | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 35 | #include <vector> |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 36 | |
| 37 | #include <android-base/file.h> |
Tao Bao | 0167d4c | 2017-05-11 14:44:15 -0700 | [diff] [blame] | 38 | #include <android-base/logging.h> |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 39 | #include <android-base/memory.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 40 | #include <android-base/properties.h> |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 41 | #include <android-base/strings.h> |
| 42 | #include <android-base/unique_fd.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 43 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 44 | #include "fuse_sideload.h" |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 45 | #include "install/install.h" |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 46 | #include "install/wipe_data.h" |
Tao Bao | 3305d48 | 2019-09-26 00:02:29 -0700 | [diff] [blame] | 47 | #include "minadbd/types.h" |
Tao Bao | c6dc325 | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 48 | #include "otautil/sysutil.h" |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 49 | #include "recovery_ui/device.h" |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 50 | #include "recovery_ui/ui.h" |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 51 | |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 52 | // A CommandFunction returns a pair of (result, should_continue), which indicates the command |
| 53 | // execution result and whether it should proceed to the next iteration. The execution result will |
| 54 | // always be sent to the minadbd side. |
| 55 | using CommandFunction = std::function<std::pair<bool, bool>()>; |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 56 | |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 57 | static bool SetUsbConfig(const std::string& state) { |
| 58 | android::base::SetProperty("sys.usb.config", state); |
| 59 | return android::base::WaitForProperty("sys.usb.state", state); |
| 60 | } |
| 61 | |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 62 | // Parses the minadbd command in |message|; returns MinadbdCommand::kError upon errors. |
| 63 | static MinadbdCommand ParseMinadbdCommand(const std::string& message) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 64 | if (!android::base::StartsWith(message, kMinadbdCommandPrefix)) { |
| 65 | LOG(ERROR) << "Failed to parse command in message " << message; |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 66 | return MinadbdCommand::kError; |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | auto cmd_code_string = message.substr(strlen(kMinadbdCommandPrefix)); |
| 70 | auto cmd_code = android::base::get_unaligned<uint32_t>(cmd_code_string.c_str()); |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 71 | if (cmd_code >= static_cast<uint32_t>(MinadbdCommand::kError)) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 72 | LOG(ERROR) << "Unsupported command code: " << cmd_code; |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 73 | return MinadbdCommand::kError; |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 76 | return static_cast<MinadbdCommand>(cmd_code); |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static bool WriteStatusToFd(MinadbdCommandStatus status, int fd) { |
| 80 | char message[kMinadbdMessageSize]; |
| 81 | memcpy(message, kMinadbdStatusPrefix, strlen(kMinadbdStatusPrefix)); |
| 82 | android::base::put_unaligned(message + strlen(kMinadbdStatusPrefix), status); |
| 83 | |
| 84 | if (!android::base::WriteFully(fd, message, kMinadbdMessageSize)) { |
| 85 | PLOG(ERROR) << "Failed to write message " << message; |
| 86 | return false; |
| 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 91 | // Installs the package from FUSE. Returns the installation result and whether it should continue |
| 92 | // waiting for new commands. |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 93 | static auto AdbInstallPackageHandler(Device* device, InstallResult* result) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 94 | // How long (in seconds) we wait for the package path to be ready. It doesn't need to be too long |
| 95 | // because the minadbd service has already issued an install command. FUSE_SIDELOAD_HOST_PATHNAME |
| 96 | // will start to exist once the host connects and starts serving a package. Poll for its |
| 97 | // appearance. (Note that inotify doesn't work with FUSE.) |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 98 | auto ui = device->GetUI(); |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 99 | constexpr int ADB_INSTALL_TIMEOUT = 15; |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 100 | bool should_continue = true; |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 101 | *result = INSTALL_ERROR; |
| 102 | for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) { |
| 103 | struct stat st; |
| 104 | if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) { |
| 105 | if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT - 1) { |
| 106 | sleep(1); |
| 107 | continue; |
| 108 | } else { |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 109 | should_continue = false; |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 110 | ui->Print("\nTimed out waiting for fuse to be ready.\n\n"); |
| 111 | break; |
| 112 | } |
| 113 | } |
Tom Marshall | 8f4e010 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 114 | ui->CancelWaitKey(); |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 115 | |
| 116 | auto package = |
| 117 | Package::CreateFilePackage(FUSE_SIDELOAD_HOST_PATHNAME, |
| 118 | std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1)); |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 119 | *result = InstallPackage(package.get(), FUSE_SIDELOAD_HOST_PATHNAME, false, 0, device); |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | |
| 123 | // Calling stat() on this magic filename signals the FUSE to exit. |
| 124 | struct stat st; |
| 125 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 126 | return std::make_pair(*result == INSTALL_SUCCESS, should_continue); |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Tao Bao | adc99ef | 2019-04-29 23:48:02 -0700 | [diff] [blame] | 129 | static auto AdbRebootHandler(MinadbdCommand command, InstallResult* result, |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 130 | Device::BuiltinAction* reboot_action) { |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 131 | // Use Device::REBOOT_{FASTBOOT,RECOVERY,RESCUE}, instead of the ones with ENTER_. This allows |
| 132 | // rebooting back into fastboot/recovery/rescue mode through bootloader, which may use a newly |
| 133 | // installed bootloader/recovery image. |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 134 | switch (command) { |
| 135 | case MinadbdCommand::kRebootBootloader: |
| 136 | *reboot_action = Device::REBOOT_BOOTLOADER; |
| 137 | break; |
| 138 | case MinadbdCommand::kRebootFastboot: |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 139 | *reboot_action = Device::REBOOT_FASTBOOT; |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 140 | break; |
| 141 | case MinadbdCommand::kRebootRecovery: |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 142 | *reboot_action = Device::REBOOT_RECOVERY; |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 143 | break; |
| 144 | case MinadbdCommand::kRebootRescue: |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 145 | *reboot_action = Device::REBOOT_RESCUE; |
| 146 | break; |
| 147 | case MinadbdCommand::kRebootAndroid: |
| 148 | default: |
| 149 | *reboot_action = Device::REBOOT; |
| 150 | break; |
| 151 | } |
| 152 | *result = INSTALL_REBOOT; |
| 153 | return std::make_pair(true, false); |
| 154 | } |
| 155 | |
| 156 | // Parses and executes the command from minadbd. Returns whether the caller should keep waiting for |
| 157 | // next command. |
| 158 | static bool HandleMessageFromMinadbd(int socket_fd, |
| 159 | const std::map<MinadbdCommand, CommandFunction>& command_map) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 160 | char buffer[kMinadbdMessageSize]; |
| 161 | if (!android::base::ReadFully(socket_fd, buffer, kMinadbdMessageSize)) { |
| 162 | PLOG(ERROR) << "Failed to read message from minadbd"; |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | std::string message(buffer, buffer + kMinadbdMessageSize); |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 167 | auto command_type = ParseMinadbdCommand(message); |
| 168 | if (command_type == MinadbdCommand::kError) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | if (command_map.find(command_type) == command_map.end()) { |
| 172 | LOG(ERROR) << "Unsupported command: " |
| 173 | << android::base::get_unaligned<unsigned int>( |
| 174 | message.substr(strlen(kMinadbdCommandPrefix)).c_str()); |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // We have received a valid command, execute the corresponding function. |
| 179 | const auto& command_func = command_map.at(command_type); |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 180 | const auto [result, should_continue] = command_func(); |
| 181 | LOG(INFO) << "Command " << static_cast<uint32_t>(command_type) << " finished with " << result; |
| 182 | if (!WriteStatusToFd(result ? MinadbdCommandStatus::kSuccess : MinadbdCommandStatus::kFailure, |
| 183 | socket_fd)) { |
| 184 | return false; |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 185 | } |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 186 | return should_continue; |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // TODO(xunchang) add a wrapper function and kill the minadbd service there. |
| 190 | static void ListenAndExecuteMinadbdCommands( |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 191 | RecoveryUI* ui, pid_t minadbd_pid, android::base::unique_fd&& socket_fd, |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 192 | const std::map<MinadbdCommand, CommandFunction>& command_map) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 193 | android::base::unique_fd epoll_fd(epoll_create1(O_CLOEXEC)); |
| 194 | if (epoll_fd == -1) { |
| 195 | PLOG(ERROR) << "Failed to create epoll"; |
| 196 | kill(minadbd_pid, SIGKILL); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | constexpr int EPOLL_MAX_EVENTS = 10; |
| 201 | struct epoll_event ev = {}; |
| 202 | ev.events = EPOLLIN | EPOLLHUP; |
| 203 | ev.data.fd = socket_fd.get(); |
| 204 | struct epoll_event events[EPOLL_MAX_EVENTS]; |
| 205 | if (epoll_ctl(epoll_fd.get(), EPOLL_CTL_ADD, socket_fd.get(), &ev) == -1) { |
| 206 | PLOG(ERROR) << "Failed to add socket fd to epoll"; |
| 207 | kill(minadbd_pid, SIGKILL); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | // Set the timeout to be 300s when waiting for minadbd commands. |
| 212 | constexpr int TIMEOUT_MILLIS = 300 * 1000; |
| 213 | while (true) { |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 214 | // Reset the progress bar and the background image before each command. |
| 215 | ui->SetProgressType(RecoveryUI::EMPTY); |
| 216 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
| 217 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 218 | // Poll for the status change of the socket_fd, and handle the message if the fd is ready to |
| 219 | // read. |
| 220 | int event_count = |
| 221 | TEMP_FAILURE_RETRY(epoll_wait(epoll_fd.get(), events, EPOLL_MAX_EVENTS, TIMEOUT_MILLIS)); |
| 222 | if (event_count == -1) { |
| 223 | PLOG(ERROR) << "Failed to wait for epoll events"; |
| 224 | kill(minadbd_pid, SIGKILL); |
| 225 | return; |
| 226 | } |
| 227 | if (event_count == 0) { |
| 228 | LOG(ERROR) << "Timeout waiting for messages from minadbd"; |
| 229 | kill(minadbd_pid, SIGKILL); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | for (int n = 0; n < event_count; n++) { |
| 234 | if (events[n].events & EPOLLHUP) { |
| 235 | LOG(INFO) << "Socket has been closed"; |
| 236 | kill(minadbd_pid, SIGKILL); |
| 237 | return; |
| 238 | } |
| 239 | if (!HandleMessageFromMinadbd(socket_fd.get(), command_map)) { |
| 240 | kill(minadbd_pid, SIGKILL); |
| 241 | return; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // Recovery starts minadbd service as a child process, and spawns another thread to listen for the |
| 248 | // message from minadbd through a socket pair. Here is an example to execute one command from adb |
| 249 | // host. |
| 250 | // a. recovery b. listener thread c. minadbd service |
| 251 | // |
| 252 | // a1. create socket pair |
| 253 | // a2. fork minadbd service |
| 254 | // c3. wait for the adb commands |
| 255 | // from host |
| 256 | // c4. after receiving host commands: |
| 257 | // 1) set up pre-condition (i.e. |
| 258 | // start fuse for adb sideload) |
| 259 | // 2) issue command through |
| 260 | // socket. |
| 261 | // 3) wait for result |
| 262 | // a5. start listener thread |
| 263 | // b6. listen for message from |
| 264 | // minadbd in a loop. |
| 265 | // b7. After receiving a minadbd |
| 266 | // command from socket |
| 267 | // 1) execute the command function |
| 268 | // 2) send the result back to |
| 269 | // minadbd |
| 270 | // ...... |
| 271 | // c8. exit upon receiving the |
| 272 | // result |
| 273 | // a9. wait for listener thread |
| 274 | // to exit. |
| 275 | // |
| 276 | // a10. wait for minadbd to |
| 277 | // exit |
| 278 | // b11. exit the listening loop |
| 279 | // |
| 280 | static void CreateMinadbdServiceAndExecuteCommands( |
Tom Marshall | 8f4e010 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 281 | Device* device, const std::map<MinadbdCommand, CommandFunction>& command_map, |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 282 | bool rescue_mode) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 283 | signal(SIGPIPE, SIG_IGN); |
| 284 | |
| 285 | android::base::unique_fd recovery_socket; |
| 286 | android::base::unique_fd minadbd_socket; |
| 287 | if (!android::base::Socketpair(AF_UNIX, SOCK_STREAM, 0, &recovery_socket, &minadbd_socket)) { |
| 288 | PLOG(ERROR) << "Failed to create socket"; |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | pid_t child = fork(); |
| 293 | if (child == -1) { |
| 294 | PLOG(ERROR) << "Failed to fork child process"; |
| 295 | return; |
| 296 | } |
| 297 | if (child == 0) { |
| 298 | recovery_socket.reset(); |
Tao Bao | c6dc325 | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 299 | std::vector<std::string> minadbd_commands = { |
| 300 | "/system/bin/minadbd", |
| 301 | "--socket_fd", |
| 302 | std::to_string(minadbd_socket.release()), |
| 303 | }; |
| 304 | if (rescue_mode) { |
| 305 | minadbd_commands.push_back("--rescue"); |
| 306 | } |
| 307 | auto exec_args = StringVectorToNullTerminatedArray(minadbd_commands); |
| 308 | execv(exec_args[0], exec_args.data()); |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 309 | _exit(EXIT_FAILURE); |
| 310 | } |
| 311 | |
| 312 | minadbd_socket.reset(); |
| 313 | |
| 314 | // We need to call SetUsbConfig() after forking minadbd service. Because the function waits for |
| 315 | // the usb state to be updated, which depends on sys.usb.ffs.ready=1 set in the adb daemon. |
| 316 | if (!SetUsbConfig("sideload")) { |
| 317 | LOG(ERROR) << "Failed to set usb config to sideload"; |
| 318 | return; |
| 319 | } |
| 320 | |
Tom Marshall | 8f4e010 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 321 | RecoveryUI* ui = device->GetUI(); |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 322 | std::thread listener_thread(ListenAndExecuteMinadbdCommands, ui, child, |
| 323 | std::move(recovery_socket), std::ref(command_map)); |
Tom Marshall | 8f4e010 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 324 | |
| 325 | if (ui->IsTextVisible()) { |
| 326 | std::vector<std::string> headers{ rescue_mode ? "Rescue mode" : "ADB Sideload" }; |
| 327 | std::vector<std::string> entries{ "Cancel" }; |
| 328 | size_t chosen_item = ui->ShowMenu( |
| 329 | headers, entries, 0, true, |
| 330 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
| 331 | |
| 332 | if (chosen_item != Device::kDoSideload) { |
| 333 | // Kill minadbd if 'cancel' was selected, to abort sideload. |
| 334 | kill(child, SIGKILL); |
| 335 | } |
| 336 | } |
| 337 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 338 | if (listener_thread.joinable()) { |
| 339 | listener_thread.join(); |
| 340 | } |
| 341 | |
| 342 | int status; |
| 343 | waitpid(child, &status, 0); |
| 344 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 345 | if (WEXITSTATUS(status) == MinadbdErrorCode::kMinadbdAdbVersionError) { |
| 346 | LOG(ERROR) << "\nYou need adb 1.0.32 or newer to sideload\nto this device.\n"; |
| 347 | } else if (!WIFSIGNALED(status)) { |
| 348 | LOG(ERROR) << "\n(adbd status " << WEXITSTATUS(status) << ")"; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | signal(SIGPIPE, SIG_DFL); |
| 353 | } |
| 354 | |
Tao Bao | adc99ef | 2019-04-29 23:48:02 -0700 | [diff] [blame] | 355 | InstallResult ApplyFromAdb(Device* device, bool rescue_mode, Device::BuiltinAction* reboot_action) { |
Hridya Valsaraju | e4ef453 | 2018-08-31 11:57:51 -0700 | [diff] [blame] | 356 | // Save the usb state to restore after the sideload operation. |
| 357 | std::string usb_state = android::base::GetProperty("sys.usb.state", "none"); |
| 358 | // Clean up state and stop adbd. |
| 359 | if (usb_state != "none" && !SetUsbConfig("none")) { |
| 360 | LOG(ERROR) << "Failed to clear USB config"; |
| 361 | return INSTALL_ERROR; |
| 362 | } |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 363 | |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 364 | RecoveryUI* ui = device->GetUI(); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 365 | |
Alessandro Astone | 11b1acf | 2020-10-04 21:59:52 +0200 | [diff] [blame] | 366 | InstallResult install_result = INSTALL_NONE; |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 367 | std::map<MinadbdCommand, CommandFunction> command_map{ |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 368 | { MinadbdCommand::kInstall, std::bind(&AdbInstallPackageHandler, device, &install_result) }, |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 369 | { MinadbdCommand::kRebootAndroid, std::bind(&AdbRebootHandler, MinadbdCommand::kRebootAndroid, |
| 370 | &install_result, reboot_action) }, |
| 371 | { MinadbdCommand::kRebootBootloader, |
| 372 | std::bind(&AdbRebootHandler, MinadbdCommand::kRebootBootloader, &install_result, |
| 373 | reboot_action) }, |
| 374 | { MinadbdCommand::kRebootFastboot, std::bind(&AdbRebootHandler, MinadbdCommand::kRebootFastboot, |
| 375 | &install_result, reboot_action) }, |
| 376 | { MinadbdCommand::kRebootRecovery, std::bind(&AdbRebootHandler, MinadbdCommand::kRebootRecovery, |
| 377 | &install_result, reboot_action) }, |
| 378 | { MinadbdCommand::kRebootRescue, |
| 379 | std::bind(&AdbRebootHandler, MinadbdCommand::kRebootRescue, &install_result, reboot_action) }, |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 380 | }; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 381 | |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 382 | if (!rescue_mode) { |
| 383 | ui->Print( |
| 384 | "\n\nNow send the package you want to apply\n" |
| 385 | "to the device with \"adb sideload <filename>\"...\n"); |
| 386 | } else { |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 387 | command_map.emplace(MinadbdCommand::kWipeData, [&device]() { |
Eric Biggers | fde69fb | 2022-03-10 22:13:39 +0000 | [diff] [blame] | 388 | bool result = WipeData(device); |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 389 | return std::make_pair(result, true); |
| 390 | }); |
Tao Bao | 2223e6a | 2019-07-08 18:07:22 -0700 | [diff] [blame] | 391 | command_map.emplace(MinadbdCommand::kNoOp, []() { return std::make_pair(true, true); }); |
| 392 | |
| 393 | ui->Print("\n\nWaiting for rescue commands...\n"); |
xunchang | fedeef6 | 2019-04-22 12:18:14 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Tom Marshall | 8f4e010 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 396 | CreateMinadbdServiceAndExecuteCommands(device, command_map, rescue_mode); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 397 | |
Hridya Valsaraju | e4ef453 | 2018-08-31 11:57:51 -0700 | [diff] [blame] | 398 | // Clean up before switching to the older state, for example setting the state |
| 399 | // to none sets sys/class/android_usb/android0/enable to 0. |
| 400 | if (!SetUsbConfig("none")) { |
| 401 | LOG(ERROR) << "Failed to clear USB config"; |
| 402 | } |
| 403 | |
| 404 | if (usb_state != "none") { |
| 405 | if (!SetUsbConfig(usb_state)) { |
| 406 | LOG(ERROR) << "Failed to set USB config to " << usb_state; |
| 407 | } |
| 408 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 409 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 410 | return install_result; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 411 | } |