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> |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 28 | |
| 29 | #include "ui.h" |
| 30 | #include "cutils/properties.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 31 | #include "adb_install.h" |
| 32 | extern "C" { |
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 | |
| 37 | static RecoveryUI* ui = NULL; |
| 38 | |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 39 | void |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 40 | set_usb_driver(bool enabled) { |
| 41 | int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY); |
| 42 | if (fd < 0) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 43 | /* These error messages show when built in older Android branches (e.g. Gingerbread) |
| 44 | It's not a critical error so we're disabling the error messages. |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 45 | ui->Print("failed to open driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 46 | */ |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 47 | printf("failed to open driver control: %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | if (write(fd, enabled ? "1" : "0", 1) < 0) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 51 | /* |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 52 | ui->Print("failed to set driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 53 | */ |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 54 | printf("failed to set driver control: %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 55 | } |
| 56 | if (close(fd) < 0) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 57 | /* |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 58 | ui->Print("failed to close driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 59 | */ |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 60 | printf("failed to close driver control: %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | stop_adbd() { |
| 66 | property_set("ctl.stop", "adbd"); |
| 67 | set_usb_driver(false); |
| 68 | } |
| 69 | |
| 70 | |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 71 | void |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 72 | maybe_restart_adbd() { |
| 73 | char value[PROPERTY_VALUE_MAX+1]; |
| 74 | int len = property_get("ro.debuggable", value, NULL); |
| 75 | if (len == 1 && value[0] == '1') { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 76 | printf("Restarting adbd...\n"); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 77 | set_usb_driver(true); |
| 78 | property_set("ctl.start", "adbd"); |
| 79 | } |
| 80 | } |
| 81 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 82 | // How long (in seconds) we wait for the host to start sending us a |
| 83 | // package, before timing out. |
| 84 | #define ADB_INSTALL_TIMEOUT 300 |
| 85 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 86 | int |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 87 | apply_from_adb(const char* install_file) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 88 | |
| 89 | stop_adbd(); |
| 90 | set_usb_driver(true); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 91 | /* |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 92 | ui->Print("\n\nNow send the package you want to apply\n" |
| 93 | "to the device with \"adb sideload <filename>\"...\n"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 94 | */ |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 95 | pid_t child; |
| 96 | if ((child = fork()) == 0) { |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 97 | execl("/sbin/recovery", "recovery", "--adbd", install_file, NULL); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 98 | _exit(-1); |
| 99 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 100 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 101 | char child_prop[PROPERTY_VALUE_MAX]; |
| 102 | sprintf(child_prop, "%i", child); |
| 103 | property_set("tw_child_pid", child_prop); |
Ethan Yonker | a167416 | 2014-11-06 08:35:10 -0600 | [diff] [blame] | 104 | |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 105 | // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 106 | // connects and starts serving a package. Poll for its |
| 107 | // appearance. (Note that inotify doesn't work with FUSE.) |
| 108 | int result; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 109 | int status; |
Ethan Yonker | 2686009 | 2014-11-06 09:49:25 -0600 | [diff] [blame] | 110 | int wipe_cache; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 111 | bool waited = false; |
| 112 | struct stat st; |
| 113 | for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) { |
| 114 | if (waitpid(child, &status, WNOHANG) != 0) { |
Ethan Yonker | 2686009 | 2014-11-06 09:49:25 -0600 | [diff] [blame] | 115 | result = -1; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 116 | waited = true; |
| 117 | break; |
| 118 | } |
| 119 | |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 120 | if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) { |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 121 | if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT-1) { |
| 122 | sleep(1); |
| 123 | continue; |
| 124 | } else { |
Ethan Yonker | 2686009 | 2014-11-06 09:49:25 -0600 | [diff] [blame] | 125 | printf("\nTimed out waiting for package.\n\n", strerror(errno)); |
| 126 | result = -1; |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 127 | kill(child, SIGKILL); |
| 128 | break; |
| 129 | } |
| 130 | } |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 131 | property_set("tw_sideload_file", FUSE_SIDELOAD_HOST_PATHNAME); |
| 132 | // Install is handled elsewhere in TWRP |
| 133 | result = 5; //install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 137 | // We do this elsewhere in TWRP |
| 138 | /*if (!waited) { |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 139 | // Calling stat() on this magic filename signals the minadbd |
| 140 | // subprocess to shut down. |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 141 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 142 | |
| 143 | // TODO(dougz): there should be a way to cancel waiting for a |
| 144 | // package (by pushing some button combo on the device). For now |
| 145 | // you just have to 'adb sideload' a file that's not a valid |
| 146 | // package, like "/dev/null". |
| 147 | waitpid(child, &status, 0); |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 148 | }*/ |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 149 | |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 150 | if (result != 5) { |
| 151 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
| 152 | waitpid(child, &status, 0); |
| 153 | result = -1; |
| 154 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 155 | if (WEXITSTATUS(status) == 3) { |
| 156 | printf("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n"); |
| 157 | result = -2; |
| 158 | } else if (!WIFSIGNALED(status)) { |
| 159 | printf("status %d\n", WEXITSTATUS(status)); |
| 160 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 161 | } |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 162 | set_usb_driver(false); |
| 163 | maybe_restart_adbd(); |
| 164 | return result; |
| 165 | } else { |
| 166 | return 0; |
| 167 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 168 | |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 169 | return -1; // This should not happen |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 170 | } |