blob: 9e605e2d5f169e939eae051ab23023eaf27722e2 [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
17#include <unistd.h>
18#include <dirent.h>
19#include <errno.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/types.h>
23#include <sys/wait.h>
24#include <sys/stat.h>
25#include <signal.h>
26#include <fcntl.h>
27
28#include "ui.h"
29#include "cutils/properties.h"
30#include "install.h"
31#include "common.h"
32#include "adb_install.h"
33extern "C" {
Doug Zongker18a78e02014-07-10 07:31:46 -070034#include "minadbd/fuse_adb_provider.h"
35#include "fuse_sideload.h"
Doug Zongker9270a202012-01-09 15:16:13 -080036}
37
38static RecoveryUI* ui = NULL;
39
40static void
41set_usb_driver(bool enabled) {
42 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
43 if (fd < 0) {
44 ui->Print("failed to open driver control: %s\n", strerror(errno));
45 return;
46 }
47 if (write(fd, enabled ? "1" : "0", 1) < 0) {
48 ui->Print("failed to set driver control: %s\n", strerror(errno));
49 }
50 if (close(fd) < 0) {
51 ui->Print("failed to close driver control: %s\n", strerror(errno));
52 }
53}
54
55static void
56stop_adbd() {
57 property_set("ctl.stop", "adbd");
58 set_usb_driver(false);
59}
60
61
62static void
63maybe_restart_adbd() {
Elliott Hughesf14af802015-02-10 14:46:14 -080064 if (is_ro_debuggable()) {
Doug Zongker9270a202012-01-09 15:16:13 -080065 ui->Print("Restarting adbd...\n");
66 set_usb_driver(true);
67 property_set("ctl.start", "adbd");
68 }
69}
70
Doug Zongker075ad802014-06-26 15:35:51 -070071// How long (in seconds) we wait for the host to start sending us a
72// package, before timing out.
73#define ADB_INSTALL_TIMEOUT 300
74
Doug Zongker9270a202012-01-09 15:16:13 -080075int
Tao Bao145d8612015-03-25 15:51:15 -070076apply_from_adb(RecoveryUI* ui_, bool* wipe_cache, const char* install_file) {
Tao Bao682c34b2015-04-07 17:16:35 -070077 modified_flash = true;
78
Doug Zongker9270a202012-01-09 15:16:13 -080079 ui = ui_;
80
81 stop_adbd();
82 set_usb_driver(true);
83
84 ui->Print("\n\nNow send the package you want to apply\n"
85 "to the device with \"adb sideload <filename>\"...\n");
86
87 pid_t child;
88 if ((child = fork()) == 0) {
89 execl("/sbin/recovery", "recovery", "--adbd", NULL);
90 _exit(-1);
91 }
Doug Zongker075ad802014-06-26 15:35:51 -070092
Doug Zongker18a78e02014-07-10 07:31:46 -070093 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host
Doug Zongker075ad802014-06-26 15:35:51 -070094 // connects and starts serving a package. Poll for its
95 // appearance. (Note that inotify doesn't work with FUSE.)
96 int result;
Doug Zongker9270a202012-01-09 15:16:13 -080097 int status;
Doug Zongker075ad802014-06-26 15:35:51 -070098 bool waited = false;
99 struct stat st;
100 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) {
101 if (waitpid(child, &status, WNOHANG) != 0) {
102 result = INSTALL_ERROR;
103 waited = true;
104 break;
105 }
106
Doug Zongker18a78e02014-07-10 07:31:46 -0700107 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
Doug Zongker075ad802014-06-26 15:35:51 -0700108 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT-1) {
109 sleep(1);
110 continue;
111 } else {
Elliott Hughes018ed312015-04-08 16:51:36 -0700112 ui->Print("\nTimed out waiting for package.\n\n");
Doug Zongker075ad802014-06-26 15:35:51 -0700113 result = INSTALL_ERROR;
114 kill(child, SIGKILL);
115 break;
116 }
117 }
Doug Zongker18a78e02014-07-10 07:31:46 -0700118 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false);
Doug Zongker075ad802014-06-26 15:35:51 -0700119 break;
120 }
121
122 if (!waited) {
123 // Calling stat() on this magic filename signals the minadbd
124 // subprocess to shut down.
Doug Zongker18a78e02014-07-10 07:31:46 -0700125 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
Doug Zongker075ad802014-06-26 15:35:51 -0700126
127 // TODO(dougz): there should be a way to cancel waiting for a
128 // package (by pushing some button combo on the device). For now
129 // you just have to 'adb sideload' a file that's not a valid
130 // package, like "/dev/null".
131 waitpid(child, &status, 0);
132 }
133
Doug Zongker9270a202012-01-09 15:16:13 -0800134 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker075ad802014-06-26 15:35:51 -0700135 if (WEXITSTATUS(status) == 3) {
136 ui->Print("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
137 } else if (!WIFSIGNALED(status)) {
138 ui->Print("\n(adbd status %d)\n", WEXITSTATUS(status));
139 }
Doug Zongker9270a202012-01-09 15:16:13 -0800140 }
141
142 set_usb_driver(false);
143 maybe_restart_adbd();
144
Doug Zongker075ad802014-06-26 15:35:51 -0700145 return result;
Doug Zongker9270a202012-01-09 15:16:13 -0800146}