blob: e7d7758f9337f86c047c2587c0f92e732b893b1e [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
Elliott Hughescb220402016-09-23 15:30:55 -070029#include <android-base/properties.h>
30
Tao Bao0150d012017-05-01 11:31:28 -070031#include "common.h"
32#include "fuse_sideload.h"
33#include "install.h"
34#include "ui.h"
35
36static void set_usb_driver(bool enabled) {
37 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
38 if (fd < 0) {
39 ui->Print("failed to open driver control: %s\n", strerror(errno));
40 return;
41 }
42 if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) {
43 ui->Print("failed to set driver control: %s\n", strerror(errno));
44 }
45 if (close(fd) < 0) {
46 ui->Print("failed to close driver control: %s\n", strerror(errno));
47 }
Doug Zongker9270a202012-01-09 15:16:13 -080048}
49
Tao Bao0150d012017-05-01 11:31:28 -070050static void stop_adbd() {
51 ui->Print("Stopping adbd...\n");
52 android::base::SetProperty("ctl.stop", "adbd");
53 set_usb_driver(false);
Doug Zongker9270a202012-01-09 15:16:13 -080054}
55
Tao Bao0150d012017-05-01 11:31:28 -070056static void maybe_restart_adbd() {
57 if (is_ro_debuggable()) {
58 ui->Print("Restarting adbd...\n");
59 set_usb_driver(true);
60 android::base::SetProperty("ctl.start", "adbd");
61 }
Doug Zongker9270a202012-01-09 15:16:13 -080062}
63
Tao Bao0150d012017-05-01 11:31:28 -070064int apply_from_adb(bool* wipe_cache, const char* install_file) {
65 modified_flash = true;
Doug Zongker075ad802014-06-26 15:35:51 -070066
Tao Bao0150d012017-05-01 11:31:28 -070067 stop_adbd();
68 set_usb_driver(true);
Tao Bao682c34b2015-04-07 17:16:35 -070069
Tao Bao0150d012017-05-01 11:31:28 -070070 ui->Print(
71 "\n\nNow send the package you want to apply\n"
72 "to the device with \"adb sideload <filename>\"...\n");
Doug Zongker9270a202012-01-09 15:16:13 -080073
Tao Bao0150d012017-05-01 11:31:28 -070074 pid_t child;
75 if ((child = fork()) == 0) {
76 execl("/sbin/recovery", "recovery", "--adbd", nullptr);
77 _exit(EXIT_FAILURE);
78 }
Doug Zongker9270a202012-01-09 15:16:13 -080079
Tao Bao0150d012017-05-01 11:31:28 -070080 // How long (in seconds) we wait for the host to start sending us a package, before timing out.
81 static constexpr int ADB_INSTALL_TIMEOUT = 300;
82
83 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host connects and starts serving a
84 // package. Poll for its appearance. (Note that inotify doesn't work with FUSE.)
85 int result = INSTALL_ERROR;
86 int status;
87 bool waited = false;
88 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) {
89 if (waitpid(child, &status, WNOHANG) != 0) {
90 result = INSTALL_ERROR;
91 waited = true;
92 break;
Doug Zongker9270a202012-01-09 15:16:13 -080093 }
Doug Zongker075ad802014-06-26 15:35:51 -070094
Doug Zongker075ad802014-06-26 15:35:51 -070095 struct stat st;
Tao Bao0150d012017-05-01 11:31:28 -070096 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
97 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT - 1) {
98 sleep(1);
99 continue;
100 } else {
101 ui->Print("\nTimed out waiting for package.\n\n");
102 result = INSTALL_ERROR;
103 kill(child, SIGKILL);
Doug Zongker075ad802014-06-26 15:35:51 -0700104 break;
Tao Bao0150d012017-05-01 11:31:28 -0700105 }
Doug Zongker075ad802014-06-26 15:35:51 -0700106 }
Tao Bao0150d012017-05-01 11:31:28 -0700107 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false, 0);
108 break;
109 }
Doug Zongker075ad802014-06-26 15:35:51 -0700110
Tao Bao0150d012017-05-01 11:31:28 -0700111 if (!waited) {
112 // Calling stat() on this magic filename signals the minadbd subprocess to shut down.
113 struct stat st;
114 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
Doug Zongker075ad802014-06-26 15:35:51 -0700115
Tao Bao0150d012017-05-01 11:31:28 -0700116 // TODO: there should be a way to cancel waiting for a package (by pushing some button combo on
117 // the device). For now you just have to 'adb sideload' a file that's not a valid package, like
118 // "/dev/null".
119 waitpid(child, &status, 0);
120 }
121
122 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
123 if (WEXITSTATUS(status) == 3) {
124 ui->Print("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
125 } else if (!WIFSIGNALED(status)) {
126 ui->Print("\n(adbd status %d)\n", WEXITSTATUS(status));
Doug Zongker075ad802014-06-26 15:35:51 -0700127 }
Tao Bao0150d012017-05-01 11:31:28 -0700128 }
Doug Zongker075ad802014-06-26 15:35:51 -0700129
Tao Bao0150d012017-05-01 11:31:28 -0700130 set_usb_driver(false);
131 maybe_restart_adbd();
Doug Zongker9270a202012-01-09 15:16:13 -0800132
Tao Bao0150d012017-05-01 11:31:28 -0700133 return result;
Doug Zongker9270a202012-01-09 15:16:13 -0800134}