blob: 79b8df91b4bd74be7ed6ad05cf9536c791ce075a [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
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"
Doug Zongker9270a202012-01-09 15:16:13 -080029#include "install.h"
30#include "common.h"
31#include "adb_install.h"
Doug Zongker18a78e02014-07-10 07:31:46 -070032#include "minadbd/fuse_adb_provider.h"
33#include "fuse_sideload.h"
Doug Zongker9270a202012-01-09 15:16:13 -080034
Elliott Hughescb220402016-09-23 15:30:55 -070035#include <android-base/properties.h>
36
Elliott Hughes59bf0da2016-06-15 15:14:04 -070037static void set_usb_driver(RecoveryUI* ui, bool enabled) {
Doug Zongker9270a202012-01-09 15:16:13 -080038 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
39 if (fd < 0) {
40 ui->Print("failed to open driver control: %s\n", strerror(errno));
41 return;
42 }
Elliott Hughes7bad7c42015-04-28 17:24:24 -070043 if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) {
Doug Zongker9270a202012-01-09 15:16:13 -080044 ui->Print("failed to set driver control: %s\n", strerror(errno));
45 }
46 if (close(fd) < 0) {
47 ui->Print("failed to close driver control: %s\n", strerror(errno));
48 }
49}
50
Elliott Hughes59bf0da2016-06-15 15:14:04 -070051static void stop_adbd(RecoveryUI* ui) {
52 ui->Print("Stopping adbd...\n");
Elliott Hughescb220402016-09-23 15:30:55 -070053 android::base::SetProperty("ctl.stop", "adbd");
Elliott Hughes59bf0da2016-06-15 15:14:04 -070054 set_usb_driver(ui, false);
Doug Zongker9270a202012-01-09 15:16:13 -080055}
56
Elliott Hughes59bf0da2016-06-15 15:14:04 -070057static void maybe_restart_adbd(RecoveryUI* ui) {
Elliott Hughesf14af802015-02-10 14:46:14 -080058 if (is_ro_debuggable()) {
Doug Zongker9270a202012-01-09 15:16:13 -080059 ui->Print("Restarting adbd...\n");
Elliott Hughes59bf0da2016-06-15 15:14:04 -070060 set_usb_driver(ui, true);
Elliott Hughescb220402016-09-23 15:30:55 -070061 android::base::SetProperty("ctl.start", "adbd");
Doug Zongker9270a202012-01-09 15:16:13 -080062 }
63}
64
Doug Zongker075ad802014-06-26 15:35:51 -070065// How long (in seconds) we wait for the host to start sending us a
66// package, before timing out.
67#define ADB_INSTALL_TIMEOUT 300
68
Elliott Hughes59bf0da2016-06-15 15:14:04 -070069int apply_from_adb(RecoveryUI* ui, bool* wipe_cache, const char* install_file) {
Tao Bao682c34b2015-04-07 17:16:35 -070070 modified_flash = true;
71
Elliott Hughes59bf0da2016-06-15 15:14:04 -070072 stop_adbd(ui);
73 set_usb_driver(ui, true);
Doug Zongker9270a202012-01-09 15:16:13 -080074
75 ui->Print("\n\nNow send the package you want to apply\n"
76 "to the device with \"adb sideload <filename>\"...\n");
77
78 pid_t child;
79 if ((child = fork()) == 0) {
80 execl("/sbin/recovery", "recovery", "--adbd", NULL);
Tao Bao3da88012017-02-03 13:09:23 -080081 _exit(EXIT_FAILURE);
Doug Zongker9270a202012-01-09 15:16:13 -080082 }
Doug Zongker075ad802014-06-26 15:35:51 -070083
Doug Zongker18a78e02014-07-10 07:31:46 -070084 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host
Doug Zongker075ad802014-06-26 15:35:51 -070085 // connects and starts serving a package. Poll for its
86 // appearance. (Note that inotify doesn't work with FUSE.)
Tao Bao80e46e02015-06-03 10:49:29 -070087 int result = INSTALL_ERROR;
Doug Zongker9270a202012-01-09 15:16:13 -080088 int status;
Doug Zongker075ad802014-06-26 15:35:51 -070089 bool waited = false;
90 struct stat st;
91 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) {
92 if (waitpid(child, &status, WNOHANG) != 0) {
93 result = INSTALL_ERROR;
94 waited = true;
95 break;
96 }
97
Doug Zongker18a78e02014-07-10 07:31:46 -070098 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
Doug Zongker075ad802014-06-26 15:35:51 -070099 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT-1) {
100 sleep(1);
101 continue;
102 } else {
Elliott Hughes018ed312015-04-08 16:51:36 -0700103 ui->Print("\nTimed out waiting for package.\n\n");
Doug Zongker075ad802014-06-26 15:35:51 -0700104 result = INSTALL_ERROR;
105 kill(child, SIGKILL);
106 break;
107 }
108 }
Tianjie Xu16255832016-04-30 11:49:59 -0700109 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false, 0);
Doug Zongker075ad802014-06-26 15:35:51 -0700110 break;
111 }
112
113 if (!waited) {
114 // Calling stat() on this magic filename signals the minadbd
115 // subprocess to shut down.
Doug Zongker18a78e02014-07-10 07:31:46 -0700116 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
Doug Zongker075ad802014-06-26 15:35:51 -0700117
118 // TODO(dougz): there should be a way to cancel waiting for a
119 // package (by pushing some button combo on the device). For now
120 // you just have to 'adb sideload' a file that's not a valid
121 // package, like "/dev/null".
122 waitpid(child, &status, 0);
123 }
124
Doug Zongker9270a202012-01-09 15:16:13 -0800125 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker075ad802014-06-26 15:35:51 -0700126 if (WEXITSTATUS(status) == 3) {
127 ui->Print("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
128 } else if (!WIFSIGNALED(status)) {
129 ui->Print("\n(adbd status %d)\n", WEXITSTATUS(status));
130 }
Doug Zongker9270a202012-01-09 15:16:13 -0800131 }
132
Elliott Hughes59bf0da2016-06-15 15:14:04 -0700133 set_usb_driver(ui, false);
134 maybe_restart_adbd(ui);
Doug Zongker9270a202012-01-09 15:16:13 -0800135
Doug Zongker075ad802014-06-26 15:35:51 -0700136 return result;
Doug Zongker9270a202012-01-09 15:16:13 -0800137}