blob: 291708c69c04cb5c892365625442fd65ae0b3d88 [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 <dirent.h>
20#include <errno.h>
Tao Bao0150d012017-05-01 11:31:28 -070021#include <fcntl.h>
22#include <signal.h>
Doug Zongker9270a202012-01-09 15:16:13 -080023#include <stdlib.h>
24#include <string.h>
Tao Bao0150d012017-05-01 11:31:28 -070025#include <sys/stat.h>
Doug Zongker9270a202012-01-09 15:16:13 -080026#include <sys/types.h>
27#include <sys/wait.h>
28#include <sys/stat.h>
29#include <signal.h>
30#include <fcntl.h>
Dees_Troy2673cec2013-04-02 20:22:16 +000031#include <stdio.h>
Tao Bao0150d012017-05-01 11:31:28 -070032#include <unistd.h>
Doug Zongker9270a202012-01-09 15:16:13 -080033
34#include "ui.h"
35#include "cutils/properties.h"
Elliott Hughescb220402016-09-23 15:30:55 -070036
Doug Zongker9270a202012-01-09 15:16:13 -080037#include "common.h"
Doug Zongker18a78e02014-07-10 07:31:46 -070038#include "fuse_sideload.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050039#ifdef USE_OLD_VERIFIER
Ethan Yonker4bf259f2016-08-29 11:50:34 -050040#include "verifier24/verifier.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050041#else
Ethan Yonker34ae4832016-08-24 15:32:18 -050042#include "verifier.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050043#endif
Doug Zongker9270a202012-01-09 15:16:13 -080044
Ethan Yonker8373cfe2017-09-08 06:50:54 -050045static void set_usb_driver(bool enabled) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060046 char configfs[PROPERTY_VALUE_MAX];
47 property_get("sys.usb.configfs", configfs, "false");
48 if (strcmp(configfs, "false") == 0 || strcmp(configfs, "0") == 0)
49 return;
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050050
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060051 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
52 if (fd < 0) {
53/* These error messages show when built in older Android branches (e.g. Gingerbread)
54 It's not a critical error so we're disabling the error messages.
55 ui->Print("failed to open driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040056*/
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060057 printf("failed to open driver control: %s\n", strerror(errno));
Tao Bao0150d012017-05-01 11:31:28 -070058 return;
59 }
Tao Bao0167d4c2017-05-11 14:44:15 -070060
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060061 if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) {
Dees_Troye00c83a2012-09-21 10:00:52 -040062/*
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060063 ui->Print("failed to set driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040064*/
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060065 printf("failed to set driver control: %s\n", strerror(errno));
Tao Bao0150d012017-05-01 11:31:28 -070066 }
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060067 if (close(fd) < 0) {
68/*
69 ui->Print("failed to close driver control: %s\n", strerror(errno));
70*/
71 printf("failed to close driver control: %s\n", strerror(errno));
72 }
Doug Zongker9270a202012-01-09 15:16:13 -080073}
74
Ethan Yonkerfefe5912017-09-30 22:22:13 -050075// On Android 8.0 for some reason init can't seem to completely stop adbd
76// so we have to kill it too if it doesn't die on its own.
77static void kill_adbd() {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060078 DIR* dir = opendir("/proc");
79 if (dir) {
80 struct dirent* de = 0;
Ethan Yonkerfefe5912017-09-30 22:22:13 -050081
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060082 while ((de = readdir(dir)) != 0) {
83 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
84 continue;
Ethan Yonkerfefe5912017-09-30 22:22:13 -050085
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060086 int pid = -1;
87 int ret = sscanf(de->d_name, "%d", &pid);
Ethan Yonkerfefe5912017-09-30 22:22:13 -050088
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060089 if (ret == 1) {
90 char cmdpath[PATH_MAX];
91 sprintf(cmdpath, "/proc/%d/cmdline", pid);
Ethan Yonkerfefe5912017-09-30 22:22:13 -050092
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060093 FILE* file = fopen(cmdpath, "r");
94 size_t task_size = PATH_MAX;
95 char task[PATH_MAX];
96 char* p = task;
97 if (getline(&p, &task_size, file) > 0) {
98 if (strstr(task, "adbd") != 0) {
99 printf("adbd pid %d found, sending kill.\n", pid);
100 kill(pid, SIGINT);
101 usleep(5000);
102 kill(pid, SIGKILL);
103 }
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500104 }
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600105 fclose(file);
106 }
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500107 }
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600108 closedir(dir);
Tao Bao0150d012017-05-01 11:31:28 -0700109 }
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500110}
111
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500112static void stop_adbd() {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600113 printf("Stopping adbd...\n");
114 property_set("ctl.stop", "adbd");
115 usleep(5000);
116 kill_adbd();
Tao Bao0150d012017-05-01 11:31:28 -0700117 set_usb_driver(false);
Doug Zongker9270a202012-01-09 15:16:13 -0800118}
119
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500120static bool is_ro_debuggable() {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600121 char value[PROPERTY_VALUE_MAX+1];
122 return (property_get("ro.debuggable", value, NULL) == 1 && value[0] == '1');
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500123}
Doug Zongker9270a202012-01-09 15:16:13 -0800124
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500125static void maybe_restart_adbd() {
Tao Bao0150d012017-05-01 11:31:28 -0700126 if (is_ro_debuggable()) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600127 printf("Restarting adbd...\n");
Tao Bao0150d012017-05-01 11:31:28 -0700128 set_usb_driver(true);
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600129 property_set("ctl.start", "adbd");
Tao Bao0150d012017-05-01 11:31:28 -0700130 }
Doug Zongker9270a202012-01-09 15:16:13 -0800131}
132
Doug Zongker075ad802014-06-26 15:35:51 -0700133// How long (in seconds) we wait for the host to start sending us a
134// package, before timing out.
135#define ADB_INSTALL_TIMEOUT 300
136
Doug Zongker9270a202012-01-09 15:16:13 -0800137int
thatcc8ddca2015-01-03 01:59:36 +0100138apply_from_adb(const char* install_file, pid_t* child_pid) {
Doug Zongker9270a202012-01-09 15:16:13 -0800139
Tao Bao0150d012017-05-01 11:31:28 -0700140 stop_adbd();
141 set_usb_driver(true);
Dees_Troy2673cec2013-04-02 20:22:16 +0000142/*
Elliott Hughes59bf0da2016-06-15 15:14:04 -0700143int apply_from_adb(RecoveryUI* ui, bool* wipe_cache, const char* install_file) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600144 modified_flash = true;
Tao Bao682c34b2015-04-07 17:16:35 -0700145
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600146 stop_adbd(ui);
147 set_usb_driver(ui, true);
Doug Zongker9270a202012-01-09 15:16:13 -0800148
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600149 ui->Print("\n\nNow send the package you want to apply\n"
150 "to the device with \"adb sideload <filename>\"...\n");
Dees_Troy2673cec2013-04-02 20:22:16 +0000151*/
Tao Bao0150d012017-05-01 11:31:28 -0700152 pid_t child;
153 if ((child = fork()) == 0) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600154 execl("/sbin/recovery", "recovery", "--adbd", install_file, NULL);
155 _exit(-1);
Tao Bao0150d012017-05-01 11:31:28 -0700156 }
Doug Zongker9270a202012-01-09 15:16:13 -0800157
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600158 *child_pid = child;
159 // caller can now kill the child thread from another thread
Tao Bao0150d012017-05-01 11:31:28 -0700160
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600161 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host
162 // connects and starts serving a package. Poll for its
163 // appearance. (Note that inotify doesn't work with FUSE.)
Tao Bao0150d012017-05-01 11:31:28 -0700164 int result = INSTALL_ERROR;
165 int status;
166 bool waited = false;
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600167 struct stat st;
Tao Bao0150d012017-05-01 11:31:28 -0700168 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) {
169 if (waitpid(child, &status, WNOHANG) != 0) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600170 result = -1;
Tao Bao0150d012017-05-01 11:31:28 -0700171 waited = true;
172 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800173 }
Doug Zongker075ad802014-06-26 15:35:51 -0700174
Tao Bao0150d012017-05-01 11:31:28 -0700175 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600176 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT-1) {
Tao Bao0150d012017-05-01 11:31:28 -0700177 sleep(1);
178 continue;
179 } else {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600180 printf("\nTimed out waiting for package: %s\n\n", strerror(errno));
181 result = -1;
Tao Bao0150d012017-05-01 11:31:28 -0700182 kill(child, SIGKILL);
Doug Zongker075ad802014-06-26 15:35:51 -0700183 break;
Tao Bao0150d012017-05-01 11:31:28 -0700184 }
Doug Zongker075ad802014-06-26 15:35:51 -0700185 }
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600186 // Install is handled elsewhere in TWRP
187 //install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false);
188 return 0;
Tao Bao0150d012017-05-01 11:31:28 -0700189 }
Doug Zongker075ad802014-06-26 15:35:51 -0700190
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600191 // if we got here, something failed
192 *child_pid = 0;
thatcc8ddca2015-01-03 01:59:36 +0100193
Tao Bao0150d012017-05-01 11:31:28 -0700194 if (!waited) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600195 // Calling stat() on this magic filename signals the minadbd
196 // subprocess to shut down.
Tao Bao0150d012017-05-01 11:31:28 -0700197 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
Doug Zongker075ad802014-06-26 15:35:51 -0700198
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600199 // TODO(dougz): there should be a way to cancel waiting for a
200 // package (by pushing some button combo on the device). For now
201 // you just have to 'adb sideload' a file that's not a valid
202 // package, like "/dev/null".
Tao Bao0150d012017-05-01 11:31:28 -0700203 waitpid(child, &status, 0);
204 }
205
206 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
207 if (WEXITSTATUS(status) == 3) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600208 printf("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
209 result = -2;
Tao Bao0150d012017-05-01 11:31:28 -0700210 } else if (!WIFSIGNALED(status)) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600211 printf("adbd status %d\n", WEXITSTATUS(status));
thatcc8ddca2015-01-03 01:59:36 +0100212 }
Tao Bao0150d012017-05-01 11:31:28 -0700213 }
Doug Zongker075ad802014-06-26 15:35:51 -0700214
Tao Bao0150d012017-05-01 11:31:28 -0700215 set_usb_driver(false);
216 maybe_restart_adbd();
Doug Zongker9270a202012-01-09 15:16:13 -0800217
Tao Bao0150d012017-05-01 11:31:28 -0700218 return result;
Doug Zongker9270a202012-01-09 15:16:13 -0800219}