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