blob: d9936f22235ec188ef384c8be1792130006b09aa [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"
Ethan Yonker34ae4832016-08-24 15:32:18 -050034#include "verifier.h"
Doug Zongker9270a202012-01-09 15:16:13 -080035
36static RecoveryUI* ui = NULL;
37
Ethan Yonker24813422014-11-07 17:19:07 -060038void
Doug Zongker9270a202012-01-09 15:16:13 -080039set_usb_driver(bool enabled) {
40 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
41 if (fd < 0) {
Dees_Troye00c83a2012-09-21 10:00:52 -040042/* These error messages show when built in older Android branches (e.g. Gingerbread)
43 It's not a critical error so we're disabling the error messages.
Doug Zongker9270a202012-01-09 15:16:13 -080044 ui->Print("failed to open driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040045*/
Dees_Troy2673cec2013-04-02 20:22:16 +000046 printf("failed to open driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080047 return;
48 }
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050049
Elliott Hughes2f5feed2015-04-28 17:24:24 -070050 if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) {
Dees_Troye00c83a2012-09-21 10:00:52 -040051/*
Doug Zongker9270a202012-01-09 15:16:13 -080052 ui->Print("failed to set driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040053*/
Dees_Troy2673cec2013-04-02 20:22:16 +000054 printf("failed to set driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080055 }
56 if (close(fd) < 0) {
Dees_Troye00c83a2012-09-21 10:00:52 -040057/*
Doug Zongker9270a202012-01-09 15:16:13 -080058 ui->Print("failed to close driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040059*/
Dees_Troy2673cec2013-04-02 20:22:16 +000060 printf("failed to close driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080061 }
62}
63
64static void
65stop_adbd() {
66 property_set("ctl.stop", "adbd");
67 set_usb_driver(false);
68}
69
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050070bool is_ro_debuggable() {
71 char value[PROPERTY_VALUE_MAX+1];
72 return (property_get("ro.debuggable", value, NULL) == 1 && value[0] == '1');
73}
Doug Zongker9270a202012-01-09 15:16:13 -080074
Ethan Yonker24813422014-11-07 17:19:07 -060075void
Doug Zongker9270a202012-01-09 15:16:13 -080076maybe_restart_adbd() {
77 char value[PROPERTY_VALUE_MAX+1];
Elliott Hughesf14af802015-02-10 14:46:14 -080078 if (is_ro_debuggable()) {
Dees_Troy2673cec2013-04-02 20:22:16 +000079 printf("Restarting adbd...\n");
Doug Zongker9270a202012-01-09 15:16:13 -080080 set_usb_driver(true);
81 property_set("ctl.start", "adbd");
82 }
83}
84
Doug Zongker075ad802014-06-26 15:35:51 -070085// How long (in seconds) we wait for the host to start sending us a
86// package, before timing out.
87#define ADB_INSTALL_TIMEOUT 300
88
Doug Zongker9270a202012-01-09 15:16:13 -080089int
thatcc8ddca2015-01-03 01:59:36 +010090apply_from_adb(const char* install_file, pid_t* child_pid) {
Doug Zongker9270a202012-01-09 15:16:13 -080091
92 stop_adbd();
93 set_usb_driver(true);
Dees_Troy2673cec2013-04-02 20:22:16 +000094/*
Doug Zongker9270a202012-01-09 15:16:13 -080095 ui->Print("\n\nNow send the package you want to apply\n"
96 "to the device with \"adb sideload <filename>\"...\n");
Dees_Troy2673cec2013-04-02 20:22:16 +000097*/
Doug Zongker9270a202012-01-09 15:16:13 -080098 pid_t child;
99 if ((child = fork()) == 0) {
Dees_Troy9a4b5692012-09-19 15:09:45 -0400100 execl("/sbin/recovery", "recovery", "--adbd", install_file, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800101 _exit(-1);
102 }
Doug Zongker075ad802014-06-26 15:35:51 -0700103
thatcc8ddca2015-01-03 01:59:36 +0100104 *child_pid = child;
105 // caller can now kill the child thread from another thread
Ethan Yonkera1674162014-11-06 08:35:10 -0600106
Doug Zongker18a78e02014-07-10 07:31:46 -0700107 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host
Doug Zongker075ad802014-06-26 15:35:51 -0700108 // connects and starts serving a package. Poll for its
109 // appearance. (Note that inotify doesn't work with FUSE.)
Tao Bao80e46e02015-06-03 10:49:29 -0700110 int result = INSTALL_ERROR;
Doug Zongker9270a202012-01-09 15:16:13 -0800111 int status;
Doug Zongker075ad802014-06-26 15:35:51 -0700112 bool waited = false;
113 struct stat st;
114 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) {
115 if (waitpid(child, &status, WNOHANG) != 0) {
Ethan Yonker26860092014-11-06 09:49:25 -0600116 result = -1;
Doug Zongker075ad802014-06-26 15:35:51 -0700117 waited = true;
118 break;
119 }
120
Doug Zongker18a78e02014-07-10 07:31:46 -0700121 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
Doug Zongker075ad802014-06-26 15:35:51 -0700122 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT-1) {
123 sleep(1);
124 continue;
125 } else {
thatcc8ddca2015-01-03 01:59:36 +0100126 printf("\nTimed out waiting for package: %s\n\n", strerror(errno));
Ethan Yonker26860092014-11-06 09:49:25 -0600127 result = -1;
Doug Zongker075ad802014-06-26 15:35:51 -0700128 kill(child, SIGKILL);
129 break;
130 }
131 }
Ethan Yonker24813422014-11-07 17:19:07 -0600132 // Install is handled elsewhere in TWRP
thatcc8ddca2015-01-03 01:59:36 +0100133 //install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false);
134 return 0;
Doug Zongker075ad802014-06-26 15:35:51 -0700135 }
136
thatcc8ddca2015-01-03 01:59:36 +0100137 // if we got here, something failed
138 *child_pid = 0;
139
140 if (!waited) {
Doug Zongker075ad802014-06-26 15:35:51 -0700141 // Calling stat() on this magic filename signals the minadbd
142 // subprocess to shut down.
Doug Zongker18a78e02014-07-10 07:31:46 -0700143 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
Doug Zongker075ad802014-06-26 15:35:51 -0700144
145 // TODO(dougz): there should be a way to cancel waiting for a
146 // package (by pushing some button combo on the device). For now
147 // you just have to 'adb sideload' a file that's not a valid
148 // package, like "/dev/null".
149 waitpid(child, &status, 0);
thatcc8ddca2015-01-03 01:59:36 +0100150 }
Doug Zongker075ad802014-06-26 15:35:51 -0700151
thatcc8ddca2015-01-03 01:59:36 +0100152 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
153 if (WEXITSTATUS(status) == 3) {
154 printf("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
155 result = -2;
156 } else if (!WIFSIGNALED(status)) {
157 printf("status %d\n", WEXITSTATUS(status));
Doug Zongker075ad802014-06-26 15:35:51 -0700158 }
thatcc8ddca2015-01-03 01:59:36 +0100159 }
Doug Zongker9270a202012-01-09 15:16:13 -0800160
thatcc8ddca2015-01-03 01:59:36 +0100161 set_usb_driver(false);
162 maybe_restart_adbd();
163
164 return result;
Doug Zongker9270a202012-01-09 15:16:13 -0800165}