blob: 63eb967e193c10577821a7aa2eeb9a8699f1d479 [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"
29#include "cutils/properties.h"
30#include "install.h"
31#include "common.h"
32#include "adb_install.h"
33extern "C" {
34#include "minadbd/adb.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040035#include "twinstall.h"
Dees_Troycfb63ae2012-09-19 14:30:17 -040036#include "data.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040037int TWinstall_zip(const char* path, int* wipe_cache);
Doug Zongker9270a202012-01-09 15:16:13 -080038}
39
40static RecoveryUI* ui = NULL;
41
42static void
43set_usb_driver(bool enabled) {
44 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
45 if (fd < 0) {
Dees_Troye00c83a2012-09-21 10:00:52 -040046/* These error messages show when built in older Android branches (e.g. Gingerbread)
47 It's not a critical error so we're disabling the error messages.
Doug Zongker9270a202012-01-09 15:16:13 -080048 ui->Print("failed to open driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040049*/
50 LOGI("failed to open driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080051 return;
52 }
53 if (write(fd, enabled ? "1" : "0", 1) < 0) {
Dees_Troye00c83a2012-09-21 10:00:52 -040054/*
Doug Zongker9270a202012-01-09 15:16:13 -080055 ui->Print("failed to set driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040056*/
57 LOGI("failed to set driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080058 }
59 if (close(fd) < 0) {
Dees_Troye00c83a2012-09-21 10:00:52 -040060/*
Doug Zongker9270a202012-01-09 15:16:13 -080061 ui->Print("failed to close driver control: %s\n", strerror(errno));
Dees_Troye00c83a2012-09-21 10:00:52 -040062*/
63 LOGI("failed to close driver control: %s\n", strerror(errno));
Doug Zongker9270a202012-01-09 15:16:13 -080064 }
65}
66
67static void
68stop_adbd() {
69 property_set("ctl.stop", "adbd");
70 set_usb_driver(false);
71}
72
73
74static void
75maybe_restart_adbd() {
76 char value[PROPERTY_VALUE_MAX+1];
77 int len = property_get("ro.debuggable", value, NULL);
78 if (len == 1 && value[0] == '1') {
79 ui->Print("Restarting adbd...\n");
80 set_usb_driver(true);
81 property_set("ctl.start", "adbd");
82 }
83}
84
85int
86apply_from_adb(RecoveryUI* ui_, int* wipe_cache, const char* install_file) {
87 ui = ui_;
88
89 stop_adbd();
90 set_usb_driver(true);
91
92 ui->Print("\n\nNow send the package you want to apply\n"
93 "to the device with \"adb sideload <filename>\"...\n");
94
95 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 }
Dees_Troycfb63ae2012-09-19 14:30:17 -0400100 DataManager_SetIntValue("tw_child_pid", child);
Doug Zongker9270a202012-01-09 15:16:13 -0800101 int status;
102 // TODO(dougz): there should be a way to cancel waiting for a
103 // package (by pushing some button combo on the device). For now
104 // you just have to 'adb sideload' a file that's not a valid
105 // package, like "/dev/null".
106 waitpid(child, &status, 0);
107 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
108 ui->Print("status %d\n", WEXITSTATUS(status));
109 }
110
111 set_usb_driver(false);
112 maybe_restart_adbd();
113
114 struct stat st;
Dees_Troy9a4b5692012-09-19 15:09:45 -0400115 if (stat(install_file, &st) != 0) {
Doug Zongker9270a202012-01-09 15:16:13 -0800116 if (errno == ENOENT) {
117 ui->Print("No package received.\n");
118 } else {
119 ui->Print("Error reading package:\n %s\n", strerror(errno));
120 }
121 return INSTALL_ERROR;
122 }
Dees_Troy9a4b5692012-09-19 15:09:45 -0400123 return TWinstall_zip(install_file, wipe_cache);
Doug Zongker9270a202012-01-09 15:16:13 -0800124}