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