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