blob: 7b0388279832829a725f44e022f7497da017118f [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>
Dees_Troy2673cec2013-04-02 20:22:16 +000027#include <stdio.h>
Doug Zongker9270a202012-01-09 15:16:13 -080028
29#include "ui.h"
30#include "cutils/properties.h"
31#include "install.h"
32#include "common.h"
33#include "adb_install.h"
Doug Zongker18a78e02014-07-10 07:31:46 -070034#include "minadbd/fuse_adb_provider.h"
35#include "fuse_sideload.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050036#ifdef USE_OLD_VERIFIER
Ethan Yonker4bf259f2016-08-29 11:50:34 -050037#include "verifier24/verifier.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050038#else
Ethan Yonker34ae4832016-08-24 15:32:18 -050039#include "verifier.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050040#endif
Doug Zongker9270a202012-01-09 15:16:13 -080041
Ethan Yonker8373cfe2017-09-08 06:50:54 -050042static void set_usb_driver(bool enabled) {
Doug Zongker9270a202012-01-09 15:16:13 -080043 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
44 if (fd < 0) {
Dees_Troye00c83a2012-09-21 10:00:52 -040045/* These error messages show when built in older Android branches (e.g. Gingerbread)
46 It's not a critical error so we're disabling the error messages.
Doug Zongker9270a202012-01-09 15:16:13 -080047 ui->Print("failed to open driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040048*/
Dees_Troy2673cec2013-04-02 20:22:16 +000049 printf("failed to open driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080050 return;
51 }
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050052
Elliott Hughes2f5feed2015-04-28 17:24:24 -070053 if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) {
Dees_Troye00c83a2012-09-21 10:00:52 -040054/*
Doug Zongker9270a202012-01-09 15:16:13 -080055 ui->Print("failed to set driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040056*/
Dees_Troy2673cec2013-04-02 20:22:16 +000057 printf("failed to set driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080058 }
59 if (close(fd) < 0) {
Dees_Troye00c83a2012-09-21 10:00:52 -040060/*
Doug Zongker9270a202012-01-09 15:16:13 -080061 ui->Print("failed to close driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040062*/
Dees_Troy2673cec2013-04-02 20:22:16 +000063 printf("failed to close driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080064 }
65}
66
Ethan Yonkerfefe5912017-09-30 22:22:13 -050067// On Android 8.0 for some reason init can't seem to completely stop adbd
68// so we have to kill it too if it doesn't die on its own.
69static void kill_adbd() {
70 DIR* dir = opendir("/proc");
71 if (dir) {
72 struct dirent* de = 0;
73
74 while ((de = readdir(dir)) != 0) {
75 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
76 continue;
77
78 int pid = -1;
79 int ret = sscanf(de->d_name, "%d", &pid);
80
81 if (ret == 1) {
82 char cmdpath[PATH_MAX];
83 sprintf(cmdpath, "/proc/%d/cmdline", pid);
84
85 FILE* file = fopen(cmdpath, "r");
86 size_t task_size = PATH_MAX;
87 char task[PATH_MAX];
88 char* p = task;
89 if (getline(&p, &task_size, file) > 0) {
90 if (strstr(task, "adbd") != 0) {
91 printf("adbd pid %d found, sending kill.\n", pid);
92 kill(pid, SIGINT);
93 usleep(5000);
94 kill(pid, SIGKILL);
95 }
96 }
97 fclose(file);
98 }
99 }
100 closedir(dir);
101 }
102}
103
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500104static void stop_adbd() {
105 printf("Stopping adbd...\n");
Doug Zongker9270a202012-01-09 15:16:13 -0800106 property_set("ctl.stop", "adbd");
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500107 usleep(5000);
108 kill_adbd();
Doug Zongker9270a202012-01-09 15:16:13 -0800109 set_usb_driver(false);
110}
111
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500112static bool is_ro_debuggable() {
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500113 char value[PROPERTY_VALUE_MAX+1];
114 return (property_get("ro.debuggable", value, NULL) == 1 && value[0] == '1');
115}
Doug Zongker9270a202012-01-09 15:16:13 -0800116
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500117static void maybe_restart_adbd() {
Elliott Hughesf14af802015-02-10 14:46:14 -0800118 if (is_ro_debuggable()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000119 printf("Restarting adbd...\n");
Doug Zongker9270a202012-01-09 15:16:13 -0800120 set_usb_driver(true);
121 property_set("ctl.start", "adbd");
122 }
123}
124
Doug Zongker075ad802014-06-26 15:35:51 -0700125// How long (in seconds) we wait for the host to start sending us a
126// package, before timing out.
127#define ADB_INSTALL_TIMEOUT 300
128
Doug Zongker9270a202012-01-09 15:16:13 -0800129int
thatcc8ddca2015-01-03 01:59:36 +0100130apply_from_adb(const char* install_file, pid_t* child_pid) {
Doug Zongker9270a202012-01-09 15:16:13 -0800131
132 stop_adbd();
133 set_usb_driver(true);
Dees_Troy2673cec2013-04-02 20:22:16 +0000134/*
Elliott Hughes59bf0da2016-06-15 15:14:04 -0700135int apply_from_adb(RecoveryUI* ui, bool* wipe_cache, const char* install_file) {
Tao Bao682c34b2015-04-07 17:16:35 -0700136 modified_flash = true;
137
Elliott Hughes59bf0da2016-06-15 15:14:04 -0700138 stop_adbd(ui);
139 set_usb_driver(ui, true);
Doug Zongker9270a202012-01-09 15:16:13 -0800140
141 ui->Print("\n\nNow send the package you want to apply\n"
142 "to the device with \"adb sideload <filename>\"...\n");
Dees_Troy2673cec2013-04-02 20:22:16 +0000143*/
Doug Zongker9270a202012-01-09 15:16:13 -0800144 pid_t child;
145 if ((child = fork()) == 0) {
Dees_Troy9a4b5692012-09-19 15:09:45 -0400146 execl("/sbin/recovery", "recovery", "--adbd", install_file, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800147 _exit(-1);
148 }
Doug Zongker075ad802014-06-26 15:35:51 -0700149
thatcc8ddca2015-01-03 01:59:36 +0100150 *child_pid = child;
151 // caller can now kill the child thread from another thread
Ethan Yonkera1674162014-11-06 08:35:10 -0600152
Doug Zongker18a78e02014-07-10 07:31:46 -0700153 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host
Doug Zongker075ad802014-06-26 15:35:51 -0700154 // connects and starts serving a package. Poll for its
155 // appearance. (Note that inotify doesn't work with FUSE.)
Tao Bao80e46e02015-06-03 10:49:29 -0700156 int result = INSTALL_ERROR;
Doug Zongker9270a202012-01-09 15:16:13 -0800157 int status;
Doug Zongker075ad802014-06-26 15:35:51 -0700158 bool waited = false;
159 struct stat st;
160 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) {
161 if (waitpid(child, &status, WNOHANG) != 0) {
Ethan Yonker26860092014-11-06 09:49:25 -0600162 result = -1;
Doug Zongker075ad802014-06-26 15:35:51 -0700163 waited = true;
164 break;
165 }
166
Doug Zongker18a78e02014-07-10 07:31:46 -0700167 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
Doug Zongker075ad802014-06-26 15:35:51 -0700168 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT-1) {
169 sleep(1);
170 continue;
171 } else {
thatcc8ddca2015-01-03 01:59:36 +0100172 printf("\nTimed out waiting for package: %s\n\n", strerror(errno));
Ethan Yonker26860092014-11-06 09:49:25 -0600173 result = -1;
Doug Zongker075ad802014-06-26 15:35:51 -0700174 kill(child, SIGKILL);
175 break;
176 }
177 }
Ethan Yonker24813422014-11-07 17:19:07 -0600178 // Install is handled elsewhere in TWRP
thatcc8ddca2015-01-03 01:59:36 +0100179 //install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false);
180 return 0;
Doug Zongker075ad802014-06-26 15:35:51 -0700181 }
182
thatcc8ddca2015-01-03 01:59:36 +0100183 // if we got here, something failed
184 *child_pid = 0;
185
186 if (!waited) {
Doug Zongker075ad802014-06-26 15:35:51 -0700187 // Calling stat() on this magic filename signals the minadbd
188 // subprocess to shut down.
Doug Zongker18a78e02014-07-10 07:31:46 -0700189 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
Doug Zongker075ad802014-06-26 15:35:51 -0700190
191 // TODO(dougz): there should be a way to cancel waiting for a
192 // package (by pushing some button combo on the device). For now
193 // you just have to 'adb sideload' a file that's not a valid
194 // package, like "/dev/null".
195 waitpid(child, &status, 0);
thatcc8ddca2015-01-03 01:59:36 +0100196 }
Doug Zongker075ad802014-06-26 15:35:51 -0700197
thatcc8ddca2015-01-03 01:59:36 +0100198 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
199 if (WEXITSTATUS(status) == 3) {
200 printf("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
201 result = -2;
202 } else if (!WIFSIGNALED(status)) {
203 printf("status %d\n", WEXITSTATUS(status));
Doug Zongker075ad802014-06-26 15:35:51 -0700204 }
thatcc8ddca2015-01-03 01:59:36 +0100205 }
Doug Zongker9270a202012-01-09 15:16:13 -0800206
thatcc8ddca2015-01-03 01:59:36 +0100207 set_usb_driver(false);
208 maybe_restart_adbd();
209
210 return result;
Doug Zongker9270a202012-01-09 15:16:13 -0800211}