blob: e10cb4a2e44b1203c77ce37b57c93b3a1bcfdb1d [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"
32extern "C" {
Doug Zongker18a78e02014-07-10 07:31:46 -070033#include "minadbd/fuse_adb_provider.h"
34#include "fuse_sideload.h"
Doug Zongker9270a202012-01-09 15:16:13 -080035}
36
37static RecoveryUI* ui = NULL;
38
39static void
40set_usb_driver(bool enabled) {
41 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
42 if (fd < 0) {
Dees_Troye00c83a2012-09-21 10:00:52 -040043/* These error messages show when built in older Android branches (e.g. Gingerbread)
44 It's not a critical error so we're disabling the error messages.
Doug Zongker9270a202012-01-09 15:16:13 -080045 ui->Print("failed to open driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040046*/
Dees_Troy2673cec2013-04-02 20:22:16 +000047 printf("failed to open driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080048 return;
49 }
50 if (write(fd, enabled ? "1" : "0", 1) < 0) {
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
70
71static void
72maybe_restart_adbd() {
73 char value[PROPERTY_VALUE_MAX+1];
74 int len = property_get("ro.debuggable", value, NULL);
75 if (len == 1 && value[0] == '1') {
Dees_Troy2673cec2013-04-02 20:22:16 +000076 printf("Restarting adbd...\n");
Doug Zongker9270a202012-01-09 15:16:13 -080077 set_usb_driver(true);
78 property_set("ctl.start", "adbd");
79 }
80}
81
Doug Zongker075ad802014-06-26 15:35:51 -070082// How long (in seconds) we wait for the host to start sending us a
83// package, before timing out.
84#define ADB_INSTALL_TIMEOUT 300
85
Doug Zongker9270a202012-01-09 15:16:13 -080086int
Dees_Troy2673cec2013-04-02 20:22:16 +000087apply_from_adb(const char* install_file) {
Doug Zongker9270a202012-01-09 15:16:13 -080088
89 stop_adbd();
90 set_usb_driver(true);
Dees_Troy2673cec2013-04-02 20:22:16 +000091/*
Doug Zongker9270a202012-01-09 15:16:13 -080092 ui->Print("\n\nNow send the package you want to apply\n"
93 "to the device with \"adb sideload <filename>\"...\n");
Dees_Troy2673cec2013-04-02 20:22:16 +000094*/
Doug Zongker9270a202012-01-09 15:16:13 -080095 pid_t child;
96 if ((child = fork()) == 0) {
Dees_Troy9a4b5692012-09-19 15:09:45 -040097 execl("/sbin/recovery", "recovery", "--adbd", install_file, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -080098 _exit(-1);
99 }
Doug Zongker075ad802014-06-26 15:35:51 -0700100
Dees_Troy2673cec2013-04-02 20:22:16 +0000101 char child_prop[PROPERTY_VALUE_MAX];
102 sprintf(child_prop, "%i", child);
103 property_set("tw_child_pid", child_prop);
Ethan Yonkera1674162014-11-06 08:35:10 -0600104
Doug Zongker18a78e02014-07-10 07:31:46 -0700105 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host
Doug Zongker075ad802014-06-26 15:35:51 -0700106 // connects and starts serving a package. Poll for its
107 // appearance. (Note that inotify doesn't work with FUSE.)
108 int result;
Doug Zongker9270a202012-01-09 15:16:13 -0800109 int status;
Doug Zongker075ad802014-06-26 15:35:51 -0700110 bool waited = false;
111 struct stat st;
112 for (int i = 0; i < ADB_INSTALL_TIMEOUT; ++i) {
113 if (waitpid(child, &status, WNOHANG) != 0) {
114 result = INSTALL_ERROR;
115 waited = true;
116 break;
117 }
118
Doug Zongker18a78e02014-07-10 07:31:46 -0700119 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
Doug Zongker075ad802014-06-26 15:35:51 -0700120 if (errno == ENOENT && i < ADB_INSTALL_TIMEOUT-1) {
121 sleep(1);
122 continue;
123 } else {
124 ui->Print("\nTimed out waiting for package.\n\n", strerror(errno));
125 result = INSTALL_ERROR;
126 kill(child, SIGKILL);
127 break;
128 }
129 }
Doug Zongker18a78e02014-07-10 07:31:46 -0700130 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false);
Doug Zongker075ad802014-06-26 15:35:51 -0700131 break;
132 }
133
134 if (!waited) {
135 // Calling stat() on this magic filename signals the minadbd
136 // subprocess to shut down.
Doug Zongker18a78e02014-07-10 07:31:46 -0700137 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
Doug Zongker075ad802014-06-26 15:35:51 -0700138
139 // TODO(dougz): there should be a way to cancel waiting for a
140 // package (by pushing some button combo on the device). For now
141 // you just have to 'adb sideload' a file that's not a valid
142 // package, like "/dev/null".
143 waitpid(child, &status, 0);
144 }
145
Doug Zongker9270a202012-01-09 15:16:13 -0800146 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker075ad802014-06-26 15:35:51 -0700147 if (WEXITSTATUS(status) == 3) {
Ethan Yonkera1674162014-11-06 08:35:10 -0600148 printf("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
Doug Zongker075ad802014-06-26 15:35:51 -0700149 } else if (!WIFSIGNALED(status)) {
Ethan Yonkera1674162014-11-06 08:35:10 -0600150 printf("status %d\n", WEXITSTATUS(status));
Doug Zongker075ad802014-06-26 15:35:51 -0700151 }
Doug Zongker9270a202012-01-09 15:16:13 -0800152 }
Doug Zongker9270a202012-01-09 15:16:13 -0800153 set_usb_driver(false);
154 maybe_restart_adbd();
155
Dees_Troy9a4b5692012-09-19 15:09:45 -0400156 if (stat(install_file, &st) != 0) {
Doug Zongker9270a202012-01-09 15:16:13 -0800157 if (errno == ENOENT) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000158 printf("No package received.\n");
Doug Zongker9270a202012-01-09 15:16:13 -0800159 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000160 printf("Error reading package:\n %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -0800161 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000162 return -1;
Doug Zongker9270a202012-01-09 15:16:13 -0800163 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000164 return 0;
Doug Zongker9270a202012-01-09 15:16:13 -0800165}