The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 <ctype.h> |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 18 | #include <dirent.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <getopt.h> |
| 22 | #include <limits.h> |
| 23 | #include <linux/input.h> |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 24 | #include <stdarg.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 28 | #include <sys/stat.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <time.h> |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | #include "bootloader.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | #include "common.h" |
| 35 | #include "cutils/properties.h" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 36 | #ifdef ANDROID_RB_RESTART |
Ken Sumrall | 6e4472a | 2011-03-07 23:37:27 -0800 | [diff] [blame] | 37 | #include "cutils/android_reboot.h" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 38 | #else |
| 39 | #include <sys/reboot.h> |
| 40 | #endif |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 41 | #include "install.h" |
| 42 | #include "minui/minui.h" |
| 43 | #include "minzip/DirUtil.h" |
| 44 | #include "roots.h" |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 45 | #include "ui.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 46 | #include "screen_ui.h" |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 47 | #include "device.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 48 | #include "adb_install.h" |
| 49 | extern "C" { |
| 50 | #include "minadbd/adb.h" |
| 51 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 52 | |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 53 | extern "C" { |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 54 | #include "data.h" |
| 55 | #include "gui/gui.h" |
| 56 | } |
| 57 | #include "partitions.hpp" |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 58 | #include "variables.h" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 59 | #include "openrecoveryscript.hpp" |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 60 | #include "twrp-functions.hpp" |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 61 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 62 | TWPartitionManager PartitionManager; |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 63 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 64 | struct selabel_handle *sehandle; |
| 65 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 66 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 67 | static const struct option OPTIONS[] = { |
| 68 | { "send_intent", required_argument, NULL, 's' }, |
| 69 | { "update_package", required_argument, NULL, 'u' }, |
| 70 | { "wipe_data", no_argument, NULL, 'w' }, |
| 71 | { "wipe_cache", no_argument, NULL, 'c' }, |
Doug Zongker | 4bc9806 | 2010-09-03 11:00:13 -0700 | [diff] [blame] | 72 | { "show_text", no_argument, NULL, 't' }, |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 73 | { "just_exit", no_argument, NULL, 'x' }, |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 74 | { "locale", required_argument, NULL, 'l' }, |
Doug Zongker | 988500b | 2009-10-06 14:41:38 -0700 | [diff] [blame] | 75 | { NULL, 0, NULL, 0 }, |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
Doug Zongker | da1ebae | 2013-05-16 11:23:48 -0700 | [diff] [blame] | 78 | #define LAST_LOG_FILE "/cache/recovery/last_log" |
| 79 | |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 80 | static const char *CACHE_LOG_DIR = "/cache/recovery"; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 81 | static const char *COMMAND_FILE = "/cache/recovery/command"; |
| 82 | static const char *INTENT_FILE = "/cache/recovery/intent"; |
| 83 | static const char *LOG_FILE = "/cache/recovery/log"; |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 84 | static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install"; |
Doug Zongker | 8b240cc | 2012-08-29 15:19:29 -0700 | [diff] [blame] | 85 | static const char *LOCALE_FILE = "/cache/recovery/last_locale"; |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 86 | static const char *CACHE_ROOT = "/cache"; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 87 | static const char *SDCARD_ROOT = "/sdcard"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 88 | static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 89 | static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 90 | static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 91 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 92 | RecoveryUI* ui = NULL; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 93 | char* locale = NULL; |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 94 | char recovery_version[PROPERTY_VALUE_MAX+1]; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 95 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 96 | /* |
| 97 | * The recovery tool communicates with the main system through /cache files. |
| 98 | * /cache/recovery/command - INPUT - command line for tool, one arg per line |
| 99 | * /cache/recovery/log - OUTPUT - combined log file from recovery run(s) |
| 100 | * /cache/recovery/intent - OUTPUT - intent that was passed in |
| 101 | * |
| 102 | * The arguments which may be supplied in the recovery.command file: |
| 103 | * --send_intent=anystring - write the text out to recovery.intent |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 104 | * --update_package=path - verify install an OTA package file |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 105 | * --wipe_data - erase user data (and cache), then reboot |
| 106 | * --wipe_cache - wipe cache (but not user data), then reboot |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 107 | * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 108 | * --just_exit - do nothing; exit and reboot |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 109 | * |
| 110 | * After completing, we remove /cache/recovery/command and reboot. |
| 111 | * Arguments may also be supplied in the bootloader control block (BCB). |
| 112 | * These important scenarios must be safely restartable at any point: |
| 113 | * |
| 114 | * FACTORY RESET |
| 115 | * 1. user selects "factory reset" |
| 116 | * 2. main system writes "--wipe_data" to /cache/recovery/command |
| 117 | * 3. main system reboots into recovery |
| 118 | * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data" |
| 119 | * -- after this, rebooting will restart the erase -- |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 120 | * 5. erase_volume() reformats /data |
| 121 | * 6. erase_volume() reformats /cache |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 122 | * 7. finish_recovery() erases BCB |
| 123 | * -- after this, rebooting will restart the main system -- |
| 124 | * 8. main() calls reboot() to boot main system |
| 125 | * |
| 126 | * OTA INSTALL |
| 127 | * 1. main system downloads OTA package to /cache/some-filename.zip |
Doug Zongker | 9b125b0 | 2010-09-22 12:01:37 -0700 | [diff] [blame] | 128 | * 2. main system writes "--update_package=/cache/some-filename.zip" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 129 | * 3. main system reboots into recovery |
| 130 | * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..." |
| 131 | * -- after this, rebooting will attempt to reinstall the update -- |
| 132 | * 5. install_package() attempts to install the update |
| 133 | * NOTE: the package install must itself be restartable from any point |
| 134 | * 6. finish_recovery() erases BCB |
| 135 | * -- after this, rebooting will (try to) restart the main system -- |
| 136 | * 7. ** if install failed ** |
| 137 | * 7a. prompt_and_wait() shows an error icon and waits for the user |
| 138 | * 7b; the user reboots (pulling the battery, etc) into the main system |
| 139 | * 8. main() calls maybe_install_firmware_update() |
| 140 | * ** if the update contained radio/hboot firmware **: |
| 141 | * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache" |
| 142 | * -- after this, rebooting will reformat cache & restart main system -- |
| 143 | * 8b. m_i_f_u() writes firmware image into raw cache partition |
| 144 | * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache" |
| 145 | * -- after this, rebooting will attempt to reinstall firmware -- |
| 146 | * 8d. bootloader tries to flash firmware |
| 147 | * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache") |
| 148 | * -- after this, rebooting will reformat cache & restart main system -- |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 149 | * 8f. erase_volume() reformats /cache |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 150 | * 8g. finish_recovery() erases BCB |
| 151 | * -- after this, rebooting will (try to) restart the main system -- |
| 152 | * 9. main() calls reboot() to boot main system |
| 153 | */ |
| 154 | |
| 155 | static const int MAX_ARG_LENGTH = 4096; |
| 156 | static const int MAX_ARGS = 100; |
| 157 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 158 | // open a given path, mounting partitions as necessary |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 159 | FILE* |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 160 | fopen_path(const char *path, const char *mode) { |
| 161 | if (ensure_path_mounted(path) != 0) { |
| 162 | LOGE("Can't mount %s\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | // When writing, try to create the containing directory, if necessary. |
| 167 | // Use generous permissions, the system (init.rc) will reset them. |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 168 | if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 169 | |
| 170 | FILE *fp = fopen(path, mode); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 171 | return fp; |
| 172 | } |
| 173 | |
| 174 | // close a file, log an error if the error indicator is set |
| 175 | static void |
| 176 | check_and_fclose(FILE *fp, const char *name) { |
| 177 | fflush(fp); |
| 178 | if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno)); |
| 179 | fclose(fp); |
| 180 | } |
| 181 | |
| 182 | // command line args come from, in decreasing precedence: |
| 183 | // - the actual command line |
| 184 | // - the bootloader control block (one per line, after "recovery") |
| 185 | // - the contents of COMMAND_FILE (one per line) |
| 186 | static void |
| 187 | get_args(int *argc, char ***argv) { |
| 188 | struct bootloader_message boot; |
| 189 | memset(&boot, 0, sizeof(boot)); |
| 190 | get_bootloader_message(&boot); // this may fail, leaving a zeroed structure |
| 191 | |
| 192 | if (boot.command[0] != 0 && boot.command[0] != 255) { |
| 193 | LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command); |
| 194 | } |
| 195 | |
| 196 | if (boot.status[0] != 0 && boot.status[0] != 255) { |
| 197 | LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status); |
| 198 | } |
| 199 | |
| 200 | // --- if arguments weren't supplied, look in the bootloader control block |
| 201 | if (*argc <= 1) { |
| 202 | boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination |
| 203 | const char *arg = strtok(boot.recovery, "\n"); |
| 204 | if (arg != NULL && !strcmp(arg, "recovery")) { |
| 205 | *argv = (char **) malloc(sizeof(char *) * MAX_ARGS); |
| 206 | (*argv)[0] = strdup(arg); |
| 207 | for (*argc = 1; *argc < MAX_ARGS; ++*argc) { |
| 208 | if ((arg = strtok(NULL, "\n")) == NULL) break; |
| 209 | (*argv)[*argc] = strdup(arg); |
| 210 | } |
| 211 | LOGI("Got arguments from boot message\n"); |
| 212 | } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) { |
| 213 | LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // --- if that doesn't work, try the command file |
| 218 | if (*argc <= 1) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 219 | FILE *fp = fopen_path(COMMAND_FILE, "r"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 220 | if (fp != NULL) { |
Jin Feng | 93ffa75 | 2013-06-04 17:46:24 +0800 | [diff] [blame] | 221 | char *token; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 222 | char *argv0 = (*argv)[0]; |
| 223 | *argv = (char **) malloc(sizeof(char *) * MAX_ARGS); |
| 224 | (*argv)[0] = argv0; // use the same program name |
| 225 | |
| 226 | char buf[MAX_ARG_LENGTH]; |
| 227 | for (*argc = 1; *argc < MAX_ARGS; ++*argc) { |
| 228 | if (!fgets(buf, sizeof(buf), fp)) break; |
Jin Feng | 93ffa75 | 2013-06-04 17:46:24 +0800 | [diff] [blame] | 229 | token = strtok(buf, "\r\n"); |
| 230 | if (token != NULL) { |
| 231 | (*argv)[*argc] = strdup(token); // Strip newline. |
| 232 | } else { |
| 233 | --*argc; |
| 234 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | check_and_fclose(fp, COMMAND_FILE); |
| 238 | LOGI("Got arguments from %s\n", COMMAND_FILE); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // --> write the arguments we have back into the bootloader control block |
| 243 | // always boot into recovery after this (until finish_recovery() is called) |
| 244 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 245 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 246 | int i; |
| 247 | for (i = 1; i < *argc; ++i) { |
| 248 | strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery)); |
| 249 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 250 | } |
| 251 | set_bootloader_message(&boot); |
| 252 | } |
| 253 | |
Doug Zongker | 34c98df | 2009-08-18 12:05:45 -0700 | [diff] [blame] | 254 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 255 | set_sdcard_update_bootloader_message() { |
Doug Zongker | 34c98df | 2009-08-18 12:05:45 -0700 | [diff] [blame] | 256 | struct bootloader_message boot; |
| 257 | memset(&boot, 0, sizeof(boot)); |
| 258 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 259 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 260 | set_bootloader_message(&boot); |
| 261 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 262 | |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 263 | // How much of the temp log we have copied to the copy in cache. |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 264 | //static long tmplog_offset = 0; |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 265 | |
| 266 | static void |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 267 | copy_log_file(const char* source, const char* destination, int append) { |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 268 | FILE *log = fopen_path(destination, append ? "a" : "w"); |
| 269 | if (log == NULL) { |
| 270 | LOGE("Can't open %s\n", destination); |
| 271 | } else { |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 272 | FILE *tmplog = fopen(source, "r"); |
| 273 | if (tmplog != NULL) { |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 274 | if (append) { |
| 275 | fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write |
| 276 | } |
| 277 | char buf[4096]; |
| 278 | while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log); |
| 279 | if (append) { |
| 280 | tmplog_offset = ftell(tmplog); |
| 281 | } |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 282 | check_and_fclose(tmplog, source); |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 283 | } |
| 284 | check_and_fclose(log, destination); |
| 285 | } |
| 286 | } |
| 287 | |
Doug Zongker | da1ebae | 2013-05-16 11:23:48 -0700 | [diff] [blame] | 288 | // Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max |
| 289 | // Overwrites any existing last_log.$max. |
| 290 | static void |
| 291 | rotate_last_logs(int max) { |
| 292 | char oldfn[256]; |
| 293 | char newfn[256]; |
| 294 | |
| 295 | int i; |
| 296 | for (i = max-1; i >= 0; --i) { |
| 297 | snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i); |
| 298 | snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1); |
| 299 | // ignore errors |
| 300 | rename(oldfn, newfn); |
| 301 | } |
| 302 | } |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 303 | |
Doug Zongker | f24fd7e | 2013-07-02 11:43:25 -0700 | [diff] [blame] | 304 | static void |
| 305 | copy_logs() { |
| 306 | // Copy logs to cache so the system can find out what happened. |
| 307 | copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); |
| 308 | copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); |
| 309 | copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false); |
| 310 | chmod(LOG_FILE, 0600); |
| 311 | chown(LOG_FILE, 1000, 1000); // system user |
| 312 | chmod(LAST_LOG_FILE, 0640); |
| 313 | chmod(LAST_INSTALL_FILE, 0644); |
| 314 | sync(); |
| 315 | } |
| 316 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 317 | // clear the recovery command and prepare to boot a (hopefully working) system, |
| 318 | // copy our log file to cache as well (for the system to read), and |
| 319 | // record any intent we were asked to communicate back to the system. |
| 320 | // this function is idempotent: call it as many times as you like. |
| 321 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 322 | finish_recovery(const char *send_intent) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 323 | // By this point, we're ready to return to the main system... |
| 324 | if (send_intent != NULL) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 325 | FILE *fp = fopen_path(INTENT_FILE, "w"); |
Jay Freeman (saurik) | 619ec2f | 2008-11-17 01:56:05 +0000 | [diff] [blame] | 326 | if (fp == NULL) { |
| 327 | LOGE("Can't open %s\n", INTENT_FILE); |
| 328 | } else { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 329 | fputs(send_intent, fp); |
| 330 | check_and_fclose(fp, INTENT_FILE); |
| 331 | } |
| 332 | } |
| 333 | |
Doug Zongker | 4f33e55 | 2012-08-23 13:16:12 -0700 | [diff] [blame] | 334 | // Save the locale to cache, so if recovery is next started up |
| 335 | // without a --locale argument (eg, directly from the bootloader) |
| 336 | // it will use the last-known locale. |
| 337 | if (locale != NULL) { |
| 338 | LOGI("Saving locale \"%s\"\n", locale); |
| 339 | FILE* fp = fopen_path(LOCALE_FILE, "w"); |
| 340 | fwrite(locale, 1, strlen(locale), fp); |
| 341 | fflush(fp); |
| 342 | fsync(fileno(fp)); |
| 343 | check_and_fclose(fp, LOCALE_FILE); |
| 344 | } |
| 345 | |
Doug Zongker | f24fd7e | 2013-07-02 11:43:25 -0700 | [diff] [blame] | 346 | copy_logs(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 347 | |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 348 | // Reset to normal system boot so recovery won't cycle indefinitely. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 349 | struct bootloader_message boot; |
| 350 | memset(&boot, 0, sizeof(boot)); |
| 351 | set_bootloader_message(&boot); |
| 352 | |
| 353 | // Remove the command file, so recovery won't repeat indefinitely. |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 354 | if (ensure_path_mounted(COMMAND_FILE) != 0 || |
| 355 | (unlink(COMMAND_FILE) && errno != ENOENT)) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 356 | LOGW("Can't unlink %s\n", COMMAND_FILE); |
| 357 | } |
| 358 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 359 | ensure_path_unmounted(CACHE_ROOT); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 360 | sync(); // For good measure. |
| 361 | } |
| 362 | |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 363 | typedef struct _saved_log_file { |
| 364 | char* name; |
| 365 | struct stat st; |
| 366 | unsigned char* data; |
| 367 | struct _saved_log_file* next; |
| 368 | } saved_log_file; |
| 369 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 370 | static int |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 371 | erase_volume(const char *volume) { |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 372 | bool is_cache = (strcmp(volume, CACHE_ROOT) == 0); |
| 373 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 374 | ui->SetBackground(RecoveryUI::ERASING); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 375 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 376 | |
| 377 | saved_log_file* head = NULL; |
| 378 | |
| 379 | if (is_cache) { |
| 380 | // If we're reformatting /cache, we load any |
| 381 | // "/cache/recovery/last*" files into memory, so we can restore |
| 382 | // them after the reformat. |
| 383 | |
| 384 | ensure_path_mounted(volume); |
| 385 | |
| 386 | DIR* d; |
| 387 | struct dirent* de; |
| 388 | d = opendir(CACHE_LOG_DIR); |
| 389 | if (d) { |
| 390 | char path[PATH_MAX]; |
| 391 | strcpy(path, CACHE_LOG_DIR); |
| 392 | strcat(path, "/"); |
| 393 | int path_len = strlen(path); |
| 394 | while ((de = readdir(d)) != NULL) { |
| 395 | if (strncmp(de->d_name, "last", 4) == 0) { |
| 396 | saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file)); |
| 397 | strcpy(path+path_len, de->d_name); |
| 398 | p->name = strdup(path); |
| 399 | if (stat(path, &(p->st)) == 0) { |
| 400 | // truncate files to 512kb |
| 401 | if (p->st.st_size > (1 << 19)) { |
| 402 | p->st.st_size = 1 << 19; |
| 403 | } |
| 404 | p->data = (unsigned char*) malloc(p->st.st_size); |
| 405 | FILE* f = fopen(path, "rb"); |
| 406 | fread(p->data, 1, p->st.st_size, f); |
| 407 | fclose(f); |
| 408 | p->next = head; |
| 409 | head = p; |
| 410 | } else { |
| 411 | free(p); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | closedir(d); |
| 416 | } else { |
| 417 | if (errno != ENOENT) { |
| 418 | printf("opendir failed: %s\n", strerror(errno)); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 423 | ui->Print("Formatting %s...\n", volume); |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 424 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 425 | ensure_path_unmounted(volume); |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 426 | int result = format_volume(volume); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 427 | |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 428 | if (is_cache) { |
| 429 | while (head) { |
| 430 | FILE* f = fopen_path(head->name, "wb"); |
| 431 | if (f) { |
| 432 | fwrite(head->data, 1, head->st.st_size, f); |
| 433 | fclose(f); |
| 434 | chmod(head->name, head->st.st_mode); |
| 435 | chown(head->name, head->st.st_uid, head->st.st_gid); |
| 436 | } |
| 437 | free(head->name); |
| 438 | free(head->data); |
| 439 | saved_log_file* temp = head->next; |
| 440 | free(head); |
| 441 | head = temp; |
| 442 | } |
| 443 | |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 444 | // Any part of the log we'd copied to cache is now gone. |
| 445 | // Reset the pointer so we copy from the beginning of the temp |
| 446 | // log. |
| 447 | tmplog_offset = 0; |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 448 | copy_logs(); |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 451 | return result; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 454 | static char* |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 455 | copy_sideloaded_package(const char* original_path) { |
| 456 | if (ensure_path_mounted(original_path) != 0) { |
| 457 | LOGE("Can't mount %s\n", original_path); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 458 | return NULL; |
| 459 | } |
| 460 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 461 | if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) { |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 462 | LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR); |
| 463 | return NULL; |
| 464 | } |
| 465 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 466 | if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) { |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 467 | if (errno != EEXIST) { |
| 468 | LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno)); |
| 469 | return NULL; |
| 470 | } |
| 471 | } |
| 472 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 473 | // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a |
| 474 | // directory, owned by root, readable and writable only by root. |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 475 | struct stat st; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 476 | if (stat(SIDELOAD_TEMP_DIR, &st) != 0) { |
| 477 | LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno)); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 478 | return NULL; |
| 479 | } |
| 480 | if (!S_ISDIR(st.st_mode)) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 481 | LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 482 | return NULL; |
| 483 | } |
| 484 | if ((st.st_mode & 0777) != 0700) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 485 | LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 486 | return NULL; |
| 487 | } |
| 488 | if (st.st_uid != 0) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 489 | LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 490 | return NULL; |
| 491 | } |
| 492 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 493 | char copy_path[PATH_MAX]; |
| 494 | strcpy(copy_path, SIDELOAD_TEMP_DIR); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 495 | strcat(copy_path, "/package.zip"); |
| 496 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 497 | char* buffer = (char*)malloc(BUFSIZ); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 498 | if (buffer == NULL) { |
| 499 | LOGE("Failed to allocate buffer\n"); |
| 500 | return NULL; |
| 501 | } |
| 502 | |
| 503 | size_t read; |
| 504 | FILE* fin = fopen(original_path, "rb"); |
| 505 | if (fin == NULL) { |
| 506 | LOGE("Failed to open %s (%s)\n", original_path, strerror(errno)); |
| 507 | return NULL; |
| 508 | } |
| 509 | FILE* fout = fopen(copy_path, "wb"); |
| 510 | if (fout == NULL) { |
| 511 | LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno)); |
| 512 | return NULL; |
| 513 | } |
| 514 | |
| 515 | while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) { |
| 516 | if (fwrite(buffer, 1, read, fout) != read) { |
| 517 | LOGE("Short write of %s (%s)\n", copy_path, strerror(errno)); |
| 518 | return NULL; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | free(buffer); |
| 523 | |
| 524 | if (fclose(fout) != 0) { |
| 525 | LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno)); |
| 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | if (fclose(fin) != 0) { |
| 530 | LOGE("Failed to close %s (%s)\n", original_path, strerror(errno)); |
| 531 | return NULL; |
| 532 | } |
| 533 | |
| 534 | // "adb push" is happy to overwrite read-only files when it's |
| 535 | // running as root, but we'll try anyway. |
| 536 | if (chmod(copy_path, 0400) != 0) { |
| 537 | LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno)); |
| 538 | return NULL; |
| 539 | } |
| 540 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 541 | return strdup(copy_path); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 544 | static const char** |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 545 | prepend_title(const char* const* headers) { |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 546 | // count the number of lines in our title, plus the |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 547 | // caller-provided headers. |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 548 | int count = 3; // our title has 3 lines |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 549 | const char* const* p; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 550 | for (p = headers; *p; ++p, ++count); |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 551 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 552 | const char** new_headers = (const char**)malloc((count+1) * sizeof(char*)); |
| 553 | const char** h = new_headers; |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 554 | *(h++) = "Android system recovery <" EXPAND(RECOVERY_API_VERSION) "e>"; |
| 555 | *(h++) = recovery_version; |
| 556 | *(h++) = ""; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 557 | for (p = headers; *p; ++p, ++h) *h = *p; |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 558 | *h = NULL; |
| 559 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 560 | return new_headers; |
| 561 | } |
| 562 | |
| 563 | static int |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 564 | get_menu_selection(const char* const * headers, const char* const * items, |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 565 | int menu_only, int initial_selection, Device* device) { |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 566 | // throw away keys pressed previously, so user doesn't |
| 567 | // accidentally trigger menu items. |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 568 | ui->FlushKeys(); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 569 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 570 | ui->StartMenu(headers, items, initial_selection); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 571 | int selected = initial_selection; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 572 | int chosen_item = -1; |
| 573 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 574 | while (chosen_item < 0) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 575 | int key = ui->WaitKey(); |
| 576 | int visible = ui->IsTextVisible(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 577 | |
Doug Zongker | 5cae445 | 2011-01-25 13:15:30 -0800 | [diff] [blame] | 578 | if (key == -1) { // ui_wait_key() timed out |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 579 | if (ui->WasTextEverVisible()) { |
Doug Zongker | 5cae445 | 2011-01-25 13:15:30 -0800 | [diff] [blame] | 580 | continue; |
| 581 | } else { |
| 582 | LOGI("timed out waiting for key input; rebooting.\n"); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 583 | ui->EndMenu(); |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 584 | return 0; // XXX fixme |
Doug Zongker | 5cae445 | 2011-01-25 13:15:30 -0800 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 588 | int action = device->HandleMenuKey(key, visible); |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 589 | |
| 590 | if (action < 0) { |
| 591 | switch (action) { |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 592 | case Device::kHighlightUp: |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 593 | --selected; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 594 | selected = ui->SelectMenu(selected); |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 595 | break; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 596 | case Device::kHighlightDown: |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 597 | ++selected; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 598 | selected = ui->SelectMenu(selected); |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 599 | break; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 600 | case Device::kInvokeItem: |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 601 | chosen_item = selected; |
| 602 | break; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 603 | case Device::kNoAction: |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 604 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 605 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 606 | } else if (!menu_only) { |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 607 | chosen_item = action; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 608 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 609 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 610 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 611 | ui->EndMenu(); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 612 | return chosen_item; |
| 613 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 614 | |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 615 | static int compare_string(const void* a, const void* b) { |
| 616 | return strcmp(*(const char**)a, *(const char**)b); |
| 617 | } |
| 618 | |
| 619 | static int |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 620 | update_directory(const char* path, const char* unmount_when_done, |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 621 | int* wipe_cache, Device* device) { |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 622 | ensure_path_mounted(path); |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 623 | |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 624 | const char* MENU_HEADERS[] = { "Choose a package to install:", |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 625 | path, |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 626 | "", |
| 627 | NULL }; |
| 628 | DIR* d; |
| 629 | struct dirent* de; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 630 | d = opendir(path); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 631 | if (d == NULL) { |
| 632 | LOGE("error opening %s: %s\n", path, strerror(errno)); |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 633 | if (unmount_when_done != NULL) { |
| 634 | ensure_path_unmounted(unmount_when_done); |
| 635 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 639 | const char** headers = prepend_title(MENU_HEADERS); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 640 | |
| 641 | int d_size = 0; |
| 642 | int d_alloc = 10; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 643 | char** dirs = (char**)malloc(d_alloc * sizeof(char*)); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 644 | int z_size = 1; |
| 645 | int z_alloc = 10; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 646 | char** zips = (char**)malloc(z_alloc * sizeof(char*)); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 647 | zips[0] = strdup("../"); |
| 648 | |
| 649 | while ((de = readdir(d)) != NULL) { |
| 650 | int name_len = strlen(de->d_name); |
| 651 | |
| 652 | if (de->d_type == DT_DIR) { |
| 653 | // skip "." and ".." entries |
| 654 | if (name_len == 1 && de->d_name[0] == '.') continue; |
| 655 | if (name_len == 2 && de->d_name[0] == '.' && |
| 656 | de->d_name[1] == '.') continue; |
| 657 | |
| 658 | if (d_size >= d_alloc) { |
| 659 | d_alloc *= 2; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 660 | dirs = (char**)realloc(dirs, d_alloc * sizeof(char*)); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 661 | } |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 662 | dirs[d_size] = (char*)malloc(name_len + 2); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 663 | strcpy(dirs[d_size], de->d_name); |
| 664 | dirs[d_size][name_len] = '/'; |
| 665 | dirs[d_size][name_len+1] = '\0'; |
| 666 | ++d_size; |
| 667 | } else if (de->d_type == DT_REG && |
| 668 | name_len >= 4 && |
| 669 | strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) { |
| 670 | if (z_size >= z_alloc) { |
| 671 | z_alloc *= 2; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 672 | zips = (char**)realloc(zips, z_alloc * sizeof(char*)); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 673 | } |
| 674 | zips[z_size++] = strdup(de->d_name); |
| 675 | } |
| 676 | } |
| 677 | closedir(d); |
| 678 | |
| 679 | qsort(dirs, d_size, sizeof(char*), compare_string); |
| 680 | qsort(zips, z_size, sizeof(char*), compare_string); |
| 681 | |
| 682 | // append dirs to the zips list |
| 683 | if (d_size + z_size + 1 > z_alloc) { |
| 684 | z_alloc = d_size + z_size + 1; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 685 | zips = (char**)realloc(zips, z_alloc * sizeof(char*)); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 686 | } |
| 687 | memcpy(zips + z_size, dirs, d_size * sizeof(char*)); |
| 688 | free(dirs); |
| 689 | z_size += d_size; |
| 690 | zips[z_size] = NULL; |
| 691 | |
| 692 | int result; |
| 693 | int chosen_item = 0; |
| 694 | do { |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 695 | chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 696 | |
| 697 | char* item = zips[chosen_item]; |
| 698 | int item_len = strlen(item); |
| 699 | if (chosen_item == 0) { // item 0 is always "../" |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 700 | // go up but continue browsing (if the caller is update_directory) |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 701 | result = -1; |
| 702 | break; |
| 703 | } else if (item[item_len-1] == '/') { |
| 704 | // recurse down into a subdirectory |
| 705 | char new_path[PATH_MAX]; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 706 | strlcpy(new_path, path, PATH_MAX); |
| 707 | strlcat(new_path, "/", PATH_MAX); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 708 | strlcat(new_path, item, PATH_MAX); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 709 | new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/' |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 710 | result = update_directory(new_path, unmount_when_done, wipe_cache, device); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 711 | if (result >= 0) break; |
| 712 | } else { |
| 713 | // selected a zip file: attempt to install it, and return |
| 714 | // the status to the caller. |
| 715 | char new_path[PATH_MAX]; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 716 | strlcpy(new_path, path, PATH_MAX); |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 717 | strlcat(new_path, "/", PATH_MAX); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 718 | strlcat(new_path, item, PATH_MAX); |
| 719 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 720 | ui->Print("\n-- Install %s ...\n", path); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 721 | set_sdcard_update_bootloader_message(); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 722 | char* copy = copy_sideloaded_package(new_path); |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 723 | if (unmount_when_done != NULL) { |
| 724 | ensure_path_unmounted(unmount_when_done); |
| 725 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 726 | if (copy) { |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 727 | result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 728 | free(copy); |
| 729 | } else { |
| 730 | result = INSTALL_ERROR; |
| 731 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 732 | break; |
| 733 | } |
| 734 | } while (true); |
| 735 | |
| 736 | int i; |
| 737 | for (i = 0; i < z_size; ++i) free(zips[i]); |
| 738 | free(zips); |
| 739 | free(headers); |
| 740 | |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 741 | if (unmount_when_done != NULL) { |
| 742 | ensure_path_unmounted(unmount_when_done); |
| 743 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 744 | return result; |
| 745 | } |
| 746 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 747 | static void |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 748 | wipe_data(int confirm, Device* device) { |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 749 | if (confirm) { |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 750 | static const char** title_headers = NULL; |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 751 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 752 | if (title_headers == NULL) { |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 753 | const char* headers[] = { "Confirm wipe of all user data?", |
| 754 | " THIS CAN NOT BE UNDONE.", |
| 755 | "", |
| 756 | NULL }; |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 757 | title_headers = prepend_title((const char**)headers); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 758 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 759 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 760 | const char* items[] = { " No", |
| 761 | " No", |
| 762 | " No", |
| 763 | " No", |
| 764 | " No", |
| 765 | " No", |
| 766 | " No", |
| 767 | " Yes -- delete all user data", // [7] |
| 768 | " No", |
| 769 | " No", |
| 770 | " No", |
| 771 | NULL }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 772 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 773 | int chosen_item = get_menu_selection(title_headers, items, 1, 0, device); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 774 | if (chosen_item != 7) { |
| 775 | return; |
| 776 | } |
| 777 | } |
Doug Zongker | 1066d2c | 2009-04-01 13:57:40 -0700 | [diff] [blame] | 778 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 779 | ui->Print("\n-- Wiping data...\n"); |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 780 | device->WipeData(); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 781 | erase_volume("/data"); |
| 782 | erase_volume("/cache"); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 783 | ui->Print("Data wipe complete.\n"); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static void |
Doug Zongker | 6c8553d | 2012-09-24 10:40:47 -0700 | [diff] [blame] | 787 | prompt_and_wait(Device* device, int status) { |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 788 | const char* const* headers = prepend_title(device->GetMenuHeaders()); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 789 | |
| 790 | for (;;) { |
| 791 | finish_recovery(NULL); |
Doug Zongker | 6c8553d | 2012-09-24 10:40:47 -0700 | [diff] [blame] | 792 | switch (status) { |
| 793 | case INSTALL_SUCCESS: |
| 794 | case INSTALL_NONE: |
| 795 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
| 796 | break; |
| 797 | |
| 798 | case INSTALL_ERROR: |
| 799 | case INSTALL_CORRUPT: |
| 800 | ui->SetBackground(RecoveryUI::ERROR); |
| 801 | break; |
| 802 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 803 | ui->SetProgressType(RecoveryUI::EMPTY); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 804 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 805 | int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 806 | |
| 807 | // device-specific code may take some action here. It may |
| 808 | // return one of the core actions handled in the switch |
| 809 | // statement below. |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 810 | chosen_item = device->InvokeMenuItem(chosen_item); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 811 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 812 | int wipe_cache; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 813 | switch (chosen_item) { |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 814 | case Device::REBOOT: |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 815 | return; |
| 816 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 817 | case Device::WIPE_DATA: |
| 818 | wipe_data(ui->IsTextVisible(), device); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 819 | if (!ui->IsTextVisible()) return; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 820 | break; |
| 821 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 822 | case Device::WIPE_CACHE: |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 823 | ui->Print("\n-- Wiping cache...\n"); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 824 | erase_volume("/cache"); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 825 | ui->Print("Cache wipe complete.\n"); |
| 826 | if (!ui->IsTextVisible()) return; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 827 | break; |
| 828 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 829 | case Device::APPLY_EXT: |
| 830 | status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 831 | if (status == INSTALL_SUCCESS && wipe_cache) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 832 | ui->Print("\n-- Wiping cache (at package request)...\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 833 | if (erase_volume("/cache")) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 834 | ui->Print("Cache wipe failed.\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 835 | } else { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 836 | ui->Print("Cache wipe complete.\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 837 | } |
| 838 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 839 | if (status >= 0) { |
| 840 | if (status != INSTALL_SUCCESS) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 841 | ui->SetBackground(RecoveryUI::ERROR); |
| 842 | ui->Print("Installation aborted.\n"); |
| 843 | } else if (!ui->IsTextVisible()) { |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 844 | return; // reboot if logs aren't visible |
| 845 | } else { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 846 | ui->Print("\nInstall from sdcard complete.\n"); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 847 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 848 | } |
| 849 | break; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 850 | |
| 851 | case Device::APPLY_CACHE: |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 852 | // Don't unmount cache at the end of this. |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 853 | status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 854 | if (status == INSTALL_SUCCESS && wipe_cache) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 855 | ui->Print("\n-- Wiping cache (at package request)...\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 856 | if (erase_volume("/cache")) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 857 | ui->Print("Cache wipe failed.\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 858 | } else { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 859 | ui->Print("Cache wipe complete.\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 860 | } |
| 861 | } |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 862 | if (status >= 0) { |
| 863 | if (status != INSTALL_SUCCESS) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 864 | ui->SetBackground(RecoveryUI::ERROR); |
| 865 | ui->Print("Installation aborted.\n"); |
| 866 | } else if (!ui->IsTextVisible()) { |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 867 | return; // reboot if logs aren't visible |
| 868 | } else { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 869 | ui->Print("\nInstall from cache complete.\n"); |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | break; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 873 | |
| 874 | case Device::APPLY_ADB_SIDELOAD: |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 875 | status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE); |
| 876 | if (status >= 0) { |
| 877 | if (status != INSTALL_SUCCESS) { |
| 878 | ui->SetBackground(RecoveryUI::ERROR); |
| 879 | ui->Print("Installation aborted.\n"); |
Doug Zongker | f24fd7e | 2013-07-02 11:43:25 -0700 | [diff] [blame] | 880 | copy_logs(); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 881 | } else if (!ui->IsTextVisible()) { |
| 882 | return; // reboot if logs aren't visible |
| 883 | } else { |
| 884 | ui->Print("\nInstall from ADB complete.\n"); |
| 885 | } |
| 886 | } |
| 887 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 893 | print_property(const char *key, const char *name, void *cookie) { |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 894 | printf("%s=%s\n", key, name); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 895 | } |
| 896 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 897 | static void |
| 898 | load_locale_from_cache() { |
| 899 | FILE* fp = fopen_path(LOCALE_FILE, "r"); |
| 900 | char buffer[80]; |
| 901 | if (fp != NULL) { |
| 902 | fgets(buffer, sizeof(buffer), fp); |
| 903 | int j = 0; |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 904 | unsigned int i; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 905 | for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) { |
| 906 | if (!isspace(buffer[i])) { |
| 907 | buffer[j++] = buffer[i]; |
| 908 | } |
| 909 | } |
| 910 | buffer[j] = 0; |
| 911 | locale = strdup(buffer); |
Devin Kim | 6016d08 | 2012-10-08 09:47:37 -0700 | [diff] [blame] | 912 | check_and_fclose(fp, LOCALE_FILE); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 916 | static RecoveryUI* gCurrentUI = NULL; |
| 917 | |
| 918 | void |
| 919 | ui_print(const char* format, ...) { |
| 920 | char buffer[256]; |
| 921 | |
| 922 | va_list ap; |
| 923 | va_start(ap, format); |
| 924 | vsnprintf(buffer, sizeof(buffer), format, ap); |
| 925 | va_end(ap); |
| 926 | |
| 927 | if (gCurrentUI != NULL) { |
| 928 | gCurrentUI->Print("%s", buffer); |
| 929 | } else { |
| 930 | fputs(buffer, stdout); |
| 931 | } |
| 932 | } |
| 933 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 934 | int |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 935 | main(int argc, char **argv) { |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 936 | // Recovery needs to install world-readable files, so clear umask |
| 937 | // set by init |
| 938 | umask(0); |
| 939 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 940 | time_t start = time(NULL); |
| 941 | |
| 942 | // If these fail, there's not really anywhere to complain... |
| 943 | freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL); |
| 944 | freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 945 | |
| 946 | // If this binary is started with the single argument "--adbd", |
| 947 | // instead of being the normal recovery binary, it turns into kind |
| 948 | // of a stripped-down version of adbd that only supports the |
| 949 | // 'sideload' command. Note this must be a real argument, not |
| 950 | // anything in the command file or bootloader control block; the |
| 951 | // only way recovery should be run with this argument is when it |
| 952 | // starts a copy of itself from the apply_from_adb() function. |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 953 | if (argc == 3 && strcmp(argv[1], "--adbd") == 0) { |
| 954 | adb_main(argv[2]); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 955 | return 0; |
| 956 | } |
| 957 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 958 | printf("Starting TWRP %s on %s", TW_VERSION_STR, ctime(&start)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 959 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 960 | Device* device = make_device(); |
| 961 | ui = device->GetUI(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 962 | |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 963 | //ui->Init(); |
| 964 | //ui->SetBackground(RecoveryUI::NONE); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 965 | //load_volume_table(); |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 966 | |
| 967 | // Load default values to set DataManager constants and handle ifdefs |
| 968 | DataManager_LoadDefaults(); |
| 969 | printf("Starting the UI..."); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 970 | gui_init(); |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 971 | printf("=> Linking mtab\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 972 | symlink("/proc/mounts", "/etc/mtab"); |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 973 | printf("=> Processing recovery.fstab\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 974 | if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) { |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 975 | LOGE("Failing out of recovery due to problem with recovery.fstab.\n"); |
| 976 | //return -1; |
| 977 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 978 | PartitionManager.Output_Partition_Logging(); |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 979 | // Load up all the resources |
| 980 | gui_loadResources(); |
| 981 | |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 982 | PartitionManager.Mount_By_Path("/cache", true); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 983 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 984 | load_volume_table(); |
Doug Zongker | da1ebae | 2013-05-16 11:23:48 -0700 | [diff] [blame] | 985 | ensure_path_mounted(LAST_LOG_FILE); |
Dees_Troy | 1669f89 | 2013-09-04 18:35:08 +0000 | [diff] [blame] | 986 | |
Doug Zongker | f24fd7e | 2013-07-02 11:43:25 -0700 | [diff] [blame] | 987 | rotate_last_logs(10); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 988 | get_args(&argc, &argv); |
| 989 | |
| 990 | int previous_runs = 0; |
| 991 | const char *send_intent = NULL; |
| 992 | const char *update_package = NULL; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 993 | int wipe_data = 0, wipe_cache = 0, show_text = 0; |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 994 | bool just_exit = false; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 995 | bool perform_backup = false; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 996 | |
| 997 | int arg; |
| 998 | while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) { |
| 999 | switch (arg) { |
| 1000 | case 'p': previous_runs = atoi(optarg); break; |
| 1001 | case 's': send_intent = optarg; break; |
| 1002 | case 'u': update_package = optarg; break; |
| 1003 | case 'w': wipe_data = wipe_cache = 1; break; |
| 1004 | case 'c': wipe_cache = 1; break; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1005 | case 't': show_text = 1; break; |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 1006 | case 'x': just_exit = true; break; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1007 | case 'l': locale = optarg; break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1008 | case '?': |
| 1009 | LOGE("Invalid command argument\n"); |
| 1010 | continue; |
| 1011 | } |
| 1012 | } |
| 1013 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1014 | if (locale == NULL) { |
| 1015 | load_locale_from_cache(); |
| 1016 | } |
| 1017 | printf("locale is [%s]\n", locale); |
| 1018 | |
| 1019 | Device* device = make_device(); |
| 1020 | ui = device->GetUI(); |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 1021 | gCurrentUI = ui; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1022 | |
| 1023 | ui->Init(); |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 1024 | ui->SetLocale(locale); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1025 | ui->SetBackground(RecoveryUI::NONE); |
| 1026 | if (show_text) ui->ShowText(true); |
| 1027 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 1028 | struct selinux_opt seopts[] = { |
| 1029 | { SELABEL_OPT_PATH, "/file_contexts" } |
| 1030 | }; |
| 1031 | |
| 1032 | sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1); |
| 1033 | |
| 1034 | if (!sehandle) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1035 | ui->Print("Warning: No file_contexts\n"); |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 1036 | } |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 1037 | |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 1038 | //device->StartRecovery(); |
Doug Zongker | efa1bab | 2010-02-01 15:59:12 -0800 | [diff] [blame] | 1039 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 1040 | printf("Command:"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1041 | for (arg = 0; arg < argc; arg++) { |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 1042 | printf(" \"%s\"", argv[arg]); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1043 | } |
Doug Zongker | 9b125b0 | 2010-09-22 12:01:37 -0700 | [diff] [blame] | 1044 | printf("\n"); |
| 1045 | |
| 1046 | if (update_package) { |
| 1047 | // For backwards compatibility on the cache partition only, if |
| 1048 | // we're given an old 'root' path "CACHE:foo", change it to |
| 1049 | // "/cache/foo". |
| 1050 | if (strncmp(update_package, "CACHE:", 6) == 0) { |
| 1051 | int len = strlen(update_package) + 10; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 1052 | char* modified_path = (char*)malloc(len); |
Doug Zongker | 9b125b0 | 2010-09-22 12:01:37 -0700 | [diff] [blame] | 1053 | strlcpy(modified_path, "/cache/", len); |
| 1054 | strlcat(modified_path, update_package+6, len); |
| 1055 | printf("(replacing path \"%s\" with \"%s\")\n", |
| 1056 | update_package, modified_path); |
| 1057 | update_package = modified_path; |
| 1058 | } |
| 1059 | } |
| 1060 | printf("\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1061 | |
| 1062 | property_list(print_property, NULL); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 1063 | property_get("ro.build.display.id", recovery_version, ""); |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 1064 | printf("\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1065 | |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 1066 | // Check for and run startup script if script exists |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 1067 | TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot"); |
| 1068 | TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot"); |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 1069 | |
| 1070 | #ifdef TW_INCLUDE_INJECTTWRP |
| 1071 | // Back up TWRP Ramdisk if needed: |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1072 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 1073 | LOGI("Backing up TWRP ramdisk...\n"); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1074 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1075 | TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img"); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1076 | else { |
| 1077 | string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1078 | TWFunc::Exec_Cmd(injectcmd); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1079 | } |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 1080 | LOGI("Backup of TWRP ramdisk done.\n"); |
| 1081 | #endif |
| 1082 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1083 | int status = INSTALL_SUCCESS; |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 1084 | string ORSCommand; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1085 | |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 1086 | if (perform_backup) { |
| 1087 | char empt[50]; |
| 1088 | gui_console_only(); |
| 1089 | strcpy(empt, "(Current Date)"); |
| 1090 | DataManager_SetStrValue(TW_BACKUP_NAME, empt); |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 1091 | if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n")) |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 1092 | status = INSTALL_ERROR; |
| 1093 | } |
| 1094 | if (status == INSTALL_SUCCESS) { // Prevent other actions if backup failed |
Doug Zongker | 540d57f | 2011-01-18 13:36:58 -0800 | [diff] [blame] | 1095 | if (update_package != NULL) { |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 1096 | ORSCommand = "install "; |
| 1097 | ORSCommand += update_package; |
| 1098 | ORSCommand += "\n"; |
| 1099 | |
| 1100 | if (OpenRecoveryScript::Insert_ORS_Command(ORSCommand)) |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 1101 | status = INSTALL_SUCCESS; |
| 1102 | else |
| 1103 | status = INSTALL_ERROR; |
| 1104 | /* |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1105 | status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE); |
| 1106 | if (status == INSTALL_SUCCESS && wipe_cache) { |
| 1107 | if (erase_volume("/cache")) { |
| 1108 | LOGE("Cache wipe (requested by package) failed."); |
| 1109 | } |
| 1110 | } |
Dees_Troy | 1669f89 | 2013-09-04 18:35:08 +0000 | [diff] [blame] | 1111 | |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 1112 | if (status != INSTALL_SUCCESS) { |
| 1113 | ui->Print("Installation aborted.\n"); |
| 1114 | |
| 1115 | // If this is an eng or userdebug build, then automatically |
| 1116 | // turn the text display on if the script fails so the error |
| 1117 | // message is visible. |
| 1118 | char buffer[PROPERTY_VALUE_MAX+1]; |
| 1119 | property_get("ro.build.fingerprint", buffer, ""); |
| 1120 | if (strstr(buffer, ":userdebug/") || strstr(buffer, ":eng/")) { |
| 1121 | ui->ShowText(true); |
| 1122 | } |
| 1123 | } |
Doug Zongker | b128f54 | 2009-06-18 15:07:14 -0700 | [diff] [blame] | 1124 | } else if (wipe_data) { |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 1125 | if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n")) |
| 1126 | status = INSTALL_ERROR; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 1127 | /* |
| 1128 | if (device->WipeData()) status = INSTALL_ERROR; |
| 1129 | if (erase_volume("/data")) status = INSTALL_ERROR; |
| 1130 | if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; |
| 1131 | */ |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1132 | if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n"); |
Doug Zongker | b128f54 | 2009-06-18 15:07:14 -0700 | [diff] [blame] | 1133 | } else if (wipe_cache) { |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 1134 | if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n")) |
| 1135 | status = INSTALL_ERROR; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1136 | if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n"); |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 1137 | } else if (!just_exit) { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1138 | status = INSTALL_NONE; // No command specified |
| 1139 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1140 | } |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 1141 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1142 | |
Dees_Troy | d15e3f7 | 2013-02-21 14:28:29 -0600 | [diff] [blame] | 1143 | finish_recovery(NULL); |
| 1144 | // Offer to decrypt if the device is encrypted |
| 1145 | if (DataManager_GetIntValue(TW_IS_ENCRYPTED) != 0) { |
| 1146 | LOGI("Is encrypted, do decrypt page first\n"); |
| 1147 | if (gui_startPage("decrypt") != 0) { |
| 1148 | LOGE("Failed to start decrypt GUI page.\n"); |
Dees_Troy | c7c4341 | 2012-10-02 23:03:01 -0400 | [diff] [blame] | 1149 | } |
Dees_Troy | d15e3f7 | 2013-02-21 14:28:29 -0600 | [diff] [blame] | 1150 | } |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1151 | |
Dees_Troy | d15e3f7 | 2013-02-21 14:28:29 -0600 | [diff] [blame] | 1152 | // Read the settings file |
| 1153 | DataManager_ReadSettingsFile(); |
| 1154 | // Run any outstanding OpenRecoveryScript |
| 1155 | if (DataManager_GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) { |
| 1156 | OpenRecoveryScript::Run_OpenRecoveryScript(); |
| 1157 | } |
| 1158 | // Launch the main GUI |
| 1159 | gui_start(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1160 | |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 1161 | // Check for su to see if the device is rooted or not |
| 1162 | if (PartitionManager.Mount_By_Path("/system", false)) { |
Dees_Troy | d15e3f7 | 2013-02-21 14:28:29 -0600 | [diff] [blame] | 1163 | // Disable flashing of stock recovery |
dhacker29 | 30f100e | 2014-07-15 21:33:29 -0400 | [diff] [blame] | 1164 | if (TWFunc::Path_Exists("/system/recovery-from-boot.p") && TWFunc::Path_Exists("/system/etc/install-recovery.sh")) { |
Dees_Troy | d15e3f7 | 2013-02-21 14:28:29 -0600 | [diff] [blame] | 1165 | rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak"); |
| 1166 | ui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n"); |
| 1167 | } |
jt1134 | 113ee73 | 2013-02-22 23:26:10 -0600 | [diff] [blame] | 1168 | if (TWFunc::Path_Exists("/supersu/su") && !TWFunc::Path_Exists("/system/bin/su") && !TWFunc::Path_Exists("/system/xbin/su") && !TWFunc::Path_Exists("/system/bin/.ext/.su")) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 1169 | // Device doesn't have su installed |
| 1170 | DataManager_SetIntValue("tw_busy", 1); |
| 1171 | if (gui_startPage("installsu") != 0) { |
| 1172 | LOGE("Failed to start decrypt GUI page.\n"); |
| 1173 | } |
| 1174 | } else if (TWFunc::Check_su_Perms() > 0) { |
| 1175 | // su perms are set incorrectly |
| 1176 | DataManager_SetIntValue("tw_busy", 1); |
| 1177 | if (gui_startPage("fixsu") != 0) { |
| 1178 | LOGE("Failed to start decrypt GUI page.\n"); |
| 1179 | } |
| 1180 | } |
Dees_Troy | d15e3f7 | 2013-02-21 14:28:29 -0600 | [diff] [blame] | 1181 | sync(); |
| 1182 | PartitionManager.UnMount_By_Path("/system", false); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 1183 | } |
| 1184 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1185 | if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) { |
Doug Zongker | f24fd7e | 2013-07-02 11:43:25 -0700 | [diff] [blame] | 1186 | copy_logs(); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1187 | ui->SetBackground(RecoveryUI::ERROR); |
| 1188 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1189 | if (status != INSTALL_SUCCESS || ui->IsTextVisible()) { |
Doug Zongker | 6c8553d | 2012-09-24 10:40:47 -0700 | [diff] [blame] | 1190 | prompt_and_wait(device, status); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | // Otherwise, get ready to boot the main system... |
| 1194 | finish_recovery(send_intent); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1195 | ui->Print("Rebooting...\n"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 1196 | char backup_arg_char[50]; |
| 1197 | strcpy(backup_arg_char, DataManager_GetStrValue("tw_reboot_arg")); |
| 1198 | string backup_arg = backup_arg_char; |
| 1199 | if (backup_arg == "recovery") |
| 1200 | TWFunc::tw_reboot(rb_recovery); |
| 1201 | else if (backup_arg == "poweroff") |
| 1202 | TWFunc::tw_reboot(rb_poweroff); |
| 1203 | else if (backup_arg == "bootloader") |
| 1204 | TWFunc::tw_reboot(rb_bootloader); |
| 1205 | else if (backup_arg == "download") |
| 1206 | TWFunc::tw_reboot(rb_download); |
| 1207 | else |
| 1208 | TWFunc::tw_reboot(rb_system); |
| 1209 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1210 | #ifdef ANDROID_RB_RESTART |
Ken Sumrall | 6e4472a | 2011-03-07 23:37:27 -0800 | [diff] [blame] | 1211 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1212 | #else |
| 1213 | reboot(RB_AUTOBOOT); |
| 1214 | #endif |
Doug Zongker | 3b5a987 | 2013-09-03 14:29:54 -0700 | [diff] [blame] | 1215 | property_set(ANDROID_RB_PROPERTY, "reboot,"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1216 | return EXIT_SUCCESS; |
| 1217 | } |