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