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 <errno.h> |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <signal.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 24 | #include <sys/stat.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/wait.h> |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 28 | |
Tao Bao | 0167d4c | 2017-05-11 14:44:15 -0700 | [diff] [blame] | 29 | #include <android-base/file.h> |
| 30 | #include <android-base/logging.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 31 | #include <android-base/properties.h> |
Tao Bao | 0167d4c | 2017-05-11 14:44:15 -0700 | [diff] [blame] | 32 | #include <android-base/unique_fd.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 33 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 34 | #include "common.h" |
| 35 | #include "fuse_sideload.h" |
| 36 | #include "install.h" |
| 37 | #include "ui.h" |
| 38 | |
| 39 | static void set_usb_driver(bool enabled) { |
Tao Bao | 0167d4c | 2017-05-11 14:44:15 -0700 | [diff] [blame] | 40 | // USB configfs doesn't use /s/c/a/a/enable. |
| 41 | if (android::base::GetBoolProperty("sys.usb.configfs", false)) { |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 42 | return; |
| 43 | } |
Tao Bao | 0167d4c | 2017-05-11 14:44:15 -0700 | [diff] [blame] | 44 | |
| 45 | static constexpr const char* USB_DRIVER_CONTROL = "/sys/class/android_usb/android0/enable"; |
| 46 | android::base::unique_fd fd(open(USB_DRIVER_CONTROL, O_WRONLY)); |
| 47 | if (fd == -1) { |
| 48 | PLOG(ERROR) << "Failed to open driver control"; |
| 49 | return; |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 50 | } |
Tao Bao | 0167d4c | 2017-05-11 14:44:15 -0700 | [diff] [blame] | 51 | // Not using android::base::WriteStringToFile since that will open with O_CREAT and give EPERM |
| 52 | // when USB_DRIVER_CONTROL doesn't exist. When it gives EPERM, we don't know whether that's due |
| 53 | // to non-existent USB_DRIVER_CONTROL or indeed a permission issue. |
| 54 | if (!android::base::WriteStringToFd(enabled ? "1" : "0", fd)) { |
| 55 | PLOG(ERROR) << "Failed to set driver control"; |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 56 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 59 | static void stop_adbd() { |
| 60 | ui->Print("Stopping adbd...\n"); |
| 61 | android::base::SetProperty("ctl.stop", "adbd"); |
| 62 | set_usb_driver(false); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 65 | static void maybe_restart_adbd() { |
| 66 | if (is_ro_debuggable()) { |
| 67 | ui->Print("Restarting adbd...\n"); |
| 68 | set_usb_driver(true); |
| 69 | android::base::SetProperty("ctl.start", "adbd"); |
| 70 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 73 | int apply_from_adb(bool* wipe_cache, const char* install_file) { |
| 74 | modified_flash = true; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 75 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 76 | stop_adbd(); |
| 77 | set_usb_driver(true); |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 78 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 79 | ui->Print( |
| 80 | "\n\nNow send the package you want to apply\n" |
| 81 | "to the device with \"adb sideload <filename>\"...\n"); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 82 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 83 | pid_t child; |
| 84 | if ((child = fork()) == 0) { |
| 85 | execl("/sbin/recovery", "recovery", "--adbd", nullptr); |
| 86 | _exit(EXIT_FAILURE); |
| 87 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 88 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 89 | // How long (in seconds) we wait for the host to start sending us a package, before timing out. |
| 90 | static constexpr int ADB_INSTALL_TIMEOUT = 300; |
| 91 | |
| 92 | // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host connects and starts serving a |
| 93 | // package. Poll for its appearance. (Note that inotify doesn't work with FUSE.) |
| 94 | int result = INSTALL_ERROR; |
| 95 | int status; |
| 96 | bool waited = false; |
| 97 | for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) { |
| 98 | if (waitpid(child, &status, WNOHANG) != 0) { |
| 99 | result = INSTALL_ERROR; |
| 100 | waited = true; |
| 101 | break; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 102 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 103 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 104 | struct stat st; |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 105 | if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) { |
| 106 | if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT - 1) { |
| 107 | sleep(1); |
| 108 | continue; |
| 109 | } else { |
| 110 | ui->Print("\nTimed out waiting for package.\n\n"); |
| 111 | result = INSTALL_ERROR; |
| 112 | kill(child, SIGKILL); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 113 | break; |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 114 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 115 | } |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 116 | result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false, 0); |
| 117 | break; |
| 118 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 119 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 120 | if (!waited) { |
| 121 | // Calling stat() on this magic filename signals the minadbd subprocess to shut down. |
| 122 | struct stat st; |
| 123 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 124 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 125 | // TODO: there should be a way to cancel waiting for a package (by pushing some button combo on |
| 126 | // the device). For now you just have to 'adb sideload' a file that's not a valid package, like |
| 127 | // "/dev/null". |
| 128 | waitpid(child, &status, 0); |
| 129 | } |
| 130 | |
| 131 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 132 | if (WEXITSTATUS(status) == 3) { |
| 133 | ui->Print("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n"); |
| 134 | } else if (!WIFSIGNALED(status)) { |
| 135 | ui->Print("\n(adbd status %d)\n", WEXITSTATUS(status)); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 136 | } |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 137 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 138 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 139 | set_usb_driver(false); |
| 140 | maybe_restart_adbd(); |
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 | return result; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 143 | } |