blob: 5b57641108f96ad1f3e6d2c2796280d76b0ccb98 [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
Doug Zongker9270a202012-01-09 15:16:13 -080017#include <dirent.h>
18#include <errno.h>
Tao Bao0150d012017-05-01 11:31:28 -070019#include <fcntl.h>
20#include <signal.h>
Doug Zongker9270a202012-01-09 15:16:13 -080021#include <stdlib.h>
22#include <string.h>
Tao Bao0150d012017-05-01 11:31:28 -070023#include <sys/stat.h>
Doug Zongker9270a202012-01-09 15:16:13 -080024#include <sys/types.h>
25#include <sys/wait.h>
26#include <sys/stat.h>
27#include <signal.h>
28#include <fcntl.h>
Dees_Troy2673cec2013-04-02 20:22:16 +000029#include <stdio.h>
Tao Bao0150d012017-05-01 11:31:28 -070030#include <unistd.h>
Doug Zongker9270a202012-01-09 15:16:13 -080031
Doug Zongker9270a202012-01-09 15:16:13 -080032#include "cutils/properties.h"
Elliott Hughescb220402016-09-23 15:30:55 -070033
Doug Zongker9270a202012-01-09 15:16:13 -080034#include "common.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050035#ifdef USE_OLD_VERIFIER
Ethan Yonker4bf259f2016-08-29 11:50:34 -050036#include "verifier24/verifier.h"
bigbiffd58ba182020-03-23 10:02:29 -040037#include "ui.h"
38#elif USE_28_VERIFIER
39#include "verifier28/verifier.h"
40#include "verifier28/adb_install.h"
41#include "verifier28/ui.h"
42#include "verifier28/fuse_sideload.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050043#else
bigbiffd58ba182020-03-23 10:02:29 -040044#include "install/install.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050045#endif
Doug Zongker9270a202012-01-09 15:16:13 -080046
Ethan Yonker8373cfe2017-09-08 06:50:54 -050047static void set_usb_driver(bool enabled) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060048 char configfs[PROPERTY_VALUE_MAX];
49 property_get("sys.usb.configfs", configfs, "false");
50 if (strcmp(configfs, "false") == 0 || strcmp(configfs, "0") == 0)
51 return;
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050052
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060053 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
54 if (fd < 0) {
55/* These error messages show when built in older Android branches (e.g. Gingerbread)
56 It's not a critical error so we're disabling the error messages.
57 ui->Print("failed to open driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040058*/
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060059 printf("failed to open driver control: %s\n", strerror(errno));
Tao Bao0150d012017-05-01 11:31:28 -070060 return;
61 }
Tao Bao0167d4c2017-05-11 14:44:15 -070062
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060063 if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) {
Dees_Troye00c83a2012-09-21 10:00:52 -040064/*
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060065 ui->Print("failed to set driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040066*/
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060067 printf("failed to set driver control: %s\n", strerror(errno));
Tao Bao0150d012017-05-01 11:31:28 -070068 }
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060069 if (close(fd) < 0) {
70/*
71 ui->Print("failed to close driver control: %s\n", strerror(errno));
72*/
73 printf("failed to close driver control: %s\n", strerror(errno));
74 }
Doug Zongker9270a202012-01-09 15:16:13 -080075}
76
Ethan Yonkerfefe5912017-09-30 22:22:13 -050077// On Android 8.0 for some reason init can't seem to completely stop adbd
78// so we have to kill it too if it doesn't die on its own.
79static void kill_adbd() {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060080 DIR* dir = opendir("/proc");
81 if (dir) {
82 struct dirent* de = 0;
Ethan Yonkerfefe5912017-09-30 22:22:13 -050083
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060084 while ((de = readdir(dir)) != 0) {
85 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
86 continue;
Ethan Yonkerfefe5912017-09-30 22:22:13 -050087
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060088 int pid = -1;
89 int ret = sscanf(de->d_name, "%d", &pid);
Ethan Yonkerfefe5912017-09-30 22:22:13 -050090
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060091 if (ret == 1) {
92 char cmdpath[PATH_MAX];
93 sprintf(cmdpath, "/proc/%d/cmdline", pid);
Ethan Yonkerfefe5912017-09-30 22:22:13 -050094
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060095 FILE* file = fopen(cmdpath, "r");
96 size_t task_size = PATH_MAX;
97 char task[PATH_MAX];
98 char* p = task;
99 if (getline(&p, &task_size, file) > 0) {
100 if (strstr(task, "adbd") != 0) {
101 printf("adbd pid %d found, sending kill.\n", pid);
102 kill(pid, SIGINT);
103 usleep(5000);
104 kill(pid, SIGKILL);
105 }
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500106 }
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600107 fclose(file);
108 }
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500109 }
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600110 closedir(dir);
Tao Bao0150d012017-05-01 11:31:28 -0700111 }
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500112}
113
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500114static void stop_adbd() {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600115 printf("Stopping adbd...\n");
116 property_set("ctl.stop", "adbd");
117 usleep(5000);
118 kill_adbd();
Tao Bao0150d012017-05-01 11:31:28 -0700119 set_usb_driver(false);
Doug Zongker9270a202012-01-09 15:16:13 -0800120}
121
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500122static bool is_ro_debuggable() {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600123 char value[PROPERTY_VALUE_MAX+1];
124 return (property_get("ro.debuggable", value, NULL) == 1 && value[0] == '1');
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500125}
Doug Zongker9270a202012-01-09 15:16:13 -0800126
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500127static void maybe_restart_adbd() {
Tao Bao0150d012017-05-01 11:31:28 -0700128 if (is_ro_debuggable()) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600129 printf("Restarting adbd...\n");
Tao Bao0150d012017-05-01 11:31:28 -0700130 set_usb_driver(true);
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600131 property_set("ctl.start", "adbd");
Tao Bao0150d012017-05-01 11:31:28 -0700132 }
Doug Zongker9270a202012-01-09 15:16:13 -0800133}
134
Doug Zongker075ad802014-06-26 15:35:51 -0700135// How long (in seconds) we wait for the host to start sending us a
136// package, before timing out.
137#define ADB_INSTALL_TIMEOUT 300
138
Doug Zongker9270a202012-01-09 15:16:13 -0800139int
thatcc8ddca2015-01-03 01:59:36 +0100140apply_from_adb(const char* install_file, pid_t* child_pid) {
Doug Zongker9270a202012-01-09 15:16:13 -0800141
Tao Bao0150d012017-05-01 11:31:28 -0700142 stop_adbd();
143 set_usb_driver(true);
Dees_Troy2673cec2013-04-02 20:22:16 +0000144/*
Elliott Hughes59bf0da2016-06-15 15:14:04 -0700145int apply_from_adb(RecoveryUI* ui, bool* wipe_cache, const char* install_file) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600146 modified_flash = true;
Tao Bao682c34b2015-04-07 17:16:35 -0700147
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600148 stop_adbd(ui);
149 set_usb_driver(ui, true);
Doug Zongker9270a202012-01-09 15:16:13 -0800150
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600151 ui->Print("\n\nNow send the package you want to apply\n"
152 "to the device with \"adb sideload <filename>\"...\n");
Dees_Troy2673cec2013-04-02 20:22:16 +0000153*/
Tao Bao0150d012017-05-01 11:31:28 -0700154 pid_t child;
155 if ((child = fork()) == 0) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600156 execl("/sbin/recovery", "recovery", "--adbd", install_file, NULL);
157 _exit(-1);
Tao Bao0150d012017-05-01 11:31:28 -0700158 }
Doug Zongker9270a202012-01-09 15:16:13 -0800159
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600160 *child_pid = child;
161 // caller can now kill the child thread from another thread
Tao Bao0150d012017-05-01 11:31:28 -0700162
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600163 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host
164 // connects and starts serving a package. Poll for its
165 // appearance. (Note that inotify doesn't work with FUSE.)
Tao Bao0150d012017-05-01 11:31:28 -0700166 int result = INSTALL_ERROR;
167 int status;
168 bool waited = false;
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600169 struct stat st;
Tao Bao0150d012017-05-01 11:31:28 -0700170 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) {
171 if (waitpid(child, &status, WNOHANG) != 0) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600172 result = -1;
Tao Bao0150d012017-05-01 11:31:28 -0700173 waited = true;
174 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800175 }
Doug Zongker075ad802014-06-26 15:35:51 -0700176
Tao Bao0150d012017-05-01 11:31:28 -0700177 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600178 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT-1) {
Tao Bao0150d012017-05-01 11:31:28 -0700179 sleep(1);
180 continue;
181 } else {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600182 printf("\nTimed out waiting for package: %s\n\n", strerror(errno));
183 result = -1;
Tao Bao0150d012017-05-01 11:31:28 -0700184 kill(child, SIGKILL);
Doug Zongker075ad802014-06-26 15:35:51 -0700185 break;
Tao Bao0150d012017-05-01 11:31:28 -0700186 }
Doug Zongker075ad802014-06-26 15:35:51 -0700187 }
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600188 // Install is handled elsewhere in TWRP
189 //install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false);
190 return 0;
Tao Bao0150d012017-05-01 11:31:28 -0700191 }
Doug Zongker075ad802014-06-26 15:35:51 -0700192
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600193 // if we got here, something failed
194 *child_pid = 0;
thatcc8ddca2015-01-03 01:59:36 +0100195
Tao Bao0150d012017-05-01 11:31:28 -0700196 if (!waited) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600197 // Calling stat() on this magic filename signals the minadbd
198 // subprocess to shut down.
Tao Bao0150d012017-05-01 11:31:28 -0700199 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
Doug Zongker075ad802014-06-26 15:35:51 -0700200
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600201 // TODO(dougz): there should be a way to cancel waiting for a
202 // package (by pushing some button combo on the device). For now
203 // you just have to 'adb sideload' a file that's not a valid
204 // package, like "/dev/null".
Tao Bao0150d012017-05-01 11:31:28 -0700205 waitpid(child, &status, 0);
206 }
207
208 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
209 if (WEXITSTATUS(status) == 3) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600210 printf("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
211 result = -2;
Tao Bao0150d012017-05-01 11:31:28 -0700212 } else if (!WIFSIGNALED(status)) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600213 printf("adbd status %d\n", WEXITSTATUS(status));
thatcc8ddca2015-01-03 01:59:36 +0100214 }
Tao Bao0150d012017-05-01 11:31:28 -0700215 }
Doug Zongker075ad802014-06-26 15:35:51 -0700216
Tao Bao0150d012017-05-01 11:31:28 -0700217 set_usb_driver(false);
218 maybe_restart_adbd();
Doug Zongker9270a202012-01-09 15:16:13 -0800219
Tao Bao0150d012017-05-01 11:31:28 -0700220 return result;
Doug Zongker9270a202012-01-09 15:16:13 -0800221}