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