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 | |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 17 | #include "install/adb_install.h" |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 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 | |
Tao Bao | 0167d4c | 2017-05-11 14:44:15 -0700 | [diff] [blame] | 29 | #include <android-base/logging.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 30 | #include <android-base/properties.h> |
| 31 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 32 | #include "fuse_sideload.h" |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 33 | #include "install/install.h" |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 34 | #include "recovery_ui/ui.h" |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 35 | |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 36 | static bool SetUsbConfig(const std::string& state) { |
| 37 | android::base::SetProperty("sys.usb.config", state); |
| 38 | return android::base::WaitForProperty("sys.usb.state", state); |
| 39 | } |
| 40 | |
| 41 | int apply_from_adb(bool* wipe_cache, RecoveryUI* ui) { |
Hridya Valsaraju | e4ef453 | 2018-08-31 11:57:51 -0700 | [diff] [blame] | 42 | // Save the usb state to restore after the sideload operation. |
| 43 | std::string usb_state = android::base::GetProperty("sys.usb.state", "none"); |
| 44 | // Clean up state and stop adbd. |
| 45 | if (usb_state != "none" && !SetUsbConfig("none")) { |
| 46 | LOG(ERROR) << "Failed to clear USB config"; |
| 47 | return INSTALL_ERROR; |
| 48 | } |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 49 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 50 | ui->Print( |
| 51 | "\n\nNow send the package you want to apply\n" |
| 52 | "to the device with \"adb sideload <filename>\"...\n"); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 53 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 54 | pid_t child; |
| 55 | if ((child = fork()) == 0) { |
Hridya Valsaraju | cfb3c92 | 2018-07-26 13:04:06 -0700 | [diff] [blame] | 56 | execl("/system/bin/recovery", "recovery", "--adbd", nullptr); |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 57 | _exit(EXIT_FAILURE); |
| 58 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 59 | |
Hridya Valsaraju | e4ef453 | 2018-08-31 11:57:51 -0700 | [diff] [blame] | 60 | if (!SetUsbConfig("sideload")) { |
| 61 | LOG(ERROR) << "Failed to set usb config to sideload"; |
| 62 | return INSTALL_ERROR; |
| 63 | } |
| 64 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 65 | // How long (in seconds) we wait for the host to start sending us a package, before timing out. |
| 66 | static constexpr int ADB_INSTALL_TIMEOUT = 300; |
| 67 | |
| 68 | // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host connects and starts serving a |
| 69 | // package. Poll for its appearance. (Note that inotify doesn't work with FUSE.) |
| 70 | int result = INSTALL_ERROR; |
| 71 | int status; |
| 72 | bool waited = false; |
| 73 | for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) { |
| 74 | if (waitpid(child, &status, WNOHANG) != 0) { |
| 75 | result = INSTALL_ERROR; |
| 76 | waited = true; |
| 77 | break; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 78 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 79 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 80 | struct stat st; |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 81 | if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) { |
| 82 | if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT - 1) { |
| 83 | sleep(1); |
| 84 | continue; |
| 85 | } else { |
| 86 | ui->Print("\nTimed out waiting for package.\n\n"); |
| 87 | result = INSTALL_ERROR; |
| 88 | kill(child, SIGKILL); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 89 | break; |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 90 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 91 | } |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 92 | result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0, ui); |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 93 | break; |
| 94 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 95 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 96 | if (!waited) { |
| 97 | // Calling stat() on this magic filename signals the minadbd subprocess to shut down. |
| 98 | struct stat st; |
| 99 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 100 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 101 | // TODO: there should be a way to cancel waiting for a package (by pushing some button combo on |
| 102 | // the device). For now you just have to 'adb sideload' a file that's not a valid package, like |
| 103 | // "/dev/null". |
| 104 | waitpid(child, &status, 0); |
| 105 | } |
| 106 | |
| 107 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 108 | if (WEXITSTATUS(status) == 3) { |
| 109 | ui->Print("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n"); |
| 110 | } else if (!WIFSIGNALED(status)) { |
| 111 | ui->Print("\n(adbd status %d)\n", WEXITSTATUS(status)); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 112 | } |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 113 | } |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 114 | |
Hridya Valsaraju | e4ef453 | 2018-08-31 11:57:51 -0700 | [diff] [blame] | 115 | // Clean up before switching to the older state, for example setting the state |
| 116 | // to none sets sys/class/android_usb/android0/enable to 0. |
| 117 | if (!SetUsbConfig("none")) { |
| 118 | LOG(ERROR) << "Failed to clear USB config"; |
| 119 | } |
| 120 | |
| 121 | if (usb_state != "none") { |
| 122 | if (!SetUsbConfig(usb_state)) { |
| 123 | LOG(ERROR) << "Failed to set USB config to " << usb_state; |
| 124 | } |
| 125 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 126 | |
Tao Bao | 0150d01 | 2017-05-01 11:31:28 -0700 | [diff] [blame] | 127 | return result; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 128 | } |