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