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 | |
| 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_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 28 | |
| 29 | #include "ui.h" |
| 30 | #include "cutils/properties.h" |
| 31 | #include "install.h" |
| 32 | #include "common.h" |
| 33 | #include "adb_install.h" |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 34 | #include "minadbd/fuse_adb_provider.h" |
| 35 | #include "fuse_sideload.h" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 36 | #ifdef USE_OLD_VERIFIER |
Ethan Yonker | 4bf259f | 2016-08-29 11:50:34 -0500 | [diff] [blame] | 37 | #include "verifier24/verifier.h" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 38 | #else |
Ethan Yonker | 34ae483 | 2016-08-24 15:32:18 -0500 | [diff] [blame] | 39 | #include "verifier.h" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 40 | #endif |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 41 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 42 | static void set_usb_driver(bool enabled) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 43 | int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY); |
| 44 | if (fd < 0) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 45 | /* 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 Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 47 | ui->Print("failed to open driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 48 | */ |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 49 | printf("failed to open driver control: %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 50 | return; |
| 51 | } |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 52 | |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 53 | if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 54 | /* |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 55 | ui->Print("failed to set driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 56 | */ |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 57 | printf("failed to set driver control: %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 58 | } |
| 59 | if (close(fd) < 0) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 60 | /* |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 61 | ui->Print("failed to close driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 62 | */ |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 63 | printf("failed to close driver control: %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 67 | static void stop_adbd() { |
| 68 | printf("Stopping adbd...\n"); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 69 | property_set("ctl.stop", "adbd"); |
| 70 | set_usb_driver(false); |
| 71 | } |
| 72 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 73 | static bool is_ro_debuggable() { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 74 | char value[PROPERTY_VALUE_MAX+1]; |
| 75 | return (property_get("ro.debuggable", value, NULL) == 1 && value[0] == '1'); |
| 76 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 77 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 78 | static void maybe_restart_adbd() { |
Elliott Hughes | f14af80 | 2015-02-10 14:46:14 -0800 | [diff] [blame] | 79 | if (is_ro_debuggable()) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 80 | printf("Restarting adbd...\n"); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 81 | set_usb_driver(true); |
| 82 | property_set("ctl.start", "adbd"); |
| 83 | } |
| 84 | } |
| 85 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 86 | // How long (in seconds) we wait for the host to start sending us a |
| 87 | // package, before timing out. |
| 88 | #define ADB_INSTALL_TIMEOUT 300 |
| 89 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 90 | int |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 91 | apply_from_adb(const char* install_file, pid_t* child_pid) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 92 | |
| 93 | stop_adbd(); |
| 94 | set_usb_driver(true); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 95 | /* |
Elliott Hughes | 59bf0da | 2016-06-15 15:14:04 -0700 | [diff] [blame] | 96 | int apply_from_adb(RecoveryUI* ui, bool* wipe_cache, const char* install_file) { |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 97 | modified_flash = true; |
| 98 | |
Elliott Hughes | 59bf0da | 2016-06-15 15:14:04 -0700 | [diff] [blame] | 99 | stop_adbd(ui); |
| 100 | set_usb_driver(ui, true); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 101 | |
| 102 | ui->Print("\n\nNow send the package you want to apply\n" |
| 103 | "to the device with \"adb sideload <filename>\"...\n"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 104 | */ |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 105 | pid_t child; |
| 106 | if ((child = fork()) == 0) { |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 107 | execl("/sbin/recovery", "recovery", "--adbd", install_file, NULL); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 108 | _exit(-1); |
| 109 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 110 | |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 111 | *child_pid = child; |
| 112 | // caller can now kill the child thread from another thread |
Ethan Yonker | a167416 | 2014-11-06 08:35:10 -0600 | [diff] [blame] | 113 | |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 114 | // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 115 | // connects and starts serving a package. Poll for its |
| 116 | // appearance. (Note that inotify doesn't work with FUSE.) |
Tao Bao | 80e46e0 | 2015-06-03 10:49:29 -0700 | [diff] [blame] | 117 | int result = INSTALL_ERROR; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 118 | int status; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 119 | bool waited = false; |
| 120 | struct stat st; |
| 121 | for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) { |
| 122 | if (waitpid(child, &status, WNOHANG) != 0) { |
Ethan Yonker | 2686009 | 2014-11-06 09:49:25 -0600 | [diff] [blame] | 123 | result = -1; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 124 | waited = true; |
| 125 | break; |
| 126 | } |
| 127 | |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 128 | if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) { |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 129 | if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT-1) { |
| 130 | sleep(1); |
| 131 | continue; |
| 132 | } else { |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 133 | printf("\nTimed out waiting for package: %s\n\n", strerror(errno)); |
Ethan Yonker | 2686009 | 2014-11-06 09:49:25 -0600 | [diff] [blame] | 134 | result = -1; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 135 | kill(child, SIGKILL); |
| 136 | break; |
| 137 | } |
| 138 | } |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 139 | // Install is handled elsewhere in TWRP |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 140 | //install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false); |
| 141 | return 0; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 142 | } |
| 143 | |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 144 | // if we got here, something failed |
| 145 | *child_pid = 0; |
| 146 | |
| 147 | if (!waited) { |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 148 | // Calling stat() on this magic filename signals the minadbd |
| 149 | // subprocess to shut down. |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 150 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 151 | |
| 152 | // TODO(dougz): there should be a way to cancel waiting for a |
| 153 | // package (by pushing some button combo on the device). For now |
| 154 | // you just have to 'adb sideload' a file that's not a valid |
| 155 | // package, like "/dev/null". |
| 156 | waitpid(child, &status, 0); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 157 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 158 | |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 159 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 160 | if (WEXITSTATUS(status) == 3) { |
| 161 | printf("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n"); |
| 162 | result = -2; |
| 163 | } else if (!WIFSIGNALED(status)) { |
| 164 | printf("status %d\n", WEXITSTATUS(status)); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 165 | } |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 166 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 167 | |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 168 | set_usb_driver(false); |
| 169 | maybe_restart_adbd(); |
| 170 | |
| 171 | return result; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 172 | } |