Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 1 | /* |
| 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 Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 17 | #include "adb_install.h" |
| 18 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <errno.h> |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <signal.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 25 | #include <sys/stat.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/wait.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <signal.h> |
| 30 | #include <fcntl.h> |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 32 | #include <unistd.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 33 | |
| 34 | #include "ui.h" |
| 35 | #include "cutils/properties.h" |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 36 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 37 | #include "common.h" |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 38 | #include "fuse_sideload.h" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 39 | #ifdef USE_OLD_VERIFIER |
Ethan Yonker | 4bf259f | 2016-08-29 11:50:34 -0500 | [diff] [blame] | 40 | #include "verifier24/verifier.h" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 41 | #else |
Ethan Yonker | 34ae483 | 2016-08-24 15:32:18 -0500 | [diff] [blame] | 42 | #include "verifier.h" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 43 | #endif |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 44 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 45 | static void set_usb_driver(bool enabled) { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 46 | 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 Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 50 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 51 | 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_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 56 | */ |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 57 | printf("failed to open driver control: %s\n", strerror(errno)); |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 58 | return; |
| 59 | } |
Tao Bao | 0167d4c | 2017-05-11 14:44:15 -0700 | [diff] [blame] | 60 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 61 | if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 62 | /* |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 63 | ui->Print("failed to set driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 64 | */ |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 65 | printf("failed to set driver control: %s\n", strerror(errno)); |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 66 | } |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 67 | 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 Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 75 | // 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. |
| 77 | static void kill_adbd() { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 78 | DIR* dir = opendir("/proc"); |
| 79 | if (dir) { |
| 80 | struct dirent* de = 0; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 81 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 82 | while ((de = readdir(dir)) != 0) { |
| 83 | if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) |
| 84 | continue; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 85 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 86 | int pid = -1; |
| 87 | int ret = sscanf(de->d_name, "%d", &pid); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 88 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 89 | if (ret == 1) { |
| 90 | char cmdpath[PATH_MAX]; |
| 91 | sprintf(cmdpath, "/proc/%d/cmdline", pid); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 92 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 93 | 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 Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 104 | } |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 105 | fclose(file); |
| 106 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 107 | } |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 108 | closedir(dir); |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 109 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 110 | } |
| 111 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 112 | static void stop_adbd() { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 113 | printf("Stopping adbd...\n"); |
| 114 | property_set("ctl.stop", "adbd"); |
| 115 | usleep(5000); |
| 116 | kill_adbd(); |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 117 | set_usb_driver(false); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 120 | static bool is_ro_debuggable() { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 121 | char value[PROPERTY_VALUE_MAX+1]; |
| 122 | return (property_get("ro.debuggable", value, NULL) == 1 && value[0] == '1'); |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 123 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 124 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 125 | static void maybe_restart_adbd() { |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 126 | if (is_ro_debuggable()) { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 127 | printf("Restarting adbd...\n"); |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 128 | set_usb_driver(true); |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 129 | property_set("ctl.start", "adbd"); |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 130 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 133 | // 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 Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 137 | int |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 138 | apply_from_adb(const char* install_file, pid_t* child_pid) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 139 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 140 | stop_adbd(); |
| 141 | set_usb_driver(true); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 142 | /* |
Elliott Hughes | 59bf0da | 2016-06-15 15:14:04 -0700 | [diff] [blame] | 143 | int apply_from_adb(RecoveryUI* ui, bool* wipe_cache, const char* install_file) { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 144 | modified_flash = true; |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 145 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 146 | stop_adbd(ui); |
| 147 | set_usb_driver(ui, true); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 148 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 149 | ui->Print("\n\nNow send the package you want to apply\n" |
| 150 | "to the device with \"adb sideload <filename>\"...\n"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 151 | */ |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 152 | pid_t child; |
| 153 | if ((child = fork()) == 0) { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 154 | execl("/sbin/recovery", "recovery", "--adbd", install_file, NULL); |
| 155 | _exit(-1); |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 156 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 157 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 158 | *child_pid = child; |
| 159 | // caller can now kill the child thread from another thread |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 160 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 161 | // 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 Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 164 | int result = INSTALL_ERROR; |
| 165 | int status; |
| 166 | bool waited = false; |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 167 | struct stat st; |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 168 | for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) { |
| 169 | if (waitpid(child, &status, WNOHANG) != 0) { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 170 | result = -1; |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 171 | waited = true; |
| 172 | break; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 173 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 174 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 175 | if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 176 | if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT-1) { |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 177 | sleep(1); |
| 178 | continue; |
| 179 | } else { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 180 | printf("\nTimed out waiting for package: %s\n\n", strerror(errno)); |
| 181 | result = -1; |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 182 | kill(child, SIGKILL); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 183 | break; |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 184 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 185 | } |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 186 | // Install is handled elsewhere in TWRP |
| 187 | //install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false); |
| 188 | return 0; |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 189 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 190 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 191 | // if we got here, something failed |
| 192 | *child_pid = 0; |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 193 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 194 | if (!waited) { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 195 | // Calling stat() on this magic filename signals the minadbd |
| 196 | // subprocess to shut down. |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 197 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 198 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 199 | // 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 Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 203 | waitpid(child, &status, 0); |
| 204 | } |
| 205 | |
| 206 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 207 | if (WEXITSTATUS(status) == 3) { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 208 | printf("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n"); |
| 209 | result = -2; |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 210 | } else if (!WIFSIGNALED(status)) { |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 211 | printf("adbd status %d\n", WEXITSTATUS(status)); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 212 | } |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 213 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 214 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 215 | set_usb_driver(false); |
| 216 | maybe_restart_adbd(); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 217 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 218 | return result; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 219 | } |