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> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <getopt.h> |
| 21 | #include <limits.h> |
| 22 | #include <linux/input.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/reboot.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <time.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include "bootloader.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 32 | #include "common.h" |
| 33 | #include "cutils/properties.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | #include "install.h" |
| 35 | #include "minui/minui.h" |
| 36 | #include "minzip/DirUtil.h" |
| 37 | #include "roots.h" |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 38 | #include "recovery_ui.h" |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 39 | #include "efs_migration.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 40 | |
| 41 | static const struct option OPTIONS[] = { |
| 42 | { "send_intent", required_argument, NULL, 's' }, |
| 43 | { "update_package", required_argument, NULL, 'u' }, |
| 44 | { "wipe_data", no_argument, NULL, 'w' }, |
| 45 | { "wipe_cache", no_argument, NULL, 'c' }, |
Doug Zongker | efa1bab | 2010-02-01 15:59:12 -0800 | [diff] [blame] | 46 | // TODO{oam}: implement improved command line passing key, egnor to review. |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 47 | { "set_encrypted_filesystem", required_argument, NULL, 'e' }, |
Doug Zongker | 988500b | 2009-10-06 14:41:38 -0700 | [diff] [blame] | 48 | { NULL, 0, NULL, 0 }, |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static const char *COMMAND_FILE = "CACHE:recovery/command"; |
| 52 | static const char *INTENT_FILE = "CACHE:recovery/intent"; |
| 53 | static const char *LOG_FILE = "CACHE:recovery/log"; |
| 54 | static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip"; |
| 55 | static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; |
| 56 | |
| 57 | /* |
| 58 | * The recovery tool communicates with the main system through /cache files. |
| 59 | * /cache/recovery/command - INPUT - command line for tool, one arg per line |
| 60 | * /cache/recovery/log - OUTPUT - combined log file from recovery run(s) |
| 61 | * /cache/recovery/intent - OUTPUT - intent that was passed in |
| 62 | * |
| 63 | * The arguments which may be supplied in the recovery.command file: |
| 64 | * --send_intent=anystring - write the text out to recovery.intent |
| 65 | * --update_package=root:path - verify install an OTA package file |
| 66 | * --wipe_data - erase user data (and cache), then reboot |
| 67 | * --wipe_cache - wipe cache (but not user data), then reboot |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 68 | * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 69 | * |
| 70 | * After completing, we remove /cache/recovery/command and reboot. |
| 71 | * Arguments may also be supplied in the bootloader control block (BCB). |
| 72 | * These important scenarios must be safely restartable at any point: |
| 73 | * |
| 74 | * FACTORY RESET |
| 75 | * 1. user selects "factory reset" |
| 76 | * 2. main system writes "--wipe_data" to /cache/recovery/command |
| 77 | * 3. main system reboots into recovery |
| 78 | * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data" |
| 79 | * -- after this, rebooting will restart the erase -- |
| 80 | * 5. erase_root() reformats /data |
| 81 | * 6. erase_root() reformats /cache |
| 82 | * 7. finish_recovery() erases BCB |
| 83 | * -- after this, rebooting will restart the main system -- |
| 84 | * 8. main() calls reboot() to boot main system |
| 85 | * |
| 86 | * OTA INSTALL |
| 87 | * 1. main system downloads OTA package to /cache/some-filename.zip |
| 88 | * 2. main system writes "--update_package=CACHE:some-filename.zip" |
| 89 | * 3. main system reboots into recovery |
| 90 | * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..." |
| 91 | * -- after this, rebooting will attempt to reinstall the update -- |
| 92 | * 5. install_package() attempts to install the update |
| 93 | * NOTE: the package install must itself be restartable from any point |
| 94 | * 6. finish_recovery() erases BCB |
| 95 | * -- after this, rebooting will (try to) restart the main system -- |
| 96 | * 7. ** if install failed ** |
| 97 | * 7a. prompt_and_wait() shows an error icon and waits for the user |
| 98 | * 7b; the user reboots (pulling the battery, etc) into the main system |
| 99 | * 8. main() calls maybe_install_firmware_update() |
| 100 | * ** if the update contained radio/hboot firmware **: |
| 101 | * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache" |
| 102 | * -- after this, rebooting will reformat cache & restart main system -- |
| 103 | * 8b. m_i_f_u() writes firmware image into raw cache partition |
| 104 | * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache" |
| 105 | * -- after this, rebooting will attempt to reinstall firmware -- |
| 106 | * 8d. bootloader tries to flash firmware |
| 107 | * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache") |
| 108 | * -- after this, rebooting will reformat cache & restart main system -- |
| 109 | * 8f. erase_root() reformats /cache |
| 110 | * 8g. finish_recovery() erases BCB |
| 111 | * -- after this, rebooting will (try to) restart the main system -- |
| 112 | * 9. main() calls reboot() to boot main system |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 113 | * |
| 114 | * ENCRYPTED FILE SYSTEMS ENABLE/DISABLE |
| 115 | * 1. user selects "enable encrypted file systems" |
| 116 | * 2. main system writes "--set_encrypted_filesystem=on|off" to |
| 117 | * /cache/recovery/command |
| 118 | * 3. main system reboots into recovery |
| 119 | * 4. get_args() writes BCB with "boot-recovery" and |
| 120 | * "--set_encrypted_filesystems=on|off" |
| 121 | * -- after this, rebooting will restart the transition -- |
| 122 | * 5. read_encrypted_fs_info() retrieves encrypted file systems settings from /data |
| 123 | * Settings include: property to specify the Encrypted FS istatus and |
| 124 | * FS encryption key if enabled (not yet implemented) |
| 125 | * 6. erase_root() reformats /data |
| 126 | * 7. erase_root() reformats /cache |
| 127 | * 8. restore_encrypted_fs_info() writes required encrypted file systems settings to /data |
| 128 | * Settings include: property to specify the Encrypted FS status and |
| 129 | * FS encryption key if enabled (not yet implemented) |
| 130 | * 9. finish_recovery() erases BCB |
| 131 | * -- after this, rebooting will restart the main system -- |
| 132 | * 10. main() calls reboot() to boot main system |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 133 | */ |
| 134 | |
| 135 | static const int MAX_ARG_LENGTH = 4096; |
| 136 | static const int MAX_ARGS = 100; |
| 137 | |
| 138 | // open a file given in root:path format, mounting partitions as necessary |
| 139 | static FILE* |
| 140 | fopen_root_path(const char *root_path, const char *mode) { |
| 141 | if (ensure_root_path_mounted(root_path) != 0) { |
| 142 | LOGE("Can't mount %s\n", root_path); |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | char path[PATH_MAX] = ""; |
| 147 | if (translate_root_path(root_path, path, sizeof(path)) == NULL) { |
| 148 | LOGE("Bad path %s\n", root_path); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | // When writing, try to create the containing directory, if necessary. |
| 153 | // Use generous permissions, the system (init.rc) will reset them. |
| 154 | if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1); |
| 155 | |
| 156 | FILE *fp = fopen(path, mode); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 157 | return fp; |
| 158 | } |
| 159 | |
| 160 | // close a file, log an error if the error indicator is set |
| 161 | static void |
| 162 | check_and_fclose(FILE *fp, const char *name) { |
| 163 | fflush(fp); |
| 164 | if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno)); |
| 165 | fclose(fp); |
| 166 | } |
| 167 | |
| 168 | // command line args come from, in decreasing precedence: |
| 169 | // - the actual command line |
| 170 | // - the bootloader control block (one per line, after "recovery") |
| 171 | // - the contents of COMMAND_FILE (one per line) |
| 172 | static void |
| 173 | get_args(int *argc, char ***argv) { |
| 174 | struct bootloader_message boot; |
| 175 | memset(&boot, 0, sizeof(boot)); |
| 176 | get_bootloader_message(&boot); // this may fail, leaving a zeroed structure |
| 177 | |
| 178 | if (boot.command[0] != 0 && boot.command[0] != 255) { |
| 179 | LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command); |
| 180 | } |
| 181 | |
| 182 | if (boot.status[0] != 0 && boot.status[0] != 255) { |
| 183 | LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status); |
| 184 | } |
| 185 | |
| 186 | // --- if arguments weren't supplied, look in the bootloader control block |
| 187 | if (*argc <= 1) { |
| 188 | boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination |
| 189 | const char *arg = strtok(boot.recovery, "\n"); |
| 190 | if (arg != NULL && !strcmp(arg, "recovery")) { |
| 191 | *argv = (char **) malloc(sizeof(char *) * MAX_ARGS); |
| 192 | (*argv)[0] = strdup(arg); |
| 193 | for (*argc = 1; *argc < MAX_ARGS; ++*argc) { |
| 194 | if ((arg = strtok(NULL, "\n")) == NULL) break; |
| 195 | (*argv)[*argc] = strdup(arg); |
| 196 | } |
| 197 | LOGI("Got arguments from boot message\n"); |
| 198 | } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) { |
| 199 | LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // --- if that doesn't work, try the command file |
| 204 | if (*argc <= 1) { |
| 205 | FILE *fp = fopen_root_path(COMMAND_FILE, "r"); |
| 206 | if (fp != NULL) { |
| 207 | char *argv0 = (*argv)[0]; |
| 208 | *argv = (char **) malloc(sizeof(char *) * MAX_ARGS); |
| 209 | (*argv)[0] = argv0; // use the same program name |
| 210 | |
| 211 | char buf[MAX_ARG_LENGTH]; |
| 212 | for (*argc = 1; *argc < MAX_ARGS; ++*argc) { |
| 213 | if (!fgets(buf, sizeof(buf), fp)) break; |
| 214 | (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline. |
| 215 | } |
| 216 | |
| 217 | check_and_fclose(fp, COMMAND_FILE); |
| 218 | LOGI("Got arguments from %s\n", COMMAND_FILE); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // --> write the arguments we have back into the bootloader control block |
| 223 | // always boot into recovery after this (until finish_recovery() is called) |
| 224 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 225 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 226 | int i; |
| 227 | for (i = 1; i < *argc; ++i) { |
| 228 | strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery)); |
| 229 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 230 | } |
| 231 | set_bootloader_message(&boot); |
| 232 | } |
| 233 | |
Doug Zongker | 34c98df | 2009-08-18 12:05:45 -0700 | [diff] [blame] | 234 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 235 | set_sdcard_update_bootloader_message() { |
Doug Zongker | 34c98df | 2009-08-18 12:05:45 -0700 | [diff] [blame] | 236 | struct bootloader_message boot; |
| 237 | memset(&boot, 0, sizeof(boot)); |
| 238 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 239 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 240 | set_bootloader_message(&boot); |
| 241 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 242 | |
| 243 | // clear the recovery command and prepare to boot a (hopefully working) system, |
| 244 | // copy our log file to cache as well (for the system to read), and |
| 245 | // record any intent we were asked to communicate back to the system. |
| 246 | // this function is idempotent: call it as many times as you like. |
| 247 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 248 | finish_recovery(const char *send_intent) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 249 | // By this point, we're ready to return to the main system... |
| 250 | if (send_intent != NULL) { |
| 251 | FILE *fp = fopen_root_path(INTENT_FILE, "w"); |
Jay Freeman (saurik) | 619ec2f | 2008-11-17 01:56:05 +0000 | [diff] [blame] | 252 | if (fp == NULL) { |
| 253 | LOGE("Can't open %s\n", INTENT_FILE); |
| 254 | } else { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 255 | fputs(send_intent, fp); |
| 256 | check_and_fclose(fp, INTENT_FILE); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // Copy logs to cache so the system can find out what happened. |
| 261 | FILE *log = fopen_root_path(LOG_FILE, "a"); |
Jay Freeman (saurik) | 619ec2f | 2008-11-17 01:56:05 +0000 | [diff] [blame] | 262 | if (log == NULL) { |
| 263 | LOGE("Can't open %s\n", LOG_FILE); |
| 264 | } else { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 265 | FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r"); |
| 266 | if (tmplog == NULL) { |
| 267 | LOGE("Can't open %s\n", TEMPORARY_LOG_FILE); |
| 268 | } else { |
| 269 | static long tmplog_offset = 0; |
| 270 | fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write |
| 271 | char buf[4096]; |
| 272 | while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log); |
| 273 | tmplog_offset = ftell(tmplog); |
| 274 | check_and_fclose(tmplog, TEMPORARY_LOG_FILE); |
| 275 | } |
| 276 | check_and_fclose(log, LOG_FILE); |
| 277 | } |
| 278 | |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 279 | // Reset to mormal system boot so recovery won't cycle indefinitely. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 280 | struct bootloader_message boot; |
| 281 | memset(&boot, 0, sizeof(boot)); |
| 282 | set_bootloader_message(&boot); |
| 283 | |
| 284 | // Remove the command file, so recovery won't repeat indefinitely. |
| 285 | char path[PATH_MAX] = ""; |
| 286 | if (ensure_root_path_mounted(COMMAND_FILE) != 0 || |
| 287 | translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL || |
| 288 | (unlink(path) && errno != ENOENT)) { |
| 289 | LOGW("Can't unlink %s\n", COMMAND_FILE); |
| 290 | } |
| 291 | |
| 292 | sync(); // For good measure. |
| 293 | } |
| 294 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 295 | static int |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 296 | erase_root(const char *root) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 297 | ui_set_background(BACKGROUND_ICON_INSTALLING); |
| 298 | ui_show_indeterminate_progress(); |
| 299 | ui_print("Formatting %s...\n", root); |
| 300 | return format_root_device(root); |
| 301 | } |
| 302 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 303 | static char** |
| 304 | prepend_title(char** headers) { |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 305 | char* title[] = { "Android system recovery <" |
Doug Zongker | 64893cc | 2009-07-14 16:31:56 -0700 | [diff] [blame] | 306 | EXPAND(RECOVERY_API_VERSION) "e>", |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 307 | "", |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 308 | NULL }; |
| 309 | |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 310 | // count the number of lines in our title, plus the |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 311 | // caller-provided headers. |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 312 | int count = 0; |
| 313 | char** p; |
| 314 | for (p = title; *p; ++p, ++count); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 315 | for (p = headers; *p; ++p, ++count); |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 316 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 317 | char** new_headers = malloc((count+1) * sizeof(char*)); |
| 318 | char** h = new_headers; |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 319 | for (p = title; *p; ++p, ++h) *h = *p; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 320 | for (p = headers; *p; ++p, ++h) *h = *p; |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 321 | *h = NULL; |
| 322 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 323 | return new_headers; |
| 324 | } |
| 325 | |
| 326 | static int |
| 327 | get_menu_selection(char** headers, char** items, int menu_only) { |
| 328 | // throw away keys pressed previously, so user doesn't |
| 329 | // accidentally trigger menu items. |
| 330 | ui_clear_key_queue(); |
| 331 | |
| 332 | ui_start_menu(headers, items); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 333 | int selected = 0; |
| 334 | int chosen_item = -1; |
| 335 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 336 | while (chosen_item < 0) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 337 | int key = ui_wait_key(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 338 | int visible = ui_text_visible(); |
| 339 | |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 340 | int action = device_handle_key(key, visible); |
| 341 | |
| 342 | if (action < 0) { |
| 343 | switch (action) { |
| 344 | case HIGHLIGHT_UP: |
| 345 | --selected; |
| 346 | selected = ui_menu_select(selected); |
| 347 | break; |
| 348 | case HIGHLIGHT_DOWN: |
| 349 | ++selected; |
| 350 | selected = ui_menu_select(selected); |
| 351 | break; |
| 352 | case SELECT_ITEM: |
| 353 | chosen_item = selected; |
| 354 | break; |
| 355 | case NO_ACTION: |
| 356 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 357 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 358 | } else if (!menu_only) { |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 359 | chosen_item = action; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 360 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 361 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 362 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 363 | ui_end_menu(); |
| 364 | return chosen_item; |
| 365 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 366 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 367 | static void |
| 368 | wipe_data(int confirm) { |
| 369 | if (confirm) { |
| 370 | static char** title_headers = NULL; |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 371 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 372 | if (title_headers == NULL) { |
| 373 | char* headers[] = { "Confirm wipe of all user data?", |
| 374 | " THIS CAN NOT BE UNDONE.", |
| 375 | "", |
| 376 | NULL }; |
| 377 | title_headers = prepend_title(headers); |
| 378 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 379 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 380 | char* items[] = { " No", |
| 381 | " No", |
| 382 | " No", |
| 383 | " No", |
| 384 | " No", |
| 385 | " No", |
| 386 | " No", |
| 387 | " Yes -- delete all user data", // [7] |
| 388 | " No", |
| 389 | " No", |
| 390 | " No", |
| 391 | NULL }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 392 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 393 | int chosen_item = get_menu_selection(title_headers, items, 1); |
| 394 | if (chosen_item != 7) { |
| 395 | return; |
| 396 | } |
| 397 | } |
Doug Zongker | 1066d2c | 2009-04-01 13:57:40 -0700 | [diff] [blame] | 398 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 399 | ui_print("\n-- Wiping data...\n"); |
| 400 | device_wipe_data(); |
| 401 | erase_root("DATA:"); |
| 402 | erase_root("CACHE:"); |
| 403 | ui_print("Data wipe complete.\n"); |
| 404 | } |
| 405 | |
| 406 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 407 | prompt_and_wait() { |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 408 | char** headers = prepend_title(MENU_HEADERS); |
| 409 | |
| 410 | for (;;) { |
| 411 | finish_recovery(NULL); |
| 412 | ui_reset_progress(); |
| 413 | |
| 414 | int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0); |
| 415 | |
| 416 | // device-specific code may take some action here. It may |
| 417 | // return one of the core actions handled in the switch |
| 418 | // statement below. |
| 419 | chosen_item = device_perform_action(chosen_item); |
| 420 | |
| 421 | switch (chosen_item) { |
| 422 | case ITEM_REBOOT: |
| 423 | return; |
| 424 | |
| 425 | case ITEM_WIPE_DATA: |
| 426 | wipe_data(ui_text_visible()); |
| 427 | if (!ui_text_visible()) return; |
| 428 | break; |
| 429 | |
| 430 | case ITEM_WIPE_CACHE: |
| 431 | ui_print("\n-- Wiping cache...\n"); |
| 432 | erase_root("CACHE:"); |
| 433 | ui_print("Cache wipe complete.\n"); |
| 434 | if (!ui_text_visible()) return; |
| 435 | break; |
| 436 | |
| 437 | case ITEM_APPLY_SDCARD: |
| 438 | ui_print("\n-- Install from sdcard...\n"); |
| 439 | set_sdcard_update_bootloader_message(); |
| 440 | int status = install_package(SDCARD_PACKAGE_FILE); |
| 441 | if (status != INSTALL_SUCCESS) { |
| 442 | ui_set_background(BACKGROUND_ICON_ERROR); |
| 443 | ui_print("Installation aborted.\n"); |
| 444 | } else if (!ui_text_visible()) { |
| 445 | return; // reboot if logs aren't visible |
| 446 | } else { |
Doug Zongker | e08991e | 2010-02-02 13:09:52 -0800 | [diff] [blame] | 447 | ui_print("\nInstall from sdcard complete.\n"); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 448 | } |
| 449 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 455 | print_property(const char *key, const char *name, void *cookie) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 456 | fprintf(stderr, "%s=%s\n", key, name); |
| 457 | } |
| 458 | |
| 459 | int |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 460 | main(int argc, char **argv) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 461 | time_t start = time(NULL); |
| 462 | |
| 463 | // If these fail, there's not really anywhere to complain... |
| 464 | freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL); |
| 465 | freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL); |
| 466 | fprintf(stderr, "Starting recovery on %s", ctime(&start)); |
| 467 | |
| 468 | ui_init(); |
| 469 | get_args(&argc, &argv); |
| 470 | |
| 471 | int previous_runs = 0; |
| 472 | const char *send_intent = NULL; |
| 473 | const char *update_package = NULL; |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 474 | const char *efs_mode = NULL; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 475 | int wipe_data = 0, wipe_cache = 0; |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 476 | int toggle_efs = 0; |
| 477 | encrypted_fs_info efs_data; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 478 | |
| 479 | int arg; |
| 480 | while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) { |
| 481 | switch (arg) { |
| 482 | case 'p': previous_runs = atoi(optarg); break; |
| 483 | case 's': send_intent = optarg; break; |
| 484 | case 'u': update_package = optarg; break; |
| 485 | case 'w': wipe_data = wipe_cache = 1; break; |
| 486 | case 'c': wipe_cache = 1; break; |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 487 | case 'e': efs_mode = optarg; toggle_efs = 1; break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 488 | case '?': |
| 489 | LOGE("Invalid command argument\n"); |
| 490 | continue; |
| 491 | } |
| 492 | } |
| 493 | |
Doug Zongker | efa1bab | 2010-02-01 15:59:12 -0800 | [diff] [blame] | 494 | device_recovery_start(); |
| 495 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 496 | fprintf(stderr, "Command:"); |
| 497 | for (arg = 0; arg < argc; arg++) { |
| 498 | fprintf(stderr, " \"%s\"", argv[arg]); |
| 499 | } |
| 500 | fprintf(stderr, "\n\n"); |
| 501 | |
| 502 | property_list(print_property, NULL); |
| 503 | fprintf(stderr, "\n"); |
| 504 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 505 | int status = INSTALL_SUCCESS; |
| 506 | |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 507 | if (toggle_efs) { |
| 508 | if (strcmp(efs_mode,"on") == 0) { |
| 509 | efs_data.encrypted_fs_mode = MODE_ENCRYPTEDFS_ENABLED; |
| 510 | ui_print("Enabling Encrypted FS.\n"); |
| 511 | } else if (strcmp(efs_mode,"off") == 0) { |
| 512 | efs_data.encrypted_fs_mode = MODE_ENCRYPTEDFS_DISABLED; |
| 513 | ui_print("Disabling Encrypted FS.\n"); |
| 514 | } else { |
| 515 | ui_print("Error: invalid Encrypted FS setting.\n"); |
| 516 | status = INSTALL_ERROR; |
| 517 | } |
| 518 | |
| 519 | // Recovery strategy: if the data partition is damaged, disable encrypted file systems. |
| 520 | // This preventsthe device recycling endlessly in recovery mode. |
| 521 | // TODO{oam}: implement improved recovery strategy later. egnor to review. |
| 522 | if (read_encrypted_fs_info(&efs_data)) { |
| 523 | ui_print("Encrypted FS change aborted, resetting to disabled state.\n"); |
| 524 | efs_data.encrypted_fs_mode = MODE_ENCRYPTEDFS_DISABLED; |
| 525 | } |
| 526 | |
| 527 | if (status != INSTALL_ERROR) { |
| 528 | if (erase_root("DATA:")) { |
| 529 | ui_print("Data wipe failed.\n"); |
| 530 | status = INSTALL_ERROR; |
| 531 | } else if (erase_root("CACHE:")) { |
| 532 | ui_print("Cache wipe failed.\n"); |
| 533 | status = INSTALL_ERROR; |
| 534 | } else if (restore_encrypted_fs_info(&efs_data)) { |
| 535 | ui_print("Encrypted FS change aborted.\n"); |
| 536 | status = INSTALL_ERROR; |
| 537 | } else { |
| 538 | ui_print("Successfully updated Encrypted FS.\n"); |
| 539 | status = INSTALL_SUCCESS; |
| 540 | } |
| 541 | } |
| 542 | } else if (update_package != NULL) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 543 | status = install_package(update_package); |
| 544 | if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n"); |
Doug Zongker | b128f54 | 2009-06-18 15:07:14 -0700 | [diff] [blame] | 545 | } else if (wipe_data) { |
| 546 | if (device_wipe_data()) status = INSTALL_ERROR; |
| 547 | if (erase_root("DATA:")) status = INSTALL_ERROR; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 548 | if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR; |
| 549 | if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n"); |
Doug Zongker | b128f54 | 2009-06-18 15:07:14 -0700 | [diff] [blame] | 550 | } else if (wipe_cache) { |
| 551 | if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR; |
| 552 | if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 553 | } else { |
| 554 | status = INSTALL_ERROR; // No command specified |
| 555 | } |
| 556 | |
| 557 | if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR); |
| 558 | if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait(); |
| 559 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 560 | // Otherwise, get ready to boot the main system... |
| 561 | finish_recovery(send_intent); |
| 562 | ui_print("Rebooting...\n"); |
| 563 | sync(); |
| 564 | reboot(RB_AUTOBOOT); |
| 565 | return EXIT_SUCCESS; |
| 566 | } |