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