blob: 5c54468d43884b04fe106bab95af79b576e7cfe9 [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>
Dees_Troy2673cec2013-04-02 20:22:16 +000027#include <stdio.h>
Doug Zongker9270a202012-01-09 15:16:13 -080028
29#include "ui.h"
30#include "cutils/properties.h"
Doug Zongker9270a202012-01-09 15:16:13 -080031#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
35static RecoveryUI* ui = NULL;
36
Ethan Yonker24813422014-11-07 17:19:07 -060037void
Doug Zongker9270a202012-01-09 15:16:13 -080038set_usb_driver(bool enabled) {
39 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
40 if (fd < 0) {
Dees_Troye00c83a2012-09-21 10:00:52 -040041/* These error messages show when built in older Android branches (e.g. Gingerbread)
42 It's not a critical error so we're disabling the error messages.
Doug Zongker9270a202012-01-09 15:16:13 -080043 ui->Print("failed to open driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040044*/
Dees_Troy2673cec2013-04-02 20:22:16 +000045 printf("failed to open driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080046 return;
47 }
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050048
Elliott Hughes2f5feed2015-04-28 17:24:24 -070049 if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) {
Dees_Troye00c83a2012-09-21 10:00:52 -040050/*
Doug Zongker9270a202012-01-09 15:16:13 -080051 ui->Print("failed to set driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040052*/
Dees_Troy2673cec2013-04-02 20:22:16 +000053 printf("failed to set driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080054 }
55 if (close(fd) < 0) {
Dees_Troye00c83a2012-09-21 10:00:52 -040056/*
Doug Zongker9270a202012-01-09 15:16:13 -080057 ui->Print("failed to close driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040058*/
Dees_Troy2673cec2013-04-02 20:22:16 +000059 printf("failed to close driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080060 }
61}
62
63static void
64stop_adbd() {
65 property_set("ctl.stop", "adbd");
66 set_usb_driver(false);
67}
68
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050069bool is_ro_debuggable() {
70 char value[PROPERTY_VALUE_MAX+1];
71 return (property_get("ro.debuggable", value, NULL) == 1 && value[0] == '1');
72}
Doug Zongker9270a202012-01-09 15:16:13 -080073
Ethan Yonker24813422014-11-07 17:19:07 -060074void
Doug Zongker9270a202012-01-09 15:16:13 -080075maybe_restart_adbd() {
76 char value[PROPERTY_VALUE_MAX+1];
Elliott Hughesf14af802015-02-10 14:46:14 -080077 if (is_ro_debuggable()) {
Dees_Troy2673cec2013-04-02 20:22:16 +000078 printf("Restarting adbd...\n");
Doug Zongker9270a202012-01-09 15:16:13 -080079 set_usb_driver(true);
80 property_set("ctl.start", "adbd");
81 }
82}
83
Doug Zongker075ad802014-06-26 15:35:51 -070084// How long (in seconds) we wait for the host to start sending us a
85// package, before timing out.
86#define ADB_INSTALL_TIMEOUT 300
87
Doug Zongker9270a202012-01-09 15:16:13 -080088int
thatcc8ddca2015-01-03 01:59:36 +010089apply_from_adb(const char* install_file, pid_t* child_pid) {
Doug Zongker9270a202012-01-09 15:16:13 -080090
91 stop_adbd();
92 set_usb_driver(true);
Dees_Troy2673cec2013-04-02 20:22:16 +000093/*
Doug Zongker9270a202012-01-09 15:16:13 -080094 ui->Print("\n\nNow send the package you want to apply\n"
95 "to the device with \"adb sideload <filename>\"...\n");
Dees_Troy2673cec2013-04-02 20:22:16 +000096*/
Doug Zongker9270a202012-01-09 15:16:13 -080097 pid_t child;
98 if ((child = fork()) == 0) {
Dees_Troy9a4b5692012-09-19 15:09:45 -040099 execl("/sbin/recovery", "recovery", "--adbd", install_file, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800100 _exit(-1);
101 }
Doug Zongker075ad802014-06-26 15:35:51 -0700102
thatcc8ddca2015-01-03 01:59:36 +0100103 *child_pid = child;
104 // caller can now kill the child thread from another thread
Ethan Yonkera1674162014-11-06 08:35:10 -0600105
Doug Zongker18a78e02014-07-10 07:31:46 -0700106 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host
Doug Zongker075ad802014-06-26 15:35:51 -0700107 // connects and starts serving a package. Poll for its
108 // appearance. (Note that inotify doesn't work with FUSE.)
109 int result;
Doug Zongker9270a202012-01-09 15:16:13 -0800110 int status;
Doug Zongker075ad802014-06-26 15:35:51 -0700111 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 Yonker26860092014-11-06 09:49:25 -0600115 result = -1;
Doug Zongker075ad802014-06-26 15:35:51 -0700116 waited = true;
117 break;
118 }
119
Doug Zongker18a78e02014-07-10 07:31:46 -0700120 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
Doug Zongker075ad802014-06-26 15:35:51 -0700121 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT-1) {
122 sleep(1);
123 continue;
124 } else {
thatcc8ddca2015-01-03 01:59:36 +0100125 printf("\nTimed out waiting for package: %s\n\n", strerror(errno));
Ethan Yonker26860092014-11-06 09:49:25 -0600126 result = -1;
Doug Zongker075ad802014-06-26 15:35:51 -0700127 kill(child, SIGKILL);
128 break;
129 }
130 }
Ethan Yonker24813422014-11-07 17:19:07 -0600131 // Install is handled elsewhere in TWRP
thatcc8ddca2015-01-03 01:59:36 +0100132 //install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false);
133 return 0;
Doug Zongker075ad802014-06-26 15:35:51 -0700134 }
135
thatcc8ddca2015-01-03 01:59:36 +0100136 // if we got here, something failed
137 *child_pid = 0;
138
139 if (!waited) {
Doug Zongker075ad802014-06-26 15:35:51 -0700140 // Calling stat() on this magic filename signals the minadbd
141 // subprocess to shut down.
Doug Zongker18a78e02014-07-10 07:31:46 -0700142 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
Doug Zongker075ad802014-06-26 15:35:51 -0700143
144 // TODO(dougz): there should be a way to cancel waiting for a
145 // package (by pushing some button combo on the device). For now
146 // you just have to 'adb sideload' a file that's not a valid
147 // package, like "/dev/null".
148 waitpid(child, &status, 0);
thatcc8ddca2015-01-03 01:59:36 +0100149 }
Doug Zongker075ad802014-06-26 15:35:51 -0700150
thatcc8ddca2015-01-03 01:59:36 +0100151 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
152 if (WEXITSTATUS(status) == 3) {
153 printf("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
154 result = -2;
155 } else if (!WIFSIGNALED(status)) {
156 printf("status %d\n", WEXITSTATUS(status));
Doug Zongker075ad802014-06-26 15:35:51 -0700157 }
thatcc8ddca2015-01-03 01:59:36 +0100158 }
Doug Zongker9270a202012-01-09 15:16:13 -0800159
thatcc8ddca2015-01-03 01:59:36 +0100160 set_usb_driver(false);
161 maybe_restart_adbd();
162
163 return result;
Doug Zongker9270a202012-01-09 15:16:13 -0800164}