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