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