blob: 12bce1cca8d46e3b55093e97b7f80de0e8aa9784 [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"
36int TWinstall_zip(const char* path, int* wipe_cache);
Doug Zongker9270a202012-01-09 15:16:13 -080037}
38
39static RecoveryUI* ui = NULL;
40
41static void
42set_usb_driver(bool enabled) {
43 int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
44 if (fd < 0) {
45 ui->Print("failed to open driver control: %s\n", strerror(errno));
46 return;
47 }
48 if (write(fd, enabled ? "1" : "0", 1) < 0) {
49 ui->Print("failed to set driver control: %s\n", strerror(errno));
50 }
51 if (close(fd) < 0) {
52 ui->Print("failed to close driver control: %s\n", strerror(errno));
53 }
54}
55
56static void
57stop_adbd() {
58 property_set("ctl.stop", "adbd");
59 set_usb_driver(false);
60}
61
62
63static void
64maybe_restart_adbd() {
65 char value[PROPERTY_VALUE_MAX+1];
66 int len = property_get("ro.debuggable", value, NULL);
67 if (len == 1 && value[0] == '1') {
68 ui->Print("Restarting adbd...\n");
69 set_usb_driver(true);
70 property_set("ctl.start", "adbd");
71 }
72}
73
74int
75apply_from_adb(RecoveryUI* ui_, int* wipe_cache, const char* install_file) {
76 ui = ui_;
77
78 stop_adbd();
79 set_usb_driver(true);
80
81 ui->Print("\n\nNow send the package you want to apply\n"
82 "to the device with \"adb sideload <filename>\"...\n");
83
84 pid_t child;
85 if ((child = fork()) == 0) {
86 execl("/sbin/recovery", "recovery", "--adbd", NULL);
87 _exit(-1);
88 }
89 int status;
90 // TODO(dougz): there should be a way to cancel waiting for a
91 // package (by pushing some button combo on the device). For now
92 // you just have to 'adb sideload' a file that's not a valid
93 // package, like "/dev/null".
94 waitpid(child, &status, 0);
95 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
96 ui->Print("status %d\n", WEXITSTATUS(status));
97 }
98
99 set_usb_driver(false);
100 maybe_restart_adbd();
101
102 struct stat st;
103 if (stat(ADB_SIDELOAD_FILENAME, &st) != 0) {
104 if (errno == ENOENT) {
105 ui->Print("No package received.\n");
106 } else {
107 ui->Print("Error reading package:\n %s\n", strerror(errno));
108 }
109 return INSTALL_ERROR;
110 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400111 return TWinstall_zip(ADB_SIDELOAD_FILENAME, wipe_cache);
Doug Zongker9270a202012-01-09 15:16:13 -0800112}