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 "adb_install.h" 18 19 #include <errno.h> 20 #include <fcntl.h> 21 #include <signal.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <sys/stat.h> 25 #include <sys/types.h> 26 #include <sys/wait.h> 27 #include <unistd.h> 28 29 #include <android-base/file.h> 30 #include <android-base/logging.h> 31 #include <android-base/properties.h> 32 #include <android-base/unique_fd.h> 33 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) { 40 // USB configfs doesn't use /s/c/a/a/enable. 41 if (android::base::GetBoolProperty("sys.usb.configfs", false)) { 42 return; 43 } 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; 50 } 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"; 56 } 57 } 58 59 static void stop_adbd() { 60 ui->Print("Stopping adbd...\n"); 61 android::base::SetProperty("ctl.stop", "adbd"); 62 set_usb_driver(false); 63 } 64 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 } 71 } 72 73 int apply_from_adb(bool* wipe_cache, const char* install_file) { 74 modified_flash = true; 75 76 stop_adbd(); 77 set_usb_driver(true); 78 79 ui->Print( 80 "\n\nNow send the package you want to apply\n" 81 "to the device with \"adb sideload <filename>\"...\n"); 82 83 pid_t child; 84 if ((child = fork()) == 0) { 85 execl("/sbin/recovery", "recovery", "--adbd", nullptr); 86 _exit(EXIT_FAILURE); 87 } 88 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; 102 } 103 104 struct stat st; 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); 113 break; 114 } 115 } 116 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false, 0); 117 break; 118 } 119 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); 124 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)); 136 } 137 } 138 139 set_usb_driver(false); 140 maybe_restart_adbd(); 141 142 return result; 143 } 144