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