blob: 9497df5018f92f10934092d4b55bb0ce8eb59143 [file] [log] [blame]
Doug Zongker9270a202012-01-09 15:16:13 -08001/*
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
xunchang24788852019-03-22 16:08:52 -070017#include "install/adb_install.h"
Tao Bao0150d012017-05-01 11:31:28 -070018
Doug Zongker9270a202012-01-09 15:16:13 -080019#include <errno.h>
Tao Bao0150d012017-05-01 11:31:28 -070020#include <fcntl.h>
21#include <signal.h>
Doug Zongker9270a202012-01-09 15:16:13 -080022#include <stdlib.h>
23#include <string.h>
xunchang95d67322019-04-05 16:16:07 -070024#include <sys/epoll.h>
25#include <sys/socket.h>
Tao Bao0150d012017-05-01 11:31:28 -070026#include <sys/stat.h>
Doug Zongker9270a202012-01-09 15:16:13 -080027#include <sys/types.h>
28#include <sys/wait.h>
Tao Bao0150d012017-05-01 11:31:28 -070029#include <unistd.h>
Doug Zongker9270a202012-01-09 15:16:13 -080030
xunchang95d67322019-04-05 16:16:07 -070031#include <atomic>
32#include <functional>
33#include <map>
Tao Bao7b9b7db2019-04-19 15:22:15 -070034#include <utility>
Tao Bao378bfbf2019-04-16 14:22:25 -070035#include <vector>
xunchang95d67322019-04-05 16:16:07 -070036
37#include <android-base/file.h>
Tao Bao0167d4c2017-05-11 14:44:15 -070038#include <android-base/logging.h>
xunchang95d67322019-04-05 16:16:07 -070039#include <android-base/memory.h>
Elliott Hughescb220402016-09-23 15:30:55 -070040#include <android-base/properties.h>
xunchang95d67322019-04-05 16:16:07 -070041#include <android-base/strings.h>
42#include <android-base/unique_fd.h>
Elliott Hughescb220402016-09-23 15:30:55 -070043
Tao Bao0150d012017-05-01 11:31:28 -070044#include "fuse_sideload.h"
xunchang24788852019-03-22 16:08:52 -070045#include "install/install.h"
xunchang5a1916b2019-04-22 12:18:14 -070046#include "install/wipe_data.h"
xunchang95d67322019-04-05 16:16:07 -070047#include "minadbd_types.h"
Tao Bao378bfbf2019-04-16 14:22:25 -070048#include "otautil/sysutil.h"
Tao Bao7b9b7db2019-04-19 15:22:15 -070049#include "recovery_ui/device.h"
Tianjie Xu8f397302018-08-20 13:40:47 -070050#include "recovery_ui/ui.h"
Tao Bao0150d012017-05-01 11:31:28 -070051
Tao Bao7b9b7db2019-04-19 15:22:15 -070052// 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.
55using CommandFunction = std::function<std::pair<bool, bool>()>;
xunchang95d67322019-04-05 16:16:07 -070056
xunchang24788852019-03-22 16:08:52 -070057static 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 Bao7b9b7db2019-04-19 15:22:15 -070062// Parses the minadbd command in |message|; returns MinadbdCommand::kError upon errors.
63static MinadbdCommand ParseMinadbdCommand(const std::string& message) {
xunchang95d67322019-04-05 16:16:07 -070064 if (!android::base::StartsWith(message, kMinadbdCommandPrefix)) {
65 LOG(ERROR) << "Failed to parse command in message " << message;
Tao Bao7b9b7db2019-04-19 15:22:15 -070066 return MinadbdCommand::kError;
xunchang95d67322019-04-05 16:16:07 -070067 }
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 Bao7b9b7db2019-04-19 15:22:15 -070071 if (cmd_code >= static_cast<uint32_t>(MinadbdCommand::kError)) {
xunchang95d67322019-04-05 16:16:07 -070072 LOG(ERROR) << "Unsupported command code: " << cmd_code;
Tao Bao7b9b7db2019-04-19 15:22:15 -070073 return MinadbdCommand::kError;
xunchang95d67322019-04-05 16:16:07 -070074 }
75
Tao Bao7b9b7db2019-04-19 15:22:15 -070076 return static_cast<MinadbdCommand>(cmd_code);
xunchang95d67322019-04-05 16:16:07 -070077}
78
79static 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 Bao7b9b7db2019-04-19 15:22:15 -070091// Installs the package from FUSE. Returns the installation result and whether it should continue
92// waiting for new commands.
93static auto AdbInstallPackageHandler(RecoveryUI* ui, int* result) {
xunchang95d67322019-04-05 16:16:07 -070094 // 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.)
98 constexpr int ADB_INSTALL_TIMEOUT = 15;
Tao Bao7b9b7db2019-04-19 15:22:15 -070099 bool should_continue = true;
xunchang95d67322019-04-05 16:16:07 -0700100 *result = INSTALL_ERROR;
101 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) {
102 struct stat st;
103 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
104 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT - 1) {
105 sleep(1);
106 continue;
107 } else {
Tao Bao7b9b7db2019-04-19 15:22:15 -0700108 should_continue = false;
xunchang95d67322019-04-05 16:16:07 -0700109 ui->Print("\nTimed out waiting for fuse to be ready.\n\n");
110 break;
111 }
112 }
xunchang388d2532019-04-12 16:22:15 -0700113 *result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, false, false, 0, ui);
xunchang95d67322019-04-05 16:16:07 -0700114 break;
115 }
116
117 // Calling stat() on this magic filename signals the FUSE to exit.
118 struct stat st;
119 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
Tao Bao7b9b7db2019-04-19 15:22:15 -0700120 return std::make_pair(*result == INSTALL_SUCCESS, should_continue);
xunchang95d67322019-04-05 16:16:07 -0700121}
122
Tao Bao7b9b7db2019-04-19 15:22:15 -0700123static auto AdbRebootHandler(MinadbdCommand command, int* result,
124 Device::BuiltinAction* reboot_action) {
Tao Bao75321ad2019-04-23 11:46:25 -0700125 // Use Device::REBOOT_{FASTBOOT,RECOVERY,RESCUE}, instead of the ones with ENTER_. This allows
126 // rebooting back into fastboot/recovery/rescue mode through bootloader, which may use a newly
127 // installed bootloader/recovery image.
Tao Bao7b9b7db2019-04-19 15:22:15 -0700128 switch (command) {
129 case MinadbdCommand::kRebootBootloader:
130 *reboot_action = Device::REBOOT_BOOTLOADER;
131 break;
132 case MinadbdCommand::kRebootFastboot:
Tao Bao75321ad2019-04-23 11:46:25 -0700133 *reboot_action = Device::REBOOT_FASTBOOT;
Tao Bao7b9b7db2019-04-19 15:22:15 -0700134 break;
135 case MinadbdCommand::kRebootRecovery:
Tao Bao75321ad2019-04-23 11:46:25 -0700136 *reboot_action = Device::REBOOT_RECOVERY;
Tao Bao7b9b7db2019-04-19 15:22:15 -0700137 break;
138 case MinadbdCommand::kRebootRescue:
Tao Bao7b9b7db2019-04-19 15:22:15 -0700139 *reboot_action = Device::REBOOT_RESCUE;
140 break;
141 case MinadbdCommand::kRebootAndroid:
142 default:
143 *reboot_action = Device::REBOOT;
144 break;
145 }
146 *result = INSTALL_REBOOT;
147 return std::make_pair(true, false);
148}
149
150// Parses and executes the command from minadbd. Returns whether the caller should keep waiting for
151// next command.
152static bool HandleMessageFromMinadbd(int socket_fd,
153 const std::map<MinadbdCommand, CommandFunction>& command_map) {
xunchang95d67322019-04-05 16:16:07 -0700154 char buffer[kMinadbdMessageSize];
155 if (!android::base::ReadFully(socket_fd, buffer, kMinadbdMessageSize)) {
156 PLOG(ERROR) << "Failed to read message from minadbd";
157 return false;
158 }
159
160 std::string message(buffer, buffer + kMinadbdMessageSize);
Tao Bao7b9b7db2019-04-19 15:22:15 -0700161 auto command_type = ParseMinadbdCommand(message);
162 if (command_type == MinadbdCommand::kError) {
xunchang95d67322019-04-05 16:16:07 -0700163 return false;
164 }
165 if (command_map.find(command_type) == command_map.end()) {
166 LOG(ERROR) << "Unsupported command: "
167 << android::base::get_unaligned<unsigned int>(
168 message.substr(strlen(kMinadbdCommandPrefix)).c_str());
169 return false;
170 }
171
172 // We have received a valid command, execute the corresponding function.
173 const auto& command_func = command_map.at(command_type);
Tao Bao7b9b7db2019-04-19 15:22:15 -0700174 const auto [result, should_continue] = command_func();
175 LOG(INFO) << "Command " << static_cast<uint32_t>(command_type) << " finished with " << result;
176 if (!WriteStatusToFd(result ? MinadbdCommandStatus::kSuccess : MinadbdCommandStatus::kFailure,
177 socket_fd)) {
178 return false;
xunchang95d67322019-04-05 16:16:07 -0700179 }
Tao Bao7b9b7db2019-04-19 15:22:15 -0700180 return should_continue;
xunchang95d67322019-04-05 16:16:07 -0700181}
182
183// TODO(xunchang) add a wrapper function and kill the minadbd service there.
184static void ListenAndExecuteMinadbdCommands(
Tao Bao75321ad2019-04-23 11:46:25 -0700185 RecoveryUI* ui, pid_t minadbd_pid, android::base::unique_fd&& socket_fd,
Tao Bao7b9b7db2019-04-19 15:22:15 -0700186 const std::map<MinadbdCommand, CommandFunction>& command_map) {
xunchang95d67322019-04-05 16:16:07 -0700187 android::base::unique_fd epoll_fd(epoll_create1(O_CLOEXEC));
188 if (epoll_fd == -1) {
189 PLOG(ERROR) << "Failed to create epoll";
190 kill(minadbd_pid, SIGKILL);
191 return;
192 }
193
194 constexpr int EPOLL_MAX_EVENTS = 10;
195 struct epoll_event ev = {};
196 ev.events = EPOLLIN | EPOLLHUP;
197 ev.data.fd = socket_fd.get();
198 struct epoll_event events[EPOLL_MAX_EVENTS];
199 if (epoll_ctl(epoll_fd.get(), EPOLL_CTL_ADD, socket_fd.get(), &ev) == -1) {
200 PLOG(ERROR) << "Failed to add socket fd to epoll";
201 kill(minadbd_pid, SIGKILL);
202 return;
203 }
204
205 // Set the timeout to be 300s when waiting for minadbd commands.
206 constexpr int TIMEOUT_MILLIS = 300 * 1000;
207 while (true) {
Tao Bao75321ad2019-04-23 11:46:25 -0700208 // Reset the progress bar and the background image before each command.
209 ui->SetProgressType(RecoveryUI::EMPTY);
210 ui->SetBackground(RecoveryUI::NO_COMMAND);
211
xunchang95d67322019-04-05 16:16:07 -0700212 // Poll for the status change of the socket_fd, and handle the message if the fd is ready to
213 // read.
214 int event_count =
215 TEMP_FAILURE_RETRY(epoll_wait(epoll_fd.get(), events, EPOLL_MAX_EVENTS, TIMEOUT_MILLIS));
216 if (event_count == -1) {
217 PLOG(ERROR) << "Failed to wait for epoll events";
218 kill(minadbd_pid, SIGKILL);
219 return;
220 }
221 if (event_count == 0) {
222 LOG(ERROR) << "Timeout waiting for messages from minadbd";
223 kill(minadbd_pid, SIGKILL);
224 return;
225 }
226
227 for (int n = 0; n < event_count; n++) {
228 if (events[n].events & EPOLLHUP) {
229 LOG(INFO) << "Socket has been closed";
230 kill(minadbd_pid, SIGKILL);
231 return;
232 }
233 if (!HandleMessageFromMinadbd(socket_fd.get(), command_map)) {
234 kill(minadbd_pid, SIGKILL);
235 return;
236 }
237 }
238 }
239}
240
241// Recovery starts minadbd service as a child process, and spawns another thread to listen for the
242// message from minadbd through a socket pair. Here is an example to execute one command from adb
243// host.
244// a. recovery b. listener thread c. minadbd service
245//
246// a1. create socket pair
247// a2. fork minadbd service
248// c3. wait for the adb commands
249// from host
250// c4. after receiving host commands:
251// 1) set up pre-condition (i.e.
252// start fuse for adb sideload)
253// 2) issue command through
254// socket.
255// 3) wait for result
256// a5. start listener thread
257// b6. listen for message from
258// minadbd in a loop.
259// b7. After receiving a minadbd
260// command from socket
261// 1) execute the command function
262// 2) send the result back to
263// minadbd
264// ......
265// c8. exit upon receiving the
266// result
267// a9. wait for listener thread
268// to exit.
269//
270// a10. wait for minadbd to
271// exit
272// b11. exit the listening loop
273//
274static void CreateMinadbdServiceAndExecuteCommands(
Tao Bao75321ad2019-04-23 11:46:25 -0700275 RecoveryUI* ui, const std::map<MinadbdCommand, CommandFunction>& command_map,
276 bool rescue_mode) {
xunchang95d67322019-04-05 16:16:07 -0700277 signal(SIGPIPE, SIG_IGN);
278
279 android::base::unique_fd recovery_socket;
280 android::base::unique_fd minadbd_socket;
281 if (!android::base::Socketpair(AF_UNIX, SOCK_STREAM, 0, &recovery_socket, &minadbd_socket)) {
282 PLOG(ERROR) << "Failed to create socket";
283 return;
284 }
285
286 pid_t child = fork();
287 if (child == -1) {
288 PLOG(ERROR) << "Failed to fork child process";
289 return;
290 }
291 if (child == 0) {
292 recovery_socket.reset();
Tao Bao378bfbf2019-04-16 14:22:25 -0700293 std::vector<std::string> minadbd_commands = {
294 "/system/bin/minadbd",
295 "--socket_fd",
296 std::to_string(minadbd_socket.release()),
297 };
298 if (rescue_mode) {
299 minadbd_commands.push_back("--rescue");
300 }
301 auto exec_args = StringVectorToNullTerminatedArray(minadbd_commands);
302 execv(exec_args[0], exec_args.data());
xunchang95d67322019-04-05 16:16:07 -0700303 _exit(EXIT_FAILURE);
304 }
305
306 minadbd_socket.reset();
307
308 // We need to call SetUsbConfig() after forking minadbd service. Because the function waits for
309 // the usb state to be updated, which depends on sys.usb.ffs.ready=1 set in the adb daemon.
310 if (!SetUsbConfig("sideload")) {
311 LOG(ERROR) << "Failed to set usb config to sideload";
312 return;
313 }
314
Tao Bao75321ad2019-04-23 11:46:25 -0700315 std::thread listener_thread(ListenAndExecuteMinadbdCommands, ui, child,
316 std::move(recovery_socket), std::ref(command_map));
xunchang95d67322019-04-05 16:16:07 -0700317 if (listener_thread.joinable()) {
318 listener_thread.join();
319 }
320
321 int status;
322 waitpid(child, &status, 0);
323 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
324 if (WEXITSTATUS(status) == MinadbdErrorCode::kMinadbdAdbVersionError) {
325 LOG(ERROR) << "\nYou need adb 1.0.32 or newer to sideload\nto this device.\n";
326 } else if (!WIFSIGNALED(status)) {
327 LOG(ERROR) << "\n(adbd status " << WEXITSTATUS(status) << ")";
328 }
329 }
330
331 signal(SIGPIPE, SIG_DFL);
332}
333
xunchang5a1916b2019-04-22 12:18:14 -0700334int ApplyFromAdb(Device* device, bool rescue_mode, Device::BuiltinAction* reboot_action) {
Hridya Valsarajue4ef4532018-08-31 11:57:51 -0700335 // Save the usb state to restore after the sideload operation.
336 std::string usb_state = android::base::GetProperty("sys.usb.state", "none");
337 // Clean up state and stop adbd.
338 if (usb_state != "none" && !SetUsbConfig("none")) {
339 LOG(ERROR) << "Failed to clear USB config";
340 return INSTALL_ERROR;
341 }
Tao Bao682c34b2015-04-07 17:16:35 -0700342
xunchang5a1916b2019-04-22 12:18:14 -0700343 RecoveryUI* ui = device->GetUI();
Doug Zongker9270a202012-01-09 15:16:13 -0800344
xunchang95d67322019-04-05 16:16:07 -0700345 int install_result = INSTALL_ERROR;
Tao Bao7b9b7db2019-04-19 15:22:15 -0700346 std::map<MinadbdCommand, CommandFunction> command_map{
347 { MinadbdCommand::kInstall, std::bind(&AdbInstallPackageHandler, ui, &install_result) },
348 { MinadbdCommand::kRebootAndroid, std::bind(&AdbRebootHandler, MinadbdCommand::kRebootAndroid,
349 &install_result, reboot_action) },
350 { MinadbdCommand::kRebootBootloader,
351 std::bind(&AdbRebootHandler, MinadbdCommand::kRebootBootloader, &install_result,
352 reboot_action) },
353 { MinadbdCommand::kRebootFastboot, std::bind(&AdbRebootHandler, MinadbdCommand::kRebootFastboot,
354 &install_result, reboot_action) },
355 { MinadbdCommand::kRebootRecovery, std::bind(&AdbRebootHandler, MinadbdCommand::kRebootRecovery,
356 &install_result, reboot_action) },
357 { MinadbdCommand::kRebootRescue,
358 std::bind(&AdbRebootHandler, MinadbdCommand::kRebootRescue, &install_result, reboot_action) },
xunchang95d67322019-04-05 16:16:07 -0700359 };
Doug Zongker9270a202012-01-09 15:16:13 -0800360
xunchang5a1916b2019-04-22 12:18:14 -0700361 if (!rescue_mode) {
362 ui->Print(
363 "\n\nNow send the package you want to apply\n"
364 "to the device with \"adb sideload <filename>\"...\n");
365 } else {
xunchang5a1916b2019-04-22 12:18:14 -0700366 command_map.emplace(MinadbdCommand::kWipeData, [&device]() {
367 bool result = WipeData(device, false);
368 return std::make_pair(result, true);
369 });
Tao Bao793e8942019-07-08 18:07:22 -0700370 command_map.emplace(MinadbdCommand::kNoOp, []() { return std::make_pair(true, true); });
371
372 ui->Print("\n\nWaiting for rescue commands...\n");
xunchang5a1916b2019-04-22 12:18:14 -0700373 }
374
Tao Bao75321ad2019-04-23 11:46:25 -0700375 CreateMinadbdServiceAndExecuteCommands(ui, command_map, rescue_mode);
Doug Zongker075ad802014-06-26 15:35:51 -0700376
Hridya Valsarajue4ef4532018-08-31 11:57:51 -0700377 // Clean up before switching to the older state, for example setting the state
378 // to none sets sys/class/android_usb/android0/enable to 0.
379 if (!SetUsbConfig("none")) {
380 LOG(ERROR) << "Failed to clear USB config";
381 }
382
383 if (usb_state != "none") {
384 if (!SetUsbConfig(usb_state)) {
385 LOG(ERROR) << "Failed to set USB config to " << usb_state;
386 }
387 }
Doug Zongker9270a202012-01-09 15:16:13 -0800388
xunchang95d67322019-04-05 16:16:07 -0700389 return install_result;
Doug Zongker9270a202012-01-09 15:16:13 -0800390}