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