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> |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 28 | |
| 29 | #include "ui.h" |
| 30 | #include "cutils/properties.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 31 | #include "adb_install.h" |
| 32 | extern "C" { |
| 33 | #include "minadbd/adb.h" |
| 34 | } |
| 35 | |
| 36 | static RecoveryUI* ui = NULL; |
| 37 | |
| 38 | static void |
| 39 | set_usb_driver(bool enabled) { |
| 40 | int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY); |
| 41 | if (fd < 0) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 42 | /* 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 Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 44 | ui->Print("failed to open driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 45 | */ |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 46 | printf("failed to open driver control: %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 47 | return; |
| 48 | } |
| 49 | if (write(fd, enabled ? "1" : "0", 1) < 0) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 50 | /* |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 51 | ui->Print("failed to set driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 52 | */ |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 53 | printf("failed to set driver control: %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 54 | } |
| 55 | if (close(fd) < 0) { |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 56 | /* |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 57 | ui->Print("failed to close driver control: %s\n", strerror(errno)); |
Dees_Troy | e00c83a | 2012-09-21 10:00:52 -0400 | [diff] [blame] | 58 | */ |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 59 | printf("failed to close driver control: %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | stop_adbd() { |
| 65 | property_set("ctl.stop", "adbd"); |
| 66 | set_usb_driver(false); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | static void |
| 71 | maybe_restart_adbd() { |
| 72 | char value[PROPERTY_VALUE_MAX+1]; |
| 73 | int len = property_get("ro.debuggable", value, NULL); |
| 74 | if (len == 1 && value[0] == '1') { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 75 | printf("Restarting adbd...\n"); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 76 | set_usb_driver(true); |
| 77 | property_set("ctl.start", "adbd"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | int |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 82 | apply_from_adb(const char* install_file) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 83 | |
| 84 | stop_adbd(); |
| 85 | set_usb_driver(true); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 86 | /* |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 87 | ui->Print("\n\nNow send the package you want to apply\n" |
| 88 | "to the device with \"adb sideload <filename>\"...\n"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 89 | */ |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 90 | pid_t child; |
| 91 | if ((child = fork()) == 0) { |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 92 | execl("/sbin/recovery", "recovery", "--adbd", install_file, NULL); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 93 | _exit(-1); |
| 94 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 95 | char child_prop[PROPERTY_VALUE_MAX]; |
| 96 | sprintf(child_prop, "%i", child); |
| 97 | property_set("tw_child_pid", child_prop); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 98 | int status; |
| 99 | // TODO(dougz): there should be a way to cancel waiting for a |
| 100 | // package (by pushing some button combo on the device). For now |
| 101 | // you just have to 'adb sideload' a file that's not a valid |
| 102 | // package, like "/dev/null". |
| 103 | waitpid(child, &status, 0); |
| 104 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 105 | printf("status %d\n", WEXITSTATUS(status)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 106 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 107 | set_usb_driver(false); |
| 108 | maybe_restart_adbd(); |
| 109 | |
| 110 | struct stat st; |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 111 | if (stat(install_file, &st) != 0) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 112 | if (errno == ENOENT) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 113 | printf("No package received.\n"); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 114 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 115 | printf("Error reading package:\n %s\n", strerror(errno)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 116 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 117 | return -1; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 118 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 119 | return 0; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 120 | } |