blob: 1d19fd3a1a60e27d08188d0079aa42116d66e56b [file] [log] [blame]
Doug Zongker9270a202012-01-09 15:16:13 -08001/*
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
Tao Bao0150d012017-05-01 11:31:28 -070017#include "adb_install.h"
18
Doug Zongker9270a202012-01-09 15:16:13 -080019#include <errno.h>
Tao Bao0150d012017-05-01 11:31:28 -070020#include <fcntl.h>
21#include <signal.h>
Doug Zongker9270a202012-01-09 15:16:13 -080022#include <stdlib.h>
23#include <string.h>
Tao Bao0150d012017-05-01 11:31:28 -070024#include <sys/stat.h>
Doug Zongker9270a202012-01-09 15:16:13 -080025#include <sys/types.h>
26#include <sys/wait.h>
Tao Bao0150d012017-05-01 11:31:28 -070027#include <unistd.h>
Doug Zongker9270a202012-01-09 15:16:13 -080028
Tao Bao0167d4c2017-05-11 14:44:15 -070029#include <android-base/logging.h>
Elliott Hughescb220402016-09-23 15:30:55 -070030#include <android-base/properties.h>
31
Tao Bao0150d012017-05-01 11:31:28 -070032#include "common.h"
33#include "fuse_sideload.h"
34#include "install.h"
Tianjie Xu8f397302018-08-20 13:40:47 -070035#include "recovery_ui/ui.h"
Tao Bao0150d012017-05-01 11:31:28 -070036
Tao Bao641fa972018-04-25 18:59:40 -070037int apply_from_adb(bool* wipe_cache) {
Hridya Valsarajue4ef4532018-08-31 11:57:51 -070038 // Save the usb state to restore after the sideload operation.
39 std::string usb_state = android::base::GetProperty("sys.usb.state", "none");
40 // Clean up state and stop adbd.
41 if (usb_state != "none" && !SetUsbConfig("none")) {
42 LOG(ERROR) << "Failed to clear USB config";
43 return INSTALL_ERROR;
44 }
Tao Bao682c34b2015-04-07 17:16:35 -070045
Tao Bao0150d012017-05-01 11:31:28 -070046 ui->Print(
47 "\n\nNow send the package you want to apply\n"
48 "to the device with \"adb sideload <filename>\"...\n");
Doug Zongker9270a202012-01-09 15:16:13 -080049
Tao Bao0150d012017-05-01 11:31:28 -070050 pid_t child;
51 if ((child = fork()) == 0) {
Hridya Valsarajucfb3c922018-07-26 13:04:06 -070052 execl("/system/bin/recovery", "recovery", "--adbd", nullptr);
Tao Bao0150d012017-05-01 11:31:28 -070053 _exit(EXIT_FAILURE);
54 }
Doug Zongker9270a202012-01-09 15:16:13 -080055
Hridya Valsarajue4ef4532018-08-31 11:57:51 -070056 if (!SetUsbConfig("sideload")) {
57 LOG(ERROR) << "Failed to set usb config to sideload";
58 return INSTALL_ERROR;
59 }
60
Tao Bao0150d012017-05-01 11:31:28 -070061 // How long (in seconds) we wait for the host to start sending us a package, before timing out.
62 static constexpr int ADB_INSTALL_TIMEOUT = 300;
63
64 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host connects and starts serving a
65 // package. Poll for its appearance. (Note that inotify doesn't work with FUSE.)
66 int result = INSTALL_ERROR;
67 int status;
68 bool waited = false;
69 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) {
70 if (waitpid(child, &status, WNOHANG) != 0) {
71 result = INSTALL_ERROR;
72 waited = true;
73 break;
Doug Zongker9270a202012-01-09 15:16:13 -080074 }
Doug Zongker075ad802014-06-26 15:35:51 -070075
Doug Zongker075ad802014-06-26 15:35:51 -070076 struct stat st;
Tao Bao0150d012017-05-01 11:31:28 -070077 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
78 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT - 1) {
79 sleep(1);
80 continue;
81 } else {
82 ui->Print("\nTimed out waiting for package.\n\n");
83 result = INSTALL_ERROR;
84 kill(child, SIGKILL);
Doug Zongker075ad802014-06-26 15:35:51 -070085 break;
Tao Bao0150d012017-05-01 11:31:28 -070086 }
Doug Zongker075ad802014-06-26 15:35:51 -070087 }
Tao Bao641fa972018-04-25 18:59:40 -070088 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0);
Tao Bao0150d012017-05-01 11:31:28 -070089 break;
90 }
Doug Zongker075ad802014-06-26 15:35:51 -070091
Tao Bao0150d012017-05-01 11:31:28 -070092 if (!waited) {
93 // Calling stat() on this magic filename signals the minadbd subprocess to shut down.
94 struct stat st;
95 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
Doug Zongker075ad802014-06-26 15:35:51 -070096
Tao Bao0150d012017-05-01 11:31:28 -070097 // TODO: there should be a way to cancel waiting for a package (by pushing some button combo on
98 // the device). For now you just have to 'adb sideload' a file that's not a valid package, like
99 // "/dev/null".
100 waitpid(child, &status, 0);
101 }
102
103 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
104 if (WEXITSTATUS(status) == 3) {
105 ui->Print("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
106 } else if (!WIFSIGNALED(status)) {
107 ui->Print("\n(adbd status %d)\n", WEXITSTATUS(status));
Doug Zongker075ad802014-06-26 15:35:51 -0700108 }
Tao Bao0150d012017-05-01 11:31:28 -0700109 }
Doug Zongker075ad802014-06-26 15:35:51 -0700110
Hridya Valsarajue4ef4532018-08-31 11:57:51 -0700111 // Clean up before switching to the older state, for example setting the state
112 // to none sets sys/class/android_usb/android0/enable to 0.
113 if (!SetUsbConfig("none")) {
114 LOG(ERROR) << "Failed to clear USB config";
115 }
116
117 if (usb_state != "none") {
118 if (!SetUsbConfig(usb_state)) {
119 LOG(ERROR) << "Failed to set USB config to " << usb_state;
120 }
121 }
Doug Zongker9270a202012-01-09 15:16:13 -0800122
Tao Bao0150d012017-05-01 11:31:28 -0700123 return result;
Doug Zongker9270a202012-01-09 15:16:13 -0800124}