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 Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame^] | 341 | // Some users with dead enter keys need a way to turn on power to select. |
| 342 | // Jiggering across the wrapping menu is one "secret" way to enable it. |
| 343 | // We can't rely on /cache or /sdcard since they may not be available. |
| 344 | int wrap_count = 0; |
| 345 | |
Koushik K. Dutta | a3c2f73 | 2010-02-19 14:17:22 -0800 | [diff] [blame] | 346 | while (chosen_item < 0 && chosen_item != GO_BACK) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 347 | int key = ui_wait_key(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 348 | int visible = ui_text_visible(); |
| 349 | |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 350 | int action = device_handle_key(key, visible); |
| 351 | |
Koushik Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame^] | 352 | int old_selected = selected; |
| 353 | |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 354 | if (action < 0) { |
| 355 | switch (action) { |
| 356 | case HIGHLIGHT_UP: |
| 357 | --selected; |
| 358 | selected = ui_menu_select(selected); |
| 359 | break; |
| 360 | case HIGHLIGHT_DOWN: |
| 361 | ++selected; |
| 362 | selected = ui_menu_select(selected); |
| 363 | break; |
| 364 | case SELECT_ITEM: |
| 365 | chosen_item = selected; |
Koushik Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame^] | 366 | if (ui_get_showing_back_button()) { |
| 367 | if (chosen_item == item_count) { |
| 368 | chosen_item = GO_BACK; |
| 369 | } |
Koushik Dutta | f4e3a67 | 2010-06-09 12:19:41 -0700 | [diff] [blame] | 370 | } |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 371 | break; |
| 372 | case NO_ACTION: |
| 373 | break; |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 374 | case GO_BACK: |
Koushik K. Dutta | a3c2f73 | 2010-02-19 14:17:22 -0800 | [diff] [blame] | 375 | chosen_item = GO_BACK; |
| 376 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 377 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 378 | } else if (!menu_only) { |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 379 | chosen_item = action; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 380 | } |
Koushik Dutta | 4ca9b4c | 2010-07-14 18:37:33 -0700 | [diff] [blame^] | 381 | |
| 382 | if (abs(selected - old_selected) > 1) { |
| 383 | wrap_count++; |
| 384 | if (wrap_count == 3) { |
| 385 | wrap_count = 0; |
| 386 | if (ui_get_showing_back_button()) { |
| 387 | ui_print("Back button disabled.\n"); |
| 388 | ui_set_showing_back_button(0); |
| 389 | } |
| 390 | else { |
| 391 | ui_print("Back button enabled.\n"); |
| 392 | ui_set_showing_back_button(1); |
| 393 | } |
| 394 | } |
| 395 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 396 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 397 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 398 | ui_end_menu(); |
Koushik K. Dutta | 981b0cd | 2010-02-22 08:53:34 -0800 | [diff] [blame] | 399 | ui_clear_key_queue(); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 400 | return chosen_item; |
| 401 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 402 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 403 | static void |
| 404 | wipe_data(int confirm) { |
| 405 | if (confirm) { |
| 406 | static char** title_headers = NULL; |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 407 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 408 | if (title_headers == NULL) { |
| 409 | char* headers[] = { "Confirm wipe of all user data?", |
| 410 | " THIS CAN NOT BE UNDONE.", |
| 411 | "", |
| 412 | NULL }; |
| 413 | title_headers = prepend_title(headers); |
| 414 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 415 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 416 | char* items[] = { " No", |
| 417 | " No", |
| 418 | " No", |
| 419 | " No", |
| 420 | " No", |
| 421 | " No", |
| 422 | " No", |
| 423 | " Yes -- delete all user data", // [7] |
| 424 | " No", |
| 425 | " No", |
| 426 | " No", |
| 427 | NULL }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 428 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 429 | int chosen_item = get_menu_selection(title_headers, items, 1); |
| 430 | if (chosen_item != 7) { |
| 431 | return; |
| 432 | } |
| 433 | } |
Doug Zongker | 1066d2c | 2009-04-01 13:57:40 -0700 | [diff] [blame] | 434 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 435 | ui_print("\n-- Wiping data...\n"); |
| 436 | device_wipe_data(); |
| 437 | erase_root("DATA:"); |
Koushik Dutta | 63e0476 | 2010-06-15 12:56:17 -0700 | [diff] [blame] | 438 | #ifdef HAS_DATADATA |
| 439 | erase_root("DATADATA:"); |
| 440 | #endif |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 441 | erase_root("CACHE:"); |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 442 | erase_root("SDEXT:"); |
Koushik Dutta | 82c2ca2 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 443 | erase_root("SDCARD:/.android_secure"); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 444 | ui_print("Data wipe complete.\n"); |
| 445 | } |
| 446 | |
| 447 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 448 | prompt_and_wait() { |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 449 | char** headers = prepend_title(MENU_HEADERS); |
Koushik K. Dutta | 261dde9 | 2010-02-25 16:51:45 -0800 | [diff] [blame] | 450 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 451 | for (;;) { |
| 452 | finish_recovery(NULL); |
| 453 | ui_reset_progress(); |
| 454 | |
Koushik K. Dutta | a3c2f73 | 2010-02-19 14:17:22 -0800 | [diff] [blame] | 455 | allow_display_toggle = 1; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 456 | int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0); |
Koushik K. Dutta | a3c2f73 | 2010-02-19 14:17:22 -0800 | [diff] [blame] | 457 | allow_display_toggle = 0; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 458 | |
| 459 | // device-specific code may take some action here. It may |
| 460 | // return one of the core actions handled in the switch |
| 461 | // statement below. |
| 462 | chosen_item = device_perform_action(chosen_item); |
| 463 | |
| 464 | switch (chosen_item) { |
| 465 | case ITEM_REBOOT: |
| 466 | return; |
| 467 | |
| 468 | case ITEM_WIPE_DATA: |
| 469 | wipe_data(ui_text_visible()); |
| 470 | if (!ui_text_visible()) return; |
| 471 | break; |
| 472 | |
| 473 | case ITEM_WIPE_CACHE: |
| 474 | ui_print("\n-- Wiping cache...\n"); |
| 475 | erase_root("CACHE:"); |
| 476 | ui_print("Cache wipe complete.\n"); |
| 477 | if (!ui_text_visible()) return; |
| 478 | break; |
| 479 | |
| 480 | case ITEM_APPLY_SDCARD: |
| 481 | ui_print("\n-- Install from sdcard...\n"); |
| 482 | set_sdcard_update_bootloader_message(); |
| 483 | int status = install_package(SDCARD_PACKAGE_FILE); |
| 484 | if (status != INSTALL_SUCCESS) { |
| 485 | ui_set_background(BACKGROUND_ICON_ERROR); |
| 486 | ui_print("Installation aborted.\n"); |
| 487 | } else if (!ui_text_visible()) { |
| 488 | return; // reboot if logs aren't visible |
| 489 | } else { |
Doug Zongker | e08991e | 2010-02-02 13:09:52 -0800 | [diff] [blame] | 490 | ui_print("\nInstall from sdcard complete.\n"); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 491 | } |
| 492 | break; |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 493 | case ITEM_INSTALL_ZIP: |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 494 | show_install_update_menu(); |
| 495 | break; |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 496 | case ITEM_NANDROID: |
| 497 | show_nandroid_menu(); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 498 | break; |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 499 | case ITEM_PARTITION: |
| 500 | show_partition_menu(); |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 501 | break; |
Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 502 | case ITEM_ADVANCED: |
| 503 | show_advanced_menu(); |
Koushik K. Dutta | 1fa52ec | 2010-02-21 21:29:10 -0800 | [diff] [blame] | 504 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 510 | 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] | 511 | fprintf(stderr, "%s=%s\n", key, name); |
| 512 | } |
| 513 | |
| 514 | int |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 515 | main(int argc, char **argv) { |
Koushik K. Dutta | 99fb6fe | 2010-03-03 00:42:58 -0800 | [diff] [blame] | 516 | if (strstr(argv[0], "recovery") == NULL) |
| 517 | { |
| 518 | if (strstr(argv[0], "flash_image") != NULL) |
| 519 | return flash_image_main(argc, argv); |
| 520 | if (strstr(argv[0], "dump_image") != NULL) |
| 521 | return dump_image_main(argc, argv); |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 522 | if (strstr(argv[0], "erase_image") != NULL) |
| 523 | return erase_image_main(argc, argv); |
Koushik K. Dutta | 99fb6fe | 2010-03-03 00:42:58 -0800 | [diff] [blame] | 524 | if (strstr(argv[0], "mkyaffs2image") != NULL) |
| 525 | return mkyaffs2image_main(argc, argv); |
| 526 | if (strstr(argv[0], "unyaffs") != NULL) |
| 527 | return unyaffs_main(argc, argv); |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 528 | if (strstr(argv[0], "amend")) |
| 529 | return amend_main(argc, argv); |
Koushik Dutta | d3cc60b | 2010-07-03 13:56:45 -0700 | [diff] [blame] | 530 | if (strstr(argv[0], "nandroid")) |
| 531 | return nandroid_main(argc, argv); |
Koushik K. Dutta | 99fb6fe | 2010-03-03 00:42:58 -0800 | [diff] [blame] | 532 | return busybox_driver(argc, argv); |
| 533 | } |
Koushik Dutta | a6522b3 | 2010-06-20 13:16:06 -0700 | [diff] [blame] | 534 | create_fstab(); |
Koushik Dutta | f8b21c2 | 2010-06-14 12:49:47 -0700 | [diff] [blame] | 535 | __system("/sbin/postrecoveryboot.sh"); |
Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 536 | |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 537 | int is_user_initiated_recovery = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 538 | time_t start = time(NULL); |
| 539 | |
| 540 | // If these fail, there's not really anywhere to complain... |
| 541 | freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL); |
| 542 | freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL); |
| 543 | fprintf(stderr, "Starting recovery on %s", ctime(&start)); |
| 544 | |
| 545 | ui_init(); |
Koushik Dutta | fdda0d6 | 2010-07-01 12:52:34 -0700 | [diff] [blame] | 546 | ui_print(EXPAND(RECOVERY_VERSION)"\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 547 | get_args(&argc, &argv); |
| 548 | |
| 549 | int previous_runs = 0; |
| 550 | const char *send_intent = NULL; |
| 551 | const char *update_package = NULL; |
| 552 | int wipe_data = 0, wipe_cache = 0; |
| 553 | |
| 554 | int arg; |
| 555 | while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) { |
| 556 | switch (arg) { |
| 557 | case 'p': previous_runs = atoi(optarg); break; |
| 558 | case 's': send_intent = optarg; break; |
| 559 | case 'u': update_package = optarg; break; |
| 560 | case 'w': wipe_data = wipe_cache = 1; break; |
| 561 | case 'c': wipe_cache = 1; break; |
| 562 | case '?': |
| 563 | LOGE("Invalid command argument\n"); |
| 564 | continue; |
| 565 | } |
| 566 | } |
| 567 | |
Doug Zongker | efa1bab | 2010-02-01 15:59:12 -0800 | [diff] [blame] | 568 | device_recovery_start(); |
| 569 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 570 | fprintf(stderr, "Command:"); |
| 571 | for (arg = 0; arg < argc; arg++) { |
| 572 | fprintf(stderr, " \"%s\"", argv[arg]); |
| 573 | } |
| 574 | fprintf(stderr, "\n\n"); |
| 575 | |
| 576 | property_list(print_property, NULL); |
| 577 | fprintf(stderr, "\n"); |
| 578 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 579 | int status = INSTALL_SUCCESS; |
Koushik K. Dutta | 6060e5c | 2010-02-11 22:27:06 -0800 | [diff] [blame] | 580 | |
| 581 | RecoveryCommandContext ctx = { NULL }; |
| 582 | if (register_update_commands(&ctx)) { |
| 583 | LOGE("Can't install update commands\n"); |
| 584 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 585 | |
| 586 | if (update_package != NULL) { |
Chris Soyars | a1749d9 | 2010-02-26 02:45:55 -0500 | [diff] [blame] | 587 | if (wipe_data && erase_root("DATA:")) status = INSTALL_ERROR; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 588 | status = install_package(update_package); |
| 589 | if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n"); |
Doug Zongker | b128f54 | 2009-06-18 15:07:14 -0700 | [diff] [blame] | 590 | } else if (wipe_data) { |
| 591 | if (device_wipe_data()) status = INSTALL_ERROR; |
| 592 | if (erase_root("DATA:")) status = INSTALL_ERROR; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 593 | if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR; |
| 594 | if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n"); |
Doug Zongker | b128f54 | 2009-06-18 15:07:14 -0700 | [diff] [blame] | 595 | } else if (wipe_cache) { |
| 596 | if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR; |
| 597 | 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] | 598 | } else { |
Koushik Dutta | f4e3a67 | 2010-06-09 12:19:41 -0700 | [diff] [blame] | 599 | LOGI("Checking for extendedcommand...\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 600 | status = INSTALL_ERROR; // No command specified |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 601 | // we are starting up in user initiated recovery here |
| 602 | // let's set up some default options |
| 603 | signature_check_enabled = 0; |
Koushik K. Dutta | 2bda3e9 | 2010-03-06 16:40:52 -0800 | [diff] [blame] | 604 | script_assert_enabled = 0; |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 605 | is_user_initiated_recovery = 1; |
| 606 | ui_set_show_text(1); |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 607 | |
Koushik K. Dutta | 32e4111 | 2010-03-07 14:11:56 -0800 | [diff] [blame] | 608 | if (extendedcommand_file_exists()) { |
Koushik Dutta | f4e3a67 | 2010-06-09 12:19:41 -0700 | [diff] [blame] | 609 | LOGI("Running extendedcommand...\n"); |
Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 610 | int ret; |
| 611 | if (0 == (ret = run_and_remove_extendedcommand())) { |
Koushik K. Dutta | 32e4111 | 2010-03-07 14:11:56 -0800 | [diff] [blame] | 612 | status = INSTALL_SUCCESS; |
Koushik K. Dutta | 13d8fcc | 2010-03-07 14:15:14 -0800 | [diff] [blame] | 613 | ui_set_show_text(0); |
Koushik K. Dutta | 32e4111 | 2010-03-07 14:11:56 -0800 | [diff] [blame] | 614 | } |
Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 615 | else { |
| 616 | handle_failure(ret); |
| 617 | } |
Koushik Dutta | f4e3a67 | 2010-06-09 12:19:41 -0700 | [diff] [blame] | 618 | } else { |
| 619 | LOGI("Skipping execution of extendedcommand, file not found...\n"); |
Koushik K. Dutta | 32e4111 | 2010-03-07 14:11:56 -0800 | [diff] [blame] | 620 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 623 | 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] | 624 | if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait(); |
| 625 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 626 | // Otherwise, get ready to boot the main system... |
| 627 | finish_recovery(send_intent); |
| 628 | ui_print("Rebooting...\n"); |
| 629 | sync(); |
| 630 | reboot(RB_AUTOBOOT); |
| 631 | return EXIT_SUCCESS; |
| 632 | } |
Koushik K. Dutta | a3c2f73 | 2010-02-19 14:17:22 -0800 | [diff] [blame] | 633 | |
| 634 | int get_allow_toggle_display() { |
| 635 | return allow_display_toggle; |
Chris Soyars | a1749d9 | 2010-02-26 02:45:55 -0500 | [diff] [blame] | 636 | } |