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