blob: ac0130651ba1769df62747c4426263ae92dd7e40 [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
Tao Bao0150d012017-05-01 11:31:28 -070017#include "adb_install.h"
18
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>
Tao Bao0150d012017-05-01 11:31:28 -070024#include <sys/stat.h>
Doug Zongker9270a202012-01-09 15:16:13 -080025#include <sys/types.h>
26#include <sys/wait.h>
Tao Bao0150d012017-05-01 11:31:28 -070027#include <unistd.h>
Doug Zongker9270a202012-01-09 15:16:13 -080028
Tao Bao0167d4c2017-05-11 14:44:15 -070029#include <android-base/file.h>
30#include <android-base/logging.h>
Elliott Hughescb220402016-09-23 15:30:55 -070031#include <android-base/properties.h>
Tao Bao0167d4c2017-05-11 14:44:15 -070032#include <android-base/unique_fd.h>
Elliott Hughescb220402016-09-23 15:30:55 -070033
Tao Bao0150d012017-05-01 11:31:28 -070034#include "common.h"
35#include "fuse_sideload.h"
36#include "install.h"
37#include "ui.h"
38
39static void set_usb_driver(bool enabled) {
Tao Bao0167d4c2017-05-11 14:44:15 -070040 // USB configfs doesn't use /s/c/a/a/enable.
41 if (android::base::GetBoolProperty("sys.usb.configfs", false)) {
Tao Bao0150d012017-05-01 11:31:28 -070042 return;
43 }
Tao Bao0167d4c2017-05-11 14:44:15 -070044
45 static constexpr const char* USB_DRIVER_CONTROL = "/sys/class/android_usb/android0/enable";
46 android::base::unique_fd fd(open(USB_DRIVER_CONTROL, O_WRONLY));
47 if (fd == -1) {
48 PLOG(ERROR) << "Failed to open driver control";
49 return;
Tao Bao0150d012017-05-01 11:31:28 -070050 }
Tao Bao0167d4c2017-05-11 14:44:15 -070051 // Not using android::base::WriteStringToFile since that will open with O_CREAT and give EPERM
52 // when USB_DRIVER_CONTROL doesn't exist. When it gives EPERM, we don't know whether that's due
53 // to non-existent USB_DRIVER_CONTROL or indeed a permission issue.
54 if (!android::base::WriteStringToFd(enabled ? "1" : "0", fd)) {
55 PLOG(ERROR) << "Failed to set driver control";
Tao Bao0150d012017-05-01 11:31:28 -070056 }
Doug Zongker9270a202012-01-09 15:16:13 -080057}
58
Tao Bao0150d012017-05-01 11:31:28 -070059static void stop_adbd() {
60 ui->Print("Stopping adbd...\n");
61 android::base::SetProperty("ctl.stop", "adbd");
62 set_usb_driver(false);
Doug Zongker9270a202012-01-09 15:16:13 -080063}
64
Tao Bao0150d012017-05-01 11:31:28 -070065static void maybe_restart_adbd() {
66 if (is_ro_debuggable()) {
67 ui->Print("Restarting adbd...\n");
68 set_usb_driver(true);
69 android::base::SetProperty("ctl.start", "adbd");
70 }
Doug Zongker9270a202012-01-09 15:16:13 -080071}
72
Tao Bao0150d012017-05-01 11:31:28 -070073int apply_from_adb(bool* wipe_cache, const char* install_file) {
74 modified_flash = true;
Doug Zongker075ad802014-06-26 15:35:51 -070075
Tao Bao0150d012017-05-01 11:31:28 -070076 stop_adbd();
77 set_usb_driver(true);
Tao Bao682c34b2015-04-07 17:16:35 -070078
Tao Bao0150d012017-05-01 11:31:28 -070079 ui->Print(
80 "\n\nNow send the package you want to apply\n"
81 "to the device with \"adb sideload <filename>\"...\n");
Doug Zongker9270a202012-01-09 15:16:13 -080082
Tao Bao0150d012017-05-01 11:31:28 -070083 pid_t child;
84 if ((child = fork()) == 0) {
85 execl("/sbin/recovery", "recovery", "--adbd", nullptr);
86 _exit(EXIT_FAILURE);
87 }
Doug Zongker9270a202012-01-09 15:16:13 -080088
Tao Bao0150d012017-05-01 11:31:28 -070089 // How long (in seconds) we wait for the host to start sending us a package, before timing out.
90 static constexpr int ADB_INSTALL_TIMEOUT = 300;
91
92 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host connects and starts serving a
93 // package. Poll for its appearance. (Note that inotify doesn't work with FUSE.)
94 int result = INSTALL_ERROR;
95 int status;
96 bool waited = false;
97 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) {
98 if (waitpid(child, &status, WNOHANG) != 0) {
99 result = INSTALL_ERROR;
100 waited = true;
101 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800102 }
Doug Zongker075ad802014-06-26 15:35:51 -0700103
Doug Zongker075ad802014-06-26 15:35:51 -0700104 struct stat st;
Tao Bao0150d012017-05-01 11:31:28 -0700105 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
106 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT - 1) {
107 sleep(1);
108 continue;
109 } else {
110 ui->Print("\nTimed out waiting for package.\n\n");
111 result = INSTALL_ERROR;
112 kill(child, SIGKILL);
Doug Zongker075ad802014-06-26 15:35:51 -0700113 break;
Tao Bao0150d012017-05-01 11:31:28 -0700114 }
Doug Zongker075ad802014-06-26 15:35:51 -0700115 }
Tao Bao0150d012017-05-01 11:31:28 -0700116 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false, 0);
117 break;
118 }
Doug Zongker075ad802014-06-26 15:35:51 -0700119
Tao Bao0150d012017-05-01 11:31:28 -0700120 if (!waited) {
121 // Calling stat() on this magic filename signals the minadbd subprocess to shut down.
122 struct stat st;
123 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
Doug Zongker075ad802014-06-26 15:35:51 -0700124
Tao Bao0150d012017-05-01 11:31:28 -0700125 // TODO: there should be a way to cancel waiting for a package (by pushing some button combo on
126 // the device). For now you just have to 'adb sideload' a file that's not a valid package, like
127 // "/dev/null".
128 waitpid(child, &status, 0);
129 }
130
131 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
132 if (WEXITSTATUS(status) == 3) {
133 ui->Print("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
134 } else if (!WIFSIGNALED(status)) {
135 ui->Print("\n(adbd status %d)\n", WEXITSTATUS(status));
Doug Zongker075ad802014-06-26 15:35:51 -0700136 }
Tao Bao0150d012017-05-01 11:31:28 -0700137 }
Doug Zongker075ad802014-06-26 15:35:51 -0700138
Tao Bao0150d012017-05-01 11:31:28 -0700139 set_usb_driver(false);
140 maybe_restart_adbd();
Doug Zongker9270a202012-01-09 15:16:13 -0800141
Tao Bao0150d012017-05-01 11:31:28 -0700142 return result;
Doug Zongker9270a202012-01-09 15:16:13 -0800143}