Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 1 | /* |
| 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" |
| 33 | extern "C" { |
| 34 | #include "minadbd/adb.h" |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 35 | #include "twinstall.h" |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 36 | #include "data.h" |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 37 | int TWinstall_zip(const char* path, int* wipe_cache); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static RecoveryUI* ui = NULL; |
| 41 | |
| 42 | static void |
| 43 | set_usb_driver(bool enabled) { |
| 44 | int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY); |
| 45 | if (fd < 0) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 46 | /* 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 Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 48 | ui->Print("failed to open driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 49 | */ |
| 50 | LOGI("failed to open driver control: %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | if (write(fd, enabled ? "1" : "0", 1) < 0) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 54 | /* |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 55 | ui->Print("failed to set driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 56 | */ |
| 57 | LOGI("failed to set driver control: %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 58 | } |
| 59 | if (close(fd) < 0) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 60 | /* |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 61 | ui->Print("failed to close driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 62 | */ |
| 63 | LOGI("failed to close driver control: %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | static void |
| 68 | stop_adbd() { |
| 69 | property_set("ctl.stop", "adbd"); |
| 70 | set_usb_driver(false); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | static void |
| 75 | maybe_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 | |
| 85 | int |
| 86 | apply_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_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 97 | execl("/sbin/recovery", "recovery", "--adbd", install_file, NULL); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 98 | _exit(-1); |
| 99 | } |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 100 | DataManager_SetIntValue("tw_child_pid", child); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 101 | 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 | } |
Dees_Troy | 2691f9d | 2012-09-24 11:15:49 -0400 | [diff] [blame] | 110 | DataManager_SetIntValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 111 | set_usb_driver(false); |
| 112 | maybe_restart_adbd(); |
| 113 | |
| 114 | struct stat st; |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 115 | if (stat(install_file, &st) != 0) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 116 | 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_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 123 | return TWinstall_zip(install_file, wipe_cache); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 124 | } |