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