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