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> |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 27 | #include <sys/stat.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <time.h> |
| 30 | #include <unistd.h> |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 31 | #include <dirent.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 32 | |
| 33 | #include "bootloader.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | #include "common.h" |
| 35 | #include "cutils/properties.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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" |
Oscar Montemayor | 52219a6 | 2010-02-25 16:47:02 -0800 | [diff] [blame] | 41 | #include "encryptedfs_provisioning.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 42 | |
| 43 | static const struct option OPTIONS[] = { |
| 44 | { "send_intent", required_argument, NULL, 's' }, |
| 45 | { "update_package", required_argument, NULL, 'u' }, |
| 46 | { "wipe_data", no_argument, NULL, 'w' }, |
| 47 | { "wipe_cache", no_argument, NULL, 'c' }, |
Oscar Montemayor | 52219a6 | 2010-02-25 16:47:02 -0800 | [diff] [blame] | 48 | { "set_encrypted_filesystems", required_argument, NULL, 'e' }, |
Doug Zongker | 4bc9806 | 2010-09-03 11:00:13 -0700 | [diff] [blame] | 49 | { "show_text", no_argument, NULL, 't' }, |
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 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 53 | static const char *COMMAND_FILE = "/cache/recovery/command"; |
| 54 | static const char *INTENT_FILE = "/cache/recovery/intent"; |
| 55 | static const char *LOG_FILE = "/cache/recovery/log"; |
| 56 | static const char *SDCARD_ROOT = "/sdcard"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 57 | static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 58 | static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 68 | * --update_package=path - verify install an OTA package file |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 -- |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 83 | * 5. erase_volume() reformats /data |
| 84 | * 6. erase_volume() reformats /cache |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 -- |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 112 | * 8f. erase_volume() reformats /cache |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 | * |
Oscar Montemayor | 52219a6 | 2010-02-25 16:47:02 -0800 | [diff] [blame] | 117 | * SECURE FILE SYSTEMS ENABLE/DISABLE |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 118 | * 1. user selects "enable encrypted file systems" |
Oscar Montemayor | 52219a6 | 2010-02-25 16:47:02 -0800 | [diff] [blame] | 119 | * 2. main system writes "--set_encrypted_filesystems=on|off" to |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 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) |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 128 | * 6. erase_volume() reformats /data |
| 129 | * 7. erase_volume() reformats /cache |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 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 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 141 | // open a given path, mounting partitions as necessary |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 142 | static FILE* |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 143 | fopen_path(const char *path, const char *mode) { |
| 144 | if (ensure_path_mounted(path) != 0) { |
| 145 | LOGE("Can't mount %s\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | // When writing, try to create the containing directory, if necessary. |
| 150 | // Use generous permissions, the system (init.rc) will reset them. |
| 151 | if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1); |
| 152 | |
| 153 | FILE *fp = fopen(path, mode); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 154 | return fp; |
| 155 | } |
| 156 | |
| 157 | // close a file, log an error if the error indicator is set |
| 158 | static void |
| 159 | check_and_fclose(FILE *fp, const char *name) { |
| 160 | fflush(fp); |
| 161 | if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno)); |
| 162 | fclose(fp); |
| 163 | } |
| 164 | |
| 165 | // command line args come from, in decreasing precedence: |
| 166 | // - the actual command line |
| 167 | // - the bootloader control block (one per line, after "recovery") |
| 168 | // - the contents of COMMAND_FILE (one per line) |
| 169 | static void |
| 170 | get_args(int *argc, char ***argv) { |
| 171 | struct bootloader_message boot; |
| 172 | memset(&boot, 0, sizeof(boot)); |
| 173 | get_bootloader_message(&boot); // this may fail, leaving a zeroed structure |
| 174 | |
| 175 | if (boot.command[0] != 0 && boot.command[0] != 255) { |
| 176 | LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command); |
| 177 | } |
| 178 | |
| 179 | if (boot.status[0] != 0 && boot.status[0] != 255) { |
| 180 | LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status); |
| 181 | } |
| 182 | |
| 183 | // --- if arguments weren't supplied, look in the bootloader control block |
| 184 | if (*argc <= 1) { |
| 185 | boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination |
| 186 | const char *arg = strtok(boot.recovery, "\n"); |
| 187 | if (arg != NULL && !strcmp(arg, "recovery")) { |
| 188 | *argv = (char **) malloc(sizeof(char *) * MAX_ARGS); |
| 189 | (*argv)[0] = strdup(arg); |
| 190 | for (*argc = 1; *argc < MAX_ARGS; ++*argc) { |
| 191 | if ((arg = strtok(NULL, "\n")) == NULL) break; |
| 192 | (*argv)[*argc] = strdup(arg); |
| 193 | } |
| 194 | LOGI("Got arguments from boot message\n"); |
| 195 | } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) { |
| 196 | LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // --- if that doesn't work, try the command file |
| 201 | if (*argc <= 1) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 202 | FILE *fp = fopen_path(COMMAND_FILE, "r"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 203 | if (fp != NULL) { |
| 204 | char *argv0 = (*argv)[0]; |
| 205 | *argv = (char **) malloc(sizeof(char *) * MAX_ARGS); |
| 206 | (*argv)[0] = argv0; // use the same program name |
| 207 | |
| 208 | char buf[MAX_ARG_LENGTH]; |
| 209 | for (*argc = 1; *argc < MAX_ARGS; ++*argc) { |
| 210 | if (!fgets(buf, sizeof(buf), fp)) break; |
| 211 | (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline. |
| 212 | } |
| 213 | |
| 214 | check_and_fclose(fp, COMMAND_FILE); |
| 215 | LOGI("Got arguments from %s\n", COMMAND_FILE); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // --> write the arguments we have back into the bootloader control block |
| 220 | // always boot into recovery after this (until finish_recovery() is called) |
| 221 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 222 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 223 | int i; |
| 224 | for (i = 1; i < *argc; ++i) { |
| 225 | strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery)); |
| 226 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 227 | } |
| 228 | set_bootloader_message(&boot); |
| 229 | } |
| 230 | |
Doug Zongker | 34c98df | 2009-08-18 12:05:45 -0700 | [diff] [blame] | 231 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 232 | set_sdcard_update_bootloader_message() { |
Doug Zongker | 34c98df | 2009-08-18 12:05:45 -0700 | [diff] [blame] | 233 | struct bootloader_message boot; |
| 234 | memset(&boot, 0, sizeof(boot)); |
| 235 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 236 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 237 | set_bootloader_message(&boot); |
| 238 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 239 | |
| 240 | // clear the recovery command and prepare to boot a (hopefully working) system, |
| 241 | // copy our log file to cache as well (for the system to read), and |
| 242 | // record any intent we were asked to communicate back to the system. |
| 243 | // this function is idempotent: call it as many times as you like. |
| 244 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 245 | finish_recovery(const char *send_intent) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 246 | // By this point, we're ready to return to the main system... |
| 247 | if (send_intent != NULL) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 248 | FILE *fp = fopen_path(INTENT_FILE, "w"); |
Jay Freeman (saurik) | 619ec2f | 2008-11-17 01:56:05 +0000 | [diff] [blame] | 249 | if (fp == NULL) { |
| 250 | LOGE("Can't open %s\n", INTENT_FILE); |
| 251 | } else { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 252 | fputs(send_intent, fp); |
| 253 | check_and_fclose(fp, INTENT_FILE); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Copy logs to cache so the system can find out what happened. |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 258 | FILE *log = fopen_path(LOG_FILE, "a"); |
Jay Freeman (saurik) | 619ec2f | 2008-11-17 01:56:05 +0000 | [diff] [blame] | 259 | if (log == NULL) { |
| 260 | LOGE("Can't open %s\n", LOG_FILE); |
| 261 | } else { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 262 | FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r"); |
| 263 | if (tmplog == NULL) { |
| 264 | LOGE("Can't open %s\n", TEMPORARY_LOG_FILE); |
| 265 | } else { |
| 266 | static long tmplog_offset = 0; |
| 267 | fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write |
| 268 | char buf[4096]; |
| 269 | while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log); |
| 270 | tmplog_offset = ftell(tmplog); |
| 271 | check_and_fclose(tmplog, TEMPORARY_LOG_FILE); |
| 272 | } |
| 273 | check_and_fclose(log, LOG_FILE); |
| 274 | } |
| 275 | |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 276 | // 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] | 277 | struct bootloader_message boot; |
| 278 | memset(&boot, 0, sizeof(boot)); |
| 279 | set_bootloader_message(&boot); |
| 280 | |
| 281 | // Remove the command file, so recovery won't repeat indefinitely. |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 282 | if (ensure_path_mounted(COMMAND_FILE) != 0 || |
| 283 | (unlink(COMMAND_FILE) && errno != ENOENT)) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 284 | LOGW("Can't unlink %s\n", COMMAND_FILE); |
| 285 | } |
| 286 | |
| 287 | sync(); // For good measure. |
| 288 | } |
| 289 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 290 | static int |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 291 | erase_volume(const char *volume) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 292 | ui_set_background(BACKGROUND_ICON_INSTALLING); |
| 293 | ui_show_indeterminate_progress(); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 294 | ui_print("Formatting %s...\n", volume); |
| 295 | return format_volume(volume); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 298 | static char* |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 299 | copy_sideloaded_package(const char* original_path) { |
| 300 | if (ensure_path_mounted(original_path) != 0) { |
| 301 | LOGE("Can't mount %s\n", original_path); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 302 | return NULL; |
| 303 | } |
| 304 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 305 | if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) { |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 306 | LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR); |
| 307 | return NULL; |
| 308 | } |
| 309 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 310 | if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) { |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 311 | if (errno != EEXIST) { |
| 312 | LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno)); |
| 313 | return NULL; |
| 314 | } |
| 315 | } |
| 316 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 317 | // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a |
| 318 | // directory, owned by root, readable and writable only by root. |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 319 | struct stat st; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 320 | if (stat(SIDELOAD_TEMP_DIR, &st) != 0) { |
| 321 | LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno)); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 322 | return NULL; |
| 323 | } |
| 324 | if (!S_ISDIR(st.st_mode)) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 325 | LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 326 | return NULL; |
| 327 | } |
| 328 | if ((st.st_mode & 0777) != 0700) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 329 | LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 330 | return NULL; |
| 331 | } |
| 332 | if (st.st_uid != 0) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 333 | LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 334 | return NULL; |
| 335 | } |
| 336 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 337 | char copy_path[PATH_MAX]; |
| 338 | strcpy(copy_path, SIDELOAD_TEMP_DIR); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 339 | strcat(copy_path, "/package.zip"); |
| 340 | |
| 341 | char* buffer = malloc(BUFSIZ); |
| 342 | if (buffer == NULL) { |
| 343 | LOGE("Failed to allocate buffer\n"); |
| 344 | return NULL; |
| 345 | } |
| 346 | |
| 347 | size_t read; |
| 348 | FILE* fin = fopen(original_path, "rb"); |
| 349 | if (fin == NULL) { |
| 350 | LOGE("Failed to open %s (%s)\n", original_path, strerror(errno)); |
| 351 | return NULL; |
| 352 | } |
| 353 | FILE* fout = fopen(copy_path, "wb"); |
| 354 | if (fout == NULL) { |
| 355 | LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno)); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) { |
| 360 | if (fwrite(buffer, 1, read, fout) != read) { |
| 361 | LOGE("Short write of %s (%s)\n", copy_path, strerror(errno)); |
| 362 | return NULL; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | free(buffer); |
| 367 | |
| 368 | if (fclose(fout) != 0) { |
| 369 | LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno)); |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | if (fclose(fin) != 0) { |
| 374 | LOGE("Failed to close %s (%s)\n", original_path, strerror(errno)); |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | // "adb push" is happy to overwrite read-only files when it's |
| 379 | // running as root, but we'll try anyway. |
| 380 | if (chmod(copy_path, 0400) != 0) { |
| 381 | LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno)); |
| 382 | return NULL; |
| 383 | } |
| 384 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 385 | return strdup(copy_path); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 388 | static char** |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 389 | prepend_title(const char** headers) { |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 390 | char* title[] = { "Android system recovery <" |
Doug Zongker | 64893cc | 2009-07-14 16:31:56 -0700 | [diff] [blame] | 391 | EXPAND(RECOVERY_API_VERSION) "e>", |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 392 | "", |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 393 | NULL }; |
| 394 | |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 395 | // count the number of lines in our title, plus the |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 396 | // caller-provided headers. |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 397 | int count = 0; |
| 398 | char** p; |
| 399 | for (p = title; *p; ++p, ++count); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 400 | for (p = headers; *p; ++p, ++count); |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 401 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 402 | char** new_headers = malloc((count+1) * sizeof(char*)); |
| 403 | char** h = new_headers; |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 404 | for (p = title; *p; ++p, ++h) *h = *p; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 405 | for (p = headers; *p; ++p, ++h) *h = *p; |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 406 | *h = NULL; |
| 407 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 408 | return new_headers; |
| 409 | } |
| 410 | |
| 411 | static int |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 412 | get_menu_selection(char** headers, char** items, int menu_only, |
| 413 | int initial_selection) { |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 414 | // throw away keys pressed previously, so user doesn't |
| 415 | // accidentally trigger menu items. |
| 416 | ui_clear_key_queue(); |
| 417 | |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 418 | ui_start_menu(headers, items, initial_selection); |
| 419 | int selected = initial_selection; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 420 | int chosen_item = -1; |
| 421 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 422 | while (chosen_item < 0) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 423 | int key = ui_wait_key(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 424 | int visible = ui_text_visible(); |
| 425 | |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 426 | int action = device_handle_key(key, visible); |
| 427 | |
| 428 | if (action < 0) { |
| 429 | switch (action) { |
| 430 | case HIGHLIGHT_UP: |
| 431 | --selected; |
| 432 | selected = ui_menu_select(selected); |
| 433 | break; |
| 434 | case HIGHLIGHT_DOWN: |
| 435 | ++selected; |
| 436 | selected = ui_menu_select(selected); |
| 437 | break; |
| 438 | case SELECT_ITEM: |
| 439 | chosen_item = selected; |
| 440 | break; |
| 441 | case NO_ACTION: |
| 442 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 443 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 444 | } else if (!menu_only) { |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 445 | chosen_item = action; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 446 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 447 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 448 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 449 | ui_end_menu(); |
| 450 | return chosen_item; |
| 451 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 452 | |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 453 | static int compare_string(const void* a, const void* b) { |
| 454 | return strcmp(*(const char**)a, *(const char**)b); |
| 455 | } |
| 456 | |
| 457 | static int |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 458 | sdcard_directory(const char* path) { |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 459 | const char* MENU_HEADERS[] = { "Choose a package to install:", |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 460 | path, |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 461 | "", |
| 462 | NULL }; |
| 463 | DIR* d; |
| 464 | struct dirent* de; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 465 | d = opendir(path); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 466 | if (d == NULL) { |
| 467 | LOGE("error opening %s: %s\n", path, strerror(errno)); |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | char** headers = prepend_title(MENU_HEADERS); |
| 472 | |
| 473 | int d_size = 0; |
| 474 | int d_alloc = 10; |
| 475 | char** dirs = malloc(d_alloc * sizeof(char*)); |
| 476 | int z_size = 1; |
| 477 | int z_alloc = 10; |
| 478 | char** zips = malloc(z_alloc * sizeof(char*)); |
| 479 | zips[0] = strdup("../"); |
| 480 | |
| 481 | while ((de = readdir(d)) != NULL) { |
| 482 | int name_len = strlen(de->d_name); |
| 483 | |
| 484 | if (de->d_type == DT_DIR) { |
| 485 | // skip "." and ".." entries |
| 486 | if (name_len == 1 && de->d_name[0] == '.') continue; |
| 487 | if (name_len == 2 && de->d_name[0] == '.' && |
| 488 | de->d_name[1] == '.') continue; |
| 489 | |
| 490 | if (d_size >= d_alloc) { |
| 491 | d_alloc *= 2; |
| 492 | dirs = realloc(dirs, d_alloc * sizeof(char*)); |
| 493 | } |
| 494 | dirs[d_size] = malloc(name_len + 2); |
| 495 | strcpy(dirs[d_size], de->d_name); |
| 496 | dirs[d_size][name_len] = '/'; |
| 497 | dirs[d_size][name_len+1] = '\0'; |
| 498 | ++d_size; |
| 499 | } else if (de->d_type == DT_REG && |
| 500 | name_len >= 4 && |
| 501 | strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) { |
| 502 | if (z_size >= z_alloc) { |
| 503 | z_alloc *= 2; |
| 504 | zips = realloc(zips, z_alloc * sizeof(char*)); |
| 505 | } |
| 506 | zips[z_size++] = strdup(de->d_name); |
| 507 | } |
| 508 | } |
| 509 | closedir(d); |
| 510 | |
| 511 | qsort(dirs, d_size, sizeof(char*), compare_string); |
| 512 | qsort(zips, z_size, sizeof(char*), compare_string); |
| 513 | |
| 514 | // append dirs to the zips list |
| 515 | if (d_size + z_size + 1 > z_alloc) { |
| 516 | z_alloc = d_size + z_size + 1; |
| 517 | zips = realloc(zips, z_alloc * sizeof(char*)); |
| 518 | } |
| 519 | memcpy(zips + z_size, dirs, d_size * sizeof(char*)); |
| 520 | free(dirs); |
| 521 | z_size += d_size; |
| 522 | zips[z_size] = NULL; |
| 523 | |
| 524 | int result; |
| 525 | int chosen_item = 0; |
| 526 | do { |
| 527 | chosen_item = get_menu_selection(headers, zips, 1, chosen_item); |
| 528 | |
| 529 | char* item = zips[chosen_item]; |
| 530 | int item_len = strlen(item); |
| 531 | if (chosen_item == 0) { // item 0 is always "../" |
| 532 | // go up but continue browsing (if the caller is sdcard_directory) |
| 533 | result = -1; |
| 534 | break; |
| 535 | } else if (item[item_len-1] == '/') { |
| 536 | // recurse down into a subdirectory |
| 537 | char new_path[PATH_MAX]; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 538 | strlcpy(new_path, path, PATH_MAX); |
| 539 | strlcat(new_path, "/", PATH_MAX); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 540 | strlcat(new_path, item, PATH_MAX); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 541 | new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/' |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 542 | result = sdcard_directory(new_path); |
| 543 | if (result >= 0) break; |
| 544 | } else { |
| 545 | // selected a zip file: attempt to install it, and return |
| 546 | // the status to the caller. |
| 547 | char new_path[PATH_MAX]; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 548 | strlcpy(new_path, path, PATH_MAX); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 549 | strlcat(new_path, item, PATH_MAX); |
| 550 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 551 | ui_print("\n-- Install %s ...\n", path); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 552 | set_sdcard_update_bootloader_message(); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 553 | char* copy = copy_sideloaded_package(new_path); |
| 554 | if (copy) { |
| 555 | result = install_package(copy); |
| 556 | free(copy); |
| 557 | } else { |
| 558 | result = INSTALL_ERROR; |
| 559 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 560 | break; |
| 561 | } |
| 562 | } while (true); |
| 563 | |
| 564 | int i; |
| 565 | for (i = 0; i < z_size; ++i) free(zips[i]); |
| 566 | free(zips); |
| 567 | free(headers); |
| 568 | |
| 569 | return result; |
| 570 | } |
| 571 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 572 | static void |
| 573 | wipe_data(int confirm) { |
| 574 | if (confirm) { |
| 575 | static char** title_headers = NULL; |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 576 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 577 | if (title_headers == NULL) { |
| 578 | char* headers[] = { "Confirm wipe of all user data?", |
| 579 | " THIS CAN NOT BE UNDONE.", |
| 580 | "", |
| 581 | NULL }; |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 582 | title_headers = prepend_title((const char**)headers); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 583 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 584 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 585 | char* items[] = { " No", |
| 586 | " No", |
| 587 | " No", |
| 588 | " No", |
| 589 | " No", |
| 590 | " No", |
| 591 | " No", |
| 592 | " Yes -- delete all user data", // [7] |
| 593 | " No", |
| 594 | " No", |
| 595 | " No", |
| 596 | NULL }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 597 | |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 598 | int chosen_item = get_menu_selection(title_headers, items, 1, 0); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 599 | if (chosen_item != 7) { |
| 600 | return; |
| 601 | } |
| 602 | } |
Doug Zongker | 1066d2c | 2009-04-01 13:57:40 -0700 | [diff] [blame] | 603 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 604 | ui_print("\n-- Wiping data...\n"); |
| 605 | device_wipe_data(); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 606 | erase_volume("/data"); |
| 607 | erase_volume("/cache"); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 608 | ui_print("Data wipe complete.\n"); |
| 609 | } |
| 610 | |
| 611 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 612 | prompt_and_wait() { |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 613 | char** headers = prepend_title((const char**)MENU_HEADERS); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 614 | |
| 615 | for (;;) { |
| 616 | finish_recovery(NULL); |
| 617 | ui_reset_progress(); |
| 618 | |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 619 | int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 620 | |
| 621 | // device-specific code may take some action here. It may |
| 622 | // return one of the core actions handled in the switch |
| 623 | // statement below. |
| 624 | chosen_item = device_perform_action(chosen_item); |
| 625 | |
| 626 | switch (chosen_item) { |
| 627 | case ITEM_REBOOT: |
| 628 | return; |
| 629 | |
| 630 | case ITEM_WIPE_DATA: |
| 631 | wipe_data(ui_text_visible()); |
| 632 | if (!ui_text_visible()) return; |
| 633 | break; |
| 634 | |
| 635 | case ITEM_WIPE_CACHE: |
| 636 | ui_print("\n-- Wiping cache...\n"); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 637 | erase_volume("/cache"); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 638 | ui_print("Cache wipe complete.\n"); |
| 639 | if (!ui_text_visible()) return; |
| 640 | break; |
| 641 | |
| 642 | case ITEM_APPLY_SDCARD: |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 643 | ; |
| 644 | int status = sdcard_directory(SDCARD_ROOT); |
| 645 | if (status >= 0) { |
| 646 | if (status != INSTALL_SUCCESS) { |
| 647 | ui_set_background(BACKGROUND_ICON_ERROR); |
| 648 | ui_print("Installation aborted.\n"); |
| 649 | } else if (!ui_text_visible()) { |
| 650 | return; // reboot if logs aren't visible |
| 651 | } else { |
| 652 | ui_print("\nInstall from sdcard complete.\n"); |
| 653 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 654 | } |
| 655 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 661 | print_property(const char *key, const char *name, void *cookie) { |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 662 | printf("%s=%s\n", key, name); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | int |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 666 | main(int argc, char **argv) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 667 | time_t start = time(NULL); |
| 668 | |
| 669 | // If these fail, there's not really anywhere to complain... |
| 670 | freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL); |
| 671 | freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL); |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 672 | printf("Starting recovery on %s", ctime(&start)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 673 | |
| 674 | ui_init(); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 675 | load_volume_table(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 676 | get_args(&argc, &argv); |
| 677 | |
| 678 | int previous_runs = 0; |
| 679 | const char *send_intent = NULL; |
| 680 | const char *update_package = NULL; |
Oscar Montemayor | 52219a6 | 2010-02-25 16:47:02 -0800 | [diff] [blame] | 681 | const char *encrypted_fs_mode = NULL; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 682 | int wipe_data = 0, wipe_cache = 0; |
Oscar Montemayor | 52219a6 | 2010-02-25 16:47:02 -0800 | [diff] [blame] | 683 | int toggle_secure_fs = 0; |
| 684 | encrypted_fs_info encrypted_fs_data; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 685 | |
| 686 | int arg; |
| 687 | while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) { |
| 688 | switch (arg) { |
| 689 | case 'p': previous_runs = atoi(optarg); break; |
| 690 | case 's': send_intent = optarg; break; |
| 691 | case 'u': update_package = optarg; break; |
| 692 | case 'w': wipe_data = wipe_cache = 1; break; |
| 693 | case 'c': wipe_cache = 1; break; |
Oscar Montemayor | 52219a6 | 2010-02-25 16:47:02 -0800 | [diff] [blame] | 694 | case 'e': encrypted_fs_mode = optarg; toggle_secure_fs = 1; break; |
Doug Zongker | 4bc9806 | 2010-09-03 11:00:13 -0700 | [diff] [blame] | 695 | case 't': ui_show_text(1); break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 696 | case '?': |
| 697 | LOGE("Invalid command argument\n"); |
| 698 | continue; |
| 699 | } |
| 700 | } |
| 701 | |
Doug Zongker | efa1bab | 2010-02-01 15:59:12 -0800 | [diff] [blame] | 702 | device_recovery_start(); |
| 703 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 704 | printf("Command:"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 705 | for (arg = 0; arg < argc; arg++) { |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 706 | printf(" \"%s\"", argv[arg]); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 707 | } |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 708 | printf("\n\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 709 | |
| 710 | property_list(print_property, NULL); |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 711 | printf("\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 712 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 713 | int status = INSTALL_SUCCESS; |
| 714 | |
Oscar Montemayor | 52219a6 | 2010-02-25 16:47:02 -0800 | [diff] [blame] | 715 | if (toggle_secure_fs) { |
| 716 | if (strcmp(encrypted_fs_mode,"on") == 0) { |
| 717 | encrypted_fs_data.mode = MODE_ENCRYPTED_FS_ENABLED; |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 718 | ui_print("Enabling Encrypted FS.\n"); |
Oscar Montemayor | 52219a6 | 2010-02-25 16:47:02 -0800 | [diff] [blame] | 719 | } else if (strcmp(encrypted_fs_mode,"off") == 0) { |
| 720 | encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED; |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 721 | ui_print("Disabling Encrypted FS.\n"); |
| 722 | } else { |
| 723 | ui_print("Error: invalid Encrypted FS setting.\n"); |
| 724 | status = INSTALL_ERROR; |
| 725 | } |
| 726 | |
| 727 | // Recovery strategy: if the data partition is damaged, disable encrypted file systems. |
| 728 | // This preventsthe device recycling endlessly in recovery mode. |
Oscar Montemayor | 52219a6 | 2010-02-25 16:47:02 -0800 | [diff] [blame] | 729 | if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) && |
| 730 | (read_encrypted_fs_info(&encrypted_fs_data))) { |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 731 | ui_print("Encrypted FS change aborted, resetting to disabled state.\n"); |
Oscar Montemayor | 52219a6 | 2010-02-25 16:47:02 -0800 | [diff] [blame] | 732 | encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED; |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | if (status != INSTALL_ERROR) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 736 | if (erase_volume("/data")) { |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 737 | ui_print("Data wipe failed.\n"); |
| 738 | status = INSTALL_ERROR; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 739 | } else if (erase_volume("/cache")) { |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 740 | ui_print("Cache wipe failed.\n"); |
| 741 | status = INSTALL_ERROR; |
Oscar Montemayor | 52219a6 | 2010-02-25 16:47:02 -0800 | [diff] [blame] | 742 | } else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) && |
| 743 | (restore_encrypted_fs_info(&encrypted_fs_data))) { |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 744 | ui_print("Encrypted FS change aborted.\n"); |
| 745 | status = INSTALL_ERROR; |
| 746 | } else { |
| 747 | ui_print("Successfully updated Encrypted FS.\n"); |
| 748 | status = INSTALL_SUCCESS; |
| 749 | } |
| 750 | } |
| 751 | } else if (update_package != NULL) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 752 | status = install_package(update_package); |
| 753 | if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n"); |
Doug Zongker | b128f54 | 2009-06-18 15:07:14 -0700 | [diff] [blame] | 754 | } else if (wipe_data) { |
| 755 | if (device_wipe_data()) status = INSTALL_ERROR; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 756 | if (erase_volume("/data")) status = INSTALL_ERROR; |
| 757 | if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 758 | if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n"); |
Doug Zongker | b128f54 | 2009-06-18 15:07:14 -0700 | [diff] [blame] | 759 | } else if (wipe_cache) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 760 | if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; |
Doug Zongker | b128f54 | 2009-06-18 15:07:14 -0700 | [diff] [blame] | 761 | 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] | 762 | } else { |
| 763 | status = INSTALL_ERROR; // No command specified |
| 764 | } |
| 765 | |
| 766 | if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 767 | if (status != INSTALL_SUCCESS || ui_text_visible()) { |
| 768 | // Mount the sdcard when the menu is enabled so you can "adb |
| 769 | // push" packages to the sdcard and immediately install them. |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame^] | 770 | ensure_path_mounted(SDCARD_ROOT); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 771 | prompt_and_wait(); |
| 772 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 773 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 774 | // Otherwise, get ready to boot the main system... |
| 775 | finish_recovery(send_intent); |
| 776 | ui_print("Rebooting...\n"); |
| 777 | sync(); |
| 778 | reboot(RB_AUTOBOOT); |
| 779 | return EXIT_SUCCESS; |
| 780 | } |